gma500: begin the config based split
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / rtl8192e / r8192E_core.c
blob58d800f1b5ee1878f66da3717677eb816cdc7e74
1 /******************************************************************************
2 * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
3 * Linux device driver for RTL8192E
5 * Based on the r8180 driver, which is:
6 * Copyright 2004-2005 Andrea Merello <andreamrl@tiscali.it>, et al.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 * The full GNU General Public License is included in this distribution in the
21 * file called LICENSE.
23 * Contact Information:
24 * Jerry chuang <wlanfae@realtek.com>
28 #include <linux/vmalloc.h>
29 #include <linux/slab.h>
30 #include <asm/uaccess.h>
31 #include "r8192E_hw.h"
32 #include "r8192E.h"
33 #include "r8190_rtl8256.h" /* RTL8225 Radio frontend */
34 #include "r8180_93cx6.h" /* Card EEPROM */
35 #include "r8192E_wx.h"
36 #include "r819xE_phy.h" //added by WB 4.30.2008
37 #include "r819xE_phyreg.h"
38 #include "r819xE_cmdpkt.h"
39 #include "r8192E_dm.h"
41 #ifdef CONFIG_PM
42 #include "r8192_pm.h"
43 #endif
45 #ifdef ENABLE_DOT11D
46 #include "ieee80211/dot11d.h"
47 #endif
49 //set here to open your trace code. //WB
50 u32 rt_global_debug_component = COMP_ERR ; //always open err flags on
52 static DEFINE_PCI_DEVICE_TABLE(rtl8192_pci_id_tbl) = {
53 /* Realtek */
54 { PCI_DEVICE(0x10ec, 0x8192) },
56 /* Corega */
57 { PCI_DEVICE(0x07aa, 0x0044) },
58 { PCI_DEVICE(0x07aa, 0x0047) },
62 static char ifname[IFNAMSIZ] = "wlan%d";
63 static int hwwep = 1; //default use hw. set 0 to use software security
64 static int channels = 0x3fff;
66 MODULE_LICENSE("GPL");
67 MODULE_VERSION("V 1.1");
68 MODULE_DEVICE_TABLE(pci, rtl8192_pci_id_tbl);
69 //MODULE_AUTHOR("Andrea Merello <andreamrl@tiscali.it>");
70 MODULE_DESCRIPTION("Linux driver for Realtek RTL819x WiFi cards");
73 module_param_string(ifname, ifname, sizeof(ifname), S_IRUGO|S_IWUSR);
74 module_param(hwwep,int, S_IRUGO|S_IWUSR);
75 module_param(channels,int, S_IRUGO|S_IWUSR);
77 MODULE_PARM_DESC(ifname," Net interface name, wlan%d=default");
78 MODULE_PARM_DESC(hwwep," Try to use hardware WEP support. Still broken and not available on all cards");
79 MODULE_PARM_DESC(channels," Channel bitmask for specific locales. NYI");
81 static int __devinit rtl8192_pci_probe(struct pci_dev *pdev,
82 const struct pci_device_id *id);
83 static void __devexit rtl8192_pci_disconnect(struct pci_dev *pdev);
85 static struct pci_driver rtl8192_pci_driver = {
86 .name = RTL819xE_MODULE_NAME, /* Driver name */
87 .id_table = rtl8192_pci_id_tbl, /* PCI_ID table */
88 .probe = rtl8192_pci_probe, /* probe fn */
89 .remove = __devexit_p(rtl8192_pci_disconnect), /* remove fn */
90 #ifdef CONFIG_PM
91 .suspend = rtl8192E_suspend, /* PM suspend fn */
92 .resume = rtl8192E_resume, /* PM resume fn */
93 #else
94 .suspend = NULL, /* PM suspend fn */
95 .resume = NULL, /* PM resume fn */
96 #endif
99 static void rtl8192_start_beacon(struct ieee80211_device *ieee80211);
100 static void rtl8192_stop_beacon(struct ieee80211_device *ieee80211);
101 static void rtl819x_watchdog_wqcallback(struct work_struct *work);
102 static void rtl8192_irq_rx_tasklet(unsigned long arg);
103 static void rtl8192_irq_tx_tasklet(unsigned long arg);
104 static void rtl8192_prepare_beacon(unsigned long arg);
105 static irqreturn_t rtl8192_interrupt(int irq, void *param);
106 static void rtl819xE_tx_cmd(struct r8192_priv *priv, struct sk_buff *skb);
107 static void rtl8192_update_ratr_table(struct r8192_priv *priv);
108 static void rtl8192_restart(struct work_struct *work);
109 static void watch_dog_timer_callback(unsigned long data);
110 static int _rtl8192_up(struct r8192_priv *priv);
111 static void rtl8192_cancel_deferred_work(struct r8192_priv* priv);
112 static short rtl8192_tx(struct r8192_priv *priv, struct sk_buff* skb);
114 #ifdef ENABLE_DOT11D
116 typedef struct _CHANNEL_LIST
118 u8 Channel[32];
119 u8 Len;
120 }CHANNEL_LIST, *PCHANNEL_LIST;
122 static const CHANNEL_LIST ChannelPlan[] = {
123 {{1,2,3,4,5,6,7,8,9,10,11,36,40,44,48,52,56,60,64,149,153,157,161,165},24}, //FCC
124 {{1,2,3,4,5,6,7,8,9,10,11},11}, //IC
125 {{1,2,3,4,5,6,7,8,9,10,11,12,13,36,40,44,48,52,56,60,64},21}, //ETSI
126 {{1,2,3,4,5,6,7,8,9,10,11,12,13},13}, //Spain. Change to ETSI.
127 {{1,2,3,4,5,6,7,8,9,10,11,12,13},13}, //France. Change to ETSI.
128 {{1,2,3,4,5,6,7,8,9,10,11,12,13,14,36,40,44,48,52,56,60,64},22}, //MKK //MKK
129 {{1,2,3,4,5,6,7,8,9,10,11,12,13,14,36,40,44,48,52,56,60,64},22},//MKK1
130 {{1,2,3,4,5,6,7,8,9,10,11,12,13},13}, //Israel.
131 {{1,2,3,4,5,6,7,8,9,10,11,12,13,14,36,40,44,48,52,56,60,64},22}, // For 11a , TELEC
132 {{1,2,3,4,5,6,7,8,9,10,11,12,13,14,36,40,44,48,52,56,60,64}, 22}, //MIC
133 {{1,2,3,4,5,6,7,8,9,10,11,12,13,14},14} //For Global Domain. 1-11:active scan, 12-14 passive scan. //+YJ, 080626
136 static void rtl819x_set_channel_map(u8 channel_plan, struct r8192_priv* priv)
138 int i, max_chan=-1, min_chan=-1;
139 struct ieee80211_device* ieee = priv->ieee80211;
140 switch (channel_plan)
142 case COUNTRY_CODE_FCC:
143 case COUNTRY_CODE_IC:
144 case COUNTRY_CODE_ETSI:
145 case COUNTRY_CODE_SPAIN:
146 case COUNTRY_CODE_FRANCE:
147 case COUNTRY_CODE_MKK:
148 case COUNTRY_CODE_MKK1:
149 case COUNTRY_CODE_ISRAEL:
150 case COUNTRY_CODE_TELEC:
151 case COUNTRY_CODE_MIC:
153 Dot11d_Init(ieee);
154 ieee->bGlobalDomain = false;
155 //acturally 8225 & 8256 rf chip only support B,G,24N mode
156 min_chan = 1;
157 max_chan = 14;
159 if (ChannelPlan[channel_plan].Len != 0){
160 // Clear old channel map
161 memset(GET_DOT11D_INFO(ieee)->channel_map, 0, sizeof(GET_DOT11D_INFO(ieee)->channel_map));
162 // Set new channel map
163 for (i=0;i<ChannelPlan[channel_plan].Len;i++)
165 if (ChannelPlan[channel_plan].Channel[i] < min_chan || ChannelPlan[channel_plan].Channel[i] > max_chan)
166 break;
167 GET_DOT11D_INFO(ieee)->channel_map[ChannelPlan[channel_plan].Channel[i]] = 1;
170 break;
172 case COUNTRY_CODE_GLOBAL_DOMAIN:
174 GET_DOT11D_INFO(ieee)->bEnabled = 0; //this flag enabled to follow 11d country IE setting, otherwise, it shall follow global domain setting
175 Dot11d_Reset(ieee);
176 ieee->bGlobalDomain = true;
177 break;
179 default:
180 break;
183 #endif
185 static inline bool rx_hal_is_cck_rate(prx_fwinfo_819x_pci pdrvinfo)
187 return (pdrvinfo->RxRate == DESC90_RATE1M ||
188 pdrvinfo->RxRate == DESC90_RATE2M ||
189 pdrvinfo->RxRate == DESC90_RATE5_5M ||
190 pdrvinfo->RxRate == DESC90_RATE11M) &&
191 !pdrvinfo->RxHT;
194 void CamResetAllEntry(struct r8192_priv* priv)
196 write_nic_dword(priv, RWCAM, BIT31|BIT30);
199 void write_cam(struct r8192_priv *priv, u8 addr, u32 data)
201 write_nic_dword(priv, WCAMI, data);
202 write_nic_dword(priv, RWCAM, BIT31|BIT16|(addr&0xff) );
205 u32 read_cam(struct r8192_priv *priv, u8 addr)
207 write_nic_dword(priv, RWCAM, 0x80000000|(addr&0xff) );
208 return read_nic_dword(priv, 0xa8);
211 u8 read_nic_byte(struct r8192_priv *priv, int x)
213 return 0xff & readb(priv->mem_start + x);
216 u32 read_nic_dword(struct r8192_priv *priv, int x)
218 return readl(priv->mem_start + x);
221 u16 read_nic_word(struct r8192_priv *priv, int x)
223 return readw(priv->mem_start + x);
226 void write_nic_byte(struct r8192_priv *priv, int x,u8 y)
228 writeb(y, priv->mem_start + x);
229 udelay(20);
232 void write_nic_dword(struct r8192_priv *priv, int x,u32 y)
234 writel(y, priv->mem_start + x);
235 udelay(20);
238 void write_nic_word(struct r8192_priv *priv, int x,u16 y)
240 writew(y, priv->mem_start + x);
241 udelay(20);
244 u8 rtl8192e_ap_sec_type(struct ieee80211_device *ieee)
246 static const u8 ccmp_ie[4] = {0x00,0x50,0xf2,0x04};
247 static const u8 ccmp_rsn_ie[4] = {0x00, 0x0f, 0xac, 0x04};
248 int wpa_ie_len= ieee->wpa_ie_len;
249 struct ieee80211_crypt_data* crypt;
250 int encrypt;
252 crypt = ieee->crypt[ieee->tx_keyidx];
254 encrypt = (ieee->current_network.capability & WLAN_CAPABILITY_PRIVACY) ||
255 (ieee->host_encrypt && crypt && crypt->ops &&
256 (0 == strcmp(crypt->ops->name,"WEP")));
258 /* simply judge */
259 if(encrypt && (wpa_ie_len == 0)) {
260 // wep encryption, no N mode setting */
261 return SEC_ALG_WEP;
262 } else if((wpa_ie_len != 0)) {
263 // parse pairwise key type */
264 if (((ieee->wpa_ie[0] == 0xdd) && (!memcmp(&(ieee->wpa_ie[14]),ccmp_ie,4))) ||
265 ((ieee->wpa_ie[0] == 0x30) && (!memcmp(&ieee->wpa_ie[10],ccmp_rsn_ie, 4))))
266 return SEC_ALG_CCMP;
267 else
268 return SEC_ALG_TKIP;
269 } else {
270 return SEC_ALG_NONE;
274 void rtl8192e_SetHwReg(struct ieee80211_device *ieee80211, u8 variable, u8 *val)
276 struct r8192_priv *priv = ieee80211_priv(ieee80211->dev);
278 switch(variable)
281 case HW_VAR_BSSID:
282 write_nic_dword(priv, BSSIDR, ((u32*)(val))[0]);
283 write_nic_word(priv, BSSIDR+2, ((u16*)(val+2))[0]);
284 break;
286 case HW_VAR_MEDIA_STATUS:
288 RT_OP_MODE OpMode = *((RT_OP_MODE *)(val));
289 u8 btMsr = read_nic_byte(priv, MSR);
291 btMsr &= 0xfc;
293 switch(OpMode)
295 case RT_OP_MODE_INFRASTRUCTURE:
296 btMsr |= MSR_INFRA;
297 break;
299 case RT_OP_MODE_IBSS:
300 btMsr |= MSR_ADHOC;
301 break;
303 case RT_OP_MODE_AP:
304 btMsr |= MSR_AP;
305 break;
307 default:
308 btMsr |= MSR_NOLINK;
309 break;
312 write_nic_byte(priv, MSR, btMsr);
314 break;
316 case HW_VAR_CHECK_BSSID:
318 u32 RegRCR, Type;
320 Type = ((u8*)(val))[0];
321 RegRCR = read_nic_dword(priv, RCR);
322 priv->ReceiveConfig = RegRCR;
324 if (Type == true)
325 RegRCR |= (RCR_CBSSID);
326 else if (Type == false)
327 RegRCR &= (~RCR_CBSSID);
329 write_nic_dword(priv, RCR,RegRCR);
330 priv->ReceiveConfig = RegRCR;
333 break;
335 case HW_VAR_SLOT_TIME:
337 priv->slot_time = val[0];
338 write_nic_byte(priv, SLOT_TIME, val[0]);
341 break;
343 case HW_VAR_ACK_PREAMBLE:
345 u32 regTmp = 0;
346 priv->short_preamble = (bool)(*(u8*)val );
347 regTmp = priv->basic_rate;
348 if (priv->short_preamble)
349 regTmp |= BRSR_AckShortPmb;
350 write_nic_dword(priv, RRSR, regTmp);
352 break;
354 case HW_VAR_CPU_RST:
355 write_nic_dword(priv, CPU_GEN, ((u32*)(val))[0]);
356 break;
358 default:
359 break;
364 static struct proc_dir_entry *rtl8192_proc = NULL;
366 static int proc_get_stats_ap(char *page, char **start,
367 off_t offset, int count,
368 int *eof, void *data)
370 struct r8192_priv *priv = data;
371 struct ieee80211_device *ieee = priv->ieee80211;
372 struct ieee80211_network *target;
373 int len = 0;
375 list_for_each_entry(target, &ieee->network_list, list) {
377 len += snprintf(page + len, count - len,
378 "%s ", target->ssid);
380 if(target->wpa_ie_len>0 || target->rsn_ie_len>0){
381 len += snprintf(page + len, count - len,
382 "WPA\n");
384 else{
385 len += snprintf(page + len, count - len,
386 "non_WPA\n");
391 *eof = 1;
392 return len;
395 static int proc_get_registers(char *page, char **start,
396 off_t offset, int count,
397 int *eof, void *data)
399 struct r8192_priv *priv = data;
400 int len = 0;
401 int i,n;
402 int max=0xff;
404 /* This dump the current register page */
405 len += snprintf(page + len, count - len,
406 "\n####################page 0##################\n ");
408 for(n=0;n<=max;)
410 len += snprintf(page + len, count - len,
411 "\nD: %2x > ",n);
413 for(i=0;i<16 && n<=max;i++,n++)
414 len += snprintf(page + len, count - len,
415 "%2x ",read_nic_byte(priv,n));
417 len += snprintf(page + len, count - len,"\n");
418 len += snprintf(page + len, count - len,
419 "\n####################page 1##################\n ");
420 for(n=0;n<=max;)
422 len += snprintf(page + len, count - len,
423 "\nD: %2x > ",n);
425 for(i=0;i<16 && n<=max;i++,n++)
426 len += snprintf(page + len, count - len,
427 "%2x ",read_nic_byte(priv,0x100|n));
430 len += snprintf(page + len, count - len,
431 "\n####################page 3##################\n ");
432 for(n=0;n<=max;)
434 len += snprintf(page + len, count - len,
435 "\nD: %2x > ",n);
437 for(i=0;i<16 && n<=max;i++,n++)
438 len += snprintf(page + len, count - len,
439 "%2x ",read_nic_byte(priv,0x300|n));
442 *eof = 1;
443 return len;
447 static int proc_get_stats_tx(char *page, char **start,
448 off_t offset, int count,
449 int *eof, void *data)
451 struct r8192_priv *priv = data;
453 int len = 0;
455 len += snprintf(page + len, count - len,
456 "TX VI priority ok int: %lu\n"
457 "TX VO priority ok int: %lu\n"
458 "TX BE priority ok int: %lu\n"
459 "TX BK priority ok int: %lu\n"
460 "TX MANAGE priority ok int: %lu\n"
461 "TX BEACON priority ok int: %lu\n"
462 "TX BEACON priority error int: %lu\n"
463 "TX CMDPKT priority ok int: %lu\n"
464 "TX queue stopped?: %d\n"
465 "TX fifo overflow: %lu\n"
466 "TX total data packets %lu\n"
467 "TX total data bytes :%lu\n",
468 priv->stats.txviokint,
469 priv->stats.txvookint,
470 priv->stats.txbeokint,
471 priv->stats.txbkokint,
472 priv->stats.txmanageokint,
473 priv->stats.txbeaconokint,
474 priv->stats.txbeaconerr,
475 priv->stats.txcmdpktokint,
476 netif_queue_stopped(priv->ieee80211->dev),
477 priv->stats.txoverflow,
478 priv->ieee80211->stats.tx_packets,
479 priv->ieee80211->stats.tx_bytes);
481 *eof = 1;
482 return len;
487 static int proc_get_stats_rx(char *page, char **start,
488 off_t offset, int count,
489 int *eof, void *data)
491 struct r8192_priv *priv = data;
492 int len = 0;
494 len += snprintf(page + len, count - len,
495 "RX packets: %lu\n"
496 "RX desc err: %lu\n"
497 "RX rx overflow error: %lu\n",
498 priv->stats.rxint,
499 priv->stats.rxrdu,
500 priv->stats.rxoverflow);
502 *eof = 1;
503 return len;
506 static void rtl8192_proc_module_init(void)
508 RT_TRACE(COMP_INIT, "Initializing proc filesystem\n");
509 rtl8192_proc=create_proc_entry(RTL819xE_MODULE_NAME, S_IFDIR, init_net.proc_net);
513 static void rtl8192_proc_module_remove(void)
515 remove_proc_entry(RTL819xE_MODULE_NAME, init_net.proc_net);
519 static void rtl8192_proc_remove_one(struct r8192_priv *priv)
521 struct net_device *dev = priv->ieee80211->dev;
523 printk("dev name=======> %s\n",dev->name);
525 if (priv->dir_dev) {
526 remove_proc_entry("stats-tx", priv->dir_dev);
527 remove_proc_entry("stats-rx", priv->dir_dev);
528 remove_proc_entry("stats-ap", priv->dir_dev);
529 remove_proc_entry("registers", priv->dir_dev);
530 remove_proc_entry("wlan0", rtl8192_proc);
531 priv->dir_dev = NULL;
536 static void rtl8192_proc_init_one(struct r8192_priv *priv)
538 struct net_device *dev = priv->ieee80211->dev;
539 struct proc_dir_entry *e;
541 priv->dir_dev = create_proc_entry(dev->name,
542 S_IFDIR | S_IRUGO | S_IXUGO,
543 rtl8192_proc);
544 if (!priv->dir_dev) {
545 RT_TRACE(COMP_ERR, "Unable to initialize /proc/net/rtl8192/%s\n",
546 dev->name);
547 return;
549 e = create_proc_read_entry("stats-rx", S_IFREG | S_IRUGO,
550 priv->dir_dev, proc_get_stats_rx, priv);
552 if (!e) {
553 RT_TRACE(COMP_ERR,"Unable to initialize "
554 "/proc/net/rtl8192/%s/stats-rx\n",
555 dev->name);
559 e = create_proc_read_entry("stats-tx", S_IFREG | S_IRUGO,
560 priv->dir_dev, proc_get_stats_tx, priv);
562 if (!e) {
563 RT_TRACE(COMP_ERR, "Unable to initialize "
564 "/proc/net/rtl8192/%s/stats-tx\n",
565 dev->name);
568 e = create_proc_read_entry("stats-ap", S_IFREG | S_IRUGO,
569 priv->dir_dev, proc_get_stats_ap, priv);
571 if (!e) {
572 RT_TRACE(COMP_ERR, "Unable to initialize "
573 "/proc/net/rtl8192/%s/stats-ap\n",
574 dev->name);
577 e = create_proc_read_entry("registers", S_IFREG | S_IRUGO,
578 priv->dir_dev, proc_get_registers, priv);
579 if (!e) {
580 RT_TRACE(COMP_ERR, "Unable to initialize "
581 "/proc/net/rtl8192/%s/registers\n",
582 dev->name);
586 static short check_nic_enough_desc(struct ieee80211_device *ieee, int prio)
588 struct r8192_priv *priv = ieee80211_priv(ieee->dev);
589 struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
591 /* for now we reserve two free descriptor as a safety boundary
592 * between the tail and the head
594 return (ring->entries - skb_queue_len(&ring->queue) >= 2);
597 static void tx_timeout(struct net_device *dev)
599 struct r8192_priv *priv = ieee80211_priv(dev);
601 schedule_work(&priv->reset_wq);
602 printk("TXTIMEOUT");
605 static void rtl8192_irq_enable(struct r8192_priv *priv)
607 u32 mask;
609 mask = IMR_ROK | IMR_VODOK | IMR_VIDOK | IMR_BEDOK | IMR_BKDOK |
610 IMR_HCCADOK | IMR_MGNTDOK | IMR_COMDOK | IMR_HIGHDOK |
611 IMR_BDOK | IMR_RXCMDOK | IMR_TIMEOUT0 | IMR_RDU | IMR_RXFOVW |
612 IMR_TXFOVW | IMR_BcnInt | IMR_TBDOK | IMR_TBDER;
614 write_nic_dword(priv, INTA_MASK, mask);
617 static void rtl8192_irq_disable(struct r8192_priv *priv)
619 write_nic_dword(priv, INTA_MASK, 0);
620 synchronize_irq(priv->irq);
623 static void rtl8192_update_msr(struct r8192_priv *priv)
625 u8 msr;
627 msr = read_nic_byte(priv, MSR);
628 msr &= ~ MSR_LINK_MASK;
630 /* do not change in link_state != WLAN_LINK_ASSOCIATED.
631 * msr must be updated if the state is ASSOCIATING.
632 * this is intentional and make sense for ad-hoc and
633 * master (see the create BSS/IBSS func)
635 if (priv->ieee80211->state == IEEE80211_LINKED){
637 if (priv->ieee80211->iw_mode == IW_MODE_INFRA)
638 msr |= (MSR_LINK_MANAGED<<MSR_LINK_SHIFT);
639 else if (priv->ieee80211->iw_mode == IW_MODE_ADHOC)
640 msr |= (MSR_LINK_ADHOC<<MSR_LINK_SHIFT);
641 else if (priv->ieee80211->iw_mode == IW_MODE_MASTER)
642 msr |= (MSR_LINK_MASTER<<MSR_LINK_SHIFT);
644 }else
645 msr |= (MSR_LINK_NONE<<MSR_LINK_SHIFT);
647 write_nic_byte(priv, MSR, msr);
650 static void rtl8192_set_chan(struct ieee80211_device *ieee80211, short ch)
652 struct r8192_priv *priv = ieee80211_priv(ieee80211->dev);
654 priv->chan = ch;
656 /* need to implement rf set channel here WB */
658 if (priv->rf_set_chan)
659 priv->rf_set_chan(ieee80211, priv->chan);
662 static void rtl8192_rx_enable(struct r8192_priv *priv)
664 write_nic_dword(priv, RDQDA, priv->rx_ring_dma);
667 /* the TX_DESC_BASE setting is according to the following queue index
668 * BK_QUEUE ===> 0
669 * BE_QUEUE ===> 1
670 * VI_QUEUE ===> 2
671 * VO_QUEUE ===> 3
672 * HCCA_QUEUE ===> 4
673 * TXCMD_QUEUE ===> 5
674 * MGNT_QUEUE ===> 6
675 * HIGH_QUEUE ===> 7
676 * BEACON_QUEUE ===> 8
677 * */
678 static const u32 TX_DESC_BASE[] = {BKQDA, BEQDA, VIQDA, VOQDA, HCCAQDA, CQDA, MQDA, HQDA, BQDA};
679 static void rtl8192_tx_enable(struct r8192_priv *priv)
681 u32 i;
683 for (i = 0; i < MAX_TX_QUEUE_COUNT; i++)
684 write_nic_dword(priv, TX_DESC_BASE[i], priv->tx_ring[i].dma);
686 ieee80211_reset_queue(priv->ieee80211);
690 static void rtl8192_free_rx_ring(struct r8192_priv *priv)
692 int i;
694 for (i = 0; i < priv->rxringcount; i++) {
695 struct sk_buff *skb = priv->rx_buf[i];
696 if (!skb)
697 continue;
699 pci_unmap_single(priv->pdev,
700 *((dma_addr_t *)skb->cb),
701 priv->rxbuffersize, PCI_DMA_FROMDEVICE);
702 kfree_skb(skb);
705 pci_free_consistent(priv->pdev, sizeof(*priv->rx_ring) * priv->rxringcount,
706 priv->rx_ring, priv->rx_ring_dma);
707 priv->rx_ring = NULL;
710 static void rtl8192_free_tx_ring(struct r8192_priv *priv, unsigned int prio)
712 struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
714 while (skb_queue_len(&ring->queue)) {
715 tx_desc_819x_pci *entry = &ring->desc[ring->idx];
716 struct sk_buff *skb = __skb_dequeue(&ring->queue);
718 pci_unmap_single(priv->pdev, le32_to_cpu(entry->TxBuffAddr),
719 skb->len, PCI_DMA_TODEVICE);
720 kfree_skb(skb);
721 ring->idx = (ring->idx + 1) % ring->entries;
724 pci_free_consistent(priv->pdev, sizeof(*ring->desc)*ring->entries,
725 ring->desc, ring->dma);
726 ring->desc = NULL;
729 void PHY_SetRtl8192eRfOff(struct r8192_priv *priv)
731 //disable RF-Chip A/B
732 rtl8192_setBBreg(priv, rFPGA0_XA_RFInterfaceOE, BIT4, 0x0);
733 //analog to digital off, for power save
734 rtl8192_setBBreg(priv, rFPGA0_AnalogParameter4, 0x300, 0x0);
735 //digital to analog off, for power save
736 rtl8192_setBBreg(priv, rFPGA0_AnalogParameter1, 0x18, 0x0);
737 //rx antenna off
738 rtl8192_setBBreg(priv, rOFDM0_TRxPathEnable, 0xf, 0x0);
739 //rx antenna off
740 rtl8192_setBBreg(priv, rOFDM1_TRxPathEnable, 0xf, 0x0);
741 //analog to digital part2 off, for power save
742 rtl8192_setBBreg(priv, rFPGA0_AnalogParameter1, 0x60, 0x0);
743 rtl8192_setBBreg(priv, rFPGA0_AnalogParameter1, 0x4, 0x0);
744 // Analog parameter!!Change bias and Lbus control.
745 write_nic_byte(priv, ANAPAR_FOR_8192PciE, 0x07);
748 static void rtl8192_halt_adapter(struct r8192_priv *priv, bool reset)
750 int i;
751 u8 OpMode;
752 u32 ulRegRead;
754 OpMode = RT_OP_MODE_NO_LINK;
755 priv->ieee80211->SetHwRegHandler(priv->ieee80211, HW_VAR_MEDIA_STATUS, &OpMode);
757 if (!priv->ieee80211->bSupportRemoteWakeUp) {
759 * disable tx/rx. In 8185 we write 0x10 (Reset bit),
760 * but here we make reference to WMAC and wirte 0x0
762 write_nic_byte(priv, CMDR, 0);
765 mdelay(20);
767 if (!reset) {
768 mdelay(150);
770 priv->bHwRfOffAction = 2;
773 * Call MgntActSet_RF_State instead to
774 * prevent RF config race condition.
776 if (!priv->ieee80211->bSupportRemoteWakeUp) {
777 PHY_SetRtl8192eRfOff(priv);
778 ulRegRead = read_nic_dword(priv, CPU_GEN);
779 ulRegRead |= CPU_GEN_SYSTEM_RESET;
780 write_nic_dword(priv,CPU_GEN, ulRegRead);
781 } else {
782 /* for WOL */
783 write_nic_dword(priv, WFCRC0, 0xffffffff);
784 write_nic_dword(priv, WFCRC1, 0xffffffff);
785 write_nic_dword(priv, WFCRC2, 0xffffffff);
787 /* Write PMR register */
788 write_nic_byte(priv, PMR, 0x5);
789 /* Disable tx, enanble rx */
790 write_nic_byte(priv, MacBlkCtrl, 0xa);
794 for(i = 0; i < MAX_QUEUE_SIZE; i++) {
795 skb_queue_purge(&priv->ieee80211->skb_waitQ [i]);
797 for(i = 0; i < MAX_QUEUE_SIZE; i++) {
798 skb_queue_purge(&priv->ieee80211->skb_aggQ [i]);
801 skb_queue_purge(&priv->skb_queue);
804 static void rtl8192_data_hard_stop(struct ieee80211_device *ieee80211)
808 static void rtl8192_data_hard_resume(struct ieee80211_device *ieee80211)
813 * this function TX data frames when the ieee80211 stack requires this.
814 * It checks also if we need to stop the ieee tx queue, eventually do it
816 static void rtl8192_hard_data_xmit(struct sk_buff *skb,
817 struct ieee80211_device *ieee80211, int rate)
819 struct r8192_priv *priv = ieee80211_priv(ieee80211->dev);
820 int ret;
821 cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
822 u8 queue_index = tcb_desc->queue_index;
824 /* shall not be referred by command packet */
825 BUG_ON(queue_index == TXCMD_QUEUE);
827 if (priv->bHwRadioOff || (!priv->up))
829 kfree_skb(skb);
830 return;
833 skb_push(skb, priv->ieee80211->tx_headroom);
834 ret = rtl8192_tx(priv, skb);
835 if (ret != 0) {
836 kfree_skb(skb);
839 if (queue_index != MGNT_QUEUE) {
840 priv->ieee80211->stats.tx_bytes += (skb->len - priv->ieee80211->tx_headroom);
841 priv->ieee80211->stats.tx_packets++;
846 * This is a rough attempt to TX a frame
847 * This is called by the ieee 80211 stack to TX management frames.
848 * If the ring is full packet are dropped (for data frame the queue
849 * is stopped before this can happen).
851 static int rtl8192_hard_start_xmit(struct sk_buff *skb, struct ieee80211_device *ieee80211)
853 struct r8192_priv *priv = ieee80211_priv(ieee80211->dev);
854 int ret;
855 cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
856 u8 queue_index = tcb_desc->queue_index;
858 if (queue_index != TXCMD_QUEUE) {
859 if (priv->bHwRadioOff || (!priv->up))
861 kfree_skb(skb);
862 return 0;
866 if (queue_index == TXCMD_QUEUE) {
867 rtl819xE_tx_cmd(priv, skb);
868 ret = 0;
869 return ret;
870 } else {
871 tcb_desc->RATRIndex = 7;
872 tcb_desc->bTxDisableRateFallBack = 1;
873 tcb_desc->bTxUseDriverAssingedRate = 1;
874 tcb_desc->bTxEnableFwCalcDur = 1;
875 skb_push(skb, ieee80211->tx_headroom);
876 ret = rtl8192_tx(priv, skb);
877 if (ret != 0) {
878 kfree_skb(skb);
882 return ret;
886 static void rtl8192_tx_isr(struct r8192_priv *priv, int prio)
888 struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
890 while (skb_queue_len(&ring->queue)) {
891 tx_desc_819x_pci *entry = &ring->desc[ring->idx];
892 struct sk_buff *skb;
895 * beacon packet will only use the first descriptor defaultly,
896 * and the OWN may not be cleared by the hardware
898 if (prio != BEACON_QUEUE) {
899 if (entry->OWN)
900 return;
901 ring->idx = (ring->idx + 1) % ring->entries;
904 skb = __skb_dequeue(&ring->queue);
905 pci_unmap_single(priv->pdev, le32_to_cpu(entry->TxBuffAddr),
906 skb->len, PCI_DMA_TODEVICE);
908 kfree_skb(skb);
911 if (prio != BEACON_QUEUE) {
912 /* try to deal with the pending packets */
913 tasklet_schedule(&priv->irq_tx_tasklet);
917 static void rtl8192_stop_beacon(struct ieee80211_device *ieee80211)
921 static void rtl8192_config_rate(struct r8192_priv *priv, u16* rate_config)
923 struct ieee80211_network *net;
924 u8 i=0, basic_rate = 0;
925 net = & priv->ieee80211->current_network;
927 for (i=0; i<net->rates_len; i++)
929 basic_rate = net->rates[i]&0x7f;
930 switch(basic_rate)
932 case MGN_1M: *rate_config |= RRSR_1M; break;
933 case MGN_2M: *rate_config |= RRSR_2M; break;
934 case MGN_5_5M: *rate_config |= RRSR_5_5M; break;
935 case MGN_11M: *rate_config |= RRSR_11M; break;
936 case MGN_6M: *rate_config |= RRSR_6M; break;
937 case MGN_9M: *rate_config |= RRSR_9M; break;
938 case MGN_12M: *rate_config |= RRSR_12M; break;
939 case MGN_18M: *rate_config |= RRSR_18M; break;
940 case MGN_24M: *rate_config |= RRSR_24M; break;
941 case MGN_36M: *rate_config |= RRSR_36M; break;
942 case MGN_48M: *rate_config |= RRSR_48M; break;
943 case MGN_54M: *rate_config |= RRSR_54M; break;
946 for (i=0; i<net->rates_ex_len; i++)
948 basic_rate = net->rates_ex[i]&0x7f;
949 switch(basic_rate)
951 case MGN_1M: *rate_config |= RRSR_1M; break;
952 case MGN_2M: *rate_config |= RRSR_2M; break;
953 case MGN_5_5M: *rate_config |= RRSR_5_5M; break;
954 case MGN_11M: *rate_config |= RRSR_11M; break;
955 case MGN_6M: *rate_config |= RRSR_6M; break;
956 case MGN_9M: *rate_config |= RRSR_9M; break;
957 case MGN_12M: *rate_config |= RRSR_12M; break;
958 case MGN_18M: *rate_config |= RRSR_18M; break;
959 case MGN_24M: *rate_config |= RRSR_24M; break;
960 case MGN_36M: *rate_config |= RRSR_36M; break;
961 case MGN_48M: *rate_config |= RRSR_48M; break;
962 case MGN_54M: *rate_config |= RRSR_54M; break;
968 #define SHORT_SLOT_TIME 9
969 #define NON_SHORT_SLOT_TIME 20
971 static void rtl8192_update_cap(struct r8192_priv *priv, u16 cap)
973 u32 tmp = 0;
974 struct ieee80211_network *net = &priv->ieee80211->current_network;
976 priv->short_preamble = cap & WLAN_CAPABILITY_SHORT_PREAMBLE;
977 tmp = priv->basic_rate;
978 if (priv->short_preamble)
979 tmp |= BRSR_AckShortPmb;
980 write_nic_dword(priv, RRSR, tmp);
982 if (net->mode & (IEEE_G|IEEE_N_24G))
984 u8 slot_time = 0;
985 if ((cap & WLAN_CAPABILITY_SHORT_SLOT)&&(!priv->ieee80211->pHTInfo->bCurrentRT2RTLongSlotTime))
986 {//short slot time
987 slot_time = SHORT_SLOT_TIME;
989 else //long slot time
990 slot_time = NON_SHORT_SLOT_TIME;
991 priv->slot_time = slot_time;
992 write_nic_byte(priv, SLOT_TIME, slot_time);
997 static void rtl8192_net_update(struct r8192_priv *priv)
999 struct ieee80211_network *net;
1000 u16 BcnTimeCfg = 0, BcnCW = 6, BcnIFS = 0xf;
1001 u16 rate_config = 0;
1002 net = &priv->ieee80211->current_network;
1004 /* update Basic rate: RR, BRSR */
1005 rtl8192_config_rate(priv, &rate_config);
1008 * Select RRSR (in Legacy-OFDM and CCK)
1009 * For 8190, we select only 24M, 12M, 6M, 11M, 5.5M,
1010 * 2M, and 1M from the Basic rate.
1011 * We do not use other rates.
1013 priv->basic_rate = rate_config &= 0x15f;
1015 /* BSSID */
1016 write_nic_dword(priv, BSSIDR, ((u32 *)net->bssid)[0]);
1017 write_nic_word(priv, BSSIDR+4, ((u16 *)net->bssid)[2]);
1019 if (priv->ieee80211->iw_mode == IW_MODE_ADHOC)
1021 write_nic_word(priv, ATIMWND, 2);
1022 write_nic_word(priv, BCN_DMATIME, 256);
1023 write_nic_word(priv, BCN_INTERVAL, net->beacon_interval);
1025 * BIT15 of BCN_DRV_EARLY_INT will indicate
1026 * whether software beacon or hw beacon is applied.
1028 write_nic_word(priv, BCN_DRV_EARLY_INT, 10);
1029 write_nic_byte(priv, BCN_ERR_THRESH, 100);
1031 BcnTimeCfg |= (BcnCW<<BCN_TCFG_CW_SHIFT);
1032 /* TODO: BcnIFS may required to be changed on ASIC */
1033 BcnTimeCfg |= BcnIFS<<BCN_TCFG_IFS;
1034 write_nic_word(priv, BCN_TCFG, BcnTimeCfg);
1038 static void rtl819xE_tx_cmd(struct r8192_priv *priv, struct sk_buff *skb)
1040 struct rtl8192_tx_ring *ring;
1041 tx_desc_819x_pci *entry;
1042 unsigned int idx;
1043 dma_addr_t mapping;
1044 cb_desc *tcb_desc;
1045 unsigned long flags;
1047 ring = &priv->tx_ring[TXCMD_QUEUE];
1048 mapping = pci_map_single(priv->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
1050 spin_lock_irqsave(&priv->irq_th_lock,flags);
1051 idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
1052 entry = &ring->desc[idx];
1054 tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1055 memset(entry,0,12);
1056 entry->LINIP = tcb_desc->bLastIniPkt;
1057 entry->FirstSeg = 1;//first segment
1058 entry->LastSeg = 1; //last segment
1059 if(tcb_desc->bCmdOrInit == DESC_PACKET_TYPE_INIT) {
1060 entry->CmdInit = DESC_PACKET_TYPE_INIT;
1061 } else {
1062 entry->CmdInit = DESC_PACKET_TYPE_NORMAL;
1063 entry->Offset = sizeof(TX_FWINFO_8190PCI) + 8;
1064 entry->PktSize = (u16)(tcb_desc->pkt_size + entry->Offset);
1065 entry->QueueSelect = QSLT_CMD;
1066 entry->TxFWInfoSize = 0x08;
1067 entry->RATid = (u8)DESC_PACKET_TYPE_INIT;
1069 entry->TxBufferSize = skb->len;
1070 entry->TxBuffAddr = cpu_to_le32(mapping);
1071 entry->OWN = 1;
1073 __skb_queue_tail(&ring->queue, skb);
1074 spin_unlock_irqrestore(&priv->irq_th_lock,flags);
1076 write_nic_byte(priv, TPPoll, TPPoll_CQ);
1078 return;
1082 * Mapping Software/Hardware descriptor queue id to "Queue Select Field"
1083 * in TxFwInfo data structure
1085 static u8 MapHwQueueToFirmwareQueue(u8 QueueID)
1087 u8 QueueSelect = 0;
1089 switch (QueueID) {
1090 case BE_QUEUE:
1091 QueueSelect = QSLT_BE;
1092 break;
1094 case BK_QUEUE:
1095 QueueSelect = QSLT_BK;
1096 break;
1098 case VO_QUEUE:
1099 QueueSelect = QSLT_VO;
1100 break;
1102 case VI_QUEUE:
1103 QueueSelect = QSLT_VI;
1104 break;
1106 case MGNT_QUEUE:
1107 QueueSelect = QSLT_MGNT;
1108 break;
1110 case BEACON_QUEUE:
1111 QueueSelect = QSLT_BEACON;
1112 break;
1114 case TXCMD_QUEUE:
1115 QueueSelect = QSLT_CMD;
1116 break;
1118 case HIGH_QUEUE:
1119 default:
1120 RT_TRACE(COMP_ERR, "Impossible Queue Selection: %d\n", QueueID);
1121 break;
1123 return QueueSelect;
1126 static u8 MRateToHwRate8190Pci(u8 rate)
1128 u8 ret = DESC90_RATE1M;
1130 switch(rate) {
1131 case MGN_1M: ret = DESC90_RATE1M; break;
1132 case MGN_2M: ret = DESC90_RATE2M; break;
1133 case MGN_5_5M: ret = DESC90_RATE5_5M; break;
1134 case MGN_11M: ret = DESC90_RATE11M; break;
1135 case MGN_6M: ret = DESC90_RATE6M; break;
1136 case MGN_9M: ret = DESC90_RATE9M; break;
1137 case MGN_12M: ret = DESC90_RATE12M; break;
1138 case MGN_18M: ret = DESC90_RATE18M; break;
1139 case MGN_24M: ret = DESC90_RATE24M; break;
1140 case MGN_36M: ret = DESC90_RATE36M; break;
1141 case MGN_48M: ret = DESC90_RATE48M; break;
1142 case MGN_54M: ret = DESC90_RATE54M; break;
1144 // HT rate since here
1145 case MGN_MCS0: ret = DESC90_RATEMCS0; break;
1146 case MGN_MCS1: ret = DESC90_RATEMCS1; break;
1147 case MGN_MCS2: ret = DESC90_RATEMCS2; break;
1148 case MGN_MCS3: ret = DESC90_RATEMCS3; break;
1149 case MGN_MCS4: ret = DESC90_RATEMCS4; break;
1150 case MGN_MCS5: ret = DESC90_RATEMCS5; break;
1151 case MGN_MCS6: ret = DESC90_RATEMCS6; break;
1152 case MGN_MCS7: ret = DESC90_RATEMCS7; break;
1153 case MGN_MCS8: ret = DESC90_RATEMCS8; break;
1154 case MGN_MCS9: ret = DESC90_RATEMCS9; break;
1155 case MGN_MCS10: ret = DESC90_RATEMCS10; break;
1156 case MGN_MCS11: ret = DESC90_RATEMCS11; break;
1157 case MGN_MCS12: ret = DESC90_RATEMCS12; break;
1158 case MGN_MCS13: ret = DESC90_RATEMCS13; break;
1159 case MGN_MCS14: ret = DESC90_RATEMCS14; break;
1160 case MGN_MCS15: ret = DESC90_RATEMCS15; break;
1161 case (0x80|0x20): ret = DESC90_RATEMCS32; break;
1163 default: break;
1165 return ret;
1169 static u8 QueryIsShort(u8 TxHT, u8 TxRate, cb_desc *tcb_desc)
1171 u8 tmp_Short;
1173 tmp_Short = (TxHT==1)?((tcb_desc->bUseShortGI)?1:0):((tcb_desc->bUseShortPreamble)?1:0);
1175 if(TxHT==1 && TxRate != DESC90_RATEMCS15)
1176 tmp_Short = 0;
1178 return tmp_Short;
1182 * The tx procedure is just as following,
1183 * skb->cb will contain all the following information,
1184 * priority, morefrag, rate, &dev.
1186 static short rtl8192_tx(struct r8192_priv *priv, struct sk_buff* skb)
1188 struct rtl8192_tx_ring *ring;
1189 unsigned long flags;
1190 cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1191 tx_desc_819x_pci *pdesc = NULL;
1192 TX_FWINFO_8190PCI *pTxFwInfo = NULL;
1193 dma_addr_t mapping;
1194 bool multi_addr = false, broad_addr = false, uni_addr = false;
1195 u8 *pda_addr = NULL;
1196 int idx;
1198 if (priv->bdisable_nic) {
1199 RT_TRACE(COMP_ERR, "Nic is disabled! Can't tx packet len=%d qidx=%d!!!\n",
1200 skb->len, tcb_desc->queue_index);
1201 return skb->len;
1204 #ifdef ENABLE_LPS
1205 priv->ieee80211->bAwakePktSent = true;
1206 #endif
1208 mapping = pci_map_single(priv->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
1210 /* collect the tx packets statitcs */
1211 pda_addr = ((u8 *)skb->data) + sizeof(TX_FWINFO_8190PCI);
1212 if (is_multicast_ether_addr(pda_addr))
1213 multi_addr = true;
1214 else if (is_broadcast_ether_addr(pda_addr))
1215 broad_addr = true;
1216 else
1217 uni_addr = true;
1219 if (uni_addr)
1220 priv->stats.txbytesunicast += (u8)(skb->len) - sizeof(TX_FWINFO_8190PCI);
1222 /* fill tx firmware */
1223 pTxFwInfo = (PTX_FWINFO_8190PCI)skb->data;
1224 memset(pTxFwInfo, 0, sizeof(TX_FWINFO_8190PCI));
1225 pTxFwInfo->TxHT = (tcb_desc->data_rate&0x80) ? 1 : 0;
1226 pTxFwInfo->TxRate = MRateToHwRate8190Pci((u8)tcb_desc->data_rate);
1227 pTxFwInfo->EnableCPUDur = tcb_desc->bTxEnableFwCalcDur;
1228 pTxFwInfo->Short = QueryIsShort(pTxFwInfo->TxHT, pTxFwInfo->TxRate, tcb_desc);
1230 /* Aggregation related */
1231 if (tcb_desc->bAMPDUEnable) {
1232 pTxFwInfo->AllowAggregation = 1;
1233 pTxFwInfo->RxMF = tcb_desc->ampdu_factor;
1234 pTxFwInfo->RxAMD = tcb_desc->ampdu_density;
1235 } else {
1236 pTxFwInfo->AllowAggregation = 0;
1237 pTxFwInfo->RxMF = 0;
1238 pTxFwInfo->RxAMD = 0;
1241 /* Protection mode related */
1242 pTxFwInfo->RtsEnable = (tcb_desc->bRTSEnable) ? 1 : 0;
1243 pTxFwInfo->CtsEnable = (tcb_desc->bCTSEnable) ? 1 : 0;
1244 pTxFwInfo->RtsSTBC = (tcb_desc->bRTSSTBC) ? 1 : 0;
1245 pTxFwInfo->RtsHT = (tcb_desc->rts_rate&0x80) ? 1 : 0;
1246 pTxFwInfo->RtsRate = MRateToHwRate8190Pci((u8)tcb_desc->rts_rate);
1247 pTxFwInfo->RtsBandwidth = 0;
1248 pTxFwInfo->RtsSubcarrier = tcb_desc->RTSSC;
1249 pTxFwInfo->RtsShort = (pTxFwInfo->RtsHT == 0) ? (tcb_desc->bRTSUseShortPreamble ? 1 : 0) : (tcb_desc->bRTSUseShortGI? 1 : 0);
1251 /* Set Bandwidth and sub-channel settings. */
1252 if (priv->CurrentChannelBW == HT_CHANNEL_WIDTH_20_40) {
1253 if (tcb_desc->bPacketBW) {
1254 pTxFwInfo->TxBandwidth = 1;
1255 /* use duplicated mode */
1256 pTxFwInfo->TxSubCarrier = 0;
1257 } else {
1258 pTxFwInfo->TxBandwidth = 0;
1259 pTxFwInfo->TxSubCarrier = priv->nCur40MhzPrimeSC;
1261 } else {
1262 pTxFwInfo->TxBandwidth = 0;
1263 pTxFwInfo->TxSubCarrier = 0;
1266 spin_lock_irqsave(&priv->irq_th_lock, flags);
1267 ring = &priv->tx_ring[tcb_desc->queue_index];
1268 if (tcb_desc->queue_index != BEACON_QUEUE)
1269 idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
1270 else
1271 idx = 0;
1273 pdesc = &ring->desc[idx];
1274 if ((pdesc->OWN == 1) && (tcb_desc->queue_index != BEACON_QUEUE)) {
1275 RT_TRACE(COMP_ERR, "No more TX desc@%d, ring->idx = %d,idx = %d,%x\n",
1276 tcb_desc->queue_index, ring->idx, idx, skb->len);
1277 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1278 return skb->len;
1281 /* fill tx descriptor */
1282 memset(pdesc, 0, 12);
1284 /*DWORD 0*/
1285 pdesc->LINIP = 0;
1286 pdesc->CmdInit = 1;
1287 pdesc->Offset = sizeof(TX_FWINFO_8190PCI) + 8; /* We must add 8!! */
1288 pdesc->PktSize = (u16)skb->len-sizeof(TX_FWINFO_8190PCI);
1290 /*DWORD 1*/
1291 pdesc->SecCAMID = 0;
1292 pdesc->RATid = tcb_desc->RATRIndex;
1294 pdesc->NoEnc = 1;
1295 pdesc->SecType = 0x0;
1296 if (tcb_desc->bHwSec) {
1297 switch (priv->ieee80211->pairwise_key_type) {
1298 case KEY_TYPE_WEP40:
1299 case KEY_TYPE_WEP104:
1300 pdesc->SecType = 0x1;
1301 pdesc->NoEnc = 0;
1302 break;
1303 case KEY_TYPE_TKIP:
1304 pdesc->SecType = 0x2;
1305 pdesc->NoEnc = 0;
1306 break;
1307 case KEY_TYPE_CCMP:
1308 pdesc->SecType = 0x3;
1309 pdesc->NoEnc = 0;
1310 break;
1311 case KEY_TYPE_NA:
1312 pdesc->SecType = 0x0;
1313 pdesc->NoEnc = 1;
1314 break;
1318 /* Set Packet ID */
1319 pdesc->PktId = 0x0;
1321 pdesc->QueueSelect = MapHwQueueToFirmwareQueue(tcb_desc->queue_index);
1322 pdesc->TxFWInfoSize = sizeof(TX_FWINFO_8190PCI);
1324 pdesc->DISFB = tcb_desc->bTxDisableRateFallBack;
1325 pdesc->USERATE = tcb_desc->bTxUseDriverAssingedRate;
1327 pdesc->FirstSeg = 1;
1328 pdesc->LastSeg = 1;
1329 pdesc->TxBufferSize = skb->len;
1331 pdesc->TxBuffAddr = cpu_to_le32(mapping);
1332 __skb_queue_tail(&ring->queue, skb);
1333 pdesc->OWN = 1;
1334 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1335 priv->ieee80211->dev->trans_start = jiffies;
1336 write_nic_word(priv, TPPoll, 0x01<<tcb_desc->queue_index);
1337 return 0;
1340 static short rtl8192_alloc_rx_desc_ring(struct r8192_priv *priv)
1342 rx_desc_819x_pci *entry = NULL;
1343 int i;
1345 priv->rx_ring = pci_alloc_consistent(priv->pdev,
1346 sizeof(*priv->rx_ring) * priv->rxringcount, &priv->rx_ring_dma);
1348 if (!priv->rx_ring || (unsigned long)priv->rx_ring & 0xFF) {
1349 RT_TRACE(COMP_ERR,"Cannot allocate RX ring\n");
1350 return -ENOMEM;
1353 memset(priv->rx_ring, 0, sizeof(*priv->rx_ring) * priv->rxringcount);
1354 priv->rx_idx = 0;
1356 for (i = 0; i < priv->rxringcount; i++) {
1357 struct sk_buff *skb = dev_alloc_skb(priv->rxbuffersize);
1358 dma_addr_t *mapping;
1359 entry = &priv->rx_ring[i];
1360 if (!skb)
1361 return 0;
1362 priv->rx_buf[i] = skb;
1363 mapping = (dma_addr_t *)skb->cb;
1364 *mapping = pci_map_single(priv->pdev, skb_tail_pointer(skb),
1365 priv->rxbuffersize, PCI_DMA_FROMDEVICE);
1367 entry->BufferAddress = cpu_to_le32(*mapping);
1369 entry->Length = priv->rxbuffersize;
1370 entry->OWN = 1;
1373 entry->EOR = 1;
1374 return 0;
1377 static int rtl8192_alloc_tx_desc_ring(struct r8192_priv *priv,
1378 unsigned int prio, unsigned int entries)
1380 tx_desc_819x_pci *ring;
1381 dma_addr_t dma;
1382 int i;
1384 ring = pci_alloc_consistent(priv->pdev, sizeof(*ring) * entries, &dma);
1385 if (!ring || (unsigned long)ring & 0xFF) {
1386 RT_TRACE(COMP_ERR, "Cannot allocate TX ring (prio = %d)\n", prio);
1387 return -ENOMEM;
1390 memset(ring, 0, sizeof(*ring)*entries);
1391 priv->tx_ring[prio].desc = ring;
1392 priv->tx_ring[prio].dma = dma;
1393 priv->tx_ring[prio].idx = 0;
1394 priv->tx_ring[prio].entries = entries;
1395 skb_queue_head_init(&priv->tx_ring[prio].queue);
1397 for (i = 0; i < entries; i++)
1398 ring[i].NextDescAddress =
1399 cpu_to_le32((u32)dma + ((i + 1) % entries) * sizeof(*ring));
1401 return 0;
1404 static short rtl8192_pci_initdescring(struct r8192_priv *priv)
1406 u32 ret;
1407 int i;
1409 ret = rtl8192_alloc_rx_desc_ring(priv);
1410 if (ret)
1411 return ret;
1413 /* general process for other queue */
1414 for (i = 0; i < MAX_TX_QUEUE_COUNT; i++) {
1415 ret = rtl8192_alloc_tx_desc_ring(priv, i, priv->txringcount);
1416 if (ret)
1417 goto err_free_rings;
1420 return 0;
1422 err_free_rings:
1423 rtl8192_free_rx_ring(priv);
1424 for (i = 0; i < MAX_TX_QUEUE_COUNT; i++)
1425 if (priv->tx_ring[i].desc)
1426 rtl8192_free_tx_ring(priv, i);
1427 return 1;
1430 static void rtl8192_pci_resetdescring(struct r8192_priv *priv)
1432 int i;
1434 /* force the rx_idx to the first one */
1435 if(priv->rx_ring) {
1436 rx_desc_819x_pci *entry = NULL;
1437 for (i = 0; i < priv->rxringcount; i++) {
1438 entry = &priv->rx_ring[i];
1439 entry->OWN = 1;
1441 priv->rx_idx = 0;
1444 /* after reset, release previous pending packet, and force the
1445 * tx idx to the first one */
1446 for (i = 0; i < MAX_TX_QUEUE_COUNT; i++) {
1447 if (priv->tx_ring[i].desc) {
1448 struct rtl8192_tx_ring *ring = &priv->tx_ring[i];
1450 while (skb_queue_len(&ring->queue)) {
1451 tx_desc_819x_pci *entry = &ring->desc[ring->idx];
1452 struct sk_buff *skb = __skb_dequeue(&ring->queue);
1454 pci_unmap_single(priv->pdev, le32_to_cpu(entry->TxBuffAddr),
1455 skb->len, PCI_DMA_TODEVICE);
1456 kfree_skb(skb);
1457 ring->idx = (ring->idx + 1) % ring->entries;
1459 ring->idx = 0;
1464 static void rtl8192_link_change(struct ieee80211_device *ieee)
1466 struct r8192_priv *priv = ieee80211_priv(ieee->dev);
1468 if (ieee->state == IEEE80211_LINKED)
1470 rtl8192_net_update(priv);
1471 rtl8192_update_ratr_table(priv);
1473 //add this as in pure N mode, wep encryption will use software way, but there is no chance to set this as wep will not set group key in wext. WB.2008.07.08
1474 if ((KEY_TYPE_WEP40 == ieee->pairwise_key_type) || (KEY_TYPE_WEP104 == ieee->pairwise_key_type))
1475 EnableHWSecurityConfig8192(priv);
1477 else
1479 write_nic_byte(priv, 0x173, 0);
1482 rtl8192_update_msr(priv);
1484 // 2007/10/16 MH MAC Will update TSF according to all received beacon, so we have
1485 // // To set CBSSID bit when link with any AP or STA.
1486 if (ieee->iw_mode == IW_MODE_INFRA || ieee->iw_mode == IW_MODE_ADHOC)
1488 u32 reg = 0;
1489 reg = read_nic_dword(priv, RCR);
1490 if (priv->ieee80211->state == IEEE80211_LINKED)
1491 priv->ReceiveConfig = reg |= RCR_CBSSID;
1492 else
1493 priv->ReceiveConfig = reg &= ~RCR_CBSSID;
1494 write_nic_dword(priv, RCR, reg);
1499 static const struct ieee80211_qos_parameters def_qos_parameters = {
1500 {3,3,3,3},/* cw_min */
1501 {7,7,7,7},/* cw_max */
1502 {2,2,2,2},/* aifs */
1503 {0,0,0,0},/* flags */
1504 {0,0,0,0} /* tx_op_limit */
1507 static void rtl8192_update_beacon(struct work_struct * work)
1509 struct r8192_priv *priv = container_of(work, struct r8192_priv, update_beacon_wq.work);
1510 struct ieee80211_device* ieee = priv->ieee80211;
1511 struct ieee80211_network* net = &ieee->current_network;
1513 if (ieee->pHTInfo->bCurrentHTSupport)
1514 HTUpdateSelfAndPeerSetting(ieee, net);
1515 ieee->pHTInfo->bCurrentRT2RTLongSlotTime = net->bssht.bdRT2RTLongSlotTime;
1516 rtl8192_update_cap(priv, net->capability);
1520 * background support to run QoS activate functionality
1522 static const int WDCAPARA_ADD[] = {EDCAPARA_BE,EDCAPARA_BK,EDCAPARA_VI,EDCAPARA_VO};
1523 static void rtl8192_qos_activate(struct work_struct * work)
1525 struct r8192_priv *priv = container_of(work, struct r8192_priv, qos_activate);
1526 struct ieee80211_qos_parameters *qos_parameters = &priv->ieee80211->current_network.qos_data.parameters;
1527 u8 mode = priv->ieee80211->current_network.mode;
1528 u8 u1bAIFS;
1529 u32 u4bAcParam;
1530 int i;
1532 mutex_lock(&priv->mutex);
1533 if(priv->ieee80211->state != IEEE80211_LINKED)
1534 goto success;
1535 RT_TRACE(COMP_QOS,"qos active process with associate response received\n");
1536 /* It better set slot time at first */
1537 /* For we just support b/g mode at present, let the slot time at 9/20 selection */
1538 /* update the ac parameter to related registers */
1539 for(i = 0; i < QOS_QUEUE_NUM; i++) {
1540 //Mode G/A: slotTimeTimer = 9; Mode B: 20
1541 u1bAIFS = qos_parameters->aifs[i] * ((mode&(IEEE_G|IEEE_N_24G)) ?9:20) + aSifsTime;
1542 u4bAcParam = ((((u32)(qos_parameters->tx_op_limit[i]))<< AC_PARAM_TXOP_LIMIT_OFFSET)|
1543 (((u32)(qos_parameters->cw_max[i]))<< AC_PARAM_ECW_MAX_OFFSET)|
1544 (((u32)(qos_parameters->cw_min[i]))<< AC_PARAM_ECW_MIN_OFFSET)|
1545 ((u32)u1bAIFS << AC_PARAM_AIFS_OFFSET));
1546 write_nic_dword(priv, WDCAPARA_ADD[i], u4bAcParam);
1549 success:
1550 mutex_unlock(&priv->mutex);
1553 static int rtl8192_qos_handle_probe_response(struct r8192_priv *priv,
1554 int active_network,
1555 struct ieee80211_network *network)
1557 int ret = 0;
1558 u32 size = sizeof(struct ieee80211_qos_parameters);
1560 if(priv->ieee80211->state !=IEEE80211_LINKED)
1561 return ret;
1563 if ((priv->ieee80211->iw_mode != IW_MODE_INFRA))
1564 return ret;
1566 if (network->flags & NETWORK_HAS_QOS_MASK) {
1567 if (active_network &&
1568 (network->flags & NETWORK_HAS_QOS_PARAMETERS))
1569 network->qos_data.active = network->qos_data.supported;
1571 if ((network->qos_data.active == 1) && (active_network == 1) &&
1572 (network->flags & NETWORK_HAS_QOS_PARAMETERS) &&
1573 (network->qos_data.old_param_count !=
1574 network->qos_data.param_count)) {
1575 network->qos_data.old_param_count =
1576 network->qos_data.param_count;
1577 queue_work(priv->priv_wq, &priv->qos_activate);
1578 RT_TRACE (COMP_QOS, "QoS parameters change call "
1579 "qos_activate\n");
1581 } else {
1582 memcpy(&priv->ieee80211->current_network.qos_data.parameters,
1583 &def_qos_parameters, size);
1585 if ((network->qos_data.active == 1) && (active_network == 1)) {
1586 queue_work(priv->priv_wq, &priv->qos_activate);
1587 RT_TRACE(COMP_QOS, "QoS was disabled call qos_activate\n");
1589 network->qos_data.active = 0;
1590 network->qos_data.supported = 0;
1593 return 0;
1596 /* handle manage frame frame beacon and probe response */
1597 static int rtl8192_handle_beacon(struct ieee80211_device *ieee,
1598 struct ieee80211_beacon * beacon,
1599 struct ieee80211_network * network)
1601 struct r8192_priv *priv = ieee80211_priv(ieee->dev);
1603 rtl8192_qos_handle_probe_response(priv,1,network);
1605 queue_delayed_work(priv->priv_wq, &priv->update_beacon_wq, 0);
1606 return 0;
1611 * handling the beaconing responses. if we get different QoS setting
1612 * off the network from the associated setting, adjust the QoS setting
1614 static int rtl8192_qos_association_resp(struct r8192_priv *priv,
1615 struct ieee80211_network *network)
1617 int ret = 0;
1618 unsigned long flags;
1619 u32 size = sizeof(struct ieee80211_qos_parameters);
1620 int set_qos_param = 0;
1622 if ((priv == NULL) || (network == NULL))
1623 return ret;
1625 if (priv->ieee80211->state != IEEE80211_LINKED)
1626 return ret;
1628 if ((priv->ieee80211->iw_mode != IW_MODE_INFRA))
1629 return ret;
1631 spin_lock_irqsave(&priv->ieee80211->lock, flags);
1632 if (network->flags & NETWORK_HAS_QOS_PARAMETERS) {
1633 memcpy(&priv->ieee80211->current_network.qos_data.parameters,
1634 &network->qos_data.parameters,
1635 sizeof(struct ieee80211_qos_parameters));
1636 priv->ieee80211->current_network.qos_data.active = 1;
1637 set_qos_param = 1;
1638 /* update qos parameter for current network */
1639 priv->ieee80211->current_network.qos_data.old_param_count =
1640 priv->ieee80211->current_network.qos_data.param_count;
1641 priv->ieee80211->current_network.qos_data.param_count =
1642 network->qos_data.param_count;
1644 } else {
1645 memcpy(&priv->ieee80211->current_network.qos_data.parameters,
1646 &def_qos_parameters, size);
1647 priv->ieee80211->current_network.qos_data.active = 0;
1648 priv->ieee80211->current_network.qos_data.supported = 0;
1649 set_qos_param = 1;
1652 spin_unlock_irqrestore(&priv->ieee80211->lock, flags);
1654 RT_TRACE(COMP_QOS, "%s: network->flags = %d,%d\n", __FUNCTION__,
1655 network->flags, priv->ieee80211->current_network.qos_data.active);
1656 if (set_qos_param == 1)
1657 queue_work(priv->priv_wq, &priv->qos_activate);
1659 return ret;
1663 static int rtl8192_handle_assoc_response(struct ieee80211_device *ieee,
1664 struct ieee80211_assoc_response_frame *resp,
1665 struct ieee80211_network *network)
1667 struct r8192_priv *priv = ieee80211_priv(ieee->dev);
1668 rtl8192_qos_association_resp(priv, network);
1669 return 0;
1673 /* updateRATRTabel for MCS only. Basic rate is not implemented. */
1674 static void rtl8192_update_ratr_table(struct r8192_priv* priv)
1676 struct ieee80211_device* ieee = priv->ieee80211;
1677 u8* pMcsRate = ieee->dot11HTOperationalRateSet;
1678 u32 ratr_value = 0;
1679 u8 rate_index = 0;
1681 rtl8192_config_rate(priv, (u16*)(&ratr_value));
1682 ratr_value |= (*(u16*)(pMcsRate)) << 12;
1684 switch (ieee->mode)
1686 case IEEE_A:
1687 ratr_value &= 0x00000FF0;
1688 break;
1689 case IEEE_B:
1690 ratr_value &= 0x0000000F;
1691 break;
1692 case IEEE_G:
1693 ratr_value &= 0x00000FF7;
1694 break;
1695 case IEEE_N_24G:
1696 case IEEE_N_5G:
1697 if (ieee->pHTInfo->PeerMimoPs == 0) //MIMO_PS_STATIC
1698 ratr_value &= 0x0007F007;
1699 else{
1700 if (priv->rf_type == RF_1T2R)
1701 ratr_value &= 0x000FF007;
1702 else
1703 ratr_value &= 0x0F81F007;
1705 break;
1706 default:
1707 break;
1709 ratr_value &= 0x0FFFFFFF;
1710 if(ieee->pHTInfo->bCurTxBW40MHz && ieee->pHTInfo->bCurShortGI40MHz){
1711 ratr_value |= 0x80000000;
1712 }else if(!ieee->pHTInfo->bCurTxBW40MHz && ieee->pHTInfo->bCurShortGI20MHz){
1713 ratr_value |= 0x80000000;
1715 write_nic_dword(priv, RATR0+rate_index*4, ratr_value);
1716 write_nic_byte(priv, UFWP, 1);
1719 static bool GetNmodeSupportBySecCfg8190Pci(struct ieee80211_device *ieee)
1721 return !(ieee->rtllib_ap_sec_type &&
1722 (ieee->rtllib_ap_sec_type(ieee)&(SEC_ALG_WEP|SEC_ALG_TKIP)));
1725 static void rtl8192_refresh_supportrate(struct r8192_priv* priv)
1727 struct ieee80211_device* ieee = priv->ieee80211;
1728 //we donot consider set support rate for ABG mode, only HT MCS rate is set here.
1729 if (ieee->mode == WIRELESS_MODE_N_24G || ieee->mode == WIRELESS_MODE_N_5G)
1731 memcpy(ieee->Regdot11HTOperationalRateSet, ieee->RegHTSuppRateSet, 16);
1733 else
1734 memset(ieee->Regdot11HTOperationalRateSet, 0, 16);
1737 static u8 rtl8192_getSupportedWireleeMode(void)
1739 return (WIRELESS_MODE_N_24G|WIRELESS_MODE_G|WIRELESS_MODE_B);
1742 static void rtl8192_SetWirelessMode(struct ieee80211_device *ieee, u8 wireless_mode)
1744 struct r8192_priv *priv = ieee80211_priv(ieee->dev);
1745 u8 bSupportMode = rtl8192_getSupportedWireleeMode();
1747 if ((wireless_mode == WIRELESS_MODE_AUTO) || ((wireless_mode&bSupportMode)==0))
1749 if(bSupportMode & WIRELESS_MODE_N_24G)
1751 wireless_mode = WIRELESS_MODE_N_24G;
1753 else if(bSupportMode & WIRELESS_MODE_N_5G)
1755 wireless_mode = WIRELESS_MODE_N_5G;
1757 else if((bSupportMode & WIRELESS_MODE_A))
1759 wireless_mode = WIRELESS_MODE_A;
1761 else if((bSupportMode & WIRELESS_MODE_G))
1763 wireless_mode = WIRELESS_MODE_G;
1765 else if((bSupportMode & WIRELESS_MODE_B))
1767 wireless_mode = WIRELESS_MODE_B;
1769 else{
1770 RT_TRACE(COMP_ERR, "%s(), No valid wireless mode supported, SupportedWirelessMode(%x)!!!\n", __FUNCTION__,bSupportMode);
1771 wireless_mode = WIRELESS_MODE_B;
1774 priv->ieee80211->mode = wireless_mode;
1776 if ((wireless_mode == WIRELESS_MODE_N_24G) || (wireless_mode == WIRELESS_MODE_N_5G))
1777 priv->ieee80211->pHTInfo->bEnableHT = 1;
1778 else
1779 priv->ieee80211->pHTInfo->bEnableHT = 0;
1780 RT_TRACE(COMP_INIT, "Current Wireless Mode is %x\n", wireless_mode);
1781 rtl8192_refresh_supportrate(priv);
1784 static bool GetHalfNmodeSupportByAPs819xPci(struct ieee80211_device* ieee)
1786 return ieee->bHalfWirelessN24GMode;
1789 static short rtl8192_is_tx_queue_empty(struct ieee80211_device *ieee)
1791 int i=0;
1792 struct r8192_priv *priv = ieee80211_priv(ieee->dev);
1794 for (i=0; i<=MGNT_QUEUE; i++)
1796 if ((i== TXCMD_QUEUE) || (i == HCCA_QUEUE) )
1797 continue;
1798 if (skb_queue_len(&(&priv->tx_ring[i])->queue) > 0){
1799 printk("===>tx queue is not empty:%d, %d\n", i, skb_queue_len(&(&priv->tx_ring[i])->queue));
1800 return 0;
1803 return 1;
1806 static void rtl8192_hw_sleep_down(struct r8192_priv *priv)
1808 MgntActSet_RF_State(priv, eRfSleep, RF_CHANGE_BY_PS);
1811 static void rtl8192_hw_wakeup(struct ieee80211_device *ieee)
1813 struct r8192_priv *priv = ieee80211_priv(ieee->dev);
1814 MgntActSet_RF_State(priv, eRfOn, RF_CHANGE_BY_PS);
1817 static void rtl8192_hw_wakeup_wq (struct work_struct *work)
1819 struct delayed_work *dwork = container_of(work,struct delayed_work,work);
1820 struct ieee80211_device *ieee = container_of(dwork,struct ieee80211_device,hw_wakeup_wq);
1822 rtl8192_hw_wakeup(ieee);
1825 #define MIN_SLEEP_TIME 50
1826 #define MAX_SLEEP_TIME 10000
1827 static void rtl8192_hw_to_sleep(struct ieee80211_device *ieee, u32 th, u32 tl)
1829 struct r8192_priv *priv = ieee80211_priv(ieee->dev);
1830 u32 tmp;
1831 u32 rb = jiffies;
1833 // Writing HW register with 0 equals to disable
1834 // the timer, that is not really what we want
1836 tl -= MSECS(8+16+7);
1838 // If the interval in witch we are requested to sleep is too
1839 // short then give up and remain awake
1840 // when we sleep after send null frame, the timer will be too short to sleep.
1842 if(((tl>=rb)&& (tl-rb) <= MSECS(MIN_SLEEP_TIME))
1843 ||((rb>tl)&& (rb-tl) < MSECS(MIN_SLEEP_TIME))) {
1844 printk("too short to sleep::%x, %x, %lx\n",tl, rb, MSECS(MIN_SLEEP_TIME));
1845 return;
1848 if(((tl > rb) && ((tl-rb) > MSECS(MAX_SLEEP_TIME)))||
1849 ((tl < rb) && (tl>MSECS(69)) && ((rb-tl) > MSECS(MAX_SLEEP_TIME)))||
1850 ((tl<rb)&&(tl<MSECS(69))&&((tl+0xffffffff-rb)>MSECS(MAX_SLEEP_TIME)))) {
1851 printk("========>too long to sleep:%x, %x, %lx\n", tl, rb, MSECS(MAX_SLEEP_TIME));
1852 return;
1855 tmp = (tl>rb)?(tl-rb):(rb-tl);
1856 queue_delayed_work(priv->ieee80211->wq,
1857 &priv->ieee80211->hw_wakeup_wq,tmp);
1859 rtl8192_hw_sleep_down(priv);
1862 static void rtl8192_init_priv_variable(struct r8192_priv *priv)
1864 u8 i;
1865 PRT_POWER_SAVE_CONTROL pPSC = &priv->PowerSaveControl;
1867 // Default Halt the NIC if RF is OFF.
1868 pPSC->RegRfPsLevel |= RT_RF_OFF_LEVL_HALT_NIC;
1869 pPSC->RegRfPsLevel |= RT_RF_OFF_LEVL_CLK_REQ;
1870 pPSC->RegRfPsLevel |= RT_RF_OFF_LEVL_ASPM;
1871 pPSC->RegRfPsLevel |= RT_RF_LPS_LEVEL_ASPM;
1872 pPSC->bLeisurePs = true;
1873 priv->ieee80211->RegMaxLPSAwakeIntvl = 5;
1874 priv->bHwRadioOff = false;
1876 priv->being_init_adapter = false;
1877 priv->txringcount = 64;//32;
1878 priv->rxbuffersize = 9100;//2048;//1024;
1879 priv->rxringcount = MAX_RX_COUNT;//64;
1880 priv->chan = 1; //set to channel 1
1881 priv->RegWirelessMode = WIRELESS_MODE_AUTO;
1882 priv->RegChannelPlan = 0xf;
1883 priv->ieee80211->mode = WIRELESS_MODE_AUTO; //SET AUTO
1884 priv->ieee80211->iw_mode = IW_MODE_INFRA;
1885 priv->ieee80211->ieee_up=0;
1886 priv->retry_rts = DEFAULT_RETRY_RTS;
1887 priv->retry_data = DEFAULT_RETRY_DATA;
1888 priv->ieee80211->rts = DEFAULT_RTS_THRESHOLD;
1889 priv->ieee80211->rate = 110; //11 mbps
1890 priv->ieee80211->short_slot = 1;
1891 priv->promisc = (priv->ieee80211->dev->flags & IFF_PROMISC) ? 1:0;
1892 priv->bcck_in_ch14 = false;
1893 priv->CCKPresentAttentuation = 0;
1894 priv->rfa_txpowertrackingindex = 0;
1895 priv->rfc_txpowertrackingindex = 0;
1896 priv->CckPwEnl = 6;
1897 //added by amy for silent reset
1898 priv->ResetProgress = RESET_TYPE_NORESET;
1899 priv->bForcedSilentReset = 0;
1900 priv->bDisableNormalResetCheck = false;
1901 priv->force_reset = false;
1902 //added by amy for power save
1903 priv->RfOffReason = 0;
1904 priv->bHwRfOffAction = 0;
1905 priv->PowerSaveControl.bInactivePs = true;
1906 priv->PowerSaveControl.bIPSModeBackup = false;
1908 priv->ieee80211->current_network.beacon_interval = DEFAULT_BEACONINTERVAL;
1909 priv->ieee80211->iw_mode = IW_MODE_INFRA;
1910 priv->ieee80211->softmac_features = IEEE_SOFTMAC_SCAN |
1911 IEEE_SOFTMAC_ASSOCIATE | IEEE_SOFTMAC_PROBERQ |
1912 IEEE_SOFTMAC_PROBERS | IEEE_SOFTMAC_TX_QUEUE;/* |
1913 IEEE_SOFTMAC_BEACONS;*///added by amy 080604 //| //IEEE_SOFTMAC_SINGLE_QUEUE;
1915 priv->ieee80211->active_scan = 1;
1916 priv->ieee80211->modulation = IEEE80211_CCK_MODULATION | IEEE80211_OFDM_MODULATION;
1917 priv->ieee80211->host_encrypt = 1;
1918 priv->ieee80211->host_decrypt = 1;
1919 priv->ieee80211->start_send_beacons = rtl8192_start_beacon;
1920 priv->ieee80211->stop_send_beacons = rtl8192_stop_beacon;
1921 priv->ieee80211->softmac_hard_start_xmit = rtl8192_hard_start_xmit;
1922 priv->ieee80211->set_chan = rtl8192_set_chan;
1923 priv->ieee80211->link_change = rtl8192_link_change;
1924 priv->ieee80211->softmac_data_hard_start_xmit = rtl8192_hard_data_xmit;
1925 priv->ieee80211->data_hard_stop = rtl8192_data_hard_stop;
1926 priv->ieee80211->data_hard_resume = rtl8192_data_hard_resume;
1927 priv->ieee80211->init_wmmparam_flag = 0;
1928 priv->ieee80211->fts = DEFAULT_FRAG_THRESHOLD;
1929 priv->ieee80211->check_nic_enough_desc = check_nic_enough_desc;
1930 priv->ieee80211->tx_headroom = sizeof(TX_FWINFO_8190PCI);
1931 priv->ieee80211->qos_support = 1;
1932 priv->ieee80211->SetBWModeHandler = rtl8192_SetBWMode;
1933 priv->ieee80211->handle_assoc_response = rtl8192_handle_assoc_response;
1934 priv->ieee80211->handle_beacon = rtl8192_handle_beacon;
1936 priv->ieee80211->sta_wake_up = rtl8192_hw_wakeup;
1937 priv->ieee80211->enter_sleep_state = rtl8192_hw_to_sleep;
1938 priv->ieee80211->ps_is_queue_empty = rtl8192_is_tx_queue_empty;
1939 priv->ieee80211->GetNmodeSupportBySecCfg = GetNmodeSupportBySecCfg8190Pci;
1940 priv->ieee80211->SetWirelessMode = rtl8192_SetWirelessMode;
1941 priv->ieee80211->GetHalfNmodeSupportByAPsHandler = GetHalfNmodeSupportByAPs819xPci;
1943 priv->ieee80211->InitialGainHandler = InitialGain819xPci;
1945 #ifdef ENABLE_IPS
1946 priv->ieee80211->ieee80211_ips_leave_wq = ieee80211_ips_leave_wq;
1947 priv->ieee80211->ieee80211_ips_leave = ieee80211_ips_leave;
1948 #endif
1949 #ifdef ENABLE_LPS
1950 priv->ieee80211->LeisurePSLeave = LeisurePSLeave;
1951 #endif
1953 priv->ieee80211->SetHwRegHandler = rtl8192e_SetHwReg;
1954 priv->ieee80211->rtllib_ap_sec_type = rtl8192e_ap_sec_type;
1956 priv->ShortRetryLimit = 0x30;
1957 priv->LongRetryLimit = 0x30;
1959 priv->ReceiveConfig = RCR_ADD3 |
1960 RCR_AMF | RCR_ADF | //accept management/data
1961 RCR_AICV | //accept control frame for SW AP needs PS-poll, 2005.07.07, by rcnjko.
1962 RCR_AB | RCR_AM | RCR_APM | //accept BC/MC/UC
1963 RCR_AAP | ((u32)7<<RCR_MXDMA_OFFSET) |
1964 ((u32)7 << RCR_FIFO_OFFSET) | RCR_ONLYERLPKT;
1966 priv->pFirmware = vzalloc(sizeof(rt_firmware));
1968 /* rx related queue */
1969 skb_queue_head_init(&priv->skb_queue);
1971 /* Tx related queue */
1972 for(i = 0; i < MAX_QUEUE_SIZE; i++) {
1973 skb_queue_head_init(&priv->ieee80211->skb_waitQ [i]);
1975 for(i = 0; i < MAX_QUEUE_SIZE; i++) {
1976 skb_queue_head_init(&priv->ieee80211->skb_aggQ [i]);
1978 priv->rf_set_chan = rtl8192_phy_SwChnl;
1981 static void rtl8192_init_priv_lock(struct r8192_priv* priv)
1983 spin_lock_init(&priv->irq_th_lock);
1984 spin_lock_init(&priv->rf_ps_lock);
1985 sema_init(&priv->wx_sem,1);
1986 sema_init(&priv->rf_sem,1);
1987 mutex_init(&priv->mutex);
1990 /* init tasklet and wait_queue here */
1991 #define DRV_NAME "wlan0"
1992 static void rtl8192_init_priv_task(struct r8192_priv *priv)
1994 priv->priv_wq = create_workqueue(DRV_NAME);
1996 #ifdef ENABLE_IPS
1997 INIT_WORK(&priv->ieee80211->ips_leave_wq, IPSLeave_wq);
1998 #endif
2000 INIT_WORK(&priv->reset_wq, rtl8192_restart);
2001 INIT_DELAYED_WORK(&priv->watch_dog_wq, rtl819x_watchdog_wqcallback);
2002 INIT_DELAYED_WORK(&priv->txpower_tracking_wq, dm_txpower_trackingcallback);
2003 INIT_DELAYED_WORK(&priv->rfpath_check_wq, dm_rf_pathcheck_workitemcallback);
2004 INIT_DELAYED_WORK(&priv->update_beacon_wq, rtl8192_update_beacon);
2005 INIT_WORK(&priv->qos_activate, rtl8192_qos_activate);
2006 INIT_DELAYED_WORK(&priv->ieee80211->hw_wakeup_wq, rtl8192_hw_wakeup_wq);
2008 tasklet_init(&priv->irq_rx_tasklet, rtl8192_irq_rx_tasklet,
2009 (unsigned long) priv);
2010 tasklet_init(&priv->irq_tx_tasklet, rtl8192_irq_tx_tasklet,
2011 (unsigned long) priv);
2012 tasklet_init(&priv->irq_prepare_beacon_tasklet, rtl8192_prepare_beacon,
2013 (unsigned long) priv);
2016 static void rtl8192_get_eeprom_size(struct r8192_priv *priv)
2018 u16 curCR = 0;
2019 RT_TRACE(COMP_INIT, "===========>%s()\n", __FUNCTION__);
2020 curCR = read_nic_dword(priv, EPROM_CMD);
2021 RT_TRACE(COMP_INIT, "read from Reg Cmd9346CR(%x):%x\n", EPROM_CMD, curCR);
2022 //whether need I consider BIT5?
2023 priv->epromtype = (curCR & EPROM_CMD_9356SEL) ? EPROM_93c56 : EPROM_93c46;
2024 RT_TRACE(COMP_INIT, "<===========%s(), epromtype:%d\n", __FUNCTION__, priv->epromtype);
2028 * Adapter->EEPROMAddressSize should be set before this function call.
2029 * EEPROM address size can be got through GetEEPROMSize8185()
2031 static void rtl8192_read_eeprom_info(struct r8192_priv *priv)
2033 struct net_device *dev = priv->ieee80211->dev;
2034 u8 tempval;
2035 u8 ICVer8192, ICVer8256;
2036 u16 i,usValue, IC_Version;
2037 u16 EEPROMId;
2038 u8 bMac_Tmp_Addr[6] = {0x00, 0xe0, 0x4c, 0x00, 0x00, 0x01};
2039 RT_TRACE(COMP_INIT, "====> rtl8192_read_eeprom_info\n");
2042 // TODO: I don't know if we need to apply EF function to EEPROM read function
2044 //2 Read EEPROM ID to make sure autoload is success
2045 EEPROMId = eprom_read(priv, 0);
2046 if( EEPROMId != RTL8190_EEPROM_ID )
2048 RT_TRACE(COMP_ERR, "EEPROM ID is invalid:%x, %x\n", EEPROMId, RTL8190_EEPROM_ID);
2049 priv->AutoloadFailFlag=true;
2051 else
2053 priv->AutoloadFailFlag=false;
2057 // Assign Chip Version ID
2059 // Read IC Version && Channel Plan
2060 if(!priv->AutoloadFailFlag)
2062 // VID, PID
2063 priv->eeprom_vid = eprom_read(priv, (EEPROM_VID >> 1));
2064 priv->eeprom_did = eprom_read(priv, (EEPROM_DID >> 1));
2066 usValue = eprom_read(priv, (u16)(EEPROM_Customer_ID>>1)) >> 8 ;
2067 priv->eeprom_CustomerID = (u8)( usValue & 0xff);
2068 usValue = eprom_read(priv, (EEPROM_ICVersion_ChannelPlan>>1));
2069 priv->eeprom_ChannelPlan = usValue&0xff;
2070 IC_Version = ((usValue&0xff00)>>8);
2072 ICVer8192 = (IC_Version&0xf); //bit0~3; 1:A cut, 2:B cut, 3:C cut...
2073 ICVer8256 = ((IC_Version&0xf0)>>4);//bit4~6, bit7 reserved for other RF chip; 1:A cut, 2:B cut, 3:C cut...
2074 RT_TRACE(COMP_INIT, "ICVer8192 = 0x%x\n", ICVer8192);
2075 RT_TRACE(COMP_INIT, "ICVer8256 = 0x%x\n", ICVer8256);
2076 if(ICVer8192 == 0x2) //B-cut
2078 if(ICVer8256 == 0x5) //E-cut
2079 priv->card_8192_version= VERSION_8190_BE;
2082 switch(priv->card_8192_version)
2084 case VERSION_8190_BD:
2085 case VERSION_8190_BE:
2086 break;
2087 default:
2088 priv->card_8192_version = VERSION_8190_BD;
2089 break;
2091 RT_TRACE(COMP_INIT, "\nIC Version = 0x%x\n", priv->card_8192_version);
2093 else
2095 priv->card_8192_version = VERSION_8190_BD;
2096 priv->eeprom_vid = 0;
2097 priv->eeprom_did = 0;
2098 priv->eeprom_CustomerID = 0;
2099 priv->eeprom_ChannelPlan = 0;
2100 RT_TRACE(COMP_INIT, "IC Version = 0x%x\n", 0xff);
2103 RT_TRACE(COMP_INIT, "EEPROM VID = 0x%4x\n", priv->eeprom_vid);
2104 RT_TRACE(COMP_INIT, "EEPROM DID = 0x%4x\n", priv->eeprom_did);
2105 RT_TRACE(COMP_INIT,"EEPROM Customer ID: 0x%2x\n", priv->eeprom_CustomerID);
2107 //2 Read Permanent MAC address
2108 if(!priv->AutoloadFailFlag)
2110 for(i = 0; i < 6; i += 2)
2112 usValue = eprom_read(priv, (u16) ((EEPROM_NODE_ADDRESS_BYTE_0+i)>>1));
2113 *(u16*)(&dev->dev_addr[i]) = usValue;
2115 } else {
2116 // when auto load failed, the last address byte set to be a random one.
2117 // added by david woo.2007/11/7
2118 memcpy(dev->dev_addr, bMac_Tmp_Addr, 6);
2121 RT_TRACE(COMP_INIT, "Permanent Address = %pM\n", dev->dev_addr);
2123 //2 TX Power Check EEPROM Fail or not
2124 if(priv->card_8192_version > VERSION_8190_BD) {
2125 priv->bTXPowerDataReadFromEEPORM = true;
2126 } else {
2127 priv->bTXPowerDataReadFromEEPORM = false;
2130 // 2007/11/15 MH 8190PCI Default=2T4R, 8192PCIE default=1T2R
2131 priv->rf_type = RTL819X_DEFAULT_RF_TYPE;
2133 if(priv->card_8192_version > VERSION_8190_BD)
2135 // Read RF-indication and Tx Power gain index diff of legacy to HT OFDM rate.
2136 if(!priv->AutoloadFailFlag)
2138 tempval = (eprom_read(priv, (EEPROM_RFInd_PowerDiff>>1))) & 0xff;
2139 priv->EEPROMLegacyHTTxPowerDiff = tempval & 0xf; // bit[3:0]
2141 if (tempval&0x80) //RF-indication, bit[7]
2142 priv->rf_type = RF_1T2R;
2143 else
2144 priv->rf_type = RF_2T4R;
2146 else
2148 priv->EEPROMLegacyHTTxPowerDiff = EEPROM_Default_LegacyHTTxPowerDiff;
2150 RT_TRACE(COMP_INIT, "EEPROMLegacyHTTxPowerDiff = %d\n",
2151 priv->EEPROMLegacyHTTxPowerDiff);
2153 // Read ThermalMeter from EEPROM
2154 if(!priv->AutoloadFailFlag)
2156 priv->EEPROMThermalMeter = (u8)(((eprom_read(priv, (EEPROM_ThermalMeter>>1))) & 0xff00)>>8);
2158 else
2160 priv->EEPROMThermalMeter = EEPROM_Default_ThermalMeter;
2162 RT_TRACE(COMP_INIT, "ThermalMeter = %d\n", priv->EEPROMThermalMeter);
2163 //vivi, for tx power track
2164 priv->TSSI_13dBm = priv->EEPROMThermalMeter *100;
2166 if(priv->epromtype == EPROM_93c46)
2168 // Read antenna tx power offset of B/C/D to A and CrystalCap from EEPROM
2169 if(!priv->AutoloadFailFlag)
2171 usValue = eprom_read(priv, (EEPROM_TxPwDiff_CrystalCap>>1));
2172 priv->EEPROMAntPwDiff = (usValue&0x0fff);
2173 priv->EEPROMCrystalCap = (u8)((usValue&0xf000)>>12);
2175 else
2177 priv->EEPROMAntPwDiff = EEPROM_Default_AntTxPowerDiff;
2178 priv->EEPROMCrystalCap = EEPROM_Default_TxPwDiff_CrystalCap;
2180 RT_TRACE(COMP_INIT, "EEPROMAntPwDiff = %d\n", priv->EEPROMAntPwDiff);
2181 RT_TRACE(COMP_INIT, "EEPROMCrystalCap = %d\n", priv->EEPROMCrystalCap);
2184 // Get per-channel Tx Power Level
2186 for(i=0; i<14; i+=2)
2188 if(!priv->AutoloadFailFlag)
2190 usValue = eprom_read(priv, (u16) ((EEPROM_TxPwIndex_CCK+i)>>1) );
2192 else
2194 usValue = EEPROM_Default_TxPower;
2196 *((u16*)(&priv->EEPROMTxPowerLevelCCK[i])) = usValue;
2197 RT_TRACE(COMP_INIT,"CCK Tx Power Level, Index %d = 0x%02x\n", i, priv->EEPROMTxPowerLevelCCK[i]);
2198 RT_TRACE(COMP_INIT, "CCK Tx Power Level, Index %d = 0x%02x\n", i+1, priv->EEPROMTxPowerLevelCCK[i+1]);
2200 for(i=0; i<14; i+=2)
2202 if(!priv->AutoloadFailFlag)
2204 usValue = eprom_read(priv, (u16) ((EEPROM_TxPwIndex_OFDM_24G+i)>>1) );
2206 else
2208 usValue = EEPROM_Default_TxPower;
2210 *((u16*)(&priv->EEPROMTxPowerLevelOFDM24G[i])) = usValue;
2211 RT_TRACE(COMP_INIT, "OFDM 2.4G Tx Power Level, Index %d = 0x%02x\n", i, priv->EEPROMTxPowerLevelOFDM24G[i]);
2212 RT_TRACE(COMP_INIT, "OFDM 2.4G Tx Power Level, Index %d = 0x%02x\n", i+1, priv->EEPROMTxPowerLevelOFDM24G[i+1]);
2217 // Update HAL variables.
2219 if(priv->epromtype == EPROM_93c46)
2221 for(i=0; i<14; i++)
2223 priv->TxPowerLevelCCK[i] = priv->EEPROMTxPowerLevelCCK[i];
2224 priv->TxPowerLevelOFDM24G[i] = priv->EEPROMTxPowerLevelOFDM24G[i];
2226 priv->LegacyHTTxPowerDiff = priv->EEPROMLegacyHTTxPowerDiff;
2227 // Antenna B gain offset to antenna A, bit0~3
2228 priv->AntennaTxPwDiff[0] = (priv->EEPROMAntPwDiff & 0xf);
2229 // Antenna C gain offset to antenna A, bit4~7
2230 priv->AntennaTxPwDiff[1] = ((priv->EEPROMAntPwDiff & 0xf0)>>4);
2231 // Antenna D gain offset to antenna A, bit8~11
2232 priv->AntennaTxPwDiff[2] = ((priv->EEPROMAntPwDiff & 0xf00)>>8);
2233 // CrystalCap, bit12~15
2234 priv->CrystalCap = priv->EEPROMCrystalCap;
2235 // ThermalMeter, bit0~3 for RFIC1, bit4~7 for RFIC2
2236 priv->ThermalMeter[0] = (priv->EEPROMThermalMeter & 0xf);
2237 priv->ThermalMeter[1] = ((priv->EEPROMThermalMeter & 0xf0)>>4);
2239 else if(priv->epromtype == EPROM_93c56)
2241 for(i=0; i<3; i++) // channel 1~3 use the same Tx Power Level.
2243 priv->TxPowerLevelCCK_A[i] = priv->EEPROMRfACCKChnl1TxPwLevel[0];
2244 priv->TxPowerLevelOFDM24G_A[i] = priv->EEPROMRfAOfdmChnlTxPwLevel[0];
2245 priv->TxPowerLevelCCK_C[i] = priv->EEPROMRfCCCKChnl1TxPwLevel[0];
2246 priv->TxPowerLevelOFDM24G_C[i] = priv->EEPROMRfCOfdmChnlTxPwLevel[0];
2248 for(i=3; i<9; i++) // channel 4~9 use the same Tx Power Level
2250 priv->TxPowerLevelCCK_A[i] = priv->EEPROMRfACCKChnl1TxPwLevel[1];
2251 priv->TxPowerLevelOFDM24G_A[i] = priv->EEPROMRfAOfdmChnlTxPwLevel[1];
2252 priv->TxPowerLevelCCK_C[i] = priv->EEPROMRfCCCKChnl1TxPwLevel[1];
2253 priv->TxPowerLevelOFDM24G_C[i] = priv->EEPROMRfCOfdmChnlTxPwLevel[1];
2255 for(i=9; i<14; i++) // channel 10~14 use the same Tx Power Level
2257 priv->TxPowerLevelCCK_A[i] = priv->EEPROMRfACCKChnl1TxPwLevel[2];
2258 priv->TxPowerLevelOFDM24G_A[i] = priv->EEPROMRfAOfdmChnlTxPwLevel[2];
2259 priv->TxPowerLevelCCK_C[i] = priv->EEPROMRfCCCKChnl1TxPwLevel[2];
2260 priv->TxPowerLevelOFDM24G_C[i] = priv->EEPROMRfCOfdmChnlTxPwLevel[2];
2262 for(i=0; i<14; i++)
2263 RT_TRACE(COMP_INIT, "priv->TxPowerLevelCCK_A[%d] = 0x%x\n", i, priv->TxPowerLevelCCK_A[i]);
2264 for(i=0; i<14; i++)
2265 RT_TRACE(COMP_INIT,"priv->TxPowerLevelOFDM24G_A[%d] = 0x%x\n", i, priv->TxPowerLevelOFDM24G_A[i]);
2266 for(i=0; i<14; i++)
2267 RT_TRACE(COMP_INIT, "priv->TxPowerLevelCCK_C[%d] = 0x%x\n", i, priv->TxPowerLevelCCK_C[i]);
2268 for(i=0; i<14; i++)
2269 RT_TRACE(COMP_INIT, "priv->TxPowerLevelOFDM24G_C[%d] = 0x%x\n", i, priv->TxPowerLevelOFDM24G_C[i]);
2270 priv->LegacyHTTxPowerDiff = priv->EEPROMLegacyHTTxPowerDiff;
2271 priv->AntennaTxPwDiff[0] = 0;
2272 priv->AntennaTxPwDiff[1] = 0;
2273 priv->AntennaTxPwDiff[2] = 0;
2274 priv->CrystalCap = priv->EEPROMCrystalCap;
2275 // ThermalMeter, bit0~3 for RFIC1, bit4~7 for RFIC2
2276 priv->ThermalMeter[0] = (priv->EEPROMThermalMeter & 0xf);
2277 priv->ThermalMeter[1] = ((priv->EEPROMThermalMeter & 0xf0)>>4);
2281 if(priv->rf_type == RF_1T2R)
2283 RT_TRACE(COMP_INIT, "1T2R config\n");
2285 else if (priv->rf_type == RF_2T4R)
2287 RT_TRACE(COMP_INIT, "2T4R config\n");
2290 // 2008/01/16 MH We can only know RF type in the function. So we have to init
2291 // DIG RATR table again.
2292 init_rate_adaptive(priv);
2294 //1 Make a copy for following variables and we can change them if we want
2296 if(priv->RegChannelPlan == 0xf)
2298 priv->ChannelPlan = priv->eeprom_ChannelPlan;
2300 else
2302 priv->ChannelPlan = priv->RegChannelPlan;
2306 // Used PID and DID to Set CustomerID
2308 if( priv->eeprom_vid == 0x1186 && priv->eeprom_did == 0x3304 )
2310 priv->CustomerID = RT_CID_DLINK;
2313 switch(priv->eeprom_CustomerID)
2315 case EEPROM_CID_DEFAULT:
2316 priv->CustomerID = RT_CID_DEFAULT;
2317 break;
2318 case EEPROM_CID_CAMEO:
2319 priv->CustomerID = RT_CID_819x_CAMEO;
2320 break;
2321 case EEPROM_CID_RUNTOP:
2322 priv->CustomerID = RT_CID_819x_RUNTOP;
2323 break;
2324 case EEPROM_CID_NetCore:
2325 priv->CustomerID = RT_CID_819x_Netcore;
2326 break;
2327 case EEPROM_CID_TOSHIBA: // Merge by Jacken, 2008/01/31
2328 priv->CustomerID = RT_CID_TOSHIBA;
2329 if(priv->eeprom_ChannelPlan&0x80)
2330 priv->ChannelPlan = priv->eeprom_ChannelPlan&0x7f;
2331 else
2332 priv->ChannelPlan = 0x0;
2333 RT_TRACE(COMP_INIT, "Toshiba ChannelPlan = 0x%x\n",
2334 priv->ChannelPlan);
2335 break;
2336 case EEPROM_CID_Nettronix:
2337 priv->CustomerID = RT_CID_Nettronix;
2338 break;
2339 case EEPROM_CID_Pronet:
2340 priv->CustomerID = RT_CID_PRONET;
2341 break;
2342 case EEPROM_CID_DLINK:
2343 priv->CustomerID = RT_CID_DLINK;
2344 break;
2346 case EEPROM_CID_WHQL:
2347 break;
2348 default:
2349 // value from RegCustomerID
2350 break;
2353 //Avoid the channel plan array overflow, by Bruce, 2007-08-27.
2354 if(priv->ChannelPlan > CHANNEL_PLAN_LEN - 1)
2355 priv->ChannelPlan = 0; //FCC
2357 if( priv->eeprom_vid == 0x1186 && priv->eeprom_did == 0x3304)
2358 priv->ieee80211->bSupportRemoteWakeUp = true;
2359 else
2360 priv->ieee80211->bSupportRemoteWakeUp = false;
2363 RT_TRACE(COMP_INIT, "RegChannelPlan(%d)\n", priv->RegChannelPlan);
2364 RT_TRACE(COMP_INIT, "ChannelPlan = %d\n", priv->ChannelPlan);
2365 RT_TRACE(COMP_TRACE, "<==== ReadAdapterInfo\n");
2369 static short rtl8192_get_channel_map(struct r8192_priv *priv)
2371 #ifdef ENABLE_DOT11D
2372 if(priv->ChannelPlan> COUNTRY_CODE_GLOBAL_DOMAIN){
2373 printk("rtl8180_init:Error channel plan! Set to default.\n");
2374 priv->ChannelPlan= 0;
2376 RT_TRACE(COMP_INIT, "Channel plan is %d\n",priv->ChannelPlan);
2378 rtl819x_set_channel_map(priv->ChannelPlan, priv);
2379 #else
2380 int ch,i;
2381 //Set Default Channel Plan
2382 if(!channels){
2383 DMESG("No channels, aborting");
2384 return -1;
2386 ch=channels;
2387 priv->ChannelPlan= 0;//hikaru
2388 // set channels 1..14 allowed in given locale
2389 for (i=1; i<=14; i++) {
2390 (priv->ieee80211->channel_map)[i] = (u8)(ch & 0x01);
2391 ch >>= 1;
2393 #endif
2394 return 0;
2397 static short rtl8192_init(struct r8192_priv *priv)
2399 struct net_device *dev = priv->ieee80211->dev;
2401 memset(&(priv->stats),0,sizeof(struct Stats));
2402 rtl8192_init_priv_variable(priv);
2403 rtl8192_init_priv_lock(priv);
2404 rtl8192_init_priv_task(priv);
2405 rtl8192_get_eeprom_size(priv);
2406 rtl8192_read_eeprom_info(priv);
2407 rtl8192_get_channel_map(priv);
2408 init_hal_dm(priv);
2409 init_timer(&priv->watch_dog_timer);
2410 priv->watch_dog_timer.data = (unsigned long)priv;
2411 priv->watch_dog_timer.function = watch_dog_timer_callback;
2412 if (request_irq(dev->irq, rtl8192_interrupt, IRQF_SHARED, dev->name, priv)) {
2413 printk("Error allocating IRQ %d",dev->irq);
2414 return -1;
2415 }else{
2416 priv->irq=dev->irq;
2417 printk("IRQ %d",dev->irq);
2419 if (rtl8192_pci_initdescring(priv) != 0){
2420 printk("Endopoints initialization failed");
2421 return -1;
2424 return 0;
2428 * Actually only set RRSR, RATR and BW_OPMODE registers
2429 * not to do all the hw config as its name says
2430 * This part need to modified according to the rate set we filtered
2432 static void rtl8192_hwconfig(struct r8192_priv *priv)
2434 u32 regRATR = 0, regRRSR = 0;
2435 u8 regBwOpMode = 0, regTmp = 0;
2437 // Set RRSR, RATR, and BW_OPMODE registers
2439 switch (priv->ieee80211->mode)
2441 case WIRELESS_MODE_B:
2442 regBwOpMode = BW_OPMODE_20MHZ;
2443 regRATR = RATE_ALL_CCK;
2444 regRRSR = RATE_ALL_CCK;
2445 break;
2446 case WIRELESS_MODE_A:
2447 regBwOpMode = BW_OPMODE_5G |BW_OPMODE_20MHZ;
2448 regRATR = RATE_ALL_OFDM_AG;
2449 regRRSR = RATE_ALL_OFDM_AG;
2450 break;
2451 case WIRELESS_MODE_G:
2452 regBwOpMode = BW_OPMODE_20MHZ;
2453 regRATR = RATE_ALL_CCK | RATE_ALL_OFDM_AG;
2454 regRRSR = RATE_ALL_CCK | RATE_ALL_OFDM_AG;
2455 break;
2456 case WIRELESS_MODE_AUTO:
2457 case WIRELESS_MODE_N_24G:
2458 // It support CCK rate by default.
2459 // CCK rate will be filtered out only when associated AP does not support it.
2460 regBwOpMode = BW_OPMODE_20MHZ;
2461 regRATR = RATE_ALL_CCK | RATE_ALL_OFDM_AG | RATE_ALL_OFDM_1SS | RATE_ALL_OFDM_2SS;
2462 regRRSR = RATE_ALL_CCK | RATE_ALL_OFDM_AG;
2463 break;
2464 case WIRELESS_MODE_N_5G:
2465 regBwOpMode = BW_OPMODE_5G;
2466 regRATR = RATE_ALL_OFDM_AG | RATE_ALL_OFDM_1SS | RATE_ALL_OFDM_2SS;
2467 regRRSR = RATE_ALL_OFDM_AG;
2468 break;
2471 write_nic_byte(priv, BW_OPMODE, regBwOpMode);
2473 u32 ratr_value = 0;
2474 ratr_value = regRATR;
2475 if (priv->rf_type == RF_1T2R)
2477 ratr_value &= ~(RATE_ALL_OFDM_2SS);
2479 write_nic_dword(priv, RATR0, ratr_value);
2480 write_nic_byte(priv, UFWP, 1);
2482 regTmp = read_nic_byte(priv, 0x313);
2483 regRRSR = ((regTmp) << 24) | (regRRSR & 0x00ffffff);
2484 write_nic_dword(priv, RRSR, regRRSR);
2487 // Set Retry Limit here
2489 write_nic_word(priv, RETRY_LIMIT,
2490 priv->ShortRetryLimit << RETRY_LIMIT_SHORT_SHIFT |
2491 priv->LongRetryLimit << RETRY_LIMIT_LONG_SHIFT);
2492 // Set Contention Window here
2494 // Set Tx AGC
2496 // Set Tx Antenna including Feedback control
2498 // Set Auto Rate fallback control
2504 static RT_STATUS rtl8192_adapter_start(struct r8192_priv *priv)
2506 struct net_device *dev = priv->ieee80211->dev;
2507 u32 ulRegRead;
2508 RT_STATUS rtStatus = RT_STATUS_SUCCESS;
2509 u8 tmpvalue;
2510 u8 ICVersion,SwitchingRegulatorOutput;
2511 bool bfirmwareok = true;
2512 u32 tmpRegA, tmpRegC, TempCCk;
2513 int i =0;
2515 RT_TRACE(COMP_INIT, "====>%s()\n", __FUNCTION__);
2516 priv->being_init_adapter = true;
2517 rtl8192_pci_resetdescring(priv);
2518 // 2007/11/02 MH Before initalizing RF. We can not use FW to do RF-R/W.
2519 priv->Rf_Mode = RF_OP_By_SW_3wire;
2521 //dPLL on
2522 if(priv->ResetProgress == RESET_TYPE_NORESET)
2524 write_nic_byte(priv, ANAPAR, 0x37);
2525 // Accordign to designer's explain, LBUS active will never > 10ms. We delay 10ms
2526 // Joseph increae the time to prevent firmware download fail
2527 mdelay(500);
2530 //PlatformSleepUs(10000);
2531 // For any kind of InitializeAdapter process, we shall use system now!!
2532 priv->pFirmware->firmware_status = FW_STATUS_0_INIT;
2535 //3 //Config CPUReset Register
2536 //3//
2537 //3 Firmware Reset Or Not
2538 ulRegRead = read_nic_dword(priv, CPU_GEN);
2539 if(priv->pFirmware->firmware_status == FW_STATUS_0_INIT)
2540 { //called from MPInitialized. do nothing
2541 ulRegRead |= CPU_GEN_SYSTEM_RESET;
2542 }else if(priv->pFirmware->firmware_status == FW_STATUS_5_READY)
2543 ulRegRead |= CPU_GEN_FIRMWARE_RESET; // Called from MPReset
2544 else
2545 RT_TRACE(COMP_ERR, "ERROR in %s(): undefined firmware state(%d)\n", __FUNCTION__, priv->pFirmware->firmware_status);
2547 write_nic_dword(priv, CPU_GEN, ulRegRead);
2549 //3//
2550 //3 //Fix the issue of E-cut high temperature issue
2551 //3//
2552 // TODO: E cut only
2553 ICVersion = read_nic_byte(priv, IC_VERRSION);
2554 if(ICVersion >= 0x4) //E-cut only
2556 // HW SD suggest that we should not wirte this register too often, so driver
2557 // should readback this register. This register will be modified only when
2558 // power on reset
2559 SwitchingRegulatorOutput = read_nic_byte(priv, SWREGULATOR);
2560 if(SwitchingRegulatorOutput != 0xb8)
2562 write_nic_byte(priv, SWREGULATOR, 0xa8);
2563 mdelay(1);
2564 write_nic_byte(priv, SWREGULATOR, 0xb8);
2568 //3//
2569 //3// Initialize BB before MAC
2570 //3//
2571 RT_TRACE(COMP_INIT, "BB Config Start!\n");
2572 rtStatus = rtl8192_BBConfig(priv);
2573 if(rtStatus != RT_STATUS_SUCCESS)
2575 RT_TRACE(COMP_ERR, "BB Config failed\n");
2576 return rtStatus;
2578 RT_TRACE(COMP_INIT,"BB Config Finished!\n");
2580 //3//Set Loopback mode or Normal mode
2581 //3//
2582 //2006.12.13 by emily. Note!We should not merge these two CPU_GEN register writings
2583 // because setting of System_Reset bit reset MAC to default transmission mode.
2584 //Loopback mode or not
2585 priv->LoopbackMode = RTL819X_NO_LOOPBACK;
2586 if(priv->ResetProgress == RESET_TYPE_NORESET)
2588 ulRegRead = read_nic_dword(priv, CPU_GEN);
2589 if(priv->LoopbackMode == RTL819X_NO_LOOPBACK)
2591 ulRegRead = ((ulRegRead & CPU_GEN_NO_LOOPBACK_MSK) | CPU_GEN_NO_LOOPBACK_SET);
2593 else if (priv->LoopbackMode == RTL819X_MAC_LOOPBACK )
2595 ulRegRead |= CPU_CCK_LOOPBACK;
2597 else
2599 RT_TRACE(COMP_ERR,"Serious error: wrong loopback mode setting\n");
2602 //2008.06.03, for WOL
2603 //ulRegRead &= (~(CPU_GEN_GPIO_UART));
2604 write_nic_dword(priv, CPU_GEN, ulRegRead);
2606 // 2006.11.29. After reset cpu, we sholud wait for a second, otherwise, it may fail to write registers. Emily
2607 udelay(500);
2609 //3Set Hardware(Do nothing now)
2610 rtl8192_hwconfig(priv);
2611 //2=======================================================
2612 // Common Setting for all of the FPGA platform. (part 1)
2613 //2=======================================================
2614 // If there is changes, please make sure it applies to all of the FPGA version
2615 //3 Turn on Tx/Rx
2616 write_nic_byte(priv, CMDR, CR_RE|CR_TE);
2618 //2Set Tx dma burst
2619 write_nic_byte(priv, PCIF, ((MXDMA2_NoLimit<<MXDMA2_RX_SHIFT) |
2620 (MXDMA2_NoLimit<<MXDMA2_TX_SHIFT) ));
2622 //set IDR0 here
2623 write_nic_dword(priv, MAC0, ((u32*)dev->dev_addr)[0]);
2624 write_nic_word(priv, MAC4, ((u16*)(dev->dev_addr + 4))[0]);
2625 //set RCR
2626 write_nic_dword(priv, RCR, priv->ReceiveConfig);
2628 //3 Initialize Number of Reserved Pages in Firmware Queue
2629 write_nic_dword(priv, RQPN1, NUM_OF_PAGE_IN_FW_QUEUE_BK << RSVD_FW_QUEUE_PAGE_BK_SHIFT |
2630 NUM_OF_PAGE_IN_FW_QUEUE_BE << RSVD_FW_QUEUE_PAGE_BE_SHIFT |
2631 NUM_OF_PAGE_IN_FW_QUEUE_VI << RSVD_FW_QUEUE_PAGE_VI_SHIFT |
2632 NUM_OF_PAGE_IN_FW_QUEUE_VO <<RSVD_FW_QUEUE_PAGE_VO_SHIFT);
2633 write_nic_dword(priv, RQPN2, NUM_OF_PAGE_IN_FW_QUEUE_MGNT << RSVD_FW_QUEUE_PAGE_MGNT_SHIFT);
2634 write_nic_dword(priv, RQPN3, APPLIED_RESERVED_QUEUE_IN_FW|
2635 NUM_OF_PAGE_IN_FW_QUEUE_BCN<<RSVD_FW_QUEUE_PAGE_BCN_SHIFT|
2636 NUM_OF_PAGE_IN_FW_QUEUE_PUB<<RSVD_FW_QUEUE_PAGE_PUB_SHIFT);
2638 rtl8192_tx_enable(priv);
2639 rtl8192_rx_enable(priv);
2640 //3Set Response Rate Setting Register
2641 // CCK rate is supported by default.
2642 // CCK rate will be filtered out only when associated AP does not support it.
2643 ulRegRead = (0xFFF00000 & read_nic_dword(priv, RRSR)) | RATE_ALL_OFDM_AG | RATE_ALL_CCK;
2644 write_nic_dword(priv, RRSR, ulRegRead);
2645 write_nic_dword(priv, RATR0+4*7, (RATE_ALL_OFDM_AG | RATE_ALL_CCK));
2647 //2Set AckTimeout
2648 // TODO: (it value is only for FPGA version). need to be changed!!2006.12.18, by Emily
2649 write_nic_byte(priv, ACK_TIMEOUT, 0x30);
2651 if(priv->ResetProgress == RESET_TYPE_NORESET)
2652 rtl8192_SetWirelessMode(priv->ieee80211, priv->ieee80211->mode);
2653 //-----------------------------------------------------------------------------
2654 // Set up security related. 070106, by rcnjko:
2655 // 1. Clear all H/W keys.
2656 // 2. Enable H/W encryption/decryption.
2657 //-----------------------------------------------------------------------------
2658 CamResetAllEntry(priv);
2660 u8 SECR_value = 0x0;
2661 SECR_value |= SCR_TxEncEnable;
2662 SECR_value |= SCR_RxDecEnable;
2663 SECR_value |= SCR_NoSKMC;
2664 write_nic_byte(priv, SECR, SECR_value);
2666 //3Beacon related
2667 write_nic_word(priv, ATIMWND, 2);
2668 write_nic_word(priv, BCN_INTERVAL, 100);
2669 for (i=0; i<QOS_QUEUE_NUM; i++)
2670 write_nic_dword(priv, WDCAPARA_ADD[i], 0x005e4332);
2672 // Switching regulator controller: This is set temporarily.
2673 // It's not sure if this can be removed in the future.
2674 // PJ advised to leave it by default.
2676 write_nic_byte(priv, 0xbe, 0xc0);
2678 //2=======================================================
2679 // Set PHY related configuration defined in MAC register bank
2680 //2=======================================================
2681 rtl8192_phy_configmac(priv);
2683 if (priv->card_8192_version > (u8) VERSION_8190_BD) {
2684 rtl8192_phy_getTxPower(priv);
2685 rtl8192_phy_setTxPower(priv, priv->chan);
2688 //if D or C cut
2689 tmpvalue = read_nic_byte(priv, IC_VERRSION);
2690 priv->IC_Cut = tmpvalue;
2691 RT_TRACE(COMP_INIT, "priv->IC_Cut = 0x%x\n", priv->IC_Cut);
2692 if(priv->IC_Cut >= IC_VersionCut_D)
2694 //pHalData->bDcut = TRUE;
2695 if(priv->IC_Cut == IC_VersionCut_D)
2696 RT_TRACE(COMP_INIT, "D-cut\n");
2697 if(priv->IC_Cut == IC_VersionCut_E)
2699 RT_TRACE(COMP_INIT, "E-cut\n");
2700 // HW SD suggest that we should not wirte this register too often, so driver
2701 // should readback this register. This register will be modified only when
2702 // power on reset
2705 else
2707 //pHalData->bDcut = FALSE;
2708 RT_TRACE(COMP_INIT, "Before C-cut\n");
2711 //Firmware download
2712 RT_TRACE(COMP_INIT, "Load Firmware!\n");
2713 bfirmwareok = init_firmware(priv);
2714 if(bfirmwareok != true) {
2715 rtStatus = RT_STATUS_FAILURE;
2716 return rtStatus;
2718 RT_TRACE(COMP_INIT, "Load Firmware finished!\n");
2720 //RF config
2721 if(priv->ResetProgress == RESET_TYPE_NORESET)
2723 RT_TRACE(COMP_INIT, "RF Config Started!\n");
2724 rtStatus = rtl8192_phy_RFConfig(priv);
2725 if(rtStatus != RT_STATUS_SUCCESS)
2727 RT_TRACE(COMP_ERR, "RF Config failed\n");
2728 return rtStatus;
2730 RT_TRACE(COMP_INIT, "RF Config Finished!\n");
2732 rtl8192_phy_updateInitGain(priv);
2734 /*---- Set CCK and OFDM Block "ON"----*/
2735 rtl8192_setBBreg(priv, rFPGA0_RFMOD, bCCKEn, 0x1);
2736 rtl8192_setBBreg(priv, rFPGA0_RFMOD, bOFDMEn, 0x1);
2738 //Enable Led
2739 write_nic_byte(priv, 0x87, 0x0);
2741 //2=======================================================
2742 // RF Power Save
2743 //2=======================================================
2744 #ifdef ENABLE_IPS
2747 if(priv->RfOffReason > RF_CHANGE_BY_PS)
2748 { // H/W or S/W RF OFF before sleep.
2749 RT_TRACE((COMP_INIT|COMP_RF|COMP_POWER), "%s(): Turn off RF for RfOffReason(%d)\n", __FUNCTION__,priv->RfOffReason);
2750 MgntActSet_RF_State(priv, eRfOff, priv->RfOffReason);
2752 else if(priv->RfOffReason >= RF_CHANGE_BY_IPS)
2753 { // H/W or S/W RF OFF before sleep.
2754 RT_TRACE((COMP_INIT|COMP_RF|COMP_POWER), "%s(): Turn off RF for RfOffReason(%d)\n", __FUNCTION__, priv->RfOffReason);
2755 MgntActSet_RF_State(priv, eRfOff, priv->RfOffReason);
2757 else
2759 RT_TRACE((COMP_INIT|COMP_RF|COMP_POWER), "%s(): RF-ON \n",__FUNCTION__);
2760 priv->eRFPowerState = eRfOn;
2761 priv->RfOffReason = 0;
2764 #endif
2765 // We can force firmware to do RF-R/W
2766 if(priv->ieee80211->FwRWRF)
2767 priv->Rf_Mode = RF_OP_By_FW;
2768 else
2769 priv->Rf_Mode = RF_OP_By_SW_3wire;
2771 if(priv->ResetProgress == RESET_TYPE_NORESET)
2773 dm_initialize_txpower_tracking(priv);
2775 if(priv->IC_Cut >= IC_VersionCut_D)
2777 tmpRegA = rtl8192_QueryBBReg(priv, rOFDM0_XATxIQImbalance, bMaskDWord);
2778 tmpRegC = rtl8192_QueryBBReg(priv, rOFDM0_XCTxIQImbalance, bMaskDWord);
2779 for(i = 0; i<TxBBGainTableLength; i++)
2781 if(tmpRegA == priv->txbbgain_table[i].txbbgain_value)
2783 priv->rfa_txpowertrackingindex= (u8)i;
2784 priv->rfa_txpowertrackingindex_real= (u8)i;
2785 priv->rfa_txpowertracking_default = priv->rfa_txpowertrackingindex;
2786 break;
2790 TempCCk = rtl8192_QueryBBReg(priv, rCCK0_TxFilter1, bMaskByte2);
2792 for(i=0 ; i<CCKTxBBGainTableLength ; i++)
2794 if(TempCCk == priv->cck_txbbgain_table[i].ccktxbb_valuearray[0])
2796 priv->CCKPresentAttentuation_20Mdefault =(u8) i;
2797 break;
2800 priv->CCKPresentAttentuation_40Mdefault = 0;
2801 priv->CCKPresentAttentuation_difference = 0;
2802 priv->CCKPresentAttentuation = priv->CCKPresentAttentuation_20Mdefault;
2803 RT_TRACE(COMP_POWER_TRACKING, "priv->rfa_txpowertrackingindex_initial = %d\n", priv->rfa_txpowertrackingindex);
2804 RT_TRACE(COMP_POWER_TRACKING, "priv->rfa_txpowertrackingindex_real__initial = %d\n", priv->rfa_txpowertrackingindex_real);
2805 RT_TRACE(COMP_POWER_TRACKING, "priv->CCKPresentAttentuation_difference_initial = %d\n", priv->CCKPresentAttentuation_difference);
2806 RT_TRACE(COMP_POWER_TRACKING, "priv->CCKPresentAttentuation_initial = %d\n", priv->CCKPresentAttentuation);
2807 priv->btxpower_tracking = FALSE;//TEMPLY DISABLE
2811 rtl8192_irq_enable(priv);
2812 priv->being_init_adapter = false;
2813 return rtStatus;
2817 static void rtl8192_prepare_beacon(unsigned long arg)
2819 struct r8192_priv *priv = (struct r8192_priv*) arg;
2820 struct sk_buff *skb;
2821 cb_desc *tcb_desc;
2823 skb = ieee80211_get_beacon(priv->ieee80211);
2824 tcb_desc = (cb_desc *)(skb->cb + 8);
2825 /* prepare misc info for the beacon xmit */
2826 tcb_desc->queue_index = BEACON_QUEUE;
2827 /* IBSS does not support HT yet, use 1M defaultly */
2828 tcb_desc->data_rate = 2;
2829 tcb_desc->RATRIndex = 7;
2830 tcb_desc->bTxDisableRateFallBack = 1;
2831 tcb_desc->bTxUseDriverAssingedRate = 1;
2833 skb_push(skb, priv->ieee80211->tx_headroom);
2834 if(skb){
2835 rtl8192_tx(priv, skb);
2841 * configure registers for beacon tx and enables it via
2842 * rtl8192_beacon_tx_enable(). rtl8192_beacon_tx_disable() might
2843 * be used to stop beacon transmission
2845 static void rtl8192_start_beacon(struct ieee80211_device *ieee80211)
2847 struct r8192_priv *priv = ieee80211_priv(ieee80211->dev);
2848 struct ieee80211_network *net = &priv->ieee80211->current_network;
2849 u16 BcnTimeCfg = 0;
2850 u16 BcnCW = 6;
2851 u16 BcnIFS = 0xf;
2853 DMESG("Enabling beacon TX");
2854 rtl8192_irq_disable(priv);
2855 //rtl8192_beacon_tx_enable(dev);
2857 /* ATIM window */
2858 write_nic_word(priv, ATIMWND, 2);
2860 /* Beacon interval (in unit of TU) */
2861 write_nic_word(priv, BCN_INTERVAL, net->beacon_interval);
2864 * DrvErlyInt (in unit of TU).
2865 * (Time to send interrupt to notify driver to c
2866 * hange beacon content)
2867 * */
2868 write_nic_word(priv, BCN_DRV_EARLY_INT, 10);
2871 * BcnDMATIM(in unit of us).
2872 * Indicates the time before TBTT to perform beacon queue DMA
2873 * */
2874 write_nic_word(priv, BCN_DMATIME, 256);
2877 * Force beacon frame transmission even after receiving
2878 * beacon frame from other ad hoc STA
2879 * */
2880 write_nic_byte(priv, BCN_ERR_THRESH, 100);
2882 /* Set CW and IFS */
2883 BcnTimeCfg |= BcnCW<<BCN_TCFG_CW_SHIFT;
2884 BcnTimeCfg |= BcnIFS<<BCN_TCFG_IFS;
2885 write_nic_word(priv, BCN_TCFG, BcnTimeCfg);
2888 /* enable the interrupt for ad-hoc process */
2889 rtl8192_irq_enable(priv);
2892 static bool HalRxCheckStuck8190Pci(struct r8192_priv *priv)
2894 u16 RegRxCounter = read_nic_word(priv, 0x130);
2895 bool bStuck = FALSE;
2897 RT_TRACE(COMP_RESET,"%s(): RegRxCounter is %d,RxCounter is %d\n",__FUNCTION__,RegRxCounter,priv->RxCounter);
2898 // If rssi is small, we should check rx for long time because of bad rx.
2899 // or maybe it will continuous silent reset every 2 seconds.
2900 priv->rx_chk_cnt++;
2901 if(priv->undecorated_smoothed_pwdb >= (RateAdaptiveTH_High+5))
2903 priv->rx_chk_cnt = 0; /* high rssi, check rx stuck right now. */
2905 else if(priv->undecorated_smoothed_pwdb < (RateAdaptiveTH_High+5) &&
2906 ((priv->CurrentChannelBW!=HT_CHANNEL_WIDTH_20&&priv->undecorated_smoothed_pwdb>=RateAdaptiveTH_Low_40M) ||
2907 (priv->CurrentChannelBW==HT_CHANNEL_WIDTH_20&&priv->undecorated_smoothed_pwdb>=RateAdaptiveTH_Low_20M)) )
2910 if(priv->rx_chk_cnt < 2)
2912 return bStuck;
2914 else
2916 priv->rx_chk_cnt = 0;
2919 else if(((priv->CurrentChannelBW!=HT_CHANNEL_WIDTH_20&&priv->undecorated_smoothed_pwdb<RateAdaptiveTH_Low_40M) ||
2920 (priv->CurrentChannelBW==HT_CHANNEL_WIDTH_20&&priv->undecorated_smoothed_pwdb<RateAdaptiveTH_Low_20M)) &&
2921 priv->undecorated_smoothed_pwdb >= VeryLowRSSI)
2923 if(priv->rx_chk_cnt < 4)
2925 return bStuck;
2927 else
2929 priv->rx_chk_cnt = 0;
2932 else
2934 if(priv->rx_chk_cnt < 8)
2936 return bStuck;
2938 else
2940 priv->rx_chk_cnt = 0;
2943 if(priv->RxCounter==RegRxCounter)
2944 bStuck = TRUE;
2946 priv->RxCounter = RegRxCounter;
2948 return bStuck;
2951 static RESET_TYPE RxCheckStuck(struct r8192_priv *priv)
2954 if(HalRxCheckStuck8190Pci(priv))
2956 RT_TRACE(COMP_RESET, "RxStuck Condition\n");
2957 return RESET_TYPE_SILENT;
2960 return RESET_TYPE_NORESET;
2963 static RESET_TYPE rtl819x_check_reset(struct r8192_priv *priv)
2965 RESET_TYPE RxResetType = RESET_TYPE_NORESET;
2966 RT_RF_POWER_STATE rfState;
2968 rfState = priv->eRFPowerState;
2970 if (rfState != eRfOff && (priv->ieee80211->iw_mode != IW_MODE_ADHOC)) {
2972 * If driver is in the status of firmware download failure,
2973 * driver skips RF initialization and RF is in turned off state.
2974 * Driver should check whether Rx stuck and do silent reset. And
2975 * if driver is in firmware download failure status, driver
2976 * should initialize RF in the following silent reset procedure
2978 * Driver should not check RX stuck in IBSS mode because it is
2979 * required to set Check BSSID in order to send beacon, however,
2980 * if check BSSID is set, STA cannot hear any packet a all.
2982 RxResetType = RxCheckStuck(priv);
2985 RT_TRACE(COMP_RESET, "%s(): RxResetType is %d\n", __FUNCTION__, RxResetType);
2987 return RxResetType;
2990 #ifdef ENABLE_IPS
2991 static void InactivePsWorkItemCallback(struct r8192_priv *priv)
2993 PRT_POWER_SAVE_CONTROL pPSC = &priv->PowerSaveControl;
2995 RT_TRACE(COMP_POWER, "InactivePsWorkItemCallback() --------->\n");
2997 // This flag "bSwRfProcessing", indicates the status of IPS procedure, should be set if the IPS workitem
2998 // is really scheduled.
2999 // The old code, sets this flag before scheduling the IPS workitem and however, at the same time the
3000 // previous IPS workitem did not end yet, fails to schedule the current workitem. Thus, bSwRfProcessing
3001 // blocks the IPS procedure of switching RF.
3002 // By Bruce, 2007-12-25.
3004 pPSC->bSwRfProcessing = TRUE;
3006 RT_TRACE(COMP_RF, "InactivePsWorkItemCallback(): Set RF to %s.\n",
3007 pPSC->eInactivePowerState == eRfOff?"OFF":"ON");
3010 MgntActSet_RF_State(priv, pPSC->eInactivePowerState, RF_CHANGE_BY_IPS);
3013 // To solve CAM values miss in RF OFF, rewrite CAM values after RF ON. By Bruce, 2007-09-20.
3015 pPSC->bSwRfProcessing = FALSE;
3016 RT_TRACE(COMP_POWER, "InactivePsWorkItemCallback() <---------\n");
3019 #ifdef ENABLE_LPS
3020 /* Change current and default preamble mode. */
3021 bool MgntActSet_802_11_PowerSaveMode(struct r8192_priv *priv, u8 rtPsMode)
3024 // Currently, we do not change power save mode on IBSS mode.
3025 if(priv->ieee80211->iw_mode == IW_MODE_ADHOC)
3027 return false;
3031 // <RJ_NOTE> If we make HW to fill up the PwrMgt bit for us,
3032 // some AP will not response to our mgnt frames with PwrMgt bit set,
3033 // e.g. cannot associate the AP.
3034 // So I commented out it. 2005.02.16, by rcnjko.
3036 // // Change device's power save mode.
3037 // Adapter->HalFunc.SetPSModeHandler( Adapter, rtPsMode );
3039 // Update power save mode configured.
3040 //RT_TRACE(COMP_LPS,"%s(): set ieee->ps = %x\n",__FUNCTION__,rtPsMode);
3041 if(!priv->ps_force) {
3042 priv->ieee80211->ps = rtPsMode;
3045 // Awake immediately
3046 if(priv->ieee80211->sta_sleep != 0 && rtPsMode == IEEE80211_PS_DISABLED)
3048 // Notify the AP we awke.
3049 rtl8192_hw_wakeup(priv->ieee80211);
3050 priv->ieee80211->sta_sleep = 0;
3052 spin_lock(&priv->ieee80211->mgmt_tx_lock);
3053 printk("LPS leave: notify AP we are awaked ++++++++++ SendNullFunctionData\n");
3054 ieee80211_sta_ps_send_null_frame(priv->ieee80211, 0);
3055 spin_unlock(&priv->ieee80211->mgmt_tx_lock);
3058 return true;
3061 /* Enter the leisure power save mode. */
3062 void LeisurePSEnter(struct ieee80211_device *ieee80211)
3064 struct r8192_priv *priv = ieee80211_priv(ieee80211->dev);
3065 PRT_POWER_SAVE_CONTROL pPSC = &priv->PowerSaveControl;
3067 if(!((priv->ieee80211->iw_mode == IW_MODE_INFRA) &&
3068 (priv->ieee80211->state == IEEE80211_LINKED)) ||
3069 (priv->ieee80211->iw_mode == IW_MODE_ADHOC) ||
3070 (priv->ieee80211->iw_mode == IW_MODE_MASTER))
3071 return;
3073 if (pPSC->bLeisurePs)
3075 // Idle for a while if we connect to AP a while ago.
3076 if(pPSC->LpsIdleCount >= RT_CHECK_FOR_HANG_PERIOD) // 4 Sec
3079 if(priv->ieee80211->ps == IEEE80211_PS_DISABLED)
3081 MgntActSet_802_11_PowerSaveMode(priv, IEEE80211_PS_MBCAST|IEEE80211_PS_UNICAST);
3085 else
3086 pPSC->LpsIdleCount++;
3091 /* Leave leisure power save mode. */
3092 void LeisurePSLeave(struct ieee80211_device *ieee80211)
3094 struct r8192_priv *priv = ieee80211_priv(ieee80211->dev);
3095 PRT_POWER_SAVE_CONTROL pPSC = &priv->PowerSaveControl;
3097 if (pPSC->bLeisurePs)
3099 if(priv->ieee80211->ps != IEEE80211_PS_DISABLED)
3101 // move to lps_wakecomplete()
3102 MgntActSet_802_11_PowerSaveMode(priv, IEEE80211_PS_DISABLED);
3107 #endif
3110 /* Enter the inactive power save mode. RF will be off */
3111 void IPSEnter(struct r8192_priv *priv)
3113 PRT_POWER_SAVE_CONTROL pPSC = &priv->PowerSaveControl;
3114 RT_RF_POWER_STATE rtState;
3116 if (pPSC->bInactivePs)
3118 rtState = priv->eRFPowerState;
3120 // Added by Bruce, 2007-12-25.
3121 // Do not enter IPS in the following conditions:
3122 // (1) RF is already OFF or Sleep
3123 // (2) bSwRfProcessing (indicates the IPS is still under going)
3124 // (3) Connectted (only disconnected can trigger IPS)
3125 // (4) IBSS (send Beacon)
3126 // (5) AP mode (send Beacon)
3128 if (rtState == eRfOn && !pPSC->bSwRfProcessing
3129 && (priv->ieee80211->state != IEEE80211_LINKED) )
3131 RT_TRACE(COMP_RF,"IPSEnter(): Turn off RF.\n");
3132 pPSC->eInactivePowerState = eRfOff;
3133 // queue_work(priv->priv_wq,&(pPSC->InactivePsWorkItem));
3134 InactivePsWorkItemCallback(priv);
3140 // Description:
3141 // Leave the inactive power save mode, RF will be on.
3142 // 2007.08.17, by shien chang.
3144 void IPSLeave(struct r8192_priv *priv)
3146 PRT_POWER_SAVE_CONTROL pPSC = &priv->PowerSaveControl;
3147 RT_RF_POWER_STATE rtState;
3149 if (pPSC->bInactivePs)
3151 rtState = priv->eRFPowerState;
3152 if (rtState != eRfOn && !pPSC->bSwRfProcessing && priv->RfOffReason <= RF_CHANGE_BY_IPS)
3154 RT_TRACE(COMP_POWER, "IPSLeave(): Turn on RF.\n");
3155 pPSC->eInactivePowerState = eRfOn;
3156 InactivePsWorkItemCallback(priv);
3161 void IPSLeave_wq(struct work_struct *work)
3163 struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, ips_leave_wq);
3164 struct net_device *dev = ieee->dev;
3166 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
3167 down(&priv->ieee80211->ips_sem);
3168 IPSLeave(priv);
3169 up(&priv->ieee80211->ips_sem);
3172 void ieee80211_ips_leave_wq(struct ieee80211_device *ieee80211)
3174 struct r8192_priv *priv = ieee80211_priv(ieee80211->dev);
3175 RT_RF_POWER_STATE rtState;
3176 rtState = priv->eRFPowerState;
3178 if (priv->PowerSaveControl.bInactivePs){
3179 if(rtState == eRfOff){
3180 if(priv->RfOffReason > RF_CHANGE_BY_IPS)
3182 RT_TRACE(COMP_ERR, "%s(): RF is OFF.\n",__FUNCTION__);
3183 return;
3185 else{
3186 printk("=========>%s(): IPSLeave\n",__FUNCTION__);
3187 queue_work(priv->ieee80211->wq,&priv->ieee80211->ips_leave_wq);
3192 //added by amy 090331 end
3193 void ieee80211_ips_leave(struct ieee80211_device *ieee80211)
3195 struct r8192_priv *priv = ieee80211_priv(ieee80211->dev);
3196 down(&ieee80211->ips_sem);
3197 IPSLeave(priv);
3198 up(&ieee80211->ips_sem);
3200 #endif
3202 static void rtl819x_update_rxcounts(
3203 struct r8192_priv *priv,
3204 u32* TotalRxBcnNum,
3205 u32* TotalRxDataNum
3208 u16 SlotIndex;
3209 u8 i;
3211 *TotalRxBcnNum = 0;
3212 *TotalRxDataNum = 0;
3214 SlotIndex = (priv->ieee80211->LinkDetectInfo.SlotIndex++)%(priv->ieee80211->LinkDetectInfo.SlotNum);
3215 priv->ieee80211->LinkDetectInfo.RxBcnNum[SlotIndex] = priv->ieee80211->LinkDetectInfo.NumRecvBcnInPeriod;
3216 priv->ieee80211->LinkDetectInfo.RxDataNum[SlotIndex] = priv->ieee80211->LinkDetectInfo.NumRecvDataInPeriod;
3217 for( i=0; i<priv->ieee80211->LinkDetectInfo.SlotNum; i++ ){
3218 *TotalRxBcnNum += priv->ieee80211->LinkDetectInfo.RxBcnNum[i];
3219 *TotalRxDataNum += priv->ieee80211->LinkDetectInfo.RxDataNum[i];
3224 static void rtl819x_watchdog_wqcallback(struct work_struct *work)
3226 struct delayed_work *dwork = container_of(work,struct delayed_work,work);
3227 struct r8192_priv *priv = container_of(dwork,struct r8192_priv,watch_dog_wq);
3228 struct ieee80211_device* ieee = priv->ieee80211;
3229 RESET_TYPE ResetType = RESET_TYPE_NORESET;
3230 bool bBusyTraffic = false;
3231 bool bEnterPS = false;
3233 if ((!priv->up) || priv->bHwRadioOff)
3234 return;
3236 if(!priv->up)
3237 return;
3238 hal_dm_watchdog(priv);
3239 #ifdef ENABLE_IPS
3240 if(ieee->actscanning == false){
3241 if((ieee->iw_mode == IW_MODE_INFRA) && (ieee->state == IEEE80211_NOLINK) &&
3242 (priv->eRFPowerState == eRfOn) && !ieee->is_set_key &&
3243 (!ieee->proto_stoppping) && !ieee->wx_set_enc){
3244 if (priv->PowerSaveControl.ReturnPoint == IPS_CALLBACK_NONE){
3245 IPSEnter(priv);
3249 #endif
3250 {//to get busy traffic condition
3251 if(ieee->state == IEEE80211_LINKED)
3253 if( ieee->LinkDetectInfo.NumRxOkInPeriod> 100 ||
3254 ieee->LinkDetectInfo.NumTxOkInPeriod> 100 ) {
3255 bBusyTraffic = true;
3258 #ifdef ENABLE_LPS
3259 //added by amy for Leisure PS
3260 if( ((ieee->LinkDetectInfo.NumRxUnicastOkInPeriod + ieee->LinkDetectInfo.NumTxOkInPeriod) > 8 ) ||
3261 (ieee->LinkDetectInfo.NumRxUnicastOkInPeriod > 2) )
3263 bEnterPS= false;
3265 else
3267 bEnterPS= true;
3270 // LeisurePS only work in infra mode.
3271 if(bEnterPS)
3273 LeisurePSEnter(priv->ieee80211);
3275 else
3277 LeisurePSLeave(priv->ieee80211);
3279 #endif
3282 else
3284 #ifdef ENABLE_LPS
3285 LeisurePSLeave(priv->ieee80211);
3286 #endif
3289 ieee->LinkDetectInfo.NumRxOkInPeriod = 0;
3290 ieee->LinkDetectInfo.NumTxOkInPeriod = 0;
3291 ieee->LinkDetectInfo.NumRxUnicastOkInPeriod = 0;
3292 ieee->LinkDetectInfo.bBusyTraffic = bBusyTraffic;
3296 //added by amy for AP roaming
3297 if(ieee->state == IEEE80211_LINKED && ieee->iw_mode == IW_MODE_INFRA)
3299 u32 TotalRxBcnNum = 0;
3300 u32 TotalRxDataNum = 0;
3302 rtl819x_update_rxcounts(priv, &TotalRxBcnNum, &TotalRxDataNum);
3303 if((TotalRxBcnNum+TotalRxDataNum) == 0)
3305 if (priv->eRFPowerState == eRfOff)
3306 RT_TRACE(COMP_ERR,"========>%s()\n",__FUNCTION__);
3307 printk("===>%s(): AP is power off,connect another one\n",__FUNCTION__);
3308 // Dot11d_Reset(dev);
3309 ieee->state = IEEE80211_ASSOCIATING;
3310 notify_wx_assoc_event(priv->ieee80211);
3311 RemovePeerTS(priv->ieee80211,priv->ieee80211->current_network.bssid);
3312 ieee->is_roaming = true;
3313 ieee->is_set_key = false;
3314 ieee->link_change(ieee);
3315 queue_work(ieee->wq, &ieee->associate_procedure_wq);
3318 ieee->LinkDetectInfo.NumRecvBcnInPeriod=0;
3319 ieee->LinkDetectInfo.NumRecvDataInPeriod=0;
3321 //check if reset the driver
3322 if (priv->watchdog_check_reset_cnt++ >= 3 && !ieee->is_roaming &&
3323 priv->watchdog_last_time != 1)
3325 ResetType = rtl819x_check_reset(priv);
3326 priv->watchdog_check_reset_cnt = 3;
3328 if(!priv->bDisableNormalResetCheck && ResetType == RESET_TYPE_NORMAL)
3330 priv->ResetProgress = RESET_TYPE_NORMAL;
3331 RT_TRACE(COMP_RESET,"%s(): NOMAL RESET\n",__FUNCTION__);
3332 return;
3334 /* disable silent reset temply 2008.9.11*/
3336 if( ((priv->force_reset) || (!priv->bDisableNormalResetCheck && ResetType==RESET_TYPE_SILENT))) // This is control by OID set in Pomelo
3338 priv->watchdog_last_time = 1;
3340 else
3341 priv->watchdog_last_time = 0;
3343 priv->force_reset = false;
3344 priv->bForcedSilentReset = false;
3345 priv->bResetInProgress = false;
3346 RT_TRACE(COMP_TRACE, " <==RtUsbCheckForHangWorkItemCallback()\n");
3350 void watch_dog_timer_callback(unsigned long data)
3352 struct r8192_priv *priv = (struct r8192_priv *) data;
3353 queue_delayed_work(priv->priv_wq,&priv->watch_dog_wq,0);
3354 mod_timer(&priv->watch_dog_timer, jiffies + MSECS(IEEE80211_WATCH_DOG_TIME));
3358 static int _rtl8192_up(struct r8192_priv *priv)
3360 RT_STATUS init_status = RT_STATUS_SUCCESS;
3361 struct net_device *dev = priv->ieee80211->dev;
3363 priv->up=1;
3364 priv->ieee80211->ieee_up=1;
3365 priv->bdisable_nic = false; //YJ,add,091111
3366 RT_TRACE(COMP_INIT, "Bringing up iface\n");
3368 init_status = rtl8192_adapter_start(priv);
3369 if(init_status != RT_STATUS_SUCCESS)
3371 RT_TRACE(COMP_ERR,"ERR!!! %s(): initialization is failed!\n",__FUNCTION__);
3372 return -1;
3374 RT_TRACE(COMP_INIT, "start adapter finished\n");
3376 if (priv->eRFPowerState != eRfOn)
3377 MgntActSet_RF_State(priv, eRfOn, priv->RfOffReason);
3379 if(priv->ieee80211->state != IEEE80211_LINKED)
3380 ieee80211_softmac_start_protocol(priv->ieee80211);
3381 ieee80211_reset_queue(priv->ieee80211);
3382 watch_dog_timer_callback((unsigned long) priv);
3383 if(!netif_queue_stopped(dev))
3384 netif_start_queue(dev);
3385 else
3386 netif_wake_queue(dev);
3388 return 0;
3392 static int rtl8192_open(struct net_device *dev)
3394 struct r8192_priv *priv = ieee80211_priv(dev);
3395 int ret;
3397 down(&priv->wx_sem);
3398 ret = rtl8192_up(dev);
3399 up(&priv->wx_sem);
3400 return ret;
3405 int rtl8192_up(struct net_device *dev)
3407 struct r8192_priv *priv = ieee80211_priv(dev);
3409 if (priv->up == 1) return -1;
3411 return _rtl8192_up(priv);
3415 static int rtl8192_close(struct net_device *dev)
3417 struct r8192_priv *priv = ieee80211_priv(dev);
3418 int ret;
3420 down(&priv->wx_sem);
3422 ret = rtl8192_down(dev);
3424 up(&priv->wx_sem);
3426 return ret;
3430 int rtl8192_down(struct net_device *dev)
3432 struct r8192_priv *priv = ieee80211_priv(dev);
3434 if (priv->up == 0) return -1;
3436 #ifdef ENABLE_LPS
3437 //LZM for PS-Poll AID issue. 090429
3438 if(priv->ieee80211->state == IEEE80211_LINKED)
3439 LeisurePSLeave(priv->ieee80211);
3440 #endif
3442 priv->up=0;
3443 priv->ieee80211->ieee_up = 0;
3444 RT_TRACE(COMP_DOWN, "==========>%s()\n", __FUNCTION__);
3445 /* FIXME */
3446 if (!netif_queue_stopped(dev))
3447 netif_stop_queue(dev);
3449 rtl8192_irq_disable(priv);
3450 rtl8192_cancel_deferred_work(priv);
3451 deinit_hal_dm(priv);
3452 del_timer_sync(&priv->watch_dog_timer);
3454 ieee80211_softmac_stop_protocol(priv->ieee80211,true);
3456 rtl8192_halt_adapter(priv, false);
3457 memset(&priv->ieee80211->current_network, 0 , offsetof(struct ieee80211_network, list));
3459 RT_TRACE(COMP_DOWN, "<==========%s()\n", __FUNCTION__);
3461 return 0;
3465 void rtl8192_commit(struct r8192_priv *priv)
3467 if (priv->up == 0) return ;
3470 ieee80211_softmac_stop_protocol(priv->ieee80211,true);
3472 rtl8192_irq_disable(priv);
3473 rtl8192_halt_adapter(priv, true);
3474 _rtl8192_up(priv);
3477 static void rtl8192_restart(struct work_struct *work)
3479 struct r8192_priv *priv = container_of(work, struct r8192_priv, reset_wq);
3481 down(&priv->wx_sem);
3483 rtl8192_commit(priv);
3485 up(&priv->wx_sem);
3488 static void r8192_set_multicast(struct net_device *dev)
3490 struct r8192_priv *priv = ieee80211_priv(dev);
3492 priv->promisc = (dev->flags & IFF_PROMISC) ? 1 : 0;
3496 static int r8192_set_mac_adr(struct net_device *dev, void *mac)
3498 struct r8192_priv *priv = ieee80211_priv(dev);
3499 struct sockaddr *addr = mac;
3501 down(&priv->wx_sem);
3503 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
3505 schedule_work(&priv->reset_wq);
3506 up(&priv->wx_sem);
3508 return 0;
3511 static void r8192e_set_hw_key(struct r8192_priv *priv, struct ieee_param *ipw)
3513 struct ieee80211_device *ieee = priv->ieee80211;
3514 u8 broadcast_addr[6] = {0xff,0xff,0xff,0xff,0xff,0xff};
3515 u32 key[4];
3517 if (ipw->u.crypt.set_tx) {
3518 if (strcmp(ipw->u.crypt.alg, "CCMP") == 0)
3519 ieee->pairwise_key_type = KEY_TYPE_CCMP;
3520 else if (strcmp(ipw->u.crypt.alg, "TKIP") == 0)
3521 ieee->pairwise_key_type = KEY_TYPE_TKIP;
3522 else if (strcmp(ipw->u.crypt.alg, "WEP") == 0) {
3523 if (ipw->u.crypt.key_len == 13)
3524 ieee->pairwise_key_type = KEY_TYPE_WEP104;
3525 else if (ipw->u.crypt.key_len == 5)
3526 ieee->pairwise_key_type = KEY_TYPE_WEP40;
3527 } else
3528 ieee->pairwise_key_type = KEY_TYPE_NA;
3530 if (ieee->pairwise_key_type) {
3531 memcpy(key, ipw->u.crypt.key, 16);
3532 EnableHWSecurityConfig8192(priv);
3534 * We fill both index entry and 4th entry for pairwise
3535 * key as in IPW interface, adhoc will only get here,
3536 * so we need index entry for its default key serching!
3538 setKey(priv, 4, ipw->u.crypt.idx,
3539 ieee->pairwise_key_type,
3540 (u8*)ieee->ap_mac_addr, 0, key);
3542 /* LEAP WEP will never set this. */
3543 if (ieee->auth_mode != 2)
3544 setKey(priv, ipw->u.crypt.idx, ipw->u.crypt.idx,
3545 ieee->pairwise_key_type,
3546 (u8*)ieee->ap_mac_addr, 0, key);
3548 if ((ieee->pairwise_key_type == KEY_TYPE_CCMP) &&
3549 ieee->pHTInfo->bCurrentHTSupport) {
3550 write_nic_byte(priv, 0x173, 1); /* fix aes bug */
3552 } else {
3553 memcpy(key, ipw->u.crypt.key, 16);
3554 if (strcmp(ipw->u.crypt.alg, "CCMP") == 0)
3555 ieee->group_key_type= KEY_TYPE_CCMP;
3556 else if (strcmp(ipw->u.crypt.alg, "TKIP") == 0)
3557 ieee->group_key_type = KEY_TYPE_TKIP;
3558 else if (strcmp(ipw->u.crypt.alg, "WEP") == 0) {
3559 if (ipw->u.crypt.key_len == 13)
3560 ieee->group_key_type = KEY_TYPE_WEP104;
3561 else if (ipw->u.crypt.key_len == 5)
3562 ieee->group_key_type = KEY_TYPE_WEP40;
3563 } else
3564 ieee->group_key_type = KEY_TYPE_NA;
3566 if (ieee->group_key_type) {
3567 setKey(priv, ipw->u.crypt.idx, ipw->u.crypt.idx,
3568 ieee->group_key_type, broadcast_addr, 0, key);
3573 /* based on ipw2200 driver */
3574 static int rtl8192_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3576 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
3577 struct iwreq *wrq = (struct iwreq *)rq;
3578 int ret=-1;
3579 struct iw_point *p = &wrq->u.data;
3580 struct ieee_param *ipw = NULL;//(struct ieee_param *)wrq->u.data.pointer;
3582 down(&priv->wx_sem);
3585 if (p->length < sizeof(struct ieee_param) || !p->pointer){
3586 ret = -EINVAL;
3587 goto out;
3590 ipw = kmalloc(p->length, GFP_KERNEL);
3591 if (ipw == NULL){
3592 ret = -ENOMEM;
3593 goto out;
3595 if (copy_from_user(ipw, p->pointer, p->length)) {
3596 kfree(ipw);
3597 ret = -EFAULT;
3598 goto out;
3601 switch (cmd) {
3602 case RTL_IOCTL_WPA_SUPPLICANT:
3603 /* parse here for HW security */
3604 if (ipw->cmd == IEEE_CMD_SET_ENCRYPTION)
3605 r8192e_set_hw_key(priv, ipw);
3606 ret = ieee80211_wpa_supplicant_ioctl(priv->ieee80211, &wrq->u.data);
3607 break;
3609 default:
3610 ret = -EOPNOTSUPP;
3611 break;
3614 kfree(ipw);
3615 out:
3616 up(&priv->wx_sem);
3618 return ret;
3621 static u8 HwRateToMRate90(bool bIsHT, u8 rate)
3623 u8 ret_rate = 0x02;
3625 if(!bIsHT) {
3626 switch(rate) {
3627 case DESC90_RATE1M: ret_rate = MGN_1M; break;
3628 case DESC90_RATE2M: ret_rate = MGN_2M; break;
3629 case DESC90_RATE5_5M: ret_rate = MGN_5_5M; break;
3630 case DESC90_RATE11M: ret_rate = MGN_11M; break;
3631 case DESC90_RATE6M: ret_rate = MGN_6M; break;
3632 case DESC90_RATE9M: ret_rate = MGN_9M; break;
3633 case DESC90_RATE12M: ret_rate = MGN_12M; break;
3634 case DESC90_RATE18M: ret_rate = MGN_18M; break;
3635 case DESC90_RATE24M: ret_rate = MGN_24M; break;
3636 case DESC90_RATE36M: ret_rate = MGN_36M; break;
3637 case DESC90_RATE48M: ret_rate = MGN_48M; break;
3638 case DESC90_RATE54M: ret_rate = MGN_54M; break;
3640 default:
3641 RT_TRACE(COMP_RECV, "HwRateToMRate90(): Non supported Rate [%x], bIsHT = %d!!!\n", rate, bIsHT);
3642 break;
3645 } else {
3646 switch(rate) {
3647 case DESC90_RATEMCS0: ret_rate = MGN_MCS0; break;
3648 case DESC90_RATEMCS1: ret_rate = MGN_MCS1; break;
3649 case DESC90_RATEMCS2: ret_rate = MGN_MCS2; break;
3650 case DESC90_RATEMCS3: ret_rate = MGN_MCS3; break;
3651 case DESC90_RATEMCS4: ret_rate = MGN_MCS4; break;
3652 case DESC90_RATEMCS5: ret_rate = MGN_MCS5; break;
3653 case DESC90_RATEMCS6: ret_rate = MGN_MCS6; break;
3654 case DESC90_RATEMCS7: ret_rate = MGN_MCS7; break;
3655 case DESC90_RATEMCS8: ret_rate = MGN_MCS8; break;
3656 case DESC90_RATEMCS9: ret_rate = MGN_MCS9; break;
3657 case DESC90_RATEMCS10: ret_rate = MGN_MCS10; break;
3658 case DESC90_RATEMCS11: ret_rate = MGN_MCS11; break;
3659 case DESC90_RATEMCS12: ret_rate = MGN_MCS12; break;
3660 case DESC90_RATEMCS13: ret_rate = MGN_MCS13; break;
3661 case DESC90_RATEMCS14: ret_rate = MGN_MCS14; break;
3662 case DESC90_RATEMCS15: ret_rate = MGN_MCS15; break;
3663 case DESC90_RATEMCS32: ret_rate = (0x80|0x20); break;
3665 default:
3666 RT_TRACE(COMP_RECV, "HwRateToMRate90(): Non supported Rate [%x], bIsHT = %d!!!\n",rate, bIsHT);
3667 break;
3671 return ret_rate;
3674 /* Record the TSF time stamp when receiving a packet */
3675 static void UpdateRxPktTimeStamp8190(struct r8192_priv *priv, struct ieee80211_rx_stats *stats)
3678 if(stats->bIsAMPDU && !stats->bFirstMPDU) {
3679 stats->mac_time[0] = priv->LastRxDescTSFLow;
3680 stats->mac_time[1] = priv->LastRxDescTSFHigh;
3681 } else {
3682 priv->LastRxDescTSFLow = stats->mac_time[0];
3683 priv->LastRxDescTSFHigh = stats->mac_time[1];
3687 static long rtl819x_translate_todbm(u8 signal_strength_index)// 0-100 index.
3689 long signal_power; // in dBm.
3691 // Translate to dBm (x=0.5y-95).
3692 signal_power = (long)((signal_strength_index + 1) >> 1);
3693 signal_power -= 95;
3695 return signal_power;
3698 /* 2008/01/22 MH We can not delcare RSSI/EVM total value of sliding window to
3699 be a local static. Otherwise, it may increase when we return from S3/S4. The
3700 value will be kept in memory or disk. We must delcare the value in adapter
3701 and it will be reinitialized when return from S3/S4. */
3702 static void rtl8192_process_phyinfo(struct r8192_priv * priv, u8* buffer,struct ieee80211_rx_stats * pprevious_stats, struct ieee80211_rx_stats * pcurrent_stats)
3704 bool bcheck = false;
3705 u8 rfpath;
3706 u32 nspatial_stream, tmp_val;
3707 static u32 slide_rssi_index=0, slide_rssi_statistics=0;
3708 static u32 slide_evm_index=0, slide_evm_statistics=0;
3709 static u32 last_rssi=0, last_evm=0;
3710 //cosa add for beacon rssi smoothing
3711 static u32 slide_beacon_adc_pwdb_index=0, slide_beacon_adc_pwdb_statistics=0;
3712 static u32 last_beacon_adc_pwdb=0;
3714 struct ieee80211_hdr_3addr *hdr;
3715 u16 sc ;
3716 unsigned int frag,seq;
3717 hdr = (struct ieee80211_hdr_3addr *)buffer;
3718 sc = le16_to_cpu(hdr->seq_ctl);
3719 frag = WLAN_GET_SEQ_FRAG(sc);
3720 seq = WLAN_GET_SEQ_SEQ(sc);
3723 // Check whether we should take the previous packet into accounting
3725 if(!pprevious_stats->bIsAMPDU)
3727 // if previous packet is not aggregated packet
3728 bcheck = true;
3731 if(slide_rssi_statistics++ >= PHY_RSSI_SLID_WIN_MAX)
3733 slide_rssi_statistics = PHY_RSSI_SLID_WIN_MAX;
3734 last_rssi = priv->stats.slide_signal_strength[slide_rssi_index];
3735 priv->stats.slide_rssi_total -= last_rssi;
3737 priv->stats.slide_rssi_total += pprevious_stats->SignalStrength;
3739 priv->stats.slide_signal_strength[slide_rssi_index++] = pprevious_stats->SignalStrength;
3740 if(slide_rssi_index >= PHY_RSSI_SLID_WIN_MAX)
3741 slide_rssi_index = 0;
3743 // <1> Showed on UI for user, in dbm
3744 tmp_val = priv->stats.slide_rssi_total/slide_rssi_statistics;
3745 priv->stats.signal_strength = rtl819x_translate_todbm((u8)tmp_val);
3746 pcurrent_stats->rssi = priv->stats.signal_strength;
3748 // If the previous packet does not match the criteria, neglect it
3750 if(!pprevious_stats->bPacketMatchBSSID)
3752 if(!pprevious_stats->bToSelfBA)
3753 return;
3756 if(!bcheck)
3757 return;
3759 // <2> Showed on UI for engineering
3760 // hardware does not provide rssi information for each rf path in CCK
3761 if(!pprevious_stats->bIsCCK && pprevious_stats->bPacketToSelf)
3763 for (rfpath = RF90_PATH_A; rfpath < RF90_PATH_C; rfpath++)
3765 if (!rtl8192_phy_CheckIsLegalRFPath(priv, rfpath))
3766 continue;
3767 RT_TRACE(COMP_DBG, "pPreviousstats->RxMIMOSignalStrength[rfpath] = %d\n", pprevious_stats->RxMIMOSignalStrength[rfpath]);
3768 //Fixed by Jacken 2008-03-20
3769 if(priv->stats.rx_rssi_percentage[rfpath] == 0)
3771 priv->stats.rx_rssi_percentage[rfpath] = pprevious_stats->RxMIMOSignalStrength[rfpath];
3773 if(pprevious_stats->RxMIMOSignalStrength[rfpath] > priv->stats.rx_rssi_percentage[rfpath])
3775 priv->stats.rx_rssi_percentage[rfpath] =
3776 ( (priv->stats.rx_rssi_percentage[rfpath]*(Rx_Smooth_Factor-1)) +
3777 (pprevious_stats->RxMIMOSignalStrength[rfpath])) /(Rx_Smooth_Factor);
3778 priv->stats.rx_rssi_percentage[rfpath] = priv->stats.rx_rssi_percentage[rfpath] + 1;
3780 else
3782 priv->stats.rx_rssi_percentage[rfpath] =
3783 ( (priv->stats.rx_rssi_percentage[rfpath]*(Rx_Smooth_Factor-1)) +
3784 (pprevious_stats->RxMIMOSignalStrength[rfpath])) /(Rx_Smooth_Factor);
3786 RT_TRACE(COMP_DBG, "priv->RxStats.RxRSSIPercentage[rfPath] = %d \n" , priv->stats.rx_rssi_percentage[rfpath]);
3792 // Check PWDB.
3794 //cosa add for beacon rssi smoothing by average.
3795 if(pprevious_stats->bPacketBeacon)
3797 /* record the beacon pwdb to the sliding window. */
3798 if(slide_beacon_adc_pwdb_statistics++ >= PHY_Beacon_RSSI_SLID_WIN_MAX)
3800 slide_beacon_adc_pwdb_statistics = PHY_Beacon_RSSI_SLID_WIN_MAX;
3801 last_beacon_adc_pwdb = priv->stats.Slide_Beacon_pwdb[slide_beacon_adc_pwdb_index];
3802 priv->stats.Slide_Beacon_Total -= last_beacon_adc_pwdb;
3803 // slide_beacon_adc_pwdb_index, last_beacon_adc_pwdb, Adapter->RxStats.Slide_Beacon_Total);
3805 priv->stats.Slide_Beacon_Total += pprevious_stats->RxPWDBAll;
3806 priv->stats.Slide_Beacon_pwdb[slide_beacon_adc_pwdb_index] = pprevious_stats->RxPWDBAll;
3807 slide_beacon_adc_pwdb_index++;
3808 if(slide_beacon_adc_pwdb_index >= PHY_Beacon_RSSI_SLID_WIN_MAX)
3809 slide_beacon_adc_pwdb_index = 0;
3810 pprevious_stats->RxPWDBAll = priv->stats.Slide_Beacon_Total/slide_beacon_adc_pwdb_statistics;
3811 if(pprevious_stats->RxPWDBAll >= 3)
3812 pprevious_stats->RxPWDBAll -= 3;
3815 RT_TRACE(COMP_RXDESC, "Smooth %s PWDB = %d\n",
3816 pprevious_stats->bIsCCK? "CCK": "OFDM",
3817 pprevious_stats->RxPWDBAll);
3819 if(pprevious_stats->bPacketToSelf || pprevious_stats->bPacketBeacon || pprevious_stats->bToSelfBA)
3821 if(priv->undecorated_smoothed_pwdb < 0) // initialize
3823 priv->undecorated_smoothed_pwdb = pprevious_stats->RxPWDBAll;
3826 if(pprevious_stats->RxPWDBAll > (u32)priv->undecorated_smoothed_pwdb)
3828 priv->undecorated_smoothed_pwdb =
3829 ( ((priv->undecorated_smoothed_pwdb)*(Rx_Smooth_Factor-1)) +
3830 (pprevious_stats->RxPWDBAll)) /(Rx_Smooth_Factor);
3831 priv->undecorated_smoothed_pwdb = priv->undecorated_smoothed_pwdb + 1;
3833 else
3835 priv->undecorated_smoothed_pwdb =
3836 ( ((priv->undecorated_smoothed_pwdb)*(Rx_Smooth_Factor-1)) +
3837 (pprevious_stats->RxPWDBAll)) /(Rx_Smooth_Factor);
3842 // Check EVM
3844 /* record the general EVM to the sliding window. */
3845 if(pprevious_stats->SignalQuality == 0)
3848 else
3850 if(pprevious_stats->bPacketToSelf || pprevious_stats->bPacketBeacon || pprevious_stats->bToSelfBA){
3851 if(slide_evm_statistics++ >= PHY_RSSI_SLID_WIN_MAX){
3852 slide_evm_statistics = PHY_RSSI_SLID_WIN_MAX;
3853 last_evm = priv->stats.slide_evm[slide_evm_index];
3854 priv->stats.slide_evm_total -= last_evm;
3857 priv->stats.slide_evm_total += pprevious_stats->SignalQuality;
3859 priv->stats.slide_evm[slide_evm_index++] = pprevious_stats->SignalQuality;
3860 if(slide_evm_index >= PHY_RSSI_SLID_WIN_MAX)
3861 slide_evm_index = 0;
3863 // <1> Showed on UI for user, in percentage.
3864 tmp_val = priv->stats.slide_evm_total/slide_evm_statistics;
3865 //cosa add 10/11/2007, Showed on UI for user in Windows Vista, for Link quality.
3868 // <2> Showed on UI for engineering
3869 if(pprevious_stats->bPacketToSelf || pprevious_stats->bPacketBeacon || pprevious_stats->bToSelfBA)
3871 for(nspatial_stream = 0; nspatial_stream<2 ; nspatial_stream++) // 2 spatial stream
3873 if(pprevious_stats->RxMIMOSignalQuality[nspatial_stream] != -1)
3875 if(priv->stats.rx_evm_percentage[nspatial_stream] == 0) // initialize
3877 priv->stats.rx_evm_percentage[nspatial_stream] = pprevious_stats->RxMIMOSignalQuality[nspatial_stream];
3879 priv->stats.rx_evm_percentage[nspatial_stream] =
3880 ( (priv->stats.rx_evm_percentage[nspatial_stream]* (Rx_Smooth_Factor-1)) +
3881 (pprevious_stats->RxMIMOSignalQuality[nspatial_stream]* 1)) / (Rx_Smooth_Factor);
3889 static u8 rtl819x_query_rxpwrpercentage(
3890 char antpower
3893 if ((antpower <= -100) || (antpower >= 20))
3895 return 0;
3897 else if (antpower >= 0)
3899 return 100;
3901 else
3903 return (100+antpower);
3908 static u8
3909 rtl819x_evm_dbtopercentage(
3910 char value
3913 char ret_val;
3915 ret_val = value;
3917 if(ret_val >= 0)
3918 ret_val = 0;
3919 if(ret_val <= -33)
3920 ret_val = -33;
3921 ret_val = 0 - ret_val;
3922 ret_val*=3;
3923 if(ret_val == 99)
3924 ret_val = 100;
3925 return ret_val;
3928 /* We want good-looking for signal strength/quality */
3929 static long rtl819x_signal_scale_mapping(long currsig)
3931 long retsig;
3933 // Step 1. Scale mapping.
3934 if(currsig >= 61 && currsig <= 100)
3936 retsig = 90 + ((currsig - 60) / 4);
3938 else if(currsig >= 41 && currsig <= 60)
3940 retsig = 78 + ((currsig - 40) / 2);
3942 else if(currsig >= 31 && currsig <= 40)
3944 retsig = 66 + (currsig - 30);
3946 else if(currsig >= 21 && currsig <= 30)
3948 retsig = 54 + (currsig - 20);
3950 else if(currsig >= 5 && currsig <= 20)
3952 retsig = 42 + (((currsig - 5) * 2) / 3);
3954 else if(currsig == 4)
3956 retsig = 36;
3958 else if(currsig == 3)
3960 retsig = 27;
3962 else if(currsig == 2)
3964 retsig = 18;
3966 else if(currsig == 1)
3968 retsig = 9;
3970 else
3972 retsig = currsig;
3975 return retsig;
3978 static void rtl8192_query_rxphystatus(
3979 struct r8192_priv * priv,
3980 struct ieee80211_rx_stats * pstats,
3981 prx_desc_819x_pci pdesc,
3982 prx_fwinfo_819x_pci pdrvinfo,
3983 struct ieee80211_rx_stats * precord_stats,
3984 bool bpacket_match_bssid,
3985 bool bpacket_toself,
3986 bool bPacketBeacon,
3987 bool bToSelfBA
3990 //PRT_RFD_STATUS pRtRfdStatus = &(pRfd->Status);
3991 phy_sts_ofdm_819xpci_t* pofdm_buf;
3992 phy_sts_cck_819xpci_t * pcck_buf;
3993 phy_ofdm_rx_status_rxsc_sgien_exintfflag* prxsc;
3994 u8 *prxpkt;
3995 u8 i,max_spatial_stream, tmp_rxsnr, tmp_rxevm, rxsc_sgien_exflg;
3996 char rx_pwr[4], rx_pwr_all=0;
3997 //long rx_avg_pwr = 0;
3998 char rx_snrX, rx_evmX;
3999 u8 evm, pwdb_all;
4000 u32 RSSI, total_rssi=0;//, total_evm=0;
4001 // long signal_strength_index = 0;
4002 u8 is_cck_rate=0;
4003 u8 rf_rx_num = 0;
4005 is_cck_rate = rx_hal_is_cck_rate(pdrvinfo);
4007 // Record it for next packet processing
4008 memset(precord_stats, 0, sizeof(struct ieee80211_rx_stats));
4009 pstats->bPacketMatchBSSID = precord_stats->bPacketMatchBSSID = bpacket_match_bssid;
4010 pstats->bPacketToSelf = precord_stats->bPacketToSelf = bpacket_toself;
4011 pstats->bIsCCK = precord_stats->bIsCCK = is_cck_rate;//RX_HAL_IS_CCK_RATE(pDrvInfo);
4012 pstats->bPacketBeacon = precord_stats->bPacketBeacon = bPacketBeacon;
4013 pstats->bToSelfBA = precord_stats->bToSelfBA = bToSelfBA;
4014 /*2007.08.30 requested by SD3 Jerry */
4015 if (priv->phy_check_reg824 == 0)
4017 priv->phy_reg824_bit9 = rtl8192_QueryBBReg(priv, rFPGA0_XA_HSSIParameter2, 0x200);
4018 priv->phy_check_reg824 = 1;
4022 prxpkt = (u8*)pdrvinfo;
4024 /* Move pointer to the 16th bytes. Phy status start address. */
4025 prxpkt += sizeof(rx_fwinfo_819x_pci);
4027 /* Initial the cck and ofdm buffer pointer */
4028 pcck_buf = (phy_sts_cck_819xpci_t *)prxpkt;
4029 pofdm_buf = (phy_sts_ofdm_819xpci_t *)prxpkt;
4031 pstats->RxMIMOSignalQuality[0] = -1;
4032 pstats->RxMIMOSignalQuality[1] = -1;
4033 precord_stats->RxMIMOSignalQuality[0] = -1;
4034 precord_stats->RxMIMOSignalQuality[1] = -1;
4036 if(is_cck_rate)
4039 // (1)Hardware does not provide RSSI for CCK
4043 // (2)PWDB, Average PWDB cacluated by hardware (for rate adaptive)
4045 u8 report;//, cck_agc_rpt;
4047 if (!priv->phy_reg824_bit9)
4049 report = pcck_buf->cck_agc_rpt & 0xc0;
4050 report = report>>6;
4051 switch(report)
4053 //Fixed by Jacken from Bryant 2008-03-20
4054 //Original value is -38 , -26 , -14 , -2
4055 //Fixed value is -35 , -23 , -11 , 6
4056 case 0x3:
4057 rx_pwr_all = -35 - (pcck_buf->cck_agc_rpt & 0x3e);
4058 break;
4059 case 0x2:
4060 rx_pwr_all = -23 - (pcck_buf->cck_agc_rpt & 0x3e);
4061 break;
4062 case 0x1:
4063 rx_pwr_all = -11 - (pcck_buf->cck_agc_rpt & 0x3e);
4064 break;
4065 case 0x0:
4066 rx_pwr_all = 8 - (pcck_buf->cck_agc_rpt & 0x3e);
4067 break;
4070 else
4072 report = pcck_buf->cck_agc_rpt & 0x60;
4073 report = report>>5;
4074 switch(report)
4076 case 0x3:
4077 rx_pwr_all = -35 - ((pcck_buf->cck_agc_rpt & 0x1f)<<1) ;
4078 break;
4079 case 0x2:
4080 rx_pwr_all = -23 - ((pcck_buf->cck_agc_rpt & 0x1f)<<1);
4081 break;
4082 case 0x1:
4083 rx_pwr_all = -11 - ((pcck_buf->cck_agc_rpt & 0x1f)<<1) ;
4084 break;
4085 case 0x0:
4086 rx_pwr_all = -8 - ((pcck_buf->cck_agc_rpt & 0x1f)<<1) ;
4087 break;
4091 pwdb_all = rtl819x_query_rxpwrpercentage(rx_pwr_all);
4092 pstats->RxPWDBAll = precord_stats->RxPWDBAll = pwdb_all;
4093 pstats->RecvSignalPower = rx_pwr_all;
4096 // (3) Get Signal Quality (EVM)
4098 if(bpacket_match_bssid)
4100 u8 sq;
4102 if(pstats->RxPWDBAll > 40)
4104 sq = 100;
4105 }else
4107 sq = pcck_buf->sq_rpt;
4109 if(pcck_buf->sq_rpt > 64)
4110 sq = 0;
4111 else if (pcck_buf->sq_rpt < 20)
4112 sq = 100;
4113 else
4114 sq = ((64-sq) * 100) / 44;
4116 pstats->SignalQuality = precord_stats->SignalQuality = sq;
4117 pstats->RxMIMOSignalQuality[0] = precord_stats->RxMIMOSignalQuality[0] = sq;
4118 pstats->RxMIMOSignalQuality[1] = precord_stats->RxMIMOSignalQuality[1] = -1;
4121 else
4124 // (1)Get RSSI for HT rate
4126 for(i=RF90_PATH_A; i<RF90_PATH_MAX; i++)
4128 // 2008/01/30 MH we will judge RF RX path now.
4129 if (priv->brfpath_rxenable[i])
4130 rf_rx_num++;
4131 //else
4132 //continue;
4134 //Fixed by Jacken from Bryant 2008-03-20
4135 //Original value is 106
4136 rx_pwr[i] = ((pofdm_buf->trsw_gain_X[i]&0x3F)*2) - 110;
4138 //Get Rx snr value in DB
4139 tmp_rxsnr = pofdm_buf->rxsnr_X[i];
4140 rx_snrX = (char)(tmp_rxsnr);
4141 rx_snrX /= 2;
4143 /* Translate DBM to percentage. */
4144 RSSI = rtl819x_query_rxpwrpercentage(rx_pwr[i]);
4145 if (priv->brfpath_rxenable[i])
4146 total_rssi += RSSI;
4148 /* Record Signal Strength for next packet */
4149 if(bpacket_match_bssid)
4151 pstats->RxMIMOSignalStrength[i] =(u8) RSSI;
4152 precord_stats->RxMIMOSignalStrength[i] =(u8) RSSI;
4158 // (2)PWDB, Average PWDB cacluated by hardware (for rate adaptive)
4160 //Fixed by Jacken from Bryant 2008-03-20
4161 //Original value is 106
4162 rx_pwr_all = (((pofdm_buf->pwdb_all ) >> 1 )& 0x7f) -106;
4163 pwdb_all = rtl819x_query_rxpwrpercentage(rx_pwr_all);
4165 pstats->RxPWDBAll = precord_stats->RxPWDBAll = pwdb_all;
4166 pstats->RxPower = precord_stats->RxPower = rx_pwr_all;
4167 pstats->RecvSignalPower = rx_pwr_all;
4169 // (3)EVM of HT rate
4171 if(pdrvinfo->RxHT && pdrvinfo->RxRate>=DESC90_RATEMCS8 &&
4172 pdrvinfo->RxRate<=DESC90_RATEMCS15)
4173 max_spatial_stream = 2; //both spatial stream make sense
4174 else
4175 max_spatial_stream = 1; //only spatial stream 1 makes sense
4177 for(i=0; i<max_spatial_stream; i++)
4179 tmp_rxevm = pofdm_buf->rxevm_X[i];
4180 rx_evmX = (char)(tmp_rxevm);
4182 // Do not use shift operation like "rx_evmX >>= 1" because the compilor of free build environment
4183 // fill most significant bit to "zero" when doing shifting operation which may change a negative
4184 // value to positive one, then the dbm value (which is supposed to be negative) is not correct anymore.
4185 rx_evmX /= 2; //dbm
4187 evm = rtl819x_evm_dbtopercentage(rx_evmX);
4188 if(bpacket_match_bssid)
4190 if(i==0) // Fill value in RFD, Get the first spatial stream only
4191 pstats->SignalQuality = precord_stats->SignalQuality = (u8)(evm & 0xff);
4192 pstats->RxMIMOSignalQuality[i] = precord_stats->RxMIMOSignalQuality[i] = (u8)(evm & 0xff);
4197 /* record rx statistics for debug */
4198 rxsc_sgien_exflg = pofdm_buf->rxsc_sgien_exflg;
4199 prxsc = (phy_ofdm_rx_status_rxsc_sgien_exintfflag *)&rxsc_sgien_exflg;
4202 //UI BSS List signal strength(in percentage), make it good looking, from 0~100.
4203 //It is assigned to the BSS List in GetValueFromBeaconOrProbeRsp().
4204 if(is_cck_rate)
4206 pstats->SignalStrength = precord_stats->SignalStrength = (u8)(rtl819x_signal_scale_mapping((long)pwdb_all));//PWDB_ALL;
4209 else
4211 //pRfd->Status.SignalStrength = pRecordRfd->Status.SignalStrength = (u1Byte)(SignalScaleMapping(total_rssi/=RF90_PATH_MAX));//(u1Byte)(total_rssi/=RF90_PATH_MAX);
4212 // We can judge RX path number now.
4213 if (rf_rx_num != 0)
4214 pstats->SignalStrength = precord_stats->SignalStrength = (u8)(rtl819x_signal_scale_mapping((long)(total_rssi/=rf_rx_num)));
4218 static void
4219 rtl8192_record_rxdesc_forlateruse(
4220 struct ieee80211_rx_stats * psrc_stats,
4221 struct ieee80211_rx_stats * ptarget_stats
4224 ptarget_stats->bIsAMPDU = psrc_stats->bIsAMPDU;
4225 ptarget_stats->bFirstMPDU = psrc_stats->bFirstMPDU;
4230 static void TranslateRxSignalStuff819xpci(struct r8192_priv *priv,
4231 struct sk_buff *skb,
4232 struct ieee80211_rx_stats * pstats,
4233 prx_desc_819x_pci pdesc,
4234 prx_fwinfo_819x_pci pdrvinfo)
4236 // TODO: We must only check packet for current MAC address. Not finish
4237 bool bpacket_match_bssid, bpacket_toself;
4238 bool bPacketBeacon=false, bToSelfBA=false;
4239 struct ieee80211_hdr_3addr *hdr;
4240 u16 fc,type;
4242 // Get Signal Quality for only RX data queue (but not command queue)
4244 u8* tmp_buf;
4245 u8 *praddr;
4247 /* Get MAC frame start address. */
4248 tmp_buf = skb->data;
4250 hdr = (struct ieee80211_hdr_3addr *)tmp_buf;
4251 fc = le16_to_cpu(hdr->frame_ctl);
4252 type = WLAN_FC_GET_TYPE(fc);
4253 praddr = hdr->addr1;
4255 /* Check if the received packet is acceptabe. */
4256 bpacket_match_bssid = ((IEEE80211_FTYPE_CTL != type) &&
4257 (!compare_ether_addr(priv->ieee80211->current_network.bssid, (fc & IEEE80211_FCTL_TODS)? hdr->addr1 : (fc & IEEE80211_FCTL_FROMDS )? hdr->addr2 : hdr->addr3))
4258 && (!pstats->bHwError) && (!pstats->bCRC)&& (!pstats->bICV));
4259 bpacket_toself = bpacket_match_bssid & (!compare_ether_addr(praddr, priv->ieee80211->dev->dev_addr));
4261 if(WLAN_FC_GET_FRAMETYPE(fc)== IEEE80211_STYPE_BEACON)
4263 bPacketBeacon = true;
4265 if(WLAN_FC_GET_FRAMETYPE(fc) == IEEE80211_STYPE_BLOCKACK)
4267 if (!compare_ether_addr(praddr, priv->ieee80211->dev->dev_addr))
4268 bToSelfBA = true;
4272 // Process PHY information for previous packet (RSSI/PWDB/EVM)
4274 // Because phy information is contained in the last packet of AMPDU only, so driver
4275 // should process phy information of previous packet
4276 rtl8192_process_phyinfo(priv, tmp_buf, &priv->previous_stats, pstats);
4277 rtl8192_query_rxphystatus(priv, pstats, pdesc, pdrvinfo, &priv->previous_stats, bpacket_match_bssid,
4278 bpacket_toself ,bPacketBeacon, bToSelfBA);
4279 rtl8192_record_rxdesc_forlateruse(pstats, &priv->previous_stats);
4284 static void rtl8192_tx_resume(struct r8192_priv *priv)
4286 struct ieee80211_device *ieee = priv->ieee80211;
4287 struct sk_buff *skb;
4288 int i;
4290 for (i = BK_QUEUE; i < TXCMD_QUEUE; i++) {
4291 while ((!skb_queue_empty(&ieee->skb_waitQ[i])) &&
4292 (priv->ieee80211->check_nic_enough_desc(ieee, i) > 0)) {
4293 /* 1. dequeue the packet from the wait queue */
4294 skb = skb_dequeue(&ieee->skb_waitQ[i]);
4295 /* 2. tx the packet directly */
4296 ieee->softmac_data_hard_start_xmit(skb, ieee, 0);
4301 static void rtl8192_irq_tx_tasklet(unsigned long arg)
4303 struct r8192_priv *priv = (struct r8192_priv*) arg;
4304 struct rtl8192_tx_ring *mgnt_ring = &priv->tx_ring[MGNT_QUEUE];
4305 unsigned long flags;
4307 /* check if we need to report that the management queue is drained */
4308 spin_lock_irqsave(&priv->irq_th_lock, flags);
4310 if (!skb_queue_len(&mgnt_ring->queue) &&
4311 priv->ieee80211->ack_tx_to_ieee &&
4312 rtl8192_is_tx_queue_empty(priv->ieee80211)) {
4313 priv->ieee80211->ack_tx_to_ieee = 0;
4314 ieee80211_ps_tx_ack(priv->ieee80211, 1);
4317 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
4319 rtl8192_tx_resume(priv);
4322 /* Record the received data rate */
4323 static void UpdateReceivedRateHistogramStatistics8190(
4324 struct r8192_priv *priv,
4325 struct ieee80211_rx_stats* pstats
4328 u32 rcvType=1; //0: Total, 1:OK, 2:CRC, 3:ICV
4329 u32 rateIndex;
4330 u32 preamble_guardinterval; //1: short preamble/GI, 0: long preamble/GI
4332 if(pstats->bCRC)
4333 rcvType = 2;
4334 else if(pstats->bICV)
4335 rcvType = 3;
4337 if(pstats->bShortPreamble)
4338 preamble_guardinterval = 1;// short
4339 else
4340 preamble_guardinterval = 0;// long
4342 switch(pstats->rate)
4345 // CCK rate
4347 case MGN_1M: rateIndex = 0; break;
4348 case MGN_2M: rateIndex = 1; break;
4349 case MGN_5_5M: rateIndex = 2; break;
4350 case MGN_11M: rateIndex = 3; break;
4352 // Legacy OFDM rate
4354 case MGN_6M: rateIndex = 4; break;
4355 case MGN_9M: rateIndex = 5; break;
4356 case MGN_12M: rateIndex = 6; break;
4357 case MGN_18M: rateIndex = 7; break;
4358 case MGN_24M: rateIndex = 8; break;
4359 case MGN_36M: rateIndex = 9; break;
4360 case MGN_48M: rateIndex = 10; break;
4361 case MGN_54M: rateIndex = 11; break;
4363 // 11n High throughput rate
4365 case MGN_MCS0: rateIndex = 12; break;
4366 case MGN_MCS1: rateIndex = 13; break;
4367 case MGN_MCS2: rateIndex = 14; break;
4368 case MGN_MCS3: rateIndex = 15; break;
4369 case MGN_MCS4: rateIndex = 16; break;
4370 case MGN_MCS5: rateIndex = 17; break;
4371 case MGN_MCS6: rateIndex = 18; break;
4372 case MGN_MCS7: rateIndex = 19; break;
4373 case MGN_MCS8: rateIndex = 20; break;
4374 case MGN_MCS9: rateIndex = 21; break;
4375 case MGN_MCS10: rateIndex = 22; break;
4376 case MGN_MCS11: rateIndex = 23; break;
4377 case MGN_MCS12: rateIndex = 24; break;
4378 case MGN_MCS13: rateIndex = 25; break;
4379 case MGN_MCS14: rateIndex = 26; break;
4380 case MGN_MCS15: rateIndex = 27; break;
4381 default: rateIndex = 28; break;
4383 priv->stats.received_rate_histogram[0][rateIndex]++; //total
4384 priv->stats.received_rate_histogram[rcvType][rateIndex]++;
4387 static void rtl8192_rx(struct r8192_priv *priv)
4389 struct ieee80211_hdr_1addr *ieee80211_hdr = NULL;
4390 bool unicast_packet = false;
4391 struct ieee80211_rx_stats stats = {
4392 .signal = 0,
4393 .noise = -98,
4394 .rate = 0,
4395 .freq = IEEE80211_24GHZ_BAND,
4397 unsigned int count = priv->rxringcount;
4398 prx_fwinfo_819x_pci pDrvInfo = NULL;
4399 struct sk_buff *new_skb;
4401 while (count--) {
4402 rx_desc_819x_pci *pdesc = &priv->rx_ring[priv->rx_idx];//rx descriptor
4403 struct sk_buff *skb = priv->rx_buf[priv->rx_idx];//rx pkt
4405 if (pdesc->OWN)
4406 /* wait data to be filled by hardware */
4407 return;
4409 stats.bICV = pdesc->ICV;
4410 stats.bCRC = pdesc->CRC32;
4411 stats.bHwError = pdesc->CRC32 | pdesc->ICV;
4413 stats.Length = pdesc->Length;
4414 if(stats.Length < 24)
4415 stats.bHwError |= 1;
4417 if(stats.bHwError) {
4418 stats.bShift = false;
4419 goto done;
4421 pDrvInfo = NULL;
4422 new_skb = dev_alloc_skb(priv->rxbuffersize);
4424 if (unlikely(!new_skb))
4425 goto done;
4427 stats.RxDrvInfoSize = pdesc->RxDrvInfoSize;
4428 stats.RxBufShift = ((pdesc->Shift)&0x03);
4429 stats.Decrypted = !pdesc->SWDec;
4431 pci_dma_sync_single_for_cpu(priv->pdev,
4432 *((dma_addr_t *)skb->cb),
4433 priv->rxbuffersize,
4434 PCI_DMA_FROMDEVICE);
4435 skb_put(skb, pdesc->Length);
4436 pDrvInfo = (rx_fwinfo_819x_pci *)(skb->data + stats.RxBufShift);
4437 skb_reserve(skb, stats.RxDrvInfoSize + stats.RxBufShift);
4439 stats.rate = HwRateToMRate90((bool)pDrvInfo->RxHT, (u8)pDrvInfo->RxRate);
4440 stats.bShortPreamble = pDrvInfo->SPLCP;
4442 /* it is debug only. It should be disabled in released driver.
4443 * 2007.1.11 by Emily
4444 * */
4445 UpdateReceivedRateHistogramStatistics8190(priv, &stats);
4447 stats.bIsAMPDU = (pDrvInfo->PartAggr==1);
4448 stats.bFirstMPDU = (pDrvInfo->PartAggr==1) && (pDrvInfo->FirstAGGR==1);
4450 stats.TimeStampLow = pDrvInfo->TSFL;
4451 stats.TimeStampHigh = read_nic_dword(priv, TSFR+4);
4453 UpdateRxPktTimeStamp8190(priv, &stats);
4456 // Get Total offset of MPDU Frame Body
4458 if((stats.RxBufShift + stats.RxDrvInfoSize) > 0)
4459 stats.bShift = 1;
4461 /* ???? */
4462 TranslateRxSignalStuff819xpci(priv, skb, &stats, pdesc, pDrvInfo);
4464 /* Rx A-MPDU */
4465 if(pDrvInfo->FirstAGGR==1 || pDrvInfo->PartAggr == 1)
4466 RT_TRACE(COMP_RXDESC, "pDrvInfo->FirstAGGR = %d, pDrvInfo->PartAggr = %d\n",
4467 pDrvInfo->FirstAGGR, pDrvInfo->PartAggr);
4468 skb_trim(skb, skb->len - 4/*sCrcLng*/);
4469 /* rx packets statistics */
4470 ieee80211_hdr = (struct ieee80211_hdr_1addr *)skb->data;
4471 unicast_packet = false;
4473 if(is_broadcast_ether_addr(ieee80211_hdr->addr1)) {
4474 //TODO
4475 }else if(is_multicast_ether_addr(ieee80211_hdr->addr1)){
4476 //TODO
4477 }else {
4478 /* unicast packet */
4479 unicast_packet = true;
4482 if(!ieee80211_rtl_rx(priv->ieee80211, skb, &stats)){
4483 dev_kfree_skb_any(skb);
4484 } else {
4485 priv->stats.rxok++;
4486 if(unicast_packet) {
4487 priv->stats.rxbytesunicast += skb->len;
4491 pci_unmap_single(priv->pdev, *((dma_addr_t *) skb->cb),
4492 priv->rxbuffersize, PCI_DMA_FROMDEVICE);
4494 skb = new_skb;
4495 priv->rx_buf[priv->rx_idx] = skb;
4496 *((dma_addr_t *) skb->cb) = pci_map_single(priv->pdev, skb_tail_pointer(skb), priv->rxbuffersize, PCI_DMA_FROMDEVICE);
4498 done:
4499 pdesc->BufferAddress = cpu_to_le32(*((dma_addr_t *)skb->cb));
4500 pdesc->OWN = 1;
4501 pdesc->Length = priv->rxbuffersize;
4502 if (priv->rx_idx == priv->rxringcount-1)
4503 pdesc->EOR = 1;
4504 priv->rx_idx = (priv->rx_idx + 1) % priv->rxringcount;
4509 static void rtl8192_irq_rx_tasklet(unsigned long arg)
4511 struct r8192_priv *priv = (struct r8192_priv*) arg;
4512 rtl8192_rx(priv);
4513 /* unmask RDU */
4514 write_nic_dword(priv, INTA_MASK, read_nic_dword(priv, INTA_MASK) | IMR_RDU);
4517 static const struct net_device_ops rtl8192_netdev_ops = {
4518 .ndo_open = rtl8192_open,
4519 .ndo_stop = rtl8192_close,
4520 .ndo_tx_timeout = tx_timeout,
4521 .ndo_do_ioctl = rtl8192_ioctl,
4522 .ndo_set_multicast_list = r8192_set_multicast,
4523 .ndo_set_mac_address = r8192_set_mac_adr,
4524 .ndo_start_xmit = ieee80211_rtl_xmit,
4527 static int __devinit rtl8192_pci_probe(struct pci_dev *pdev,
4528 const struct pci_device_id *id)
4530 struct net_device *dev = NULL;
4531 struct r8192_priv *priv= NULL;
4532 u8 unit = 0;
4533 int ret = -ENODEV;
4534 unsigned long pmem_start, pmem_len, pmem_flags;
4536 RT_TRACE(COMP_INIT,"Configuring chip resources\n");
4538 if( pci_enable_device (pdev) ){
4539 RT_TRACE(COMP_ERR,"Failed to enable PCI device");
4540 return -EIO;
4543 pci_set_master(pdev);
4544 //pci_set_wmi(pdev);
4545 pci_set_dma_mask(pdev, 0xffffff00ULL);
4546 pci_set_consistent_dma_mask(pdev,0xffffff00ULL);
4547 dev = alloc_ieee80211(sizeof(struct r8192_priv));
4548 if (!dev) {
4549 ret = -ENOMEM;
4550 goto fail_free;
4553 pci_set_drvdata(pdev, dev);
4554 SET_NETDEV_DEV(dev, &pdev->dev);
4555 priv = ieee80211_priv(dev);
4556 priv->ieee80211 = netdev_priv(dev);
4557 priv->pdev=pdev;
4558 if((pdev->subsystem_vendor == PCI_VENDOR_ID_DLINK)&&(pdev->subsystem_device == 0x3304)){
4559 priv->ieee80211->bSupportRemoteWakeUp = 1;
4560 } else
4562 priv->ieee80211->bSupportRemoteWakeUp = 0;
4565 pmem_start = pci_resource_start(pdev, 1);
4566 pmem_len = pci_resource_len(pdev, 1);
4567 pmem_flags = pci_resource_flags (pdev, 1);
4569 if (!(pmem_flags & IORESOURCE_MEM)) {
4570 RT_TRACE(COMP_ERR, "region #1 not a MMIO resource, aborting\n");
4571 goto fail;
4574 //DMESG("Memory mapped space @ 0x%08lx ", pmem_start);
4575 if( ! request_mem_region(pmem_start, pmem_len, RTL819xE_MODULE_NAME)) {
4576 RT_TRACE(COMP_ERR,"request_mem_region failed!\n");
4577 goto fail;
4580 priv->mem_start = ioremap_nocache(pmem_start, pmem_len);
4581 if (!priv->mem_start) {
4582 RT_TRACE(COMP_ERR,"ioremap failed!\n");
4583 goto fail1;
4586 dev->mem_start = (unsigned long) priv->mem_start;
4587 dev->mem_end = (unsigned long) (priv->mem_start +
4588 pci_resource_len(pdev, 0));
4590 /* We disable the RETRY_TIMEOUT register (0x41) to keep
4591 * PCI Tx retries from interfering with C3 CPU state */
4592 pci_write_config_byte(pdev, 0x41, 0x00);
4595 pci_read_config_byte(pdev, 0x05, &unit);
4596 pci_write_config_byte(pdev, 0x05, unit & (~0x04));
4598 dev->irq = pdev->irq;
4599 priv->irq = 0;
4601 dev->netdev_ops = &rtl8192_netdev_ops;
4603 dev->wireless_handlers = &r8192_wx_handlers_def;
4604 dev->type=ARPHRD_ETHER;
4606 dev->watchdog_timeo = HZ*3;
4608 if (dev_alloc_name(dev, ifname) < 0){
4609 RT_TRACE(COMP_INIT, "Oops: devname already taken! Trying wlan%%d...\n");
4610 strcpy(ifname, "wlan%d");
4611 dev_alloc_name(dev, ifname);
4614 RT_TRACE(COMP_INIT, "Driver probe completed1\n");
4615 if (rtl8192_init(priv)!=0) {
4616 RT_TRACE(COMP_ERR, "Initialization failed\n");
4617 goto fail;
4620 register_netdev(dev);
4621 RT_TRACE(COMP_INIT, "dev name=======> %s\n",dev->name);
4622 rtl8192_proc_init_one(priv);
4625 RT_TRACE(COMP_INIT, "Driver probe completed\n");
4626 return 0;
4628 fail1:
4630 if (priv->mem_start) {
4631 iounmap(priv->mem_start);
4632 release_mem_region( pci_resource_start(pdev, 1),
4633 pci_resource_len(pdev, 1) );
4636 fail:
4637 if(dev){
4639 if (priv->irq) {
4640 free_irq(priv->irq, priv);
4641 priv->irq = 0;
4643 free_ieee80211(dev);
4646 fail_free:
4647 pci_disable_device(pdev);
4649 DMESG("wlan driver load failed\n");
4650 pci_set_drvdata(pdev, NULL);
4651 return ret;
4655 /* detach all the work and timer structure declared or inititialized
4656 * in r8192_init function.
4657 * */
4658 static void rtl8192_cancel_deferred_work(struct r8192_priv* priv)
4660 /* call cancel_work_sync instead of cancel_delayed_work if and only if Linux_version_code
4661 * is or is newer than 2.6.20 and work structure is defined to be struct work_struct.
4662 * Otherwise call cancel_delayed_work is enough.
4663 * FIXME (2.6.20 should 2.6.22, work_struct should not cancel)
4664 * */
4665 cancel_delayed_work(&priv->watch_dog_wq);
4666 cancel_delayed_work(&priv->update_beacon_wq);
4667 cancel_delayed_work(&priv->ieee80211->hw_wakeup_wq);
4668 cancel_delayed_work(&priv->gpio_change_rf_wq);
4669 cancel_work_sync(&priv->reset_wq);
4670 cancel_work_sync(&priv->qos_activate);
4674 static void __devexit rtl8192_pci_disconnect(struct pci_dev *pdev)
4676 struct net_device *dev = pci_get_drvdata(pdev);
4677 struct r8192_priv *priv ;
4678 u32 i;
4680 if (dev) {
4682 unregister_netdev(dev);
4684 priv = ieee80211_priv(dev);
4686 rtl8192_proc_remove_one(priv);
4688 rtl8192_down(dev);
4689 if (priv->pFirmware)
4691 vfree(priv->pFirmware);
4692 priv->pFirmware = NULL;
4694 destroy_workqueue(priv->priv_wq);
4696 /* free tx/rx rings */
4697 rtl8192_free_rx_ring(priv);
4698 for (i = 0; i < MAX_TX_QUEUE_COUNT; i++)
4699 rtl8192_free_tx_ring(priv, i);
4701 if (priv->irq) {
4702 printk("Freeing irq %d\n", priv->irq);
4703 free_irq(priv->irq, priv);
4704 priv->irq = 0;
4707 if (priv->mem_start) {
4708 iounmap(priv->mem_start);
4709 release_mem_region( pci_resource_start(pdev, 1),
4710 pci_resource_len(pdev, 1) );
4713 free_ieee80211(dev);
4716 pci_disable_device(pdev);
4717 RT_TRACE(COMP_DOWN, "wlan driver removed\n");
4720 extern int ieee80211_rtl_init(void);
4721 extern void ieee80211_rtl_exit(void);
4723 static int __init rtl8192_pci_module_init(void)
4725 int retval;
4727 retval = ieee80211_rtl_init();
4728 if (retval)
4729 return retval;
4731 printk(KERN_INFO "\nLinux kernel driver for RTL8192 based WLAN cards\n");
4732 printk(KERN_INFO "Copyright (c) 2007-2008, Realsil Wlan\n");
4733 RT_TRACE(COMP_INIT, "Initializing module\n");
4734 rtl8192_proc_module_init();
4735 if(0!=pci_register_driver(&rtl8192_pci_driver))
4737 DMESG("No device found");
4738 /*pci_unregister_driver (&rtl8192_pci_driver);*/
4739 return -ENODEV;
4741 return 0;
4745 static void __exit rtl8192_pci_module_exit(void)
4747 pci_unregister_driver(&rtl8192_pci_driver);
4749 RT_TRACE(COMP_DOWN, "Exiting\n");
4750 rtl8192_proc_module_remove();
4751 ieee80211_rtl_exit();
4754 static irqreturn_t rtl8192_interrupt(int irq, void *param)
4756 struct r8192_priv *priv = param;
4757 struct net_device *dev = priv->ieee80211->dev;
4758 unsigned long flags;
4759 u32 inta;
4760 irqreturn_t ret = IRQ_HANDLED;
4762 spin_lock_irqsave(&priv->irq_th_lock, flags);
4764 /* ISR: 4bytes */
4766 inta = read_nic_dword(priv, ISR); /* & priv->IntrMask; */
4767 write_nic_dword(priv, ISR, inta); /* reset int situation */
4769 if (!inta) {
4771 * most probably we can safely return IRQ_NONE,
4772 * but for now is better to avoid problems
4774 goto out_unlock;
4777 if (inta == 0xffff) {
4778 /* HW disappared */
4779 goto out_unlock;
4782 if (!netif_running(dev))
4783 goto out_unlock;
4785 if (inta & IMR_TBDOK) {
4786 RT_TRACE(COMP_INTR, "beacon ok interrupt!\n");
4787 rtl8192_tx_isr(priv, BEACON_QUEUE);
4788 priv->stats.txbeaconokint++;
4791 if (inta & IMR_TBDER) {
4792 RT_TRACE(COMP_INTR, "beacon ok interrupt!\n");
4793 rtl8192_tx_isr(priv, BEACON_QUEUE);
4794 priv->stats.txbeaconerr++;
4797 if (inta & IMR_MGNTDOK ) {
4798 RT_TRACE(COMP_INTR, "Manage ok interrupt!\n");
4799 priv->stats.txmanageokint++;
4800 rtl8192_tx_isr(priv, MGNT_QUEUE);
4803 if (inta & IMR_COMDOK)
4805 priv->stats.txcmdpktokint++;
4806 rtl8192_tx_isr(priv, TXCMD_QUEUE);
4809 if (inta & IMR_ROK) {
4810 priv->stats.rxint++;
4811 tasklet_schedule(&priv->irq_rx_tasklet);
4814 if (inta & IMR_BcnInt) {
4815 RT_TRACE(COMP_INTR, "prepare beacon for interrupt!\n");
4816 tasklet_schedule(&priv->irq_prepare_beacon_tasklet);
4819 if (inta & IMR_RDU) {
4820 RT_TRACE(COMP_INTR, "rx descriptor unavailable!\n");
4821 priv->stats.rxrdu++;
4822 /* reset int situation */
4823 write_nic_dword(priv, INTA_MASK, read_nic_dword(priv, INTA_MASK) & ~IMR_RDU);
4824 tasklet_schedule(&priv->irq_rx_tasklet);
4827 if (inta & IMR_RXFOVW) {
4828 RT_TRACE(COMP_INTR, "rx overflow !\n");
4829 priv->stats.rxoverflow++;
4830 tasklet_schedule(&priv->irq_rx_tasklet);
4833 if (inta & IMR_TXFOVW)
4834 priv->stats.txoverflow++;
4836 if (inta & IMR_BKDOK) {
4837 RT_TRACE(COMP_INTR, "BK Tx OK interrupt!\n");
4838 priv->stats.txbkokint++;
4839 priv->ieee80211->LinkDetectInfo.NumTxOkInPeriod++;
4840 rtl8192_tx_isr(priv, BK_QUEUE);
4843 if (inta & IMR_BEDOK) {
4844 RT_TRACE(COMP_INTR, "BE TX OK interrupt!\n");
4845 priv->stats.txbeokint++;
4846 priv->ieee80211->LinkDetectInfo.NumTxOkInPeriod++;
4847 rtl8192_tx_isr(priv, BE_QUEUE);
4850 if (inta & IMR_VIDOK) {
4851 RT_TRACE(COMP_INTR, "VI TX OK interrupt!\n");
4852 priv->stats.txviokint++;
4853 priv->ieee80211->LinkDetectInfo.NumTxOkInPeriod++;
4854 rtl8192_tx_isr(priv, VI_QUEUE);
4857 if (inta & IMR_VODOK) {
4858 priv->stats.txvookint++;
4859 priv->ieee80211->LinkDetectInfo.NumTxOkInPeriod++;
4860 rtl8192_tx_isr(priv, VO_QUEUE);
4863 out_unlock:
4864 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
4866 return ret;
4869 void EnableHWSecurityConfig8192(struct r8192_priv *priv)
4871 u8 SECR_value = 0x0;
4872 struct ieee80211_device* ieee = priv->ieee80211;
4874 SECR_value = SCR_TxEncEnable | SCR_RxDecEnable;
4876 if (((KEY_TYPE_WEP40 == ieee->pairwise_key_type) || (KEY_TYPE_WEP104 == ieee->pairwise_key_type)) && (priv->ieee80211->auth_mode != 2))
4878 SECR_value |= SCR_RxUseDK;
4879 SECR_value |= SCR_TxUseDK;
4881 else if ((ieee->iw_mode == IW_MODE_ADHOC) && (ieee->pairwise_key_type & (KEY_TYPE_CCMP | KEY_TYPE_TKIP)))
4883 SECR_value |= SCR_RxUseDK;
4884 SECR_value |= SCR_TxUseDK;
4887 //add HWSec active enable here.
4888 //default using hwsec. when peer AP is in N mode only and pairwise_key_type is none_aes(which HT_IOT_ACT_PURE_N_MODE indicates it), use software security. when peer AP is in b,g,n mode mixed and pairwise_key_type is none_aes, use g mode hw security. WB on 2008.7.4
4889 ieee->hwsec_active = 1;
4891 if ((ieee->pHTInfo->IOTAction&HT_IOT_ACT_PURE_N_MODE) || !hwwep)//!ieee->hwsec_support) //add hwsec_support flag to totol control hw_sec on/off
4893 ieee->hwsec_active = 0;
4894 SECR_value &= ~SCR_RxDecEnable;
4897 RT_TRACE(COMP_SEC,"%s:, hwsec:%d, pairwise_key:%d, SECR_value:%x\n", __FUNCTION__,
4898 ieee->hwsec_active, ieee->pairwise_key_type, SECR_value);
4900 write_nic_byte(priv, SECR, SECR_value);//SECR_value | SCR_UseDK );
4904 #define TOTAL_CAM_ENTRY 32
4905 //#define CAM_CONTENT_COUNT 8
4906 void setKey(struct r8192_priv *priv, u8 EntryNo, u8 KeyIndex, u16 KeyType,
4907 const u8 *MacAddr, u8 DefaultKey, u32 *KeyContent)
4909 u32 TargetCommand = 0;
4910 u32 TargetContent = 0;
4911 u16 usConfig = 0;
4912 u8 i;
4913 #ifdef ENABLE_IPS
4914 RT_RF_POWER_STATE rtState;
4916 rtState = priv->eRFPowerState;
4917 if (priv->PowerSaveControl.bInactivePs){
4918 if(rtState == eRfOff){
4919 if(priv->RfOffReason > RF_CHANGE_BY_IPS)
4921 RT_TRACE(COMP_ERR, "%s(): RF is OFF.\n",__FUNCTION__);
4922 //up(&priv->wx_sem);
4923 return ;
4925 else{
4926 down(&priv->ieee80211->ips_sem);
4927 IPSLeave(priv);
4928 up(&priv->ieee80211->ips_sem);
4932 priv->ieee80211->is_set_key = true;
4933 #endif
4934 if (EntryNo >= TOTAL_CAM_ENTRY)
4935 RT_TRACE(COMP_ERR, "cam entry exceeds in setKey()\n");
4937 RT_TRACE(COMP_SEC, "====>to setKey(), priv:%p, EntryNo:%d, KeyIndex:%d, KeyType:%d, MacAddr%pM\n", priv, EntryNo, KeyIndex, KeyType, MacAddr);
4939 if (DefaultKey)
4940 usConfig |= BIT15 | (KeyType<<2);
4941 else
4942 usConfig |= BIT15 | (KeyType<<2) | KeyIndex;
4943 // usConfig |= BIT15 | (KeyType<<2) | (DefaultKey<<5) | KeyIndex;
4946 for(i=0 ; i<CAM_CONTENT_COUNT; i++){
4947 TargetCommand = i+CAM_CONTENT_COUNT*EntryNo;
4948 TargetCommand |= BIT31|BIT16;
4950 if(i==0){//MAC|Config
4951 TargetContent = (u32)(*(MacAddr+0)) << 16|
4952 (u32)(*(MacAddr+1)) << 24|
4953 (u32)usConfig;
4955 write_nic_dword(priv, WCAMI, TargetContent);
4956 write_nic_dword(priv, RWCAM, TargetCommand);
4958 else if(i==1){//MAC
4959 TargetContent = (u32)(*(MacAddr+2)) |
4960 (u32)(*(MacAddr+3)) << 8|
4961 (u32)(*(MacAddr+4)) << 16|
4962 (u32)(*(MacAddr+5)) << 24;
4963 write_nic_dword(priv, WCAMI, TargetContent);
4964 write_nic_dword(priv, RWCAM, TargetCommand);
4966 else { //Key Material
4967 if(KeyContent != NULL)
4969 write_nic_dword(priv, WCAMI, (u32)(*(KeyContent+i-2)) );
4970 write_nic_dword(priv, RWCAM, TargetCommand);
4974 RT_TRACE(COMP_SEC,"=========>after set key, usconfig:%x\n", usConfig);
4977 bool NicIFEnableNIC(struct r8192_priv *priv)
4979 RT_STATUS init_status = RT_STATUS_SUCCESS;
4980 PRT_POWER_SAVE_CONTROL pPSC = &priv->PowerSaveControl;
4982 //YJ,add,091109
4983 if (priv->up == 0){
4984 RT_TRACE(COMP_ERR, "ERR!!! %s(): Driver is already down!\n",__FUNCTION__);
4985 priv->bdisable_nic = false; //YJ,add,091111
4986 return false;
4988 // <1> Reset memory: descriptor, buffer,..
4989 //NicIFResetMemory(Adapter);
4991 // <2> Enable Adapter
4992 //priv->bfirst_init = true;
4993 init_status = rtl8192_adapter_start(priv);
4994 if (init_status != RT_STATUS_SUCCESS) {
4995 RT_TRACE(COMP_ERR,"ERR!!! %s(): initialization is failed!\n",__FUNCTION__);
4996 priv->bdisable_nic = false; //YJ,add,091111
4997 return -1;
4999 RT_CLEAR_PS_LEVEL(pPSC, RT_RF_OFF_LEVL_HALT_NIC);
5000 //priv->bfirst_init = false;
5002 // <3> Enable Interrupt
5003 rtl8192_irq_enable(priv);
5004 priv->bdisable_nic = false;
5006 return (init_status == RT_STATUS_SUCCESS);
5009 bool NicIFDisableNIC(struct r8192_priv *priv)
5011 bool status = true;
5012 u8 tmp_state = 0;
5013 // <1> Disable Interrupt
5015 priv->bdisable_nic = true; //YJ,move,091109
5016 tmp_state = priv->ieee80211->state;
5018 ieee80211_softmac_stop_protocol(priv->ieee80211, false);
5020 priv->ieee80211->state = tmp_state;
5021 rtl8192_cancel_deferred_work(priv);
5022 rtl8192_irq_disable(priv);
5023 // <2> Stop all timer
5025 // <3> Disable Adapter
5026 rtl8192_halt_adapter(priv, false);
5027 // priv->bdisable_nic = true;
5029 return status;
5032 module_init(rtl8192_pci_module_init);
5033 module_exit(rtl8192_pci_module_exit);