staging:iio:meter:ade7753 should be 16 bit read not 8 bit for mode register.
[pandora-kernel.git] / drivers / staging / rtl8187se / ieee80211 / ieee80211_softmac.c
1 /* IEEE 802.11 SoftMAC layer
2  * Copyright (c) 2005 Andrea Merello <andreamrl@tiscali.it>
3  *
4  * Mostly extracted from the rtl8180-sa2400 driver for the
5  * in-kernel generic ieee802.11 stack.
6  *
7  * Few lines might be stolen from other part of the ieee80211
8  * stack. Copyright who own it's copyright
9  *
10  * WPA code stolen from the ipw2200 driver.
11  * Copyright who own it's copyright.
12  *
13  * released under the GPL
14  */
15
16
17 #include "ieee80211.h"
18
19 #include <linux/random.h>
20 #include <linux/delay.h>
21 #include <linux/slab.h>
22 #include <asm/uaccess.h>
23
24 #include "dot11d.h"
25 u8 rsn_authen_cipher_suite[16][4] = {
26         {0x00,0x0F,0xAC,0x00}, //Use group key, //Reserved
27         {0x00,0x0F,0xAC,0x01}, //WEP-40         //RSNA default
28         {0x00,0x0F,0xAC,0x02}, //TKIP           //NONE          //{used just as default}
29         {0x00,0x0F,0xAC,0x03}, //WRAP-historical
30         {0x00,0x0F,0xAC,0x04}, //CCMP
31         {0x00,0x0F,0xAC,0x05}, //WEP-104
32 };
33
34 short ieee80211_is_54g(struct ieee80211_network net)
35 {
36         return ((net.rates_ex_len > 0) || (net.rates_len > 4));
37 }
38
39 short ieee80211_is_shortslot(struct ieee80211_network net)
40 {
41         return (net.capability & WLAN_CAPABILITY_SHORT_SLOT);
42 }
43
44 /* returns the total length needed for pleacing the RATE MFIE
45  * tag and the EXTENDED RATE MFIE tag if needed.
46  * It encludes two bytes per tag for the tag itself and its len
47  */
48 unsigned int ieee80211_MFIE_rate_len(struct ieee80211_device *ieee)
49 {
50         unsigned int rate_len = 0;
51
52         if (ieee->modulation & IEEE80211_CCK_MODULATION)
53                 rate_len = IEEE80211_CCK_RATE_LEN + 2;
54
55         if (ieee->modulation & IEEE80211_OFDM_MODULATION)
56
57                 rate_len += IEEE80211_OFDM_RATE_LEN + 2;
58
59         return rate_len;
60 }
61
62 /* pleace the MFIE rate, tag to the memory (double) poined.
63  * Then it updates the pointer so that
64  * it points after the new MFIE tag added.
65  */
66 void ieee80211_MFIE_Brate(struct ieee80211_device *ieee, u8 **tag_p)
67 {
68         u8 *tag = *tag_p;
69
70         if (ieee->modulation & IEEE80211_CCK_MODULATION){
71                 *tag++ = MFIE_TYPE_RATES;
72                 *tag++ = 4;
73                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
74                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
75                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
76                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
77         }
78
79         /* We may add an option for custom rates that specific HW might support */
80         *tag_p = tag;
81 }
82
83 void ieee80211_MFIE_Grate(struct ieee80211_device *ieee, u8 **tag_p)
84 {
85         u8 *tag = *tag_p;
86
87                 if (ieee->modulation & IEEE80211_OFDM_MODULATION){
88
89                 *tag++ = MFIE_TYPE_RATES_EX;
90                 *tag++ = 8;
91                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_6MB;
92                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_9MB;
93                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_12MB;
94                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_18MB;
95                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
96                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_36MB;
97                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_48MB;
98                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_54MB;
99
100         }
101
102         /* We may add an option for custom rates that specific HW might support */
103         *tag_p = tag;
104 }
105
106
107 void ieee80211_WMM_Info(struct ieee80211_device *ieee, u8 **tag_p) {
108         u8 *tag = *tag_p;
109
110         *tag++ = MFIE_TYPE_GENERIC; //0
111         *tag++ = 7;
112         *tag++ = 0x00;
113         *tag++ = 0x50;
114         *tag++ = 0xf2;
115         *tag++ = 0x02;//5
116         *tag++ = 0x00;
117         *tag++ = 0x01;
118 #ifdef SUPPORT_USPD
119         if(ieee->current_network.wmm_info & 0x80) {
120                 *tag++ = 0x0f|MAX_SP_Len;
121         } else {
122                 *tag++ = MAX_SP_Len;
123         }
124 #else
125         *tag++ = MAX_SP_Len;
126 #endif
127         *tag_p = tag;
128 }
129
130 void ieee80211_TURBO_Info(struct ieee80211_device *ieee, u8 **tag_p) {
131         u8 *tag = *tag_p;
132
133         *tag++ = MFIE_TYPE_GENERIC; //0
134         *tag++ = 7;
135         *tag++ = 0x00;
136         *tag++ = 0xe0;
137         *tag++ = 0x4c;
138         *tag++ = 0x01;//5
139         *tag++ = 0x02;
140         *tag++ = 0x11;
141         *tag++ = 0x00;
142
143         *tag_p = tag;
144         printk(KERN_ALERT "This is enable turbo mode IE process\n");
145 }
146
147 void enqueue_mgmt(struct ieee80211_device *ieee, struct sk_buff *skb)
148 {
149         int nh;
150         nh = (ieee->mgmt_queue_head +1) % MGMT_QUEUE_NUM;
151
152 /*
153  * if the queue is full but we have newer frames then
154  * just overwrites the oldest.
155  *
156  * if (nh == ieee->mgmt_queue_tail)
157  *              return -1;
158  */
159         ieee->mgmt_queue_head = nh;
160         ieee->mgmt_queue_ring[nh] = skb;
161
162         //return 0;
163 }
164
165 struct sk_buff *dequeue_mgmt(struct ieee80211_device *ieee)
166 {
167         struct sk_buff *ret;
168
169         if(ieee->mgmt_queue_tail == ieee->mgmt_queue_head)
170                 return NULL;
171
172         ret = ieee->mgmt_queue_ring[ieee->mgmt_queue_tail];
173
174         ieee->mgmt_queue_tail =
175                 (ieee->mgmt_queue_tail+1) % MGMT_QUEUE_NUM;
176
177         return ret;
178 }
179
180 void init_mgmt_queue(struct ieee80211_device *ieee)
181 {
182         ieee->mgmt_queue_tail = ieee->mgmt_queue_head = 0;
183 }
184
185
186 void ieee80211_sta_wakeup(struct ieee80211_device *ieee, short nl);
187
188 inline void softmac_mgmt_xmit(struct sk_buff *skb, struct ieee80211_device *ieee)
189 {
190         unsigned long flags;
191         short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
192         struct ieee80211_hdr_3addr  *header=
193                 (struct ieee80211_hdr_3addr  *) skb->data;
194
195
196         spin_lock_irqsave(&ieee->lock, flags);
197
198         /* called with 2nd param 0, no mgmt lock required */
199         ieee80211_sta_wakeup(ieee,0);
200
201         if(single){
202                 if(ieee->queue_stop){
203
204                         enqueue_mgmt(ieee,skb);
205                 }else{
206                         header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0]<<4);
207
208                         if (ieee->seq_ctrl[0] == 0xFFF)
209                                 ieee->seq_ctrl[0] = 0;
210                         else
211                                 ieee->seq_ctrl[0]++;
212
213                         /* avoid watchdog triggers */
214                         ieee->dev->trans_start = jiffies;
215                         ieee->softmac_data_hard_start_xmit(skb,ieee->dev,ieee->basic_rate);
216                 }
217
218                 spin_unlock_irqrestore(&ieee->lock, flags);
219         }else{
220                 spin_unlock_irqrestore(&ieee->lock, flags);
221                 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags);
222
223                 header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
224
225                 if (ieee->seq_ctrl[0] == 0xFFF)
226                         ieee->seq_ctrl[0] = 0;
227                 else
228                         ieee->seq_ctrl[0]++;
229
230                 /* avoid watchdog triggers */
231                 ieee->dev->trans_start = jiffies;
232                 ieee->softmac_hard_start_xmit(skb,ieee->dev);
233
234                 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags);
235         }
236 }
237
238
239 inline void softmac_ps_mgmt_xmit(struct sk_buff *skb, struct ieee80211_device *ieee)
240 {
241
242         short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
243         struct ieee80211_hdr_3addr  *header =
244                 (struct ieee80211_hdr_3addr  *) skb->data;
245
246
247         if(single){
248
249                 header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
250
251                 if (ieee->seq_ctrl[0] == 0xFFF)
252                         ieee->seq_ctrl[0] = 0;
253                 else
254                         ieee->seq_ctrl[0]++;
255
256                 /* avoid watchdog triggers */
257                 ieee->dev->trans_start = jiffies;
258                 ieee->softmac_data_hard_start_xmit(skb,ieee->dev,ieee->basic_rate);
259
260         }else{
261
262                 header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
263
264                 if (ieee->seq_ctrl[0] == 0xFFF)
265                         ieee->seq_ctrl[0] = 0;
266                 else
267                         ieee->seq_ctrl[0]++;
268
269                 /* avoid watchdog triggers */
270                 ieee->dev->trans_start = jiffies;
271                 ieee->softmac_hard_start_xmit(skb,ieee->dev);
272
273         }
274 //      dev_kfree_skb_any(skb);//edit by thomas
275 }
276 //by amy for power save
277 inline struct sk_buff *ieee80211_disassociate_skb(
278                                                         struct ieee80211_network *beacon,
279                                                         struct ieee80211_device *ieee,
280                                                         u8      asRsn)
281 {
282         struct sk_buff *skb;
283         struct ieee80211_disassoc_frame *disass;
284
285         skb = dev_alloc_skb(sizeof(struct ieee80211_disassoc_frame));
286         if (!skb)
287                 return NULL;
288
289         disass = (struct ieee80211_disassoc_frame *) skb_put(skb,sizeof(struct ieee80211_disassoc_frame));
290         disass->header.frame_control = cpu_to_le16(IEEE80211_STYPE_DISASSOC);
291         disass->header.duration_id = 0;
292
293         memcpy(disass->header.addr1, beacon->bssid, ETH_ALEN);
294         memcpy(disass->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
295         memcpy(disass->header.addr3, beacon->bssid, ETH_ALEN);
296
297         disass->reasoncode = asRsn;
298         return skb;
299 }
300 void
301 SendDisassociation(
302         struct ieee80211_device *ieee,
303         u8*                     asSta,
304         u8                      asRsn
305 )
306 {
307         struct ieee80211_network *beacon = &ieee->current_network;
308         struct sk_buff *skb;
309         skb = ieee80211_disassociate_skb(beacon,ieee,asRsn);
310         if (skb){
311                 softmac_mgmt_xmit(skb, ieee);
312                 //dev_kfree_skb_any(skb);//edit by thomas
313         }
314 }
315
316 //by amy for power save
317 inline struct sk_buff *ieee80211_probe_req(struct ieee80211_device *ieee)
318 {
319         unsigned int len,rate_len;
320         u8 *tag;
321         struct sk_buff *skb;
322         struct ieee80211_probe_request *req;
323
324         len = ieee->current_network.ssid_len;
325
326         rate_len = ieee80211_MFIE_rate_len(ieee);
327
328         skb = dev_alloc_skb(sizeof(struct ieee80211_probe_request) +
329                             2 + len + rate_len);
330         if (!skb)
331                 return NULL;
332
333         req = (struct ieee80211_probe_request *) skb_put(skb,sizeof(struct ieee80211_probe_request));
334         req->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
335         req->header.duration_id = 0; //FIXME: is this OK ?
336
337         memset(req->header.addr1, 0xff, ETH_ALEN);
338         memcpy(req->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
339         memset(req->header.addr3, 0xff, ETH_ALEN);
340
341         tag = (u8 *) skb_put(skb,len+2+rate_len);
342
343         *tag++ = MFIE_TYPE_SSID;
344         *tag++ = len;
345         memcpy(tag, ieee->current_network.ssid, len);
346         tag += len;
347         ieee80211_MFIE_Brate(ieee,&tag);
348         ieee80211_MFIE_Grate(ieee,&tag);
349
350         return skb;
351 }
352
353 struct sk_buff *ieee80211_get_beacon_(struct ieee80211_device *ieee);
354
355 void ext_ieee80211_send_beacon_wq(struct ieee80211_device *ieee)
356 {
357         struct sk_buff *skb;
358
359         //unsigned long flags;
360
361         skb = ieee80211_get_beacon_(ieee);
362
363         if (skb){
364                 softmac_mgmt_xmit(skb, ieee);
365                 ieee->softmac_stats.tx_beacons++;
366                 dev_kfree_skb_any(skb);//edit by thomas
367         }
368
369
370         //printk(KERN_WARNING "[1] beacon sending!\n");
371         ieee->beacon_timer.expires = jiffies +
372                 (MSECS( ieee->current_network.beacon_interval -5));
373
374         //spin_lock_irqsave(&ieee->beacon_lock,flags);
375         if(ieee->beacon_txing)
376                 add_timer(&ieee->beacon_timer);
377         //spin_unlock_irqrestore(&ieee->beacon_lock,flags);
378 }
379
380 void ieee80211_send_beacon(struct ieee80211_device *ieee)
381 {
382         struct sk_buff *skb;
383
384         //unsigned long flags;
385
386         skb = ieee80211_get_beacon_(ieee);
387
388         if (skb){
389                 softmac_mgmt_xmit(skb, ieee);
390                 ieee->softmac_stats.tx_beacons++;
391                 dev_kfree_skb_any(skb);//edit by thomas
392         }
393
394         //printk(KERN_WARNING "[1] beacon sending!\n");
395         ieee->beacon_timer.expires = jiffies +
396                 (MSECS( ieee->current_network.beacon_interval -5));
397
398         //spin_lock_irqsave(&ieee->beacon_lock,flags);
399         if(ieee->beacon_txing)
400                 add_timer(&ieee->beacon_timer);
401         //spin_unlock_irqrestore(&ieee->beacon_lock,flags);
402 }
403
404
405 void ieee80211_send_beacon_cb(unsigned long _ieee)
406 {
407         struct ieee80211_device *ieee =
408                 (struct ieee80211_device *) _ieee;
409         unsigned long flags;
410
411         spin_lock_irqsave(&ieee->beacon_lock, flags);
412         ieee80211_send_beacon(ieee);
413         spin_unlock_irqrestore(&ieee->beacon_lock, flags);
414 }
415
416 void ieee80211_send_probe(struct ieee80211_device *ieee)
417 {
418         struct sk_buff *skb;
419
420         skb = ieee80211_probe_req(ieee);
421         if (skb){
422                 softmac_mgmt_xmit(skb, ieee);
423                 ieee->softmac_stats.tx_probe_rq++;
424                 //dev_kfree_skb_any(skb);//edit by thomas
425         }
426 }
427
428 void ieee80211_send_probe_requests(struct ieee80211_device *ieee)
429 {
430         if (ieee->active_scan && (ieee->softmac_features & IEEE_SOFTMAC_PROBERQ)){
431                 ieee80211_send_probe(ieee);
432                 ieee80211_send_probe(ieee);
433         }
434 }
435
436 /* this performs syncro scan blocking the caller until all channels
437  * in the allowed channel map has been checked.
438  */
439 void ieee80211_softmac_scan_syncro(struct ieee80211_device *ieee)
440 {
441         short ch = 0;
442         u8 channel_map[MAX_CHANNEL_NUMBER+1];
443         memcpy(channel_map, GET_DOT11D_INFO(ieee)->channel_map, MAX_CHANNEL_NUMBER+1);
444         down(&ieee->scan_sem);
445 //      printk("==================> Sync scan\n");
446
447         while(1)
448         {
449
450                 do{
451                         ch++;
452                         if (ch > MAX_CHANNEL_NUMBER)
453                                 goto out; /* scan completed */
454
455                 }while(!channel_map[ch]);
456                 /* this function can be called in two situations
457                  * 1- We have switched to ad-hoc mode and we are
458                  *    performing a complete syncro scan before conclude
459                  *    there are no interesting cell and to create a
460                  *    new one. In this case the link state is
461                  *    IEEE80211_NOLINK until we found an interesting cell.
462                  *    If so the ieee8021_new_net, called by the RX path
463                  *    will set the state to IEEE80211_LINKED, so we stop
464                  *    scanning
465                  * 2- We are linked and the root uses run iwlist scan.
466                  *    So we switch to IEEE80211_LINKED_SCANNING to remember
467                  *    that we are still logically linked (not interested in
468                  *    new network events, despite for updating the net list,
469                  *    but we are temporarly 'unlinked' as the driver shall
470                  *    not filter RX frames and the channel is changing.
471                  * So the only situation in witch are interested is to check
472                  * if the state become LINKED because of the #1 situation
473                  */
474
475                 if (ieee->state == IEEE80211_LINKED)
476                         goto out;
477
478                 ieee->set_chan(ieee->dev, ch);
479 //              printk("=====>channel=%d   ",ch);
480                 if(channel_map[ch] == 1)
481                 {
482 //                      printk("====send probe request\n");
483                         ieee80211_send_probe_requests(ieee);
484                 }
485                 /* this prevent excessive time wait when we
486                  * need to wait for a syncro scan to end..
487                  */
488                 if (ieee->sync_scan_hurryup)
489                         goto out;
490
491
492                 msleep_interruptible_rtl(IEEE80211_SOFTMAC_SCAN_TIME);
493
494         }
495 out:
496         ieee->sync_scan_hurryup = 0;
497         up(&ieee->scan_sem);
498         if(IS_DOT11D_ENABLE(ieee))
499                 DOT11D_ScanComplete(ieee);
500 }
501
502 void ieee80211_softmac_ips_scan_syncro(struct ieee80211_device *ieee)
503 {
504         int ch;
505         unsigned int watch_dog = 0;
506         u8 channel_map[MAX_CHANNEL_NUMBER+1];
507         memcpy(channel_map, GET_DOT11D_INFO(ieee)->channel_map, MAX_CHANNEL_NUMBER+1);
508         down(&ieee->scan_sem);
509         ch = ieee->current_network.channel;
510 //              if(ieee->sync_scan_hurryup)
511 //      {
512
513 //              printk("stop scan sync\n");
514 //              goto out;
515 //      }
516 //      printk("=======hh===============>ips scan\n");
517         while(1)
518         {
519                 /* this function can be called in two situations
520                  * 1- We have switched to ad-hoc mode and we are
521                  *    performing a complete syncro scan before conclude
522                  *    there are no interesting cell and to create a
523                  *    new one. In this case the link state is
524                  *    IEEE80211_NOLINK until we found an interesting cell.
525                  *    If so the ieee8021_new_net, called by the RX path
526                  *    will set the state to IEEE80211_LINKED, so we stop
527                  *    scanning
528                  * 2- We are linked and the root uses run iwlist scan.
529                  *    So we switch to IEEE80211_LINKED_SCANNING to remember
530                  *    that we are still logically linked (not interested in
531                  *    new network events, despite for updating the net list,
532                  *    but we are temporarly 'unlinked' as the driver shall
533                  *    not filter RX frames and the channel is changing.
534                  * So the only situation in witch are interested is to check
535                  * if the state become LINKED because of the #1 situation
536                  */
537                 if (ieee->state == IEEE80211_LINKED)
538                 {
539                         goto out;
540                 }
541                 if(channel_map[ieee->current_network.channel] > 0)
542                 {
543                         ieee->set_chan(ieee->dev, ieee->current_network.channel);
544 //                      printk("======>channel=%d  ",ieee->current_network.channel);
545                 }
546                 if(channel_map[ieee->current_network.channel] == 1)
547                 {
548 //                      printk("====send probe request\n");
549                         ieee80211_send_probe_requests(ieee);
550                 }
551                 /* this prevent excessive time wait when we
552                  * need to wait for a syncro scan to end..
553                  */
554 //                if (ieee->sync_scan_hurryup)
555 //                        goto out;
556
557                 msleep_interruptible_rtl(IEEE80211_SOFTMAC_SCAN_TIME);
558
559                 do{
560                         if (watch_dog++ >= MAX_CHANNEL_NUMBER)
561                 //      if (++watch_dog >= 15);//MAX_CHANNEL_NUMBER)  //YJ,modified,080630
562                                 goto out; /* scan completed */
563
564                         ieee->current_network.channel = (ieee->current_network.channel + 1)%MAX_CHANNEL_NUMBER;
565                 }while(!channel_map[ieee->current_network.channel]);
566         }
567 out:
568         //ieee->sync_scan_hurryup = 0;
569         //ieee->set_chan(ieee->dev, ch);
570         //ieee->current_network.channel = ch;
571         ieee->actscanning = false;
572         up(&ieee->scan_sem);
573         if(IS_DOT11D_ENABLE(ieee))
574                 DOT11D_ScanComplete(ieee);
575 }
576
577 void ieee80211_softmac_scan_wq(struct work_struct *work)
578 {
579         struct delayed_work *dwork = to_delayed_work(work);
580         struct ieee80211_device *ieee = container_of(dwork, struct ieee80211_device, softmac_scan_wq);
581         static short watchdog = 0;
582         u8 channel_map[MAX_CHANNEL_NUMBER+1];
583         memcpy(channel_map, GET_DOT11D_INFO(ieee)->channel_map, MAX_CHANNEL_NUMBER+1);
584 //      printk("ieee80211_softmac_scan_wq ENABLE_IPS\n");
585 //      printk("in %s\n",__func__);
586         down(&ieee->scan_sem);
587
588         do{
589                 ieee->current_network.channel =
590                         (ieee->current_network.channel + 1) % MAX_CHANNEL_NUMBER;
591                 if (watchdog++ > MAX_CHANNEL_NUMBER)
592                                 goto out; /* no good chans */
593
594         }while(!channel_map[ieee->current_network.channel]);
595
596         //printk("current_network.channel:%d\n", ieee->current_network.channel);
597         if (ieee->scanning == 0 )
598         {
599                 printk("error out, scanning = 0\n");
600                 goto out;
601         }
602         ieee->set_chan(ieee->dev, ieee->current_network.channel);
603         if(channel_map[ieee->current_network.channel] == 1)
604                 ieee80211_send_probe_requests(ieee);
605
606         queue_delayed_work(ieee->wq, &ieee->softmac_scan_wq, IEEE80211_SOFTMAC_SCAN_TIME);
607         up(&ieee->scan_sem);
608         return;
609 out:
610         ieee->actscanning = false;
611         watchdog = 0;
612         ieee->scanning = 0;
613         up(&ieee->scan_sem);
614
615         if(IS_DOT11D_ENABLE(ieee))
616                 DOT11D_ScanComplete(ieee);
617         return;
618 }
619
620 void ieee80211_beacons_start(struct ieee80211_device *ieee)
621 {
622         unsigned long flags;
623
624         spin_lock_irqsave(&ieee->beacon_lock,flags);
625
626         ieee->beacon_txing = 1;
627         ieee80211_send_beacon(ieee);
628
629         spin_unlock_irqrestore(&ieee->beacon_lock,flags);
630 }
631
632 void ieee80211_beacons_stop(struct ieee80211_device *ieee)
633 {
634         unsigned long flags;
635
636         spin_lock_irqsave(&ieee->beacon_lock,flags);
637
638         ieee->beacon_txing = 0;
639         del_timer_sync(&ieee->beacon_timer);
640
641         spin_unlock_irqrestore(&ieee->beacon_lock,flags);
642
643 }
644
645
646 void ieee80211_stop_send_beacons(struct ieee80211_device *ieee)
647 {
648         if(ieee->stop_send_beacons)
649                 ieee->stop_send_beacons(ieee->dev);
650         if (ieee->softmac_features & IEEE_SOFTMAC_BEACONS)
651                 ieee80211_beacons_stop(ieee);
652 }
653
654
655 void ieee80211_start_send_beacons(struct ieee80211_device *ieee)
656 {
657         if(ieee->start_send_beacons)
658                 ieee->start_send_beacons(ieee->dev);
659         if(ieee->softmac_features & IEEE_SOFTMAC_BEACONS)
660                 ieee80211_beacons_start(ieee);
661 }
662
663
664 void ieee80211_softmac_stop_scan(struct ieee80211_device *ieee)
665 {
666 //      unsigned long flags;
667
668         //ieee->sync_scan_hurryup = 1;
669
670         down(&ieee->scan_sem);
671 //      spin_lock_irqsave(&ieee->lock, flags);
672
673         if (ieee->scanning == 1){
674                 ieee->scanning = 0;
675                 //del_timer_sync(&ieee->scan_timer);
676                 cancel_delayed_work(&ieee->softmac_scan_wq);
677         }
678
679 //      spin_unlock_irqrestore(&ieee->lock, flags);
680         up(&ieee->scan_sem);
681 }
682
683 void ieee80211_stop_scan(struct ieee80211_device *ieee)
684 {
685         if (ieee->softmac_features & IEEE_SOFTMAC_SCAN)
686                 ieee80211_softmac_stop_scan(ieee);
687         else
688                 ieee->stop_scan(ieee->dev);
689 }
690
691 /* called with ieee->lock held */
692 void ieee80211_rtl_start_scan(struct ieee80211_device *ieee)
693 {
694         if(IS_DOT11D_ENABLE(ieee) )
695         {
696                 if(IS_COUNTRY_IE_VALID(ieee))
697                 {
698                         RESET_CIE_WATCHDOG(ieee);
699                 }
700         }
701         if (ieee->softmac_features & IEEE_SOFTMAC_SCAN){
702                 if (ieee->scanning == 0)
703                 {
704                         ieee->scanning = 1;
705                         //ieee80211_softmac_scan(ieee);
706                 //      queue_work(ieee->wq, &ieee->softmac_scan_wq);
707                 //care this,1203,2007,by lawrence
708 #if 1
709                         queue_delayed_work(ieee->wq, &ieee->softmac_scan_wq,0);
710 #endif
711                 }
712         }else
713                 ieee->start_scan(ieee->dev);
714
715 }
716
717 /* called with wx_sem held */
718 void ieee80211_start_scan_syncro(struct ieee80211_device *ieee)
719 {
720         if(IS_DOT11D_ENABLE(ieee) )
721         {
722                 if(IS_COUNTRY_IE_VALID(ieee))
723                 {
724                         RESET_CIE_WATCHDOG(ieee);
725                 }
726         }
727         ieee->sync_scan_hurryup = 0;
728
729         if (ieee->softmac_features & IEEE_SOFTMAC_SCAN)
730                 ieee80211_softmac_scan_syncro(ieee);
731         else
732                 ieee->scan_syncro(ieee->dev);
733
734 }
735
736 inline struct sk_buff *ieee80211_authentication_req(struct ieee80211_network *beacon,
737         struct ieee80211_device *ieee, int challengelen)
738 {
739         struct sk_buff *skb;
740         struct ieee80211_authentication *auth;
741
742         skb = dev_alloc_skb(sizeof(struct ieee80211_authentication) + challengelen);
743
744         if (!skb) return NULL;
745
746         auth = (struct ieee80211_authentication *)
747                 skb_put(skb, sizeof(struct ieee80211_authentication));
748
749         auth->header.frame_ctl = IEEE80211_STYPE_AUTH;
750         if (challengelen) auth->header.frame_ctl |= IEEE80211_FCTL_WEP;
751
752         auth->header.duration_id = 0x013a; //FIXME
753
754         memcpy(auth->header.addr1, beacon->bssid, ETH_ALEN);
755         memcpy(auth->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
756         memcpy(auth->header.addr3, beacon->bssid, ETH_ALEN);
757
758         auth->algorithm = ieee->open_wep ? WLAN_AUTH_OPEN : WLAN_AUTH_SHARED_KEY;
759
760         auth->transaction = cpu_to_le16(ieee->associate_seq);
761         ieee->associate_seq++;
762
763         auth->status = cpu_to_le16(WLAN_STATUS_SUCCESS);
764
765         return skb;
766
767 }
768
769 static struct sk_buff* ieee80211_probe_resp(struct ieee80211_device *ieee, u8 *dest)
770 {
771         u8 *tag;
772         int beacon_size;
773         struct ieee80211_probe_response *beacon_buf;
774         struct sk_buff *skb;
775         int encrypt;
776         int atim_len,erp_len;
777         struct ieee80211_crypt_data* crypt;
778
779         char *ssid = ieee->current_network.ssid;
780         int ssid_len = ieee->current_network.ssid_len;
781         int rate_len = ieee->current_network.rates_len+2;
782         int rate_ex_len = ieee->current_network.rates_ex_len;
783         int wpa_ie_len = ieee->wpa_ie_len;
784         if(rate_ex_len > 0) rate_ex_len+=2;
785
786         if(ieee->current_network.capability & WLAN_CAPABILITY_IBSS)
787                 atim_len = 4;
788         else
789                 atim_len = 0;
790
791         if(ieee80211_is_54g(ieee->current_network))
792                 erp_len = 3;
793         else
794                 erp_len = 0;
795
796         beacon_size = sizeof(struct ieee80211_probe_response)+
797                 ssid_len
798                 +3 //channel
799                 +rate_len
800                 +rate_ex_len
801                 +atim_len
802                 +wpa_ie_len
803                 +erp_len;
804
805         skb = dev_alloc_skb(beacon_size);
806
807         if (!skb)
808                 return NULL;
809
810         beacon_buf = (struct ieee80211_probe_response*) skb_put(skb, beacon_size);
811
812         memcpy (beacon_buf->header.addr1, dest,ETH_ALEN);
813         memcpy (beacon_buf->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
814         memcpy (beacon_buf->header.addr3, ieee->current_network.bssid, ETH_ALEN);
815
816         beacon_buf->header.duration_id = 0; //FIXME
817         beacon_buf->beacon_interval =
818                 cpu_to_le16(ieee->current_network.beacon_interval);
819         beacon_buf->capability =
820                 cpu_to_le16(ieee->current_network.capability & WLAN_CAPABILITY_IBSS);
821
822         if(ieee->short_slot && (ieee->current_network.capability & WLAN_CAPABILITY_SHORT_SLOT))
823                 beacon_buf->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT);
824
825         crypt = ieee->crypt[ieee->tx_keyidx];
826
827         encrypt = ieee->host_encrypt && crypt && crypt->ops &&
828                 ((0 == strcmp(crypt->ops->name, "WEP")) || wpa_ie_len);
829
830         if (encrypt)
831                 beacon_buf->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
832
833
834         beacon_buf->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_RESP);
835
836         beacon_buf->info_element.id = MFIE_TYPE_SSID;
837         beacon_buf->info_element.len = ssid_len;
838
839         tag = (u8*) beacon_buf->info_element.data;
840
841         memcpy(tag, ssid, ssid_len);
842
843         tag += ssid_len;
844
845         *(tag++) = MFIE_TYPE_RATES;
846         *(tag++) = rate_len-2;
847         memcpy(tag,ieee->current_network.rates,rate_len-2);
848         tag+=rate_len-2;
849
850         *(tag++) = MFIE_TYPE_DS_SET;
851         *(tag++) = 1;
852         *(tag++) = ieee->current_network.channel;
853
854         if(atim_len){
855                 *(tag++) = MFIE_TYPE_IBSS_SET;
856                 *(tag++) = 2;
857                 *((u16*)(tag)) = cpu_to_le16(ieee->current_network.atim_window);
858                 tag+=2;
859         }
860
861         if(erp_len){
862                 *(tag++) = MFIE_TYPE_ERP;
863                 *(tag++) = 1;
864                 *(tag++) = 0;
865         }
866
867         if(rate_ex_len){
868                 *(tag++) = MFIE_TYPE_RATES_EX;
869                 *(tag++) = rate_ex_len-2;
870                 memcpy(tag,ieee->current_network.rates_ex,rate_ex_len-2);
871                 tag+=rate_ex_len-2;
872         }
873
874         if (wpa_ie_len)
875         {
876                 if (ieee->iw_mode == IW_MODE_ADHOC)
877                 {//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
878                         memcpy(&ieee->wpa_ie[14], &ieee->wpa_ie[8], 4);
879                 }
880
881                 memcpy(tag, ieee->wpa_ie, ieee->wpa_ie_len);
882         }
883
884         skb->dev = ieee->dev;
885         return skb;
886 }
887
888 struct sk_buff* ieee80211_assoc_resp(struct ieee80211_device *ieee, u8 *dest)
889 {
890         struct sk_buff *skb;
891         u8* tag;
892
893         struct ieee80211_crypt_data* crypt;
894         struct ieee80211_assoc_response_frame *assoc;
895         short encrypt;
896
897         unsigned int rate_len = ieee80211_MFIE_rate_len(ieee);
898         int len = sizeof(struct ieee80211_assoc_response_frame) + rate_len;
899
900         skb = dev_alloc_skb(len);
901
902         if (!skb)
903                 return NULL;
904
905         assoc = (struct ieee80211_assoc_response_frame *)
906                 skb_put(skb,sizeof(struct ieee80211_assoc_response_frame));
907
908         assoc->header.frame_control = cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP);
909         memcpy(assoc->header.addr1, dest,ETH_ALEN);
910         memcpy(assoc->header.addr3, ieee->dev->dev_addr, ETH_ALEN);
911         memcpy(assoc->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
912         assoc->capability = cpu_to_le16(ieee->iw_mode == IW_MODE_MASTER ?
913                 WLAN_CAPABILITY_BSS : WLAN_CAPABILITY_IBSS);
914
915
916         if(ieee->short_slot)
917                 assoc->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT);
918
919         if (ieee->host_encrypt)
920                 crypt = ieee->crypt[ieee->tx_keyidx];
921         else crypt = NULL;
922
923         encrypt = ( crypt && crypt->ops);
924
925         if (encrypt)
926                 assoc->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
927
928         assoc->status = 0;
929         assoc->aid = cpu_to_le16(ieee->assoc_id);
930         if (ieee->assoc_id == 0x2007) ieee->assoc_id=0;
931         else ieee->assoc_id++;
932
933         tag = (u8*) skb_put(skb, rate_len);
934
935         ieee80211_MFIE_Brate(ieee, &tag);
936         ieee80211_MFIE_Grate(ieee, &tag);
937
938         return skb;
939 }
940
941 struct sk_buff* ieee80211_auth_resp(struct ieee80211_device *ieee,int status, u8 *dest)
942 {
943         struct sk_buff *skb;
944         struct ieee80211_authentication *auth;
945
946         skb = dev_alloc_skb(sizeof(struct ieee80211_authentication)+1);
947
948         if (!skb)
949                 return NULL;
950
951         skb->len = sizeof(struct ieee80211_authentication);
952
953         auth = (struct ieee80211_authentication *)skb->data;
954
955         auth->status = cpu_to_le16(status);
956         auth->transaction = cpu_to_le16(2);
957         auth->algorithm = cpu_to_le16(WLAN_AUTH_OPEN);
958
959         memcpy(auth->header.addr3, ieee->dev->dev_addr, ETH_ALEN);
960         memcpy(auth->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
961         memcpy(auth->header.addr1, dest, ETH_ALEN);
962         auth->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_AUTH);
963         return skb;
964
965
966 }
967
968 struct sk_buff* ieee80211_null_func(struct ieee80211_device *ieee,short pwr)
969 {
970         struct sk_buff *skb;
971         struct ieee80211_hdr_3addr* hdr;
972
973         skb = dev_alloc_skb(sizeof(struct ieee80211_hdr_3addr));
974
975         if (!skb)
976                 return NULL;
977
978         hdr = (struct ieee80211_hdr_3addr*)skb_put(skb,sizeof(struct ieee80211_hdr_3addr));
979
980         memcpy(hdr->addr1, ieee->current_network.bssid, ETH_ALEN);
981         memcpy(hdr->addr2, ieee->dev->dev_addr, ETH_ALEN);
982         memcpy(hdr->addr3, ieee->current_network.bssid, ETH_ALEN);
983
984         hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
985                 IEEE80211_STYPE_NULLFUNC | IEEE80211_FCTL_TODS |
986                 (pwr ? IEEE80211_FCTL_PM:0));
987
988         return skb;
989
990
991 }
992
993
994 void ieee80211_resp_to_assoc_rq(struct ieee80211_device *ieee, u8* dest)
995 {
996         struct sk_buff *buf = ieee80211_assoc_resp(ieee, dest);
997
998         if (buf){
999                 softmac_mgmt_xmit(buf, ieee);
1000                 dev_kfree_skb_any(buf);//edit by thomas
1001         }
1002 }
1003
1004
1005 void ieee80211_resp_to_auth(struct ieee80211_device *ieee, int s, u8* dest)
1006 {
1007         struct sk_buff *buf = ieee80211_auth_resp(ieee, s, dest);
1008
1009         if (buf){
1010                 softmac_mgmt_xmit(buf, ieee);
1011                 dev_kfree_skb_any(buf);//edit by thomas
1012         }
1013 }
1014
1015
1016 void ieee80211_resp_to_probe(struct ieee80211_device *ieee, u8 *dest)
1017 {
1018
1019         struct sk_buff *buf = ieee80211_probe_resp(ieee, dest);
1020
1021         if (buf) {
1022                 softmac_mgmt_xmit(buf, ieee);
1023                 dev_kfree_skb_any(buf);//edit by thomas
1024         }
1025 }
1026
1027
1028 inline struct sk_buff *ieee80211_association_req(struct ieee80211_network *beacon,struct ieee80211_device *ieee)
1029 {
1030         struct sk_buff *skb;
1031         //unsigned long flags;
1032
1033         struct ieee80211_assoc_request_frame *hdr;
1034         u8 *tag;
1035         //short info_addr = 0;
1036         //int i;
1037         //u16 suite_count = 0;
1038         //u8 suit_select = 0;
1039         unsigned int wpa_len = beacon->wpa_ie_len;
1040         //struct net_device *dev = ieee->dev;
1041         //union iwreq_data wrqu;
1042         //u8 *buff;
1043         //u8 *p;
1044 #if 1
1045         // for testing purpose
1046         unsigned int rsn_len = beacon->rsn_ie_len;
1047 #else
1048         unsigned int rsn_len = beacon->rsn_ie_len - 4;
1049 #endif
1050         unsigned int rate_len = ieee80211_MFIE_rate_len(ieee);
1051         unsigned int wmm_info_len = beacon->QoS_Enable?9:0;
1052         unsigned int turbo_info_len = beacon->Turbo_Enable?9:0;
1053
1054         u8  encry_proto = ieee->wpax_type_notify & 0xff;
1055         //u8  pairwise_type = (ieee->wpax_type_notify >> 8) & 0xff;
1056         //u8  authen_type = (ieee->wpax_type_notify >> 16) & 0xff;
1057
1058         int len = 0;
1059
1060         //[0] Notify type of encryption: WPA/WPA2
1061         //[1] pair wise type
1062         //[2] authen type
1063         if(ieee->wpax_type_set) {
1064                 if (IEEE_PROTO_WPA == encry_proto) {
1065                         rsn_len = 0;
1066                 } else if (IEEE_PROTO_RSN == encry_proto) {
1067                         wpa_len = 0;
1068                 }
1069         }
1070         len = sizeof(struct ieee80211_assoc_request_frame)+
1071                 + beacon->ssid_len//essid tagged val
1072                 + rate_len//rates tagged val
1073                 + wpa_len
1074                 + rsn_len
1075                 + wmm_info_len
1076                 + turbo_info_len;
1077
1078         skb = dev_alloc_skb(len);
1079
1080         if (!skb)
1081                 return NULL;
1082
1083         hdr = (struct ieee80211_assoc_request_frame *)
1084                 skb_put(skb, sizeof(struct ieee80211_assoc_request_frame));
1085
1086
1087         hdr->header.frame_control = IEEE80211_STYPE_ASSOC_REQ;
1088         hdr->header.duration_id= 37; //FIXME
1089         memcpy(hdr->header.addr1, beacon->bssid, ETH_ALEN);
1090         memcpy(hdr->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
1091         memcpy(hdr->header.addr3, beacon->bssid, ETH_ALEN);
1092         memcpy(ieee->ap_mac_addr, beacon->bssid, ETH_ALEN);//for HW security, John
1093
1094         hdr->capability = cpu_to_le16(WLAN_CAPABILITY_BSS);
1095         if (beacon->capability & WLAN_CAPABILITY_PRIVACY )
1096                 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
1097         if (beacon->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1098                 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE);
1099
1100         if(ieee->short_slot)
1101                 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT);
1102
1103         hdr->listen_interval = 0xa; //FIXME
1104
1105         hdr->info_element.id = MFIE_TYPE_SSID;
1106
1107         hdr->info_element.len = beacon->ssid_len;
1108         tag = skb_put(skb, beacon->ssid_len);
1109         memcpy(tag, beacon->ssid, beacon->ssid_len);
1110
1111         tag = skb_put(skb, rate_len);
1112
1113         ieee80211_MFIE_Brate(ieee, &tag);
1114         ieee80211_MFIE_Grate(ieee, &tag);
1115
1116         //add rsn==0 condition for ap's mix security mode(wpa+wpa2), john2007.8.9
1117         //choose AES encryption as default algorithm while using mixed mode
1118
1119         tag = skb_put(skb,ieee->wpa_ie_len);
1120         memcpy(tag,ieee->wpa_ie,ieee->wpa_ie_len);
1121
1122         tag = skb_put(skb,wmm_info_len);
1123         if(wmm_info_len) {
1124           ieee80211_WMM_Info(ieee, &tag);
1125         }
1126         tag = skb_put(skb,turbo_info_len);
1127         if(turbo_info_len) {
1128                 ieee80211_TURBO_Info(ieee, &tag);
1129         }
1130
1131         return skb;
1132 }
1133
1134 void ieee80211_associate_abort(struct ieee80211_device *ieee)
1135 {
1136
1137         unsigned long flags;
1138         spin_lock_irqsave(&ieee->lock, flags);
1139
1140         ieee->associate_seq++;
1141
1142         /* don't scan, and avoid to have the RX path possibily
1143          * try again to associate. Even do not react to AUTH or
1144          * ASSOC response. Just wait for the retry wq to be scheduled.
1145          * Here we will check if there are good nets to associate
1146          * with, so we retry or just get back to NO_LINK and scanning
1147          */
1148         if (ieee->state == IEEE80211_ASSOCIATING_AUTHENTICATING){
1149                 IEEE80211_DEBUG_MGMT("Authentication failed\n");
1150                 ieee->softmac_stats.no_auth_rs++;
1151         }else{
1152                 IEEE80211_DEBUG_MGMT("Association failed\n");
1153                 ieee->softmac_stats.no_ass_rs++;
1154         }
1155
1156         ieee->state = IEEE80211_ASSOCIATING_RETRY;
1157
1158         queue_delayed_work(ieee->wq, &ieee->associate_retry_wq,IEEE80211_SOFTMAC_ASSOC_RETRY_TIME);
1159
1160         spin_unlock_irqrestore(&ieee->lock, flags);
1161 }
1162
1163 void ieee80211_associate_abort_cb(unsigned long dev)
1164 {
1165         ieee80211_associate_abort((struct ieee80211_device *) dev);
1166 }
1167
1168
1169 void ieee80211_associate_step1(struct ieee80211_device *ieee)
1170 {
1171         struct ieee80211_network *beacon = &ieee->current_network;
1172         struct sk_buff *skb;
1173
1174         IEEE80211_DEBUG_MGMT("Stopping scan\n");
1175         ieee->softmac_stats.tx_auth_rq++;
1176         skb=ieee80211_authentication_req(beacon, ieee, 0);
1177         if (!skb){
1178
1179                 ieee80211_associate_abort(ieee);
1180         }
1181         else{
1182                 ieee->state = IEEE80211_ASSOCIATING_AUTHENTICATING ;
1183                 IEEE80211_DEBUG_MGMT("Sending authentication request\n");
1184                 //printk("---Sending authentication request\n");
1185                 softmac_mgmt_xmit(skb, ieee);
1186                 //BUGON when you try to add_timer twice, using mod_timer may be better, john0709
1187                 if(!timer_pending(&ieee->associate_timer)){
1188                         ieee->associate_timer.expires = jiffies + (HZ / 2);
1189                         add_timer(&ieee->associate_timer);
1190                 }
1191                 //If call dev_kfree_skb_any,a warning will ocur....
1192                 //KERNEL: assertion (!atomic_read(&skb->users)) failed at net/core/dev.c (1708)
1193                 //So ... 1204 by lawrence.
1194                 //printk("\nIn %s,line %d call kfree skb.",__func__,__LINE__);
1195                 //dev_kfree_skb_any(skb);//edit by thomas
1196         }
1197 }
1198
1199 void ieee80211_rtl_auth_challenge(struct ieee80211_device *ieee, u8 *challenge, int chlen)
1200 {
1201         u8 *c;
1202         struct sk_buff *skb;
1203         struct ieee80211_network *beacon = &ieee->current_network;
1204 //      int hlen = sizeof(struct ieee80211_authentication);
1205         del_timer_sync(&ieee->associate_timer);
1206         ieee->associate_seq++;
1207         ieee->softmac_stats.tx_auth_rq++;
1208
1209         skb = ieee80211_authentication_req(beacon, ieee, chlen+2);
1210         if (!skb)
1211                 ieee80211_associate_abort(ieee);
1212         else{
1213                 c = skb_put(skb, chlen+2);
1214                 *(c++) = MFIE_TYPE_CHALLENGE;
1215                 *(c++) = chlen;
1216                 memcpy(c, challenge, chlen);
1217
1218                 IEEE80211_DEBUG_MGMT("Sending authentication challenge response\n");
1219
1220                 ieee80211_encrypt_fragment(ieee, skb, sizeof(struct ieee80211_hdr_3addr  ));
1221
1222                 softmac_mgmt_xmit(skb, ieee);
1223                 if (!timer_pending(&ieee->associate_timer)){
1224                 //printk("=========>add timer again, to crash\n");
1225                 ieee->associate_timer.expires = jiffies + (HZ / 2);
1226                 add_timer(&ieee->associate_timer);
1227                 }
1228                 dev_kfree_skb_any(skb);//edit by thomas
1229         }
1230         kfree(challenge);
1231 }
1232
1233 void ieee80211_associate_step2(struct ieee80211_device *ieee)
1234 {
1235         struct sk_buff* skb;
1236         struct ieee80211_network *beacon = &ieee->current_network;
1237
1238         del_timer_sync(&ieee->associate_timer);
1239
1240         IEEE80211_DEBUG_MGMT("Sending association request\n");
1241         ieee->softmac_stats.tx_ass_rq++;
1242         skb=ieee80211_association_req(beacon, ieee);
1243         if (!skb)
1244                 ieee80211_associate_abort(ieee);
1245         else{
1246                 softmac_mgmt_xmit(skb, ieee);
1247                 if (!timer_pending(&ieee->associate_timer)){
1248                 ieee->associate_timer.expires = jiffies + (HZ / 2);
1249                 add_timer(&ieee->associate_timer);
1250                 }
1251                 //dev_kfree_skb_any(skb);//edit by thomas
1252         }
1253 }
1254
1255 void ieee80211_associate_complete_wq(struct work_struct *work)
1256 {
1257         struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, associate_complete_wq);
1258
1259         printk(KERN_INFO "Associated successfully\n");
1260         if(ieee80211_is_54g(ieee->current_network) &&
1261                 (ieee->modulation & IEEE80211_OFDM_MODULATION)){
1262
1263                 ieee->rate = 540;
1264                 printk(KERN_INFO"Using G rates\n");
1265         }else{
1266                 ieee->rate = 110;
1267                 printk(KERN_INFO"Using B rates\n");
1268         }
1269         ieee->link_change(ieee->dev);
1270         notify_wx_assoc_event(ieee);
1271         if (ieee->data_hard_resume)
1272                 ieee->data_hard_resume(ieee->dev);
1273         netif_carrier_on(ieee->dev);
1274 }
1275
1276 void ieee80211_associate_complete(struct ieee80211_device *ieee)
1277 {
1278         int i;
1279         del_timer_sync(&ieee->associate_timer);
1280
1281         for(i = 0; i < 6; i++) {
1282           //ieee->seq_ctrl[i] = 0;
1283         }
1284         ieee->state = IEEE80211_LINKED;
1285         IEEE80211_DEBUG_MGMT("Successfully associated\n");
1286
1287         queue_work(ieee->wq, &ieee->associate_complete_wq);
1288 }
1289
1290 void ieee80211_associate_procedure_wq(struct work_struct *work)
1291 {
1292         struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, associate_procedure_wq);
1293
1294         ieee->sync_scan_hurryup = 1;
1295         down(&ieee->wx_sem);
1296
1297         if (ieee->data_hard_stop)
1298                 ieee->data_hard_stop(ieee->dev);
1299
1300         ieee80211_stop_scan(ieee);
1301         ieee->set_chan(ieee->dev, ieee->current_network.channel);
1302
1303         ieee->associate_seq = 1;
1304         ieee80211_associate_step1(ieee);
1305
1306         up(&ieee->wx_sem);
1307 }
1308
1309 inline void ieee80211_softmac_new_net(struct ieee80211_device *ieee, struct ieee80211_network *net)
1310 {
1311         u8 tmp_ssid[IW_ESSID_MAX_SIZE+1];
1312         int tmp_ssid_len = 0;
1313
1314         short apset,ssidset,ssidbroad,apmatch,ssidmatch;
1315
1316         /* we are interested in new new only if we are not associated
1317          * and we are not associating / authenticating
1318          */
1319         if (ieee->state != IEEE80211_NOLINK)
1320                 return;
1321
1322         if ((ieee->iw_mode == IW_MODE_INFRA) && !(net->capability & WLAN_CAPABILITY_BSS))
1323                 return;
1324
1325         if ((ieee->iw_mode == IW_MODE_ADHOC) && !(net->capability & WLAN_CAPABILITY_IBSS))
1326                 return;
1327
1328
1329         if (ieee->iw_mode == IW_MODE_INFRA || ieee->iw_mode == IW_MODE_ADHOC){
1330                 /* if the user specified the AP MAC, we need also the essid
1331                  * This could be obtained by beacons or, if the network does not
1332                  * broadcast it, it can be put manually.
1333                  */
1334                 apset = ieee->wap_set;//(memcmp(ieee->current_network.bssid, zero,ETH_ALEN)!=0 );
1335                 ssidset = ieee->ssid_set;//ieee->current_network.ssid[0] != '\0';
1336                 ssidbroad =  !(net->ssid_len == 0 || net->ssid[0]== '\0');
1337                 apmatch = (memcmp(ieee->current_network.bssid, net->bssid, ETH_ALEN)==0);
1338
1339                 if(ieee->current_network.ssid_len != net->ssid_len)
1340                         ssidmatch = 0;
1341                 else
1342                         ssidmatch = (0==strncmp(ieee->current_network.ssid, net->ssid, net->ssid_len));
1343
1344                 //printk("cur: %s, %d, net:%s, %d\n", ieee->current_network.ssid, ieee->current_network.ssid_len, net->ssid, net->ssid_len);
1345                 //printk("apset=%d apmatch=%d ssidset=%d ssidbroad=%d ssidmatch=%d\n",apset,apmatch,ssidset,ssidbroad,ssidmatch);
1346
1347                 if (    /* if the user set the AP check if match.
1348                          * if the network does not broadcast essid we check the user supplyed ANY essid
1349                          * if the network does broadcast and the user does not set essid it is OK
1350                          * if the network does broadcast and the user did set essid chech if essid match
1351                          */
1352                                 ( apset && apmatch &&
1353                                   ((ssidset && ssidbroad && ssidmatch) || (ssidbroad && !ssidset) || (!ssidbroad && ssidset)) ) ||
1354                                 /* if the ap is not set, check that the user set the bssid
1355                                  * and the network does bradcast and that those two bssid matches
1356                                  */
1357                                 (!apset && ssidset && ssidbroad && ssidmatch)
1358                    ){
1359
1360
1361                         /* if the essid is hidden replace it with the
1362                          * essid provided by the user.
1363                          */
1364                         if (!ssidbroad){
1365                                 strncpy(tmp_ssid, ieee->current_network.ssid, IW_ESSID_MAX_SIZE);
1366                                 tmp_ssid_len = ieee->current_network.ssid_len;
1367                         }
1368                         memcpy(&ieee->current_network, net, sizeof(struct ieee80211_network));
1369
1370                         if (!ssidbroad){
1371                                 strncpy(ieee->current_network.ssid, tmp_ssid, IW_ESSID_MAX_SIZE);
1372                                 ieee->current_network.ssid_len = tmp_ssid_len;
1373                         }
1374                         printk(KERN_INFO"Linking with %s: channel is %d\n",ieee->current_network.ssid,ieee->current_network.channel);
1375
1376                         if (ieee->iw_mode == IW_MODE_INFRA){
1377                                 ieee->state = IEEE80211_ASSOCIATING;
1378                                 ieee->beinretry = false;
1379                                 queue_work(ieee->wq, &ieee->associate_procedure_wq);
1380                         }else{
1381                                 if(ieee80211_is_54g(ieee->current_network) &&
1382                                                 (ieee->modulation & IEEE80211_OFDM_MODULATION)){
1383                                         ieee->rate = 540;
1384                                         printk(KERN_INFO"Using G rates\n");
1385                                 }else{
1386                                         ieee->rate = 110;
1387                                         printk(KERN_INFO"Using B rates\n");
1388                                 }
1389                                 ieee->state = IEEE80211_LINKED;
1390                                 ieee->beinretry = false;
1391                         }
1392
1393                 }
1394         }
1395
1396 }
1397
1398 void ieee80211_softmac_check_all_nets(struct ieee80211_device *ieee)
1399 {
1400         unsigned long flags;
1401         struct ieee80211_network *target;
1402
1403         spin_lock_irqsave(&ieee->lock, flags);
1404         list_for_each_entry(target, &ieee->network_list, list) {
1405
1406                 /* if the state become different that NOLINK means
1407                  * we had found what we are searching for
1408                  */
1409
1410                 if (ieee->state != IEEE80211_NOLINK)
1411                         break;
1412
1413                 if (ieee->scan_age == 0 || time_after(target->last_scanned + ieee->scan_age, jiffies))
1414                         ieee80211_softmac_new_net(ieee, target);
1415         }
1416
1417         spin_unlock_irqrestore(&ieee->lock, flags);
1418
1419 }
1420
1421
1422 static inline u16 auth_parse(struct sk_buff *skb, u8** challenge, int *chlen)
1423 {
1424         struct ieee80211_authentication *a;
1425         u8 *t;
1426         if (skb->len <  (sizeof(struct ieee80211_authentication)-sizeof(struct ieee80211_info_element))){
1427                 IEEE80211_DEBUG_MGMT("invalid len in auth resp: %d\n",skb->len);
1428                 return 0xcafe;
1429         }
1430         *challenge = NULL;
1431         a = (struct ieee80211_authentication*) skb->data;
1432         if(skb->len > (sizeof(struct ieee80211_authentication) +3)){
1433                 t = skb->data + sizeof(struct ieee80211_authentication);
1434
1435                 if(*(t++) == MFIE_TYPE_CHALLENGE){
1436                         *chlen = *(t++);
1437                         *challenge = kmemdup(t, *chlen, GFP_ATOMIC);
1438                         if (!*challenge)
1439                                 return -ENOMEM;
1440                 }
1441         }
1442
1443         return cpu_to_le16(a->status);
1444
1445 }
1446
1447
1448 int auth_rq_parse(struct sk_buff *skb,u8* dest)
1449 {
1450         struct ieee80211_authentication *a;
1451
1452         if (skb->len <  (sizeof(struct ieee80211_authentication)-sizeof(struct ieee80211_info_element))){
1453                 IEEE80211_DEBUG_MGMT("invalid len in auth request: %d\n",skb->len);
1454                 return -1;
1455         }
1456         a = (struct ieee80211_authentication*) skb->data;
1457
1458         memcpy(dest,a->header.addr2, ETH_ALEN);
1459
1460         if (le16_to_cpu(a->algorithm) != WLAN_AUTH_OPEN)
1461                 return  WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
1462
1463         return WLAN_STATUS_SUCCESS;
1464 }
1465
1466 static short probe_rq_parse(struct ieee80211_device *ieee, struct sk_buff *skb, u8 *src)
1467 {
1468         u8 *tag;
1469         u8 *skbend;
1470         u8 *ssid=NULL;
1471         u8 ssidlen = 0;
1472
1473         struct ieee80211_hdr_3addr   *header =
1474                 (struct ieee80211_hdr_3addr   *) skb->data;
1475
1476         if (skb->len < sizeof (struct ieee80211_hdr_3addr  ))
1477                 return -1; /* corrupted */
1478
1479         memcpy(src,header->addr2, ETH_ALEN);
1480
1481         skbend = (u8*)skb->data + skb->len;
1482
1483         tag = skb->data + sizeof (struct ieee80211_hdr_3addr  );
1484
1485         while (tag+1 < skbend){
1486                 if (*tag == 0){
1487                         ssid = tag+2;
1488                         ssidlen = *(tag+1);
1489                         break;
1490                 }
1491                 tag++; /* point to the len field */
1492                 tag = tag + *(tag); /* point to the last data byte of the tag */
1493                 tag++; /* point to the next tag */
1494         }
1495
1496         //IEEE80211DMESG("Card MAC address is "MACSTR, MAC2STR(src));
1497         if (ssidlen == 0) return 1;
1498
1499         if (!ssid) return 1; /* ssid not found in tagged param */
1500         return (!strncmp(ssid, ieee->current_network.ssid, ssidlen));
1501
1502 }
1503
1504 int assoc_rq_parse(struct sk_buff *skb,u8* dest)
1505 {
1506         struct ieee80211_assoc_request_frame *a;
1507
1508         if (skb->len < (sizeof(struct ieee80211_assoc_request_frame) -
1509                 sizeof(struct ieee80211_info_element))) {
1510
1511                 IEEE80211_DEBUG_MGMT("invalid len in auth request:%d \n", skb->len);
1512                 return -1;
1513         }
1514
1515         a = (struct ieee80211_assoc_request_frame*) skb->data;
1516
1517         memcpy(dest,a->header.addr2,ETH_ALEN);
1518
1519         return 0;
1520 }
1521
1522 static inline u16 assoc_parse(struct sk_buff *skb, int *aid)
1523 {
1524         struct ieee80211_assoc_response_frame *a;
1525         if (skb->len <  sizeof(struct ieee80211_assoc_response_frame)){
1526                 IEEE80211_DEBUG_MGMT("invalid len in auth resp: %d\n", skb->len);
1527                 return 0xcafe;
1528         }
1529
1530         a = (struct ieee80211_assoc_response_frame*) skb->data;
1531         *aid = le16_to_cpu(a->aid) & 0x3fff;
1532         return le16_to_cpu(a->status);
1533 }
1534
1535 static inline void
1536 ieee80211_rx_probe_rq(struct ieee80211_device *ieee, struct sk_buff *skb)
1537 {
1538         u8 dest[ETH_ALEN];
1539
1540         //IEEE80211DMESG("Rx probe");
1541         ieee->softmac_stats.rx_probe_rq++;
1542         //DMESG("Dest is "MACSTR, MAC2STR(dest));
1543         if (probe_rq_parse(ieee, skb, dest)){
1544                 //IEEE80211DMESG("Was for me!");
1545                 ieee->softmac_stats.tx_probe_rs++;
1546                 ieee80211_resp_to_probe(ieee, dest);
1547         }
1548 }
1549
1550 inline void
1551 ieee80211_rx_auth_rq(struct ieee80211_device *ieee, struct sk_buff *skb)
1552 {
1553         u8 dest[ETH_ALEN];
1554         int status;
1555         //IEEE80211DMESG("Rx probe");
1556         ieee->softmac_stats.rx_auth_rq++;
1557
1558         status = auth_rq_parse(skb, dest);
1559         if (status != -1) {
1560                 ieee80211_resp_to_auth(ieee, status, dest);
1561         }
1562         //DMESG("Dest is "MACSTR, MAC2STR(dest));
1563
1564 }
1565
1566  inline void
1567 ieee80211_rx_assoc_rq(struct ieee80211_device *ieee, struct sk_buff *skb)
1568 {
1569
1570         u8 dest[ETH_ALEN];
1571         //unsigned long flags;
1572
1573         ieee->softmac_stats.rx_ass_rq++;
1574         if (assoc_rq_parse(skb,dest) != -1){
1575                 ieee80211_resp_to_assoc_rq(ieee, dest);
1576         }
1577
1578         printk(KERN_INFO"New client associated: %pM\n", dest);
1579 }
1580
1581
1582
1583 void ieee80211_sta_ps_send_null_frame(struct ieee80211_device *ieee, short pwr)
1584 {
1585
1586         struct sk_buff *buf = ieee80211_null_func(ieee, pwr);
1587
1588         if (buf)
1589                 softmac_ps_mgmt_xmit(buf, ieee);
1590
1591 }
1592
1593
1594 short ieee80211_sta_ps_sleep(struct ieee80211_device *ieee, u32 *time_h, u32 *time_l)
1595 {
1596         int timeout = 0;
1597
1598         u8 dtim;
1599         /*if(ieee->ps == IEEE80211_PS_DISABLED ||
1600                 ieee->iw_mode != IW_MODE_INFRA ||
1601                 ieee->state != IEEE80211_LINKED)
1602
1603                 return 0;
1604         */
1605         dtim = ieee->current_network.dtim_data;
1606         //printk("DTIM\n");
1607
1608         if(!(dtim & IEEE80211_DTIM_VALID))
1609                 return 0;
1610         else
1611                 timeout = ieee->current_network.beacon_interval;
1612
1613         //printk("VALID\n");
1614         ieee->current_network.dtim_data = IEEE80211_DTIM_INVALID;
1615
1616         if(dtim & ((IEEE80211_DTIM_UCAST | IEEE80211_DTIM_MBCAST)& ieee->ps))
1617                 return 2;
1618
1619         if(!time_after(jiffies, ieee->dev->trans_start + MSECS(timeout)))
1620                 return 0;
1621
1622         if(!time_after(jiffies, ieee->last_rx_ps_time + MSECS(timeout)))
1623                 return 0;
1624
1625         if((ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE ) &&
1626                 (ieee->mgmt_queue_tail != ieee->mgmt_queue_head))
1627                 return 0;
1628
1629         if(time_l){
1630                 *time_l = ieee->current_network.last_dtim_sta_time[0]
1631                         + MSECS((ieee->current_network.beacon_interval));
1632                         //* ieee->current_network.dtim_period));
1633                         //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);
1634         }
1635
1636         if(time_h){
1637                 *time_h = ieee->current_network.last_dtim_sta_time[1];
1638                 if(time_l && *time_l < ieee->current_network.last_dtim_sta_time[0])
1639                         *time_h += 1;
1640         }
1641
1642         return 1;
1643
1644
1645 }
1646
1647 inline void ieee80211_sta_ps(struct ieee80211_device *ieee)
1648 {
1649
1650         u32 th,tl;
1651         short sleep;
1652
1653         unsigned long flags,flags2;
1654
1655         spin_lock_irqsave(&ieee->lock, flags);
1656
1657         if((ieee->ps == IEEE80211_PS_DISABLED ||
1658
1659                 ieee->iw_mode != IW_MODE_INFRA ||
1660                 ieee->state != IEEE80211_LINKED)){
1661
1662                 //#warning CHECK_LOCK_HERE
1663                 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1664
1665                 ieee80211_sta_wakeup(ieee, 1);
1666
1667                 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1668         }
1669
1670         sleep = ieee80211_sta_ps_sleep(ieee,&th, &tl);
1671 //      printk("===>%s,%d[2 wake, 1 sleep, 0 do nothing], ieee->sta_sleep = %d\n",__func__, sleep,ieee->sta_sleep);
1672         /* 2 wake, 1 sleep, 0 do nothing */
1673         if(sleep == 0)
1674                 goto out;
1675
1676         if(sleep == 1){
1677
1678                 if(ieee->sta_sleep == 1)
1679                         ieee->enter_sleep_state(ieee->dev,th,tl);
1680
1681                 else if(ieee->sta_sleep == 0){
1682         //              printk("send null 1\n");
1683                         spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1684
1685                         if(ieee->ps_is_queue_empty(ieee->dev)){
1686
1687
1688                                 ieee->sta_sleep = 2;
1689
1690                                 ieee->ps_request_tx_ack(ieee->dev);
1691
1692                                 ieee80211_sta_ps_send_null_frame(ieee,1);
1693
1694                                 ieee->ps_th = th;
1695                                 ieee->ps_tl = tl;
1696                         }
1697                         spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1698
1699                 }
1700
1701
1702         }else if(sleep == 2){
1703 //#warning CHECK_LOCK_HERE
1704                 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1705
1706         //      printk("send wakeup packet\n");
1707                 ieee80211_sta_wakeup(ieee,1);
1708
1709                 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1710         }
1711
1712 out:
1713         spin_unlock_irqrestore(&ieee->lock, flags);
1714
1715 }
1716
1717 void ieee80211_sta_wakeup(struct ieee80211_device *ieee, short nl)
1718 {
1719         if(ieee->sta_sleep == 0){
1720                 if(nl){
1721                 //      printk("Warning: driver is probably failing to report TX ps error\n");
1722                         ieee->ps_request_tx_ack(ieee->dev);
1723                         ieee80211_sta_ps_send_null_frame(ieee, 0);
1724                 }
1725                 return;
1726
1727         }
1728
1729         if(ieee->sta_sleep == 1)
1730                 ieee->sta_wake_up(ieee->dev);
1731
1732         ieee->sta_sleep = 0;
1733
1734         if(nl){
1735                 ieee->ps_request_tx_ack(ieee->dev);
1736                 ieee80211_sta_ps_send_null_frame(ieee, 0);
1737         }
1738 }
1739
1740 void ieee80211_ps_tx_ack(struct ieee80211_device *ieee, short success)
1741 {
1742         unsigned long flags,flags2;
1743
1744         spin_lock_irqsave(&ieee->lock, flags);
1745         if(ieee->sta_sleep == 2){
1746                 /* Null frame with PS bit set */
1747                 if(success){
1748
1749                 //      printk("==================> %s::enter sleep state\n",__func__);
1750                         ieee->sta_sleep = 1;
1751                         ieee->enter_sleep_state(ieee->dev,ieee->ps_th,ieee->ps_tl);
1752                 }
1753                 /* if the card report not success we can't be sure the AP
1754                  * has not RXed so we can't assume the AP believe us awake
1755                  */
1756         }
1757         /* 21112005 - tx again null without PS bit if lost */
1758         else {
1759
1760                 if((ieee->sta_sleep == 0) && !success){
1761                         spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1762                         ieee80211_sta_ps_send_null_frame(ieee, 0);
1763                         spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1764                 }
1765         }
1766         spin_unlock_irqrestore(&ieee->lock, flags);
1767 }
1768
1769 inline int
1770 ieee80211_rx_frame_softmac(struct ieee80211_device *ieee, struct sk_buff *skb,
1771                         struct ieee80211_rx_stats *rx_stats, u16 type,
1772                         u16 stype)
1773 {
1774         struct ieee80211_hdr_3addr *header = (struct ieee80211_hdr_3addr *) skb->data;
1775         u16 errcode;
1776         u8* challenge=NULL;
1777         int chlen=0;
1778         int aid=0;
1779         struct ieee80211_assoc_response_frame *assoc_resp;
1780         struct ieee80211_info_element *info_element;
1781
1782         if(!ieee->proto_started)
1783                 return 0;
1784
1785         if(ieee->sta_sleep || (ieee->ps != IEEE80211_PS_DISABLED &&
1786                 ieee->iw_mode == IW_MODE_INFRA &&
1787                 ieee->state == IEEE80211_LINKED))
1788
1789                 tasklet_schedule(&ieee->ps_task);
1790
1791         if (WLAN_FC_GET_STYPE(header->frame_control) != IEEE80211_STYPE_PROBE_RESP &&
1792                 WLAN_FC_GET_STYPE(header->frame_control) != IEEE80211_STYPE_BEACON)
1793                 ieee->last_rx_ps_time = jiffies;
1794
1795         switch (WLAN_FC_GET_STYPE(header->frame_control)) {
1796
1797                 case IEEE80211_STYPE_ASSOC_RESP:
1798                 case IEEE80211_STYPE_REASSOC_RESP:
1799
1800                         IEEE80211_DEBUG_MGMT("received [RE]ASSOCIATION RESPONSE (%d)\n",
1801                                         WLAN_FC_GET_STYPE(header->frame_ctl));
1802                         if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
1803                                 ieee->state == IEEE80211_ASSOCIATING_AUTHENTICATED &&
1804                                 ieee->iw_mode == IW_MODE_INFRA){
1805                                 if (0 == (errcode=assoc_parse(skb, &aid))){
1806                                         u16 left;
1807
1808                                         ieee->state=IEEE80211_LINKED;
1809                                         ieee->assoc_id = aid;
1810                                         ieee->softmac_stats.rx_ass_ok++;
1811
1812                                         //printk(KERN_WARNING "nic_type = %s", (rx_stats->nic_type == 1)?"rtl8187":"rtl8187B");
1813                                         if(1 == rx_stats->nic_type) //card type is 8187
1814                                         {
1815                                                 goto associate_complete;
1816                                         }
1817                                         assoc_resp = (struct ieee80211_assoc_response_frame*)skb->data;
1818                                         info_element =  &assoc_resp->info_element;
1819                                         left = skb->len - ((void*)info_element - (void*)assoc_resp);
1820
1821                                         while (left >= sizeof(struct ieee80211_info_element_hdr)) {
1822                                                 if (sizeof(struct ieee80211_info_element_hdr) + info_element->len > left) {
1823                                                         printk(KERN_WARNING "[re]associate reeponse error!");
1824                                                         return 1;
1825                                                 }
1826                                                 switch (info_element->id) {
1827                                                   case MFIE_TYPE_GENERIC:
1828                                                          IEEE80211_DEBUG_SCAN("MFIE_TYPE_GENERIC: %d bytes\n", info_element->len);
1829                                                         if (info_element->len >= 8  &&
1830                                                             info_element->data[0] == 0x00 &&
1831                                                             info_element->data[1] == 0x50 &&
1832                                                             info_element->data[2] == 0xf2 &&
1833                                                             info_element->data[3] == 0x02 &&
1834                                                             info_element->data[4] == 0x01) {
1835                                                             // Not care about version at present.
1836                                                             //WMM Parameter Element
1837                                                             memcpy(ieee->current_network.wmm_param,(u8*)(info_element->data\
1838                                                                                     + 8),(info_element->len - 8));
1839
1840                                                             if (((ieee->current_network.wmm_info^info_element->data[6])& \
1841                                                                                     0x0f)||(!ieee->init_wmmparam_flag)) {
1842                                                               // refresh parameter element for current network
1843                                                               // update the register parameter for hardware
1844                                                               ieee->init_wmmparam_flag = 1;
1845                                                               queue_work(ieee->wq, &ieee->wmm_param_update_wq);
1846
1847                                                             }
1848                                                             //update info_element for current network
1849                                                             ieee->current_network.wmm_info  = info_element->data[6];
1850                                                         }
1851                                                         break;
1852                                                   default:
1853                                                         //nothing to do at present!!!
1854                                                         break;
1855                                                 }
1856
1857                                                 left -= sizeof(struct ieee80211_info_element_hdr) +
1858                                                         info_element->len;
1859                                                 info_element = (struct ieee80211_info_element *)
1860                                                         &info_element->data[info_element->len];
1861                                         }
1862                                         if(!ieee->init_wmmparam_flag) //legacy AP, reset the AC_xx_param register
1863                                         {
1864                                                 queue_work(ieee->wq,&ieee->wmm_param_update_wq);
1865                                                 ieee->init_wmmparam_flag = 1;//indicate AC_xx_param upated since last associate
1866                                         }
1867 associate_complete:
1868                                         ieee80211_associate_complete(ieee);
1869                                 }else{
1870                                         ieee->softmac_stats.rx_ass_err++;
1871                                         IEEE80211_DEBUG_MGMT(
1872                                                 "Association response status code 0x%x\n",
1873                                                 errcode);
1874                                         ieee80211_associate_abort(ieee);
1875                                 }
1876                         }
1877                         break;
1878
1879                 case IEEE80211_STYPE_ASSOC_REQ:
1880                 case IEEE80211_STYPE_REASSOC_REQ:
1881
1882                         if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
1883                                 ieee->iw_mode == IW_MODE_MASTER)
1884
1885                                 ieee80211_rx_assoc_rq(ieee, skb);
1886                         break;
1887
1888                 case IEEE80211_STYPE_AUTH:
1889
1890                         if (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE){
1891                                 if (ieee->state == IEEE80211_ASSOCIATING_AUTHENTICATING &&
1892                                 ieee->iw_mode == IW_MODE_INFRA){
1893
1894                                                 IEEE80211_DEBUG_MGMT("Received authentication response");
1895
1896                                                 if (0 == (errcode=auth_parse(skb, &challenge, &chlen))){
1897                                                         if(ieee->open_wep || !challenge){
1898                                                                 ieee->state = IEEE80211_ASSOCIATING_AUTHENTICATED;
1899                                                                 ieee->softmac_stats.rx_auth_rs_ok++;
1900
1901                                                                 ieee80211_associate_step2(ieee);
1902                                                         }else{
1903                                                                 ieee80211_rtl_auth_challenge(ieee, challenge, chlen);
1904                                                         }
1905                                                 }else{
1906                                                         ieee->softmac_stats.rx_auth_rs_err++;
1907                                                         IEEE80211_DEBUG_MGMT("Authentication respose status code 0x%x",errcode);
1908                                                         ieee80211_associate_abort(ieee);
1909                                                 }
1910
1911                                         }else if (ieee->iw_mode == IW_MODE_MASTER){
1912                                                 ieee80211_rx_auth_rq(ieee, skb);
1913                                         }
1914                                 }
1915                         break;
1916
1917                 case IEEE80211_STYPE_PROBE_REQ:
1918
1919                         if ((ieee->softmac_features & IEEE_SOFTMAC_PROBERS) &&
1920                                 ((ieee->iw_mode == IW_MODE_ADHOC ||
1921                                 ieee->iw_mode == IW_MODE_MASTER) &&
1922                                 ieee->state == IEEE80211_LINKED))
1923
1924                                 ieee80211_rx_probe_rq(ieee, skb);
1925                         break;
1926
1927                 case IEEE80211_STYPE_DISASSOC:
1928                 case IEEE80211_STYPE_DEAUTH:
1929                         /* FIXME for now repeat all the association procedure
1930                         * both for disassociation and deauthentication
1931                         */
1932                         if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
1933                                 (ieee->state == IEEE80211_LINKED) &&
1934                                 (ieee->iw_mode == IW_MODE_INFRA) &&
1935                                 (!memcmp(header->addr2,ieee->current_network.bssid,ETH_ALEN))){
1936                                 ieee->state = IEEE80211_ASSOCIATING;
1937                                 ieee->softmac_stats.reassoc++;
1938
1939                                 //notify_wx_assoc_event(ieee);  //YJ,del,080828, do not notify os here
1940                                 queue_work(ieee->wq, &ieee->associate_procedure_wq);
1941                         }
1942
1943                         break;
1944
1945                 default:
1946                         return -1;
1947                         break;
1948         }
1949
1950         //dev_kfree_skb_any(skb);
1951         return 0;
1952 }
1953
1954
1955
1956 /* following are for a simpler TX queue management.
1957  * Instead of using netif_[stop/wake]_queue the driver
1958  * will uses these two function (plus a reset one), that
1959  * will internally uses the kernel netif_* and takes
1960  * care of the ieee802.11 fragmentation.
1961  * So the driver receives a fragment per time and might
1962  * call the stop function when it want without take care
1963  * to have enough room to TX an entire packet.
1964  * This might be useful if each fragment need it's own
1965  * descriptor, thus just keep a total free memory > than
1966  * the max fragmentation threshold is not enough.. If the
1967  * ieee802.11 stack passed a TXB struct then you needed
1968  * to keep N free descriptors where
1969  * N = MAX_PACKET_SIZE / MIN_FRAG_TRESHOLD
1970  * In this way you need just one and the 802.11 stack
1971  * will take care of buffering fragments and pass them to
1972  * to the driver later, when it wakes the queue.
1973  */
1974
1975 void ieee80211_softmac_xmit(struct ieee80211_txb *txb, struct ieee80211_device *ieee)
1976 {
1977
1978
1979         unsigned long flags;
1980         int  i;
1981
1982         spin_lock_irqsave(&ieee->lock,flags);
1983
1984         /* called with 2nd parm 0, no tx mgmt lock required */
1985         ieee80211_sta_wakeup(ieee,0);
1986
1987         for(i = 0; i < txb->nr_frags; i++) {
1988
1989                 if (ieee->queue_stop){
1990                         ieee->tx_pending.txb = txb;
1991                         ieee->tx_pending.frag = i;
1992                         goto exit;
1993                 }else{
1994                         ieee->softmac_data_hard_start_xmit(
1995                                 txb->fragments[i],
1996                                 ieee->dev,ieee->rate);
1997                                 //(i+1)<txb->nr_frags);
1998                         ieee->stats.tx_packets++;
1999                         ieee->stats.tx_bytes += txb->fragments[i]->len;
2000                         ieee->dev->trans_start = jiffies;
2001                 }
2002         }
2003
2004         ieee80211_txb_free(txb);
2005
2006         exit:
2007         spin_unlock_irqrestore(&ieee->lock,flags);
2008
2009 }
2010
2011 /* called with ieee->lock acquired */
2012 void ieee80211_resume_tx(struct ieee80211_device *ieee)
2013 {
2014         int i;
2015         for(i = ieee->tx_pending.frag; i < ieee->tx_pending.txb->nr_frags; i++) {
2016
2017                 if (ieee->queue_stop){
2018                         ieee->tx_pending.frag = i;
2019                         return;
2020                 }else{
2021
2022                         ieee->softmac_data_hard_start_xmit(
2023                                 ieee->tx_pending.txb->fragments[i],
2024                                 ieee->dev,ieee->rate);
2025                                 //(i+1)<ieee->tx_pending.txb->nr_frags);
2026                         ieee->stats.tx_packets++;
2027                         ieee->dev->trans_start = jiffies;
2028                 }
2029         }
2030
2031
2032         ieee80211_txb_free(ieee->tx_pending.txb);
2033         ieee->tx_pending.txb = NULL;
2034 }
2035
2036
2037 void ieee80211_reset_queue(struct ieee80211_device *ieee)
2038 {
2039         unsigned long flags;
2040
2041         spin_lock_irqsave(&ieee->lock,flags);
2042         init_mgmt_queue(ieee);
2043         if (ieee->tx_pending.txb){
2044                 ieee80211_txb_free(ieee->tx_pending.txb);
2045                 ieee->tx_pending.txb = NULL;
2046         }
2047         ieee->queue_stop = 0;
2048         spin_unlock_irqrestore(&ieee->lock,flags);
2049
2050 }
2051
2052 void ieee80211_rtl_wake_queue(struct ieee80211_device *ieee)
2053 {
2054
2055         unsigned long flags;
2056         struct sk_buff *skb;
2057         struct ieee80211_hdr_3addr  *header;
2058
2059         spin_lock_irqsave(&ieee->lock,flags);
2060         if (! ieee->queue_stop) goto exit;
2061
2062         ieee->queue_stop = 0;
2063
2064         if(ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE){
2065                 while (!ieee->queue_stop && (skb = dequeue_mgmt(ieee))){
2066
2067                         header = (struct ieee80211_hdr_3addr  *) skb->data;
2068
2069                         header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
2070
2071                         if (ieee->seq_ctrl[0] == 0xFFF)
2072                                 ieee->seq_ctrl[0] = 0;
2073                         else
2074                                 ieee->seq_ctrl[0]++;
2075
2076                         //printk(KERN_ALERT "ieee80211_wake_queue \n");
2077                         ieee->softmac_data_hard_start_xmit(skb,ieee->dev,ieee->basic_rate);
2078                         dev_kfree_skb_any(skb);//edit by thomas
2079                 }
2080         }
2081         if (!ieee->queue_stop && ieee->tx_pending.txb)
2082                 ieee80211_resume_tx(ieee);
2083
2084         if (!ieee->queue_stop && netif_queue_stopped(ieee->dev)){
2085                 ieee->softmac_stats.swtxawake++;
2086                 netif_wake_queue(ieee->dev);
2087         }
2088
2089 exit :
2090         spin_unlock_irqrestore(&ieee->lock,flags);
2091 }
2092
2093
2094 void ieee80211_rtl_stop_queue(struct ieee80211_device *ieee)
2095 {
2096         //unsigned long flags;
2097         //spin_lock_irqsave(&ieee->lock,flags);
2098
2099         if (! netif_queue_stopped(ieee->dev)){
2100                 netif_stop_queue(ieee->dev);
2101                 ieee->softmac_stats.swtxstop++;
2102         }
2103         ieee->queue_stop = 1;
2104         //spin_unlock_irqrestore(&ieee->lock,flags);
2105
2106 }
2107
2108
2109 inline void ieee80211_randomize_cell(struct ieee80211_device *ieee)
2110 {
2111
2112         get_random_bytes(ieee->current_network.bssid, ETH_ALEN);
2113
2114         /* an IBSS cell address must have the two less significant
2115          * bits of the first byte = 2
2116          */
2117         ieee->current_network.bssid[0] &= ~0x01;
2118         ieee->current_network.bssid[0] |= 0x02;
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 swithced 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 tryed to associate when
2193          * we had just checked the ieee->state and we was going to start the
2194          * scan) beacause 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 procdure (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 (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
2811             param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
2812             param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
2813                 if (param->u.crypt.idx >= WEP_KEYS)
2814                         return -EINVAL;
2815                 crypt = &ieee->crypt[param->u.crypt.idx];
2816         } else {
2817                 return -EINVAL;
2818         }
2819
2820         if (strcmp(param->u.crypt.alg, "none") == 0) {
2821                 if (crypt) {
2822                         sec.enabled = 0;
2823                         // FIXME FIXME
2824                         //sec.encrypt = 0;
2825                         sec.level = SEC_LEVEL_0;
2826                         sec.flags |= SEC_ENABLED | SEC_LEVEL;
2827                         ieee80211_crypt_delayed_deinit(ieee, crypt);
2828                 }
2829                 goto done;
2830         }
2831         sec.enabled = 1;
2832 // FIXME FIXME
2833 //      sec.encrypt = 1;
2834         sec.flags |= SEC_ENABLED;
2835
2836         /* IPW HW cannot build TKIP MIC, host decryption still needed. */
2837         if (!(ieee->host_encrypt || ieee->host_decrypt) &&
2838             strcmp(param->u.crypt.alg, "TKIP"))
2839                 goto skip_host_crypt;
2840
2841         ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
2842         if (ops == NULL && strcmp(param->u.crypt.alg, "WEP") == 0)
2843                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
2844         else if (ops == NULL && strcmp(param->u.crypt.alg, "TKIP") == 0)
2845                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
2846         else if (ops == NULL && strcmp(param->u.crypt.alg, "CCMP") == 0)
2847                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
2848         if (ops == NULL) {
2849                 printk("unknown crypto alg '%s'\n", param->u.crypt.alg);
2850                 param->u.crypt.err = IEEE_CRYPT_ERR_UNKNOWN_ALG;
2851                 ret = -EINVAL;
2852                 goto done;
2853         }
2854
2855         if (*crypt == NULL || (*crypt)->ops != ops) {
2856                 struct ieee80211_crypt_data *new_crypt;
2857
2858                 ieee80211_crypt_delayed_deinit(ieee, crypt);
2859
2860                 new_crypt = kmalloc(sizeof(*new_crypt), GFP_KERNEL);
2861                 if (new_crypt == NULL) {
2862                         ret = -ENOMEM;
2863                         goto done;
2864                 }
2865                 memset(new_crypt, 0, sizeof(struct ieee80211_crypt_data));
2866                 new_crypt->ops = ops;
2867                 if (new_crypt->ops)
2868                         new_crypt->priv =
2869                                 new_crypt->ops->init(param->u.crypt.idx);
2870
2871                 if (new_crypt->priv == NULL) {
2872                         kfree(new_crypt);
2873                         param->u.crypt.err = IEEE_CRYPT_ERR_CRYPT_INIT_FAILED;
2874                         ret = -EINVAL;
2875                         goto done;
2876                 }
2877
2878                 *crypt = new_crypt;
2879         }
2880
2881         if (param->u.crypt.key_len > 0 && (*crypt)->ops->set_key &&
2882             (*crypt)->ops->set_key(param->u.crypt.key,
2883                                    param->u.crypt.key_len, param->u.crypt.seq,
2884                                    (*crypt)->priv) < 0) {
2885                 printk("key setting failed\n");
2886                 param->u.crypt.err = IEEE_CRYPT_ERR_KEY_SET_FAILED;
2887                 ret = -EINVAL;
2888                 goto done;
2889         }
2890
2891  skip_host_crypt:
2892         if (param->u.crypt.set_tx) {
2893                 ieee->tx_keyidx = param->u.crypt.idx;
2894                 sec.active_key = param->u.crypt.idx;
2895                 sec.flags |= SEC_ACTIVE_KEY;
2896         } else
2897                 sec.flags &= ~SEC_ACTIVE_KEY;
2898
2899         if (param->u.crypt.alg != NULL) {
2900                 memcpy(sec.keys[param->u.crypt.idx],
2901                        param->u.crypt.key,
2902                        param->u.crypt.key_len);
2903                 sec.key_sizes[param->u.crypt.idx] = param->u.crypt.key_len;
2904                 sec.flags |= (1 << param->u.crypt.idx);
2905
2906                 if (strcmp(param->u.crypt.alg, "WEP") == 0) {
2907                         sec.flags |= SEC_LEVEL;
2908                         sec.level = SEC_LEVEL_1;
2909                 } else if (strcmp(param->u.crypt.alg, "TKIP") == 0) {
2910                         sec.flags |= SEC_LEVEL;
2911                         sec.level = SEC_LEVEL_2;
2912                 } else if (strcmp(param->u.crypt.alg, "CCMP") == 0) {
2913                         sec.flags |= SEC_LEVEL;
2914                         sec.level = SEC_LEVEL_3;
2915                 }
2916         }
2917  done:
2918         if (ieee->set_security)
2919                 ieee->set_security(ieee->dev, &sec);
2920
2921         /* Do not reset port if card is in Managed mode since resetting will
2922          * generate new IEEE 802.11 authentication which may end up in looping
2923          * with IEEE 802.1X.  If your hardware requires a reset after WEP
2924          * configuration (for example... Prism2), implement the reset_port in
2925          * the callbacks structures used to initialize the 802.11 stack. */
2926         if (ieee->reset_on_keychange &&
2927             ieee->iw_mode != IW_MODE_INFRA &&
2928             ieee->reset_port &&
2929             ieee->reset_port(ieee->dev)) {
2930                 printk("reset_port failed\n");
2931                 param->u.crypt.err = IEEE_CRYPT_ERR_CARD_CONF_FAILED;
2932                 return -EINVAL;
2933         }
2934
2935         return ret;
2936 }
2937
2938 int ieee80211_wpa_supplicant_ioctl(struct ieee80211_device *ieee, struct iw_point *p)
2939 {
2940         struct ieee_param *param;
2941         int ret=0;
2942
2943         down(&ieee->wx_sem);
2944         //IEEE_DEBUG_INFO("wpa_supplicant: len=%d\n", p->length);
2945
2946         if (p->length < sizeof(struct ieee_param) || !p->pointer){
2947                 ret = -EINVAL;
2948                 goto out;
2949         }
2950
2951         param = kmalloc(p->length, GFP_KERNEL);
2952         if (param == NULL){
2953                 ret = -ENOMEM;
2954                 goto out;
2955         }
2956         if (copy_from_user(param, p->pointer, p->length)) {
2957                 kfree(param);
2958                 ret = -EFAULT;
2959                 goto out;
2960         }
2961
2962         switch (param->cmd) {
2963
2964         case IEEE_CMD_SET_WPA_PARAM:
2965                 ret = ieee80211_wpa_set_param(ieee, param->u.wpa_param.name,
2966                                         param->u.wpa_param.value);
2967                 break;
2968
2969         case IEEE_CMD_SET_WPA_IE:
2970                 ret = ieee80211_wpa_set_wpa_ie(ieee, param, p->length);
2971                 break;
2972
2973         case IEEE_CMD_SET_ENCRYPTION:
2974                 ret = ieee80211_wpa_set_encryption(ieee, param, p->length);
2975                 break;
2976
2977         case IEEE_CMD_MLME:
2978                 ret = ieee80211_wpa_mlme(ieee, param->u.mlme.command,
2979                                    param->u.mlme.reason_code);
2980                 break;
2981
2982         default:
2983                 printk("Unknown WPA supplicant request: %d\n",param->cmd);
2984                 ret = -EOPNOTSUPP;
2985                 break;
2986         }
2987
2988         if (ret == 0 && copy_to_user(p->pointer, param, p->length))
2989                 ret = -EFAULT;
2990
2991         kfree(param);
2992 out:
2993         up(&ieee->wx_sem);
2994
2995         return ret;
2996 }
2997
2998 void notify_wx_assoc_event(struct ieee80211_device *ieee)
2999 {
3000         union iwreq_data wrqu;
3001         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
3002         if (ieee->state == IEEE80211_LINKED)
3003                 memcpy(wrqu.ap_addr.sa_data, ieee->current_network.bssid, ETH_ALEN);
3004         else
3005                 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
3006         wireless_send_event(ieee->dev, SIOCGIWAP, &wrqu, NULL);
3007 }