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