]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/wireless/rtlwifi/base.c
Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/jkirsher/net...
[karo-tx-linux.git] / drivers / net / wireless / rtlwifi / base.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2009-2010  Realtek Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  * The full GNU General Public License is included in this distribution in the
19  * file called LICENSE.
20  *
21  * Contact Information:
22  * wlanfae <wlanfae@realtek.com>
23  * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
24  * Hsinchu 300, Taiwan.
25  *
26  * Larry Finger <Larry.Finger@lwfinger.net>
27  *
28  *****************************************************************************/
29
30 #include <linux/ip.h>
31 #include "wifi.h"
32 #include "rc.h"
33 #include "base.h"
34 #include "efuse.h"
35 #include "cam.h"
36 #include "ps.h"
37 #include "regd.h"
38
39 /*
40  *NOTICE!!!: This file will be very big, we hsould
41  *keep it clear under follwing roles:
42  *
43  *This file include follwing part, so, if you add new
44  *functions into this file, please check which part it
45  *should includes. or check if you should add new part
46  *for this file:
47  *
48  *1) mac80211 init functions
49  *2) tx information functions
50  *3) functions called by core.c
51  *4) wq & timer callback functions
52  *5) frame process functions
53  *6) sysfs functions
54  *7) ...
55  */
56
57 /*********************************************************
58  *
59  * mac80211 init functions
60  *
61  *********************************************************/
62 static struct ieee80211_channel rtl_channeltable[] = {
63         {.center_freq = 2412, .hw_value = 1,},
64         {.center_freq = 2417, .hw_value = 2,},
65         {.center_freq = 2422, .hw_value = 3,},
66         {.center_freq = 2427, .hw_value = 4,},
67         {.center_freq = 2432, .hw_value = 5,},
68         {.center_freq = 2437, .hw_value = 6,},
69         {.center_freq = 2442, .hw_value = 7,},
70         {.center_freq = 2447, .hw_value = 8,},
71         {.center_freq = 2452, .hw_value = 9,},
72         {.center_freq = 2457, .hw_value = 10,},
73         {.center_freq = 2462, .hw_value = 11,},
74         {.center_freq = 2467, .hw_value = 12,},
75         {.center_freq = 2472, .hw_value = 13,},
76         {.center_freq = 2484, .hw_value = 14,},
77 };
78
79 static struct ieee80211_rate rtl_ratetable[] = {
80         {.bitrate = 10, .hw_value = 0x00,},
81         {.bitrate = 20, .hw_value = 0x01,},
82         {.bitrate = 55, .hw_value = 0x02,},
83         {.bitrate = 110, .hw_value = 0x03,},
84         {.bitrate = 60, .hw_value = 0x04,},
85         {.bitrate = 90, .hw_value = 0x05,},
86         {.bitrate = 120, .hw_value = 0x06,},
87         {.bitrate = 180, .hw_value = 0x07,},
88         {.bitrate = 240, .hw_value = 0x08,},
89         {.bitrate = 360, .hw_value = 0x09,},
90         {.bitrate = 480, .hw_value = 0x0a,},
91         {.bitrate = 540, .hw_value = 0x0b,},
92 };
93
94 static const struct ieee80211_supported_band rtl_band_2ghz = {
95         .band = IEEE80211_BAND_2GHZ,
96
97         .channels = rtl_channeltable,
98         .n_channels = ARRAY_SIZE(rtl_channeltable),
99
100         .bitrates = rtl_ratetable,
101         .n_bitrates = ARRAY_SIZE(rtl_ratetable),
102
103         .ht_cap = {0},
104 };
105
106 static void _rtl_init_hw_ht_capab(struct ieee80211_hw *hw,
107                                   struct ieee80211_sta_ht_cap *ht_cap)
108 {
109         struct rtl_priv *rtlpriv = rtl_priv(hw);
110         struct rtl_phy *rtlphy = &(rtlpriv->phy);
111
112         ht_cap->ht_supported = true;
113         ht_cap->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
114             IEEE80211_HT_CAP_SGI_40 |
115             IEEE80211_HT_CAP_SGI_20 |
116             IEEE80211_HT_CAP_DSSSCCK40 | IEEE80211_HT_CAP_MAX_AMSDU;
117
118         /*
119          *Maximum length of AMPDU that the STA can receive.
120          *Length = 2 ^ (13 + max_ampdu_length_exp) - 1 (octets)
121          */
122         ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
123
124         /*Minimum MPDU start spacing , */
125         ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
126
127         ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
128
129         /*
130          *hw->wiphy->bands[IEEE80211_BAND_2GHZ]
131          *base on ant_num
132          *rx_mask: RX mask
133          *if rx_ant =1 rx_mask[0]=0xff;==>MCS0-MCS7
134          *if rx_ant =2 rx_mask[1]=0xff;==>MCS8-MCS15
135          *if rx_ant >=3 rx_mask[2]=0xff;
136          *if BW_40 rx_mask[4]=0x01;
137          *highest supported RX rate
138          */
139         if (get_rf_type(rtlphy) == RF_1T2R || get_rf_type(rtlphy) == RF_2T2R) {
140
141                 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, ("1T2R or 2T2R\n"));
142
143                 ht_cap->mcs.rx_mask[0] = 0xFF;
144                 ht_cap->mcs.rx_mask[1] = 0xFF;
145                 ht_cap->mcs.rx_mask[4] = 0x01;
146
147                 ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
148         } else if (get_rf_type(rtlphy) == RF_1T1R) {
149
150                 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, ("1T1R\n"));
151
152                 ht_cap->mcs.rx_mask[0] = 0xFF;
153                 ht_cap->mcs.rx_mask[1] = 0x00;
154                 ht_cap->mcs.rx_mask[4] = 0x01;
155
156                 ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS7);
157         }
158 }
159
160 static void _rtl_init_mac80211(struct ieee80211_hw *hw)
161 {
162         struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
163         struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
164         struct ieee80211_supported_band *sband;
165
166         /* <1> use  mac->bands as mem for hw->wiphy->bands */
167         sband = &(rtlmac->bands[IEEE80211_BAND_2GHZ]);
168
169         /*
170          * <2> set hw->wiphy->bands[IEEE80211_BAND_2GHZ]
171          * to default value(1T1R)
172          */
173         memcpy(&(rtlmac->bands[IEEE80211_BAND_2GHZ]), &rtl_band_2ghz,
174                sizeof(struct ieee80211_supported_band));
175
176         /* <3> init ht cap base on ant_num */
177         _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
178
179         /* <4> set mac->sband to wiphy->sband */
180         hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
181
182         /* <5> set hw caps */
183         hw->flags = IEEE80211_HW_SIGNAL_DBM |
184             IEEE80211_HW_RX_INCLUDES_FCS |
185             IEEE80211_HW_BEACON_FILTER | IEEE80211_HW_AMPDU_AGGREGATION | /*PS*/
186             /*IEEE80211_HW_SUPPORTS_PS | */
187             /*IEEE80211_HW_PS_NULLFUNC_STACK | */
188             /*IEEE80211_HW_SUPPORTS_DYNAMIC_PS | */
189             IEEE80211_HW_REPORTS_TX_ACK_STATUS | 0;
190
191         hw->wiphy->interface_modes =
192             BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_ADHOC);
193
194         hw->wiphy->rts_threshold = 2347;
195
196         hw->queues = AC_MAX;
197         hw->extra_tx_headroom = RTL_TX_HEADER_SIZE;
198
199         /* TODO: Correct this value for our hw */
200         /* TODO: define these hard code value */
201         hw->channel_change_time = 100;
202         hw->max_listen_interval = 5;
203         hw->max_rate_tries = 4;
204         /* hw->max_rates = 1; */
205
206         /* <6> mac address */
207         if (is_valid_ether_addr(rtlefuse->dev_addr)) {
208                 SET_IEEE80211_PERM_ADDR(hw, rtlefuse->dev_addr);
209         } else {
210                 u8 rtlmac[] = { 0x00, 0xe0, 0x4c, 0x81, 0x92, 0x00 };
211                 get_random_bytes((rtlmac + (ETH_ALEN - 1)), 1);
212                 SET_IEEE80211_PERM_ADDR(hw, rtlmac);
213         }
214
215 }
216
217 static void _rtl_init_deferred_work(struct ieee80211_hw *hw)
218 {
219         struct rtl_priv *rtlpriv = rtl_priv(hw);
220
221         /* <1> timer */
222         init_timer(&rtlpriv->works.watchdog_timer);
223         setup_timer(&rtlpriv->works.watchdog_timer,
224                     rtl_watch_dog_timer_callback, (unsigned long)hw);
225
226         /* <2> work queue */
227         rtlpriv->works.hw = hw;
228         rtlpriv->works.rtl_wq = alloc_workqueue(rtlpriv->cfg->name, 0, 0);
229         INIT_DELAYED_WORK(&rtlpriv->works.watchdog_wq,
230                           (void *)rtl_watchdog_wq_callback);
231         INIT_DELAYED_WORK(&rtlpriv->works.ips_nic_off_wq,
232                           (void *)rtl_ips_nic_off_wq_callback);
233
234 }
235
236 void rtl_deinit_deferred_work(struct ieee80211_hw *hw)
237 {
238         struct rtl_priv *rtlpriv = rtl_priv(hw);
239
240         del_timer_sync(&rtlpriv->works.watchdog_timer);
241
242         cancel_delayed_work(&rtlpriv->works.watchdog_wq);
243         cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq);
244 }
245
246 void rtl_init_rfkill(struct ieee80211_hw *hw)
247 {
248         struct rtl_priv *rtlpriv = rtl_priv(hw);
249
250         bool radio_state;
251         bool blocked;
252         u8 valid = 0;
253
254         /*set init state to on */
255         rtlpriv->rfkill.rfkill_state = 1;
256         wiphy_rfkill_set_hw_state(hw->wiphy, 0);
257
258         radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
259
260         if (valid) {
261                 printk(KERN_INFO "rtlwifi: wireless switch is %s\n",
262                                 rtlpriv->rfkill.rfkill_state ? "on" : "off");
263
264                 rtlpriv->rfkill.rfkill_state = radio_state;
265
266                 blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
267                 wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
268         }
269
270         wiphy_rfkill_start_polling(hw->wiphy);
271 }
272
273 void rtl_deinit_rfkill(struct ieee80211_hw *hw)
274 {
275         wiphy_rfkill_stop_polling(hw->wiphy);
276 }
277
278 int rtl_init_core(struct ieee80211_hw *hw)
279 {
280         struct rtl_priv *rtlpriv = rtl_priv(hw);
281         struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
282
283         /* <1> init mac80211 */
284         _rtl_init_mac80211(hw);
285         rtlmac->hw = hw;
286
287         /* <2> rate control register */
288         hw->rate_control_algorithm = "rtl_rc";
289
290         /*
291          * <3> init CRDA must come after init
292          * mac80211 hw  in _rtl_init_mac80211.
293          */
294         if (rtl_regd_init(hw, rtl_reg_notifier)) {
295                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, ("REGD init failed\n"));
296                 return 1;
297         } else {
298                 /* CRDA regd hint must after init CRDA */
299                 if (regulatory_hint(hw->wiphy, rtlpriv->regd.alpha2)) {
300                         RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
301                                  ("regulatory_hint fail\n"));
302                 }
303         }
304
305         /* <4> locks */
306         mutex_init(&rtlpriv->locks.conf_mutex);
307         spin_lock_init(&rtlpriv->locks.ips_lock);
308         spin_lock_init(&rtlpriv->locks.irq_th_lock);
309         spin_lock_init(&rtlpriv->locks.h2c_lock);
310         spin_lock_init(&rtlpriv->locks.rf_ps_lock);
311         spin_lock_init(&rtlpriv->locks.rf_lock);
312         spin_lock_init(&rtlpriv->locks.lps_lock);
313
314         rtlmac->link_state = MAC80211_NOLINK;
315
316         /* <5> init deferred work */
317         _rtl_init_deferred_work(hw);
318
319         return 0;
320 }
321
322 void rtl_deinit_core(struct ieee80211_hw *hw)
323 {
324 }
325
326 void rtl_init_rx_config(struct ieee80211_hw *hw)
327 {
328         struct rtl_priv *rtlpriv = rtl_priv(hw);
329         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
330
331         rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *) (&mac->rx_conf));
332         rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_MGT_FILTER,
333                                       (u8 *) (&mac->rx_mgt_filter));
334         rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_CTRL_FILTER,
335                                       (u8 *) (&mac->rx_ctrl_filter));
336         rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_DATA_FILTER,
337                                       (u8 *) (&mac->rx_data_filter));
338 }
339
340 /*********************************************************
341  *
342  * tx information functions
343  *
344  *********************************************************/
345 static void _rtl_qurey_shortpreamble_mode(struct ieee80211_hw *hw,
346                                           struct rtl_tcb_desc *tcb_desc,
347                                           struct ieee80211_tx_info *info)
348 {
349         struct rtl_priv *rtlpriv = rtl_priv(hw);
350         u8 rate_flag = info->control.rates[0].flags;
351
352         tcb_desc->use_shortpreamble = false;
353
354         /* 1M can only use Long Preamble. 11B spec */
355         if (tcb_desc->hw_rate == rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M])
356                 return;
357         else if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
358                 tcb_desc->use_shortpreamble = true;
359
360         return;
361 }
362
363 static void _rtl_query_shortgi(struct ieee80211_hw *hw,
364                                struct rtl_tcb_desc *tcb_desc,
365                                struct ieee80211_tx_info *info)
366 {
367         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
368         u8 rate_flag = info->control.rates[0].flags;
369
370         tcb_desc->use_shortgi = false;
371
372         if (!mac->ht_enable)
373                 return;
374
375         if (!mac->sgi_40 && !mac->sgi_20)
376                 return;
377
378         if ((mac->bw_40 == true) && mac->sgi_40)
379                 tcb_desc->use_shortgi = true;
380         else if ((mac->bw_40 == false) && mac->sgi_20)
381                 tcb_desc->use_shortgi = true;
382
383         if (!(rate_flag & IEEE80211_TX_RC_SHORT_GI))
384                 tcb_desc->use_shortgi = false;
385
386 }
387
388 static void _rtl_query_protection_mode(struct ieee80211_hw *hw,
389                                        struct rtl_tcb_desc *tcb_desc,
390                                        struct ieee80211_tx_info *info)
391 {
392         struct rtl_priv *rtlpriv = rtl_priv(hw);
393         u8 rate_flag = info->control.rates[0].flags;
394
395         /* Common Settings */
396         tcb_desc->rts_stbc = false;
397         tcb_desc->cts_enable = false;
398         tcb_desc->rts_sc = 0;
399         tcb_desc->rts_bw = false;
400         tcb_desc->rts_use_shortpreamble = false;
401         tcb_desc->rts_use_shortgi = false;
402
403         if (rate_flag & IEEE80211_TX_RC_USE_CTS_PROTECT) {
404                 /* Use CTS-to-SELF in protection mode. */
405                 tcb_desc->rts_enable = true;
406                 tcb_desc->cts_enable = true;
407                 tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
408         } else if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
409                 /* Use RTS-CTS in protection mode. */
410                 tcb_desc->rts_enable = true;
411                 tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
412         }
413
414 }
415
416 static void _rtl_txrate_selectmode(struct ieee80211_hw *hw,
417                                    struct rtl_tcb_desc *tcb_desc)
418 {
419         struct rtl_priv *rtlpriv = rtl_priv(hw);
420         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
421
422         if (!tcb_desc->disable_ratefallback || !tcb_desc->use_driver_rate) {
423                 if (mac->opmode == NL80211_IFTYPE_STATION)
424                         tcb_desc->ratr_index = 0;
425                 else if (mac->opmode == NL80211_IFTYPE_ADHOC) {
426                         if (tcb_desc->multicast || tcb_desc->broadcast) {
427                                 tcb_desc->hw_rate =
428                                     rtlpriv->cfg->maps[RTL_RC_CCK_RATE2M];
429                                 tcb_desc->use_driver_rate = 1;
430                         } else {
431                                 /* TODO */
432                         }
433                 }
434         }
435
436         if (rtlpriv->dm.useramask) {
437                 /* TODO adhoc and station handled differently in the future */
438                 tcb_desc->mac_id = 0;
439
440                 if ((mac->mode == WIRELESS_MODE_N_24G) ||
441                     (mac->mode == WIRELESS_MODE_N_5G)) {
442                         tcb_desc->ratr_index = RATR_INX_WIRELESS_NGB;
443                 } else if (mac->mode & WIRELESS_MODE_G) {
444                         tcb_desc->ratr_index = RATR_INX_WIRELESS_GB;
445                 } else if (mac->mode & WIRELESS_MODE_B) {
446                         tcb_desc->ratr_index = RATR_INX_WIRELESS_B;
447                 }
448         }
449
450 }
451
452 static void _rtl_query_bandwidth_mode(struct ieee80211_hw *hw,
453                                       struct rtl_tcb_desc *tcb_desc)
454 {
455         struct rtl_priv *rtlpriv = rtl_priv(hw);
456         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
457
458         tcb_desc->packet_bw = false;
459
460         if (!mac->bw_40 || !mac->ht_enable)
461                 return;
462
463         if (tcb_desc->multicast || tcb_desc->broadcast)
464                 return;
465
466         /*use legency rate, shall use 20MHz */
467         if (tcb_desc->hw_rate <= rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M])
468                 return;
469
470         tcb_desc->packet_bw = true;
471 }
472
473 static u8 _rtl_get_highest_n_rate(struct ieee80211_hw *hw)
474 {
475         struct rtl_priv *rtlpriv = rtl_priv(hw);
476         struct rtl_phy *rtlphy = &(rtlpriv->phy);
477         u8 hw_rate;
478
479         if (get_rf_type(rtlphy) == RF_2T2R)
480                 hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15];
481         else
482                 hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS7];
483
484         return hw_rate;
485 }
486
487 void rtl_get_tcb_desc(struct ieee80211_hw *hw,
488                       struct ieee80211_tx_info *info,
489                       struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc)
490 {
491         struct rtl_priv *rtlpriv = rtl_priv(hw);
492         struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
493         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
494         struct ieee80211_rate *txrate;
495         __le16 fc = hdr->frame_control;
496
497         memset(tcb_desc, 0, sizeof(struct rtl_tcb_desc));
498
499         if (ieee80211_is_data(fc)) {
500                 txrate = ieee80211_get_tx_rate(hw, info);
501                 tcb_desc->hw_rate = txrate->hw_value;
502
503                 /*
504                  *we set data rate RTL_RC_CCK_RATE1M
505                  *in rtl_rc.c   if skb is special data or
506                  *mgt which need low data rate.
507                  */
508
509                 /*
510                  *So tcb_desc->hw_rate is just used for
511                  *special data and mgt frames
512                  */
513                 if (tcb_desc->hw_rate < rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M]) {
514                         tcb_desc->use_driver_rate = true;
515                         tcb_desc->ratr_index = 7;
516
517                         tcb_desc->hw_rate =
518                             rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M];
519                         tcb_desc->disable_ratefallback = 1;
520                 } else {
521                         /*
522                          *because hw will nerver use hw_rate
523                          *when tcb_desc->use_driver_rate = false
524                          *so we never set highest N rate here,
525                          *and N rate will all be controlled by FW
526                          *when tcb_desc->use_driver_rate = false
527                          */
528                         if (rtlmac->ht_enable) {
529                                 tcb_desc->hw_rate = _rtl_get_highest_n_rate(hw);
530                         } else {
531                                 if (rtlmac->mode == WIRELESS_MODE_B) {
532                                         tcb_desc->hw_rate =
533                                            rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M];
534                                 } else {
535                                         tcb_desc->hw_rate =
536                                            rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M];
537                                 }
538                         }
539                 }
540
541                 if (is_multicast_ether_addr(ieee80211_get_DA(hdr)))
542                         tcb_desc->multicast = 1;
543                 else if (is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
544                         tcb_desc->broadcast = 1;
545
546                 _rtl_txrate_selectmode(hw, tcb_desc);
547                 _rtl_query_bandwidth_mode(hw, tcb_desc);
548                 _rtl_qurey_shortpreamble_mode(hw, tcb_desc, info);
549                 _rtl_query_shortgi(hw, tcb_desc, info);
550                 _rtl_query_protection_mode(hw, tcb_desc, info);
551         } else {
552                 tcb_desc->use_driver_rate = true;
553                 tcb_desc->ratr_index = 7;
554                 tcb_desc->disable_ratefallback = 1;
555                 tcb_desc->mac_id = 0;
556
557                 tcb_desc->hw_rate = rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M];
558         }
559 }
560 EXPORT_SYMBOL(rtl_get_tcb_desc);
561
562 bool rtl_tx_mgmt_proc(struct ieee80211_hw *hw, struct sk_buff *skb)
563 {
564         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
565         struct rtl_priv *rtlpriv = rtl_priv(hw);
566         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
567         __le16 fc = hdr->frame_control;
568
569         if (ieee80211_is_auth(fc)) {
570                 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, ("MAC80211_LINKING\n"));
571                 rtl_ips_nic_on(hw);
572
573                 mac->link_state = MAC80211_LINKING;
574         }
575
576         return true;
577 }
578
579 bool rtl_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
580 {
581         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
582         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
583         struct rtl_priv *rtlpriv = rtl_priv(hw);
584         __le16 fc = hdr->frame_control;
585         u8 *act = (u8 *) (((u8 *) skb->data + MAC80211_3ADDR_LEN));
586         u8 category;
587
588         if (!ieee80211_is_action(fc))
589                 return true;
590
591         category = *act;
592         act++;
593         switch (category) {
594         case ACT_CAT_BA:
595                 switch (*act) {
596                 case ACT_ADDBAREQ:
597                         if (mac->act_scanning)
598                                 return false;
599
600                         RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
601                                  ("%s ACT_ADDBAREQ From :" MAC_FMT "\n",
602                                   is_tx ? "Tx" : "Rx", MAC_ARG(hdr->addr2)));
603                         break;
604                 case ACT_ADDBARSP:
605                         RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
606                                  ("%s ACT_ADDBARSP From :" MAC_FMT "\n",
607                                   is_tx ? "Tx" : "Rx", MAC_ARG(hdr->addr2)));
608                         break;
609                 case ACT_DELBA:
610                         RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
611                                  ("ACT_ADDBADEL From :" MAC_FMT "\n",
612                                   MAC_ARG(hdr->addr2)));
613                         break;
614                 }
615                 break;
616         default:
617                 break;
618         }
619
620         return true;
621 }
622
623 /*should call before software enc*/
624 u8 rtl_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
625 {
626         struct rtl_priv *rtlpriv = rtl_priv(hw);
627         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
628         struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
629         __le16 fc = hdr->frame_control;
630         u16 ether_type;
631         u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb);
632         const struct iphdr *ip;
633
634         if (!ieee80211_is_data(fc))
635                 return false;
636
637         if (ieee80211_is_nullfunc(fc))
638                 return true;
639
640         ip = (struct iphdr *)((u8 *) skb->data + mac_hdr_len +
641                               SNAP_SIZE + PROTOC_TYPE_SIZE);
642         ether_type = *(u16 *) ((u8 *) skb->data + mac_hdr_len + SNAP_SIZE);
643
644         if (ETH_P_IP == ether_type) {
645                 if (IPPROTO_UDP == ip->protocol) {
646                         struct udphdr *udp = (struct udphdr *)((u8 *) ip +
647                                                                (ip->ihl << 2));
648                         if (((((u8 *) udp)[1] == 68) &&
649                              (((u8 *) udp)[3] == 67)) ||
650                             ((((u8 *) udp)[1] == 67) &&
651                              (((u8 *) udp)[3] == 68))) {
652                                 /*
653                                  * 68 : UDP BOOTP client
654                                  * 67 : UDP BOOTP server
655                                  */
656                                 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV),
657                                          DBG_DMESG, ("dhcp %s !!\n",
658                                                      (is_tx) ? "Tx" : "Rx"));
659
660                                 if (is_tx) {
661                                         rtl_lps_leave(hw);
662                                         ppsc->last_delaylps_stamp_jiffies =
663                                             jiffies;
664                                 }
665
666                                 return true;
667                         }
668                 }
669         } else if (ETH_P_ARP == ether_type) {
670                 if (is_tx) {
671                         rtl_lps_leave(hw);
672                         ppsc->last_delaylps_stamp_jiffies = jiffies;
673                 }
674
675                 return true;
676         } else if (ETH_P_PAE == ether_type) {
677                 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
678                          ("802.1X %s EAPOL pkt!!\n", (is_tx) ? "Tx" : "Rx"));
679
680                 if (is_tx) {
681                         rtl_lps_leave(hw);
682                         ppsc->last_delaylps_stamp_jiffies = jiffies;
683                 }
684
685                 return true;
686         } else if (ETH_P_IPV6 == ether_type) {
687                 /* IPv6 */
688                 return true;
689         }
690
691         return false;
692 }
693
694 /*********************************************************
695  *
696  * functions called by core.c
697  *
698  *********************************************************/
699 int rtl_tx_agg_start(struct ieee80211_hw *hw, const u8 *ra, u16 tid, u16 *ssn)
700 {
701         struct rtl_priv *rtlpriv = rtl_priv(hw);
702         struct rtl_tid_data *tid_data;
703         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
704
705         RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
706                  ("on ra = %pM tid = %d\n", ra, tid));
707
708         if (unlikely(tid >= MAX_TID_COUNT))
709                 return -EINVAL;
710
711         if (mac->tids[tid].agg.agg_state != RTL_AGG_OFF) {
712                 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
713                          ("Start AGG when state is not RTL_AGG_OFF !\n"));
714                 return -ENXIO;
715         }
716
717         tid_data = &mac->tids[tid];
718         *ssn = SEQ_TO_SN(tid_data->seq_number);
719
720         RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
721                  ("HW queue is empty tid:%d\n", tid));
722         tid_data->agg.agg_state = RTL_AGG_ON;
723
724         ieee80211_start_tx_ba_cb_irqsafe(mac->vif, ra, tid);
725
726         return 0;
727 }
728
729 int rtl_tx_agg_stop(struct ieee80211_hw *hw, const u8 * ra, u16 tid)
730 {
731         int ssn = -1;
732         struct rtl_priv *rtlpriv = rtl_priv(hw);
733         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
734         struct rtl_tid_data *tid_data;
735
736         if (!ra) {
737                 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG, ("ra = NULL\n"));
738                 return -EINVAL;
739         }
740
741         if (unlikely(tid >= MAX_TID_COUNT))
742                 return -EINVAL;
743
744         if (mac->tids[tid].agg.agg_state != RTL_AGG_ON)
745                 RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
746                          ("Stopping AGG while state not ON or starting\n"));
747
748         tid_data = &mac->tids[tid];
749         ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
750
751         mac->tids[tid].agg.agg_state = RTL_AGG_OFF;
752
753         ieee80211_stop_tx_ba_cb_irqsafe(mac->vif, ra, tid);
754
755         return 0;
756 }
757
758 /*********************************************************
759  *
760  * wq & timer callback functions
761  *
762  *********************************************************/
763 void rtl_watchdog_wq_callback(void *data)
764 {
765         struct rtl_works *rtlworks = container_of_dwork_rtl(data,
766                                                             struct rtl_works,
767                                                             watchdog_wq);
768         struct ieee80211_hw *hw = rtlworks->hw;
769         struct rtl_priv *rtlpriv = rtl_priv(hw);
770         struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
771         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
772
773         bool busytraffic = false;
774         bool higher_busytraffic = false;
775         bool higher_busyrxtraffic = false;
776         bool higher_busytxtraffic = false;
777
778         u8 idx = 0;
779         u32 rx_cnt_inp4eriod = 0;
780         u32 tx_cnt_inp4eriod = 0;
781         u32 aver_rx_cnt_inperiod = 0;
782         u32 aver_tx_cnt_inperiod = 0;
783
784         bool enter_ps = false;
785
786         if (is_hal_stop(rtlhal))
787                 return;
788
789         /* <1> Determine if action frame is allowed */
790         if (mac->link_state > MAC80211_NOLINK) {
791                 if (mac->cnt_after_linked < 20)
792                         mac->cnt_after_linked++;
793         } else {
794                 mac->cnt_after_linked = 0;
795         }
796
797         /* <2> DM */
798         rtlpriv->cfg->ops->dm_watchdog(hw);
799
800         /*
801          *<3> to check if traffic busy, if
802          * busytraffic we don't change channel
803          */
804         if (mac->link_state >= MAC80211_LINKED) {
805
806                 /* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */
807                 for (idx = 0; idx <= 2; idx++) {
808                         rtlpriv->link_info.num_rx_in4period[idx] =
809                             rtlpriv->link_info.num_rx_in4period[idx + 1];
810                         rtlpriv->link_info.num_tx_in4period[idx] =
811                             rtlpriv->link_info.num_tx_in4period[idx + 1];
812                 }
813                 rtlpriv->link_info.num_rx_in4period[3] =
814                     rtlpriv->link_info.num_rx_inperiod;
815                 rtlpriv->link_info.num_tx_in4period[3] =
816                     rtlpriv->link_info.num_tx_inperiod;
817                 for (idx = 0; idx <= 3; idx++) {
818                         rx_cnt_inp4eriod +=
819                             rtlpriv->link_info.num_rx_in4period[idx];
820                         tx_cnt_inp4eriod +=
821                             rtlpriv->link_info.num_tx_in4period[idx];
822                 }
823                 aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4;
824                 aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4;
825
826                 /* (2) check traffic busy */
827                 if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100)
828                         busytraffic = true;
829
830                 /* Higher Tx/Rx data. */
831                 if (aver_rx_cnt_inperiod > 4000 ||
832                     aver_tx_cnt_inperiod > 4000) {
833                         higher_busytraffic = true;
834
835                         /* Extremely high Rx data. */
836                         if (aver_rx_cnt_inperiod > 5000)
837                                 higher_busyrxtraffic = true;
838                         else
839                                 higher_busytxtraffic = false;
840                 }
841
842                 if (((rtlpriv->link_info.num_rx_inperiod +
843                       rtlpriv->link_info.num_tx_inperiod) > 8) ||
844                     (rtlpriv->link_info.num_rx_inperiod > 2))
845                         enter_ps = false;
846                 else
847                         enter_ps = true;
848
849                 /* LeisurePS only work in infra mode. */
850                 if (enter_ps)
851                         rtl_lps_enter(hw);
852                 else
853                         rtl_lps_leave(hw);
854         }
855
856         rtlpriv->link_info.num_rx_inperiod = 0;
857         rtlpriv->link_info.num_tx_inperiod = 0;
858
859         rtlpriv->link_info.busytraffic = busytraffic;
860         rtlpriv->link_info.higher_busytraffic = higher_busytraffic;
861         rtlpriv->link_info.higher_busyrxtraffic = higher_busyrxtraffic;
862
863 }
864
865 void rtl_watch_dog_timer_callback(unsigned long data)
866 {
867         struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
868         struct rtl_priv *rtlpriv = rtl_priv(hw);
869
870         queue_delayed_work(rtlpriv->works.rtl_wq,
871                            &rtlpriv->works.watchdog_wq, 0);
872
873         mod_timer(&rtlpriv->works.watchdog_timer,
874                   jiffies + MSECS(RTL_WATCH_DOG_TIME));
875 }
876
877 /*********************************************************
878  *
879  * sysfs functions
880  *
881  *********************************************************/
882 static ssize_t rtl_show_debug_level(struct device *d,
883                                     struct device_attribute *attr, char *buf)
884 {
885         struct ieee80211_hw *hw = dev_get_drvdata(d);
886         struct rtl_priv *rtlpriv = rtl_priv(hw);
887
888         return sprintf(buf, "0x%08X\n", rtlpriv->dbg.global_debuglevel);
889 }
890
891 static ssize_t rtl_store_debug_level(struct device *d,
892                                      struct device_attribute *attr,
893                                      const char *buf, size_t count)
894 {
895         struct ieee80211_hw *hw = dev_get_drvdata(d);
896         struct rtl_priv *rtlpriv = rtl_priv(hw);
897         unsigned long val;
898         int ret;
899
900         ret = strict_strtoul(buf, 0, &val);
901         if (ret) {
902                 printk(KERN_DEBUG "%s is not in hex or decimal form.\n", buf);
903         } else {
904                 rtlpriv->dbg.global_debuglevel = val;
905                 printk(KERN_DEBUG "debuglevel:%x\n",
906                        rtlpriv->dbg.global_debuglevel);
907         }
908
909         return strnlen(buf, count);
910 }
911
912 static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
913                    rtl_show_debug_level, rtl_store_debug_level);
914
915 static struct attribute *rtl_sysfs_entries[] = {
916
917         &dev_attr_debug_level.attr,
918
919         NULL
920 };
921
922 /*
923  * "name" is folder name witch will be
924  * put in device directory like :
925  * sys/devices/pci0000:00/0000:00:1c.4/
926  * 0000:06:00.0/rtl_sysfs
927  */
928 struct attribute_group rtl_attribute_group = {
929         .name = "rtlsysfs",
930         .attrs = rtl_sysfs_entries,
931 };
932
933 MODULE_AUTHOR("lizhaoming       <chaoming_li@realsil.com.cn>");
934 MODULE_AUTHOR("Realtek WlanFAE  <wlanfae@realtek.com>");
935 MODULE_AUTHOR("Larry Finger     <Larry.FInger@lwfinger.net>");
936 MODULE_LICENSE("GPL");
937 MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core");
938
939 static int __init rtl_core_module_init(void)
940 {
941         if (rtl_rate_control_register())
942                 printk(KERN_ERR "rtlwifi: Unable to register rtl_rc,"
943                        "use default RC !!\n");
944         return 0;
945 }
946
947 static void __exit rtl_core_module_exit(void)
948 {
949          /*RC*/
950         rtl_rate_control_unregister();
951 }
952
953 module_init(rtl_core_module_init);
954 module_exit(rtl_core_module_exit);