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
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"
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"
46 #include "ieee80211/dot11d.h"
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) = {
54 { PCI_DEVICE(0x10ec, 0x8192) },
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 */
91 .suspend = rtl8192E_suspend, /* PM suspend fn */
92 .resume = rtl8192E_resume, /* PM resume fn */
94 .suspend = NULL, /* PM suspend fn */
95 .resume = NULL, /* PM resume fn */
99 static void rtl8192_start_beacon(struct net_device *dev);
100 static void rtl8192_stop_beacon(struct net_device *dev);
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 *netdev);
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);
116 typedef struct _CHANNEL_LIST
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:
154 ieee->bGlobalDomain = false;
155 //acturally 8225 & 8256 rf chip only support B,G,24N mode
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)
167 GET_DOT11D_INFO(ieee)->channel_map[ChannelPlan[channel_plan].Channel[i]] = 1;
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
176 ieee->bGlobalDomain = true;
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) &&
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 struct net_device *dev = priv->ieee80211->dev;
214 return 0xff&readb((u8*)dev->mem_start +x);
217 u32 read_nic_dword(struct r8192_priv *priv, int x)
219 struct net_device *dev = priv->ieee80211->dev;
220 return readl((u8*)dev->mem_start +x);
223 u16 read_nic_word(struct r8192_priv *priv, int x)
225 struct net_device *dev = priv->ieee80211->dev;
226 return readw((u8*)dev->mem_start +x);
229 void write_nic_byte(struct r8192_priv *priv, int x,u8 y)
231 struct net_device *dev = priv->ieee80211->dev;
232 writeb(y,(u8*)dev->mem_start +x);
236 void write_nic_dword(struct r8192_priv *priv, int x,u32 y)
238 struct net_device *dev = priv->ieee80211->dev;
239 writel(y,(u8*)dev->mem_start +x);
243 void write_nic_word(struct r8192_priv *priv, int x,u16 y)
245 struct net_device *dev = priv->ieee80211->dev;
246 writew(y,(u8*)dev->mem_start +x);
250 u8 rtl8192e_ap_sec_type(struct ieee80211_device *ieee)
252 static const u8 ccmp_ie[4] = {0x00,0x50,0xf2,0x04};
253 static const u8 ccmp_rsn_ie[4] = {0x00, 0x0f, 0xac, 0x04};
254 int wpa_ie_len= ieee->wpa_ie_len;
255 struct ieee80211_crypt_data* crypt;
258 crypt = ieee->crypt[ieee->tx_keyidx];
260 encrypt = (ieee->current_network.capability & WLAN_CAPABILITY_PRIVACY) ||
261 (ieee->host_encrypt && crypt && crypt->ops &&
262 (0 == strcmp(crypt->ops->name,"WEP")));
265 if(encrypt && (wpa_ie_len == 0)) {
266 // wep encryption, no N mode setting */
268 } else if((wpa_ie_len != 0)) {
269 // parse pairwise key type */
270 if (((ieee->wpa_ie[0] == 0xdd) && (!memcmp(&(ieee->wpa_ie[14]),ccmp_ie,4))) ||
271 ((ieee->wpa_ie[0] == 0x30) && (!memcmp(&ieee->wpa_ie[10],ccmp_rsn_ie, 4))))
281 rtl8192e_SetHwReg(struct net_device *dev,u8 variable,u8* val)
283 struct r8192_priv* priv = ieee80211_priv(dev);
289 write_nic_dword(priv, BSSIDR, ((u32*)(val))[0]);
290 write_nic_word(priv, BSSIDR+2, ((u16*)(val+2))[0]);
293 case HW_VAR_MEDIA_STATUS:
295 RT_OP_MODE OpMode = *((RT_OP_MODE *)(val));
296 u8 btMsr = read_nic_byte(priv, MSR);
302 case RT_OP_MODE_INFRASTRUCTURE:
306 case RT_OP_MODE_IBSS:
319 write_nic_byte(priv, MSR, btMsr);
323 case HW_VAR_CHECK_BSSID:
327 Type = ((u8*)(val))[0];
328 RegRCR = read_nic_dword(priv, RCR);
329 priv->ReceiveConfig = RegRCR;
332 RegRCR |= (RCR_CBSSID);
333 else if (Type == false)
334 RegRCR &= (~RCR_CBSSID);
336 write_nic_dword(priv, RCR,RegRCR);
337 priv->ReceiveConfig = RegRCR;
342 case HW_VAR_SLOT_TIME:
344 priv->slot_time = val[0];
345 write_nic_byte(priv, SLOT_TIME, val[0]);
350 case HW_VAR_ACK_PREAMBLE:
353 priv->short_preamble = (bool)(*(u8*)val );
354 regTmp = priv->basic_rate;
355 if (priv->short_preamble)
356 regTmp |= BRSR_AckShortPmb;
357 write_nic_dword(priv, RRSR, regTmp);
362 write_nic_dword(priv, CPU_GEN, ((u32*)(val))[0]);
371 static struct proc_dir_entry *rtl8192_proc = NULL;
373 static int proc_get_stats_ap(char *page, char **start,
374 off_t offset, int count,
375 int *eof, void *data)
377 struct net_device *dev = data;
378 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
379 struct ieee80211_device *ieee = priv->ieee80211;
380 struct ieee80211_network *target;
383 list_for_each_entry(target, &ieee->network_list, list) {
385 len += snprintf(page + len, count - len,
386 "%s ", target->ssid);
388 if(target->wpa_ie_len>0 || target->rsn_ie_len>0){
389 len += snprintf(page + len, count - len,
393 len += snprintf(page + len, count - len,
403 static int proc_get_registers(char *page, char **start,
404 off_t offset, int count,
405 int *eof, void *data)
407 struct net_device *dev = data;
408 struct r8192_priv *priv = ieee80211_priv(dev);
413 /* This dump the current register page */
414 len += snprintf(page + len, count - len,
415 "\n####################page 0##################\n ");
419 len += snprintf(page + len, count - len,
422 for(i=0;i<16 && n<=max;i++,n++)
423 len += snprintf(page + len, count - len,
424 "%2x ",read_nic_byte(priv,n));
426 len += snprintf(page + len, count - len,"\n");
427 len += snprintf(page + len, count - len,
428 "\n####################page 1##################\n ");
431 len += snprintf(page + len, count - len,
434 for(i=0;i<16 && n<=max;i++,n++)
435 len += snprintf(page + len, count - len,
436 "%2x ",read_nic_byte(priv,0x100|n));
439 len += snprintf(page + len, count - len,
440 "\n####################page 3##################\n ");
443 len += snprintf(page + len, count - len,
446 for(i=0;i<16 && n<=max;i++,n++)
447 len += snprintf(page + len, count - len,
448 "%2x ",read_nic_byte(priv,0x300|n));
456 static int proc_get_stats_tx(char *page, char **start,
457 off_t offset, int count,
458 int *eof, void *data)
460 struct net_device *dev = data;
461 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
465 len += snprintf(page + len, count - len,
466 "TX VI priority ok int: %lu\n"
467 "TX VO priority ok int: %lu\n"
468 "TX BE priority ok int: %lu\n"
469 "TX BK priority ok int: %lu\n"
470 "TX MANAGE priority ok int: %lu\n"
471 "TX BEACON priority ok int: %lu\n"
472 "TX BEACON priority error int: %lu\n"
473 "TX CMDPKT priority ok int: %lu\n"
474 "TX queue stopped?: %d\n"
475 "TX fifo overflow: %lu\n"
476 "TX total data packets %lu\n"
477 "TX total data bytes :%lu\n",
478 priv->stats.txviokint,
479 priv->stats.txvookint,
480 priv->stats.txbeokint,
481 priv->stats.txbkokint,
482 priv->stats.txmanageokint,
483 priv->stats.txbeaconokint,
484 priv->stats.txbeaconerr,
485 priv->stats.txcmdpktokint,
486 netif_queue_stopped(dev),
487 priv->stats.txoverflow,
488 priv->ieee80211->stats.tx_packets,
489 priv->ieee80211->stats.tx_bytes);
497 static int proc_get_stats_rx(char *page, char **start,
498 off_t offset, int count,
499 int *eof, void *data)
501 struct net_device *dev = data;
502 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
506 len += snprintf(page + len, count - len,
509 "RX rx overflow error: %lu\n",
512 priv->stats.rxoverflow);
518 static void rtl8192_proc_module_init(void)
520 RT_TRACE(COMP_INIT, "Initializing proc filesystem\n");
521 rtl8192_proc=create_proc_entry(RTL819xE_MODULE_NAME, S_IFDIR, init_net.proc_net);
525 static void rtl8192_proc_module_remove(void)
527 remove_proc_entry(RTL819xE_MODULE_NAME, init_net.proc_net);
531 static void rtl8192_proc_remove_one(struct r8192_priv *priv)
533 struct net_device *dev = priv->ieee80211->dev;
535 printk("dev name=======> %s\n",dev->name);
538 remove_proc_entry("stats-tx", priv->dir_dev);
539 remove_proc_entry("stats-rx", priv->dir_dev);
540 remove_proc_entry("stats-ap", priv->dir_dev);
541 remove_proc_entry("registers", priv->dir_dev);
542 remove_proc_entry("wlan0", rtl8192_proc);
543 priv->dir_dev = NULL;
548 static void rtl8192_proc_init_one(struct r8192_priv *priv)
550 struct net_device *dev = priv->ieee80211->dev;
551 struct proc_dir_entry *e;
553 priv->dir_dev = create_proc_entry(dev->name,
554 S_IFDIR | S_IRUGO | S_IXUGO,
556 if (!priv->dir_dev) {
557 RT_TRACE(COMP_ERR, "Unable to initialize /proc/net/rtl8192/%s\n",
561 e = create_proc_read_entry("stats-rx", S_IFREG | S_IRUGO,
562 priv->dir_dev, proc_get_stats_rx, dev);
565 RT_TRACE(COMP_ERR,"Unable to initialize "
566 "/proc/net/rtl8192/%s/stats-rx\n",
571 e = create_proc_read_entry("stats-tx", S_IFREG | S_IRUGO,
572 priv->dir_dev, proc_get_stats_tx, dev);
575 RT_TRACE(COMP_ERR, "Unable to initialize "
576 "/proc/net/rtl8192/%s/stats-tx\n",
580 e = create_proc_read_entry("stats-ap", S_IFREG | S_IRUGO,
581 priv->dir_dev, proc_get_stats_ap, dev);
584 RT_TRACE(COMP_ERR, "Unable to initialize "
585 "/proc/net/rtl8192/%s/stats-ap\n",
589 e = create_proc_read_entry("registers", S_IFREG | S_IRUGO,
590 priv->dir_dev, proc_get_registers, dev);
592 RT_TRACE(COMP_ERR, "Unable to initialize "
593 "/proc/net/rtl8192/%s/registers\n",
598 static short check_nic_enough_desc(struct net_device *dev, int prio)
600 struct r8192_priv *priv = ieee80211_priv(dev);
601 struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
603 /* for now we reserve two free descriptor as a safety boundary
604 * between the tail and the head
606 return (ring->entries - skb_queue_len(&ring->queue) >= 2);
609 static void tx_timeout(struct net_device *dev)
611 struct r8192_priv *priv = ieee80211_priv(dev);
613 schedule_work(&priv->reset_wq);
617 static void rtl8192_irq_enable(struct r8192_priv *priv)
621 mask = IMR_ROK | IMR_VODOK | IMR_VIDOK | IMR_BEDOK | IMR_BKDOK |
622 IMR_HCCADOK | IMR_MGNTDOK | IMR_COMDOK | IMR_HIGHDOK |
623 IMR_BDOK | IMR_RXCMDOK | IMR_TIMEOUT0 | IMR_RDU | IMR_RXFOVW |
624 IMR_TXFOVW | IMR_BcnInt | IMR_TBDOK | IMR_TBDER;
626 write_nic_dword(priv, INTA_MASK, mask);
629 static void rtl8192_irq_disable(struct r8192_priv *priv)
631 write_nic_dword(priv, INTA_MASK, 0);
632 synchronize_irq(priv->irq);
635 static void rtl8192_update_msr(struct r8192_priv *priv)
639 msr = read_nic_byte(priv, MSR);
640 msr &= ~ MSR_LINK_MASK;
642 /* do not change in link_state != WLAN_LINK_ASSOCIATED.
643 * msr must be updated if the state is ASSOCIATING.
644 * this is intentional and make sense for ad-hoc and
645 * master (see the create BSS/IBSS func)
647 if (priv->ieee80211->state == IEEE80211_LINKED){
649 if (priv->ieee80211->iw_mode == IW_MODE_INFRA)
650 msr |= (MSR_LINK_MANAGED<<MSR_LINK_SHIFT);
651 else if (priv->ieee80211->iw_mode == IW_MODE_ADHOC)
652 msr |= (MSR_LINK_ADHOC<<MSR_LINK_SHIFT);
653 else if (priv->ieee80211->iw_mode == IW_MODE_MASTER)
654 msr |= (MSR_LINK_MASTER<<MSR_LINK_SHIFT);
657 msr |= (MSR_LINK_NONE<<MSR_LINK_SHIFT);
659 write_nic_byte(priv, MSR, msr);
662 static void rtl8192_set_chan(struct net_device *dev,short ch)
664 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
668 /* need to implement rf set channel here WB */
670 if (priv->rf_set_chan)
671 priv->rf_set_chan(dev, priv->chan);
674 static void rtl8192_rx_enable(struct r8192_priv *priv)
676 write_nic_dword(priv, RDQDA, priv->rx_ring_dma);
679 /* the TX_DESC_BASE setting is according to the following queue index
688 * BEACON_QUEUE ===> 8
690 static const u32 TX_DESC_BASE[] = {BKQDA, BEQDA, VIQDA, VOQDA, HCCAQDA, CQDA, MQDA, HQDA, BQDA};
691 static void rtl8192_tx_enable(struct r8192_priv *priv)
695 for (i = 0; i < MAX_TX_QUEUE_COUNT; i++)
696 write_nic_dword(priv, TX_DESC_BASE[i], priv->tx_ring[i].dma);
698 ieee80211_reset_queue(priv->ieee80211);
702 static void rtl8192_free_rx_ring(struct r8192_priv *priv)
706 for (i = 0; i < priv->rxringcount; i++) {
707 struct sk_buff *skb = priv->rx_buf[i];
711 pci_unmap_single(priv->pdev,
712 *((dma_addr_t *)skb->cb),
713 priv->rxbuffersize, PCI_DMA_FROMDEVICE);
717 pci_free_consistent(priv->pdev, sizeof(*priv->rx_ring) * priv->rxringcount,
718 priv->rx_ring, priv->rx_ring_dma);
719 priv->rx_ring = NULL;
722 static void rtl8192_free_tx_ring(struct r8192_priv *priv, unsigned int prio)
724 struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
726 while (skb_queue_len(&ring->queue)) {
727 tx_desc_819x_pci *entry = &ring->desc[ring->idx];
728 struct sk_buff *skb = __skb_dequeue(&ring->queue);
730 pci_unmap_single(priv->pdev, le32_to_cpu(entry->TxBuffAddr),
731 skb->len, PCI_DMA_TODEVICE);
733 ring->idx = (ring->idx + 1) % ring->entries;
736 pci_free_consistent(priv->pdev, sizeof(*ring->desc)*ring->entries,
737 ring->desc, ring->dma);
741 void PHY_SetRtl8192eRfOff(struct r8192_priv *priv)
743 //disable RF-Chip A/B
744 rtl8192_setBBreg(priv, rFPGA0_XA_RFInterfaceOE, BIT4, 0x0);
745 //analog to digital off, for power save
746 rtl8192_setBBreg(priv, rFPGA0_AnalogParameter4, 0x300, 0x0);
747 //digital to analog off, for power save
748 rtl8192_setBBreg(priv, rFPGA0_AnalogParameter1, 0x18, 0x0);
750 rtl8192_setBBreg(priv, rOFDM0_TRxPathEnable, 0xf, 0x0);
752 rtl8192_setBBreg(priv, rOFDM1_TRxPathEnable, 0xf, 0x0);
753 //analog to digital part2 off, for power save
754 rtl8192_setBBreg(priv, rFPGA0_AnalogParameter1, 0x60, 0x0);
755 rtl8192_setBBreg(priv, rFPGA0_AnalogParameter1, 0x4, 0x0);
756 // Analog parameter!!Change bias and Lbus control.
757 write_nic_byte(priv, ANAPAR_FOR_8192PciE, 0x07);
760 static void rtl8192_halt_adapter(struct r8192_priv *priv, bool reset)
762 struct net_device *dev = priv->ieee80211->dev;
767 OpMode = RT_OP_MODE_NO_LINK;
768 priv->ieee80211->SetHwRegHandler(dev, HW_VAR_MEDIA_STATUS, &OpMode);
770 if (!priv->ieee80211->bSupportRemoteWakeUp) {
772 * disable tx/rx. In 8185 we write 0x10 (Reset bit),
773 * but here we make reference to WMAC and wirte 0x0
775 write_nic_byte(priv, CMDR, 0);
783 priv->bHwRfOffAction = 2;
786 * Call MgntActSet_RF_State instead to
787 * prevent RF config race condition.
789 if (!priv->ieee80211->bSupportRemoteWakeUp) {
790 PHY_SetRtl8192eRfOff(priv);
791 ulRegRead = read_nic_dword(priv, CPU_GEN);
792 ulRegRead |= CPU_GEN_SYSTEM_RESET;
793 write_nic_dword(priv,CPU_GEN, ulRegRead);
796 write_nic_dword(priv, WFCRC0, 0xffffffff);
797 write_nic_dword(priv, WFCRC1, 0xffffffff);
798 write_nic_dword(priv, WFCRC2, 0xffffffff);
800 /* Write PMR register */
801 write_nic_byte(priv, PMR, 0x5);
802 /* Disable tx, enanble rx */
803 write_nic_byte(priv, MacBlkCtrl, 0xa);
807 for(i = 0; i < MAX_QUEUE_SIZE; i++) {
808 skb_queue_purge(&priv->ieee80211->skb_waitQ [i]);
810 for(i = 0; i < MAX_QUEUE_SIZE; i++) {
811 skb_queue_purge(&priv->ieee80211->skb_aggQ [i]);
814 skb_queue_purge(&priv->skb_queue);
817 static void rtl8192_data_hard_stop(struct net_device *dev)
821 static void rtl8192_data_hard_resume(struct net_device *dev)
826 * this function TX data frames when the ieee80211 stack requires this.
827 * It checks also if we need to stop the ieee tx queue, eventually do it
829 static void rtl8192_hard_data_xmit(struct sk_buff *skb, struct net_device *dev, int rate)
831 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
833 cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
834 u8 queue_index = tcb_desc->queue_index;
836 /* shall not be referred by command packet */
837 BUG_ON(queue_index == TXCMD_QUEUE);
839 if (priv->bHwRadioOff || (!priv->up))
845 memcpy(skb->cb, &dev, sizeof(dev));
847 skb_push(skb, priv->ieee80211->tx_headroom);
848 ret = rtl8192_tx(priv, skb);
853 if (queue_index != MGNT_QUEUE) {
854 priv->ieee80211->stats.tx_bytes += (skb->len - priv->ieee80211->tx_headroom);
855 priv->ieee80211->stats.tx_packets++;
860 * This is a rough attempt to TX a frame
861 * This is called by the ieee 80211 stack to TX management frames.
862 * If the ring is full packet are dropped (for data frame the queue
863 * is stopped before this can happen).
865 static int rtl8192_hard_start_xmit(struct sk_buff *skb,struct net_device *dev)
867 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
869 cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
870 u8 queue_index = tcb_desc->queue_index;
872 if (queue_index != TXCMD_QUEUE) {
873 if (priv->bHwRadioOff || (!priv->up))
880 memcpy(skb->cb, &dev, sizeof(dev));
881 if (queue_index == TXCMD_QUEUE) {
882 rtl819xE_tx_cmd(priv, skb);
886 tcb_desc->RATRIndex = 7;
887 tcb_desc->bTxDisableRateFallBack = 1;
888 tcb_desc->bTxUseDriverAssingedRate = 1;
889 tcb_desc->bTxEnableFwCalcDur = 1;
890 skb_push(skb, priv->ieee80211->tx_headroom);
891 ret = rtl8192_tx(priv, skb);
901 static void rtl8192_tx_isr(struct r8192_priv *priv, int prio)
903 struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
905 while (skb_queue_len(&ring->queue)) {
906 tx_desc_819x_pci *entry = &ring->desc[ring->idx];
910 * beacon packet will only use the first descriptor defaultly,
911 * and the OWN may not be cleared by the hardware
913 if (prio != BEACON_QUEUE) {
916 ring->idx = (ring->idx + 1) % ring->entries;
919 skb = __skb_dequeue(&ring->queue);
920 pci_unmap_single(priv->pdev, le32_to_cpu(entry->TxBuffAddr),
921 skb->len, PCI_DMA_TODEVICE);
926 if (prio != BEACON_QUEUE) {
927 /* try to deal with the pending packets */
928 tasklet_schedule(&priv->irq_tx_tasklet);
932 static void rtl8192_stop_beacon(struct net_device *dev)
936 static void rtl8192_config_rate(struct r8192_priv *priv, u16* rate_config)
938 struct ieee80211_network *net;
939 u8 i=0, basic_rate = 0;
940 net = & priv->ieee80211->current_network;
942 for (i=0; i<net->rates_len; i++)
944 basic_rate = net->rates[i]&0x7f;
947 case MGN_1M: *rate_config |= RRSR_1M; break;
948 case MGN_2M: *rate_config |= RRSR_2M; break;
949 case MGN_5_5M: *rate_config |= RRSR_5_5M; break;
950 case MGN_11M: *rate_config |= RRSR_11M; break;
951 case MGN_6M: *rate_config |= RRSR_6M; break;
952 case MGN_9M: *rate_config |= RRSR_9M; break;
953 case MGN_12M: *rate_config |= RRSR_12M; break;
954 case MGN_18M: *rate_config |= RRSR_18M; break;
955 case MGN_24M: *rate_config |= RRSR_24M; break;
956 case MGN_36M: *rate_config |= RRSR_36M; break;
957 case MGN_48M: *rate_config |= RRSR_48M; break;
958 case MGN_54M: *rate_config |= RRSR_54M; break;
961 for (i=0; i<net->rates_ex_len; i++)
963 basic_rate = net->rates_ex[i]&0x7f;
966 case MGN_1M: *rate_config |= RRSR_1M; break;
967 case MGN_2M: *rate_config |= RRSR_2M; break;
968 case MGN_5_5M: *rate_config |= RRSR_5_5M; break;
969 case MGN_11M: *rate_config |= RRSR_11M; break;
970 case MGN_6M: *rate_config |= RRSR_6M; break;
971 case MGN_9M: *rate_config |= RRSR_9M; break;
972 case MGN_12M: *rate_config |= RRSR_12M; break;
973 case MGN_18M: *rate_config |= RRSR_18M; break;
974 case MGN_24M: *rate_config |= RRSR_24M; break;
975 case MGN_36M: *rate_config |= RRSR_36M; break;
976 case MGN_48M: *rate_config |= RRSR_48M; break;
977 case MGN_54M: *rate_config |= RRSR_54M; break;
983 #define SHORT_SLOT_TIME 9
984 #define NON_SHORT_SLOT_TIME 20
986 static void rtl8192_update_cap(struct r8192_priv *priv, u16 cap)
989 struct ieee80211_network *net = &priv->ieee80211->current_network;
991 priv->short_preamble = cap & WLAN_CAPABILITY_SHORT_PREAMBLE;
992 tmp = priv->basic_rate;
993 if (priv->short_preamble)
994 tmp |= BRSR_AckShortPmb;
995 write_nic_dword(priv, RRSR, tmp);
997 if (net->mode & (IEEE_G|IEEE_N_24G))
1000 if ((cap & WLAN_CAPABILITY_SHORT_SLOT)&&(!priv->ieee80211->pHTInfo->bCurrentRT2RTLongSlotTime))
1002 slot_time = SHORT_SLOT_TIME;
1004 else //long slot time
1005 slot_time = NON_SHORT_SLOT_TIME;
1006 priv->slot_time = slot_time;
1007 write_nic_byte(priv, SLOT_TIME, slot_time);
1012 static void rtl8192_net_update(struct r8192_priv *priv)
1014 struct ieee80211_network *net;
1015 u16 BcnTimeCfg = 0, BcnCW = 6, BcnIFS = 0xf;
1016 u16 rate_config = 0;
1017 net = &priv->ieee80211->current_network;
1019 /* update Basic rate: RR, BRSR */
1020 rtl8192_config_rate(priv, &rate_config);
1023 * Select RRSR (in Legacy-OFDM and CCK)
1024 * For 8190, we select only 24M, 12M, 6M, 11M, 5.5M,
1025 * 2M, and 1M from the Basic rate.
1026 * We do not use other rates.
1028 priv->basic_rate = rate_config &= 0x15f;
1031 write_nic_dword(priv, BSSIDR, ((u32 *)net->bssid)[0]);
1032 write_nic_word(priv, BSSIDR+4, ((u16 *)net->bssid)[2]);
1034 if (priv->ieee80211->iw_mode == IW_MODE_ADHOC)
1036 write_nic_word(priv, ATIMWND, 2);
1037 write_nic_word(priv, BCN_DMATIME, 256);
1038 write_nic_word(priv, BCN_INTERVAL, net->beacon_interval);
1040 * BIT15 of BCN_DRV_EARLY_INT will indicate
1041 * whether software beacon or hw beacon is applied.
1043 write_nic_word(priv, BCN_DRV_EARLY_INT, 10);
1044 write_nic_byte(priv, BCN_ERR_THRESH, 100);
1046 BcnTimeCfg |= (BcnCW<<BCN_TCFG_CW_SHIFT);
1047 /* TODO: BcnIFS may required to be changed on ASIC */
1048 BcnTimeCfg |= BcnIFS<<BCN_TCFG_IFS;
1049 write_nic_word(priv, BCN_TCFG, BcnTimeCfg);
1053 static void rtl819xE_tx_cmd(struct r8192_priv *priv, struct sk_buff *skb)
1055 struct rtl8192_tx_ring *ring;
1056 tx_desc_819x_pci *entry;
1060 unsigned long flags;
1062 ring = &priv->tx_ring[TXCMD_QUEUE];
1063 mapping = pci_map_single(priv->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
1065 spin_lock_irqsave(&priv->irq_th_lock,flags);
1066 idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
1067 entry = &ring->desc[idx];
1069 tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1071 entry->LINIP = tcb_desc->bLastIniPkt;
1072 entry->FirstSeg = 1;//first segment
1073 entry->LastSeg = 1; //last segment
1074 if(tcb_desc->bCmdOrInit == DESC_PACKET_TYPE_INIT) {
1075 entry->CmdInit = DESC_PACKET_TYPE_INIT;
1077 entry->CmdInit = DESC_PACKET_TYPE_NORMAL;
1078 entry->Offset = sizeof(TX_FWINFO_8190PCI) + 8;
1079 entry->PktSize = (u16)(tcb_desc->pkt_size + entry->Offset);
1080 entry->QueueSelect = QSLT_CMD;
1081 entry->TxFWInfoSize = 0x08;
1082 entry->RATid = (u8)DESC_PACKET_TYPE_INIT;
1084 entry->TxBufferSize = skb->len;
1085 entry->TxBuffAddr = cpu_to_le32(mapping);
1088 __skb_queue_tail(&ring->queue, skb);
1089 spin_unlock_irqrestore(&priv->irq_th_lock,flags);
1091 write_nic_byte(priv, TPPoll, TPPoll_CQ);
1097 * Mapping Software/Hardware descriptor queue id to "Queue Select Field"
1098 * in TxFwInfo data structure
1100 static u8 MapHwQueueToFirmwareQueue(u8 QueueID)
1106 QueueSelect = QSLT_BE;
1110 QueueSelect = QSLT_BK;
1114 QueueSelect = QSLT_VO;
1118 QueueSelect = QSLT_VI;
1122 QueueSelect = QSLT_MGNT;
1126 QueueSelect = QSLT_BEACON;
1130 QueueSelect = QSLT_CMD;
1135 RT_TRACE(COMP_ERR, "Impossible Queue Selection: %d\n", QueueID);
1141 static u8 MRateToHwRate8190Pci(u8 rate)
1143 u8 ret = DESC90_RATE1M;
1146 case MGN_1M: ret = DESC90_RATE1M; break;
1147 case MGN_2M: ret = DESC90_RATE2M; break;
1148 case MGN_5_5M: ret = DESC90_RATE5_5M; break;
1149 case MGN_11M: ret = DESC90_RATE11M; break;
1150 case MGN_6M: ret = DESC90_RATE6M; break;
1151 case MGN_9M: ret = DESC90_RATE9M; break;
1152 case MGN_12M: ret = DESC90_RATE12M; break;
1153 case MGN_18M: ret = DESC90_RATE18M; break;
1154 case MGN_24M: ret = DESC90_RATE24M; break;
1155 case MGN_36M: ret = DESC90_RATE36M; break;
1156 case MGN_48M: ret = DESC90_RATE48M; break;
1157 case MGN_54M: ret = DESC90_RATE54M; break;
1159 // HT rate since here
1160 case MGN_MCS0: ret = DESC90_RATEMCS0; break;
1161 case MGN_MCS1: ret = DESC90_RATEMCS1; break;
1162 case MGN_MCS2: ret = DESC90_RATEMCS2; break;
1163 case MGN_MCS3: ret = DESC90_RATEMCS3; break;
1164 case MGN_MCS4: ret = DESC90_RATEMCS4; break;
1165 case MGN_MCS5: ret = DESC90_RATEMCS5; break;
1166 case MGN_MCS6: ret = DESC90_RATEMCS6; break;
1167 case MGN_MCS7: ret = DESC90_RATEMCS7; break;
1168 case MGN_MCS8: ret = DESC90_RATEMCS8; break;
1169 case MGN_MCS9: ret = DESC90_RATEMCS9; break;
1170 case MGN_MCS10: ret = DESC90_RATEMCS10; break;
1171 case MGN_MCS11: ret = DESC90_RATEMCS11; break;
1172 case MGN_MCS12: ret = DESC90_RATEMCS12; break;
1173 case MGN_MCS13: ret = DESC90_RATEMCS13; break;
1174 case MGN_MCS14: ret = DESC90_RATEMCS14; break;
1175 case MGN_MCS15: ret = DESC90_RATEMCS15; break;
1176 case (0x80|0x20): ret = DESC90_RATEMCS32; break;
1184 static u8 QueryIsShort(u8 TxHT, u8 TxRate, cb_desc *tcb_desc)
1188 tmp_Short = (TxHT==1)?((tcb_desc->bUseShortGI)?1:0):((tcb_desc->bUseShortPreamble)?1:0);
1190 if(TxHT==1 && TxRate != DESC90_RATEMCS15)
1197 * The tx procedure is just as following,
1198 * skb->cb will contain all the following information,
1199 * priority, morefrag, rate, &dev.
1201 static short rtl8192_tx(struct r8192_priv *priv, struct sk_buff* skb)
1203 struct rtl8192_tx_ring *ring;
1204 unsigned long flags;
1205 cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1206 tx_desc_819x_pci *pdesc = NULL;
1207 TX_FWINFO_8190PCI *pTxFwInfo = NULL;
1209 bool multi_addr = false, broad_addr = false, uni_addr = false;
1210 u8 *pda_addr = NULL;
1213 if (priv->bdisable_nic) {
1214 RT_TRACE(COMP_ERR, "Nic is disabled! Can't tx packet len=%d qidx=%d!!!\n",
1215 skb->len, tcb_desc->queue_index);
1220 priv->ieee80211->bAwakePktSent = true;
1223 mapping = pci_map_single(priv->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
1225 /* collect the tx packets statitcs */
1226 pda_addr = ((u8 *)skb->data) + sizeof(TX_FWINFO_8190PCI);
1227 if (is_multicast_ether_addr(pda_addr))
1229 else if (is_broadcast_ether_addr(pda_addr))
1235 priv->stats.txbytesunicast += (u8)(skb->len) - sizeof(TX_FWINFO_8190PCI);
1237 /* fill tx firmware */
1238 pTxFwInfo = (PTX_FWINFO_8190PCI)skb->data;
1239 memset(pTxFwInfo, 0, sizeof(TX_FWINFO_8190PCI));
1240 pTxFwInfo->TxHT = (tcb_desc->data_rate&0x80) ? 1 : 0;
1241 pTxFwInfo->TxRate = MRateToHwRate8190Pci((u8)tcb_desc->data_rate);
1242 pTxFwInfo->EnableCPUDur = tcb_desc->bTxEnableFwCalcDur;
1243 pTxFwInfo->Short = QueryIsShort(pTxFwInfo->TxHT, pTxFwInfo->TxRate, tcb_desc);
1245 /* Aggregation related */
1246 if (tcb_desc->bAMPDUEnable) {
1247 pTxFwInfo->AllowAggregation = 1;
1248 pTxFwInfo->RxMF = tcb_desc->ampdu_factor;
1249 pTxFwInfo->RxAMD = tcb_desc->ampdu_density;
1251 pTxFwInfo->AllowAggregation = 0;
1252 pTxFwInfo->RxMF = 0;
1253 pTxFwInfo->RxAMD = 0;
1256 /* Protection mode related */
1257 pTxFwInfo->RtsEnable = (tcb_desc->bRTSEnable) ? 1 : 0;
1258 pTxFwInfo->CtsEnable = (tcb_desc->bCTSEnable) ? 1 : 0;
1259 pTxFwInfo->RtsSTBC = (tcb_desc->bRTSSTBC) ? 1 : 0;
1260 pTxFwInfo->RtsHT = (tcb_desc->rts_rate&0x80) ? 1 : 0;
1261 pTxFwInfo->RtsRate = MRateToHwRate8190Pci((u8)tcb_desc->rts_rate);
1262 pTxFwInfo->RtsBandwidth = 0;
1263 pTxFwInfo->RtsSubcarrier = tcb_desc->RTSSC;
1264 pTxFwInfo->RtsShort = (pTxFwInfo->RtsHT == 0) ? (tcb_desc->bRTSUseShortPreamble ? 1 : 0) : (tcb_desc->bRTSUseShortGI? 1 : 0);
1266 /* Set Bandwidth and sub-channel settings. */
1267 if (priv->CurrentChannelBW == HT_CHANNEL_WIDTH_20_40) {
1268 if (tcb_desc->bPacketBW) {
1269 pTxFwInfo->TxBandwidth = 1;
1270 /* use duplicated mode */
1271 pTxFwInfo->TxSubCarrier = 0;
1273 pTxFwInfo->TxBandwidth = 0;
1274 pTxFwInfo->TxSubCarrier = priv->nCur40MhzPrimeSC;
1277 pTxFwInfo->TxBandwidth = 0;
1278 pTxFwInfo->TxSubCarrier = 0;
1281 spin_lock_irqsave(&priv->irq_th_lock, flags);
1282 ring = &priv->tx_ring[tcb_desc->queue_index];
1283 if (tcb_desc->queue_index != BEACON_QUEUE)
1284 idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
1288 pdesc = &ring->desc[idx];
1289 if ((pdesc->OWN == 1) && (tcb_desc->queue_index != BEACON_QUEUE)) {
1290 RT_TRACE(COMP_ERR, "No more TX desc@%d, ring->idx = %d,idx = %d,%x\n",
1291 tcb_desc->queue_index, ring->idx, idx, skb->len);
1292 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1296 /* fill tx descriptor */
1297 memset(pdesc, 0, 12);
1302 pdesc->Offset = sizeof(TX_FWINFO_8190PCI) + 8; /* We must add 8!! */
1303 pdesc->PktSize = (u16)skb->len-sizeof(TX_FWINFO_8190PCI);
1306 pdesc->SecCAMID = 0;
1307 pdesc->RATid = tcb_desc->RATRIndex;
1310 pdesc->SecType = 0x0;
1311 if (tcb_desc->bHwSec) {
1312 switch (priv->ieee80211->pairwise_key_type) {
1313 case KEY_TYPE_WEP40:
1314 case KEY_TYPE_WEP104:
1315 pdesc->SecType = 0x1;
1319 pdesc->SecType = 0x2;
1323 pdesc->SecType = 0x3;
1327 pdesc->SecType = 0x0;
1336 pdesc->QueueSelect = MapHwQueueToFirmwareQueue(tcb_desc->queue_index);
1337 pdesc->TxFWInfoSize = sizeof(TX_FWINFO_8190PCI);
1339 pdesc->DISFB = tcb_desc->bTxDisableRateFallBack;
1340 pdesc->USERATE = tcb_desc->bTxUseDriverAssingedRate;
1342 pdesc->FirstSeg = 1;
1344 pdesc->TxBufferSize = skb->len;
1346 pdesc->TxBuffAddr = cpu_to_le32(mapping);
1347 __skb_queue_tail(&ring->queue, skb);
1349 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1350 priv->ieee80211->dev->trans_start = jiffies;
1351 write_nic_word(priv, TPPoll, 0x01<<tcb_desc->queue_index);
1355 static short rtl8192_alloc_rx_desc_ring(struct r8192_priv *priv)
1357 rx_desc_819x_pci *entry = NULL;
1360 priv->rx_ring = pci_alloc_consistent(priv->pdev,
1361 sizeof(*priv->rx_ring) * priv->rxringcount, &priv->rx_ring_dma);
1363 if (!priv->rx_ring || (unsigned long)priv->rx_ring & 0xFF) {
1364 RT_TRACE(COMP_ERR,"Cannot allocate RX ring\n");
1368 memset(priv->rx_ring, 0, sizeof(*priv->rx_ring) * priv->rxringcount);
1371 for (i = 0; i < priv->rxringcount; i++) {
1372 struct sk_buff *skb = dev_alloc_skb(priv->rxbuffersize);
1373 dma_addr_t *mapping;
1374 entry = &priv->rx_ring[i];
1377 priv->rx_buf[i] = skb;
1378 mapping = (dma_addr_t *)skb->cb;
1379 *mapping = pci_map_single(priv->pdev, skb_tail_pointer(skb),
1380 priv->rxbuffersize, PCI_DMA_FROMDEVICE);
1382 entry->BufferAddress = cpu_to_le32(*mapping);
1384 entry->Length = priv->rxbuffersize;
1392 static int rtl8192_alloc_tx_desc_ring(struct r8192_priv *priv,
1393 unsigned int prio, unsigned int entries)
1395 tx_desc_819x_pci *ring;
1399 ring = pci_alloc_consistent(priv->pdev, sizeof(*ring) * entries, &dma);
1400 if (!ring || (unsigned long)ring & 0xFF) {
1401 RT_TRACE(COMP_ERR, "Cannot allocate TX ring (prio = %d)\n", prio);
1405 memset(ring, 0, sizeof(*ring)*entries);
1406 priv->tx_ring[prio].desc = ring;
1407 priv->tx_ring[prio].dma = dma;
1408 priv->tx_ring[prio].idx = 0;
1409 priv->tx_ring[prio].entries = entries;
1410 skb_queue_head_init(&priv->tx_ring[prio].queue);
1412 for (i = 0; i < entries; i++)
1413 ring[i].NextDescAddress =
1414 cpu_to_le32((u32)dma + ((i + 1) % entries) * sizeof(*ring));
1419 static short rtl8192_pci_initdescring(struct r8192_priv *priv)
1424 ret = rtl8192_alloc_rx_desc_ring(priv);
1428 /* general process for other queue */
1429 for (i = 0; i < MAX_TX_QUEUE_COUNT; i++) {
1430 ret = rtl8192_alloc_tx_desc_ring(priv, i, priv->txringcount);
1432 goto err_free_rings;
1438 rtl8192_free_rx_ring(priv);
1439 for (i = 0; i < MAX_TX_QUEUE_COUNT; i++)
1440 if (priv->tx_ring[i].desc)
1441 rtl8192_free_tx_ring(priv, i);
1445 static void rtl8192_pci_resetdescring(struct r8192_priv *priv)
1449 /* force the rx_idx to the first one */
1451 rx_desc_819x_pci *entry = NULL;
1452 for (i = 0; i < priv->rxringcount; i++) {
1453 entry = &priv->rx_ring[i];
1459 /* after reset, release previous pending packet, and force the
1460 * tx idx to the first one */
1461 for (i = 0; i < MAX_TX_QUEUE_COUNT; i++) {
1462 if (priv->tx_ring[i].desc) {
1463 struct rtl8192_tx_ring *ring = &priv->tx_ring[i];
1465 while (skb_queue_len(&ring->queue)) {
1466 tx_desc_819x_pci *entry = &ring->desc[ring->idx];
1467 struct sk_buff *skb = __skb_dequeue(&ring->queue);
1469 pci_unmap_single(priv->pdev, le32_to_cpu(entry->TxBuffAddr),
1470 skb->len, PCI_DMA_TODEVICE);
1472 ring->idx = (ring->idx + 1) % ring->entries;
1479 static void rtl8192_link_change(struct net_device *dev)
1481 struct r8192_priv *priv = ieee80211_priv(dev);
1482 struct ieee80211_device* ieee = priv->ieee80211;
1484 if (ieee->state == IEEE80211_LINKED)
1486 rtl8192_net_update(priv);
1487 rtl8192_update_ratr_table(priv);
1489 //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
1490 if ((KEY_TYPE_WEP40 == ieee->pairwise_key_type) || (KEY_TYPE_WEP104 == ieee->pairwise_key_type))
1491 EnableHWSecurityConfig8192(priv);
1495 write_nic_byte(priv, 0x173, 0);
1498 rtl8192_update_msr(priv);
1500 // 2007/10/16 MH MAC Will update TSF according to all received beacon, so we have
1501 // // To set CBSSID bit when link with any AP or STA.
1502 if (ieee->iw_mode == IW_MODE_INFRA || ieee->iw_mode == IW_MODE_ADHOC)
1505 reg = read_nic_dword(priv, RCR);
1506 if (priv->ieee80211->state == IEEE80211_LINKED)
1507 priv->ReceiveConfig = reg |= RCR_CBSSID;
1509 priv->ReceiveConfig = reg &= ~RCR_CBSSID;
1510 write_nic_dword(priv, RCR, reg);
1515 static const struct ieee80211_qos_parameters def_qos_parameters = {
1516 {3,3,3,3},/* cw_min */
1517 {7,7,7,7},/* cw_max */
1518 {2,2,2,2},/* aifs */
1519 {0,0,0,0},/* flags */
1520 {0,0,0,0} /* tx_op_limit */
1523 static void rtl8192_update_beacon(struct work_struct * work)
1525 struct r8192_priv *priv = container_of(work, struct r8192_priv, update_beacon_wq.work);
1526 struct ieee80211_device* ieee = priv->ieee80211;
1527 struct ieee80211_network* net = &ieee->current_network;
1529 if (ieee->pHTInfo->bCurrentHTSupport)
1530 HTUpdateSelfAndPeerSetting(ieee, net);
1531 ieee->pHTInfo->bCurrentRT2RTLongSlotTime = net->bssht.bdRT2RTLongSlotTime;
1532 rtl8192_update_cap(priv, net->capability);
1536 * background support to run QoS activate functionality
1538 static const int WDCAPARA_ADD[] = {EDCAPARA_BE,EDCAPARA_BK,EDCAPARA_VI,EDCAPARA_VO};
1539 static void rtl8192_qos_activate(struct work_struct * work)
1541 struct r8192_priv *priv = container_of(work, struct r8192_priv, qos_activate);
1542 struct ieee80211_qos_parameters *qos_parameters = &priv->ieee80211->current_network.qos_data.parameters;
1543 u8 mode = priv->ieee80211->current_network.mode;
1548 mutex_lock(&priv->mutex);
1549 if(priv->ieee80211->state != IEEE80211_LINKED)
1551 RT_TRACE(COMP_QOS,"qos active process with associate response received\n");
1552 /* It better set slot time at first */
1553 /* For we just support b/g mode at present, let the slot time at 9/20 selection */
1554 /* update the ac parameter to related registers */
1555 for(i = 0; i < QOS_QUEUE_NUM; i++) {
1556 //Mode G/A: slotTimeTimer = 9; Mode B: 20
1557 u1bAIFS = qos_parameters->aifs[i] * ((mode&(IEEE_G|IEEE_N_24G)) ?9:20) + aSifsTime;
1558 u4bAcParam = ((((u32)(qos_parameters->tx_op_limit[i]))<< AC_PARAM_TXOP_LIMIT_OFFSET)|
1559 (((u32)(qos_parameters->cw_max[i]))<< AC_PARAM_ECW_MAX_OFFSET)|
1560 (((u32)(qos_parameters->cw_min[i]))<< AC_PARAM_ECW_MIN_OFFSET)|
1561 ((u32)u1bAIFS << AC_PARAM_AIFS_OFFSET));
1562 write_nic_dword(priv, WDCAPARA_ADD[i], u4bAcParam);
1566 mutex_unlock(&priv->mutex);
1569 static int rtl8192_qos_handle_probe_response(struct r8192_priv *priv,
1571 struct ieee80211_network *network)
1574 u32 size = sizeof(struct ieee80211_qos_parameters);
1576 if(priv->ieee80211->state !=IEEE80211_LINKED)
1579 if ((priv->ieee80211->iw_mode != IW_MODE_INFRA))
1582 if (network->flags & NETWORK_HAS_QOS_MASK) {
1583 if (active_network &&
1584 (network->flags & NETWORK_HAS_QOS_PARAMETERS))
1585 network->qos_data.active = network->qos_data.supported;
1587 if ((network->qos_data.active == 1) && (active_network == 1) &&
1588 (network->flags & NETWORK_HAS_QOS_PARAMETERS) &&
1589 (network->qos_data.old_param_count !=
1590 network->qos_data.param_count)) {
1591 network->qos_data.old_param_count =
1592 network->qos_data.param_count;
1593 queue_work(priv->priv_wq, &priv->qos_activate);
1594 RT_TRACE (COMP_QOS, "QoS parameters change call "
1598 memcpy(&priv->ieee80211->current_network.qos_data.parameters,
1599 &def_qos_parameters, size);
1601 if ((network->qos_data.active == 1) && (active_network == 1)) {
1602 queue_work(priv->priv_wq, &priv->qos_activate);
1603 RT_TRACE(COMP_QOS, "QoS was disabled call qos_activate\n");
1605 network->qos_data.active = 0;
1606 network->qos_data.supported = 0;
1612 /* handle manage frame frame beacon and probe response */
1613 static int rtl8192_handle_beacon(struct net_device * dev,
1614 struct ieee80211_beacon * beacon,
1615 struct ieee80211_network * network)
1617 struct r8192_priv *priv = ieee80211_priv(dev);
1619 rtl8192_qos_handle_probe_response(priv,1,network);
1621 queue_delayed_work(priv->priv_wq, &priv->update_beacon_wq, 0);
1627 * handling the beaconing responses. if we get different QoS setting
1628 * off the network from the associated setting, adjust the QoS setting
1630 static int rtl8192_qos_association_resp(struct r8192_priv *priv,
1631 struct ieee80211_network *network)
1634 unsigned long flags;
1635 u32 size = sizeof(struct ieee80211_qos_parameters);
1636 int set_qos_param = 0;
1638 if ((priv == NULL) || (network == NULL))
1641 if (priv->ieee80211->state != IEEE80211_LINKED)
1644 if ((priv->ieee80211->iw_mode != IW_MODE_INFRA))
1647 spin_lock_irqsave(&priv->ieee80211->lock, flags);
1648 if (network->flags & NETWORK_HAS_QOS_PARAMETERS) {
1649 memcpy(&priv->ieee80211->current_network.qos_data.parameters,
1650 &network->qos_data.parameters,
1651 sizeof(struct ieee80211_qos_parameters));
1652 priv->ieee80211->current_network.qos_data.active = 1;
1654 /* update qos parameter for current network */
1655 priv->ieee80211->current_network.qos_data.old_param_count =
1656 priv->ieee80211->current_network.qos_data.param_count;
1657 priv->ieee80211->current_network.qos_data.param_count =
1658 network->qos_data.param_count;
1661 memcpy(&priv->ieee80211->current_network.qos_data.parameters,
1662 &def_qos_parameters, size);
1663 priv->ieee80211->current_network.qos_data.active = 0;
1664 priv->ieee80211->current_network.qos_data.supported = 0;
1668 spin_unlock_irqrestore(&priv->ieee80211->lock, flags);
1670 RT_TRACE(COMP_QOS, "%s: network->flags = %d,%d\n", __FUNCTION__,
1671 network->flags, priv->ieee80211->current_network.qos_data.active);
1672 if (set_qos_param == 1)
1673 queue_work(priv->priv_wq, &priv->qos_activate);
1679 static int rtl8192_handle_assoc_response(struct net_device *dev,
1680 struct ieee80211_assoc_response_frame *resp,
1681 struct ieee80211_network *network)
1683 struct r8192_priv *priv = ieee80211_priv(dev);
1684 rtl8192_qos_association_resp(priv, network);
1689 /* updateRATRTabel for MCS only. Basic rate is not implemented. */
1690 static void rtl8192_update_ratr_table(struct r8192_priv* priv)
1692 struct ieee80211_device* ieee = priv->ieee80211;
1693 u8* pMcsRate = ieee->dot11HTOperationalRateSet;
1697 rtl8192_config_rate(priv, (u16*)(&ratr_value));
1698 ratr_value |= (*(u16*)(pMcsRate)) << 12;
1703 ratr_value &= 0x00000FF0;
1706 ratr_value &= 0x0000000F;
1709 ratr_value &= 0x00000FF7;
1713 if (ieee->pHTInfo->PeerMimoPs == 0) //MIMO_PS_STATIC
1714 ratr_value &= 0x0007F007;
1716 if (priv->rf_type == RF_1T2R)
1717 ratr_value &= 0x000FF007;
1719 ratr_value &= 0x0F81F007;
1725 ratr_value &= 0x0FFFFFFF;
1726 if(ieee->pHTInfo->bCurTxBW40MHz && ieee->pHTInfo->bCurShortGI40MHz){
1727 ratr_value |= 0x80000000;
1728 }else if(!ieee->pHTInfo->bCurTxBW40MHz && ieee->pHTInfo->bCurShortGI20MHz){
1729 ratr_value |= 0x80000000;
1731 write_nic_dword(priv, RATR0+rate_index*4, ratr_value);
1732 write_nic_byte(priv, UFWP, 1);
1735 static bool GetNmodeSupportBySecCfg8190Pci(struct net_device*dev)
1737 struct r8192_priv *priv = ieee80211_priv(dev);
1738 struct ieee80211_device *ieee = priv->ieee80211;
1740 return !(ieee->rtllib_ap_sec_type &&
1741 (ieee->rtllib_ap_sec_type(ieee)&(SEC_ALG_WEP|SEC_ALG_TKIP)));
1744 static void rtl8192_refresh_supportrate(struct r8192_priv* priv)
1746 struct ieee80211_device* ieee = priv->ieee80211;
1747 //we donot consider set support rate for ABG mode, only HT MCS rate is set here.
1748 if (ieee->mode == WIRELESS_MODE_N_24G || ieee->mode == WIRELESS_MODE_N_5G)
1750 memcpy(ieee->Regdot11HTOperationalRateSet, ieee->RegHTSuppRateSet, 16);
1753 memset(ieee->Regdot11HTOperationalRateSet, 0, 16);
1756 static u8 rtl8192_getSupportedWireleeMode(void)
1758 return (WIRELESS_MODE_N_24G|WIRELESS_MODE_G|WIRELESS_MODE_B);
1761 static void rtl8192_SetWirelessMode(struct net_device* dev, u8 wireless_mode)
1763 struct r8192_priv *priv = ieee80211_priv(dev);
1764 u8 bSupportMode = rtl8192_getSupportedWireleeMode();
1766 if ((wireless_mode == WIRELESS_MODE_AUTO) || ((wireless_mode&bSupportMode)==0))
1768 if(bSupportMode & WIRELESS_MODE_N_24G)
1770 wireless_mode = WIRELESS_MODE_N_24G;
1772 else if(bSupportMode & WIRELESS_MODE_N_5G)
1774 wireless_mode = WIRELESS_MODE_N_5G;
1776 else if((bSupportMode & WIRELESS_MODE_A))
1778 wireless_mode = WIRELESS_MODE_A;
1780 else if((bSupportMode & WIRELESS_MODE_G))
1782 wireless_mode = WIRELESS_MODE_G;
1784 else if((bSupportMode & WIRELESS_MODE_B))
1786 wireless_mode = WIRELESS_MODE_B;
1789 RT_TRACE(COMP_ERR, "%s(), No valid wireless mode supported, SupportedWirelessMode(%x)!!!\n", __FUNCTION__,bSupportMode);
1790 wireless_mode = WIRELESS_MODE_B;
1793 priv->ieee80211->mode = wireless_mode;
1795 if ((wireless_mode == WIRELESS_MODE_N_24G) || (wireless_mode == WIRELESS_MODE_N_5G))
1796 priv->ieee80211->pHTInfo->bEnableHT = 1;
1798 priv->ieee80211->pHTInfo->bEnableHT = 0;
1799 RT_TRACE(COMP_INIT, "Current Wireless Mode is %x\n", wireless_mode);
1800 rtl8192_refresh_supportrate(priv);
1803 static bool GetHalfNmodeSupportByAPs819xPci(struct net_device* dev)
1805 struct r8192_priv* priv = ieee80211_priv(dev);
1806 struct ieee80211_device* ieee = priv->ieee80211;
1808 return ieee->bHalfWirelessN24GMode;
1811 static short rtl8192_is_tx_queue_empty(struct net_device *dev)
1814 struct r8192_priv *priv = ieee80211_priv(dev);
1815 for (i=0; i<=MGNT_QUEUE; i++)
1817 if ((i== TXCMD_QUEUE) || (i == HCCA_QUEUE) )
1819 if (skb_queue_len(&(&priv->tx_ring[i])->queue) > 0){
1820 printk("===>tx queue is not empty:%d, %d\n", i, skb_queue_len(&(&priv->tx_ring[i])->queue));
1827 static void rtl8192_hw_sleep_down(struct r8192_priv *priv)
1829 MgntActSet_RF_State(priv, eRfSleep, RF_CHANGE_BY_PS);
1832 static void rtl8192_hw_wakeup(struct net_device* dev)
1834 struct r8192_priv *priv = ieee80211_priv(dev);
1835 MgntActSet_RF_State(priv, eRfOn, RF_CHANGE_BY_PS);
1838 static void rtl8192_hw_wakeup_wq (struct work_struct *work)
1840 struct delayed_work *dwork = container_of(work,struct delayed_work,work);
1841 struct ieee80211_device *ieee = container_of(dwork,struct ieee80211_device,hw_wakeup_wq);
1842 struct net_device *dev = ieee->dev;
1843 rtl8192_hw_wakeup(dev);
1847 #define MIN_SLEEP_TIME 50
1848 #define MAX_SLEEP_TIME 10000
1849 static void rtl8192_hw_to_sleep(struct net_device *dev, u32 th, u32 tl)
1851 struct r8192_priv *priv = ieee80211_priv(dev);
1855 // Writing HW register with 0 equals to disable
1856 // the timer, that is not really what we want
1858 tl -= MSECS(8+16+7);
1860 // If the interval in witch we are requested to sleep is too
1861 // short then give up and remain awake
1862 // when we sleep after send null frame, the timer will be too short to sleep.
1864 if(((tl>=rb)&& (tl-rb) <= MSECS(MIN_SLEEP_TIME))
1865 ||((rb>tl)&& (rb-tl) < MSECS(MIN_SLEEP_TIME))) {
1866 printk("too short to sleep::%x, %x, %lx\n",tl, rb, MSECS(MIN_SLEEP_TIME));
1870 if(((tl > rb) && ((tl-rb) > MSECS(MAX_SLEEP_TIME)))||
1871 ((tl < rb) && (tl>MSECS(69)) && ((rb-tl) > MSECS(MAX_SLEEP_TIME)))||
1872 ((tl<rb)&&(tl<MSECS(69))&&((tl+0xffffffff-rb)>MSECS(MAX_SLEEP_TIME)))) {
1873 printk("========>too long to sleep:%x, %x, %lx\n", tl, rb, MSECS(MAX_SLEEP_TIME));
1877 tmp = (tl>rb)?(tl-rb):(rb-tl);
1878 queue_delayed_work(priv->ieee80211->wq,
1879 &priv->ieee80211->hw_wakeup_wq,tmp);
1881 rtl8192_hw_sleep_down(priv);
1884 static void rtl8192_init_priv_variable(struct r8192_priv *priv)
1887 PRT_POWER_SAVE_CONTROL pPSC = &priv->PowerSaveControl;
1889 // Default Halt the NIC if RF is OFF.
1890 pPSC->RegRfPsLevel |= RT_RF_OFF_LEVL_HALT_NIC;
1891 pPSC->RegRfPsLevel |= RT_RF_OFF_LEVL_CLK_REQ;
1892 pPSC->RegRfPsLevel |= RT_RF_OFF_LEVL_ASPM;
1893 pPSC->RegRfPsLevel |= RT_RF_LPS_LEVEL_ASPM;
1894 pPSC->bLeisurePs = true;
1895 priv->ieee80211->RegMaxLPSAwakeIntvl = 5;
1896 priv->bHwRadioOff = false;
1898 priv->being_init_adapter = false;
1899 priv->txringcount = 64;//32;
1900 priv->rxbuffersize = 9100;//2048;//1024;
1901 priv->rxringcount = MAX_RX_COUNT;//64;
1902 priv->chan = 1; //set to channel 1
1903 priv->RegWirelessMode = WIRELESS_MODE_AUTO;
1904 priv->RegChannelPlan = 0xf;
1905 priv->ieee80211->mode = WIRELESS_MODE_AUTO; //SET AUTO
1906 priv->ieee80211->iw_mode = IW_MODE_INFRA;
1907 priv->ieee80211->ieee_up=0;
1908 priv->retry_rts = DEFAULT_RETRY_RTS;
1909 priv->retry_data = DEFAULT_RETRY_DATA;
1910 priv->ieee80211->rts = DEFAULT_RTS_THRESHOLD;
1911 priv->ieee80211->rate = 110; //11 mbps
1912 priv->ieee80211->short_slot = 1;
1913 priv->promisc = (priv->ieee80211->dev->flags & IFF_PROMISC) ? 1:0;
1914 priv->bcck_in_ch14 = false;
1915 priv->CCKPresentAttentuation = 0;
1916 priv->rfa_txpowertrackingindex = 0;
1917 priv->rfc_txpowertrackingindex = 0;
1919 //added by amy for silent reset
1920 priv->ResetProgress = RESET_TYPE_NORESET;
1921 priv->bForcedSilentReset = 0;
1922 priv->bDisableNormalResetCheck = false;
1923 priv->force_reset = false;
1924 //added by amy for power save
1925 priv->RfOffReason = 0;
1926 priv->bHwRfOffAction = 0;
1927 priv->PowerSaveControl.bInactivePs = true;
1928 priv->PowerSaveControl.bIPSModeBackup = false;
1930 priv->ieee80211->current_network.beacon_interval = DEFAULT_BEACONINTERVAL;
1931 priv->ieee80211->iw_mode = IW_MODE_INFRA;
1932 priv->ieee80211->softmac_features = IEEE_SOFTMAC_SCAN |
1933 IEEE_SOFTMAC_ASSOCIATE | IEEE_SOFTMAC_PROBERQ |
1934 IEEE_SOFTMAC_PROBERS | IEEE_SOFTMAC_TX_QUEUE;/* |
1935 IEEE_SOFTMAC_BEACONS;*///added by amy 080604 //| //IEEE_SOFTMAC_SINGLE_QUEUE;
1937 priv->ieee80211->active_scan = 1;
1938 priv->ieee80211->modulation = IEEE80211_CCK_MODULATION | IEEE80211_OFDM_MODULATION;
1939 priv->ieee80211->host_encrypt = 1;
1940 priv->ieee80211->host_decrypt = 1;
1941 priv->ieee80211->start_send_beacons = rtl8192_start_beacon;//+by david 081107
1942 priv->ieee80211->stop_send_beacons = rtl8192_stop_beacon;//+by david 081107
1943 priv->ieee80211->softmac_hard_start_xmit = rtl8192_hard_start_xmit;
1944 priv->ieee80211->set_chan = rtl8192_set_chan;
1945 priv->ieee80211->link_change = rtl8192_link_change;
1946 priv->ieee80211->softmac_data_hard_start_xmit = rtl8192_hard_data_xmit;
1947 priv->ieee80211->data_hard_stop = rtl8192_data_hard_stop;
1948 priv->ieee80211->data_hard_resume = rtl8192_data_hard_resume;
1949 priv->ieee80211->init_wmmparam_flag = 0;
1950 priv->ieee80211->fts = DEFAULT_FRAG_THRESHOLD;
1951 priv->ieee80211->check_nic_enough_desc = check_nic_enough_desc;
1952 priv->ieee80211->tx_headroom = sizeof(TX_FWINFO_8190PCI);
1953 priv->ieee80211->qos_support = 1;
1954 priv->ieee80211->SetBWModeHandler = rtl8192_SetBWMode;
1955 priv->ieee80211->handle_assoc_response = rtl8192_handle_assoc_response;
1956 priv->ieee80211->handle_beacon = rtl8192_handle_beacon;
1958 priv->ieee80211->sta_wake_up = rtl8192_hw_wakeup;
1959 priv->ieee80211->enter_sleep_state = rtl8192_hw_to_sleep;
1960 priv->ieee80211->ps_is_queue_empty = rtl8192_is_tx_queue_empty;
1961 priv->ieee80211->GetNmodeSupportBySecCfg = GetNmodeSupportBySecCfg8190Pci;
1962 priv->ieee80211->SetWirelessMode = rtl8192_SetWirelessMode;
1963 priv->ieee80211->GetHalfNmodeSupportByAPsHandler = GetHalfNmodeSupportByAPs819xPci;
1965 priv->ieee80211->InitialGainHandler = InitialGain819xPci;
1968 priv->ieee80211->ieee80211_ips_leave_wq = ieee80211_ips_leave_wq;
1969 priv->ieee80211->ieee80211_ips_leave = ieee80211_ips_leave;
1972 priv->ieee80211->LeisurePSLeave = LeisurePSLeave;
1975 priv->ieee80211->SetHwRegHandler = rtl8192e_SetHwReg;
1976 priv->ieee80211->rtllib_ap_sec_type = rtl8192e_ap_sec_type;
1978 priv->ShortRetryLimit = 0x30;
1979 priv->LongRetryLimit = 0x30;
1981 priv->ReceiveConfig = RCR_ADD3 |
1982 RCR_AMF | RCR_ADF | //accept management/data
1983 RCR_AICV | //accept control frame for SW AP needs PS-poll, 2005.07.07, by rcnjko.
1984 RCR_AB | RCR_AM | RCR_APM | //accept BC/MC/UC
1985 RCR_AAP | ((u32)7<<RCR_MXDMA_OFFSET) |
1986 ((u32)7 << RCR_FIFO_OFFSET) | RCR_ONLYERLPKT;
1988 priv->pFirmware = vzalloc(sizeof(rt_firmware));
1990 /* rx related queue */
1991 skb_queue_head_init(&priv->skb_queue);
1993 /* Tx related queue */
1994 for(i = 0; i < MAX_QUEUE_SIZE; i++) {
1995 skb_queue_head_init(&priv->ieee80211->skb_waitQ [i]);
1997 for(i = 0; i < MAX_QUEUE_SIZE; i++) {
1998 skb_queue_head_init(&priv->ieee80211->skb_aggQ [i]);
2000 priv->rf_set_chan = rtl8192_phy_SwChnl;
2003 static void rtl8192_init_priv_lock(struct r8192_priv* priv)
2005 spin_lock_init(&priv->irq_th_lock);
2006 spin_lock_init(&priv->rf_ps_lock);
2007 sema_init(&priv->wx_sem,1);
2008 sema_init(&priv->rf_sem,1);
2009 mutex_init(&priv->mutex);
2012 /* init tasklet and wait_queue here */
2013 #define DRV_NAME "wlan0"
2014 static void rtl8192_init_priv_task(struct r8192_priv *priv)
2016 priv->priv_wq = create_workqueue(DRV_NAME);
2019 INIT_WORK(&priv->ieee80211->ips_leave_wq, IPSLeave_wq);
2022 INIT_WORK(&priv->reset_wq, rtl8192_restart);
2023 INIT_DELAYED_WORK(&priv->watch_dog_wq, rtl819x_watchdog_wqcallback);
2024 INIT_DELAYED_WORK(&priv->txpower_tracking_wq, dm_txpower_trackingcallback);
2025 INIT_DELAYED_WORK(&priv->rfpath_check_wq, dm_rf_pathcheck_workitemcallback);
2026 INIT_DELAYED_WORK(&priv->update_beacon_wq, rtl8192_update_beacon);
2027 INIT_WORK(&priv->qos_activate, rtl8192_qos_activate);
2028 INIT_DELAYED_WORK(&priv->ieee80211->hw_wakeup_wq, rtl8192_hw_wakeup_wq);
2030 tasklet_init(&priv->irq_rx_tasklet, rtl8192_irq_rx_tasklet,
2031 (unsigned long) priv);
2032 tasklet_init(&priv->irq_tx_tasklet, rtl8192_irq_tx_tasklet,
2033 (unsigned long) priv);
2034 tasklet_init(&priv->irq_prepare_beacon_tasklet, rtl8192_prepare_beacon,
2035 (unsigned long) priv);
2038 static void rtl8192_get_eeprom_size(struct r8192_priv *priv)
2041 RT_TRACE(COMP_INIT, "===========>%s()\n", __FUNCTION__);
2042 curCR = read_nic_dword(priv, EPROM_CMD);
2043 RT_TRACE(COMP_INIT, "read from Reg Cmd9346CR(%x):%x\n", EPROM_CMD, curCR);
2044 //whether need I consider BIT5?
2045 priv->epromtype = (curCR & EPROM_CMD_9356SEL) ? EPROM_93c56 : EPROM_93c46;
2046 RT_TRACE(COMP_INIT, "<===========%s(), epromtype:%d\n", __FUNCTION__, priv->epromtype);
2050 * Adapter->EEPROMAddressSize should be set before this function call.
2051 * EEPROM address size can be got through GetEEPROMSize8185()
2053 static void rtl8192_read_eeprom_info(struct r8192_priv *priv)
2055 struct net_device *dev = priv->ieee80211->dev;
2057 u8 ICVer8192, ICVer8256;
2058 u16 i,usValue, IC_Version;
2060 u8 bMac_Tmp_Addr[6] = {0x00, 0xe0, 0x4c, 0x00, 0x00, 0x01};
2061 RT_TRACE(COMP_INIT, "====> rtl8192_read_eeprom_info\n");
2064 // TODO: I don't know if we need to apply EF function to EEPROM read function
2066 //2 Read EEPROM ID to make sure autoload is success
2067 EEPROMId = eprom_read(priv, 0);
2068 if( EEPROMId != RTL8190_EEPROM_ID )
2070 RT_TRACE(COMP_ERR, "EEPROM ID is invalid:%x, %x\n", EEPROMId, RTL8190_EEPROM_ID);
2071 priv->AutoloadFailFlag=true;
2075 priv->AutoloadFailFlag=false;
2079 // Assign Chip Version ID
2081 // Read IC Version && Channel Plan
2082 if(!priv->AutoloadFailFlag)
2085 priv->eeprom_vid = eprom_read(priv, (EEPROM_VID >> 1));
2086 priv->eeprom_did = eprom_read(priv, (EEPROM_DID >> 1));
2088 usValue = eprom_read(priv, (u16)(EEPROM_Customer_ID>>1)) >> 8 ;
2089 priv->eeprom_CustomerID = (u8)( usValue & 0xff);
2090 usValue = eprom_read(priv, (EEPROM_ICVersion_ChannelPlan>>1));
2091 priv->eeprom_ChannelPlan = usValue&0xff;
2092 IC_Version = ((usValue&0xff00)>>8);
2094 ICVer8192 = (IC_Version&0xf); //bit0~3; 1:A cut, 2:B cut, 3:C cut...
2095 ICVer8256 = ((IC_Version&0xf0)>>4);//bit4~6, bit7 reserved for other RF chip; 1:A cut, 2:B cut, 3:C cut...
2096 RT_TRACE(COMP_INIT, "ICVer8192 = 0x%x\n", ICVer8192);
2097 RT_TRACE(COMP_INIT, "ICVer8256 = 0x%x\n", ICVer8256);
2098 if(ICVer8192 == 0x2) //B-cut
2100 if(ICVer8256 == 0x5) //E-cut
2101 priv->card_8192_version= VERSION_8190_BE;
2104 switch(priv->card_8192_version)
2106 case VERSION_8190_BD:
2107 case VERSION_8190_BE:
2110 priv->card_8192_version = VERSION_8190_BD;
2113 RT_TRACE(COMP_INIT, "\nIC Version = 0x%x\n", priv->card_8192_version);
2117 priv->card_8192_version = VERSION_8190_BD;
2118 priv->eeprom_vid = 0;
2119 priv->eeprom_did = 0;
2120 priv->eeprom_CustomerID = 0;
2121 priv->eeprom_ChannelPlan = 0;
2122 RT_TRACE(COMP_INIT, "IC Version = 0x%x\n", 0xff);
2125 RT_TRACE(COMP_INIT, "EEPROM VID = 0x%4x\n", priv->eeprom_vid);
2126 RT_TRACE(COMP_INIT, "EEPROM DID = 0x%4x\n", priv->eeprom_did);
2127 RT_TRACE(COMP_INIT,"EEPROM Customer ID: 0x%2x\n", priv->eeprom_CustomerID);
2129 //2 Read Permanent MAC address
2130 if(!priv->AutoloadFailFlag)
2132 for(i = 0; i < 6; i += 2)
2134 usValue = eprom_read(priv, (u16) ((EEPROM_NODE_ADDRESS_BYTE_0+i)>>1));
2135 *(u16*)(&dev->dev_addr[i]) = usValue;
2138 // when auto load failed, the last address byte set to be a random one.
2139 // added by david woo.2007/11/7
2140 memcpy(dev->dev_addr, bMac_Tmp_Addr, 6);
2143 RT_TRACE(COMP_INIT, "Permanent Address = %pM\n", dev->dev_addr);
2145 //2 TX Power Check EEPROM Fail or not
2146 if(priv->card_8192_version > VERSION_8190_BD) {
2147 priv->bTXPowerDataReadFromEEPORM = true;
2149 priv->bTXPowerDataReadFromEEPORM = false;
2152 // 2007/11/15 MH 8190PCI Default=2T4R, 8192PCIE default=1T2R
2153 priv->rf_type = RTL819X_DEFAULT_RF_TYPE;
2155 if(priv->card_8192_version > VERSION_8190_BD)
2157 // Read RF-indication and Tx Power gain index diff of legacy to HT OFDM rate.
2158 if(!priv->AutoloadFailFlag)
2160 tempval = (eprom_read(priv, (EEPROM_RFInd_PowerDiff>>1))) & 0xff;
2161 priv->EEPROMLegacyHTTxPowerDiff = tempval & 0xf; // bit[3:0]
2163 if (tempval&0x80) //RF-indication, bit[7]
2164 priv->rf_type = RF_1T2R;
2166 priv->rf_type = RF_2T4R;
2170 priv->EEPROMLegacyHTTxPowerDiff = EEPROM_Default_LegacyHTTxPowerDiff;
2172 RT_TRACE(COMP_INIT, "EEPROMLegacyHTTxPowerDiff = %d\n",
2173 priv->EEPROMLegacyHTTxPowerDiff);
2175 // Read ThermalMeter from EEPROM
2176 if(!priv->AutoloadFailFlag)
2178 priv->EEPROMThermalMeter = (u8)(((eprom_read(priv, (EEPROM_ThermalMeter>>1))) & 0xff00)>>8);
2182 priv->EEPROMThermalMeter = EEPROM_Default_ThermalMeter;
2184 RT_TRACE(COMP_INIT, "ThermalMeter = %d\n", priv->EEPROMThermalMeter);
2185 //vivi, for tx power track
2186 priv->TSSI_13dBm = priv->EEPROMThermalMeter *100;
2188 if(priv->epromtype == EPROM_93c46)
2190 // Read antenna tx power offset of B/C/D to A and CrystalCap from EEPROM
2191 if(!priv->AutoloadFailFlag)
2193 usValue = eprom_read(priv, (EEPROM_TxPwDiff_CrystalCap>>1));
2194 priv->EEPROMAntPwDiff = (usValue&0x0fff);
2195 priv->EEPROMCrystalCap = (u8)((usValue&0xf000)>>12);
2199 priv->EEPROMAntPwDiff = EEPROM_Default_AntTxPowerDiff;
2200 priv->EEPROMCrystalCap = EEPROM_Default_TxPwDiff_CrystalCap;
2202 RT_TRACE(COMP_INIT, "EEPROMAntPwDiff = %d\n", priv->EEPROMAntPwDiff);
2203 RT_TRACE(COMP_INIT, "EEPROMCrystalCap = %d\n", priv->EEPROMCrystalCap);
2206 // Get per-channel Tx Power Level
2208 for(i=0; i<14; i+=2)
2210 if(!priv->AutoloadFailFlag)
2212 usValue = eprom_read(priv, (u16) ((EEPROM_TxPwIndex_CCK+i)>>1) );
2216 usValue = EEPROM_Default_TxPower;
2218 *((u16*)(&priv->EEPROMTxPowerLevelCCK[i])) = usValue;
2219 RT_TRACE(COMP_INIT,"CCK Tx Power Level, Index %d = 0x%02x\n", i, priv->EEPROMTxPowerLevelCCK[i]);
2220 RT_TRACE(COMP_INIT, "CCK Tx Power Level, Index %d = 0x%02x\n", i+1, priv->EEPROMTxPowerLevelCCK[i+1]);
2222 for(i=0; i<14; i+=2)
2224 if(!priv->AutoloadFailFlag)
2226 usValue = eprom_read(priv, (u16) ((EEPROM_TxPwIndex_OFDM_24G+i)>>1) );
2230 usValue = EEPROM_Default_TxPower;
2232 *((u16*)(&priv->EEPROMTxPowerLevelOFDM24G[i])) = usValue;
2233 RT_TRACE(COMP_INIT, "OFDM 2.4G Tx Power Level, Index %d = 0x%02x\n", i, priv->EEPROMTxPowerLevelOFDM24G[i]);
2234 RT_TRACE(COMP_INIT, "OFDM 2.4G Tx Power Level, Index %d = 0x%02x\n", i+1, priv->EEPROMTxPowerLevelOFDM24G[i+1]);
2239 // Update HAL variables.
2241 if(priv->epromtype == EPROM_93c46)
2245 priv->TxPowerLevelCCK[i] = priv->EEPROMTxPowerLevelCCK[i];
2246 priv->TxPowerLevelOFDM24G[i] = priv->EEPROMTxPowerLevelOFDM24G[i];
2248 priv->LegacyHTTxPowerDiff = priv->EEPROMLegacyHTTxPowerDiff;
2249 // Antenna B gain offset to antenna A, bit0~3
2250 priv->AntennaTxPwDiff[0] = (priv->EEPROMAntPwDiff & 0xf);
2251 // Antenna C gain offset to antenna A, bit4~7
2252 priv->AntennaTxPwDiff[1] = ((priv->EEPROMAntPwDiff & 0xf0)>>4);
2253 // Antenna D gain offset to antenna A, bit8~11
2254 priv->AntennaTxPwDiff[2] = ((priv->EEPROMAntPwDiff & 0xf00)>>8);
2255 // CrystalCap, bit12~15
2256 priv->CrystalCap = priv->EEPROMCrystalCap;
2257 // ThermalMeter, bit0~3 for RFIC1, bit4~7 for RFIC2
2258 priv->ThermalMeter[0] = (priv->EEPROMThermalMeter & 0xf);
2259 priv->ThermalMeter[1] = ((priv->EEPROMThermalMeter & 0xf0)>>4);
2261 else if(priv->epromtype == EPROM_93c56)
2263 for(i=0; i<3; i++) // channel 1~3 use the same Tx Power Level.
2265 priv->TxPowerLevelCCK_A[i] = priv->EEPROMRfACCKChnl1TxPwLevel[0];
2266 priv->TxPowerLevelOFDM24G_A[i] = priv->EEPROMRfAOfdmChnlTxPwLevel[0];
2267 priv->TxPowerLevelCCK_C[i] = priv->EEPROMRfCCCKChnl1TxPwLevel[0];
2268 priv->TxPowerLevelOFDM24G_C[i] = priv->EEPROMRfCOfdmChnlTxPwLevel[0];
2270 for(i=3; i<9; i++) // channel 4~9 use the same Tx Power Level
2272 priv->TxPowerLevelCCK_A[i] = priv->EEPROMRfACCKChnl1TxPwLevel[1];
2273 priv->TxPowerLevelOFDM24G_A[i] = priv->EEPROMRfAOfdmChnlTxPwLevel[1];
2274 priv->TxPowerLevelCCK_C[i] = priv->EEPROMRfCCCKChnl1TxPwLevel[1];
2275 priv->TxPowerLevelOFDM24G_C[i] = priv->EEPROMRfCOfdmChnlTxPwLevel[1];
2277 for(i=9; i<14; i++) // channel 10~14 use the same Tx Power Level
2279 priv->TxPowerLevelCCK_A[i] = priv->EEPROMRfACCKChnl1TxPwLevel[2];
2280 priv->TxPowerLevelOFDM24G_A[i] = priv->EEPROMRfAOfdmChnlTxPwLevel[2];
2281 priv->TxPowerLevelCCK_C[i] = priv->EEPROMRfCCCKChnl1TxPwLevel[2];
2282 priv->TxPowerLevelOFDM24G_C[i] = priv->EEPROMRfCOfdmChnlTxPwLevel[2];
2285 RT_TRACE(COMP_INIT, "priv->TxPowerLevelCCK_A[%d] = 0x%x\n", i, priv->TxPowerLevelCCK_A[i]);
2287 RT_TRACE(COMP_INIT,"priv->TxPowerLevelOFDM24G_A[%d] = 0x%x\n", i, priv->TxPowerLevelOFDM24G_A[i]);
2289 RT_TRACE(COMP_INIT, "priv->TxPowerLevelCCK_C[%d] = 0x%x\n", i, priv->TxPowerLevelCCK_C[i]);
2291 RT_TRACE(COMP_INIT, "priv->TxPowerLevelOFDM24G_C[%d] = 0x%x\n", i, priv->TxPowerLevelOFDM24G_C[i]);
2292 priv->LegacyHTTxPowerDiff = priv->EEPROMLegacyHTTxPowerDiff;
2293 priv->AntennaTxPwDiff[0] = 0;
2294 priv->AntennaTxPwDiff[1] = 0;
2295 priv->AntennaTxPwDiff[2] = 0;
2296 priv->CrystalCap = priv->EEPROMCrystalCap;
2297 // ThermalMeter, bit0~3 for RFIC1, bit4~7 for RFIC2
2298 priv->ThermalMeter[0] = (priv->EEPROMThermalMeter & 0xf);
2299 priv->ThermalMeter[1] = ((priv->EEPROMThermalMeter & 0xf0)>>4);
2303 if(priv->rf_type == RF_1T2R)
2305 RT_TRACE(COMP_INIT, "1T2R config\n");
2307 else if (priv->rf_type == RF_2T4R)
2309 RT_TRACE(COMP_INIT, "2T4R config\n");
2312 // 2008/01/16 MH We can only know RF type in the function. So we have to init
2313 // DIG RATR table again.
2314 init_rate_adaptive(dev);
2316 //1 Make a copy for following variables and we can change them if we want
2318 if(priv->RegChannelPlan == 0xf)
2320 priv->ChannelPlan = priv->eeprom_ChannelPlan;
2324 priv->ChannelPlan = priv->RegChannelPlan;
2328 // Used PID and DID to Set CustomerID
2330 if( priv->eeprom_vid == 0x1186 && priv->eeprom_did == 0x3304 )
2332 priv->CustomerID = RT_CID_DLINK;
2335 switch(priv->eeprom_CustomerID)
2337 case EEPROM_CID_DEFAULT:
2338 priv->CustomerID = RT_CID_DEFAULT;
2340 case EEPROM_CID_CAMEO:
2341 priv->CustomerID = RT_CID_819x_CAMEO;
2343 case EEPROM_CID_RUNTOP:
2344 priv->CustomerID = RT_CID_819x_RUNTOP;
2346 case EEPROM_CID_NetCore:
2347 priv->CustomerID = RT_CID_819x_Netcore;
2349 case EEPROM_CID_TOSHIBA: // Merge by Jacken, 2008/01/31
2350 priv->CustomerID = RT_CID_TOSHIBA;
2351 if(priv->eeprom_ChannelPlan&0x80)
2352 priv->ChannelPlan = priv->eeprom_ChannelPlan&0x7f;
2354 priv->ChannelPlan = 0x0;
2355 RT_TRACE(COMP_INIT, "Toshiba ChannelPlan = 0x%x\n",
2358 case EEPROM_CID_Nettronix:
2359 priv->CustomerID = RT_CID_Nettronix;
2361 case EEPROM_CID_Pronet:
2362 priv->CustomerID = RT_CID_PRONET;
2364 case EEPROM_CID_DLINK:
2365 priv->CustomerID = RT_CID_DLINK;
2368 case EEPROM_CID_WHQL:
2371 // value from RegCustomerID
2375 //Avoid the channel plan array overflow, by Bruce, 2007-08-27.
2376 if(priv->ChannelPlan > CHANNEL_PLAN_LEN - 1)
2377 priv->ChannelPlan = 0; //FCC
2379 if( priv->eeprom_vid == 0x1186 && priv->eeprom_did == 0x3304)
2380 priv->ieee80211->bSupportRemoteWakeUp = true;
2382 priv->ieee80211->bSupportRemoteWakeUp = false;
2385 RT_TRACE(COMP_INIT, "RegChannelPlan(%d)\n", priv->RegChannelPlan);
2386 RT_TRACE(COMP_INIT, "ChannelPlan = %d\n", priv->ChannelPlan);
2387 RT_TRACE(COMP_TRACE, "<==== ReadAdapterInfo\n");
2391 static short rtl8192_get_channel_map(struct r8192_priv *priv)
2393 #ifdef ENABLE_DOT11D
2394 if(priv->ChannelPlan> COUNTRY_CODE_GLOBAL_DOMAIN){
2395 printk("rtl8180_init:Error channel plan! Set to default.\n");
2396 priv->ChannelPlan= 0;
2398 RT_TRACE(COMP_INIT, "Channel plan is %d\n",priv->ChannelPlan);
2400 rtl819x_set_channel_map(priv->ChannelPlan, priv);
2403 //Set Default Channel Plan
2405 DMESG("No channels, aborting");
2409 priv->ChannelPlan= 0;//hikaru
2410 // set channels 1..14 allowed in given locale
2411 for (i=1; i<=14; i++) {
2412 (priv->ieee80211->channel_map)[i] = (u8)(ch & 0x01);
2419 static short rtl8192_init(struct r8192_priv *priv)
2421 struct net_device *dev = priv->ieee80211->dev;
2423 memset(&(priv->stats),0,sizeof(struct Stats));
2424 rtl8192_init_priv_variable(priv);
2425 rtl8192_init_priv_lock(priv);
2426 rtl8192_init_priv_task(priv);
2427 rtl8192_get_eeprom_size(priv);
2428 rtl8192_read_eeprom_info(priv);
2429 rtl8192_get_channel_map(priv);
2431 init_timer(&priv->watch_dog_timer);
2432 priv->watch_dog_timer.data = (unsigned long)priv;
2433 priv->watch_dog_timer.function = watch_dog_timer_callback;
2434 if (request_irq(dev->irq, rtl8192_interrupt, IRQF_SHARED, dev->name, dev)) {
2435 printk("Error allocating IRQ %d",dev->irq);
2439 printk("IRQ %d",dev->irq);
2441 if (rtl8192_pci_initdescring(priv) != 0){
2442 printk("Endopoints initialization failed");
2450 * Actually only set RRSR, RATR and BW_OPMODE registers
2451 * not to do all the hw config as its name says
2452 * This part need to modified according to the rate set we filtered
2454 static void rtl8192_hwconfig(struct r8192_priv *priv)
2456 u32 regRATR = 0, regRRSR = 0;
2457 u8 regBwOpMode = 0, regTmp = 0;
2459 // Set RRSR, RATR, and BW_OPMODE registers
2461 switch (priv->ieee80211->mode)
2463 case WIRELESS_MODE_B:
2464 regBwOpMode = BW_OPMODE_20MHZ;
2465 regRATR = RATE_ALL_CCK;
2466 regRRSR = RATE_ALL_CCK;
2468 case WIRELESS_MODE_A:
2469 regBwOpMode = BW_OPMODE_5G |BW_OPMODE_20MHZ;
2470 regRATR = RATE_ALL_OFDM_AG;
2471 regRRSR = RATE_ALL_OFDM_AG;
2473 case WIRELESS_MODE_G:
2474 regBwOpMode = BW_OPMODE_20MHZ;
2475 regRATR = RATE_ALL_CCK | RATE_ALL_OFDM_AG;
2476 regRRSR = RATE_ALL_CCK | RATE_ALL_OFDM_AG;
2478 case WIRELESS_MODE_AUTO:
2479 case WIRELESS_MODE_N_24G:
2480 // It support CCK rate by default.
2481 // CCK rate will be filtered out only when associated AP does not support it.
2482 regBwOpMode = BW_OPMODE_20MHZ;
2483 regRATR = RATE_ALL_CCK | RATE_ALL_OFDM_AG | RATE_ALL_OFDM_1SS | RATE_ALL_OFDM_2SS;
2484 regRRSR = RATE_ALL_CCK | RATE_ALL_OFDM_AG;
2486 case WIRELESS_MODE_N_5G:
2487 regBwOpMode = BW_OPMODE_5G;
2488 regRATR = RATE_ALL_OFDM_AG | RATE_ALL_OFDM_1SS | RATE_ALL_OFDM_2SS;
2489 regRRSR = RATE_ALL_OFDM_AG;
2493 write_nic_byte(priv, BW_OPMODE, regBwOpMode);
2496 ratr_value = regRATR;
2497 if (priv->rf_type == RF_1T2R)
2499 ratr_value &= ~(RATE_ALL_OFDM_2SS);
2501 write_nic_dword(priv, RATR0, ratr_value);
2502 write_nic_byte(priv, UFWP, 1);
2504 regTmp = read_nic_byte(priv, 0x313);
2505 regRRSR = ((regTmp) << 24) | (regRRSR & 0x00ffffff);
2506 write_nic_dword(priv, RRSR, regRRSR);
2509 // Set Retry Limit here
2511 write_nic_word(priv, RETRY_LIMIT,
2512 priv->ShortRetryLimit << RETRY_LIMIT_SHORT_SHIFT |
2513 priv->LongRetryLimit << RETRY_LIMIT_LONG_SHIFT);
2514 // Set Contention Window here
2518 // Set Tx Antenna including Feedback control
2520 // Set Auto Rate fallback control
2526 static RT_STATUS rtl8192_adapter_start(struct r8192_priv *priv)
2528 struct net_device *dev = priv->ieee80211->dev;
2530 RT_STATUS rtStatus = RT_STATUS_SUCCESS;
2532 u8 ICVersion,SwitchingRegulatorOutput;
2533 bool bfirmwareok = true;
2534 u32 tmpRegA, tmpRegC, TempCCk;
2537 RT_TRACE(COMP_INIT, "====>%s()\n", __FUNCTION__);
2538 priv->being_init_adapter = true;
2539 rtl8192_pci_resetdescring(priv);
2540 // 2007/11/02 MH Before initalizing RF. We can not use FW to do RF-R/W.
2541 priv->Rf_Mode = RF_OP_By_SW_3wire;
2544 if(priv->ResetProgress == RESET_TYPE_NORESET)
2546 write_nic_byte(priv, ANAPAR, 0x37);
2547 // Accordign to designer's explain, LBUS active will never > 10ms. We delay 10ms
2548 // Joseph increae the time to prevent firmware download fail
2552 //PlatformSleepUs(10000);
2553 // For any kind of InitializeAdapter process, we shall use system now!!
2554 priv->pFirmware->firmware_status = FW_STATUS_0_INIT;
2557 //3 //Config CPUReset Register
2559 //3 Firmware Reset Or Not
2560 ulRegRead = read_nic_dword(priv, CPU_GEN);
2561 if(priv->pFirmware->firmware_status == FW_STATUS_0_INIT)
2562 { //called from MPInitialized. do nothing
2563 ulRegRead |= CPU_GEN_SYSTEM_RESET;
2564 }else if(priv->pFirmware->firmware_status == FW_STATUS_5_READY)
2565 ulRegRead |= CPU_GEN_FIRMWARE_RESET; // Called from MPReset
2567 RT_TRACE(COMP_ERR, "ERROR in %s(): undefined firmware state(%d)\n", __FUNCTION__, priv->pFirmware->firmware_status);
2569 write_nic_dword(priv, CPU_GEN, ulRegRead);
2572 //3 //Fix the issue of E-cut high temperature issue
2575 ICVersion = read_nic_byte(priv, IC_VERRSION);
2576 if(ICVersion >= 0x4) //E-cut only
2578 // HW SD suggest that we should not wirte this register too often, so driver
2579 // should readback this register. This register will be modified only when
2581 SwitchingRegulatorOutput = read_nic_byte(priv, SWREGULATOR);
2582 if(SwitchingRegulatorOutput != 0xb8)
2584 write_nic_byte(priv, SWREGULATOR, 0xa8);
2586 write_nic_byte(priv, SWREGULATOR, 0xb8);
2591 //3// Initialize BB before MAC
2593 RT_TRACE(COMP_INIT, "BB Config Start!\n");
2594 rtStatus = rtl8192_BBConfig(priv);
2595 if(rtStatus != RT_STATUS_SUCCESS)
2597 RT_TRACE(COMP_ERR, "BB Config failed\n");
2600 RT_TRACE(COMP_INIT,"BB Config Finished!\n");
2602 //3//Set Loopback mode or Normal mode
2604 //2006.12.13 by emily. Note!We should not merge these two CPU_GEN register writings
2605 // because setting of System_Reset bit reset MAC to default transmission mode.
2606 //Loopback mode or not
2607 priv->LoopbackMode = RTL819X_NO_LOOPBACK;
2608 if(priv->ResetProgress == RESET_TYPE_NORESET)
2610 ulRegRead = read_nic_dword(priv, CPU_GEN);
2611 if(priv->LoopbackMode == RTL819X_NO_LOOPBACK)
2613 ulRegRead = ((ulRegRead & CPU_GEN_NO_LOOPBACK_MSK) | CPU_GEN_NO_LOOPBACK_SET);
2615 else if (priv->LoopbackMode == RTL819X_MAC_LOOPBACK )
2617 ulRegRead |= CPU_CCK_LOOPBACK;
2621 RT_TRACE(COMP_ERR,"Serious error: wrong loopback mode setting\n");
2624 //2008.06.03, for WOL
2625 //ulRegRead &= (~(CPU_GEN_GPIO_UART));
2626 write_nic_dword(priv, CPU_GEN, ulRegRead);
2628 // 2006.11.29. After reset cpu, we sholud wait for a second, otherwise, it may fail to write registers. Emily
2631 //3Set Hardware(Do nothing now)
2632 rtl8192_hwconfig(priv);
2633 //2=======================================================
2634 // Common Setting for all of the FPGA platform. (part 1)
2635 //2=======================================================
2636 // If there is changes, please make sure it applies to all of the FPGA version
2638 write_nic_byte(priv, CMDR, CR_RE|CR_TE);
2641 write_nic_byte(priv, PCIF, ((MXDMA2_NoLimit<<MXDMA2_RX_SHIFT) |
2642 (MXDMA2_NoLimit<<MXDMA2_TX_SHIFT) ));
2645 write_nic_dword(priv, MAC0, ((u32*)dev->dev_addr)[0]);
2646 write_nic_word(priv, MAC4, ((u16*)(dev->dev_addr + 4))[0]);
2648 write_nic_dword(priv, RCR, priv->ReceiveConfig);
2650 //3 Initialize Number of Reserved Pages in Firmware Queue
2651 write_nic_dword(priv, RQPN1, NUM_OF_PAGE_IN_FW_QUEUE_BK << RSVD_FW_QUEUE_PAGE_BK_SHIFT |
2652 NUM_OF_PAGE_IN_FW_QUEUE_BE << RSVD_FW_QUEUE_PAGE_BE_SHIFT |
2653 NUM_OF_PAGE_IN_FW_QUEUE_VI << RSVD_FW_QUEUE_PAGE_VI_SHIFT |
2654 NUM_OF_PAGE_IN_FW_QUEUE_VO <<RSVD_FW_QUEUE_PAGE_VO_SHIFT);
2655 write_nic_dword(priv, RQPN2, NUM_OF_PAGE_IN_FW_QUEUE_MGNT << RSVD_FW_QUEUE_PAGE_MGNT_SHIFT);
2656 write_nic_dword(priv, RQPN3, APPLIED_RESERVED_QUEUE_IN_FW|
2657 NUM_OF_PAGE_IN_FW_QUEUE_BCN<<RSVD_FW_QUEUE_PAGE_BCN_SHIFT|
2658 NUM_OF_PAGE_IN_FW_QUEUE_PUB<<RSVD_FW_QUEUE_PAGE_PUB_SHIFT);
2660 rtl8192_tx_enable(priv);
2661 rtl8192_rx_enable(priv);
2662 //3Set Response Rate Setting Register
2663 // CCK rate is supported by default.
2664 // CCK rate will be filtered out only when associated AP does not support it.
2665 ulRegRead = (0xFFF00000 & read_nic_dword(priv, RRSR)) | RATE_ALL_OFDM_AG | RATE_ALL_CCK;
2666 write_nic_dword(priv, RRSR, ulRegRead);
2667 write_nic_dword(priv, RATR0+4*7, (RATE_ALL_OFDM_AG | RATE_ALL_CCK));
2670 // TODO: (it value is only for FPGA version). need to be changed!!2006.12.18, by Emily
2671 write_nic_byte(priv, ACK_TIMEOUT, 0x30);
2673 if(priv->ResetProgress == RESET_TYPE_NORESET)
2674 rtl8192_SetWirelessMode(dev, priv->ieee80211->mode);
2675 //-----------------------------------------------------------------------------
2676 // Set up security related. 070106, by rcnjko:
2677 // 1. Clear all H/W keys.
2678 // 2. Enable H/W encryption/decryption.
2679 //-----------------------------------------------------------------------------
2680 CamResetAllEntry(priv);
2682 u8 SECR_value = 0x0;
2683 SECR_value |= SCR_TxEncEnable;
2684 SECR_value |= SCR_RxDecEnable;
2685 SECR_value |= SCR_NoSKMC;
2686 write_nic_byte(priv, SECR, SECR_value);
2689 write_nic_word(priv, ATIMWND, 2);
2690 write_nic_word(priv, BCN_INTERVAL, 100);
2691 for (i=0; i<QOS_QUEUE_NUM; i++)
2692 write_nic_dword(priv, WDCAPARA_ADD[i], 0x005e4332);
2694 // Switching regulator controller: This is set temporarily.
2695 // It's not sure if this can be removed in the future.
2696 // PJ advised to leave it by default.
2698 write_nic_byte(priv, 0xbe, 0xc0);
2700 //2=======================================================
2701 // Set PHY related configuration defined in MAC register bank
2702 //2=======================================================
2703 rtl8192_phy_configmac(priv);
2705 if (priv->card_8192_version > (u8) VERSION_8190_BD) {
2706 rtl8192_phy_getTxPower(priv);
2707 rtl8192_phy_setTxPower(priv, priv->chan);
2711 tmpvalue = read_nic_byte(priv, IC_VERRSION);
2712 priv->IC_Cut = tmpvalue;
2713 RT_TRACE(COMP_INIT, "priv->IC_Cut = 0x%x\n", priv->IC_Cut);
2714 if(priv->IC_Cut >= IC_VersionCut_D)
2716 //pHalData->bDcut = TRUE;
2717 if(priv->IC_Cut == IC_VersionCut_D)
2718 RT_TRACE(COMP_INIT, "D-cut\n");
2719 if(priv->IC_Cut == IC_VersionCut_E)
2721 RT_TRACE(COMP_INIT, "E-cut\n");
2722 // HW SD suggest that we should not wirte this register too often, so driver
2723 // should readback this register. This register will be modified only when
2729 //pHalData->bDcut = FALSE;
2730 RT_TRACE(COMP_INIT, "Before C-cut\n");
2734 RT_TRACE(COMP_INIT, "Load Firmware!\n");
2735 bfirmwareok = init_firmware(dev);
2736 if(bfirmwareok != true) {
2737 rtStatus = RT_STATUS_FAILURE;
2740 RT_TRACE(COMP_INIT, "Load Firmware finished!\n");
2743 if(priv->ResetProgress == RESET_TYPE_NORESET)
2745 RT_TRACE(COMP_INIT, "RF Config Started!\n");
2746 rtStatus = rtl8192_phy_RFConfig(priv);
2747 if(rtStatus != RT_STATUS_SUCCESS)
2749 RT_TRACE(COMP_ERR, "RF Config failed\n");
2752 RT_TRACE(COMP_INIT, "RF Config Finished!\n");
2754 rtl8192_phy_updateInitGain(priv);
2756 /*---- Set CCK and OFDM Block "ON"----*/
2757 rtl8192_setBBreg(priv, rFPGA0_RFMOD, bCCKEn, 0x1);
2758 rtl8192_setBBreg(priv, rFPGA0_RFMOD, bOFDMEn, 0x1);
2761 write_nic_byte(priv, 0x87, 0x0);
2763 //2=======================================================
2765 //2=======================================================
2769 if(priv->RfOffReason > RF_CHANGE_BY_PS)
2770 { // H/W or S/W RF OFF before sleep.
2771 RT_TRACE((COMP_INIT|COMP_RF|COMP_POWER), "%s(): Turn off RF for RfOffReason(%d)\n", __FUNCTION__,priv->RfOffReason);
2772 MgntActSet_RF_State(priv, eRfOff, priv->RfOffReason);
2774 else if(priv->RfOffReason >= RF_CHANGE_BY_IPS)
2775 { // H/W or S/W RF OFF before sleep.
2776 RT_TRACE((COMP_INIT|COMP_RF|COMP_POWER), "%s(): Turn off RF for RfOffReason(%d)\n", __FUNCTION__, priv->RfOffReason);
2777 MgntActSet_RF_State(priv, eRfOff, priv->RfOffReason);
2781 RT_TRACE((COMP_INIT|COMP_RF|COMP_POWER), "%s(): RF-ON \n",__FUNCTION__);
2782 priv->eRFPowerState = eRfOn;
2783 priv->RfOffReason = 0;
2787 // We can force firmware to do RF-R/W
2788 if(priv->ieee80211->FwRWRF)
2789 priv->Rf_Mode = RF_OP_By_FW;
2791 priv->Rf_Mode = RF_OP_By_SW_3wire;
2793 if(priv->ResetProgress == RESET_TYPE_NORESET)
2795 dm_initialize_txpower_tracking(priv);
2797 if(priv->IC_Cut >= IC_VersionCut_D)
2799 tmpRegA = rtl8192_QueryBBReg(priv, rOFDM0_XATxIQImbalance, bMaskDWord);
2800 tmpRegC = rtl8192_QueryBBReg(priv, rOFDM0_XCTxIQImbalance, bMaskDWord);
2801 for(i = 0; i<TxBBGainTableLength; i++)
2803 if(tmpRegA == priv->txbbgain_table[i].txbbgain_value)
2805 priv->rfa_txpowertrackingindex= (u8)i;
2806 priv->rfa_txpowertrackingindex_real= (u8)i;
2807 priv->rfa_txpowertracking_default = priv->rfa_txpowertrackingindex;
2812 TempCCk = rtl8192_QueryBBReg(priv, rCCK0_TxFilter1, bMaskByte2);
2814 for(i=0 ; i<CCKTxBBGainTableLength ; i++)
2816 if(TempCCk == priv->cck_txbbgain_table[i].ccktxbb_valuearray[0])
2818 priv->CCKPresentAttentuation_20Mdefault =(u8) i;
2822 priv->CCKPresentAttentuation_40Mdefault = 0;
2823 priv->CCKPresentAttentuation_difference = 0;
2824 priv->CCKPresentAttentuation = priv->CCKPresentAttentuation_20Mdefault;
2825 RT_TRACE(COMP_POWER_TRACKING, "priv->rfa_txpowertrackingindex_initial = %d\n", priv->rfa_txpowertrackingindex);
2826 RT_TRACE(COMP_POWER_TRACKING, "priv->rfa_txpowertrackingindex_real__initial = %d\n", priv->rfa_txpowertrackingindex_real);
2827 RT_TRACE(COMP_POWER_TRACKING, "priv->CCKPresentAttentuation_difference_initial = %d\n", priv->CCKPresentAttentuation_difference);
2828 RT_TRACE(COMP_POWER_TRACKING, "priv->CCKPresentAttentuation_initial = %d\n", priv->CCKPresentAttentuation);
2829 priv->btxpower_tracking = FALSE;//TEMPLY DISABLE
2833 rtl8192_irq_enable(priv);
2834 priv->being_init_adapter = false;
2839 static void rtl8192_prepare_beacon(unsigned long arg)
2841 struct r8192_priv *priv = (struct r8192_priv*) arg;
2842 struct sk_buff *skb;
2845 skb = ieee80211_get_beacon(priv->ieee80211);
2846 tcb_desc = (cb_desc *)(skb->cb + 8);
2847 /* prepare misc info for the beacon xmit */
2848 tcb_desc->queue_index = BEACON_QUEUE;
2849 /* IBSS does not support HT yet, use 1M defaultly */
2850 tcb_desc->data_rate = 2;
2851 tcb_desc->RATRIndex = 7;
2852 tcb_desc->bTxDisableRateFallBack = 1;
2853 tcb_desc->bTxUseDriverAssingedRate = 1;
2855 skb_push(skb, priv->ieee80211->tx_headroom);
2857 rtl8192_tx(priv, skb);
2863 * configure registers for beacon tx and enables it via
2864 * rtl8192_beacon_tx_enable(). rtl8192_beacon_tx_disable() might
2865 * be used to stop beacon transmission
2867 static void rtl8192_start_beacon(struct net_device *dev)
2869 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
2870 struct ieee80211_network *net = &priv->ieee80211->current_network;
2875 DMESG("Enabling beacon TX");
2876 rtl8192_irq_disable(priv);
2877 //rtl8192_beacon_tx_enable(dev);
2880 write_nic_word(priv, ATIMWND, 2);
2882 /* Beacon interval (in unit of TU) */
2883 write_nic_word(priv, BCN_INTERVAL, net->beacon_interval);
2886 * DrvErlyInt (in unit of TU).
2887 * (Time to send interrupt to notify driver to c
2888 * hange beacon content)
2890 write_nic_word(priv, BCN_DRV_EARLY_INT, 10);
2893 * BcnDMATIM(in unit of us).
2894 * Indicates the time before TBTT to perform beacon queue DMA
2896 write_nic_word(priv, BCN_DMATIME, 256);
2899 * Force beacon frame transmission even after receiving
2900 * beacon frame from other ad hoc STA
2902 write_nic_byte(priv, BCN_ERR_THRESH, 100);
2904 /* Set CW and IFS */
2905 BcnTimeCfg |= BcnCW<<BCN_TCFG_CW_SHIFT;
2906 BcnTimeCfg |= BcnIFS<<BCN_TCFG_IFS;
2907 write_nic_word(priv, BCN_TCFG, BcnTimeCfg);
2910 /* enable the interrupt for ad-hoc process */
2911 rtl8192_irq_enable(priv);
2914 static bool HalRxCheckStuck8190Pci(struct r8192_priv *priv)
2916 u16 RegRxCounter = read_nic_word(priv, 0x130);
2917 bool bStuck = FALSE;
2919 RT_TRACE(COMP_RESET,"%s(): RegRxCounter is %d,RxCounter is %d\n",__FUNCTION__,RegRxCounter,priv->RxCounter);
2920 // If rssi is small, we should check rx for long time because of bad rx.
2921 // or maybe it will continuous silent reset every 2 seconds.
2923 if(priv->undecorated_smoothed_pwdb >= (RateAdaptiveTH_High+5))
2925 priv->rx_chk_cnt = 0; /* high rssi, check rx stuck right now. */
2927 else if(priv->undecorated_smoothed_pwdb < (RateAdaptiveTH_High+5) &&
2928 ((priv->CurrentChannelBW!=HT_CHANNEL_WIDTH_20&&priv->undecorated_smoothed_pwdb>=RateAdaptiveTH_Low_40M) ||
2929 (priv->CurrentChannelBW==HT_CHANNEL_WIDTH_20&&priv->undecorated_smoothed_pwdb>=RateAdaptiveTH_Low_20M)) )
2932 if(priv->rx_chk_cnt < 2)
2938 priv->rx_chk_cnt = 0;
2941 else if(((priv->CurrentChannelBW!=HT_CHANNEL_WIDTH_20&&priv->undecorated_smoothed_pwdb<RateAdaptiveTH_Low_40M) ||
2942 (priv->CurrentChannelBW==HT_CHANNEL_WIDTH_20&&priv->undecorated_smoothed_pwdb<RateAdaptiveTH_Low_20M)) &&
2943 priv->undecorated_smoothed_pwdb >= VeryLowRSSI)
2945 if(priv->rx_chk_cnt < 4)
2951 priv->rx_chk_cnt = 0;
2956 if(priv->rx_chk_cnt < 8)
2962 priv->rx_chk_cnt = 0;
2965 if(priv->RxCounter==RegRxCounter)
2968 priv->RxCounter = RegRxCounter;
2973 static RESET_TYPE RxCheckStuck(struct r8192_priv *priv)
2976 if(HalRxCheckStuck8190Pci(priv))
2978 RT_TRACE(COMP_RESET, "RxStuck Condition\n");
2979 return RESET_TYPE_SILENT;
2982 return RESET_TYPE_NORESET;
2986 rtl819x_ifcheck_resetornot(struct r8192_priv *priv)
2988 RESET_TYPE TxResetType = RESET_TYPE_NORESET;
2989 RESET_TYPE RxResetType = RESET_TYPE_NORESET;
2990 RT_RF_POWER_STATE rfState;
2992 rfState = priv->eRFPowerState;
2994 if( rfState != eRfOff &&
2995 /*ADAPTER_TEST_STATUS_FLAG(Adapter, ADAPTER_STATUS_FW_DOWNLOAD_FAILURE)) &&*/
2996 (priv->ieee80211->iw_mode != IW_MODE_ADHOC))
2998 // If driver is in the status of firmware download failure , driver skips RF initialization and RF is
2999 // in turned off state. Driver should check whether Rx stuck and do silent reset. And
3000 // if driver is in firmware download failure status, driver should initialize RF in the following
3001 // silent reset procedure Emily, 2008.01.21
3003 // Driver should not check RX stuck in IBSS mode because it is required to
3004 // set Check BSSID in order to send beacon, however, if check BSSID is
3005 // set, STA cannot hear any packet a all. Emily, 2008.04.12
3006 RxResetType = RxCheckStuck(priv);
3009 RT_TRACE(COMP_RESET,"%s(): TxResetType is %d, RxResetType is %d\n",__FUNCTION__,TxResetType,RxResetType);
3010 if(TxResetType==RESET_TYPE_NORMAL || RxResetType==RESET_TYPE_NORMAL)
3011 return RESET_TYPE_NORMAL;
3012 else if(TxResetType==RESET_TYPE_SILENT || RxResetType==RESET_TYPE_SILENT)
3013 return RESET_TYPE_SILENT;
3015 return RESET_TYPE_NORESET;
3020 static void InactivePsWorkItemCallback(struct r8192_priv *priv)
3022 PRT_POWER_SAVE_CONTROL pPSC = &priv->PowerSaveControl;
3024 RT_TRACE(COMP_POWER, "InactivePsWorkItemCallback() --------->\n");
3026 // This flag "bSwRfProcessing", indicates the status of IPS procedure, should be set if the IPS workitem
3027 // is really scheduled.
3028 // The old code, sets this flag before scheduling the IPS workitem and however, at the same time the
3029 // previous IPS workitem did not end yet, fails to schedule the current workitem. Thus, bSwRfProcessing
3030 // blocks the IPS procedure of switching RF.
3031 // By Bruce, 2007-12-25.
3033 pPSC->bSwRfProcessing = TRUE;
3035 RT_TRACE(COMP_RF, "InactivePsWorkItemCallback(): Set RF to %s.\n",
3036 pPSC->eInactivePowerState == eRfOff?"OFF":"ON");
3039 MgntActSet_RF_State(priv, pPSC->eInactivePowerState, RF_CHANGE_BY_IPS);
3042 // To solve CAM values miss in RF OFF, rewrite CAM values after RF ON. By Bruce, 2007-09-20.
3044 pPSC->bSwRfProcessing = FALSE;
3045 RT_TRACE(COMP_POWER, "InactivePsWorkItemCallback() <---------\n");
3049 /* Change current and default preamble mode. */
3050 bool MgntActSet_802_11_PowerSaveMode(struct r8192_priv *priv, u8 rtPsMode)
3053 // Currently, we do not change power save mode on IBSS mode.
3054 if(priv->ieee80211->iw_mode == IW_MODE_ADHOC)
3060 // <RJ_NOTE> If we make HW to fill up the PwrMgt bit for us,
3061 // some AP will not response to our mgnt frames with PwrMgt bit set,
3062 // e.g. cannot associate the AP.
3063 // So I commented out it. 2005.02.16, by rcnjko.
3065 // // Change device's power save mode.
3066 // Adapter->HalFunc.SetPSModeHandler( Adapter, rtPsMode );
3068 // Update power save mode configured.
3069 //RT_TRACE(COMP_LPS,"%s(): set ieee->ps = %x\n",__FUNCTION__,rtPsMode);
3070 if(!priv->ps_force) {
3071 priv->ieee80211->ps = rtPsMode;
3074 // Awake immediately
3075 if(priv->ieee80211->sta_sleep != 0 && rtPsMode == IEEE80211_PS_DISABLED)
3077 // Notify the AP we awke.
3078 rtl8192_hw_wakeup(priv->ieee80211->dev);
3079 priv->ieee80211->sta_sleep = 0;
3081 spin_lock(&priv->ieee80211->mgmt_tx_lock);
3082 printk("LPS leave: notify AP we are awaked ++++++++++ SendNullFunctionData\n");
3083 ieee80211_sta_ps_send_null_frame(priv->ieee80211, 0);
3084 spin_unlock(&priv->ieee80211->mgmt_tx_lock);
3090 /* Enter the leisure power save mode. */
3091 void LeisurePSEnter(struct net_device *dev)
3093 struct r8192_priv *priv = ieee80211_priv(dev);
3094 PRT_POWER_SAVE_CONTROL pPSC = &priv->PowerSaveControl;
3096 if(!((priv->ieee80211->iw_mode == IW_MODE_INFRA) &&
3097 (priv->ieee80211->state == IEEE80211_LINKED)) ||
3098 (priv->ieee80211->iw_mode == IW_MODE_ADHOC) ||
3099 (priv->ieee80211->iw_mode == IW_MODE_MASTER))
3102 if (pPSC->bLeisurePs)
3104 // Idle for a while if we connect to AP a while ago.
3105 if(pPSC->LpsIdleCount >= RT_CHECK_FOR_HANG_PERIOD) // 4 Sec
3108 if(priv->ieee80211->ps == IEEE80211_PS_DISABLED)
3110 MgntActSet_802_11_PowerSaveMode(priv, IEEE80211_PS_MBCAST|IEEE80211_PS_UNICAST);
3115 pPSC->LpsIdleCount++;
3120 /* Leave leisure power save mode. */
3121 void LeisurePSLeave(struct net_device *dev)
3123 struct r8192_priv *priv = ieee80211_priv(dev);
3124 PRT_POWER_SAVE_CONTROL pPSC = &priv->PowerSaveControl;
3126 if (pPSC->bLeisurePs)
3128 if(priv->ieee80211->ps != IEEE80211_PS_DISABLED)
3130 // move to lps_wakecomplete()
3131 MgntActSet_802_11_PowerSaveMode(priv, IEEE80211_PS_DISABLED);
3139 /* Enter the inactive power save mode. RF will be off */
3140 void IPSEnter(struct r8192_priv *priv)
3142 PRT_POWER_SAVE_CONTROL pPSC = &priv->PowerSaveControl;
3143 RT_RF_POWER_STATE rtState;
3145 if (pPSC->bInactivePs)
3147 rtState = priv->eRFPowerState;
3149 // Added by Bruce, 2007-12-25.
3150 // Do not enter IPS in the following conditions:
3151 // (1) RF is already OFF or Sleep
3152 // (2) bSwRfProcessing (indicates the IPS is still under going)
3153 // (3) Connectted (only disconnected can trigger IPS)
3154 // (4) IBSS (send Beacon)
3155 // (5) AP mode (send Beacon)
3157 if (rtState == eRfOn && !pPSC->bSwRfProcessing
3158 && (priv->ieee80211->state != IEEE80211_LINKED) )
3160 RT_TRACE(COMP_RF,"IPSEnter(): Turn off RF.\n");
3161 pPSC->eInactivePowerState = eRfOff;
3162 // queue_work(priv->priv_wq,&(pPSC->InactivePsWorkItem));
3163 InactivePsWorkItemCallback(priv);
3170 // Leave the inactive power save mode, RF will be on.
3171 // 2007.08.17, by shien chang.
3173 void IPSLeave(struct r8192_priv *priv)
3175 PRT_POWER_SAVE_CONTROL pPSC = &priv->PowerSaveControl;
3176 RT_RF_POWER_STATE rtState;
3178 if (pPSC->bInactivePs)
3180 rtState = priv->eRFPowerState;
3181 if (rtState != eRfOn && !pPSC->bSwRfProcessing && priv->RfOffReason <= RF_CHANGE_BY_IPS)
3183 RT_TRACE(COMP_POWER, "IPSLeave(): Turn on RF.\n");
3184 pPSC->eInactivePowerState = eRfOn;
3185 InactivePsWorkItemCallback(priv);
3190 void IPSLeave_wq(struct work_struct *work)
3192 struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, ips_leave_wq);
3193 struct net_device *dev = ieee->dev;
3195 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
3196 down(&priv->ieee80211->ips_sem);
3198 up(&priv->ieee80211->ips_sem);
3201 void ieee80211_ips_leave_wq(struct net_device *dev)
3203 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
3204 RT_RF_POWER_STATE rtState;
3205 rtState = priv->eRFPowerState;
3207 if (priv->PowerSaveControl.bInactivePs){
3208 if(rtState == eRfOff){
3209 if(priv->RfOffReason > RF_CHANGE_BY_IPS)
3211 RT_TRACE(COMP_ERR, "%s(): RF is OFF.\n",__FUNCTION__);
3215 printk("=========>%s(): IPSLeave\n",__FUNCTION__);
3216 queue_work(priv->ieee80211->wq,&priv->ieee80211->ips_leave_wq);
3221 //added by amy 090331 end
3222 void ieee80211_ips_leave(struct net_device *dev)
3224 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
3225 down(&priv->ieee80211->ips_sem);
3227 up(&priv->ieee80211->ips_sem);
3231 static void rtl819x_update_rxcounts(
3232 struct r8192_priv *priv,
3241 *TotalRxDataNum = 0;
3243 SlotIndex = (priv->ieee80211->LinkDetectInfo.SlotIndex++)%(priv->ieee80211->LinkDetectInfo.SlotNum);
3244 priv->ieee80211->LinkDetectInfo.RxBcnNum[SlotIndex] = priv->ieee80211->LinkDetectInfo.NumRecvBcnInPeriod;
3245 priv->ieee80211->LinkDetectInfo.RxDataNum[SlotIndex] = priv->ieee80211->LinkDetectInfo.NumRecvDataInPeriod;
3246 for( i=0; i<priv->ieee80211->LinkDetectInfo.SlotNum; i++ ){
3247 *TotalRxBcnNum += priv->ieee80211->LinkDetectInfo.RxBcnNum[i];
3248 *TotalRxDataNum += priv->ieee80211->LinkDetectInfo.RxDataNum[i];
3253 static void rtl819x_watchdog_wqcallback(struct work_struct *work)
3255 struct delayed_work *dwork = container_of(work,struct delayed_work,work);
3256 struct r8192_priv *priv = container_of(dwork,struct r8192_priv,watch_dog_wq);
3257 struct net_device *dev = priv->ieee80211->dev;
3258 struct ieee80211_device* ieee = priv->ieee80211;
3259 RESET_TYPE ResetType = RESET_TYPE_NORESET;
3260 bool bBusyTraffic = false;
3261 bool bEnterPS = false;
3263 if ((!priv->up) || priv->bHwRadioOff)
3268 hal_dm_watchdog(dev);
3270 if(ieee->actscanning == false){
3271 if((ieee->iw_mode == IW_MODE_INFRA) && (ieee->state == IEEE80211_NOLINK) &&
3272 (priv->eRFPowerState == eRfOn) && !ieee->is_set_key &&
3273 (!ieee->proto_stoppping) && !ieee->wx_set_enc){
3274 if (priv->PowerSaveControl.ReturnPoint == IPS_CALLBACK_NONE){
3280 {//to get busy traffic condition
3281 if(ieee->state == IEEE80211_LINKED)
3283 if( ieee->LinkDetectInfo.NumRxOkInPeriod> 100 ||
3284 ieee->LinkDetectInfo.NumTxOkInPeriod> 100 ) {
3285 bBusyTraffic = true;
3289 //added by amy for Leisure PS
3290 if( ((ieee->LinkDetectInfo.NumRxUnicastOkInPeriod + ieee->LinkDetectInfo.NumTxOkInPeriod) > 8 ) ||
3291 (ieee->LinkDetectInfo.NumRxUnicastOkInPeriod > 2) )
3300 // LeisurePS only work in infra mode.
3303 LeisurePSEnter(dev);
3307 LeisurePSLeave(dev);
3315 LeisurePSLeave(dev);
3319 ieee->LinkDetectInfo.NumRxOkInPeriod = 0;
3320 ieee->LinkDetectInfo.NumTxOkInPeriod = 0;
3321 ieee->LinkDetectInfo.NumRxUnicastOkInPeriod = 0;
3322 ieee->LinkDetectInfo.bBusyTraffic = bBusyTraffic;
3326 //added by amy for AP roaming
3327 if(ieee->state == IEEE80211_LINKED && ieee->iw_mode == IW_MODE_INFRA)
3329 u32 TotalRxBcnNum = 0;
3330 u32 TotalRxDataNum = 0;
3332 rtl819x_update_rxcounts(priv, &TotalRxBcnNum, &TotalRxDataNum);
3333 if((TotalRxBcnNum+TotalRxDataNum) == 0)
3335 if (priv->eRFPowerState == eRfOff)
3336 RT_TRACE(COMP_ERR,"========>%s()\n",__FUNCTION__);
3337 printk("===>%s(): AP is power off,connect another one\n",__FUNCTION__);
3338 // Dot11d_Reset(dev);
3339 ieee->state = IEEE80211_ASSOCIATING;
3340 notify_wx_assoc_event(priv->ieee80211);
3341 RemovePeerTS(priv->ieee80211,priv->ieee80211->current_network.bssid);
3342 ieee->is_roaming = true;
3343 ieee->is_set_key = false;
3344 ieee->link_change(dev);
3345 queue_work(ieee->wq, &ieee->associate_procedure_wq);
3348 ieee->LinkDetectInfo.NumRecvBcnInPeriod=0;
3349 ieee->LinkDetectInfo.NumRecvDataInPeriod=0;
3351 //check if reset the driver
3352 if (priv->watchdog_check_reset_cnt++ >= 3 && !ieee->is_roaming &&
3353 priv->watchdog_last_time != 1)
3355 ResetType = rtl819x_ifcheck_resetornot(priv);
3356 priv->watchdog_check_reset_cnt = 3;
3358 if(!priv->bDisableNormalResetCheck && ResetType == RESET_TYPE_NORMAL)
3360 priv->ResetProgress = RESET_TYPE_NORMAL;
3361 RT_TRACE(COMP_RESET,"%s(): NOMAL RESET\n",__FUNCTION__);
3364 /* disable silent reset temply 2008.9.11*/
3366 if( ((priv->force_reset) || (!priv->bDisableNormalResetCheck && ResetType==RESET_TYPE_SILENT))) // This is control by OID set in Pomelo
3368 priv->watchdog_last_time = 1;
3371 priv->watchdog_last_time = 0;
3373 priv->force_reset = false;
3374 priv->bForcedSilentReset = false;
3375 priv->bResetInProgress = false;
3376 RT_TRACE(COMP_TRACE, " <==RtUsbCheckForHangWorkItemCallback()\n");
3380 void watch_dog_timer_callback(unsigned long data)
3382 struct r8192_priv *priv = (struct r8192_priv *) data;
3383 queue_delayed_work(priv->priv_wq,&priv->watch_dog_wq,0);
3384 mod_timer(&priv->watch_dog_timer, jiffies + MSECS(IEEE80211_WATCH_DOG_TIME));
3388 static int _rtl8192_up(struct r8192_priv *priv)
3390 RT_STATUS init_status = RT_STATUS_SUCCESS;
3391 struct net_device *dev = priv->ieee80211->dev;
3394 priv->ieee80211->ieee_up=1;
3395 priv->bdisable_nic = false; //YJ,add,091111
3396 RT_TRACE(COMP_INIT, "Bringing up iface\n");
3398 init_status = rtl8192_adapter_start(priv);
3399 if(init_status != RT_STATUS_SUCCESS)
3401 RT_TRACE(COMP_ERR,"ERR!!! %s(): initialization is failed!\n",__FUNCTION__);
3404 RT_TRACE(COMP_INIT, "start adapter finished\n");
3406 if (priv->eRFPowerState != eRfOn)
3407 MgntActSet_RF_State(priv, eRfOn, priv->RfOffReason);
3409 if(priv->ieee80211->state != IEEE80211_LINKED)
3410 ieee80211_softmac_start_protocol(priv->ieee80211);
3411 ieee80211_reset_queue(priv->ieee80211);
3412 watch_dog_timer_callback((unsigned long) priv);
3413 if(!netif_queue_stopped(dev))
3414 netif_start_queue(dev);
3416 netif_wake_queue(dev);
3422 static int rtl8192_open(struct net_device *dev)
3424 struct r8192_priv *priv = ieee80211_priv(dev);
3427 down(&priv->wx_sem);
3428 ret = rtl8192_up(dev);
3435 int rtl8192_up(struct net_device *dev)
3437 struct r8192_priv *priv = ieee80211_priv(dev);
3439 if (priv->up == 1) return -1;
3441 return _rtl8192_up(priv);
3445 static int rtl8192_close(struct net_device *dev)
3447 struct r8192_priv *priv = ieee80211_priv(dev);
3450 down(&priv->wx_sem);
3452 ret = rtl8192_down(dev);
3460 int rtl8192_down(struct net_device *dev)
3462 struct r8192_priv *priv = ieee80211_priv(dev);
3464 if (priv->up == 0) return -1;
3467 //LZM for PS-Poll AID issue. 090429
3468 if(priv->ieee80211->state == IEEE80211_LINKED)
3469 LeisurePSLeave(dev);
3473 priv->ieee80211->ieee_up = 0;
3474 RT_TRACE(COMP_DOWN, "==========>%s()\n", __FUNCTION__);
3476 if (!netif_queue_stopped(dev))
3477 netif_stop_queue(dev);
3479 rtl8192_irq_disable(priv);
3480 rtl8192_cancel_deferred_work(priv);
3482 del_timer_sync(&priv->watch_dog_timer);
3484 ieee80211_softmac_stop_protocol(priv->ieee80211,true);
3486 rtl8192_halt_adapter(priv, false);
3487 memset(&priv->ieee80211->current_network, 0 , offsetof(struct ieee80211_network, list));
3489 RT_TRACE(COMP_DOWN, "<==========%s()\n", __FUNCTION__);
3495 void rtl8192_commit(struct r8192_priv *priv)
3497 if (priv->up == 0) return ;
3500 ieee80211_softmac_stop_protocol(priv->ieee80211,true);
3502 rtl8192_irq_disable(priv);
3503 rtl8192_halt_adapter(priv, true);
3507 static void rtl8192_restart(struct work_struct *work)
3509 struct r8192_priv *priv = container_of(work, struct r8192_priv, reset_wq);
3511 down(&priv->wx_sem);
3513 rtl8192_commit(priv);
3518 static void r8192_set_multicast(struct net_device *dev)
3520 struct r8192_priv *priv = ieee80211_priv(dev);
3522 priv->promisc = (dev->flags & IFF_PROMISC) ? 1 : 0;
3526 static int r8192_set_mac_adr(struct net_device *dev, void *mac)
3528 struct r8192_priv *priv = ieee80211_priv(dev);
3529 struct sockaddr *addr = mac;
3531 down(&priv->wx_sem);
3533 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
3535 schedule_work(&priv->reset_wq);
3541 static void r8192e_set_hw_key(struct r8192_priv *priv, struct ieee_param *ipw)
3543 struct ieee80211_device *ieee = priv->ieee80211;
3544 u8 broadcast_addr[6] = {0xff,0xff,0xff,0xff,0xff,0xff};
3547 if (ipw->u.crypt.set_tx) {
3548 if (strcmp(ipw->u.crypt.alg, "CCMP") == 0)
3549 ieee->pairwise_key_type = KEY_TYPE_CCMP;
3550 else if (strcmp(ipw->u.crypt.alg, "TKIP") == 0)
3551 ieee->pairwise_key_type = KEY_TYPE_TKIP;
3552 else if (strcmp(ipw->u.crypt.alg, "WEP") == 0) {
3553 if (ipw->u.crypt.key_len == 13)
3554 ieee->pairwise_key_type = KEY_TYPE_WEP104;
3555 else if (ipw->u.crypt.key_len == 5)
3556 ieee->pairwise_key_type = KEY_TYPE_WEP40;
3558 ieee->pairwise_key_type = KEY_TYPE_NA;
3560 if (ieee->pairwise_key_type) {
3561 memcpy(key, ipw->u.crypt.key, 16);
3562 EnableHWSecurityConfig8192(priv);
3564 * We fill both index entry and 4th entry for pairwise
3565 * key as in IPW interface, adhoc will only get here,
3566 * so we need index entry for its default key serching!
3568 setKey(priv, 4, ipw->u.crypt.idx,
3569 ieee->pairwise_key_type,
3570 (u8*)ieee->ap_mac_addr, 0, key);
3572 /* LEAP WEP will never set this. */
3573 if (ieee->auth_mode != 2)
3574 setKey(priv, ipw->u.crypt.idx, ipw->u.crypt.idx,
3575 ieee->pairwise_key_type,
3576 (u8*)ieee->ap_mac_addr, 0, key);
3578 if ((ieee->pairwise_key_type == KEY_TYPE_CCMP) &&
3579 ieee->pHTInfo->bCurrentHTSupport) {
3580 write_nic_byte(priv, 0x173, 1); /* fix aes bug */
3583 memcpy(key, ipw->u.crypt.key, 16);
3584 if (strcmp(ipw->u.crypt.alg, "CCMP") == 0)
3585 ieee->group_key_type= KEY_TYPE_CCMP;
3586 else if (strcmp(ipw->u.crypt.alg, "TKIP") == 0)
3587 ieee->group_key_type = KEY_TYPE_TKIP;
3588 else if (strcmp(ipw->u.crypt.alg, "WEP") == 0) {
3589 if (ipw->u.crypt.key_len == 13)
3590 ieee->group_key_type = KEY_TYPE_WEP104;
3591 else if (ipw->u.crypt.key_len == 5)
3592 ieee->group_key_type = KEY_TYPE_WEP40;
3594 ieee->group_key_type = KEY_TYPE_NA;
3596 if (ieee->group_key_type) {
3597 setKey(priv, ipw->u.crypt.idx, ipw->u.crypt.idx,
3598 ieee->group_key_type, broadcast_addr, 0, key);
3603 /* based on ipw2200 driver */
3604 static int rtl8192_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3606 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
3607 struct iwreq *wrq = (struct iwreq *)rq;
3609 struct iw_point *p = &wrq->u.data;
3610 struct ieee_param *ipw = NULL;//(struct ieee_param *)wrq->u.data.pointer;
3612 down(&priv->wx_sem);
3615 if (p->length < sizeof(struct ieee_param) || !p->pointer){
3620 ipw = kmalloc(p->length, GFP_KERNEL);
3625 if (copy_from_user(ipw, p->pointer, p->length)) {
3632 case RTL_IOCTL_WPA_SUPPLICANT:
3633 /* parse here for HW security */
3634 if (ipw->cmd == IEEE_CMD_SET_ENCRYPTION)
3635 r8192e_set_hw_key(priv, ipw);
3636 ret = ieee80211_wpa_supplicant_ioctl(priv->ieee80211, &wrq->u.data);
3651 static u8 HwRateToMRate90(bool bIsHT, u8 rate)
3657 case DESC90_RATE1M: ret_rate = MGN_1M; break;
3658 case DESC90_RATE2M: ret_rate = MGN_2M; break;
3659 case DESC90_RATE5_5M: ret_rate = MGN_5_5M; break;
3660 case DESC90_RATE11M: ret_rate = MGN_11M; break;
3661 case DESC90_RATE6M: ret_rate = MGN_6M; break;
3662 case DESC90_RATE9M: ret_rate = MGN_9M; break;
3663 case DESC90_RATE12M: ret_rate = MGN_12M; break;
3664 case DESC90_RATE18M: ret_rate = MGN_18M; break;
3665 case DESC90_RATE24M: ret_rate = MGN_24M; break;
3666 case DESC90_RATE36M: ret_rate = MGN_36M; break;
3667 case DESC90_RATE48M: ret_rate = MGN_48M; break;
3668 case DESC90_RATE54M: ret_rate = MGN_54M; break;
3671 RT_TRACE(COMP_RECV, "HwRateToMRate90(): Non supported Rate [%x], bIsHT = %d!!!\n", rate, bIsHT);
3677 case DESC90_RATEMCS0: ret_rate = MGN_MCS0; break;
3678 case DESC90_RATEMCS1: ret_rate = MGN_MCS1; break;
3679 case DESC90_RATEMCS2: ret_rate = MGN_MCS2; break;
3680 case DESC90_RATEMCS3: ret_rate = MGN_MCS3; break;
3681 case DESC90_RATEMCS4: ret_rate = MGN_MCS4; break;
3682 case DESC90_RATEMCS5: ret_rate = MGN_MCS5; break;
3683 case DESC90_RATEMCS6: ret_rate = MGN_MCS6; break;
3684 case DESC90_RATEMCS7: ret_rate = MGN_MCS7; break;
3685 case DESC90_RATEMCS8: ret_rate = MGN_MCS8; break;
3686 case DESC90_RATEMCS9: ret_rate = MGN_MCS9; break;
3687 case DESC90_RATEMCS10: ret_rate = MGN_MCS10; break;
3688 case DESC90_RATEMCS11: ret_rate = MGN_MCS11; break;
3689 case DESC90_RATEMCS12: ret_rate = MGN_MCS12; break;
3690 case DESC90_RATEMCS13: ret_rate = MGN_MCS13; break;
3691 case DESC90_RATEMCS14: ret_rate = MGN_MCS14; break;
3692 case DESC90_RATEMCS15: ret_rate = MGN_MCS15; break;
3693 case DESC90_RATEMCS32: ret_rate = (0x80|0x20); break;
3696 RT_TRACE(COMP_RECV, "HwRateToMRate90(): Non supported Rate [%x], bIsHT = %d!!!\n",rate, bIsHT);
3704 /* Record the TSF time stamp when receiving a packet */
3705 static void UpdateRxPktTimeStamp8190(struct r8192_priv *priv, struct ieee80211_rx_stats *stats)
3708 if(stats->bIsAMPDU && !stats->bFirstMPDU) {
3709 stats->mac_time[0] = priv->LastRxDescTSFLow;
3710 stats->mac_time[1] = priv->LastRxDescTSFHigh;
3712 priv->LastRxDescTSFLow = stats->mac_time[0];
3713 priv->LastRxDescTSFHigh = stats->mac_time[1];
3717 static long rtl819x_translate_todbm(u8 signal_strength_index)// 0-100 index.
3719 long signal_power; // in dBm.
3721 // Translate to dBm (x=0.5y-95).
3722 signal_power = (long)((signal_strength_index + 1) >> 1);
3725 return signal_power;
3728 /* 2008/01/22 MH We can not delcare RSSI/EVM total value of sliding window to
3729 be a local static. Otherwise, it may increase when we return from S3/S4. The
3730 value will be kept in memory or disk. We must delcare the value in adapter
3731 and it will be reinitialized when return from S3/S4. */
3732 static void rtl8192_process_phyinfo(struct r8192_priv * priv, u8* buffer,struct ieee80211_rx_stats * pprevious_stats, struct ieee80211_rx_stats * pcurrent_stats)
3734 bool bcheck = false;
3736 u32 nspatial_stream, tmp_val;
3737 static u32 slide_rssi_index=0, slide_rssi_statistics=0;
3738 static u32 slide_evm_index=0, slide_evm_statistics=0;
3739 static u32 last_rssi=0, last_evm=0;
3740 //cosa add for beacon rssi smoothing
3741 static u32 slide_beacon_adc_pwdb_index=0, slide_beacon_adc_pwdb_statistics=0;
3742 static u32 last_beacon_adc_pwdb=0;
3744 struct ieee80211_hdr_3addr *hdr;
3746 unsigned int frag,seq;
3747 hdr = (struct ieee80211_hdr_3addr *)buffer;
3748 sc = le16_to_cpu(hdr->seq_ctl);
3749 frag = WLAN_GET_SEQ_FRAG(sc);
3750 seq = WLAN_GET_SEQ_SEQ(sc);
3751 //cosa add 04292008 to record the sequence number
3752 pcurrent_stats->Seq_Num = seq;
3754 // Check whether we should take the previous packet into accounting
3756 if(!pprevious_stats->bIsAMPDU)
3758 // if previous packet is not aggregated packet
3762 if(slide_rssi_statistics++ >= PHY_RSSI_SLID_WIN_MAX)
3764 slide_rssi_statistics = PHY_RSSI_SLID_WIN_MAX;
3765 last_rssi = priv->stats.slide_signal_strength[slide_rssi_index];
3766 priv->stats.slide_rssi_total -= last_rssi;
3768 priv->stats.slide_rssi_total += pprevious_stats->SignalStrength;
3770 priv->stats.slide_signal_strength[slide_rssi_index++] = pprevious_stats->SignalStrength;
3771 if(slide_rssi_index >= PHY_RSSI_SLID_WIN_MAX)
3772 slide_rssi_index = 0;
3774 // <1> Showed on UI for user, in dbm
3775 tmp_val = priv->stats.slide_rssi_total/slide_rssi_statistics;
3776 priv->stats.signal_strength = rtl819x_translate_todbm((u8)tmp_val);
3777 pcurrent_stats->rssi = priv->stats.signal_strength;
3779 // If the previous packet does not match the criteria, neglect it
3781 if(!pprevious_stats->bPacketMatchBSSID)
3783 if(!pprevious_stats->bToSelfBA)
3790 // <2> Showed on UI for engineering
3791 // hardware does not provide rssi information for each rf path in CCK
3792 if(!pprevious_stats->bIsCCK && pprevious_stats->bPacketToSelf)
3794 for (rfpath = RF90_PATH_A; rfpath < RF90_PATH_C; rfpath++)
3796 if (!rtl8192_phy_CheckIsLegalRFPath(priv, rfpath))
3798 RT_TRACE(COMP_DBG, "pPreviousstats->RxMIMOSignalStrength[rfpath] = %d\n", pprevious_stats->RxMIMOSignalStrength[rfpath]);
3799 //Fixed by Jacken 2008-03-20
3800 if(priv->stats.rx_rssi_percentage[rfpath] == 0)
3802 priv->stats.rx_rssi_percentage[rfpath] = pprevious_stats->RxMIMOSignalStrength[rfpath];
3804 if(pprevious_stats->RxMIMOSignalStrength[rfpath] > priv->stats.rx_rssi_percentage[rfpath])
3806 priv->stats.rx_rssi_percentage[rfpath] =
3807 ( (priv->stats.rx_rssi_percentage[rfpath]*(Rx_Smooth_Factor-1)) +
3808 (pprevious_stats->RxMIMOSignalStrength[rfpath])) /(Rx_Smooth_Factor);
3809 priv->stats.rx_rssi_percentage[rfpath] = priv->stats.rx_rssi_percentage[rfpath] + 1;
3813 priv->stats.rx_rssi_percentage[rfpath] =
3814 ( (priv->stats.rx_rssi_percentage[rfpath]*(Rx_Smooth_Factor-1)) +
3815 (pprevious_stats->RxMIMOSignalStrength[rfpath])) /(Rx_Smooth_Factor);
3817 RT_TRACE(COMP_DBG, "priv->RxStats.RxRSSIPercentage[rfPath] = %d \n" , priv->stats.rx_rssi_percentage[rfpath]);
3825 //cosa add for beacon rssi smoothing by average.
3826 if(pprevious_stats->bPacketBeacon)
3828 /* record the beacon pwdb to the sliding window. */
3829 if(slide_beacon_adc_pwdb_statistics++ >= PHY_Beacon_RSSI_SLID_WIN_MAX)
3831 slide_beacon_adc_pwdb_statistics = PHY_Beacon_RSSI_SLID_WIN_MAX;
3832 last_beacon_adc_pwdb = priv->stats.Slide_Beacon_pwdb[slide_beacon_adc_pwdb_index];
3833 priv->stats.Slide_Beacon_Total -= last_beacon_adc_pwdb;
3834 // slide_beacon_adc_pwdb_index, last_beacon_adc_pwdb, Adapter->RxStats.Slide_Beacon_Total);
3836 priv->stats.Slide_Beacon_Total += pprevious_stats->RxPWDBAll;
3837 priv->stats.Slide_Beacon_pwdb[slide_beacon_adc_pwdb_index] = pprevious_stats->RxPWDBAll;
3838 slide_beacon_adc_pwdb_index++;
3839 if(slide_beacon_adc_pwdb_index >= PHY_Beacon_RSSI_SLID_WIN_MAX)
3840 slide_beacon_adc_pwdb_index = 0;
3841 pprevious_stats->RxPWDBAll = priv->stats.Slide_Beacon_Total/slide_beacon_adc_pwdb_statistics;
3842 if(pprevious_stats->RxPWDBAll >= 3)
3843 pprevious_stats->RxPWDBAll -= 3;
3846 RT_TRACE(COMP_RXDESC, "Smooth %s PWDB = %d\n",
3847 pprevious_stats->bIsCCK? "CCK": "OFDM",
3848 pprevious_stats->RxPWDBAll);
3850 if(pprevious_stats->bPacketToSelf || pprevious_stats->bPacketBeacon || pprevious_stats->bToSelfBA)
3852 if(priv->undecorated_smoothed_pwdb < 0) // initialize
3854 priv->undecorated_smoothed_pwdb = pprevious_stats->RxPWDBAll;
3857 if(pprevious_stats->RxPWDBAll > (u32)priv->undecorated_smoothed_pwdb)
3859 priv->undecorated_smoothed_pwdb =
3860 ( ((priv->undecorated_smoothed_pwdb)*(Rx_Smooth_Factor-1)) +
3861 (pprevious_stats->RxPWDBAll)) /(Rx_Smooth_Factor);
3862 priv->undecorated_smoothed_pwdb = priv->undecorated_smoothed_pwdb + 1;
3866 priv->undecorated_smoothed_pwdb =
3867 ( ((priv->undecorated_smoothed_pwdb)*(Rx_Smooth_Factor-1)) +
3868 (pprevious_stats->RxPWDBAll)) /(Rx_Smooth_Factor);
3875 /* record the general EVM to the sliding window. */
3876 if(pprevious_stats->SignalQuality == 0)
3881 if(pprevious_stats->bPacketToSelf || pprevious_stats->bPacketBeacon || pprevious_stats->bToSelfBA){
3882 if(slide_evm_statistics++ >= PHY_RSSI_SLID_WIN_MAX){
3883 slide_evm_statistics = PHY_RSSI_SLID_WIN_MAX;
3884 last_evm = priv->stats.slide_evm[slide_evm_index];
3885 priv->stats.slide_evm_total -= last_evm;
3888 priv->stats.slide_evm_total += pprevious_stats->SignalQuality;
3890 priv->stats.slide_evm[slide_evm_index++] = pprevious_stats->SignalQuality;
3891 if(slide_evm_index >= PHY_RSSI_SLID_WIN_MAX)
3892 slide_evm_index = 0;
3894 // <1> Showed on UI for user, in percentage.
3895 tmp_val = priv->stats.slide_evm_total/slide_evm_statistics;
3896 //cosa add 10/11/2007, Showed on UI for user in Windows Vista, for Link quality.
3899 // <2> Showed on UI for engineering
3900 if(pprevious_stats->bPacketToSelf || pprevious_stats->bPacketBeacon || pprevious_stats->bToSelfBA)
3902 for(nspatial_stream = 0; nspatial_stream<2 ; nspatial_stream++) // 2 spatial stream
3904 if(pprevious_stats->RxMIMOSignalQuality[nspatial_stream] != -1)
3906 if(priv->stats.rx_evm_percentage[nspatial_stream] == 0) // initialize
3908 priv->stats.rx_evm_percentage[nspatial_stream] = pprevious_stats->RxMIMOSignalQuality[nspatial_stream];
3910 priv->stats.rx_evm_percentage[nspatial_stream] =
3911 ( (priv->stats.rx_evm_percentage[nspatial_stream]* (Rx_Smooth_Factor-1)) +
3912 (pprevious_stats->RxMIMOSignalQuality[nspatial_stream]* 1)) / (Rx_Smooth_Factor);
3920 static u8 rtl819x_query_rxpwrpercentage(
3924 if ((antpower <= -100) || (antpower >= 20))
3928 else if (antpower >= 0)
3934 return (100+antpower);
3940 rtl819x_evm_dbtopercentage(
3952 ret_val = 0 - ret_val;
3959 /* We want good-looking for signal strength/quality */
3960 static long rtl819x_signal_scale_mapping(long currsig)
3964 // Step 1. Scale mapping.
3965 if(currsig >= 61 && currsig <= 100)
3967 retsig = 90 + ((currsig - 60) / 4);
3969 else if(currsig >= 41 && currsig <= 60)
3971 retsig = 78 + ((currsig - 40) / 2);
3973 else if(currsig >= 31 && currsig <= 40)
3975 retsig = 66 + (currsig - 30);
3977 else if(currsig >= 21 && currsig <= 30)
3979 retsig = 54 + (currsig - 20);
3981 else if(currsig >= 5 && currsig <= 20)
3983 retsig = 42 + (((currsig - 5) * 2) / 3);
3985 else if(currsig == 4)
3989 else if(currsig == 3)
3993 else if(currsig == 2)
3997 else if(currsig == 1)
4009 static void rtl8192_query_rxphystatus(
4010 struct r8192_priv * priv,
4011 struct ieee80211_rx_stats * pstats,
4012 prx_desc_819x_pci pdesc,
4013 prx_fwinfo_819x_pci pdrvinfo,
4014 struct ieee80211_rx_stats * precord_stats,
4015 bool bpacket_match_bssid,
4016 bool bpacket_toself,
4021 //PRT_RFD_STATUS pRtRfdStatus = &(pRfd->Status);
4022 phy_sts_ofdm_819xpci_t* pofdm_buf;
4023 phy_sts_cck_819xpci_t * pcck_buf;
4024 phy_ofdm_rx_status_rxsc_sgien_exintfflag* prxsc;
4026 u8 i,max_spatial_stream, tmp_rxsnr, tmp_rxevm, rxsc_sgien_exflg;
4027 char rx_pwr[4], rx_pwr_all=0;
4028 //long rx_avg_pwr = 0;
4029 char rx_snrX, rx_evmX;
4031 u32 RSSI, total_rssi=0;//, total_evm=0;
4032 // long signal_strength_index = 0;
4036 is_cck_rate = rx_hal_is_cck_rate(pdrvinfo);
4038 // Record it for next packet processing
4039 memset(precord_stats, 0, sizeof(struct ieee80211_rx_stats));
4040 pstats->bPacketMatchBSSID = precord_stats->bPacketMatchBSSID = bpacket_match_bssid;
4041 pstats->bPacketToSelf = precord_stats->bPacketToSelf = bpacket_toself;
4042 pstats->bIsCCK = precord_stats->bIsCCK = is_cck_rate;//RX_HAL_IS_CCK_RATE(pDrvInfo);
4043 pstats->bPacketBeacon = precord_stats->bPacketBeacon = bPacketBeacon;
4044 pstats->bToSelfBA = precord_stats->bToSelfBA = bToSelfBA;
4045 /*2007.08.30 requested by SD3 Jerry */
4046 if (priv->phy_check_reg824 == 0)
4048 priv->phy_reg824_bit9 = rtl8192_QueryBBReg(priv, rFPGA0_XA_HSSIParameter2, 0x200);
4049 priv->phy_check_reg824 = 1;
4053 prxpkt = (u8*)pdrvinfo;
4055 /* Move pointer to the 16th bytes. Phy status start address. */
4056 prxpkt += sizeof(rx_fwinfo_819x_pci);
4058 /* Initial the cck and ofdm buffer pointer */
4059 pcck_buf = (phy_sts_cck_819xpci_t *)prxpkt;
4060 pofdm_buf = (phy_sts_ofdm_819xpci_t *)prxpkt;
4062 pstats->RxMIMOSignalQuality[0] = -1;
4063 pstats->RxMIMOSignalQuality[1] = -1;
4064 precord_stats->RxMIMOSignalQuality[0] = -1;
4065 precord_stats->RxMIMOSignalQuality[1] = -1;
4070 // (1)Hardware does not provide RSSI for CCK
4074 // (2)PWDB, Average PWDB cacluated by hardware (for rate adaptive)
4076 u8 report;//, cck_agc_rpt;
4078 if (!priv->phy_reg824_bit9)
4080 report = pcck_buf->cck_agc_rpt & 0xc0;
4084 //Fixed by Jacken from Bryant 2008-03-20
4085 //Original value is -38 , -26 , -14 , -2
4086 //Fixed value is -35 , -23 , -11 , 6
4088 rx_pwr_all = -35 - (pcck_buf->cck_agc_rpt & 0x3e);
4091 rx_pwr_all = -23 - (pcck_buf->cck_agc_rpt & 0x3e);
4094 rx_pwr_all = -11 - (pcck_buf->cck_agc_rpt & 0x3e);
4097 rx_pwr_all = 8 - (pcck_buf->cck_agc_rpt & 0x3e);
4103 report = pcck_buf->cck_agc_rpt & 0x60;
4108 rx_pwr_all = -35 - ((pcck_buf->cck_agc_rpt & 0x1f)<<1) ;
4111 rx_pwr_all = -23 - ((pcck_buf->cck_agc_rpt & 0x1f)<<1);
4114 rx_pwr_all = -11 - ((pcck_buf->cck_agc_rpt & 0x1f)<<1) ;
4117 rx_pwr_all = -8 - ((pcck_buf->cck_agc_rpt & 0x1f)<<1) ;
4122 pwdb_all = rtl819x_query_rxpwrpercentage(rx_pwr_all);
4123 pstats->RxPWDBAll = precord_stats->RxPWDBAll = pwdb_all;
4124 pstats->RecvSignalPower = rx_pwr_all;
4127 // (3) Get Signal Quality (EVM)
4129 if(bpacket_match_bssid)
4133 if(pstats->RxPWDBAll > 40)
4138 sq = pcck_buf->sq_rpt;
4140 if(pcck_buf->sq_rpt > 64)
4142 else if (pcck_buf->sq_rpt < 20)
4145 sq = ((64-sq) * 100) / 44;
4147 pstats->SignalQuality = precord_stats->SignalQuality = sq;
4148 pstats->RxMIMOSignalQuality[0] = precord_stats->RxMIMOSignalQuality[0] = sq;
4149 pstats->RxMIMOSignalQuality[1] = precord_stats->RxMIMOSignalQuality[1] = -1;
4155 // (1)Get RSSI for HT rate
4157 for(i=RF90_PATH_A; i<RF90_PATH_MAX; i++)
4159 // 2008/01/30 MH we will judge RF RX path now.
4160 if (priv->brfpath_rxenable[i])
4165 //Fixed by Jacken from Bryant 2008-03-20
4166 //Original value is 106
4167 rx_pwr[i] = ((pofdm_buf->trsw_gain_X[i]&0x3F)*2) - 110;
4169 //Get Rx snr value in DB
4170 tmp_rxsnr = pofdm_buf->rxsnr_X[i];
4171 rx_snrX = (char)(tmp_rxsnr);
4174 /* Translate DBM to percentage. */
4175 RSSI = rtl819x_query_rxpwrpercentage(rx_pwr[i]);
4176 if (priv->brfpath_rxenable[i])
4179 /* Record Signal Strength for next packet */
4180 if(bpacket_match_bssid)
4182 pstats->RxMIMOSignalStrength[i] =(u8) RSSI;
4183 precord_stats->RxMIMOSignalStrength[i] =(u8) RSSI;
4189 // (2)PWDB, Average PWDB cacluated by hardware (for rate adaptive)
4191 //Fixed by Jacken from Bryant 2008-03-20
4192 //Original value is 106
4193 rx_pwr_all = (((pofdm_buf->pwdb_all ) >> 1 )& 0x7f) -106;
4194 pwdb_all = rtl819x_query_rxpwrpercentage(rx_pwr_all);
4196 pstats->RxPWDBAll = precord_stats->RxPWDBAll = pwdb_all;
4197 pstats->RxPower = precord_stats->RxPower = rx_pwr_all;
4198 pstats->RecvSignalPower = rx_pwr_all;
4200 // (3)EVM of HT rate
4202 if(pdrvinfo->RxHT && pdrvinfo->RxRate>=DESC90_RATEMCS8 &&
4203 pdrvinfo->RxRate<=DESC90_RATEMCS15)
4204 max_spatial_stream = 2; //both spatial stream make sense
4206 max_spatial_stream = 1; //only spatial stream 1 makes sense
4208 for(i=0; i<max_spatial_stream; i++)
4210 tmp_rxevm = pofdm_buf->rxevm_X[i];
4211 rx_evmX = (char)(tmp_rxevm);
4213 // Do not use shift operation like "rx_evmX >>= 1" because the compilor of free build environment
4214 // fill most significant bit to "zero" when doing shifting operation which may change a negative
4215 // value to positive one, then the dbm value (which is supposed to be negative) is not correct anymore.
4218 evm = rtl819x_evm_dbtopercentage(rx_evmX);
4219 if(bpacket_match_bssid)
4221 if(i==0) // Fill value in RFD, Get the first spatial stream only
4222 pstats->SignalQuality = precord_stats->SignalQuality = (u8)(evm & 0xff);
4223 pstats->RxMIMOSignalQuality[i] = precord_stats->RxMIMOSignalQuality[i] = (u8)(evm & 0xff);
4228 /* record rx statistics for debug */
4229 rxsc_sgien_exflg = pofdm_buf->rxsc_sgien_exflg;
4230 prxsc = (phy_ofdm_rx_status_rxsc_sgien_exintfflag *)&rxsc_sgien_exflg;
4233 //UI BSS List signal strength(in percentage), make it good looking, from 0~100.
4234 //It is assigned to the BSS List in GetValueFromBeaconOrProbeRsp().
4237 pstats->SignalStrength = precord_stats->SignalStrength = (u8)(rtl819x_signal_scale_mapping((long)pwdb_all));//PWDB_ALL;
4242 //pRfd->Status.SignalStrength = pRecordRfd->Status.SignalStrength = (u1Byte)(SignalScaleMapping(total_rssi/=RF90_PATH_MAX));//(u1Byte)(total_rssi/=RF90_PATH_MAX);
4243 // We can judge RX path number now.
4245 pstats->SignalStrength = precord_stats->SignalStrength = (u8)(rtl819x_signal_scale_mapping((long)(total_rssi/=rf_rx_num)));
4250 rtl8192_record_rxdesc_forlateruse(
4251 struct ieee80211_rx_stats * psrc_stats,
4252 struct ieee80211_rx_stats * ptarget_stats
4255 ptarget_stats->bIsAMPDU = psrc_stats->bIsAMPDU;
4256 ptarget_stats->bFirstMPDU = psrc_stats->bFirstMPDU;
4257 //ptarget_stats->Seq_Num = psrc_stats->Seq_Num;
4262 static void TranslateRxSignalStuff819xpci(struct r8192_priv *priv,
4263 struct sk_buff *skb,
4264 struct ieee80211_rx_stats * pstats,
4265 prx_desc_819x_pci pdesc,
4266 prx_fwinfo_819x_pci pdrvinfo)
4268 // TODO: We must only check packet for current MAC address. Not finish
4269 bool bpacket_match_bssid, bpacket_toself;
4270 bool bPacketBeacon=false, bToSelfBA=false;
4271 struct ieee80211_hdr_3addr *hdr;
4274 // Get Signal Quality for only RX data queue (but not command queue)
4279 /* Get MAC frame start address. */
4280 tmp_buf = skb->data;
4282 hdr = (struct ieee80211_hdr_3addr *)tmp_buf;
4283 fc = le16_to_cpu(hdr->frame_ctl);
4284 type = WLAN_FC_GET_TYPE(fc);
4285 praddr = hdr->addr1;
4287 /* Check if the received packet is acceptabe. */
4288 bpacket_match_bssid = ((IEEE80211_FTYPE_CTL != type) &&
4289 (!compare_ether_addr(priv->ieee80211->current_network.bssid, (fc & IEEE80211_FCTL_TODS)? hdr->addr1 : (fc & IEEE80211_FCTL_FROMDS )? hdr->addr2 : hdr->addr3))
4290 && (!pstats->bHwError) && (!pstats->bCRC)&& (!pstats->bICV));
4291 bpacket_toself = bpacket_match_bssid & (!compare_ether_addr(praddr, priv->ieee80211->dev->dev_addr));
4293 if(WLAN_FC_GET_FRAMETYPE(fc)== IEEE80211_STYPE_BEACON)
4295 bPacketBeacon = true;
4297 if(WLAN_FC_GET_FRAMETYPE(fc) == IEEE80211_STYPE_BLOCKACK)
4299 if (!compare_ether_addr(praddr, priv->ieee80211->dev->dev_addr))
4304 // Process PHY information for previous packet (RSSI/PWDB/EVM)
4306 // Because phy information is contained in the last packet of AMPDU only, so driver
4307 // should process phy information of previous packet
4308 rtl8192_process_phyinfo(priv, tmp_buf, &priv->previous_stats, pstats);
4309 rtl8192_query_rxphystatus(priv, pstats, pdesc, pdrvinfo, &priv->previous_stats, bpacket_match_bssid,
4310 bpacket_toself ,bPacketBeacon, bToSelfBA);
4311 rtl8192_record_rxdesc_forlateruse(pstats, &priv->previous_stats);
4316 static void rtl8192_tx_resume(struct r8192_priv *priv)
4318 struct ieee80211_device *ieee = priv->ieee80211;
4319 struct net_device *dev = priv->ieee80211->dev;
4320 struct sk_buff *skb;
4323 for(queue_index = BK_QUEUE; queue_index < TXCMD_QUEUE;queue_index++) {
4324 while((!skb_queue_empty(&ieee->skb_waitQ[queue_index]))&&
4325 (priv->ieee80211->check_nic_enough_desc(dev,queue_index) > 0)) {
4326 /* 1. dequeue the packet from the wait queue */
4327 skb = skb_dequeue(&ieee->skb_waitQ[queue_index]);
4328 /* 2. tx the packet directly */
4329 ieee->softmac_data_hard_start_xmit(skb,dev,0/* rate useless now*/);
4334 static void rtl8192_irq_tx_tasklet(unsigned long arg)
4336 struct r8192_priv *priv = (struct r8192_priv*) arg;
4337 struct rtl8192_tx_ring *mgnt_ring = &priv->tx_ring[MGNT_QUEUE];
4338 struct net_device *dev = priv->ieee80211->dev;
4339 unsigned long flags;
4341 /* check if we need to report that the management queue is drained */
4342 spin_lock_irqsave(&priv->irq_th_lock, flags);
4344 if (!skb_queue_len(&mgnt_ring->queue) &&
4345 priv->ieee80211->ack_tx_to_ieee &&
4346 rtl8192_is_tx_queue_empty(dev)) {
4347 priv->ieee80211->ack_tx_to_ieee = 0;
4348 ieee80211_ps_tx_ack(priv->ieee80211, 1);
4351 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
4353 rtl8192_tx_resume(priv);
4356 /* Record the received data rate */
4357 static void UpdateReceivedRateHistogramStatistics8190(
4358 struct r8192_priv *priv,
4359 struct ieee80211_rx_stats* pstats
4362 u32 rcvType=1; //0: Total, 1:OK, 2:CRC, 3:ICV
4364 u32 preamble_guardinterval; //1: short preamble/GI, 0: long preamble/GI
4368 else if(pstats->bICV)
4371 if(pstats->bShortPreamble)
4372 preamble_guardinterval = 1;// short
4374 preamble_guardinterval = 0;// long
4376 switch(pstats->rate)
4381 case MGN_1M: rateIndex = 0; break;
4382 case MGN_2M: rateIndex = 1; break;
4383 case MGN_5_5M: rateIndex = 2; break;
4384 case MGN_11M: rateIndex = 3; break;
4388 case MGN_6M: rateIndex = 4; break;
4389 case MGN_9M: rateIndex = 5; break;
4390 case MGN_12M: rateIndex = 6; break;
4391 case MGN_18M: rateIndex = 7; break;
4392 case MGN_24M: rateIndex = 8; break;
4393 case MGN_36M: rateIndex = 9; break;
4394 case MGN_48M: rateIndex = 10; break;
4395 case MGN_54M: rateIndex = 11; break;
4397 // 11n High throughput rate
4399 case MGN_MCS0: rateIndex = 12; break;
4400 case MGN_MCS1: rateIndex = 13; break;
4401 case MGN_MCS2: rateIndex = 14; break;
4402 case MGN_MCS3: rateIndex = 15; break;
4403 case MGN_MCS4: rateIndex = 16; break;
4404 case MGN_MCS5: rateIndex = 17; break;
4405 case MGN_MCS6: rateIndex = 18; break;
4406 case MGN_MCS7: rateIndex = 19; break;
4407 case MGN_MCS8: rateIndex = 20; break;
4408 case MGN_MCS9: rateIndex = 21; break;
4409 case MGN_MCS10: rateIndex = 22; break;
4410 case MGN_MCS11: rateIndex = 23; break;
4411 case MGN_MCS12: rateIndex = 24; break;
4412 case MGN_MCS13: rateIndex = 25; break;
4413 case MGN_MCS14: rateIndex = 26; break;
4414 case MGN_MCS15: rateIndex = 27; break;
4415 default: rateIndex = 28; break;
4417 priv->stats.received_rate_histogram[0][rateIndex]++; //total
4418 priv->stats.received_rate_histogram[rcvType][rateIndex]++;
4421 static void rtl8192_rx(struct r8192_priv *priv)
4423 struct ieee80211_hdr_1addr *ieee80211_hdr = NULL;
4424 bool unicast_packet = false;
4425 struct ieee80211_rx_stats stats = {
4429 .freq = IEEE80211_24GHZ_BAND,
4431 unsigned int count = priv->rxringcount;
4432 prx_fwinfo_819x_pci pDrvInfo = NULL;
4433 struct sk_buff *new_skb;
4436 rx_desc_819x_pci *pdesc = &priv->rx_ring[priv->rx_idx];//rx descriptor
4437 struct sk_buff *skb = priv->rx_buf[priv->rx_idx];//rx pkt
4440 /* wait data to be filled by hardware */
4443 stats.bICV = pdesc->ICV;
4444 stats.bCRC = pdesc->CRC32;
4445 stats.bHwError = pdesc->CRC32 | pdesc->ICV;
4447 stats.Length = pdesc->Length;
4448 if(stats.Length < 24)
4449 stats.bHwError |= 1;
4451 if(stats.bHwError) {
4452 stats.bShift = false;
4456 new_skb = dev_alloc_skb(priv->rxbuffersize);
4458 if (unlikely(!new_skb))
4461 stats.RxDrvInfoSize = pdesc->RxDrvInfoSize;
4462 stats.RxBufShift = ((pdesc->Shift)&0x03);
4463 stats.Decrypted = !pdesc->SWDec;
4465 pci_dma_sync_single_for_cpu(priv->pdev,
4466 *((dma_addr_t *)skb->cb),
4468 PCI_DMA_FROMDEVICE);
4469 skb_put(skb, pdesc->Length);
4470 pDrvInfo = (rx_fwinfo_819x_pci *)(skb->data + stats.RxBufShift);
4471 skb_reserve(skb, stats.RxDrvInfoSize + stats.RxBufShift);
4473 stats.rate = HwRateToMRate90((bool)pDrvInfo->RxHT, (u8)pDrvInfo->RxRate);
4474 stats.bShortPreamble = pDrvInfo->SPLCP;
4476 /* it is debug only. It should be disabled in released driver.
4477 * 2007.1.11 by Emily
4479 UpdateReceivedRateHistogramStatistics8190(priv, &stats);
4481 stats.bIsAMPDU = (pDrvInfo->PartAggr==1);
4482 stats.bFirstMPDU = (pDrvInfo->PartAggr==1) && (pDrvInfo->FirstAGGR==1);
4484 stats.TimeStampLow = pDrvInfo->TSFL;
4485 stats.TimeStampHigh = read_nic_dword(priv, TSFR+4);
4487 UpdateRxPktTimeStamp8190(priv, &stats);
4490 // Get Total offset of MPDU Frame Body
4492 if((stats.RxBufShift + stats.RxDrvInfoSize) > 0)
4495 stats.RxIs40MHzPacket = pDrvInfo->BW;
4498 TranslateRxSignalStuff819xpci(priv, skb, &stats, pdesc, pDrvInfo);
4501 if(pDrvInfo->FirstAGGR==1 || pDrvInfo->PartAggr == 1)
4502 RT_TRACE(COMP_RXDESC, "pDrvInfo->FirstAGGR = %d, pDrvInfo->PartAggr = %d\n",
4503 pDrvInfo->FirstAGGR, pDrvInfo->PartAggr);
4504 skb_trim(skb, skb->len - 4/*sCrcLng*/);
4505 /* rx packets statistics */
4506 ieee80211_hdr = (struct ieee80211_hdr_1addr *)skb->data;
4507 unicast_packet = false;
4509 if(is_broadcast_ether_addr(ieee80211_hdr->addr1)) {
4511 }else if(is_multicast_ether_addr(ieee80211_hdr->addr1)){
4514 /* unicast packet */
4515 unicast_packet = true;
4518 stats.packetlength = stats.Length-4;
4519 stats.fraglength = stats.packetlength;
4520 stats.fragoffset = 0;
4521 stats.ntotalfrag = 1;
4523 if(!ieee80211_rtl_rx(priv->ieee80211, skb, &stats)){
4524 dev_kfree_skb_any(skb);
4527 if(unicast_packet) {
4528 priv->stats.rxbytesunicast += skb->len;
4532 pci_unmap_single(priv->pdev, *((dma_addr_t *) skb->cb),
4533 priv->rxbuffersize, PCI_DMA_FROMDEVICE);
4536 priv->rx_buf[priv->rx_idx] = skb;
4537 *((dma_addr_t *) skb->cb) = pci_map_single(priv->pdev, skb_tail_pointer(skb), priv->rxbuffersize, PCI_DMA_FROMDEVICE);
4540 pdesc->BufferAddress = cpu_to_le32(*((dma_addr_t *)skb->cb));
4542 pdesc->Length = priv->rxbuffersize;
4543 if (priv->rx_idx == priv->rxringcount-1)
4545 priv->rx_idx = (priv->rx_idx + 1) % priv->rxringcount;
4550 static void rtl8192_irq_rx_tasklet(unsigned long arg)
4552 struct r8192_priv *priv = (struct r8192_priv*) arg;
4555 write_nic_dword(priv, INTA_MASK, read_nic_dword(priv, INTA_MASK) | IMR_RDU);
4558 static const struct net_device_ops rtl8192_netdev_ops = {
4559 .ndo_open = rtl8192_open,
4560 .ndo_stop = rtl8192_close,
4561 .ndo_tx_timeout = tx_timeout,
4562 .ndo_do_ioctl = rtl8192_ioctl,
4563 .ndo_set_multicast_list = r8192_set_multicast,
4564 .ndo_set_mac_address = r8192_set_mac_adr,
4565 .ndo_start_xmit = ieee80211_rtl_xmit,
4568 static int __devinit rtl8192_pci_probe(struct pci_dev *pdev,
4569 const struct pci_device_id *id)
4571 unsigned long ioaddr = 0;
4572 struct net_device *dev = NULL;
4573 struct r8192_priv *priv= NULL;
4576 unsigned long pmem_start, pmem_len, pmem_flags;
4578 RT_TRACE(COMP_INIT,"Configuring chip resources\n");
4580 if( pci_enable_device (pdev) ){
4581 RT_TRACE(COMP_ERR,"Failed to enable PCI device");
4585 pci_set_master(pdev);
4586 //pci_set_wmi(pdev);
4587 pci_set_dma_mask(pdev, 0xffffff00ULL);
4588 pci_set_consistent_dma_mask(pdev,0xffffff00ULL);
4589 dev = alloc_ieee80211(sizeof(struct r8192_priv));
4595 pci_set_drvdata(pdev, dev);
4596 SET_NETDEV_DEV(dev, &pdev->dev);
4597 priv = ieee80211_priv(dev);
4598 priv->ieee80211 = netdev_priv(dev);
4600 if((pdev->subsystem_vendor == PCI_VENDOR_ID_DLINK)&&(pdev->subsystem_device == 0x3304)){
4601 priv->ieee80211->bSupportRemoteWakeUp = 1;
4604 priv->ieee80211->bSupportRemoteWakeUp = 0;
4607 pmem_start = pci_resource_start(pdev, 1);
4608 pmem_len = pci_resource_len(pdev, 1);
4609 pmem_flags = pci_resource_flags (pdev, 1);
4611 if (!(pmem_flags & IORESOURCE_MEM)) {
4612 RT_TRACE(COMP_ERR, "region #1 not a MMIO resource, aborting\n");
4616 //DMESG("Memory mapped space @ 0x%08lx ", pmem_start);
4617 if( ! request_mem_region(pmem_start, pmem_len, RTL819xE_MODULE_NAME)) {
4618 RT_TRACE(COMP_ERR,"request_mem_region failed!\n");
4623 ioaddr = (unsigned long)ioremap_nocache( pmem_start, pmem_len);
4624 if( ioaddr == (unsigned long)NULL ){
4625 RT_TRACE(COMP_ERR,"ioremap failed!\n");
4626 // release_mem_region( pmem_start, pmem_len );
4630 dev->mem_start = ioaddr; // shared mem start
4631 dev->mem_end = ioaddr + pci_resource_len(pdev, 0); // shared mem end
4633 /* We disable the RETRY_TIMEOUT register (0x41) to keep
4634 * PCI Tx retries from interfering with C3 CPU state */
4635 pci_write_config_byte(pdev, 0x41, 0x00);
4638 pci_read_config_byte(pdev, 0x05, &unit);
4639 pci_write_config_byte(pdev, 0x05, unit & (~0x04));
4641 dev->irq = pdev->irq;
4644 dev->netdev_ops = &rtl8192_netdev_ops;
4646 dev->wireless_handlers = &r8192_wx_handlers_def;
4647 dev->type=ARPHRD_ETHER;
4649 dev->watchdog_timeo = HZ*3;
4651 if (dev_alloc_name(dev, ifname) < 0){
4652 RT_TRACE(COMP_INIT, "Oops: devname already taken! Trying wlan%%d...\n");
4653 strcpy(ifname, "wlan%d");
4654 dev_alloc_name(dev, ifname);
4657 RT_TRACE(COMP_INIT, "Driver probe completed1\n");
4658 if (rtl8192_init(priv)!=0) {
4659 RT_TRACE(COMP_ERR, "Initialization failed\n");
4663 register_netdev(dev);
4664 RT_TRACE(COMP_INIT, "dev name=======> %s\n",dev->name);
4665 rtl8192_proc_init_one(priv);
4668 RT_TRACE(COMP_INIT, "Driver probe completed\n");
4673 if( dev->mem_start != (unsigned long)NULL ){
4674 iounmap( (void *)dev->mem_start );
4675 release_mem_region( pci_resource_start(pdev, 1),
4676 pci_resource_len(pdev, 1) );
4683 free_irq(dev->irq, dev);
4686 free_ieee80211(dev);
4690 pci_disable_device(pdev);
4692 DMESG("wlan driver load failed\n");
4693 pci_set_drvdata(pdev, NULL);
4698 /* detach all the work and timer structure declared or inititialized
4699 * in r8192_init function.
4701 static void rtl8192_cancel_deferred_work(struct r8192_priv* priv)
4703 /* call cancel_work_sync instead of cancel_delayed_work if and only if Linux_version_code
4704 * is or is newer than 2.6.20 and work structure is defined to be struct work_struct.
4705 * Otherwise call cancel_delayed_work is enough.
4706 * FIXME (2.6.20 should 2.6.22, work_struct should not cancel)
4708 cancel_delayed_work(&priv->watch_dog_wq);
4709 cancel_delayed_work(&priv->update_beacon_wq);
4710 cancel_delayed_work(&priv->ieee80211->hw_wakeup_wq);
4711 cancel_delayed_work(&priv->gpio_change_rf_wq);
4712 cancel_work_sync(&priv->reset_wq);
4713 cancel_work_sync(&priv->qos_activate);
4717 static void __devexit rtl8192_pci_disconnect(struct pci_dev *pdev)
4719 struct net_device *dev = pci_get_drvdata(pdev);
4720 struct r8192_priv *priv ;
4725 unregister_netdev(dev);
4727 priv = ieee80211_priv(dev);
4729 rtl8192_proc_remove_one(priv);
4732 if (priv->pFirmware)
4734 vfree(priv->pFirmware);
4735 priv->pFirmware = NULL;
4737 destroy_workqueue(priv->priv_wq);
4739 /* free tx/rx rings */
4740 rtl8192_free_rx_ring(priv);
4741 for (i = 0; i < MAX_TX_QUEUE_COUNT; i++)
4742 rtl8192_free_tx_ring(priv, i);
4745 printk("Freeing irq %d\n",dev->irq);
4746 free_irq(dev->irq, dev);
4750 if( dev->mem_start != (unsigned long)NULL ){
4751 iounmap( (void *)dev->mem_start );
4752 release_mem_region( pci_resource_start(pdev, 1),
4753 pci_resource_len(pdev, 1) );
4756 free_ieee80211(dev);
4759 pci_disable_device(pdev);
4760 RT_TRACE(COMP_DOWN, "wlan driver removed\n");
4763 extern int ieee80211_rtl_init(void);
4764 extern void ieee80211_rtl_exit(void);
4766 static int __init rtl8192_pci_module_init(void)
4770 retval = ieee80211_rtl_init();
4774 printk(KERN_INFO "\nLinux kernel driver for RTL8192 based WLAN cards\n");
4775 printk(KERN_INFO "Copyright (c) 2007-2008, Realsil Wlan\n");
4776 RT_TRACE(COMP_INIT, "Initializing module\n");
4777 rtl8192_proc_module_init();
4778 if(0!=pci_register_driver(&rtl8192_pci_driver))
4780 DMESG("No device found");
4781 /*pci_unregister_driver (&rtl8192_pci_driver);*/
4788 static void __exit rtl8192_pci_module_exit(void)
4790 pci_unregister_driver(&rtl8192_pci_driver);
4792 RT_TRACE(COMP_DOWN, "Exiting\n");
4793 rtl8192_proc_module_remove();
4794 ieee80211_rtl_exit();
4797 static irqreturn_t rtl8192_interrupt(int irq, void *netdev)
4799 struct net_device *dev = (struct net_device *) netdev;
4800 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
4801 unsigned long flags;
4803 irqreturn_t ret = IRQ_HANDLED;
4805 spin_lock_irqsave(&priv->irq_th_lock, flags);
4809 inta = read_nic_dword(priv, ISR); /* & priv->IntrMask; */
4810 write_nic_dword(priv, ISR, inta); /* reset int situation */
4814 * most probably we can safely return IRQ_NONE,
4815 * but for now is better to avoid problems
4820 if (inta == 0xffff) {
4825 if (!netif_running(dev))
4828 if (inta & IMR_TBDOK) {
4829 RT_TRACE(COMP_INTR, "beacon ok interrupt!\n");
4830 rtl8192_tx_isr(priv, BEACON_QUEUE);
4831 priv->stats.txbeaconokint++;
4834 if (inta & IMR_TBDER) {
4835 RT_TRACE(COMP_INTR, "beacon ok interrupt!\n");
4836 rtl8192_tx_isr(priv, BEACON_QUEUE);
4837 priv->stats.txbeaconerr++;
4840 if (inta & IMR_MGNTDOK ) {
4841 RT_TRACE(COMP_INTR, "Manage ok interrupt!\n");
4842 priv->stats.txmanageokint++;
4843 rtl8192_tx_isr(priv, MGNT_QUEUE);
4846 if (inta & IMR_COMDOK)
4848 priv->stats.txcmdpktokint++;
4849 rtl8192_tx_isr(priv, TXCMD_QUEUE);
4852 if (inta & IMR_ROK) {
4853 priv->stats.rxint++;
4854 tasklet_schedule(&priv->irq_rx_tasklet);
4857 if (inta & IMR_BcnInt) {
4858 RT_TRACE(COMP_INTR, "prepare beacon for interrupt!\n");
4859 tasklet_schedule(&priv->irq_prepare_beacon_tasklet);
4862 if (inta & IMR_RDU) {
4863 RT_TRACE(COMP_INTR, "rx descriptor unavailable!\n");
4864 priv->stats.rxrdu++;
4865 /* reset int situation */
4866 write_nic_dword(priv, INTA_MASK, read_nic_dword(priv, INTA_MASK) & ~IMR_RDU);
4867 tasklet_schedule(&priv->irq_rx_tasklet);
4870 if (inta & IMR_RXFOVW) {
4871 RT_TRACE(COMP_INTR, "rx overflow !\n");
4872 priv->stats.rxoverflow++;
4873 tasklet_schedule(&priv->irq_rx_tasklet);
4876 if (inta & IMR_TXFOVW)
4877 priv->stats.txoverflow++;
4879 if (inta & IMR_BKDOK) {
4880 RT_TRACE(COMP_INTR, "BK Tx OK interrupt!\n");
4881 priv->stats.txbkokint++;
4882 priv->ieee80211->LinkDetectInfo.NumTxOkInPeriod++;
4883 rtl8192_tx_isr(priv, BK_QUEUE);
4886 if (inta & IMR_BEDOK) {
4887 RT_TRACE(COMP_INTR, "BE TX OK interrupt!\n");
4888 priv->stats.txbeokint++;
4889 priv->ieee80211->LinkDetectInfo.NumTxOkInPeriod++;
4890 rtl8192_tx_isr(priv, BE_QUEUE);
4893 if (inta & IMR_VIDOK) {
4894 RT_TRACE(COMP_INTR, "VI TX OK interrupt!\n");
4895 priv->stats.txviokint++;
4896 priv->ieee80211->LinkDetectInfo.NumTxOkInPeriod++;
4897 rtl8192_tx_isr(priv, VI_QUEUE);
4900 if (inta & IMR_VODOK) {
4901 priv->stats.txvookint++;
4902 priv->ieee80211->LinkDetectInfo.NumTxOkInPeriod++;
4903 rtl8192_tx_isr(priv, VO_QUEUE);
4907 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
4912 void EnableHWSecurityConfig8192(struct r8192_priv *priv)
4914 u8 SECR_value = 0x0;
4915 struct ieee80211_device* ieee = priv->ieee80211;
4917 SECR_value = SCR_TxEncEnable | SCR_RxDecEnable;
4919 if (((KEY_TYPE_WEP40 == ieee->pairwise_key_type) || (KEY_TYPE_WEP104 == ieee->pairwise_key_type)) && (priv->ieee80211->auth_mode != 2))
4921 SECR_value |= SCR_RxUseDK;
4922 SECR_value |= SCR_TxUseDK;
4924 else if ((ieee->iw_mode == IW_MODE_ADHOC) && (ieee->pairwise_key_type & (KEY_TYPE_CCMP | KEY_TYPE_TKIP)))
4926 SECR_value |= SCR_RxUseDK;
4927 SECR_value |= SCR_TxUseDK;
4930 //add HWSec active enable here.
4931 //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
4932 ieee->hwsec_active = 1;
4934 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
4936 ieee->hwsec_active = 0;
4937 SECR_value &= ~SCR_RxDecEnable;
4940 RT_TRACE(COMP_SEC,"%s:, hwsec:%d, pairwise_key:%d, SECR_value:%x\n", __FUNCTION__,
4941 ieee->hwsec_active, ieee->pairwise_key_type, SECR_value);
4943 write_nic_byte(priv, SECR, SECR_value);//SECR_value | SCR_UseDK );
4947 #define TOTAL_CAM_ENTRY 32
4948 //#define CAM_CONTENT_COUNT 8
4949 void setKey(struct r8192_priv *priv, u8 EntryNo, u8 KeyIndex, u16 KeyType,
4950 const u8 *MacAddr, u8 DefaultKey, u32 *KeyContent)
4952 u32 TargetCommand = 0;
4953 u32 TargetContent = 0;
4957 RT_RF_POWER_STATE rtState;
4959 rtState = priv->eRFPowerState;
4960 if (priv->PowerSaveControl.bInactivePs){
4961 if(rtState == eRfOff){
4962 if(priv->RfOffReason > RF_CHANGE_BY_IPS)
4964 RT_TRACE(COMP_ERR, "%s(): RF is OFF.\n",__FUNCTION__);
4965 //up(&priv->wx_sem);
4969 down(&priv->ieee80211->ips_sem);
4971 up(&priv->ieee80211->ips_sem);
4975 priv->ieee80211->is_set_key = true;
4977 if (EntryNo >= TOTAL_CAM_ENTRY)
4978 RT_TRACE(COMP_ERR, "cam entry exceeds in setKey()\n");
4980 RT_TRACE(COMP_SEC, "====>to setKey(), priv:%p, EntryNo:%d, KeyIndex:%d, KeyType:%d, MacAddr%pM\n", priv, EntryNo, KeyIndex, KeyType, MacAddr);
4983 usConfig |= BIT15 | (KeyType<<2);
4985 usConfig |= BIT15 | (KeyType<<2) | KeyIndex;
4986 // usConfig |= BIT15 | (KeyType<<2) | (DefaultKey<<5) | KeyIndex;
4989 for(i=0 ; i<CAM_CONTENT_COUNT; i++){
4990 TargetCommand = i+CAM_CONTENT_COUNT*EntryNo;
4991 TargetCommand |= BIT31|BIT16;
4993 if(i==0){//MAC|Config
4994 TargetContent = (u32)(*(MacAddr+0)) << 16|
4995 (u32)(*(MacAddr+1)) << 24|
4998 write_nic_dword(priv, WCAMI, TargetContent);
4999 write_nic_dword(priv, RWCAM, TargetCommand);
5002 TargetContent = (u32)(*(MacAddr+2)) |
5003 (u32)(*(MacAddr+3)) << 8|
5004 (u32)(*(MacAddr+4)) << 16|
5005 (u32)(*(MacAddr+5)) << 24;
5006 write_nic_dword(priv, WCAMI, TargetContent);
5007 write_nic_dword(priv, RWCAM, TargetCommand);
5009 else { //Key Material
5010 if(KeyContent != NULL)
5012 write_nic_dword(priv, WCAMI, (u32)(*(KeyContent+i-2)) );
5013 write_nic_dword(priv, RWCAM, TargetCommand);
5017 RT_TRACE(COMP_SEC,"=========>after set key, usconfig:%x\n", usConfig);
5020 bool NicIFEnableNIC(struct r8192_priv *priv)
5022 RT_STATUS init_status = RT_STATUS_SUCCESS;
5023 PRT_POWER_SAVE_CONTROL pPSC = &priv->PowerSaveControl;
5027 RT_TRACE(COMP_ERR, "ERR!!! %s(): Driver is already down!\n",__FUNCTION__);
5028 priv->bdisable_nic = false; //YJ,add,091111
5031 // <1> Reset memory: descriptor, buffer,..
5032 //NicIFResetMemory(Adapter);
5034 // <2> Enable Adapter
5035 //priv->bfirst_init = true;
5036 init_status = rtl8192_adapter_start(priv);
5037 if (init_status != RT_STATUS_SUCCESS) {
5038 RT_TRACE(COMP_ERR,"ERR!!! %s(): initialization is failed!\n",__FUNCTION__);
5039 priv->bdisable_nic = false; //YJ,add,091111
5042 RT_CLEAR_PS_LEVEL(pPSC, RT_RF_OFF_LEVL_HALT_NIC);
5043 //priv->bfirst_init = false;
5045 // <3> Enable Interrupt
5046 rtl8192_irq_enable(priv);
5047 priv->bdisable_nic = false;
5049 return (init_status == RT_STATUS_SUCCESS);
5052 bool NicIFDisableNIC(struct r8192_priv *priv)
5056 // <1> Disable Interrupt
5058 priv->bdisable_nic = true; //YJ,move,091109
5059 tmp_state = priv->ieee80211->state;
5061 ieee80211_softmac_stop_protocol(priv->ieee80211, false);
5063 priv->ieee80211->state = tmp_state;
5064 rtl8192_cancel_deferred_work(priv);
5065 rtl8192_irq_disable(priv);
5066 // <2> Stop all timer
5068 // <3> Disable Adapter
5069 rtl8192_halt_adapter(priv, false);
5070 // priv->bdisable_nic = true;
5075 module_init(rtl8192_pci_module_init);
5076 module_exit(rtl8192_pci_module_exit);