modify esp8089 wifi driver for p2p fuction
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rockchip_wlan / esp8089 / esp_mac80211.c
1 /*
2  * Copyright (c) 2011 Espressif System.
3  *
4  *     MAC80211 support module
5  */
6 #include <linux/etherdevice.h>
7 #include <linux/workqueue.h>
8 #include <linux/nl80211.h>
9 #include <linux/ieee80211.h>
10 #include <linux/slab.h>
11 #include <net/cfg80211.h>
12 #include <net/mac80211.h>
13 #include <linux/version.h>
14 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31))
15 #include <net/regulatory.h>
16 #endif
17 /* for support scan in p2p concurrent */
18 #include <../net/mac80211/ieee80211_i.h>
19 #include "esp_pub.h"
20 #include "esp_sip.h"
21 #include "esp_ctrl.h"
22 #include "esp_sif.h"
23 #include "esp_debug.h"
24 #include "esp_wl.h"
25 #include "esp_utils.h"
26
27 #define ESP_IEEE80211_DBG esp_dbg
28
29 #define GET_NEXT_SEQ(seq) (((seq) +1) & 0x0fff)
30
31 #ifdef P2P_CONCURRENT
32 static u8 esp_mac_addr[ETH_ALEN * 2];
33 #endif
34 static u8 getaddr_index(u8 * addr, struct esp_pub *epub);
35
36 static
37 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0))
38 void
39 esp_op_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control, struct sk_buff *skb)
40 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
41 void
42 esp_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
43 #else
44 int
45 esp_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
46 #endif /* NEW_KERNEL */
47 {
48         struct esp_pub *epub = (struct esp_pub *)hw->priv;
49
50         ESP_IEEE80211_DBG(ESP_DBG_LOG, "%s enter\n", __func__);
51         if (!mod_support_no_txampdu() &&
52 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0))
53                         cfg80211_get_chandef_type(&epub->hw->conf.chandef) != NL80211_CHAN_NO_HT
54 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29))
55                         hw->conf.channel_type != NL80211_CHAN_NO_HT
56 #else
57                         !(hw->conf.flags&IEEE80211_CONF_SUPPORT_HT_MODE)
58 #endif                
59            ) {
60                 struct ieee80211_tx_info * tx_info = IEEE80211_SKB_CB(skb);
61                 struct ieee80211_hdr * wh = (struct ieee80211_hdr *)skb->data;
62                 if(ieee80211_is_data_qos(wh->frame_control)) {
63                         if(!(tx_info->flags & IEEE80211_TX_CTL_AMPDU)) {
64                                 u8 tidno = ieee80211_get_qos_ctl(wh)[0] & IEEE80211_QOS_CTL_TID_MASK;
65 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0))
66                                 struct esp_node * node = esp_get_node_by_addr(epub, wh->addr1);
67 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
68                                 struct ieee80211_sta *sta = tx_info->control.sta;
69                                 struct esp_node * node = (struct esp_node *)sta->drv_priv;
70 #else
71                                 struct esp_node * node = esp_get_node_by_addr(epub, wh->addr1);
72 #endif
73                                 struct esp_tx_tid *tid = &node->tid[tidno];
74                                 //record ssn
75                                 spin_lock_bh(&epub->tx_ampdu_lock);
76                                 tid->ssn = GET_NEXT_SEQ(le16_to_cpu(wh->seq_ctrl)>>4);
77                                 ESP_IEEE80211_DBG(ESP_DBG_TRACE, "tidno:%u,ssn:%u\n", tidno, tid->ssn);
78                                 spin_unlock_bh(&epub->tx_ampdu_lock);
79                         } else {
80                                 ESP_IEEE80211_DBG(ESP_DBG_TRACE, "tx ampdu pkt, sn:%u, %u\n", le16_to_cpu(wh->seq_ctrl)>>4, skb->len);
81                         }
82                 }
83         }
84
85 #ifdef GEN_ERR_CHECKSUM
86         esp_gen_err_checksum(skb);
87 #endif
88
89         sip_tx_data_pkt_enqueue(epub, skb);
90         if (epub)
91 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32))
92                 ieee80211_queue_work(hw, &epub->tx_work);
93 #else
94                 queue_work(hw->workqueue,&epub->tx_work);    
95 #endif
96 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39))
97         return NETDEV_TX_OK;
98 #endif /*2.6.39*/
99 }
100
101 static int esp_op_start(struct ieee80211_hw *hw)
102 {
103         struct esp_pub *epub;
104
105         ESP_IEEE80211_DBG(ESP_DBG_OP, "%s\n", __func__);
106
107         if (!hw) {
108                 ESP_IEEE80211_DBG(ESP_DBG_ERROR, "%s no hw!\n", __func__);
109                 return -EINVAL;
110         }
111
112         epub = (struct esp_pub *)hw->priv;
113
114         if (!epub) {
115                 ESP_IEEE80211_DBG(ESP_DBG_ERROR, "%s no epub!\n", __func__);
116                 return EINVAL;
117         }
118         /*add rfkill poll function*/
119
120         atomic_set(&epub->wl.off, 0);
121 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31))
122         wiphy_rfkill_start_polling(hw->wiphy);
123 #endif
124         return 0;
125 }
126
127 static void esp_op_stop(struct ieee80211_hw *hw)
128 {
129         struct esp_pub *epub;
130
131         ESP_IEEE80211_DBG(ESP_DBG_OP, "%s\n", __func__);
132
133         if (!hw) {
134                 ESP_IEEE80211_DBG(ESP_DBG_ERROR, "%s no hw!\n", __func__);
135                 return;
136         }
137
138         epub = (struct esp_pub *)hw->priv;
139
140         if (!epub) {
141                 ESP_IEEE80211_DBG(ESP_DBG_ERROR, "%s no epub!\n", __func__);
142                 return;
143         }
144
145         atomic_set(&epub->wl.off, 1);
146
147 #ifdef HOST_RESET_BUG
148         mdelay(200);
149 #endif
150
151         if (epub->wl.scan_req) {
152                 hw_scan_done(epub, true);
153                 epub->wl.scan_req=NULL;
154                 //msleep(2);
155         }
156 }
157
158 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 39))        
159 #ifdef CONFIG_PM
160 static int esp_op_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
161 {
162         esp_dbg(ESP_DBG_OP, "%s\n", __func__);
163
164         return 0;
165 }
166
167 static int esp_op_resume(struct ieee80211_hw *hw)
168 {
169         esp_dbg(ESP_DBG_OP, "%s\n", __func__);
170
171         return 0;
172 }
173 #endif //CONFIG_PM
174 #endif
175
176 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 34))
177 static int esp_op_add_interface(struct ieee80211_hw *hw,
178                 struct ieee80211_if_init_conf *conf)
179 #else
180 static int esp_op_add_interface(struct ieee80211_hw *hw,
181                 struct ieee80211_vif *vif)
182 #endif
183 {
184         struct esp_pub *epub = (struct esp_pub *)hw->priv;
185 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 34))
186         struct ieee80211_vif *vif = conf->vif;
187 #endif
188         struct esp_vif *evif = (struct esp_vif *)vif->drv_priv;
189         struct sip_cmd_setvif svif;
190
191 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 34))
192         ESP_IEEE80211_DBG(ESP_DBG_OP, "%s enter: type %d, addr %pM\n", __func__, vif->type, conf->mac_addr);
193 #else
194         ESP_IEEE80211_DBG(ESP_DBG_OP, "%s enter: type %d, addr %pM\n", __func__, vif->type, vif->addr);
195 #endif
196
197         memset(&svif, 0, sizeof(struct sip_cmd_setvif));
198 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 34))
199         memcpy(svif.mac, conf->mac_addr, ETH_ALEN);
200         evif->index = svif.index = getaddr_index(conf->mac_addr, epub);
201 #else
202         memcpy(svif.mac, vif->addr, ETH_ALEN);
203         evif->index = svif.index = getaddr_index(vif->addr, epub);
204 #endif
205         evif->epub = epub;
206         epub->vif = vif;
207         svif.set = 1;
208         if((1 << svif.index) & epub->vif_slot){
209                 ESP_IEEE80211_DBG(ESP_DBG_ERROR, "%s interface %d already used\n", __func__, svif.index);
210                 return -EOPNOTSUPP;
211         }
212         epub->vif_slot |= 1 << svif.index;
213
214         if (svif.index == ESP_PUB_MAX_VIF) {
215                 ESP_IEEE80211_DBG(ESP_DBG_ERROR, "%s only support MAX %d interface\n", __func__, ESP_PUB_MAX_VIF);
216                 return -EOPNOTSUPP;
217         }
218
219         switch (vif->type) {
220                 case NL80211_IFTYPE_STATION:
221                         //if (svif.index == 1)
222                         //      vif->type = NL80211_IFTYPE_UNSPECIFIED;
223                         ESP_IEEE80211_DBG(ESP_SHOW, "%s STA \n", __func__);
224                         svif.op_mode = 0;
225                         svif.is_p2p = 0;
226                         break;
227                 case NL80211_IFTYPE_AP:
228                         ESP_IEEE80211_DBG(ESP_SHOW, "%s AP \n", __func__);
229                         svif.op_mode = 1;
230                         svif.is_p2p = 0;
231                         break;
232 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
233                 case NL80211_IFTYPE_P2P_CLIENT:
234                         ESP_IEEE80211_DBG(ESP_SHOW, "%s P2P_CLIENT \n", __func__);
235                         svif.op_mode = 0;
236                         svif.is_p2p = 1;
237                         break;
238                 case NL80211_IFTYPE_P2P_GO:
239                         ESP_IEEE80211_DBG(ESP_SHOW, "%s P2P_GO \n", __func__);
240                         svif.op_mode = 1;
241                         svif.is_p2p = 1;
242                         break;
243 #endif
244                 case NL80211_IFTYPE_UNSPECIFIED:
245                 case NL80211_IFTYPE_ADHOC:
246                 case NL80211_IFTYPE_AP_VLAN:
247                 case NL80211_IFTYPE_WDS:
248                 case NL80211_IFTYPE_MONITOR:
249                 default:
250                         ESP_IEEE80211_DBG(ESP_DBG_ERROR, "%s does NOT support type %d\n", __func__, vif->type);
251                         return -EOPNOTSUPP;
252         }
253
254         sip_cmd(epub, SIP_CMD_SETVIF, (u8 *)&svif, sizeof(struct sip_cmd_setvif));
255         return 0;
256 }
257
258 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
259 static int esp_op_change_interface(struct ieee80211_hw *hw,
260                                    struct ieee80211_vif *vif,
261                                    enum nl80211_iftype new_type, bool p2p)
262 {
263         struct esp_pub *epub = (struct esp_pub *)hw->priv;
264         struct esp_vif *evif = (struct esp_vif *)vif->drv_priv;
265         struct sip_cmd_setvif svif;
266         ESP_IEEE80211_DBG(ESP_DBG_OP, "%s enter,change to if:%d \n", __func__, new_type);
267         
268         if (new_type == NL80211_IFTYPE_AP) {
269                 ESP_IEEE80211_DBG(ESP_SHOW, "%s enter,change to AP \n", __func__);
270         }
271
272         if (vif->type != new_type) {
273                 ESP_IEEE80211_DBG(ESP_SHOW, "%s type from %d to %d\n", __func__, vif->type, new_type);
274         }
275         
276         memset(&svif, 0, sizeof(struct sip_cmd_setvif));
277         memcpy(svif.mac, vif->addr, ETH_ALEN);
278         svif.index = evif->index;
279         svif.set = 2;
280         
281         switch (new_type) {
282         case NL80211_IFTYPE_STATION:
283                 svif.op_mode = 0;
284                 svif.is_p2p = p2p;
285                 break;
286         case NL80211_IFTYPE_AP:
287                 svif.op_mode = 1;
288                 svif.is_p2p = p2p;
289                 break;
290         case NL80211_IFTYPE_P2P_CLIENT:
291                 svif.op_mode = 0;
292                 svif.is_p2p = 1;
293                 break;
294         case NL80211_IFTYPE_P2P_GO:
295                 svif.op_mode = 1;
296                 svif.is_p2p = 1;
297                 break;
298         case NL80211_IFTYPE_UNSPECIFIED:
299         case NL80211_IFTYPE_ADHOC:
300         case NL80211_IFTYPE_AP_VLAN:
301         case NL80211_IFTYPE_WDS:
302         case NL80211_IFTYPE_MONITOR:
303         default:
304                 ESP_IEEE80211_DBG(ESP_DBG_ERROR, "%s does NOT support type %d\n", __func__, vif->type);
305                 return -EOPNOTSUPP;
306         }
307         sip_cmd(epub, SIP_CMD_SETVIF, (u8 *)&svif, sizeof(struct sip_cmd_setvif));
308         return 0;
309 }
310 #endif
311
312 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 34))
313 static void esp_op_remove_interface(struct ieee80211_hw *hw,
314                                     struct ieee80211_if_init_conf *conf)
315 #else
316 static void esp_op_remove_interface(struct ieee80211_hw *hw,
317                                     struct ieee80211_vif *vif)
318 #endif
319 {
320         struct esp_pub *epub = (struct esp_pub *)hw->priv;
321 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 34))
322         struct ieee80211_vif *vif = conf->vif;
323 #endif
324         struct esp_vif *evif = (struct esp_vif *)vif->drv_priv;
325         struct sip_cmd_setvif svif;
326
327 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 30))
328         ESP_IEEE80211_DBG(ESP_DBG_OP, "%s enter, vif addr %pM\n", __func__, conf->mac_addr);
329 #elif (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 34))
330         ESP_IEEE80211_DBG(ESP_DBG_OP, "%s enter, vif addr %pM, beacon enable %x\n", __func__, conf->mac_addr, vif->bss_conf.enable_beacon);
331 #else
332         ESP_IEEE80211_DBG(ESP_DBG_OP, "%s enter, vif addr %pM, beacon enable %x\n", __func__, vif->addr, vif->bss_conf.enable_beacon);
333 #endif
334
335         memset(&svif, 0, sizeof(struct sip_cmd_setvif));
336         svif.index = evif->index;
337         epub->vif_slot &= ~(1 << svif.index);
338
339         if(evif->ap_up){
340                 evif->beacon_interval = 0;
341                 del_timer_sync(&evif->beacon_timer);
342                 evif->ap_up = false;
343         }
344         epub->vif = NULL;
345         evif->epub = NULL;
346
347         sip_cmd(epub, SIP_CMD_SETVIF, (u8 *)&svif, sizeof(struct sip_cmd_setvif));
348
349         /* clean up tx/rx queue */
350
351 }
352
353 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29))
354 static void drv_handle_beacon(unsigned long data)
355 {
356         struct ieee80211_vif *vif = (struct ieee80211_vif *) data;
357         struct esp_vif *evif = (struct esp_vif *)vif->drv_priv;
358         struct sk_buff *beacon;
359         struct sk_buff *skb;
360         static int dbgcnt = 0;
361
362         if(evif->epub == NULL)
363                 return;
364         beacon = ieee80211_beacon_get(evif->epub->hw, vif);
365
366         if (beacon && !(dbgcnt++ % 600)) {
367                 ESP_IEEE80211_DBG(ESP_SHOW, " beacon length:%d,fc:0x%x\n", beacon->len,
368                         ((struct ieee80211_mgmt *)(beacon->data))->frame_control);
369
370         }
371
372         sip_tx_data_pkt_enqueue(evif->epub, beacon);
373
374 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))    
375         mod_timer(&evif->beacon_timer, jiffies+msecs_to_jiffies(vif->bss_conf.beacon_int));
376 #else
377     mod_timer(&evif->beacon_timer, jiffies+msecs_to_jiffies(evif->beacon_interval));
378 #endif
379         //FIXME:the packets must be sent at home channel
380         //send buffer mcast frames
381         skb = ieee80211_get_buffered_bc(evif->epub->hw, vif);
382         while (skb) {
383                 sip_tx_data_pkt_enqueue(evif->epub, skb);
384                 skb = ieee80211_get_buffered_bc(evif->epub->hw, vif);
385         }
386 }
387
388 static void init_beacon_timer(struct ieee80211_vif *vif)
389 {
390         struct esp_vif *evif = (struct esp_vif *)vif->drv_priv;
391
392         ESP_IEEE80211_DBG(ESP_DBG_OP, " %s enter: beacon interval %x\n", __func__, evif->beacon_interval);
393
394         init_timer(&evif->beacon_timer);  //TBD, not init here...
395         evif->beacon_timer.expires=jiffies+msecs_to_jiffies(evif->beacon_interval*1024/1000);
396         evif->beacon_timer.data = (unsigned long) vif;
397         evif->beacon_timer.function = drv_handle_beacon;
398         add_timer(&evif->beacon_timer);
399 }
400 #endif
401
402 /*
403 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
404     static void init_beacon_timer(struct ieee80211_vif *vif)
405 #else
406     static void init_beacon_timer(struct ieee80211_conf *conf)
407 #endif
408 {
409         struct esp_vif *evif = (struct esp_vif *)vif->drv_priv;
410 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
411         ESP_IEEE80211_DBG(ESP_DBG_OP, " %s enter: beacon interval %x\n", __func__, vif->bss_conf.beacon_int);
412 #else
413         ESP_IEEE80211_DBG(ESP_DBG_OP, " %s enter: beacon interval %x\n", __func__, conf->beacon_int);
414 #endif
415         init_timer(&evif->beacon_timer);  //TBD, not init here...
416 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
417         evif->beacon_timer.expires=jiffies+msecs_to_jiffies(vif->bss_conf.beacon_int*102/100);
418         evif->beacon_timer.data = (unsigned long) vif;
419 #else
420         evif->beacon_timer.expires=jiffies+msecs_to_jiffies(conf->beacon_int*102/100);
421         evif->beacon_timer.data = (unsigned long) conf;
422 #endif
423         //evif->beacon_timer.data = (unsigned long) vif;
424         evif->beacon_timer.function = drv_handle_beacon;
425         add_timer(&evif->beacon_timer);
426 }
427 */
428
429 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29))
430 static int esp_op_config(struct ieee80211_hw *hw, u32 changed)
431 #else
432 static int esp_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
433 #endif
434 {
435         //struct ieee80211_conf *conf = &hw->conf;
436
437         struct esp_pub *epub = (struct esp_pub *)hw->priv;
438 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29))
439         //struct esp_vif *evif = (struct esp_vif *)epub->vif->drv_priv;
440 #endif
441
442 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29))
443         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "%s enter 0x%08x\n", __func__, changed);
444
445         if (changed&IEEE80211_CONF_CHANGE_CHANNEL) {
446                 sip_send_config(epub, &hw->conf);
447         }
448 #else
449         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "%s enter 0x%08x\n", __func__, conf->flags);
450         sip_send_config(epub, &hw->conf);
451 #endif
452
453 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29))
454         //evif->beacon_interval = conf->beacon_int;
455         //init_beacon_timer(epub->vif);
456 #endif
457
458
459 #if 0
460         if (changed & IEEE80211_CONF_CHANGE_PS) {
461                 struct esp_ps *ps = &epub->ps;
462
463                 ps->dtim_period = conf->ps_dtim_period;
464                 ps->max_sleep_period = conf->max_sleep_period;
465                 esp_ps_config(epub, ps, (conf->flags & IEEE80211_CONF_PS));
466         }
467 #endif
468     return 0;
469 }
470
471 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28))
472 static int esp_op_config_interface (struct ieee80211_hw *hw, 
473                                     struct ieee80211_vif *vif,
474                                     struct ieee80211_if_conf *conf)
475 {
476         // assoc = 2 means AP
477         struct esp_pub *epub = (struct esp_pub *)hw->priv;
478         struct esp_vif *evif = (struct esp_vif *)vif->drv_priv;
479         //struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
480         ESP_IEEE80211_DBG(ESP_DBG_OP, " %s enter: changed %x, bssid %pM,vif->type = %d\n", __func__, conf->changed, conf->bssid,vif->type);
481
482         if(conf->bssid)
483                 memcpy(epub->wl.bssid, conf->bssid, ETH_ALEN);
484         else
485                 memset(epub->wl.bssid, 0, ETH_ALEN);
486
487         if(vif->type == NL80211_IFTYPE_AP){
488                 if((conf->changed & IEEE80211_IFCC_BEACON)){
489                         sip_send_bss_info_update(epub, evif, (u8*)conf->bssid, 2);
490                         //evif->beacon_interval = conf->beacon_int;
491                 }
492                 else{
493                         ESP_IEEE80211_DBG(ESP_DBG_ERROR, "%s op----1-- mode unspecified\n", __func__);
494                 }
495         }
496         else{
497                 ESP_IEEE80211_DBG(ESP_DBG_ERROR, "%s op----2-- mode unspecified\n", __func__);
498         }
499         return 0;
500 }
501 #endif
502
503 static void esp_op_bss_info_changed(struct ieee80211_hw *hw,
504                                     struct ieee80211_vif *vif,
505                                     struct ieee80211_bss_conf *info,
506                                     u32 changed)
507 {
508         struct esp_pub *epub = (struct esp_pub *)hw->priv;
509         struct esp_vif *evif = (struct esp_vif *)vif->drv_priv;
510 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28))
511
512     struct sta_info *sta;
513     struct esp_node *node;
514     struct ieee80211_ht_info *ht_info;   
515
516     u8 addr_0[ETH_ALEN];
517     memset(addr_0,0,ETH_ALEN);
518
519     ESP_IEEE80211_DBG(ESP_DBG_OP,"%s enter, changed %x\n",__func__,changed);
520
521     if((changed & BSS_CHANGED_ASSOC) && (memcmp(epub->wl.bssid,addr_0, ETH_ALEN)))
522     {
523
524         rcu_read_lock();
525         node = esp_get_node_by_addr(epub, epub->wl.bssid );
526         sta = sta_info_get(container_of(hw,struct ieee80211_local,hw), epub->wl.bssid);
527
528         ht_info = &sta->ht_info;
529         memcpy(node->supp_rates, sta->supp_rates, sizeof(node->supp_rates));
530         memcpy(&node->ht_info.cap, &ht_info->cap, sizeof(node->ht_info.cap));
531         memcpy(&node->ht_info.ht_supported, &ht_info->ht_supported, sizeof(node->ht_info.ht_supported));
532         memcpy(&node->ht_info.ampdu_density, &ht_info->ampdu_density, sizeof(node->ht_info.ampdu_density));
533         memcpy(&node->ht_info.ampdu_factor, &ht_info->ampdu_factor, sizeof(node->ht_info.ampdu_factor));
534         if(sta->aid == 0)
535             memcpy(&node->aid, &info->aid, sizeof(node->aid));
536         else
537             memcpy(&node->aid, &sta->aid, sizeof(node->aid));
538         rcu_read_unlock();
539
540         sip_send_set_sta(epub, evif->index, 1, node, vif, (u8)node->index);
541     }
542 #else
543 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
544         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
545 #endif
546
547         // ieee80211_bss_conf(include/net/mac80211.h) is included in ieee80211_sub_if_data(net/mac80211/ieee80211_i.h) , does bssid=ieee80211_if_ap's ssid ?
548         // in 2.6.27, ieee80211_sub_if_data has ieee80211_bss_conf while in 2.6.32 ieee80211_sub_if_data don't have ieee80211_bss_conf
549         // in 2.6.27, ieee80211_bss_conf->enable_beacon don't exist, does it mean it support beacon always?
550         // ESP_IEEE80211_DBG(ESP_DBG_OP, " %s enter: vif addr %pM, changed %x, assoc %x, bssid %pM\n", __func__, vif->addr, changed, info->assoc, info->bssid);
551         // sdata->u.sta.bssid
552
553         ESP_IEEE80211_DBG(ESP_DBG_OP, " %s enter: changed %x, assoc %x, bssid %pM\n", __func__, changed, info->assoc, info->bssid);
554
555         if (vif->type == NL80211_IFTYPE_STATION) {
556                 if ((changed & BSS_CHANGED_BSSID) ||
557                                 ((changed & BSS_CHANGED_ASSOC) && (info->assoc)))
558                 {
559                         ESP_IEEE80211_DBG(ESP_DBG_TRACE, " %s STA change bssid or assoc\n", __func__);
560                         memcpy(epub->wl.bssid, (u8*)info->bssid, ETH_ALEN);
561                         sip_send_bss_info_update(epub, evif, (u8*)info->bssid, info->assoc);
562                 } else if ((changed & BSS_CHANGED_ASSOC) && (!info->assoc)) {
563                         ESP_IEEE80211_DBG(ESP_DBG_TRACE, " %s STA change disassoc\n", __func__);
564                         memset(epub->wl.bssid, 0, ETH_ALEN);
565                         sip_send_bss_info_update(epub, evif, (u8*)info->bssid, info->assoc);
566                 } else {
567                         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "%s wrong mode of STA mode\n", __func__);
568                 }
569         } else if (vif->type == NL80211_IFTYPE_AP) {
570                 if ((changed & BSS_CHANGED_BEACON_ENABLED) ||
571                                 (changed & BSS_CHANGED_BEACON_INT)) {
572                         ESP_IEEE80211_DBG(ESP_DBG_TRACE, " %s AP change enable %d, interval is %d, bssid %pM\n", __func__, info->enable_beacon, info->beacon_int, info->bssid);
573                         if (info->enable_beacon && evif->ap_up != true) {
574                                 evif->beacon_interval = info->beacon_int;
575                                 init_beacon_timer(vif);
576                                 sip_send_bss_info_update(epub, evif, (u8*)info->bssid, 2);
577                                 evif->ap_up = true;
578                         } else if (!info->enable_beacon && evif->ap_up &&
579 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
580                     !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)
581 #else
582                     true
583 #endif
584                     ) {
585                                 ESP_IEEE80211_DBG(ESP_DBG_TRACE, " %s AP disable beacon, interval is %d\n", __func__, info->beacon_int);
586                                 evif->beacon_interval = 0;
587                                 del_timer_sync(&evif->beacon_timer);
588                                 sip_send_bss_info_update(epub, evif, (u8*)info->bssid, 2);
589                                 evif->ap_up = false;
590                         }
591                 }
592         } else {
593                 ESP_IEEE80211_DBG(ESP_DBG_ERROR, "%s op mode unspecified\n", __func__);
594         }
595 #endif
596 }
597
598
599 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32))
600 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 35))
601 static u64 esp_op_prepare_multicast(struct ieee80211_hw *hw,
602                                     int mc_count, struct dev_addr_list *mc_list)
603 {
604         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "%s enter \n", __func__);
605
606         return 0;
607 }
608 #else
609 static u64 esp_op_prepare_multicast(struct ieee80211_hw *hw,
610                                     struct netdev_hw_addr_list *mc_list)
611 {
612         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "%s enter \n", __func__);
613
614         return 0;
615 }
616
617 #endif /* NEW_KERNEL && KERNEL_35 */
618 #endif
619
620 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32))
621 static void esp_op_configure_filter(struct ieee80211_hw *hw,
622                                     unsigned int changed_flags,
623                                     unsigned int *total_flags,
624                                     u64 multicast)
625 #else
626 static void esp_op_configure_filter(struct ieee80211_hw *hw,
627                                     unsigned int changed_flags,
628                                     unsigned int *total_flags,
629                                     int mc_count,
630                                     struct dev_addr_list *mc_list)
631 #endif
632 {
633         struct esp_pub *epub = (struct esp_pub *)hw->priv;
634
635         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "%s enter \n", __func__);
636
637         epub->rx_filter = 0;
638
639         if (*total_flags & FIF_PROMISC_IN_BSS)
640                 epub->rx_filter |= FIF_PROMISC_IN_BSS;
641
642         if (*total_flags & FIF_ALLMULTI)
643                 epub->rx_filter |= FIF_ALLMULTI;
644
645         *total_flags = epub->rx_filter;
646 }
647
648 #if 0
649 static int esp_op_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
650                           bool set)
651 {
652         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "%s enter \n", __func__);
653
654         return 0;
655 }
656 #endif
657
658 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30))
659 static int esp_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
660                           struct ieee80211_vif *vif, struct ieee80211_sta *sta,
661                           struct ieee80211_key_conf *key)
662 #else
663 static int esp_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
664                           const u8 *local_address,const u8 *address,
665                           struct ieee80211_key_conf *key)
666 #endif
667 {
668         u8 i;
669         int  ret;
670         struct esp_pub *epub = (struct esp_pub *)hw->priv;
671 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30))
672         struct esp_vif *evif = (struct esp_vif *)vif->drv_priv;
673         u8 ifidx = evif->index;
674 #else
675         u8 ifidx = getaddr_index((u8 *)(local_address), epub); 
676 #endif
677         u8 *peer_addr,isvalid;
678
679 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
680         ESP_IEEE80211_DBG(ESP_DBG_OP, "%s enter, flags = %x keyindx = %x cmd = %x mac = %pM cipher = %x\n", __func__, key->flags, key->keyidx, cmd, vif->addr, key->cipher);
681 #else
682         ESP_IEEE80211_DBG(ESP_DBG_OP, "%s enter, flags = %x keyindx = %x cmd = %x cipher = %x\n", __func__, key->flags, key->keyidx, cmd, key->alg);
683 #endif
684
685         key->flags= key->flags|IEEE80211_KEY_FLAG_GENERATE_IV;
686
687 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30))
688         if (sta) {
689                 if (memcmp(sta->addr, epub->wl.bssid, ETH_ALEN))
690                         peer_addr = sta->addr;
691                 else
692                         peer_addr = epub->wl.bssid;
693         } else {
694                 peer_addr=epub->wl.bssid;
695         }
696 #else
697         peer_addr = (u8 *)address;
698 #endif
699         isvalid = (cmd==SET_KEY) ? 1 : 0;
700
701 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
702         if ((key->flags&IEEE80211_KEY_FLAG_PAIRWISE) || (key->cipher == WLAN_CIPHER_SUITE_WEP40 || key->cipher == WLAN_CIPHER_SUITE_WEP104))
703 #else
704         if ((key->flags&IEEE80211_KEY_FLAG_PAIRWISE) || (key->alg == ALG_WEP))
705 #endif
706             {
707                 if (isvalid) {
708                         for (i = 0; i < 19; i++) {
709                                 if (epub->hi_map[i].flag == 0) {
710                                         epub->hi_map[i].flag = 1;
711                                         key->hw_key_idx = i + 6;
712                                         memcpy(epub->hi_map[i].mac, peer_addr, ETH_ALEN);
713                                         break;
714                                 }
715                         }
716                 } else {
717                         u8 index = key->hw_key_idx - 6;
718                         epub->hi_map[index].flag = 0;
719                         memset(epub->hi_map[index].mac, 0, ETH_ALEN);
720                 }
721         } else {
722                 if(isvalid){
723                         for(i = 0; i < 2; i++)
724                                 if (epub->low_map[ifidx][i].flag == 0) {
725                                         epub->low_map[ifidx][i].flag = 1;
726                                         key->hw_key_idx = i + ifidx * 2 + 2;
727                                         memcpy(epub->low_map[ifidx][i].mac, peer_addr, ETH_ALEN);
728                                         break;
729                                 }
730                 } else {
731                         u8 index = key->hw_key_idx - 2 - ifidx * 2;
732                                 epub->low_map[ifidx][index].flag = 0;
733                                 memset(epub->low_map[ifidx][index].mac, 0, ETH_ALEN);
734                 }
735                 //key->hw_key_idx = key->keyidx + ifidx * 2 + 1;
736         }
737
738         if (key->hw_key_idx >= 6) {
739                 /*send sub_scan task to target*/
740                 //epub->wl.ptk = (cmd==SET_KEY) ? key : NULL;
741                 if(isvalid)
742                         atomic_inc(&epub->wl.ptk_cnt);
743                 else
744                         atomic_dec(&epub->wl.ptk_cnt);
745 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
746                 if (key->cipher == WLAN_CIPHER_SUITE_WEP40 || key->cipher == WLAN_CIPHER_SUITE_WEP104)
747 #else
748                 if (key->alg == ALG_WEP)
749 #endif
750                 {
751                         if(isvalid)
752                                 atomic_inc(&epub->wl.gtk_cnt);
753                         else
754                                 atomic_dec(&epub->wl.gtk_cnt);
755                 }
756         } else {
757                 /*send sub_scan task to target*/
758                 if(isvalid)
759                         atomic_inc(&epub->wl.gtk_cnt);
760                 else
761                         atomic_dec(&epub->wl.gtk_cnt);
762
763 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
764                 if((key->cipher == WLAN_CIPHER_SUITE_WEP40 || key->cipher == WLAN_CIPHER_SUITE_WEP104))
765 #else
766         if((key->alg == ALG_WEP))
767 #endif
768                 {
769                         if(isvalid)
770                                 atomic_inc(&epub->wl.ptk_cnt);
771                         else
772                                 atomic_dec(&epub->wl.ptk_cnt);
773                         //epub->wl.ptk = (cmd==SET_KEY) ? key : NULL;
774                 }
775         }
776
777         ret = sip_send_setkey(epub, ifidx, peer_addr, key, isvalid);
778
779 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 35))
780         if((key->cipher == WLAN_CIPHER_SUITE_TKIP || key->cipher == WLAN_CIPHER_SUITE_TKIP))
781 #else
782         if((key->alg == ALG_TKIP))
783 #endif
784         {
785                 if(ret == 0)
786                         atomic_set(&epub->wl.tkip_key_set, 1);
787         }
788
789         ESP_IEEE80211_DBG(ESP_DBG_OP, "%s exit\n", __func__);
790         return ret;
791 }
792
793 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 34))
794 static void esp_op_update_tkip_key(struct ieee80211_hw *hw,
795                                    struct ieee80211_key_conf *conf, const u8 *address,
796                                    u32 iv32, u16 *phase1key)
797 {
798         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "%s enter \n", __func__);
799
800 }
801 #else
802 static void esp_op_update_tkip_key(struct ieee80211_hw *hw,
803                                    struct ieee80211_vif *vif,
804                                    struct ieee80211_key_conf *conf,
805                                    struct ieee80211_sta *sta,
806                                    u32 iv32, u16 *phase1key)
807 {
808         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "%s enter \n", __func__);
809
810 }
811 #endif /* KERNEL_35 NEW_KERNEL*/
812
813
814 void hw_scan_done(struct esp_pub *epub, bool aborted)
815 {
816         cancel_delayed_work_sync(&epub->scan_timeout_work);
817
818         ASSERT(epub->wl.scan_req != NULL);
819
820 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30))
821         ieee80211_scan_completed(epub->hw, aborted);
822 #else
823         ieee80211_scan_completed(epub->hw);
824 #endif
825         if (test_and_clear_bit(ESP_WL_FLAG_STOP_TXQ, &epub->wl.flags)) {
826                 sip_trigger_txq_process(epub->sip);
827         }
828 }
829
830 static void hw_scan_timeout_report(struct work_struct *work)
831 {
832         struct esp_pub *epub =
833                 container_of(work, struct esp_pub, scan_timeout_work.work);
834         bool aborted;
835
836         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "eagle hw scan done\n");
837
838         if (test_and_clear_bit(ESP_WL_FLAG_STOP_TXQ, &epub->wl.flags)) {
839                 sip_trigger_txq_process(epub->sip);
840         }
841         /*check if normally complete or aborted like timeout/hw error */
842         aborted = (epub->wl.scan_req) ? true : false;
843
844         if (aborted==true) {
845                 epub->wl.scan_req = NULL;
846         }
847
848 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30))
849         ieee80211_scan_completed(epub->hw, aborted);
850 #else
851         ieee80211_scan_completed(epub->hw);
852 #endif  
853 }
854
855 #if 0
856 static void esp_op_sw_scan_start(struct ieee80211_hw *hw)
857 {}
858
859 static void esp_op_sw_scan_complete(struct ieee80211_hw *hw)
860 {
861         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "%s enter \n", __func__);
862 }
863 #endif
864
865 #if 0
866 static int esp_op_get_stats(struct ieee80211_hw *hw,
867                             struct ieee80211_low_level_stats *stats)
868 {
869         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "%s enter \n", __func__);
870
871         return 0;
872 }
873
874 static void esp_op_get_tkip_seq(struct ieee80211_hw *hw, u8 hw_key_idx,
875                                 u32 *iv32, u16 *iv16)
876 {
877         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "%s enter \n", __func__);
878 }
879 #endif
880
881 static int esp_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
882 {
883         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "%s enter \n", __func__);
884
885         return 0;
886 }
887
888 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
889 static int esp_node_attach(struct ieee80211_hw *hw, u8 ifidx, struct ieee80211_sta *sta)
890 #else
891 static int esp_node_attach(struct ieee80211_hw *hw, u8 ifidx, const u8 *addr)
892 #endif
893 {
894         struct esp_pub *epub = (struct esp_pub *)hw->priv;
895         struct esp_node *node;
896         u8 tidno;
897         struct esp_tx_tid *tid;
898             int i;
899 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28))
900         struct sta_info *info = sta_info_get(container_of(hw,struct ieee80211_local,hw),(u8 *)addr);
901         struct ieee80211_ht_info *ht_info = &info->ht_info;
902 #endif
903
904         spin_lock_bh(&epub->tx_ampdu_lock);
905
906         if(hweight32(epub->enodes_maps[ifidx]) < ESP_PUB_MAX_STA && (i = ffz(epub->enodes_map)) < ESP_PUB_MAX_STA + 1){
907                 epub->enodes_map |= (1 << i);
908                 epub->enodes_maps[ifidx] |= (1 << i);
909 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
910                 node = (struct esp_node *)sta->drv_priv;
911                 epub->enodes[i] = node;
912                 node->sta = sta;
913 #else
914                 node = &epub->nodes[i];
915                 epub->enodes[i] = node;
916                 memcpy(node->addr, addr, ETH_ALEN);
917                 memcpy(&node->aid, &info->aid, sizeof(node->aid)); 
918                 memcpy(node->supp_rates, info->supp_rates, sizeof(node->supp_rates));
919                 memcpy(&node->ht_info.cap, &ht_info->cap, sizeof(node->ht_info.cap));
920                 memcpy(&node->ht_info.ht_supported, &ht_info->ht_supported, sizeof(node->ht_info.ht_supported));
921                 memcpy(&node->ht_info.ampdu_factor, &ht_info->ampdu_factor, sizeof(node->ht_info.ampdu_factor));
922                 memcpy(&node->ht_info.ampdu_density, &ht_info->ampdu_density, sizeof(node->ht_info.ampdu_density));
923 #endif
924                 node->ifidx = ifidx;
925                 node->index = i;
926
927                 for(tidno = 0, tid = &node->tid[tidno]; tidno < WME_NUM_TID; tidno++) {
928                 tid->ssn = 0;
929                 tid->cnt = 0;
930                 tid->state = ESP_TID_STATE_INIT;
931         }
932
933
934         } else {
935                 i = -1;
936         }
937
938         spin_unlock_bh(&epub->tx_ampdu_lock);
939         return i;
940 }
941
942 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
943 static int esp_node_detach(struct ieee80211_hw *hw, u8 ifidx, struct ieee80211_sta *sta)
944 #else
945 static int esp_node_detach(struct ieee80211_hw *hw, u8 ifidx, const u8 *addr)
946 #endif
947 {
948     struct esp_pub *epub = (struct esp_pub *)hw->priv;
949         u8 map;
950         int i;
951     struct esp_node *node = NULL;
952
953         spin_lock_bh(&epub->tx_ampdu_lock);
954         map = epub->enodes_maps[ifidx];
955         while(map != 0){
956                 i = ffs(map) - 1;
957 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
958                 if(epub->enodes[i]->sta == sta){
959                         epub->enodes[i]->sta = NULL;
960 #else
961                 if(memcmp(epub->enodes[i]->addr, addr, ETH_ALEN) == 0){
962 #endif
963                         node = epub->enodes[i];
964                         epub->enodes[i] = NULL;
965                         epub->enodes_map &= ~(1 << i);
966                         epub->enodes_maps[ifidx] &= ~(1 << i);
967                         
968                         spin_unlock_bh(&epub->tx_ampdu_lock);
969                         return i;
970                 }
971                 map &= ~(1 << i);
972         }
973
974         spin_unlock_bh(&epub->tx_ampdu_lock);
975         return -1;
976 }
977
978 struct esp_node * esp_get_node_by_addr(struct esp_pub * epub, const u8 *addr)
979 {
980         int i;
981         u8 map;
982         struct esp_node *node = NULL;
983         if(addr == NULL)
984                 return NULL;
985         spin_lock_bh(&epub->tx_ampdu_lock);
986         map = epub->enodes_map;
987         while(map != 0){
988                 i = ffs(map) - 1;
989                 if(i < 0){
990                         spin_unlock_bh(&epub->tx_ampdu_lock);
991                         return NULL;
992                 }
993                 map &= ~(1 << i);
994 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
995                 if(memcmp(epub->enodes[i]->sta->addr, addr, ETH_ALEN) == 0)
996 #else
997                 if(memcmp(epub->enodes[i]->addr, addr, ETH_ALEN) == 0)
998 #endif
999                 {
1000                         node = epub->enodes[i];
1001                         break;
1002                 }
1003         }
1004
1005         spin_unlock_bh(&epub->tx_ampdu_lock);
1006         return node;
1007 }
1008
1009 int esp_get_empty_rxampdu(struct esp_pub * epub, const u8 *addr, u8 tid)
1010 {
1011         int index = -1;
1012         if(addr == NULL)
1013                 return index;
1014         spin_lock_bh(&epub->rx_ampdu_lock);
1015         if((index = ffz(epub->rxampdu_map)) < ESP_PUB_MAX_RXAMPDU){
1016                 epub->rxampdu_map |= BIT(index);
1017                 epub->rxampdu_node[index] = esp_get_node_by_addr(epub, addr);
1018                 epub->rxampdu_tid[index] = tid;
1019         } else {
1020                 index = -1;
1021         }
1022         spin_unlock_bh(&epub->rx_ampdu_lock);
1023         return index;
1024 }
1025
1026 int esp_get_exist_rxampdu(struct esp_pub * epub, const u8 *addr, u8 tid)
1027 {       
1028         u8 map;
1029         int index = -1;
1030         int i;
1031         if(addr == NULL)
1032                 return index;
1033         spin_lock_bh(&epub->rx_ampdu_lock);
1034         map = epub->rxampdu_map;
1035         while(map != 0){
1036                 i = ffs(map) - 1;
1037                 if(i < 0){
1038                         spin_unlock_bh(&epub->rx_ampdu_lock);
1039                         return index;
1040                 }
1041                 map &= ~ BIT(i);
1042                 if(epub->rxampdu_tid[i] == tid && 
1043 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
1044                         memcmp(epub->rxampdu_node[i]->sta->addr, addr, ETH_ALEN) == 0
1045 #else
1046                         memcmp(epub->rxampdu_node[i]->addr, addr, ETH_ALEN) == 0
1047 #endif
1048                 ){
1049                         index = i;
1050                         break;
1051                 }
1052         }
1053
1054         epub->rxampdu_map &= ~ BIT(index);
1055         spin_unlock_bh(&epub->rx_ampdu_lock);
1056         return index;
1057
1058 }
1059
1060 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
1061 static int esp_op_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif, struct ieee80211_sta *sta)
1062 #else
1063 static int esp_op_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif, const u8 *addr)
1064 #endif
1065 {
1066 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
1067         struct esp_pub *epub = (struct esp_pub *)hw->priv;
1068 #endif
1069         struct esp_vif *evif = (struct esp_vif *)vif->drv_priv;
1070         int index;
1071 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28))
1072         ESP_IEEE80211_DBG(ESP_DBG_OP, "%s enter, addr %pM\n", __func__, addr);
1073         index = esp_node_attach(hw, evif->index, addr);
1074
1075 #else
1076 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 34))
1077         ESP_IEEE80211_DBG(ESP_DBG_OP, "%s enter, sta addr %pM\n", __func__, sta->addr);
1078 #else 
1079         ESP_IEEE80211_DBG(ESP_DBG_OP, "%s enter, vif addr %pM, sta addr %pM\n", __func__, vif->addr, sta->addr);
1080 #endif
1081         index = esp_node_attach(hw, evif->index, sta);
1082 #endif
1083
1084         if(index < 0)
1085                 return -1;
1086 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
1087         sip_send_set_sta(epub, evif->index, 1, sta, vif, (u8)index);
1088 #else
1089         //node = esp_get_node_by_addr(epub, addr);
1090         //sip_send_set_sta(epub, evif->index, 1, node, vif, (u8)index);
1091 #endif
1092     return 0;
1093 }
1094
1095 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
1096 static int esp_op_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif, struct ieee80211_sta *sta)
1097 #else
1098 static int esp_op_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif, const u8 *addr)
1099 #endif
1100 {       
1101         struct esp_pub *epub = (struct esp_pub *)hw->priv;
1102         struct esp_vif *evif = (struct esp_vif *)vif->drv_priv;
1103         int index;
1104
1105 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28))
1106         struct esp_node *node;
1107         ESP_IEEE80211_DBG(ESP_DBG_OP, "%s enter, addr %pM\n", __func__, addr);
1108 #elif (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 34))
1109         ESP_IEEE80211_DBG(ESP_DBG_OP, "%s enter, sta addr %pM\n", __func__, sta->addr);
1110 #else 
1111         ESP_IEEE80211_DBG(ESP_DBG_OP, "%s enter, vif addr %pM, sta addr %pM\n", __func__, vif->addr, sta->addr);
1112 #endif
1113         
1114         //remove a connect in target
1115 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
1116         index = esp_node_detach(hw, evif->index, sta);
1117         sip_send_set_sta(epub, evif->index, 0, sta, vif, (u8)index);
1118 #else
1119         node = esp_get_node_by_addr(epub, addr);
1120         index = esp_node_detach(hw, evif->index, addr);
1121         sip_send_set_sta(epub, evif->index, 0, node, vif, node->index);
1122 #endif
1123
1124         return 0;
1125 }
1126
1127
1128 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
1129 static void esp_op_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif, enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
1130 #else
1131 static void esp_op_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif, enum sta_notify_cmd cmd, const u8 *addr)
1132 #endif
1133 {
1134 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28))
1135         struct esp_pub *epub = (struct esp_pub *)hw->priv;
1136 #endif
1137
1138         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "%s enter \n", __func__);
1139
1140         switch (cmd) {
1141 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39))
1142         case STA_NOTIFY_ADD:
1143             ESP_IEEE80211_DBG(ESP_DBG_ERROR, "%s cmd add\n", __func__);
1144 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
1145             esp_op_sta_add(hw, vif, sta);
1146 #else
1147             memcpy(epub->wl.bssid, addr, ETH_ALEN);
1148             esp_op_sta_add(hw, vif, addr);
1149 #endif
1150             break;
1151
1152         case STA_NOTIFY_REMOVE:
1153 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
1154             esp_op_sta_remove(hw, vif, sta);
1155 #else
1156             esp_op_sta_remove(hw, vif, addr);
1157             memset(epub->wl.bssid, 0, ETH_ALEN);
1158 #endif
1159             break;
1160 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29))
1161         case STA_NOTIFY_SLEEP:
1162                 break;
1163
1164         case STA_NOTIFY_AWAKE:
1165                 break;
1166 #endif /* NEW_KERNEL */
1167
1168         default:
1169                 break;
1170         }
1171 }
1172
1173
1174 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0))
1175 static int esp_op_conf_tx(struct ieee80211_hw *hw, 
1176                           struct ieee80211_vif *vif,
1177                           u16 queue,
1178                           const struct ieee80211_tx_queue_params *params)
1179
1180 #else
1181 static int esp_op_conf_tx(struct ieee80211_hw *hw, u16 queue,
1182                           const struct ieee80211_tx_queue_params *params)
1183 #endif
1184 {
1185         struct esp_pub *epub = (struct esp_pub *)hw->priv;
1186         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "%s enter \n", __func__);
1187         return sip_send_wmm_params(epub, queue, params);
1188 }
1189
1190 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 35))
1191 static int esp_op_get_tx_stats(struct ieee80211_hw *hw,
1192                                struct ieee80211_tx_queue_stats *stats)
1193 {
1194         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "%s enter \n", __func__);
1195
1196         return 0;
1197 }
1198 #endif /* !NEW_KERNEL && !KERNEL_35*/
1199
1200 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0))
1201 static u64 esp_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1202 #else
1203 static u64 esp_op_get_tsf(struct ieee80211_hw *hw)
1204 #endif
1205 {
1206         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "%s enter \n", __func__);
1207
1208         return 0;
1209 }
1210
1211 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30))
1212 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0))
1213 static void esp_op_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u64 tsf)
1214 #else
1215 static void esp_op_set_tsf(struct ieee80211_hw *hw, u64 tsf)
1216 #endif
1217 {
1218         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "%s enter \n", __func__);
1219 }
1220 #endif
1221
1222 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0))
1223 static void esp_op_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1224 #else
1225 static void esp_op_reset_tsf(struct ieee80211_hw *hw)
1226 #endif
1227 {
1228         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "%s enter \n", __func__);
1229
1230 }
1231
1232 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31))
1233 static void esp_op_rfkill_poll(struct ieee80211_hw *hw)
1234 {
1235         struct esp_pub *epub = (struct esp_pub *)hw->priv;
1236
1237         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "%s enter \n", __func__);
1238
1239         wiphy_rfkill_set_hw_state(hw->wiphy,
1240                                   test_bit(ESP_WL_FLAG_RFKILL, &epub->wl.flags) ? true : false);
1241 }
1242 #endif
1243
1244 #ifdef HW_SCAN
1245 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 35))
1246 static int esp_op_hw_scan(struct ieee80211_hw *hw,
1247                           struct cfg80211_scan_request *req)
1248 #else
1249 static int esp_op_hw_scan(struct ieee80211_hw *hw,
1250                           struct ieee80211_vif *vif,
1251                           struct cfg80211_scan_request *req)
1252 #endif /* NEW_KERNEL && KERNEL_35 */
1253 {
1254         struct esp_pub *epub = (struct esp_pub *)hw->priv;
1255         int i, ret;
1256         bool scan_often = true;
1257
1258         ESP_IEEE80211_DBG(ESP_DBG_OP, "%s\n", __func__);
1259
1260         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "scan, %d\n", req->n_ssids);
1261         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "scan, len 1:%d,ssid 1:%s\n", req->ssids->ssid_len, req->ssids->ssid_len == 0? "":(char *)req->ssids->ssid);
1262         if(req->n_ssids > 1)
1263                 ESP_IEEE80211_DBG(ESP_DBG_TRACE, "scan, len 2:%d,ssid 2:%s\n", (req->ssids+1)->ssid_len, (req->ssids+1)->ssid_len == 0? "":(char *)(req->ssids + 1)->ssid);
1264
1265         /*scan_request is keep allocate untill scan_done,record it
1266           to split request into multi sdio_cmd*/
1267         if (atomic_read(&epub->wl.off)) {
1268                 esp_dbg(ESP_DBG_ERROR, "%s scan but wl off \n", __func__);
1269                 return -EPERM;
1270         }
1271
1272         if(req->n_ssids > 1){
1273                 struct cfg80211_ssid *ssid2 = req->ssids + 1;
1274                 if((req->ssids->ssid_len > 0 && ssid2->ssid_len > 0) || req->n_ssids > 2){
1275                         ESP_IEEE80211_DBG(ESP_DBG_ERROR, "scan ssid num: %d, ssid1:%s, ssid2:%s,not support\n", req->n_ssids, 
1276                                         req->ssids->ssid_len == 0 ? "":(char *)req->ssids->ssid, ssid2->ssid_len == 0? "":(char *)ssid2->ssid);
1277                                 return -EINVAL;
1278                         }
1279         }
1280
1281         epub->wl.scan_req = req;
1282
1283         for (i = 0; i < req->n_channels; i++)
1284                 ESP_IEEE80211_DBG(ESP_DBG_TRACE, "eagle hw_scan freq %d\n",
1285                                   req->channels[i]->center_freq);
1286 #if 0
1287         for (i = 0; i < req->n_ssids; i++) {
1288                 if (req->ssids->ssid_len> 0) {
1289                         req->ssids->ssid[req->ssids->ssid_len]='\0';
1290                         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "scan_ssid %d:%s\n",
1291                                           i, req->ssids->ssid);
1292                 }
1293         }
1294 #endif
1295
1296         /*in connect state, suspend tx data*/
1297         if(epub->sip->support_bgscan &&
1298                 test_bit(ESP_WL_FLAG_CONNECT, &epub->wl.flags) &&
1299                 req->n_channels > 0)
1300         {
1301
1302                 scan_often = epub->scan_permit_valid && time_before(jiffies, epub->scan_permit);
1303                 epub->scan_permit_valid = true;
1304
1305                 if (!scan_often) {
1306 /*                        epub->scan_permit = jiffies + msecs_to_jiffies(900);
1307                         set_bit(ESP_WL_FLAG_STOP_TXQ, &epub->wl.flags);
1308                         if (atomic_read(&epub->txq_stopped) == false) {
1309                                 atomic_set(&epub->txq_stopped, true);
1310                                 ieee80211_stop_queues(hw);
1311                         }
1312 */
1313                 } else {
1314                         ESP_IEEE80211_DBG(ESP_DBG_LOG, "scan too often\n");
1315                         return -EACCES;
1316                 }
1317         } else {
1318                 scan_often = false;
1319         }
1320
1321         /*send sub_scan task to target*/
1322         ret = sip_send_scan(epub);
1323
1324         if (ret) {
1325                 ESP_IEEE80211_DBG(ESP_DBG_ERROR, "fail to send scan_cmd\n");
1326                 return ret;
1327         } else {
1328                 if(!scan_often) {
1329                         epub->scan_permit = jiffies + msecs_to_jiffies(900);
1330                         set_bit(ESP_WL_FLAG_STOP_TXQ, &epub->wl.flags);
1331                         if (atomic_read(&epub->txq_stopped) == false) {
1332                                 atomic_set(&epub->txq_stopped, true);
1333                                 ieee80211_stop_queues(hw);
1334                         }
1335                         /*force scan complete in case target fail to report in time*/
1336                         ieee80211_queue_delayed_work(hw, &epub->scan_timeout_work, req->n_channels * HZ / 4);
1337                 }
1338         }
1339
1340         return 0;
1341 }
1342
1343 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38))
1344 static int esp_op_remain_on_channel(struct ieee80211_hw *hw,
1345                                     struct ieee80211_channel *chan,
1346                                     enum nl80211_channel_type channel_type,
1347                                     int duration)
1348 {
1349       struct esp_pub *epub = (struct esp_pub *)hw->priv;
1350
1351       ESP_IEEE80211_DBG(ESP_DBG_OP, "%s enter, center_freq = %d duration = %d\n", __func__, chan->center_freq, duration);
1352       sip_send_roc(epub, chan->center_freq, duration);
1353       return 0;
1354 }
1355
1356 static int esp_op_cancel_remain_on_channel(struct ieee80211_hw *hw)
1357 {
1358       struct esp_pub *epub = (struct esp_pub *)hw->priv;
1359
1360       ESP_IEEE80211_DBG(ESP_DBG_OP, "%s enter \n", __func__);
1361       epub->roc_flags= 0;  // to disable roc state
1362       sip_send_roc(epub, 0, 0);
1363      return 0;
1364 }
1365 #endif /* > 2.6.38 */
1366 #endif
1367
1368 void esp_rocdone_process(struct ieee80211_hw *hw, struct sip_evt_roc *report)
1369 {    
1370       struct esp_pub *epub = (struct esp_pub *)hw->priv;
1371
1372       ESP_IEEE80211_DBG(ESP_DBG_OP, "%s enter, state = %d is_ok = %d\n", __func__, report->state, report->is_ok);
1373
1374       //roc process begin 
1375       if((report->state==1)&&(report->is_ok==1)) 
1376       {
1377            epub->roc_flags=1;  //flags in roc state, to fix channel, not change
1378 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38))
1379            ieee80211_ready_on_channel(hw);
1380 #endif
1381       }
1382       else if ((report->state==0)&&(report->is_ok==1))    //roc process timeout
1383       {
1384            epub->roc_flags= 0;  // to disable roc state
1385 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38))
1386            ieee80211_remain_on_channel_expired(hw);     
1387 #endif
1388        }
1389 }
1390
1391 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 39))        
1392 static int esp_op_set_bitrate_mask(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1393                                 const struct cfg80211_bitrate_mask *mask)
1394 {
1395         ESP_IEEE80211_DBG(ESP_DBG_OP, "%s enter \n", __func__);
1396         ESP_IEEE80211_DBG(ESP_DBG_OP, "%s vif->macaddr[%pM], mask[%d]\n", __func__, vif->addr, mask->control[0].legacy);
1397
1398         return 0;
1399 }
1400 #endif
1401
1402 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0))        
1403 void esp_op_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
1404 #else
1405 void esp_op_flush(struct ieee80211_hw *hw, bool drop)
1406 #endif
1407 {
1408         
1409         ESP_IEEE80211_DBG(ESP_DBG_OP, "%s enter \n", __func__);
1410         do{
1411                 
1412                 struct esp_pub *epub = (struct esp_pub *)hw->priv;
1413                 unsigned long time = jiffies + msecs_to_jiffies(15);
1414                 while(atomic_read(&epub->sip->tx_data_pkt_queued)){
1415                         if(!time_before(jiffies, time)){
1416                                 break;
1417                         }
1418 #if  !defined(FPGA_LOOPBACK) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32))
1419             ieee80211_queue_work(epub->hw, &epub->tx_work);
1420 #else
1421             queue_work(epub->esp_wkq, &epub->tx_work);
1422 #endif
1423                         //sip_txq_process(epub);
1424                 }
1425                 mdelay(10);
1426                 
1427         }while(0);
1428 }
1429
1430 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33))
1431 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
1432 static int esp_op_ampdu_action(struct ieee80211_hw *hw,
1433                 enum ieee80211_ampdu_mlme_action action,
1434                             struct ieee80211_sta *sta, u16 tid, u16 *ssn)
1435 #else
1436 static int esp_op_ampdu_action(struct ieee80211_hw *hw,
1437                 enum ieee80211_ampdu_mlme_action action,
1438                             const u8 *addr, u16 tid, u16 *ssn)
1439 #endif
1440 #else
1441 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39))
1442 static int esp_op_ampdu_action(struct ieee80211_hw *hw,
1443                 struct ieee80211_vif *vif,
1444                                enum ieee80211_ampdu_mlme_action action,
1445                                struct ieee80211_sta *sta, u16 tid, u16 *ssn)
1446 #else
1447 static int esp_op_ampdu_action(struct ieee80211_hw *hw,
1448                                struct ieee80211_vif *vif,
1449                                enum ieee80211_ampdu_mlme_action action,
1450                                struct ieee80211_sta *sta, u16 tid, u16 *ssn,
1451                                u8 buf_size)
1452 #endif
1453 #endif /* NEW_KERNEL && KERNEL_35 */
1454 {
1455         int ret = -EOPNOTSUPP;
1456         struct esp_pub *epub = (struct esp_pub *)hw->priv;
1457 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
1458         struct esp_node * node = (struct esp_node *)sta->drv_priv;
1459 #else
1460         struct esp_node * node = esp_get_node_by_addr(epub, addr);
1461 #endif
1462         struct esp_tx_tid * tid_info = &node->tid[tid];
1463 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39))
1464         u8 buf_size = 64;
1465 #endif
1466
1467         ESP_IEEE80211_DBG(ESP_DBG_OP, "%s enter \n", __func__);
1468         switch(action) {
1469         case IEEE80211_AMPDU_TX_START:
1470                 if (mod_support_no_txampdu() ||
1471 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0))
1472                         cfg80211_get_chandef_type(&epub->hw->conf.chandef) != NL80211_CHAN_NO_HT
1473 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29))
1474                         hw->conf.channel_type == NL80211_CHAN_NO_HT
1475 #else
1476                         !(hw->conf.flags&IEEE80211_CONF_SUPPORT_HT_MODE)
1477 #endif
1478                             )
1479                         return ret;
1480
1481                 //if (vif->p2p || vif->type != NL80211_IFTYPE_STATION)
1482                 //      return ret;
1483
1484 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28))
1485                 ESP_IEEE80211_DBG(ESP_DBG_ERROR, "%s TX START, addr:%pM,tid:%u\n", __func__, addr, tid);
1486 #else
1487                 ESP_IEEE80211_DBG(ESP_DBG_ERROR, "%s TX START, addr:%pM,tid:%u,state:%d\n", __func__, sta->addr, tid, tid_info->state);
1488 #endif
1489                 spin_lock_bh(&epub->tx_ampdu_lock);
1490 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))                
1491                 ASSERT(tid_info->state == ESP_TID_STATE_TRIGGER);
1492                 *ssn = tid_info->ssn;
1493                 tid_info->state = ESP_TID_STATE_PROGRESS;
1494 #endif
1495
1496 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28))
1497                 ieee80211_start_tx_ba_cb_irqsafe(hw, addr, tid);
1498 #elif (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33))
1499                 ieee80211_start_tx_ba_cb_irqsafe(hw, sta->addr, tid);
1500 #else
1501                 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1502 #endif
1503                 spin_unlock_bh(&epub->tx_ampdu_lock);
1504 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29))
1505                 ret = 0;
1506 #else
1507                 spin_lock_bh(&epub->tx_ampdu_lock);
1508                 
1509                 if (tid_info->state != ESP_TID_STATE_PROGRESS) {
1510                         if (tid_info->state == ESP_TID_STATE_INIT) {
1511                                                 printk(KERN_ERR "%s WIFI RESET, IGNORE\n", __func__);
1512                                 spin_unlock_bh(&epub->tx_ampdu_lock);
1513                                                 return -ENETRESET;
1514                         } else {
1515                                                 ASSERT(0);
1516                         }
1517                 }
1518                         
1519                 tid_info->state = ESP_TID_STATE_OPERATIONAL;
1520                 spin_unlock_bh(&epub->tx_ampdu_lock);
1521 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
1522                 ret = sip_send_ampdu_action(epub, SIP_AMPDU_TX_OPERATIONAL, sta->addr, tid, node->ifidx, buf_size);
1523 #else
1524                 ret = sip_send_ampdu_action(epub, SIP_AMPDU_TX_OPERATIONAL, addr, tid, node->ifidx, buf_size);
1525 #endif
1526 #endif
1527                 break;
1528 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0))
1529         case IEEE80211_AMPDU_TX_STOP_CONT:
1530 #else
1531         case IEEE80211_AMPDU_TX_STOP:
1532 #endif
1533 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28))
1534                 ESP_IEEE80211_DBG(ESP_DBG_ERROR, "%s TX STOP, addr:%pM,tid:%u\n", __func__, addr, tid);
1535 #else
1536                 ESP_IEEE80211_DBG(ESP_DBG_ERROR, "%s TX STOP, addr:%pM,tid:%u,state:%d\n", __func__, sta->addr, tid, tid_info->state);
1537 #endif
1538                 spin_lock_bh(&epub->tx_ampdu_lock);
1539 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
1540                 if(tid_info->state == ESP_TID_STATE_WAIT_STOP)
1541                         tid_info->state = ESP_TID_STATE_STOP;
1542                 else
1543                         tid_info->state = ESP_TID_STATE_INIT;
1544 #endif
1545 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28))
1546                 ieee80211_stop_tx_ba_cb_irqsafe(hw, addr, tid);
1547 #elif (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33))
1548                 ieee80211_stop_tx_ba_cb_irqsafe(hw, sta->addr, tid);
1549 #else
1550                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1551 #endif
1552                 spin_unlock_bh(&epub->tx_ampdu_lock);
1553 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28))
1554                 ret = sip_send_ampdu_action(epub, SIP_AMPDU_TX_STOP, addr, tid, node->ifidx, 0);
1555 #else
1556                 ret = sip_send_ampdu_action(epub, SIP_AMPDU_TX_STOP, sta->addr, tid, node->ifidx, 0);
1557 #endif
1558                 break;
1559 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0))
1560         case IEEE80211_AMPDU_TX_STOP_FLUSH:
1561         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1562                 ret = sip_send_ampdu_action(epub, SIP_AMPDU_TX_STOP, sta->addr, tid, node->ifidx, 0);
1563                 break;
1564 #endif
1565 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29))
1566 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30))
1567         case IEEE80211_AMPDU_TX_OPERATIONAL:
1568 #else
1569         case IEEE80211_AMPDU_TX_RESUME:
1570 #endif
1571                 ESP_IEEE80211_DBG(ESP_DBG_ERROR, "%s TX OPERATION, addr:%pM,tid:%u,state:%d\n", __func__, sta->addr, tid, tid_info->state);
1572                 spin_lock_bh(&epub->tx_ampdu_lock);
1573                 
1574                 if (tid_info->state != ESP_TID_STATE_PROGRESS) {
1575                         if (tid_info->state == ESP_TID_STATE_INIT) {
1576                                                 printk(KERN_ERR "%s WIFI RESET, IGNORE\n", __func__);
1577                                 spin_unlock_bh(&epub->tx_ampdu_lock);
1578                                                 return -ENETRESET;
1579                         } else {
1580                                                 ASSERT(0);
1581                         }
1582                 }
1583                         
1584                 tid_info->state = ESP_TID_STATE_OPERATIONAL;
1585                 spin_unlock_bh(&epub->tx_ampdu_lock);
1586                 ret = sip_send_ampdu_action(epub, SIP_AMPDU_TX_OPERATIONAL, sta->addr, tid, node->ifidx, buf_size);
1587                 break;
1588 #endif
1589         case IEEE80211_AMPDU_RX_START:
1590                 if(mod_support_no_rxampdu() ||
1591 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0))
1592                         cfg80211_get_chandef_type(&epub->hw->conf.chandef) != NL80211_CHAN_NO_HT
1593 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29))
1594                         hw->conf.channel_type == NL80211_CHAN_NO_HT
1595 #else
1596                         !(hw->conf.flags&IEEE80211_CONF_SUPPORT_HT_MODE)
1597 #endif
1598                         )
1599                         return ret;
1600
1601                 if (
1602 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
1603                 (vif->p2p && false)
1604 #else
1605                 false
1606 #endif
1607 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33))
1608                 || false
1609 #else
1610                 || (vif->type != NL80211_IFTYPE_STATION && false)
1611 #endif
1612            )
1613                         return ret;
1614 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28))
1615                 ESP_IEEE80211_DBG(ESP_DBG_ERROR, "%s RX START %pM tid %u %u\n", __func__, addr, tid, *ssn);
1616                 ret = sip_send_ampdu_action(epub, SIP_AMPDU_RX_START, addr, tid, *ssn, 64);
1617 #else
1618                 ESP_IEEE80211_DBG(ESP_DBG_ERROR, "%s RX START %pM tid %u %u\n", __func__, sta->addr, tid, *ssn);
1619                 ret = sip_send_ampdu_action(epub, SIP_AMPDU_RX_START, sta->addr, tid, *ssn, 64);
1620 #endif
1621                 break;
1622         case IEEE80211_AMPDU_RX_STOP:
1623 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28))
1624                 ESP_IEEE80211_DBG(ESP_DBG_ERROR, "%s RX STOP %pM tid %u\n", __func__, addr, tid);
1625                 ret = sip_send_ampdu_action(epub, SIP_AMPDU_RX_STOP, addr, tid, 0, 0);
1626 #else
1627                 ESP_IEEE80211_DBG(ESP_DBG_ERROR, "%s RX STOP %pM tid %u\n", __func__, sta->addr, tid);
1628                 ret = sip_send_ampdu_action(epub, SIP_AMPDU_RX_STOP, sta->addr, tid, 0, 0);
1629 #endif
1630                 break;
1631         default:
1632                 break;
1633         }
1634         return ret;
1635 }
1636
1637 #if 0
1638 static int esp_op_tx_last_beacon(struct ieee80211_hw *hw)
1639 {
1640
1641         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "%s enter \n", __func__);
1642
1643         return 0;
1644 }
1645
1646 #ifdef CONFIG_NL80211_TESTMODE
1647 static int esp_op_testmode_cmd(struct ieee80211_hw *hw, void *data, int len)
1648 {
1649         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "%s enter \n", __func__);
1650
1651         return 0;
1652 }
1653 #endif /* CONFIG_NL80211_TESTMODE */
1654 #endif
1655
1656 static void
1657 esp_tx_work(struct work_struct *work)
1658 {
1659         struct esp_pub *epub = container_of(work, struct esp_pub, tx_work);
1660
1661         mutex_lock(&epub->tx_mtx);
1662         sip_txq_process(epub);
1663         mutex_unlock(&epub->tx_mtx);
1664 }
1665
1666 #ifndef RX_SENDUP_SYNC
1667 //for debug
1668 static int data_pkt_dequeue_cnt = 0;
1669 static void _esp_flush_rxq(struct esp_pub *epub)
1670 {
1671         struct sk_buff *skb = NULL;
1672
1673         while ((skb = skb_dequeue(&epub->rxq))) {
1674                 esp_dbg(ESP_DBG_TRACE, "%s call ieee80211_rx \n", __func__);
1675                 //local_bh_disable();
1676                 ieee80211_rx(epub->hw, skb);
1677                 //local_bh_enable();
1678         }
1679 }
1680
1681 static void
1682 esp_sendup_work(struct work_struct *work)
1683 {
1684         struct esp_pub *epub = container_of(work, struct esp_pub, sendup_work);
1685         spin_lock_bh(&epub->rx_lock);
1686         _esp_flush_rxq(epub);
1687         spin_unlock_bh(&epub->rx_lock);
1688 }
1689 #endif /* !RX_SENDUP_SYNC */
1690
1691 static const struct ieee80211_ops esp_mac80211_ops = {
1692         .tx = esp_op_tx,
1693         .start = esp_op_start,
1694         .stop = esp_op_stop,
1695 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 39))        
1696 #ifdef CONFIG_PM
1697         .suspend = esp_op_suspend,
1698         .resume = esp_op_resume,
1699 #endif
1700 #endif
1701         .add_interface = esp_op_add_interface,
1702         .remove_interface = esp_op_remove_interface,
1703         .config = esp_op_config,
1704
1705         .bss_info_changed = esp_op_bss_info_changed,
1706 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28))
1707         .config_interface = esp_op_config_interface,
1708 #endif
1709 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32))
1710         .prepare_multicast = esp_op_prepare_multicast,
1711 #endif
1712         .configure_filter = esp_op_configure_filter,
1713         .set_key = esp_op_set_key,
1714         .update_tkip_key = esp_op_update_tkip_key,
1715         //.sched_scan_start = esp_op_sched_scan_start,
1716         //.sched_scan_stop = esp_op_sched_scan_stop,
1717         .set_rts_threshold = esp_op_set_rts_threshold,
1718         .sta_notify = esp_op_sta_notify,
1719         .conf_tx = esp_op_conf_tx,
1720 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 35))
1721         .get_tx_stats = esp_op_get_tx_stats,
1722 #endif /* KERNEL_VERSION < 2.6.35*/
1723 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
1724         .change_interface = esp_op_change_interface,
1725 #endif
1726         .get_tsf = esp_op_get_tsf,
1727 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30))
1728         .set_tsf = esp_op_set_tsf,
1729 #endif
1730         .reset_tsf = esp_op_reset_tsf,
1731 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31))
1732         .rfkill_poll= esp_op_rfkill_poll,
1733 #endif
1734 #ifdef HW_SCAN
1735         .hw_scan = esp_op_hw_scan,
1736 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38))
1737         .remain_on_channel= esp_op_remain_on_channel,
1738         .cancel_remain_on_channel=esp_op_cancel_remain_on_channel,
1739 #endif /* >=2.6.38 */
1740 #endif
1741         .ampdu_action = esp_op_ampdu_action,
1742         //.get_survey = esp_op_get_survey,
1743 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34))
1744         .sta_add = esp_op_sta_add,
1745         .sta_remove = esp_op_sta_remove,
1746 #endif /* >= 2.6.34 */
1747 #ifdef CONFIG_NL80211_TESTMODE
1748         //CFG80211_TESTMODE_CMD(esp_op_tm_cmd)
1749 #endif
1750 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 39))
1751         .set_bitrate_mask = esp_op_set_bitrate_mask,
1752 #endif
1753 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34))
1754         .flush = esp_op_flush,
1755 #endif
1756 };
1757
1758 struct esp_pub * esp_pub_alloc_mac80211(struct device *dev)
1759 {
1760         struct ieee80211_hw *hw;
1761         struct esp_pub *epub;
1762         int ret = 0;
1763
1764         hw = ieee80211_alloc_hw(sizeof(struct esp_pub), &esp_mac80211_ops);
1765
1766         if (hw == NULL) {
1767                 esp_dbg(ESP_DBG_ERROR, "ieee80211 can't alloc hw!\n");
1768                 ret = -ENOMEM;
1769                 return ERR_PTR(ret);
1770         }
1771         #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)) 
1772         hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
1773         #endif
1774         epub = hw->priv;
1775         memset(epub, 0, sizeof(*epub));
1776         epub->hw = hw;
1777         SET_IEEE80211_DEV(hw, dev);
1778         epub->dev = dev;
1779
1780         skb_queue_head_init(&epub->txq);
1781         skb_queue_head_init(&epub->txdoneq);
1782         skb_queue_head_init(&epub->rxq);
1783
1784         spin_lock_init(&epub->tx_ampdu_lock);
1785         spin_lock_init(&epub->rx_ampdu_lock);
1786         spin_lock_init(&epub->tx_lock);
1787         mutex_init(&epub->tx_mtx);
1788         spin_lock_init(&epub->rx_lock);
1789
1790         INIT_WORK(&epub->tx_work, esp_tx_work);
1791 #ifndef RX_SENDUP_SYNC
1792         INIT_WORK(&epub->sendup_work, esp_sendup_work);
1793 #endif //!RX_SENDUP_SYNC
1794
1795 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39))
1796         //epub->esp_wkq = create_freezeable_workqueue("esp_wkq"); 
1797         epub->esp_wkq = create_singlethread_workqueue("esp_wkq");
1798 #else
1799         //epub->esp_wkq = create_freezable_workqueue("esp_wkq"); 
1800         epub->esp_wkq = create_singlethread_workqueue("esp_wkq");
1801 #endif /* NEW_KERNEL */
1802
1803         if (epub->esp_wkq == NULL) {
1804                 ret = -ENOMEM;
1805                 return ERR_PTR(ret);
1806         }
1807         epub->scan_permit_valid = false;
1808         INIT_DELAYED_WORK(&epub->scan_timeout_work, hw_scan_timeout_report);
1809
1810         return epub;
1811 }
1812
1813
1814 int esp_pub_dealloc_mac80211(struct esp_pub *epub)
1815 {
1816         set_bit(ESP_WL_FLAG_RFKILL, &epub->wl.flags);
1817
1818         destroy_workqueue(epub->esp_wkq);
1819         mutex_destroy(&epub->tx_mtx);
1820
1821 #ifdef ESP_NO_MAC80211
1822         free_netdev(epub->net_dev);
1823         wiphy_free(epub->wdev->wiphy);
1824         kfree(epub->wdev);
1825 #else
1826         if (epub->hw) {
1827                 ieee80211_free_hw(epub->hw);
1828         }
1829 #endif
1830
1831         return 0;
1832 }
1833
1834 #if 0
1835 static int esp_reg_notifier(struct wiphy *wiphy,
1836                             struct regulatory_request *request)
1837 {
1838         struct ieee80211_supported_band *sband;
1839         struct ieee80211_channel *ch;
1840         int i;
1841
1842         ESP_IEEE80211_DBG(ESP_DBG_TRACE, "%s enter %d\n", __func__, request->initiator
1843                          );
1844
1845         //TBD
1846 }
1847 #endif
1848
1849 /* 2G band channels */
1850 static struct ieee80211_channel esp_channels_2ghz[] = {
1851         { .hw_value = 1, .center_freq = 2412, .max_power = 25 },
1852         { .hw_value = 2, .center_freq = 2417, .max_power = 25 },
1853         { .hw_value = 3, .center_freq = 2422, .max_power = 25 },
1854         { .hw_value = 4, .center_freq = 2427, .max_power = 25 },
1855         { .hw_value = 5, .center_freq = 2432, .max_power = 25 },
1856         { .hw_value = 6, .center_freq = 2437, .max_power = 25 },
1857         { .hw_value = 7, .center_freq = 2442, .max_power = 25 },
1858         { .hw_value = 8, .center_freq = 2447, .max_power = 25 },
1859         { .hw_value = 9, .center_freq = 2452, .max_power = 25 },
1860         { .hw_value = 10, .center_freq = 2457, .max_power = 25 },
1861         { .hw_value = 11, .center_freq = 2462, .max_power = 25 },
1862         { .hw_value = 12, .center_freq = 2467, .max_power = 25 },
1863         { .hw_value = 13, .center_freq = 2472, .max_power = 25 },
1864         //{ .hw_value = 14, .center_freq = 2484, .max_power = 25 },
1865 };
1866
1867 /* 11G rate */
1868 static struct ieee80211_rate esp_rates_2ghz[] = {
1869         {
1870                 .bitrate = 10,
1871                 .hw_value = CONF_HW_BIT_RATE_1MBPS,
1872                 .hw_value_short = CONF_HW_BIT_RATE_1MBPS,
1873         },
1874         {
1875                 .bitrate = 20,
1876                 .hw_value = CONF_HW_BIT_RATE_2MBPS,
1877                 .hw_value_short = CONF_HW_BIT_RATE_2MBPS,
1878                 .flags = IEEE80211_RATE_SHORT_PREAMBLE
1879         },
1880         {
1881                 .bitrate = 55,
1882                 .hw_value = CONF_HW_BIT_RATE_5_5MBPS,
1883                 .hw_value_short = CONF_HW_BIT_RATE_5_5MBPS,
1884                 .flags = IEEE80211_RATE_SHORT_PREAMBLE
1885         },
1886         {
1887                 .bitrate = 110,
1888                 .hw_value = CONF_HW_BIT_RATE_11MBPS,
1889                 .hw_value_short = CONF_HW_BIT_RATE_11MBPS,
1890                 .flags = IEEE80211_RATE_SHORT_PREAMBLE
1891         },
1892         {
1893                 .bitrate = 60,
1894                 .hw_value = CONF_HW_BIT_RATE_6MBPS,
1895                 .hw_value_short = CONF_HW_BIT_RATE_6MBPS,
1896         },
1897         {
1898                 .bitrate = 90,
1899                 .hw_value = CONF_HW_BIT_RATE_9MBPS,
1900                 .hw_value_short = CONF_HW_BIT_RATE_9MBPS,
1901         },
1902         {
1903                 .bitrate = 120,
1904                 .hw_value = CONF_HW_BIT_RATE_12MBPS,
1905                 .hw_value_short = CONF_HW_BIT_RATE_12MBPS,
1906         },
1907         {
1908                 .bitrate = 180,
1909                 .hw_value = CONF_HW_BIT_RATE_18MBPS,
1910                 .hw_value_short = CONF_HW_BIT_RATE_18MBPS,
1911         },
1912         {
1913                 .bitrate = 240,
1914                 .hw_value = CONF_HW_BIT_RATE_24MBPS,
1915                 .hw_value_short = CONF_HW_BIT_RATE_24MBPS,
1916         },
1917         {
1918                 .bitrate = 360,
1919                 .hw_value = CONF_HW_BIT_RATE_36MBPS,
1920                 .hw_value_short = CONF_HW_BIT_RATE_36MBPS,
1921         },
1922         {
1923                 .bitrate = 480,
1924                 .hw_value = CONF_HW_BIT_RATE_48MBPS,
1925                 .hw_value_short = CONF_HW_BIT_RATE_48MBPS,
1926         },
1927         {
1928                 .bitrate = 540,
1929                 .hw_value = CONF_HW_BIT_RATE_54MBPS,
1930                 .hw_value_short = CONF_HW_BIT_RATE_54MBPS,
1931         },
1932 };
1933
1934 static void
1935 esp_pub_init_mac80211(struct esp_pub *epub)
1936 {
1937         struct ieee80211_hw *hw = epub->hw;
1938
1939 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31))
1940         static const u32 cipher_suites[] = {
1941                 WLAN_CIPHER_SUITE_WEP40,
1942                 WLAN_CIPHER_SUITE_WEP104,
1943                 WLAN_CIPHER_SUITE_TKIP,
1944                 WLAN_CIPHER_SUITE_CCMP,
1945         };
1946 #endif
1947
1948         hw->channel_change_time = 420000; /* in us */
1949         hw->max_listen_interval = 10;
1950
1951         hw->flags = IEEE80211_HW_SIGNAL_DBM |
1952 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33))
1953                     IEEE80211_HW_HAS_RATE_CONTROL |
1954 #endif /* >= 2.6.33 */
1955 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30))
1956                     IEEE80211_HW_SUPPORTS_PS |
1957 #endif
1958 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29))
1959                 IEEE80211_HW_AMPDU_AGGREGATION |
1960 #endif
1961                                 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
1962    //IEEE80211_HW_PS_NULLFUNC_STACK |   
1963         //IEEE80211_HW_CONNECTION_MONITOR |
1964         //IEEE80211_HW_BEACON_FILTER |
1965         //IEEE80211_HW_AMPDU_AGGREGATION |
1966         //IEEE80211_HW_REPORTS_TX_ACK_STATUS;
1967 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
1968         hw->max_rx_aggregation_subframes = 0x40;
1969         hw->max_tx_aggregation_subframes = 0x40;
1970 #endif /* >= 2.6.39 */
1971         
1972 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31))
1973         hw->wiphy->cipher_suites = cipher_suites;
1974         hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
1975         hw->wiphy->max_scan_ie_len = epub->sip->tx_blksz - sizeof(struct sip_hdr) - sizeof(struct sip_cmd_scan);
1976 #endif
1977
1978 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
1979         /* ONLY station for now, support P2P soon... */
1980         hw->wiphy->interface_modes = 
1981 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
1982             BIT(NL80211_IFTYPE_P2P_GO) |
1983                     BIT(NL80211_IFTYPE_P2P_CLIENT) |
1984 #endif
1985             BIT(NL80211_IFTYPE_STATION) |
1986                     BIT(NL80211_IFTYPE_AP);
1987 #endif
1988
1989 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30))
1990         hw->wiphy->max_scan_ssids = 2;
1991         //hw->wiphy->max_sched_scan_ssids = 16;
1992         //hw->wiphy->max_match_sets = 16;
1993 #endif
1994
1995 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38))
1996                 hw->wiphy->max_remain_on_channel_duration = 5000;
1997 #endif
1998
1999         atomic_set(&epub->wl.off, 1);
2000
2001         epub->wl.sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
2002         epub->wl.sbands[IEEE80211_BAND_2GHZ].channels = esp_channels_2ghz;
2003         epub->wl.sbands[IEEE80211_BAND_2GHZ].bitrates = esp_rates_2ghz;
2004         epub->wl.sbands[IEEE80211_BAND_2GHZ].n_channels = ARRAY_SIZE(esp_channels_2ghz);
2005         epub->wl.sbands[IEEE80211_BAND_2GHZ].n_bitrates = ARRAY_SIZE(esp_rates_2ghz);
2006         /*add to support 11n*/
2007 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29))
2008         epub->wl.sbands[IEEE80211_BAND_2GHZ].ht_cap.ht_supported = true;
2009         epub->wl.sbands[IEEE80211_BAND_2GHZ].ht_cap.cap = 0x116C;//IEEE80211_HT_CAP_RX_STBC; //IEEE80211_HT_CAP_SGI_20;
2010 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32))
2011         epub->wl.sbands[IEEE80211_BAND_2GHZ].ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_16K;
2012         epub->wl.sbands[IEEE80211_BAND_2GHZ].ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
2013 #else
2014         epub->wl.sbands[IEEE80211_BAND_2GHZ].ht_cap.ampdu_factor = 1;//IEEE80211_HT_MAX_AMPDU_16K;
2015         epub->wl.sbands[IEEE80211_BAND_2GHZ].ht_cap.ampdu_density = 0;//IEEE80211_HT_MPDU_DENSITY_NONE;
2016 #endif
2017         memset(&epub->wl.sbands[IEEE80211_BAND_2GHZ].ht_cap.mcs, 0,
2018                sizeof(epub->wl.sbands[IEEE80211_BAND_2GHZ].ht_cap.mcs));
2019         epub->wl.sbands[IEEE80211_BAND_2GHZ].ht_cap.mcs.rx_mask[0] = 0xff;
2020         //epub->wl.sbands[IEEE80211_BAND_2GHZ].ht_cap.mcs.rx_highest = 7;
2021         //epub->wl.sbands[IEEE80211_BAND_2GHZ].ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
2022 #else
2023         epub->wl.sbands[IEEE80211_BAND_2GHZ].ht_info.ht_supported = true;
2024         epub->wl.sbands[IEEE80211_BAND_2GHZ].ht_info.cap = 0x116C;//IEEE80211_HT_CAP_RX_STBC; //IEEE80211_HT_CAP_SGI_20;
2025         epub->wl.sbands[IEEE80211_BAND_2GHZ].ht_info.ampdu_factor = 1;//IEEE80211_HT_MAX_AMPDU_16K;
2026         epub->wl.sbands[IEEE80211_BAND_2GHZ].ht_info.ampdu_density = 0;//IEEE80211_HT_MPDU_DENSITY_NONE;
2027         memset(&epub->wl.sbands[IEEE80211_BAND_2GHZ].ht_info.supp_mcs_set, 0,
2028                sizeof(epub->wl.sbands[IEEE80211_BAND_2GHZ].ht_info.supp_mcs_set));
2029         epub->wl.sbands[IEEE80211_BAND_2GHZ].ht_info.supp_mcs_set[0] = 0xff;
2030 #endif
2031
2032
2033         /* BAND_5GHZ TBD */
2034
2035         hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
2036                 &epub->wl.sbands[IEEE80211_BAND_2GHZ];
2037         /* BAND_5GHZ TBD */
2038
2039 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 31))
2040         /*no fragment*/
2041         hw->wiphy->frag_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
2042 #endif
2043
2044         /* handle AC queue in f/w */
2045         hw->queues = 4;
2046 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
2047 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30))
2048         hw->max_rates = 4;
2049 #else
2050         hw->max_altrates = 4;
2051 #endif
2052 #endif
2053         //hw->wiphy->reg_notifier = esp_reg_notify;
2054
2055         hw->vif_data_size = sizeof(struct esp_vif);
2056 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
2057         hw->sta_data_size = sizeof(struct esp_node);
2058 #endif
2059
2060         //hw->max_rx_aggregation_subframes = 8;
2061 }
2062
2063 int
2064 esp_register_mac80211(struct esp_pub *epub)
2065 {
2066         int ret = 0;
2067 #ifdef P2P_CONCURRENT
2068         u8 *wlan_addr;
2069         u8 *p2p_addr;
2070         int idx;
2071 #endif
2072
2073         esp_pub_init_mac80211(epub);
2074
2075 #ifdef P2P_CONCURRENT
2076         epub->hw->wiphy->addresses = (struct mac_address *)esp_mac_addr;
2077         memcpy(&epub->hw->wiphy->addresses[0], epub->mac_addr, ETH_ALEN);
2078         memcpy(&epub->hw->wiphy->addresses[1], epub->mac_addr, ETH_ALEN);
2079         wlan_addr = (u8 *)&epub->hw->wiphy->addresses[0];
2080         p2p_addr  = (u8 *)&epub->hw->wiphy->addresses[1];
2081
2082         for (idx = 0; idx < 64; idx++) {
2083                 p2p_addr[0] = wlan_addr[0] | 0x02;
2084                 p2p_addr[0] ^= idx << 2;
2085                 if (strncmp(p2p_addr, wlan_addr, 6) != 0)
2086                         break;
2087         }
2088
2089         epub->hw->wiphy->n_addresses = 2;
2090 #else
2091
2092         SET_IEEE80211_PERM_ADDR(epub->hw, epub->mac_addr);
2093 #endif
2094
2095         ret = ieee80211_register_hw(epub->hw);
2096
2097         if (ret < 0) {
2098                 ESP_IEEE80211_DBG(ESP_DBG_ERROR, "unable to register mac80211 hw: %d\n", ret);
2099                 return ret;
2100         } else {
2101 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
2102 #ifdef MAC80211_NO_CHANGE
2103                 rtnl_lock();
2104                 if (epub->hw->wiphy->interface_modes &
2105                 (BIT(NL80211_IFTYPE_P2P_GO) | BIT(NL80211_IFTYPE_P2P_CLIENT))) {
2106                 ret = ieee80211_if_add(hw_to_local(epub->hw), "p2p%d", NULL,
2107                                           NL80211_IFTYPE_STATION, NULL);
2108                 if (ret)
2109                         wiphy_warn(epub->hw->wiphy,
2110                                    "Failed to add default virtual iface\n");
2111                 }
2112
2113                 rtnl_unlock();
2114 #endif
2115 #endif
2116         }
2117
2118         set_bit(ESP_WL_FLAG_HW_REGISTERED, &epub->wl.flags);
2119
2120         return ret;
2121 }
2122
2123 static u8 getaddr_index(u8 * addr, struct esp_pub *epub)
2124 {
2125 #ifdef P2P_CONCURRENT
2126         int i;
2127         for(i = 0; i < ESP_PUB_MAX_VIF; i++)
2128                 if(memcmp(addr, (u8 *)&epub->hw->wiphy->addresses[i], ETH_ALEN) == 0)
2129                         return i;
2130         return ESP_PUB_MAX_VIF;
2131 #else
2132         return 0;
2133 #endif
2134 }
2135