1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3 * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
4 * Linux device driver for RTL8192U
6 * Based on the r8187 driver, which is:
7 * Copyright 2004-2005 Andrea Merello <andrea.merello@gmail.com>, et al.
10 * Jerry chuang <wlanfae@realtek.com>
13 #ifndef CONFIG_FORCE_HARD_FLOAT
14 double __floatsidf(int i)
19 unsigned int __fixunsdfsi(double d)
24 double __adddf3(double a, double b)
29 double __addsf3(float a, float b)
34 double __subdf3(double a, double b)
39 double __extendsfdf2(float a)
45 #define CONFIG_RTL8192_IO_MAP
47 #include <linux/uaccess.h>
48 #include "r8192U_hw.h"
50 #include "r8190_rtl8256.h" /* RTL8225 Radio frontend */
51 #include "r8180_93cx6.h" /* Card EEPROM */
52 #include "r8192U_wx.h"
53 #include "r819xU_phy.h"
54 #include "r819xU_phyreg.h"
55 #include "r819xU_cmdpkt.h"
56 #include "r8192U_dm.h"
57 #include <linux/usb.h>
58 #include <linux/slab.h>
59 #include <linux/proc_fs.h>
60 #include <linux/seq_file.h>
61 /* FIXME: check if 2.6.7 is ok */
63 #include "ieee80211/dot11d.h"
64 /* set here to open your trace code. */
65 u32 rt_global_debug_component = COMP_DOWN |
67 COMP_ERR; /* always open err flags on */
69 #define TOTAL_CAM_ENTRY 32
70 #define CAM_CONTENT_COUNT 8
72 static const struct usb_device_id rtl8192_usb_id_tbl[] = {
74 {USB_DEVICE(0x0bda, 0x8709)},
76 {USB_DEVICE(0x07aa, 0x0043)},
78 {USB_DEVICE(0x050d, 0x805E)},
80 {USB_DEVICE(0x0df6, 0x0031)},
82 {USB_DEVICE(0x1740, 0x9201)},
84 {USB_DEVICE(0x2001, 0x3301)},
86 {USB_DEVICE(0x5a57, 0x0290)},
88 {USB_DEVICE(0x043e, 0x7a01)},
92 MODULE_LICENSE("GPL");
93 MODULE_VERSION("V 1.1");
94 MODULE_DEVICE_TABLE(usb, rtl8192_usb_id_tbl);
95 MODULE_DESCRIPTION("Linux driver for Realtek RTL8192 USB WiFi cards");
97 static char *ifname = "wlan%d";
98 static int hwwep = 1; /* default use hw. set 0 to use software security */
99 static int channels = 0x3fff;
101 module_param(ifname, charp, 0644);
102 module_param(hwwep, int, 0644);
103 module_param(channels, int, 0644);
105 MODULE_PARM_DESC(ifname, " Net interface name, wlan%d=default");
106 MODULE_PARM_DESC(hwwep, " Try to use hardware security support. ");
107 MODULE_PARM_DESC(channels, " Channel bitmask for specific locales. NYI");
109 static int rtl8192_usb_probe(struct usb_interface *intf,
110 const struct usb_device_id *id);
111 static void rtl8192_usb_disconnect(struct usb_interface *intf);
113 static struct usb_driver rtl8192_usb_driver = {
114 .name = RTL819XU_MODULE_NAME, /* Driver name */
115 .id_table = rtl8192_usb_id_tbl, /* PCI_ID table */
116 .probe = rtl8192_usb_probe, /* probe fn */
117 .disconnect = rtl8192_usb_disconnect, /* remove fn */
118 .suspend = NULL, /* PM suspend fn */
119 .resume = NULL, /* PM resume fn */
122 struct CHANNEL_LIST {
127 static struct CHANNEL_LIST ChannelPlan[] = {
129 {{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},
131 {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 11},
133 {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 36, 40, 44, 48, 52, 56, 60, 64}, 21},
134 /* Spain. Change to ETSI. */
135 {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, 13},
136 /* France. Change to ETSI. */
137 {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, 13},
139 {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 36, 40, 44, 48, 52, 56, 60, 64}, 22},
141 {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 36, 40, 44, 48, 52, 56, 60, 64}, 22},
143 {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, 13},
144 /* For 11a , TELEC */
145 {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 36, 40, 44, 48, 52, 56, 60, 64}, 22},
147 {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 36, 40, 44, 48, 52, 56, 60, 64}, 22},
148 /* For Global Domain. 1-11:active scan, 12-14 passive scan. */
149 {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, 14}
152 static void rtl819x_set_channel_map(u8 channel_plan, struct r8192_priv *priv)
154 int i, max_chan = -1, min_chan = -1;
155 struct ieee80211_device *ieee = priv->ieee80211;
157 switch (channel_plan) {
158 case COUNTRY_CODE_FCC:
159 case COUNTRY_CODE_IC:
160 case COUNTRY_CODE_ETSI:
161 case COUNTRY_CODE_SPAIN:
162 case COUNTRY_CODE_FRANCE:
163 case COUNTRY_CODE_MKK:
164 case COUNTRY_CODE_MKK1:
165 case COUNTRY_CODE_ISRAEL:
166 case COUNTRY_CODE_TELEC:
167 case COUNTRY_CODE_MIC:
168 rtl8192u_dot11d_init(ieee);
169 ieee->bGlobalDomain = false;
170 /* actually 8225 & 8256 rf chips only support B,G,24N mode */
171 if ((priv->rf_chip == RF_8225) || (priv->rf_chip == RF_8256)) {
176 "unknown rf chip, can't set channel map in function:%s()\n",
179 if (ChannelPlan[channel_plan].Len != 0) {
180 /* Clear old channel map */
181 memset(GET_DOT11D_INFO(ieee)->channel_map, 0,
182 sizeof(GET_DOT11D_INFO(ieee)->channel_map));
183 /* Set new channel map */
184 for (i = 0; i < ChannelPlan[channel_plan].Len; i++) {
185 if (ChannelPlan[channel_plan].Channel[i] < min_chan || ChannelPlan[channel_plan].Channel[i] > max_chan)
187 GET_DOT11D_INFO(ieee)->channel_map[ChannelPlan[channel_plan].Channel[i]] = 1;
192 case COUNTRY_CODE_GLOBAL_DOMAIN:
193 /* this flag enabled to follow 11d country IE setting,
194 * otherwise, it shall follow global domain settings.
196 GET_DOT11D_INFO(ieee)->dot11d_enabled = 0;
198 ieee->bGlobalDomain = true;
206 static void CamResetAllEntry(struct net_device *dev)
209 /* In static WEP, OID_ADD_KEY or OID_ADD_WEP are set before STA
210 * associate to AP. However, ResetKey is called on
211 * OID_802_11_INFRASTRUCTURE_MODE and MlmeAssociateRequest. In this
212 * condition, Cam can not be reset because upper layer will not set
213 * this static key again.
215 ulcommand |= BIT(31) | BIT(30);
216 write_nic_dword(dev, RWCAM, ulcommand);
219 int write_nic_byte_E(struct net_device *dev, int indx, u8 data)
222 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
223 struct usb_device *udev = priv->udev;
224 u8 *usbdata = kzalloc(sizeof(data), GFP_KERNEL);
230 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
231 RTL8187_REQ_SET_REGS, RTL8187_REQT_WRITE,
232 indx | 0xfe00, 0, usbdata, 1, HZ / 2);
236 netdev_err(dev, "%s TimeOut! status: %d\n", __func__, status);
242 int read_nic_byte_E(struct net_device *dev, int indx, u8 *data)
245 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
246 struct usb_device *udev = priv->udev;
247 u8 *usbdata = kzalloc(sizeof(u8), GFP_KERNEL);
252 status = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
253 RTL8187_REQ_GET_REGS, RTL8187_REQT_READ,
254 indx | 0xfe00, 0, usbdata, 1, HZ / 2);
259 netdev_err(dev, "%s failure status: %d\n", __func__, status);
266 /* as 92U has extend page from 4 to 16, so modify functions below. */
267 int write_nic_byte(struct net_device *dev, int indx, u8 data)
271 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
272 struct usb_device *udev = priv->udev;
273 u8 *usbdata = kzalloc(sizeof(data), GFP_KERNEL);
279 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
280 RTL8187_REQ_SET_REGS, RTL8187_REQT_WRITE,
281 (indx & 0xff) | 0xff00, (indx >> 8) & 0x0f,
286 netdev_err(dev, "%s TimeOut! status: %d\n", __func__, status);
293 int write_nic_word(struct net_device *dev, int indx, u16 data)
297 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
298 struct usb_device *udev = priv->udev;
299 u16 *usbdata = kzalloc(sizeof(data), GFP_KERNEL);
305 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
306 RTL8187_REQ_SET_REGS, RTL8187_REQT_WRITE,
307 (indx & 0xff) | 0xff00, (indx >> 8) & 0x0f,
312 netdev_err(dev, "%s TimeOut! status: %d\n", __func__, status);
319 int write_nic_dword(struct net_device *dev, int indx, u32 data)
323 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
324 struct usb_device *udev = priv->udev;
325 u32 *usbdata = kzalloc(sizeof(data), GFP_KERNEL);
331 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
332 RTL8187_REQ_SET_REGS, RTL8187_REQT_WRITE,
333 (indx & 0xff) | 0xff00, (indx >> 8) & 0x0f,
338 netdev_err(dev, "%s TimeOut! status: %d\n", __func__, status);
345 int read_nic_byte(struct net_device *dev, int indx, u8 *data)
348 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
349 struct usb_device *udev = priv->udev;
350 u8 *usbdata = kzalloc(sizeof(u8), GFP_KERNEL);
355 status = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
356 RTL8187_REQ_GET_REGS, RTL8187_REQT_READ,
357 (indx & 0xff) | 0xff00, (indx >> 8) & 0x0f,
363 netdev_err(dev, "%s failure status: %d\n", __func__, status);
370 int read_nic_word(struct net_device *dev, int indx, u16 *data)
373 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
374 struct usb_device *udev = priv->udev;
375 u16 *usbdata = kzalloc(sizeof(u16), GFP_KERNEL);
380 status = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
381 RTL8187_REQ_GET_REGS, RTL8187_REQT_READ,
382 (indx & 0xff) | 0xff00, (indx >> 8) & 0x0f,
388 netdev_err(dev, "%s failure status: %d\n", __func__, status);
395 static int read_nic_word_E(struct net_device *dev, int indx, u16 *data)
398 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
399 struct usb_device *udev = priv->udev;
400 u16 *usbdata = kzalloc(sizeof(u16), GFP_KERNEL);
405 status = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
406 RTL8187_REQ_GET_REGS, RTL8187_REQT_READ,
407 indx | 0xfe00, 0, usbdata, 2, HZ / 2);
412 netdev_err(dev, "%s failure status: %d\n", __func__, status);
419 int read_nic_dword(struct net_device *dev, int indx, u32 *data)
423 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
424 struct usb_device *udev = priv->udev;
425 u32 *usbdata = kzalloc(sizeof(u32), GFP_KERNEL);
430 status = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
431 RTL8187_REQ_GET_REGS, RTL8187_REQT_READ,
432 (indx & 0xff) | 0xff00, (indx >> 8) & 0x0f,
438 netdev_err(dev, "%s failure status: %d\n", __func__, status);
445 /* u8 read_phy_cck(struct net_device *dev, u8 adr); */
446 /* u8 read_phy_ofdm(struct net_device *dev, u8 adr); */
447 /* this might still called in what was the PHY rtl8185/rtl8192 common code
448 * plans are to possibility turn it again in one common code...
450 inline void force_pci_posting(struct net_device *dev)
454 static struct net_device_stats *rtl8192_stats(struct net_device *dev);
455 static void rtl8192_restart(struct work_struct *work);
456 static void watch_dog_timer_callback(struct timer_list *t);
458 /****************************************************************************
459 * -----------------------------PROCFS STUFF-------------------------
460 ****************************************************************************/
462 static struct proc_dir_entry *rtl8192_proc;
464 static int __maybe_unused proc_get_stats_ap(struct seq_file *m, void *v)
466 struct net_device *dev = m->private;
467 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
468 struct ieee80211_device *ieee = priv->ieee80211;
469 struct ieee80211_network *target;
471 list_for_each_entry(target, &ieee->network_list, list) {
472 const char *wpa = "non_WPA";
474 if (target->wpa_ie_len > 0 || target->rsn_ie_len > 0)
477 seq_printf(m, "%s %s\n", target->ssid, wpa);
483 static int __maybe_unused proc_get_registers(struct seq_file *m, void *v)
485 struct net_device *dev = m->private;
486 int i, n, max = 0xff;
489 seq_puts(m, "\n####################page 0##################\n ");
491 for (n = 0; n <= max;) {
492 seq_printf(m, "\nD: %2x > ", n);
494 for (i = 0; i < 16 && n <= max; i++, n++) {
495 read_nic_byte(dev, 0x000 | n, &byte_rd);
496 seq_printf(m, "%2x ", byte_rd);
500 seq_puts(m, "\n####################page 1##################\n ");
501 for (n = 0; n <= max;) {
502 seq_printf(m, "\nD: %2x > ", n);
504 for (i = 0; i < 16 && n <= max; i++, n++) {
505 read_nic_byte(dev, 0x100 | n, &byte_rd);
506 seq_printf(m, "%2x ", byte_rd);
510 seq_puts(m, "\n####################page 3##################\n ");
511 for (n = 0; n <= max;) {
512 seq_printf(m, "\nD: %2x > ", n);
514 for (i = 0; i < 16 && n <= max; i++, n++) {
515 read_nic_byte(dev, 0x300 | n, &byte_rd);
516 seq_printf(m, "%2x ", byte_rd);
524 static int __maybe_unused proc_get_stats_tx(struct seq_file *m, void *v)
526 struct net_device *dev = m->private;
527 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
530 "TX VI priority ok int: %lu\n"
531 "TX VI priority error int: %lu\n"
532 "TX VO priority ok int: %lu\n"
533 "TX VO priority error int: %lu\n"
534 "TX BE priority ok int: %lu\n"
535 "TX BE priority error int: %lu\n"
536 "TX BK priority ok int: %lu\n"
537 "TX BK priority error int: %lu\n"
538 "TX MANAGE priority ok int: %lu\n"
539 "TX MANAGE priority error int: %lu\n"
540 "TX BEACON priority ok int: %lu\n"
541 "TX BEACON priority error int: %lu\n"
542 "TX queue resume: %lu\n"
543 "TX queue stopped?: %d\n"
544 "TX fifo overflow: %lu\n"
549 "TX VI dropped: %lu\n"
550 "TX VO dropped: %lu\n"
551 "TX BE dropped: %lu\n"
552 "TX BK dropped: %lu\n"
553 "TX total data packets %lu\n",
554 priv->stats.txviokint,
556 priv->stats.txvookint,
558 priv->stats.txbeokint,
560 priv->stats.txbkokint,
562 priv->stats.txmanageokint,
563 priv->stats.txmanageerr,
564 priv->stats.txbeaconokint,
565 priv->stats.txbeaconerr,
566 priv->stats.txresumed,
567 netif_queue_stopped(dev),
568 priv->stats.txoverflow,
569 atomic_read(&(priv->tx_pending[VI_PRIORITY])),
570 atomic_read(&(priv->tx_pending[VO_PRIORITY])),
571 atomic_read(&(priv->tx_pending[BE_PRIORITY])),
572 atomic_read(&(priv->tx_pending[BK_PRIORITY])),
573 priv->stats.txvidrop,
574 priv->stats.txvodrop,
575 priv->stats.txbedrop,
576 priv->stats.txbkdrop,
577 priv->stats.txdatapkt
583 static int __maybe_unused proc_get_stats_rx(struct seq_file *m, void *v)
585 struct net_device *dev = m->private;
586 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
590 "RX urb status error: %lu\n"
591 "RX invalid urb error: %lu\n",
592 priv->stats.rxoktotal,
593 priv->stats.rxstaterr,
594 priv->stats.rxurberr);
599 static void rtl8192_proc_module_init(void)
601 RT_TRACE(COMP_INIT, "Initializing proc filesystem");
602 rtl8192_proc = proc_mkdir(RTL819XU_MODULE_NAME, init_net.proc_net);
605 static void rtl8192_proc_init_one(struct net_device *dev)
607 struct proc_dir_entry *dir;
612 dir = proc_mkdir_data(dev->name, 0, rtl8192_proc, dev);
616 proc_create_single("stats-rx", S_IFREG | S_IRUGO, dir,
618 proc_create_single("stats-tx", S_IFREG | S_IRUGO, dir,
620 proc_create_single("stats-ap", S_IFREG | S_IRUGO, dir,
622 proc_create_single("registers", S_IFREG | S_IRUGO, dir,
626 static void rtl8192_proc_remove_one(struct net_device *dev)
628 remove_proc_subtree(dev->name, rtl8192_proc);
631 /****************************************************************************
632 * -----------------------------MISC STUFF-------------------------
633 *****************************************************************************/
635 short check_nic_enough_desc(struct net_device *dev, int queue_index)
637 struct r8192_priv *priv = ieee80211_priv(dev);
638 int used = atomic_read(&priv->tx_pending[queue_index]);
640 return (used < MAX_TX_URB);
643 static void tx_timeout(struct net_device *dev)
645 struct r8192_priv *priv = ieee80211_priv(dev);
647 schedule_work(&priv->reset_wq);
650 void rtl8192_update_msr(struct net_device *dev)
652 struct r8192_priv *priv = ieee80211_priv(dev);
655 read_nic_byte(dev, MSR, &msr);
656 msr &= ~MSR_LINK_MASK;
658 /* do not change in link_state != WLAN_LINK_ASSOCIATED.
659 * msr must be updated if the state is ASSOCIATING.
660 * this is intentional and make sense for ad-hoc and
661 * master (see the create BSS/IBSS func)
663 if (priv->ieee80211->state == IEEE80211_LINKED) {
664 if (priv->ieee80211->iw_mode == IW_MODE_INFRA)
665 msr |= (MSR_LINK_MANAGED << MSR_LINK_SHIFT);
666 else if (priv->ieee80211->iw_mode == IW_MODE_ADHOC)
667 msr |= (MSR_LINK_ADHOC << MSR_LINK_SHIFT);
668 else if (priv->ieee80211->iw_mode == IW_MODE_MASTER)
669 msr |= (MSR_LINK_MASTER << MSR_LINK_SHIFT);
672 msr |= (MSR_LINK_NONE << MSR_LINK_SHIFT);
675 write_nic_byte(dev, MSR, msr);
678 void rtl8192_set_chan(struct net_device *dev, short ch)
680 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
682 RT_TRACE(COMP_CH, "=====>%s()====ch:%d\n", __func__, ch);
685 /* this hack should avoid frame TX during channel setting*/
687 /* need to implement rf set channel here */
689 if (priv->rf_set_chan)
690 priv->rf_set_chan(dev, priv->chan);
694 static void rtl8192_rx_isr(struct urb *urb);
696 static u32 get_rxpacket_shiftbytes_819xusb(struct ieee80211_rx_stats *pstats)
698 return (sizeof(struct rx_desc_819x_usb) + pstats->RxDrvInfoSize
699 + pstats->RxBufShift);
702 void rtl8192_rx_enable(struct net_device *dev)
704 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
707 struct rtl8192_rx_info *info;
709 /* nomal packet rx procedure */
710 while (skb_queue_len(&priv->rx_queue) < MAX_RX_URB) {
711 skb = __dev_alloc_skb(RX_URB_SIZE, GFP_KERNEL);
714 entry = usb_alloc_urb(0, GFP_KERNEL);
719 usb_fill_bulk_urb(entry, priv->udev,
720 usb_rcvbulkpipe(priv->udev, 3),
721 skb_tail_pointer(skb),
722 RX_URB_SIZE, rtl8192_rx_isr, skb);
723 info = (struct rtl8192_rx_info *)skb->cb;
726 info->out_pipe = 3; /* denote rx normal packet queue */
727 skb_queue_tail(&priv->rx_queue, skb);
728 usb_submit_urb(entry, GFP_KERNEL);
731 /* command packet rx procedure */
732 while (skb_queue_len(&priv->rx_queue) < MAX_RX_URB + 3) {
733 skb = __dev_alloc_skb(RX_URB_SIZE, GFP_KERNEL);
736 entry = usb_alloc_urb(0, GFP_KERNEL);
741 usb_fill_bulk_urb(entry, priv->udev,
742 usb_rcvbulkpipe(priv->udev, 9),
743 skb_tail_pointer(skb),
744 RX_URB_SIZE, rtl8192_rx_isr, skb);
745 info = (struct rtl8192_rx_info *)skb->cb;
748 info->out_pipe = 9; /* denote rx cmd packet queue */
749 skb_queue_tail(&priv->rx_queue, skb);
750 usb_submit_urb(entry, GFP_KERNEL);
754 void rtl8192_set_rxconf(struct net_device *dev)
756 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
759 read_nic_dword(dev, RCR, &rxconf);
760 rxconf = rxconf & ~MAC_FILTER_MASK;
761 rxconf = rxconf | RCR_AMF;
762 rxconf = rxconf | RCR_ADF;
763 rxconf = rxconf | RCR_AB;
764 rxconf = rxconf | RCR_AM;
766 if (dev->flags & IFF_PROMISC)
767 DMESG("NIC in promisc mode");
769 if (priv->ieee80211->iw_mode == IW_MODE_MONITOR ||
770 dev->flags & IFF_PROMISC) {
771 rxconf = rxconf | RCR_AAP;
773 rxconf = rxconf | RCR_APM;
774 rxconf = rxconf | RCR_CBSSID;
777 if (priv->ieee80211->iw_mode == IW_MODE_MONITOR) {
778 rxconf = rxconf | RCR_AICV;
779 rxconf = rxconf | RCR_APWRMGT;
782 if (priv->crcmon == 1 && priv->ieee80211->iw_mode == IW_MODE_MONITOR)
783 rxconf = rxconf | RCR_ACRC32;
785 rxconf = rxconf & ~RX_FIFO_THRESHOLD_MASK;
786 rxconf = rxconf | (RX_FIFO_THRESHOLD_NONE << RX_FIFO_THRESHOLD_SHIFT);
787 rxconf = rxconf & ~MAX_RX_DMA_MASK;
788 rxconf = rxconf | ((u32)7 << RCR_MXDMA_OFFSET);
790 rxconf = rxconf | RCR_ONLYERLPKT;
792 write_nic_dword(dev, RCR, rxconf);
795 void rtl8192_rtx_disable(struct net_device *dev)
798 struct r8192_priv *priv = ieee80211_priv(dev);
800 struct rtl8192_rx_info *info;
802 read_nic_byte(dev, CMDR, &cmd);
803 write_nic_byte(dev, CMDR, cmd & ~(CR_TE | CR_RE));
804 force_pci_posting(dev);
807 while ((skb = __skb_dequeue(&priv->rx_queue))) {
808 info = (struct rtl8192_rx_info *)skb->cb;
812 usb_kill_urb(info->urb);
816 if (skb_queue_len(&priv->skb_queue))
817 netdev_warn(dev, "skb_queue not empty\n");
819 skb_queue_purge(&priv->skb_queue);
822 /* The prototype of rx_isr has changed since one version of Linux Kernel */
823 static void rtl8192_rx_isr(struct urb *urb)
825 struct sk_buff *skb = (struct sk_buff *)urb->context;
826 struct rtl8192_rx_info *info = (struct rtl8192_rx_info *)skb->cb;
827 struct net_device *dev = info->dev;
828 struct r8192_priv *priv = ieee80211_priv(dev);
829 int out_pipe = info->out_pipe;
835 if (unlikely(urb->status)) {
837 priv->stats.rxstaterr++;
838 priv->ieee80211->stats.rx_errors++;
842 skb_unlink(skb, &priv->rx_queue);
843 skb_put(skb, urb->actual_length);
845 skb_queue_tail(&priv->skb_queue, skb);
846 tasklet_schedule(&priv->irq_rx_tasklet);
848 skb = dev_alloc_skb(RX_URB_SIZE);
849 if (unlikely(!skb)) {
851 netdev_err(dev, "%s(): can't alloc skb\n", __func__);
852 /* TODO check rx queue length and refill *somewhere* */
856 usb_fill_bulk_urb(urb, priv->udev,
857 usb_rcvbulkpipe(priv->udev, out_pipe),
858 skb_tail_pointer(skb),
859 RX_URB_SIZE, rtl8192_rx_isr, skb);
861 info = (struct rtl8192_rx_info *)skb->cb;
864 info->out_pipe = out_pipe;
866 urb->transfer_buffer = skb_tail_pointer(skb);
868 skb_queue_tail(&priv->rx_queue, skb);
869 err = usb_submit_urb(urb, GFP_ATOMIC);
870 if (err && err != EPERM)
872 "can not submit rxurb, err is %x, URB status is %x\n",
876 static u32 rtl819xusb_rx_command_packet(struct net_device *dev,
877 struct ieee80211_rx_stats *pstats)
881 status = cmpk_message_handle_rx(dev, pstats);
883 DMESG("rxcommandpackethandle819xusb: It is a command packet\n");
888 static void rtl8192_data_hard_stop(struct net_device *dev)
893 static void rtl8192_data_hard_resume(struct net_device *dev)
898 /* this function TX data frames when the ieee80211 stack requires this.
899 * It checks also if we need to stop the ieee tx queue, eventually do it
901 static void rtl8192_hard_data_xmit(struct sk_buff *skb, struct net_device *dev,
904 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
907 struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
908 u8 queue_index = tcb_desc->queue_index;
910 /* shall not be referred by command packet */
911 RTL8192U_ASSERT(queue_index != TXCMD_QUEUE);
913 spin_lock_irqsave(&priv->tx_lock, flags);
915 *(struct net_device **)(skb->cb) = dev;
916 tcb_desc->bTxEnableFwCalcDur = 1;
917 skb_push(skb, priv->ieee80211->tx_headroom);
918 ret = rtl8192_tx(dev, skb);
920 spin_unlock_irqrestore(&priv->tx_lock, flags);
923 /* This is a rough attempt to TX a frame
924 * This is called by the ieee 80211 stack to TX management frames.
925 * If the ring is full packet are dropped (for data frame the queue
926 * is stopped before this can happen).
928 static int rtl8192_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
930 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
933 struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
934 u8 queue_index = tcb_desc->queue_index;
936 spin_lock_irqsave(&priv->tx_lock, flags);
938 memcpy((unsigned char *)(skb->cb), &dev, sizeof(dev));
939 if (queue_index == TXCMD_QUEUE) {
940 skb_push(skb, USB_HWDESC_HEADER_LEN);
941 rtl819xU_tx_cmd(dev, skb);
944 skb_push(skb, priv->ieee80211->tx_headroom);
945 ret = rtl8192_tx(dev, skb);
948 spin_unlock_irqrestore(&priv->tx_lock, flags);
953 static void rtl8192_tx_isr(struct urb *tx_urb)
955 struct sk_buff *skb = (struct sk_buff *)tx_urb->context;
956 struct net_device *dev;
957 struct r8192_priv *priv = NULL;
958 struct cb_desc *tcb_desc;
964 dev = *(struct net_device **)(skb->cb);
965 tcb_desc = (struct cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
966 queue_index = tcb_desc->queue_index;
968 priv = ieee80211_priv(dev);
970 if (tcb_desc->queue_index != TXCMD_QUEUE) {
971 if (tx_urb->status == 0) {
972 netif_trans_update(dev);
973 priv->stats.txoktotal++;
974 priv->ieee80211->LinkDetectInfo.NumTxOkInPeriod++;
975 priv->stats.txbytesunicast +=
976 (skb->len - priv->ieee80211->tx_headroom);
978 priv->ieee80211->stats.tx_errors++;
983 /* free skb and tx_urb */
984 dev_kfree_skb_any(skb);
985 usb_free_urb(tx_urb);
986 atomic_dec(&priv->tx_pending[queue_index]);
990 * We had transfer our beacon frame to host controller at this moment.
994 * Handling the wait queue of command packets.
995 * For Tx command packets, we must not do TCB fragment because it is
996 * not handled right now. We must cut the packets to match the size of
997 * TX_CMD_PKT before we send it.
1000 /* Handle MPDU in wait queue. */
1001 if (queue_index != BEACON_QUEUE) {
1002 /* Don't send data frame during scanning.*/
1003 if ((skb_queue_len(&priv->ieee80211->skb_waitQ[queue_index]) != 0) &&
1004 (!(priv->ieee80211->queue_stop))) {
1005 skb = skb_dequeue(&(priv->ieee80211->skb_waitQ[queue_index]));
1007 priv->ieee80211->softmac_hard_start_xmit(skb,
1010 return; /* avoid further processing AMSDU */
1015 static void rtl8192_config_rate(struct net_device *dev, u16 *rate_config)
1017 struct r8192_priv *priv = ieee80211_priv(dev);
1018 struct ieee80211_network *net;
1019 u8 i = 0, basic_rate = 0;
1021 net = &priv->ieee80211->current_network;
1023 for (i = 0; i < net->rates_len; i++) {
1024 basic_rate = net->rates[i] & 0x7f;
1025 switch (basic_rate) {
1027 *rate_config |= RRSR_1M;
1030 *rate_config |= RRSR_2M;
1033 *rate_config |= RRSR_5_5M;
1036 *rate_config |= RRSR_11M;
1039 *rate_config |= RRSR_6M;
1042 *rate_config |= RRSR_9M;
1045 *rate_config |= RRSR_12M;
1048 *rate_config |= RRSR_18M;
1051 *rate_config |= RRSR_24M;
1054 *rate_config |= RRSR_36M;
1057 *rate_config |= RRSR_48M;
1060 *rate_config |= RRSR_54M;
1064 for (i = 0; i < net->rates_ex_len; i++) {
1065 basic_rate = net->rates_ex[i] & 0x7f;
1066 switch (basic_rate) {
1068 *rate_config |= RRSR_1M;
1071 *rate_config |= RRSR_2M;
1074 *rate_config |= RRSR_5_5M;
1077 *rate_config |= RRSR_11M;
1080 *rate_config |= RRSR_6M;
1083 *rate_config |= RRSR_9M;
1086 *rate_config |= RRSR_12M;
1089 *rate_config |= RRSR_18M;
1092 *rate_config |= RRSR_24M;
1095 *rate_config |= RRSR_36M;
1098 *rate_config |= RRSR_48M;
1101 *rate_config |= RRSR_54M;
1107 #define SHORT_SLOT_TIME 9
1108 #define NON_SHORT_SLOT_TIME 20
1110 static void rtl8192_update_cap(struct net_device *dev, u16 cap)
1113 struct r8192_priv *priv = ieee80211_priv(dev);
1114 struct ieee80211_network *net = &priv->ieee80211->current_network;
1116 priv->short_preamble = cap & WLAN_CAPABILITY_SHORT_PREAMBLE;
1117 tmp = priv->basic_rate;
1118 if (priv->short_preamble)
1119 tmp |= BRSR_AckShortPmb;
1120 write_nic_dword(dev, RRSR, tmp);
1122 if (net->mode & (IEEE_G | IEEE_N_24G)) {
1125 if ((cap & WLAN_CAPABILITY_SHORT_SLOT) &&
1126 (!priv->ieee80211->pHTInfo->bCurrentRT2RTLongSlotTime))
1127 /* short slot time */
1128 slot_time = SHORT_SLOT_TIME;
1129 else /* long slot time */
1130 slot_time = NON_SHORT_SLOT_TIME;
1131 priv->slot_time = slot_time;
1132 write_nic_byte(dev, SLOT_TIME, slot_time);
1136 static void rtl8192_net_update(struct net_device *dev)
1138 struct r8192_priv *priv = ieee80211_priv(dev);
1139 struct ieee80211_network *net;
1140 u16 BcnTimeCfg = 0, BcnCW = 6, BcnIFS = 0xf;
1141 u16 rate_config = 0;
1143 net = &priv->ieee80211->current_network;
1145 rtl8192_config_rate(dev, &rate_config);
1146 priv->basic_rate = rate_config & 0x15f;
1148 write_nic_dword(dev, BSSIDR, ((u32 *)net->bssid)[0]);
1149 write_nic_word(dev, BSSIDR + 4, ((u16 *)net->bssid)[2]);
1151 rtl8192_update_msr(dev);
1152 if (priv->ieee80211->iw_mode == IW_MODE_ADHOC) {
1153 write_nic_word(dev, ATIMWND, 2);
1154 write_nic_word(dev, BCN_DMATIME, 1023);
1155 write_nic_word(dev, BCN_INTERVAL, net->beacon_interval);
1156 write_nic_word(dev, BCN_DRV_EARLY_INT, 1);
1157 write_nic_byte(dev, BCN_ERR_THRESH, 100);
1158 BcnTimeCfg |= (BcnCW << BCN_TCFG_CW_SHIFT);
1159 /* TODO: BcnIFS may required to be changed on ASIC */
1160 BcnTimeCfg |= BcnIFS << BCN_TCFG_IFS;
1162 write_nic_word(dev, BCN_TCFG, BcnTimeCfg);
1166 /* temporary hw beacon is not used any more.
1167 * open it when necessary
1169 void rtl819xusb_beacon_tx(struct net_device *dev, u16 tx_rate)
1173 short rtl819xU_tx_cmd(struct net_device *dev, struct sk_buff *skb)
1175 struct r8192_priv *priv = ieee80211_priv(dev);
1178 unsigned int idx_pipe;
1179 struct tx_desc_cmd_819x_usb *pdesc = (struct tx_desc_cmd_819x_usb *)skb->data;
1180 struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1181 u8 queue_index = tcb_desc->queue_index;
1183 atomic_inc(&priv->tx_pending[queue_index]);
1184 tx_urb = usb_alloc_urb(0, GFP_ATOMIC);
1190 memset(pdesc, 0, USB_HWDESC_HEADER_LEN);
1191 /* Tx descriptor ought to be set according to the skb->cb */
1192 pdesc->FirstSeg = 1;
1194 pdesc->CmdInit = tcb_desc->bCmdOrInit;
1195 pdesc->TxBufferSize = tcb_desc->txbuf_size;
1197 pdesc->LINIP = tcb_desc->bLastIniPkt;
1199 /*---------------------------------------------------------------------
1200 * Fill up USB_OUT_CONTEXT.
1201 *---------------------------------------------------------------------
1204 usb_fill_bulk_urb(tx_urb, priv->udev,
1205 usb_sndbulkpipe(priv->udev, idx_pipe),
1206 skb->data, skb->len, rtl8192_tx_isr, skb);
1208 status = usb_submit_urb(tx_urb, GFP_ATOMIC);
1213 DMESGE("Error TX CMD URB, error %d", status);
1215 usb_free_urb(tx_urb);
1220 * Mapping Software/Hardware descriptor queue id to "Queue Select Field"
1221 * in TxFwInfo data structure
1222 * 2006.10.30 by Emily
1224 * \param QUEUEID Software Queue
1226 static u8 MapHwQueueToFirmwareQueue(u8 QueueID)
1228 u8 QueueSelect = 0x0; /* default set to */
1232 QueueSelect = QSLT_BE;
1236 QueueSelect = QSLT_BK;
1240 QueueSelect = QSLT_VO;
1244 QueueSelect = QSLT_VI;
1247 QueueSelect = QSLT_MGNT;
1251 QueueSelect = QSLT_BEACON;
1254 /* TODO: mark other queue selection until we verify it is OK */
1255 /* TODO: Remove Assertions */
1257 QueueSelect = QSLT_CMD;
1260 QueueSelect = QSLT_HIGH;
1265 "TransmitTCB(): Impossible Queue Selection: %d\n",
1272 static u8 MRateToHwRate8190Pci(u8 rate)
1274 u8 ret = DESC90_RATE1M;
1278 ret = DESC90_RATE1M;
1281 ret = DESC90_RATE2M;
1284 ret = DESC90_RATE5_5M;
1287 ret = DESC90_RATE11M;
1290 ret = DESC90_RATE6M;
1293 ret = DESC90_RATE9M;
1296 ret = DESC90_RATE12M;
1299 ret = DESC90_RATE18M;
1302 ret = DESC90_RATE24M;
1305 ret = DESC90_RATE36M;
1308 ret = DESC90_RATE48M;
1311 ret = DESC90_RATE54M;
1314 /* HT rate since here */
1316 ret = DESC90_RATEMCS0;
1319 ret = DESC90_RATEMCS1;
1322 ret = DESC90_RATEMCS2;
1325 ret = DESC90_RATEMCS3;
1328 ret = DESC90_RATEMCS4;
1331 ret = DESC90_RATEMCS5;
1334 ret = DESC90_RATEMCS6;
1337 ret = DESC90_RATEMCS7;
1340 ret = DESC90_RATEMCS8;
1343 ret = DESC90_RATEMCS9;
1346 ret = DESC90_RATEMCS10;
1349 ret = DESC90_RATEMCS11;
1352 ret = DESC90_RATEMCS12;
1355 ret = DESC90_RATEMCS13;
1358 ret = DESC90_RATEMCS14;
1361 ret = DESC90_RATEMCS15;
1364 ret = DESC90_RATEMCS32;
1373 static u8 QueryIsShort(u8 TxHT, u8 TxRate, struct cb_desc *tcb_desc)
1377 tmp_Short = (TxHT == 1) ?
1378 ((tcb_desc->bUseShortGI) ? 1 : 0) :
1379 ((tcb_desc->bUseShortPreamble) ? 1 : 0);
1381 if (TxHT == 1 && TxRate != DESC90_RATEMCS15)
1387 static void tx_zero_isr(struct urb *tx_urb)
1392 * The tx procedure is just as following,
1393 * skb->cb will contain all the following information,
1394 * priority, morefrag, rate, &dev.
1396 short rtl8192_tx(struct net_device *dev, struct sk_buff *skb)
1398 struct r8192_priv *priv = ieee80211_priv(dev);
1399 struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1400 struct tx_desc_819x_usb *tx_desc = (struct tx_desc_819x_usb *)skb->data;
1401 struct tx_fwinfo_819x_usb *tx_fwinfo =
1402 (struct tx_fwinfo_819x_usb *)(skb->data + USB_HWDESC_HEADER_LEN);
1403 struct usb_device *udev = priv->udev;
1405 int status, rt = -1;
1406 struct urb *tx_urb = NULL, *tx_urb_zero = NULL;
1407 unsigned int idx_pipe;
1409 pend = atomic_read(&priv->tx_pending[tcb_desc->queue_index]);
1410 /* we are locked here so the two atomic_read and inc are executed
1411 * without interleaves
1412 * !!! For debug purpose
1414 if (pend > MAX_TX_URB) {
1415 netdev_dbg(dev, "To discard skb packet!\n");
1416 dev_kfree_skb_any(skb);
1420 tx_urb = usb_alloc_urb(0, GFP_ATOMIC);
1422 dev_kfree_skb_any(skb);
1426 /* Fill Tx firmware info */
1427 memset(tx_fwinfo, 0, sizeof(struct tx_fwinfo_819x_usb));
1429 tx_fwinfo->TxHT = (tcb_desc->data_rate & 0x80) ? 1 : 0;
1430 tx_fwinfo->TxRate = MRateToHwRate8190Pci(tcb_desc->data_rate);
1431 tx_fwinfo->EnableCPUDur = tcb_desc->bTxEnableFwCalcDur;
1432 tx_fwinfo->Short = QueryIsShort(tx_fwinfo->TxHT, tx_fwinfo->TxRate,
1434 if (tcb_desc->bAMPDUEnable) { /* AMPDU enabled */
1435 tx_fwinfo->AllowAggregation = 1;
1437 tx_fwinfo->RxMF = tcb_desc->ampdu_factor;
1438 tx_fwinfo->RxAMD = tcb_desc->ampdu_density & 0x07;
1440 tx_fwinfo->AllowAggregation = 0;
1442 tx_fwinfo->RxMF = 0;
1443 tx_fwinfo->RxAMD = 0;
1446 /* Protection mode related */
1447 tx_fwinfo->RtsEnable = (tcb_desc->bRTSEnable) ? 1 : 0;
1448 tx_fwinfo->CtsEnable = (tcb_desc->bCTSEnable) ? 1 : 0;
1449 tx_fwinfo->RtsSTBC = (tcb_desc->bRTSSTBC) ? 1 : 0;
1450 tx_fwinfo->RtsHT = (tcb_desc->rts_rate & 0x80) ? 1 : 0;
1451 tx_fwinfo->RtsRate = MRateToHwRate8190Pci((u8)tcb_desc->rts_rate);
1452 tx_fwinfo->RtsSubcarrier = (tx_fwinfo->RtsHT == 0) ? (tcb_desc->RTSSC) : 0;
1453 tx_fwinfo->RtsBandwidth = (tx_fwinfo->RtsHT == 1) ? ((tcb_desc->bRTSBW) ? 1 : 0) : 0;
1454 tx_fwinfo->RtsShort = (tx_fwinfo->RtsHT == 0) ? (tcb_desc->bRTSUseShortPreamble ? 1 : 0) :
1455 (tcb_desc->bRTSUseShortGI ? 1 : 0);
1457 /* Set Bandwidth and sub-channel settings. */
1458 if (priv->CurrentChannelBW == HT_CHANNEL_WIDTH_20_40) {
1459 if (tcb_desc->bPacketBW) {
1460 tx_fwinfo->TxBandwidth = 1;
1461 /* use duplicated mode */
1462 tx_fwinfo->TxSubCarrier = 0;
1464 tx_fwinfo->TxBandwidth = 0;
1465 tx_fwinfo->TxSubCarrier = priv->nCur40MhzPrimeSC;
1468 tx_fwinfo->TxBandwidth = 0;
1469 tx_fwinfo->TxSubCarrier = 0;
1472 /* Fill Tx descriptor */
1473 memset(tx_desc, 0, sizeof(struct tx_desc_819x_usb));
1476 tx_desc->CmdInit = 1;
1477 tx_desc->Offset = sizeof(struct tx_fwinfo_819x_usb) + 8;
1478 tx_desc->PktSize = (skb->len - TX_PACKET_SHIFT_BYTES) & 0xffff;
1481 tx_desc->SecCAMID = 0;
1482 tx_desc->RATid = tcb_desc->RATRIndex;
1484 tx_desc->SecType = 0x0;
1485 if (tcb_desc->bHwSec) {
1486 switch (priv->ieee80211->pairwise_key_type) {
1487 case KEY_TYPE_WEP40:
1488 case KEY_TYPE_WEP104:
1489 tx_desc->SecType = 0x1;
1493 tx_desc->SecType = 0x2;
1497 tx_desc->SecType = 0x3;
1501 tx_desc->SecType = 0x0;
1507 tx_desc->QueueSelect = MapHwQueueToFirmwareQueue(tcb_desc->queue_index);
1508 tx_desc->TxFWInfoSize = sizeof(struct tx_fwinfo_819x_usb);
1510 tx_desc->DISFB = tcb_desc->bTxDisableRateFallBack;
1511 tx_desc->USERATE = tcb_desc->bTxUseDriverAssingedRate;
1513 /* Fill fields that are required to be initialized in
1514 * all of the descriptors
1517 tx_desc->FirstSeg = 1;
1518 tx_desc->LastSeg = 1;
1522 tx_desc->TxBufferSize = (u32)(skb->len - USB_HWDESC_HEADER_LEN);
1525 /* To submit bulk urb */
1526 usb_fill_bulk_urb(tx_urb, udev,
1527 usb_sndbulkpipe(udev, idx_pipe), skb->data,
1528 skb->len, rtl8192_tx_isr, skb);
1530 status = usb_submit_urb(tx_urb, GFP_ATOMIC);
1532 /* We need to send 0 byte packet whenever
1533 * 512N bytes/64N(HIGN SPEED/NORMAL SPEED) bytes packet has
1534 * been transmitted. Otherwise, it will be halt to wait for
1537 bool bSend0Byte = false;
1540 if (udev->speed == USB_SPEED_HIGH) {
1541 if (skb->len > 0 && skb->len % 512 == 0)
1544 if (skb->len > 0 && skb->len % 64 == 0)
1548 tx_urb_zero = usb_alloc_urb(0, GFP_ATOMIC);
1553 usb_fill_bulk_urb(tx_urb_zero, udev,
1554 usb_sndbulkpipe(udev, idx_pipe),
1555 &zero, 0, tx_zero_isr, dev);
1556 status = usb_submit_urb(tx_urb_zero, GFP_ATOMIC);
1559 "Error TX URB for zero byte %d, error %d",
1560 atomic_read(&priv->tx_pending[tcb_desc->queue_index]),
1565 netif_trans_update(dev);
1566 atomic_inc(&priv->tx_pending[tcb_desc->queue_index]);
1570 RT_TRACE(COMP_ERR, "Error TX URB %d, error %d",
1571 atomic_read(&priv->tx_pending[tcb_desc->queue_index]),
1575 dev_kfree_skb_any(skb);
1576 usb_free_urb(tx_urb);
1577 usb_free_urb(tx_urb_zero);
1581 static short rtl8192_usb_initendpoints(struct net_device *dev)
1583 struct r8192_priv *priv = ieee80211_priv(dev);
1585 priv->rx_urb = kmalloc_array(MAX_RX_URB + 1, sizeof(struct urb *),
1590 #ifndef JACKSON_NEW_RX
1591 for (i = 0; i < (MAX_RX_URB + 1); i++) {
1592 priv->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
1593 if (!priv->rx_urb[i])
1596 priv->rx_urb[i]->transfer_buffer =
1597 kmalloc(RX_URB_SIZE, GFP_KERNEL);
1598 if (!priv->rx_urb[i]->transfer_buffer)
1601 priv->rx_urb[i]->transfer_buffer_length = RX_URB_SIZE;
1605 #ifdef THOMAS_BEACON
1608 void *oldaddr, *newaddr;
1610 priv->rx_urb[16] = usb_alloc_urb(0, GFP_KERNEL);
1611 priv->oldaddr = kmalloc(16, GFP_KERNEL);
1614 oldaddr = priv->oldaddr;
1615 align = ((long)oldaddr) & 3;
1617 newaddr = oldaddr + 4 - align;
1618 priv->rx_urb[16]->transfer_buffer_length = 16 - 4 + align;
1621 priv->rx_urb[16]->transfer_buffer_length = 16;
1623 priv->rx_urb[16]->transfer_buffer = newaddr;
1627 memset(priv->rx_urb, 0, sizeof(struct urb *) * MAX_RX_URB);
1628 priv->pp_rxskb = kcalloc(MAX_RX_URB, sizeof(struct sk_buff *),
1630 if (!priv->pp_rxskb) {
1631 kfree(priv->rx_urb);
1633 priv->pp_rxskb = NULL;
1634 priv->rx_urb = NULL;
1636 DMESGE("Endpoint Alloc Failure");
1640 netdev_dbg(dev, "End of initendpoints\n");
1644 #ifdef THOMAS_BEACON
1645 static void rtl8192_usb_deleteendpoints(struct net_device *dev)
1648 struct r8192_priv *priv = ieee80211_priv(dev);
1651 for (i = 0; i < (MAX_RX_URB + 1); i++) {
1652 usb_kill_urb(priv->rx_urb[i]);
1653 usb_free_urb(priv->rx_urb[i]);
1655 kfree(priv->rx_urb);
1656 priv->rx_urb = NULL;
1658 kfree(priv->oldaddr);
1659 priv->oldaddr = NULL;
1661 kfree(priv->pp_rxskb);
1662 priv->pp_rxskb = NULL;
1665 void rtl8192_usb_deleteendpoints(struct net_device *dev)
1668 struct r8192_priv *priv = ieee80211_priv(dev);
1670 #ifndef JACKSON_NEW_RX
1673 for (i = 0; i < (MAX_RX_URB + 1); i++) {
1674 usb_kill_urb(priv->rx_urb[i]);
1675 kfree(priv->rx_urb[i]->transfer_buffer);
1676 usb_free_urb(priv->rx_urb[i]);
1678 kfree(priv->rx_urb);
1679 priv->rx_urb = NULL;
1682 kfree(priv->rx_urb);
1683 priv->rx_urb = NULL;
1684 kfree(priv->oldaddr);
1685 priv->oldaddr = NULL;
1687 kfree(priv->pp_rxskb);
1694 static void rtl8192_update_ratr_table(struct net_device *dev);
1695 static void rtl8192_link_change(struct net_device *dev)
1697 struct r8192_priv *priv = ieee80211_priv(dev);
1698 struct ieee80211_device *ieee = priv->ieee80211;
1700 if (ieee->state == IEEE80211_LINKED) {
1701 rtl8192_net_update(dev);
1702 rtl8192_update_ratr_table(dev);
1703 /* Add this as in pure N mode, wep encryption will use software
1704 * way, but there is no chance to set this as wep will not set
1705 * group key in wext.
1707 if (ieee->pairwise_key_type == KEY_TYPE_WEP40 ||
1708 ieee->pairwise_key_type == KEY_TYPE_WEP104)
1709 EnableHWSecurityConfig8192(dev);
1711 /*update timing params*/
1712 if (ieee->iw_mode == IW_MODE_INFRA || ieee->iw_mode == IW_MODE_ADHOC) {
1715 read_nic_dword(dev, RCR, ®);
1716 if (priv->ieee80211->state == IEEE80211_LINKED)
1717 priv->ReceiveConfig = reg |= RCR_CBSSID;
1719 priv->ReceiveConfig = reg &= ~RCR_CBSSID;
1720 write_nic_dword(dev, RCR, reg);
1724 static const struct ieee80211_qos_parameters def_qos_parameters = {
1725 {cpu_to_le16(3), cpu_to_le16(3), cpu_to_le16(3), cpu_to_le16(3)},
1726 {cpu_to_le16(7), cpu_to_le16(7), cpu_to_le16(7), cpu_to_le16(7)},
1727 {2, 2, 2, 2},/* aifs */
1728 {0, 0, 0, 0},/* flags */
1729 {0, 0, 0, 0} /* tx_op_limit */
1732 static void rtl8192_update_beacon(struct work_struct *work)
1734 struct r8192_priv *priv = container_of(work, struct r8192_priv,
1735 update_beacon_wq.work);
1736 struct net_device *dev = priv->ieee80211->dev;
1737 struct ieee80211_device *ieee = priv->ieee80211;
1738 struct ieee80211_network *net = &ieee->current_network;
1740 if (ieee->pHTInfo->bCurrentHTSupport)
1741 HTUpdateSelfAndPeerSetting(ieee, net);
1742 ieee->pHTInfo->bCurrentRT2RTLongSlotTime =
1743 net->bssht.bdRT2RTLongSlotTime;
1744 rtl8192_update_cap(dev, net->capability);
1748 * background support to run QoS activate functionality
1750 static int WDCAPARA_ADD[] = {EDCAPARA_BE, EDCAPARA_BK,
1751 EDCAPARA_VI, EDCAPARA_VO};
1752 static void rtl8192_qos_activate(struct work_struct *work)
1754 struct r8192_priv *priv = container_of(work, struct r8192_priv,
1756 struct net_device *dev = priv->ieee80211->dev;
1757 struct ieee80211_qos_parameters *qos_parameters =
1758 &priv->ieee80211->current_network.qos_data.parameters;
1759 u8 mode = priv->ieee80211->current_network.mode;
1767 mutex_lock(&priv->mutex);
1768 if (priv->ieee80211->state != IEEE80211_LINKED)
1771 "qos active process with associate response received\n");
1772 /* It better set slot time at first
1774 * For we just support b/g mode at present, let the slot time at
1777 * update the ac parameter to related registers
1779 for (i = 0; i < QOS_QUEUE_NUM; i++) {
1780 /* Mode G/A: slotTimeTimer = 9; Mode B: 20 */
1781 u1bAIFS = qos_parameters->aifs[i] * ((mode & (IEEE_G | IEEE_N_24G)) ? 9 : 20) + aSifsTime;
1782 u1bAIFS <<= AC_PARAM_AIFS_OFFSET;
1783 op_limit = (u32)le16_to_cpu(qos_parameters->tx_op_limit[i]);
1784 op_limit <<= AC_PARAM_TXOP_LIMIT_OFFSET;
1785 cw_max = (u32)le16_to_cpu(qos_parameters->cw_max[i]);
1786 cw_max <<= AC_PARAM_ECW_MAX_OFFSET;
1787 cw_min = (u32)le16_to_cpu(qos_parameters->cw_min[i]);
1788 cw_min <<= AC_PARAM_ECW_MIN_OFFSET;
1789 u4bAcParam = op_limit | cw_max | cw_min | u1bAIFS;
1790 write_nic_dword(dev, WDCAPARA_ADD[i], u4bAcParam);
1794 mutex_unlock(&priv->mutex);
1797 static int rtl8192_qos_handle_probe_response(struct r8192_priv *priv,
1799 struct ieee80211_network *network)
1802 u32 size = sizeof(struct ieee80211_qos_parameters);
1804 if (priv->ieee80211->state != IEEE80211_LINKED)
1807 if (priv->ieee80211->iw_mode != IW_MODE_INFRA)
1810 if (network->flags & NETWORK_HAS_QOS_MASK) {
1811 if (active_network &&
1812 (network->flags & NETWORK_HAS_QOS_PARAMETERS))
1813 network->qos_data.active = network->qos_data.supported;
1815 if ((network->qos_data.active == 1) && (active_network == 1) &&
1816 (network->flags & NETWORK_HAS_QOS_PARAMETERS) &&
1817 (network->qos_data.old_param_count !=
1818 network->qos_data.param_count)) {
1819 network->qos_data.old_param_count =
1820 network->qos_data.param_count;
1821 schedule_work(&priv->qos_activate);
1823 "QoS parameters change call qos_activate\n");
1826 memcpy(&priv->ieee80211->current_network.qos_data.parameters,
1827 &def_qos_parameters, size);
1829 if ((network->qos_data.active == 1) && (active_network == 1)) {
1830 schedule_work(&priv->qos_activate);
1832 "QoS was disabled call qos_activate\n");
1834 network->qos_data.active = 0;
1835 network->qos_data.supported = 0;
1841 /* handle and manage frame from beacon and probe response */
1842 static int rtl8192_handle_beacon(struct net_device *dev,
1843 struct ieee80211_beacon *beacon,
1844 struct ieee80211_network *network)
1846 struct r8192_priv *priv = ieee80211_priv(dev);
1848 rtl8192_qos_handle_probe_response(priv, 1, network);
1849 schedule_delayed_work(&priv->update_beacon_wq, 0);
1854 * handling the beaconing responses. if we get different QoS setting
1855 * off the network from the associated setting, adjust the QoS
1858 static int rtl8192_qos_association_resp(struct r8192_priv *priv,
1859 struct ieee80211_network *network)
1861 unsigned long flags;
1862 u32 size = sizeof(struct ieee80211_qos_parameters);
1863 int set_qos_param = 0;
1865 if (!priv || !network)
1868 if (priv->ieee80211->state != IEEE80211_LINKED)
1871 if (priv->ieee80211->iw_mode != IW_MODE_INFRA)
1874 spin_lock_irqsave(&priv->ieee80211->lock, flags);
1875 if (network->flags & NETWORK_HAS_QOS_PARAMETERS) {
1876 memcpy(&priv->ieee80211->current_network.qos_data.parameters,
1877 &network->qos_data.parameters,
1878 sizeof(struct ieee80211_qos_parameters));
1879 priv->ieee80211->current_network.qos_data.active = 1;
1881 /* update qos parameter for current network */
1882 priv->ieee80211->current_network.qos_data.old_param_count =
1883 priv->ieee80211->current_network.qos_data.param_count;
1884 priv->ieee80211->current_network.qos_data.param_count =
1885 network->qos_data.param_count;
1887 memcpy(&priv->ieee80211->current_network.qos_data.parameters,
1888 &def_qos_parameters, size);
1889 priv->ieee80211->current_network.qos_data.active = 0;
1890 priv->ieee80211->current_network.qos_data.supported = 0;
1894 spin_unlock_irqrestore(&priv->ieee80211->lock, flags);
1896 RT_TRACE(COMP_QOS, "%s: network->flags = %d,%d\n", __func__,
1898 priv->ieee80211->current_network.qos_data.active);
1899 if (set_qos_param == 1)
1900 schedule_work(&priv->qos_activate);
1905 static int rtl8192_handle_assoc_response(struct net_device *dev,
1906 struct ieee80211_assoc_response_frame *resp,
1907 struct ieee80211_network *network)
1909 struct r8192_priv *priv = ieee80211_priv(dev);
1911 rtl8192_qos_association_resp(priv, network);
1915 static void rtl8192_update_ratr_table(struct net_device *dev)
1917 struct r8192_priv *priv = ieee80211_priv(dev);
1918 struct ieee80211_device *ieee = priv->ieee80211;
1919 u8 *pMcsRate = ieee->dot11HTOperationalRateSet;
1923 rtl8192_config_rate(dev, (u16 *)(&ratr_value));
1924 ratr_value |= (*(u16 *)(pMcsRate)) << 12;
1925 switch (ieee->mode) {
1927 ratr_value &= 0x00000FF0;
1930 ratr_value &= 0x0000000F;
1933 ratr_value &= 0x00000FF7;
1937 if (ieee->pHTInfo->PeerMimoPs == MIMO_PS_STATIC) {
1938 ratr_value &= 0x0007F007;
1940 if (priv->rf_type == RF_1T2R)
1941 ratr_value &= 0x000FF007;
1943 ratr_value &= 0x0F81F007;
1949 ratr_value &= 0x0FFFFFFF;
1950 if (ieee->pHTInfo->bCurTxBW40MHz && ieee->pHTInfo->bCurShortGI40MHz)
1951 ratr_value |= 0x80000000;
1952 else if (!ieee->pHTInfo->bCurTxBW40MHz &&
1953 ieee->pHTInfo->bCurShortGI20MHz)
1954 ratr_value |= 0x80000000;
1955 write_nic_dword(dev, RATR0 + rate_index * 4, ratr_value);
1956 write_nic_byte(dev, UFWP, 1);
1959 static u8 ccmp_ie[4] = {0x00, 0x50, 0xf2, 0x04};
1960 static u8 ccmp_rsn_ie[4] = {0x00, 0x0f, 0xac, 0x04};
1961 static bool GetNmodeSupportBySecCfg8192(struct net_device *dev)
1963 struct r8192_priv *priv = ieee80211_priv(dev);
1964 struct ieee80211_device *ieee = priv->ieee80211;
1965 struct ieee80211_network *network = &ieee->current_network;
1966 int wpa_ie_len = ieee->wpa_ie_len;
1967 struct ieee80211_crypt_data *crypt;
1970 crypt = ieee->crypt[ieee->tx_keyidx];
1971 /* we use connecting AP's capability instead of only security config
1972 * on our driver to distinguish whether it should use N mode or G mode
1974 encrypt = (network->capability & WLAN_CAPABILITY_PRIVACY) ||
1975 (ieee->host_encrypt && crypt && crypt->ops &&
1976 (strcmp(crypt->ops->name, "WEP") == 0));
1979 if (encrypt && (wpa_ie_len == 0)) {
1980 /* wep encryption, no N mode setting */
1982 } else if ((wpa_ie_len != 0)) {
1983 /* parse pairwise key type */
1984 if (((ieee->wpa_ie[0] == 0xdd) && (!memcmp(&(ieee->wpa_ie[14]), ccmp_ie, 4))) || ((ieee->wpa_ie[0] == 0x30) && (!memcmp(&ieee->wpa_ie[10], ccmp_rsn_ie, 4))))
1995 static bool GetHalfNmodeSupportByAPs819xUsb(struct net_device *dev)
1997 struct r8192_priv *priv = ieee80211_priv(dev);
1999 return priv->ieee80211->bHalfWirelessN24GMode;
2002 static void rtl8192_refresh_supportrate(struct r8192_priv *priv)
2004 struct ieee80211_device *ieee = priv->ieee80211;
2005 /* We do not consider set support rate for ABG mode, only
2006 * HT MCS rate is set here.
2008 if (ieee->mode == WIRELESS_MODE_N_24G ||
2009 ieee->mode == WIRELESS_MODE_N_5G)
2010 memcpy(ieee->Regdot11HTOperationalRateSet,
2011 ieee->RegHTSuppRateSet, 16);
2013 memset(ieee->Regdot11HTOperationalRateSet, 0, 16);
2016 static u8 rtl8192_getSupportedWireleeMode(struct net_device *dev)
2018 struct r8192_priv *priv = ieee80211_priv(dev);
2021 switch (priv->rf_chip) {
2025 ret = WIRELESS_MODE_N_24G | WIRELESS_MODE_G | WIRELESS_MODE_B;
2028 ret = WIRELESS_MODE_A | WIRELESS_MODE_N_5G;
2031 ret = WIRELESS_MODE_B;
2037 static void rtl8192_SetWirelessMode(struct net_device *dev, u8 wireless_mode)
2039 struct r8192_priv *priv = ieee80211_priv(dev);
2040 u8 bSupportMode = rtl8192_getSupportedWireleeMode(dev);
2042 if (wireless_mode == WIRELESS_MODE_AUTO ||
2043 (wireless_mode & bSupportMode) == 0) {
2044 if (bSupportMode & WIRELESS_MODE_N_24G) {
2045 wireless_mode = WIRELESS_MODE_N_24G;
2046 } else if (bSupportMode & WIRELESS_MODE_N_5G) {
2047 wireless_mode = WIRELESS_MODE_N_5G;
2048 } else if ((bSupportMode & WIRELESS_MODE_A)) {
2049 wireless_mode = WIRELESS_MODE_A;
2050 } else if ((bSupportMode & WIRELESS_MODE_G)) {
2051 wireless_mode = WIRELESS_MODE_G;
2052 } else if ((bSupportMode & WIRELESS_MODE_B)) {
2053 wireless_mode = WIRELESS_MODE_B;
2056 "%s(), No valid wireless mode supported, SupportedWirelessMode(%x)!!!\n",
2057 __func__, bSupportMode);
2058 wireless_mode = WIRELESS_MODE_B;
2061 priv->ieee80211->mode = wireless_mode;
2063 if (wireless_mode == WIRELESS_MODE_N_24G ||
2064 wireless_mode == WIRELESS_MODE_N_5G)
2065 priv->ieee80211->pHTInfo->bEnableHT = 1;
2067 priv->ieee80211->pHTInfo->bEnableHT = 0;
2068 RT_TRACE(COMP_INIT, "Current Wireless Mode is %x\n", wireless_mode);
2069 rtl8192_refresh_supportrate(priv);
2072 /* init priv variables here. only non_zero value should be initialized here. */
2073 static int rtl8192_init_priv_variable(struct net_device *dev)
2075 struct r8192_priv *priv = ieee80211_priv(dev);
2078 priv->card_8192 = NIC_8192U;
2079 priv->chan = 1; /* set to channel 1 */
2080 priv->ieee80211->mode = WIRELESS_MODE_AUTO; /* SET AUTO */
2081 priv->ieee80211->iw_mode = IW_MODE_INFRA;
2082 priv->ieee80211->ieee_up = 0;
2083 priv->retry_rts = DEFAULT_RETRY_RTS;
2084 priv->retry_data = DEFAULT_RETRY_DATA;
2085 priv->ieee80211->rts = DEFAULT_RTS_THRESHOLD;
2086 priv->ieee80211->rate = 110; /* 11 mbps */
2087 priv->ieee80211->short_slot = 1;
2088 priv->promisc = (dev->flags & IFF_PROMISC) ? 1 : 0;
2090 /* for silent reset */
2091 priv->IrpPendingCount = 1;
2092 priv->ResetProgress = RESET_TYPE_NORESET;
2093 priv->bForcedSilentReset = false;
2094 priv->bDisableNormalResetCheck = false;
2095 priv->force_reset = false;
2097 /* we don't use FW read/write RF until stable firmware is available. */
2098 priv->ieee80211->FwRWRF = 0;
2099 priv->ieee80211->current_network.beacon_interval =
2100 DEFAULT_BEACONINTERVAL;
2101 priv->ieee80211->softmac_features = IEEE_SOFTMAC_SCAN |
2102 IEEE_SOFTMAC_ASSOCIATE | IEEE_SOFTMAC_PROBERQ |
2103 IEEE_SOFTMAC_PROBERS | IEEE_SOFTMAC_TX_QUEUE |
2104 IEEE_SOFTMAC_BEACONS;
2106 priv->ieee80211->active_scan = 1;
2107 priv->ieee80211->modulation =
2108 IEEE80211_CCK_MODULATION | IEEE80211_OFDM_MODULATION;
2109 priv->ieee80211->host_encrypt = 1;
2110 priv->ieee80211->host_decrypt = 1;
2111 priv->ieee80211->start_send_beacons = NULL;
2112 priv->ieee80211->stop_send_beacons = NULL;
2113 priv->ieee80211->softmac_hard_start_xmit = rtl8192_hard_start_xmit;
2114 priv->ieee80211->set_chan = rtl8192_set_chan;
2115 priv->ieee80211->link_change = rtl8192_link_change;
2116 priv->ieee80211->softmac_data_hard_start_xmit = rtl8192_hard_data_xmit;
2117 priv->ieee80211->data_hard_stop = rtl8192_data_hard_stop;
2118 priv->ieee80211->data_hard_resume = rtl8192_data_hard_resume;
2119 priv->ieee80211->init_wmmparam_flag = 0;
2120 priv->ieee80211->fts = DEFAULT_FRAG_THRESHOLD;
2121 priv->ieee80211->check_nic_enough_desc = check_nic_enough_desc;
2122 priv->ieee80211->tx_headroom = TX_PACKET_SHIFT_BYTES;
2123 priv->ieee80211->qos_support = 1;
2125 priv->ieee80211->SetBWModeHandler = rtl8192_SetBWMode;
2126 priv->ieee80211->handle_assoc_response = rtl8192_handle_assoc_response;
2127 priv->ieee80211->handle_beacon = rtl8192_handle_beacon;
2129 priv->ieee80211->GetNmodeSupportBySecCfg = GetNmodeSupportBySecCfg8192;
2130 priv->ieee80211->GetHalfNmodeSupportByAPsHandler =
2131 GetHalfNmodeSupportByAPs819xUsb;
2132 priv->ieee80211->SetWirelessMode = rtl8192_SetWirelessMode;
2134 priv->ieee80211->InitialGainHandler = InitialGain819xUsb;
2135 priv->card_type = USB;
2136 priv->ShortRetryLimit = 0x30;
2137 priv->LongRetryLimit = 0x30;
2138 priv->EarlyRxThreshold = 7;
2139 priv->enable_gpio0 = 0;
2140 priv->TransmitConfig =
2141 /* Max DMA Burst Size per Tx DMA Burst, 7: reserved. */
2142 (TCR_MXDMA_2048 << TCR_MXDMA_OFFSET) |
2143 /* Short retry limit */
2144 (priv->ShortRetryLimit << TCR_SRL_OFFSET) |
2145 /* Long retry limit */
2146 (priv->LongRetryLimit << TCR_LRL_OFFSET) |
2147 /* FALSE: HW provides PLCP length and LENGEXT
2148 * TRUE: SW provides them
2150 (false ? TCR_SAT : 0);
2151 priv->ReceiveConfig =
2152 /* accept management/data */
2154 /* accept control frame for SW AP needs PS-poll */
2156 /* accept BC/MC/UC */
2157 RCR_AB | RCR_AM | RCR_APM |
2158 /* Max DMA Burst Size per Rx DMA Burst, 7: unlimited. */
2159 ((u32)7 << RCR_MXDMA_OFFSET) |
2160 /* Rx FIFO Threshold, 7: No Rx threshold. */
2161 (priv->EarlyRxThreshold << RX_FIFO_THRESHOLD_SHIFT) |
2162 (priv->EarlyRxThreshold == 7 ? RCR_ONLYERLPKT : 0);
2164 priv->AcmControl = 0;
2165 priv->pFirmware = kzalloc(sizeof(rt_firmware), GFP_KERNEL);
2166 if (!priv->pFirmware)
2169 /* rx related queue */
2170 skb_queue_head_init(&priv->rx_queue);
2171 skb_queue_head_init(&priv->skb_queue);
2173 /* Tx related queue */
2174 for (i = 0; i < MAX_QUEUE_SIZE; i++)
2175 skb_queue_head_init(&priv->ieee80211->skb_waitQ[i]);
2176 for (i = 0; i < MAX_QUEUE_SIZE; i++)
2177 skb_queue_head_init(&priv->ieee80211->skb_aggQ[i]);
2178 for (i = 0; i < MAX_QUEUE_SIZE; i++)
2179 skb_queue_head_init(&priv->ieee80211->skb_drv_aggQ[i]);
2180 priv->rf_set_chan = rtl8192_phy_SwChnl;
2185 /* init lock here */
2186 static void rtl8192_init_priv_lock(struct r8192_priv *priv)
2188 spin_lock_init(&priv->tx_lock);
2189 spin_lock_init(&priv->irq_lock);
2190 mutex_init(&priv->wx_mutex);
2191 mutex_init(&priv->mutex);
2194 static void rtl819x_watchdog_wqcallback(struct work_struct *work);
2196 static void rtl8192_irq_rx_tasklet(unsigned long data);
2197 /* init tasklet and wait_queue here. only 2.6 above kernel is considered */
2198 #define DRV_NAME "wlan0"
2199 static void rtl8192_init_priv_task(struct net_device *dev)
2201 struct r8192_priv *priv = ieee80211_priv(dev);
2203 INIT_WORK(&priv->reset_wq, rtl8192_restart);
2205 INIT_DELAYED_WORK(&priv->watch_dog_wq,
2206 rtl819x_watchdog_wqcallback);
2207 INIT_DELAYED_WORK(&priv->txpower_tracking_wq,
2208 dm_txpower_trackingcallback);
2209 INIT_DELAYED_WORK(&priv->rfpath_check_wq,
2210 dm_rf_pathcheck_workitemcallback);
2211 INIT_DELAYED_WORK(&priv->update_beacon_wq,
2212 rtl8192_update_beacon);
2213 INIT_DELAYED_WORK(&priv->initialgain_operate_wq,
2214 InitialGainOperateWorkItemCallBack);
2215 INIT_WORK(&priv->qos_activate, rtl8192_qos_activate);
2217 tasklet_init(&priv->irq_rx_tasklet, rtl8192_irq_rx_tasklet,
2218 (unsigned long)priv);
2221 static void rtl8192_get_eeprom_size(struct net_device *dev)
2224 struct r8192_priv *priv = ieee80211_priv(dev);
2226 RT_TRACE(COMP_EPROM, "===========>%s()\n", __func__);
2227 read_nic_word_E(dev, EPROM_CMD, &curCR);
2228 RT_TRACE(COMP_EPROM,
2229 "read from Reg EPROM_CMD(%x):%x\n", EPROM_CMD, curCR);
2230 /* whether need I consider BIT(5?) */
2232 (curCR & Cmd9346CR_9356SEL) ? EPROM_93c56 : EPROM_93c46;
2233 RT_TRACE(COMP_EPROM,
2234 "<===========%s(), epromtype:%d\n", __func__, priv->epromtype);
2237 /* used to swap endian. as ntohl & htonl are not necessary
2238 * to swap endian, so use this instead.
2240 static inline u16 endian_swap(u16 *data)
2243 *data = (tmp >> 8) | (tmp << 8);
2247 static int rtl8192_read_eeprom_info(struct net_device *dev)
2250 u8 bMac_Tmp_Addr[6] = {0x00, 0xe0, 0x4c, 0x00, 0x00, 0x02};
2251 u8 bLoad_From_EEPOM = false;
2252 struct r8192_priv *priv = ieee80211_priv(dev);
2257 RT_TRACE(COMP_EPROM, "===========>%s()\n", __func__);
2258 ret = eprom_read(dev, 0); /* first read EEPROM ID out; */
2261 wEPROM_ID = (u16)ret;
2262 RT_TRACE(COMP_EPROM, "EEPROM ID is 0x%x\n", wEPROM_ID);
2264 if (wEPROM_ID != RTL8190_EEPROM_ID)
2266 "EEPROM ID is invalid(is 0x%x(should be 0x%x)\n",
2267 wEPROM_ID, RTL8190_EEPROM_ID);
2269 bLoad_From_EEPOM = true;
2271 if (bLoad_From_EEPOM) {
2272 tmpValue = eprom_read(dev, EEPROM_VID >> 1);
2273 ret = eprom_read(dev, EEPROM_VID >> 1);
2276 tmpValue = (u16)ret;
2277 priv->eeprom_vid = endian_swap(&tmpValue);
2278 ret = eprom_read(dev, EEPROM_PID >> 1);
2281 priv->eeprom_pid = (u16)ret;
2282 ret = eprom_read(dev, EEPROM_CHANNEL_PLAN >> 1);
2285 tmpValue = (u16)ret;
2286 priv->eeprom_ChannelPlan = (tmpValue & 0xff00) >> 8;
2287 priv->btxpowerdata_readfromEEPORM = true;
2288 ret = eprom_read(dev, (EEPROM_CUSTOMER_ID >> 1)) >> 8;
2291 priv->eeprom_CustomerID = (u16)ret;
2293 priv->eeprom_vid = 0;
2294 priv->eeprom_pid = 0;
2295 priv->card_8192_version = VERSION_819XU_B;
2296 priv->eeprom_ChannelPlan = 0;
2297 priv->eeprom_CustomerID = 0;
2299 RT_TRACE(COMP_EPROM,
2300 "vid:0x%4x, pid:0x%4x, CustomID:0x%2x, ChanPlan:0x%x\n",
2301 priv->eeprom_vid, priv->eeprom_pid, priv->eeprom_CustomerID,
2302 priv->eeprom_ChannelPlan);
2303 /* set channelplan from eeprom */
2304 priv->ChannelPlan = priv->eeprom_ChannelPlan;
2305 if (bLoad_From_EEPOM) {
2308 for (i = 0; i < 6; i += 2) {
2309 ret = eprom_read(dev, (u16)((EEPROM_NODE_ADDRESS_BYTE_0 + i) >> 1));
2312 *(u16 *)(&dev->dev_addr[i]) = (u16)ret;
2315 memcpy(dev->dev_addr, bMac_Tmp_Addr, 6);
2316 /* should I set IDR0 here? */
2318 RT_TRACE(COMP_EPROM, "MAC addr:%pM\n", dev->dev_addr);
2319 priv->rf_type = RTL819X_DEFAULT_RF_TYPE; /* default 1T2R */
2320 priv->rf_chip = RF_8256;
2322 if (priv->card_8192_version == VERSION_819XU_A) {
2323 /* read Tx power gain offset of legacy OFDM to HT rate */
2324 if (bLoad_From_EEPOM) {
2325 ret = eprom_read(dev, (EEPROM_TX_POWER_DIFF >> 1));
2328 priv->EEPROMTxPowerDiff = ((u16)ret & 0xff00) >> 8;
2330 priv->EEPROMTxPowerDiff = EEPROM_DEFAULT_TX_POWER;
2331 RT_TRACE(COMP_EPROM, "TxPowerDiff:%d\n", priv->EEPROMTxPowerDiff);
2332 /* read ThermalMeter from EEPROM */
2333 if (bLoad_From_EEPOM) {
2334 ret = eprom_read(dev, (EEPROM_THERMAL_METER >> 1));
2337 priv->EEPROMThermalMeter = (u8)((u16)ret & 0x00ff);
2339 priv->EEPROMThermalMeter = EEPROM_DEFAULT_THERNAL_METER;
2340 RT_TRACE(COMP_EPROM, "ThermalMeter:%d\n", priv->EEPROMThermalMeter);
2341 /* for tx power track */
2342 priv->TSSI_13dBm = priv->EEPROMThermalMeter * 100;
2343 /* read antenna tx power offset of B/C/D to A from EEPROM */
2344 if (bLoad_From_EEPOM) {
2345 ret = eprom_read(dev, (EEPROM_PW_DIFF >> 1));
2348 priv->EEPROMPwDiff = ((u16)ret & 0x0f00) >> 8;
2350 priv->EEPROMPwDiff = EEPROM_DEFAULT_PW_DIFF;
2351 RT_TRACE(COMP_EPROM, "TxPwDiff:%d\n", priv->EEPROMPwDiff);
2352 /* Read CrystalCap from EEPROM */
2353 if (bLoad_From_EEPOM) {
2354 ret = eprom_read(dev, (EEPROM_CRYSTAL_CAP >> 1));
2357 priv->EEPROMCrystalCap = (u16)ret & 0x0f;
2359 priv->EEPROMCrystalCap = EEPROM_DEFAULT_CRYSTAL_CAP;
2360 RT_TRACE(COMP_EPROM, "CrystalCap = %d\n", priv->EEPROMCrystalCap);
2361 /* get per-channel Tx power level */
2362 if (bLoad_From_EEPOM) {
2363 ret = eprom_read(dev, (EEPROM_TX_PW_INDEX_VER >> 1));
2366 priv->EEPROM_Def_Ver = ((u16)ret & 0xff00) >> 8;
2368 priv->EEPROM_Def_Ver = 1;
2369 RT_TRACE(COMP_EPROM, "EEPROM_DEF_VER:%d\n", priv->EEPROM_Def_Ver);
2370 if (priv->EEPROM_Def_Ver == 0) { /* old eeprom definition */
2373 if (bLoad_From_EEPOM) {
2374 ret = eprom_read(dev, (EEPROM_TX_PW_INDEX_CCK >> 1));
2377 priv->EEPROMTxPowerLevelCCK = ((u16)ret & 0xff) >> 8;
2379 priv->EEPROMTxPowerLevelCCK = 0x10;
2380 RT_TRACE(COMP_EPROM, "CCK Tx Power Levl: 0x%02x\n", priv->EEPROMTxPowerLevelCCK);
2381 for (i = 0; i < 3; i++) {
2382 if (bLoad_From_EEPOM) {
2383 ret = eprom_read(dev, (EEPROM_TX_PW_INDEX_OFDM_24G + i) >> 1);
2386 if (((EEPROM_TX_PW_INDEX_OFDM_24G + i) % 2) == 0)
2387 tmpValue = (u16)ret & 0x00ff;
2389 tmpValue = ((u16)ret & 0xff00) >> 8;
2393 priv->EEPROMTxPowerLevelOFDM24G[i] = (u8)tmpValue;
2394 RT_TRACE(COMP_EPROM, "OFDM 2.4G Tx Power Level, Index %d = 0x%02x\n", i, priv->EEPROMTxPowerLevelCCK);
2396 } else if (priv->EEPROM_Def_Ver == 1) {
2397 if (bLoad_From_EEPOM) {
2398 ret = eprom_read(dev, EEPROM_TX_PW_INDEX_CCK_V1 >> 1);
2401 tmpValue = ((u16)ret & 0xff00) >> 8;
2405 priv->EEPROMTxPowerLevelCCK_V1[0] = (u8)tmpValue;
2407 if (bLoad_From_EEPOM) {
2408 ret = eprom_read(dev, (EEPROM_TX_PW_INDEX_CCK_V1 + 2) >> 1);
2411 tmpValue = (u16)ret;
2414 *((u16 *)(&priv->EEPROMTxPowerLevelCCK_V1[1])) = tmpValue;
2415 if (bLoad_From_EEPOM)
2416 tmpValue = eprom_read(dev,
2417 EEPROM_TX_PW_INDEX_OFDM_24G_V1 >> 1);
2420 *((u16 *)(&priv->EEPROMTxPowerLevelOFDM24G[0])) = tmpValue;
2421 if (bLoad_From_EEPOM)
2422 tmpValue = eprom_read(dev, (EEPROM_TX_PW_INDEX_OFDM_24G_V1 + 2) >> 1);
2425 priv->EEPROMTxPowerLevelOFDM24G[2] = (u8)tmpValue;
2426 } /* endif EEPROM_Def_Ver == 1 */
2428 /* update HAL variables */
2429 for (i = 0; i < 14; i++) {
2431 priv->TxPowerLevelOFDM24G[i] = priv->EEPROMTxPowerLevelOFDM24G[0];
2432 else if (i >= 4 && i <= 9)
2433 priv->TxPowerLevelOFDM24G[i] = priv->EEPROMTxPowerLevelOFDM24G[1];
2435 priv->TxPowerLevelOFDM24G[i] = priv->EEPROMTxPowerLevelOFDM24G[2];
2438 for (i = 0; i < 14; i++) {
2439 if (priv->EEPROM_Def_Ver == 0) {
2441 priv->TxPowerLevelCCK[i] = priv->EEPROMTxPowerLevelOFDM24G[0] + (priv->EEPROMTxPowerLevelCCK - priv->EEPROMTxPowerLevelOFDM24G[1]);
2442 else if (i >= 4 && i <= 9)
2443 priv->TxPowerLevelCCK[i] = priv->EEPROMTxPowerLevelCCK;
2445 priv->TxPowerLevelCCK[i] = priv->EEPROMTxPowerLevelOFDM24G[2] + (priv->EEPROMTxPowerLevelCCK - priv->EEPROMTxPowerLevelOFDM24G[1]);
2446 } else if (priv->EEPROM_Def_Ver == 1) {
2448 priv->TxPowerLevelCCK[i] = priv->EEPROMTxPowerLevelCCK_V1[0];
2449 else if (i >= 4 && i <= 9)
2450 priv->TxPowerLevelCCK[i] = priv->EEPROMTxPowerLevelCCK_V1[1];
2452 priv->TxPowerLevelCCK[i] = priv->EEPROMTxPowerLevelCCK_V1[2];
2455 priv->TxPowerDiff = priv->EEPROMPwDiff;
2456 /* Antenna B gain offset to antenna A, bit0~3 */
2457 priv->AntennaTxPwDiff[0] = (priv->EEPROMTxPowerDiff & 0xf);
2458 /* Antenna C gain offset to antenna A, bit4~7 */
2459 priv->AntennaTxPwDiff[1] =
2460 (priv->EEPROMTxPowerDiff & 0xf0) >> 4;
2461 /* CrystalCap, bit12~15 */
2462 priv->CrystalCap = priv->EEPROMCrystalCap;
2463 /* ThermalMeter, bit0~3 for RFIC1, bit4~7 for RFIC2
2464 * 92U does not enable TX power tracking.
2466 priv->ThermalMeter[0] = priv->EEPROMThermalMeter;
2467 } /* end if VersionID == VERSION_819XU_A */
2470 switch (priv->eeprom_CustomerID) {
2471 case EEPROM_CID_RUNTOP:
2472 priv->CustomerID = RT_CID_819x_RUNTOP;
2475 case EEPROM_CID_DLINK:
2476 priv->CustomerID = RT_CID_DLINK;
2480 priv->CustomerID = RT_CID_DEFAULT;
2484 switch (priv->CustomerID) {
2485 case RT_CID_819x_RUNTOP:
2486 priv->LedStrategy = SW_LED_MODE2;
2490 priv->LedStrategy = SW_LED_MODE4;
2494 priv->LedStrategy = SW_LED_MODE0;
2498 if (priv->rf_type == RF_1T2R)
2499 RT_TRACE(COMP_EPROM, "\n1T2R config\n");
2501 RT_TRACE(COMP_EPROM, "\n2T4R config\n");
2503 /* We can only know RF type in the function. So we have to init
2504 * DIG RATR table again.
2506 init_rate_adaptive(dev);
2508 RT_TRACE(COMP_EPROM, "<===========%s()\n", __func__);
2513 static short rtl8192_get_channel_map(struct net_device *dev)
2515 struct r8192_priv *priv = ieee80211_priv(dev);
2517 if (priv->ChannelPlan > COUNTRY_CODE_GLOBAL_DOMAIN) {
2519 "rtl8180_init: Error channel plan! Set to default.\n");
2520 priv->ChannelPlan = 0;
2522 RT_TRACE(COMP_INIT, "Channel plan is %d\n", priv->ChannelPlan);
2524 rtl819x_set_channel_map(priv->ChannelPlan, priv);
2528 static short rtl8192_init(struct net_device *dev)
2530 struct r8192_priv *priv = ieee80211_priv(dev);
2533 memset(&(priv->stats), 0, sizeof(struct Stats));
2534 memset(priv->txqueue_to_outpipemap, 0, 9);
2538 u8 queuetopipe[] = {3, 2, 1, 0, 4, 8, 7, 6, 5};
2540 memcpy(priv->txqueue_to_outpipemap, queuetopipe, 9);
2544 u8 queuetopipe[] = {3, 2, 1, 0, 4, 4, 0, 4, 4};
2546 memcpy(priv->txqueue_to_outpipemap, queuetopipe, 9);
2549 err = rtl8192_init_priv_variable(dev);
2553 rtl8192_init_priv_lock(priv);
2554 rtl8192_init_priv_task(dev);
2555 rtl8192_get_eeprom_size(dev);
2556 err = rtl8192_read_eeprom_info(dev);
2558 DMESG("Reading EEPROM info failed");
2561 rtl8192_get_channel_map(dev);
2563 timer_setup(&priv->watch_dog_timer, watch_dog_timer_callback, 0);
2564 if (rtl8192_usb_initendpoints(dev) != 0) {
2565 DMESG("Endopoints initialization failed");
2572 /******************************************************************************
2573 *function: This function actually only set RRSR, RATR and BW_OPMODE registers
2574 * not to do all the hw config as its name says
2575 * input: net_device dev
2578 * notice: This part need to modified according to the rate set we filtered
2579 * ****************************************************************************/
2580 static void rtl8192_hwconfig(struct net_device *dev)
2582 u32 regRATR = 0, regRRSR = 0;
2583 u8 regBwOpMode = 0, regTmp = 0;
2584 struct r8192_priv *priv = ieee80211_priv(dev);
2587 /* Set RRSR, RATR, and BW_OPMODE registers */
2588 switch (priv->ieee80211->mode) {
2589 case WIRELESS_MODE_B:
2590 regBwOpMode = BW_OPMODE_20MHZ;
2591 regRATR = RATE_ALL_CCK;
2592 regRRSR = RATE_ALL_CCK;
2594 case WIRELESS_MODE_A:
2595 regBwOpMode = BW_OPMODE_5G | BW_OPMODE_20MHZ;
2596 regRATR = RATE_ALL_OFDM_AG;
2597 regRRSR = RATE_ALL_OFDM_AG;
2599 case WIRELESS_MODE_G:
2600 regBwOpMode = BW_OPMODE_20MHZ;
2601 regRATR = RATE_ALL_CCK | RATE_ALL_OFDM_AG;
2602 regRRSR = RATE_ALL_CCK | RATE_ALL_OFDM_AG;
2604 case WIRELESS_MODE_AUTO:
2605 regBwOpMode = BW_OPMODE_20MHZ;
2606 regRATR = RATE_ALL_CCK | RATE_ALL_OFDM_AG |
2607 RATE_ALL_OFDM_1SS | RATE_ALL_OFDM_2SS;
2608 regRRSR = RATE_ALL_CCK | RATE_ALL_OFDM_AG;
2610 case WIRELESS_MODE_N_24G:
2611 /* It support CCK rate by default. CCK rate will be filtered
2612 * out only when associated AP does not support it.
2614 regBwOpMode = BW_OPMODE_20MHZ;
2615 regRATR = RATE_ALL_CCK | RATE_ALL_OFDM_AG |
2616 RATE_ALL_OFDM_1SS | RATE_ALL_OFDM_2SS;
2617 regRRSR = RATE_ALL_CCK | RATE_ALL_OFDM_AG;
2619 case WIRELESS_MODE_N_5G:
2620 regBwOpMode = BW_OPMODE_5G;
2621 regRATR = RATE_ALL_OFDM_AG | RATE_ALL_OFDM_1SS |
2623 regRRSR = RATE_ALL_OFDM_AG;
2627 write_nic_byte(dev, BW_OPMODE, regBwOpMode);
2628 ratr_value = regRATR;
2629 if (priv->rf_type == RF_1T2R)
2630 ratr_value &= ~(RATE_ALL_OFDM_2SS);
2631 write_nic_dword(dev, RATR0, ratr_value);
2632 write_nic_byte(dev, UFWP, 1);
2633 read_nic_byte(dev, 0x313, ®Tmp);
2634 regRRSR = ((regTmp) << 24) | (regRRSR & 0x00ffffff);
2635 write_nic_dword(dev, RRSR, regRRSR);
2637 /* Set Retry Limit here */
2638 write_nic_word(dev, RETRY_LIMIT,
2639 priv->ShortRetryLimit << RETRY_LIMIT_SHORT_SHIFT |
2640 priv->LongRetryLimit << RETRY_LIMIT_LONG_SHIFT);
2641 /* Set Contention Window here */
2645 /* Set Tx Antenna including Feedback control */
2647 /* Set Auto Rate fallback control */
2650 /* InitializeAdapter and PhyCfg */
2651 static bool rtl8192_adapter_start(struct net_device *dev)
2653 struct r8192_priv *priv = ieee80211_priv(dev);
2655 bool init_status = true;
2656 u8 SECR_value = 0x0;
2659 RT_TRACE(COMP_INIT, "====>%s()\n", __func__);
2660 priv->Rf_Mode = RF_OP_By_SW_3wire;
2661 /* for ASIC power on sequence */
2662 write_nic_byte_E(dev, 0x5f, 0x80);
2664 write_nic_byte_E(dev, 0x5f, 0xf0);
2665 write_nic_byte_E(dev, 0x5d, 0x00);
2666 write_nic_byte_E(dev, 0x5e, 0x80);
2667 write_nic_byte(dev, 0x17, 0x37);
2669 priv->pFirmware->firmware_status = FW_STATUS_0_INIT;
2670 /* config CPUReset Register */
2671 /* Firmware Reset or not? */
2672 read_nic_dword(dev, CPU_GEN, &dwRegRead);
2673 if (priv->pFirmware->firmware_status == FW_STATUS_0_INIT)
2674 dwRegRead |= CPU_GEN_SYSTEM_RESET; /* do nothing here? */
2675 else if (priv->pFirmware->firmware_status == FW_STATUS_5_READY)
2676 dwRegRead |= CPU_GEN_FIRMWARE_RESET;
2679 "ERROR in %s(): undefined firmware state(%d)\n",
2680 __func__, priv->pFirmware->firmware_status);
2682 write_nic_dword(dev, CPU_GEN, dwRegRead);
2684 rtl8192_BBConfig(dev);
2686 /* Loopback mode or not */
2687 priv->LoopbackMode = RTL819xU_NO_LOOPBACK;
2689 read_nic_dword(dev, CPU_GEN, &dwRegRead);
2690 if (priv->LoopbackMode == RTL819xU_NO_LOOPBACK)
2691 dwRegRead = (dwRegRead & CPU_GEN_NO_LOOPBACK_MSK) |
2692 CPU_GEN_NO_LOOPBACK_SET;
2693 else if (priv->LoopbackMode == RTL819xU_MAC_LOOPBACK)
2694 dwRegRead |= CPU_CCK_LOOPBACK;
2697 "Serious error in %s(): wrong loopback mode setting(%d)\n",
2698 __func__, priv->LoopbackMode);
2700 write_nic_dword(dev, CPU_GEN, dwRegRead);
2702 /* after reset cpu, we need wait for a seconds to write in register. */
2705 /* add for new bitfile:usb suspend reset pin set to 1. Do we need? */
2706 read_nic_byte_E(dev, 0x5f, &tmp);
2707 write_nic_byte_E(dev, 0x5f, tmp | 0x20);
2710 rtl8192_hwconfig(dev);
2713 write_nic_byte(dev, CMDR, CR_RE | CR_TE);
2716 write_nic_dword(dev, MAC0, ((u32 *)dev->dev_addr)[0]);
2717 write_nic_word(dev, MAC4, ((u16 *)(dev->dev_addr + 4))[0]);
2720 write_nic_dword(dev, RCR, priv->ReceiveConfig);
2722 /* Initialize Number of Reserved Pages in Firmware Queue */
2723 write_nic_dword(dev, RQPN1,
2724 NUM_OF_PAGE_IN_FW_QUEUE_BK << RSVD_FW_QUEUE_PAGE_BK_SHIFT |
2725 NUM_OF_PAGE_IN_FW_QUEUE_BE << RSVD_FW_QUEUE_PAGE_BE_SHIFT |
2726 NUM_OF_PAGE_IN_FW_QUEUE_VI << RSVD_FW_QUEUE_PAGE_VI_SHIFT |
2727 NUM_OF_PAGE_IN_FW_QUEUE_VO << RSVD_FW_QUEUE_PAGE_VO_SHIFT);
2728 write_nic_dword(dev, RQPN2,
2729 NUM_OF_PAGE_IN_FW_QUEUE_MGNT << RSVD_FW_QUEUE_PAGE_MGNT_SHIFT |
2730 NUM_OF_PAGE_IN_FW_QUEUE_CMD << RSVD_FW_QUEUE_PAGE_CMD_SHIFT);
2731 write_nic_dword(dev, RQPN3,
2732 APPLIED_RESERVED_QUEUE_IN_FW |
2733 NUM_OF_PAGE_IN_FW_QUEUE_BCN << RSVD_FW_QUEUE_PAGE_BCN_SHIFT);
2734 write_nic_dword(dev, RATR0 + 4 * 7, (RATE_ALL_OFDM_AG | RATE_ALL_CCK));
2736 /* Set AckTimeout */
2737 /* TODO: (it value is only for FPGA version). need to be changed!! */
2738 write_nic_byte(dev, ACK_TIMEOUT, 0x30);
2740 if (priv->ResetProgress == RESET_TYPE_NORESET)
2741 rtl8192_SetWirelessMode(dev, priv->ieee80211->mode);
2742 if (priv->ResetProgress == RESET_TYPE_NORESET) {
2743 CamResetAllEntry(dev);
2744 SECR_value |= SCR_TxEncEnable;
2745 SECR_value |= SCR_RxDecEnable;
2746 SECR_value |= SCR_NoSKMC;
2747 write_nic_byte(dev, SECR, SECR_value);
2750 /* Beacon related */
2751 write_nic_word(dev, ATIMWND, 2);
2752 write_nic_word(dev, BCN_INTERVAL, 100);
2754 #define DEFAULT_EDCA 0x005e4332
2758 for (i = 0; i < QOS_QUEUE_NUM; i++)
2759 write_nic_dword(dev, WDCAPARA_ADD[i], DEFAULT_EDCA);
2762 rtl8192_phy_configmac(dev);
2764 if (priv->card_8192_version == VERSION_819XU_A) {
2765 rtl8192_phy_getTxPower(dev);
2766 rtl8192_phy_setTxPower(dev, priv->chan);
2769 /* Firmware download */
2770 init_status = init_firmware(dev);
2772 RT_TRACE(COMP_ERR, "ERR!!! %s(): Firmware download is failed\n",
2776 RT_TRACE(COMP_INIT, "%s():after firmware download\n", __func__);
2779 if (priv->ResetProgress == RESET_TYPE_NORESET) {
2780 rtl8192_phy_RFConfig(dev);
2781 RT_TRACE(COMP_INIT, "%s():after phy RF config\n", __func__);
2784 if (priv->ieee80211->FwRWRF)
2785 /* We can force firmware to do RF-R/W */
2786 priv->Rf_Mode = RF_OP_By_FW;
2788 priv->Rf_Mode = RF_OP_By_SW_3wire;
2790 rtl8192_phy_updateInitGain(dev);
2791 /*--set CCK and OFDM Block "ON"--*/
2792 rtl8192_setBBreg(dev, rFPGA0_RFMOD, bCCKEn, 0x1);
2793 rtl8192_setBBreg(dev, rFPGA0_RFMOD, bOFDMEn, 0x1);
2795 if (priv->ResetProgress == RESET_TYPE_NORESET) {
2799 read_nic_byte(dev, 0x301, &tmpvalue);
2800 if (tmpvalue == 0x03) {
2802 RT_TRACE(COMP_POWER_TRACKING, "D-cut\n");
2804 priv->bDcut = false;
2805 RT_TRACE(COMP_POWER_TRACKING, "C-cut\n");
2807 dm_initialize_txpower_tracking(dev);
2811 u32 tmpRegA = rtl8192_QueryBBReg(dev,
2812 rOFDM0_XATxIQImbalance,
2815 for (i = 0; i < TxBBGainTableLength; i++) {
2816 if (tmpRegA == priv->txbbgain_table[i].txbbgain_value) {
2817 priv->rfa_txpowertrackingindex = (u8)i;
2818 priv->rfa_txpowertrackingindex_real =
2820 priv->rfa_txpowertracking_default =
2821 priv->rfa_txpowertrackingindex;
2826 TempCCk = rtl8192_QueryBBReg(dev,
2830 for (i = 0; i < CCKTxBBGainTableLength; i++) {
2831 if (TempCCk == priv->cck_txbbgain_table[i].ccktxbb_valuearray[0]) {
2832 priv->cck_present_attenuation_20Mdefault = (u8)i;
2836 priv->cck_present_attenuation_40Mdefault = 0;
2837 priv->cck_present_attenuation_difference = 0;
2838 priv->cck_present_attenuation =
2839 priv->cck_present_attenuation_20Mdefault;
2842 write_nic_byte(dev, 0x87, 0x0);
2847 /* this configures registers for beacon tx and enables it via
2848 * rtl8192_beacon_tx_enable(). rtl8192_beacon_tx_disable() might
2849 * be used to stop beacon transmission
2851 /***************************************************************************
2852 * -------------------------------NET STUFF---------------------------
2853 ***************************************************************************/
2855 static struct net_device_stats *rtl8192_stats(struct net_device *dev)
2857 struct r8192_priv *priv = ieee80211_priv(dev);
2859 return &priv->ieee80211->stats;
2862 static bool HalTxCheckStuck819xUsb(struct net_device *dev)
2864 struct r8192_priv *priv = ieee80211_priv(dev);
2866 bool bStuck = false;
2868 read_nic_word(dev, 0x128, &RegTxCounter);
2869 RT_TRACE(COMP_RESET,
2870 "%s():RegTxCounter is %d,TxCounter is %d\n", __func__,
2871 RegTxCounter, priv->TxCounter);
2872 if (priv->TxCounter == RegTxCounter)
2875 priv->TxCounter = RegTxCounter;
2881 * <Assumption: RT_TX_SPINLOCK is acquired.>
2882 * First added: 2006.11.19 by emily
2884 static RESET_TYPE TxCheckStuck(struct net_device *dev)
2886 struct r8192_priv *priv = ieee80211_priv(dev);
2888 bool bCheckFwTxCnt = false;
2890 /* Decide such threshold according to current power save mode */
2892 for (QueueID = 0; QueueID <= BEACON_QUEUE; QueueID++) {
2893 if (QueueID == TXCMD_QUEUE)
2895 if ((skb_queue_len(&priv->ieee80211->skb_waitQ[QueueID]) == 0) && (skb_queue_len(&priv->ieee80211->skb_aggQ[QueueID]) == 0))
2898 bCheckFwTxCnt = true;
2900 if (bCheckFwTxCnt) {
2901 if (HalTxCheckStuck819xUsb(dev)) {
2902 RT_TRACE(COMP_RESET,
2903 "%s: Fw indicates no Tx condition!\n",
2905 return RESET_TYPE_SILENT;
2908 return RESET_TYPE_NORESET;
2911 static bool HalRxCheckStuck819xUsb(struct net_device *dev)
2914 struct r8192_priv *priv = ieee80211_priv(dev);
2915 bool bStuck = false;
2916 static u8 rx_chk_cnt;
2918 read_nic_word(dev, 0x130, &RegRxCounter);
2919 RT_TRACE(COMP_RESET,
2920 "%s(): RegRxCounter is %d,RxCounter is %d\n", __func__,
2921 RegRxCounter, priv->RxCounter);
2922 /* If rssi is small, we should check rx for long time because of bad rx.
2923 * or maybe it will continuous silent reset every 2 seconds.
2926 if (priv->undecorated_smoothed_pwdb >= (RATE_ADAPTIVE_TH_HIGH + 5)) {
2927 rx_chk_cnt = 0; /* high rssi, check rx stuck right now. */
2928 } else if (priv->undecorated_smoothed_pwdb < (RATE_ADAPTIVE_TH_HIGH + 5) &&
2929 ((priv->CurrentChannelBW != HT_CHANNEL_WIDTH_20 && priv->undecorated_smoothed_pwdb >= RATE_ADAPTIVE_TH_LOW_40M) ||
2930 (priv->CurrentChannelBW == HT_CHANNEL_WIDTH_20 && priv->undecorated_smoothed_pwdb >= RATE_ADAPTIVE_TH_LOW_20M))) {
2935 } else if (((priv->CurrentChannelBW != HT_CHANNEL_WIDTH_20 && priv->undecorated_smoothed_pwdb < RATE_ADAPTIVE_TH_LOW_40M) ||
2936 (priv->CurrentChannelBW == HT_CHANNEL_WIDTH_20 && priv->undecorated_smoothed_pwdb < RATE_ADAPTIVE_TH_LOW_20M)) &&
2937 priv->undecorated_smoothed_pwdb >= VERY_LOW_RSSI) {
2949 if (priv->RxCounter == RegRxCounter)
2952 priv->RxCounter = RegRxCounter;
2957 static RESET_TYPE RxCheckStuck(struct net_device *dev)
2959 struct r8192_priv *priv = ieee80211_priv(dev);
2960 bool bRxCheck = false;
2962 if (priv->IrpPendingCount > 1)
2966 if (HalRxCheckStuck819xUsb(dev)) {
2967 RT_TRACE(COMP_RESET, "RxStuck Condition\n");
2968 return RESET_TYPE_SILENT;
2971 return RESET_TYPE_NORESET;
2975 * This function is called by Checkforhang to check whether we should
2976 * ask OS to reset driver
2978 * \param pAdapter The adapter context for this miniport
2980 * Note:NIC with USB interface sholud not call this function because we
2981 * cannot scan descriptor to judge whether there is tx stuck.
2982 * Note: This function may be required to be rewrite for Vista OS.
2983 * <<<Assumption: Tx spinlock has been acquired >>>
2985 * 8185 and 8185b does not implement this function.
2987 static RESET_TYPE rtl819x_ifcheck_resetornot(struct net_device *dev)
2989 struct r8192_priv *priv = ieee80211_priv(dev);
2990 RESET_TYPE TxResetType = RESET_TYPE_NORESET;
2991 RESET_TYPE RxResetType = RESET_TYPE_NORESET;
2992 RT_RF_POWER_STATE rfState;
2994 rfState = priv->ieee80211->eRFPowerState;
2996 TxResetType = TxCheckStuck(dev);
2997 if (rfState != eRfOff ||
2998 (priv->ieee80211->iw_mode != IW_MODE_ADHOC)) {
2999 /* If driver is in the status of firmware download failure,
3000 * driver skips RF initialization and RF is in turned off
3001 * state. Driver should check whether Rx stuck and do silent
3002 * reset. And if driver is in firmware download failure status,
3003 * driver should initialize RF in the following silent reset
3006 * Driver should not check RX stuck in IBSS mode because it is
3007 * required to set Check BSSID in order to send beacon,
3008 * however, if check BSSID is set, STA cannot hear any packet
3011 RxResetType = RxCheckStuck(dev);
3013 if (TxResetType == RESET_TYPE_NORMAL ||
3014 RxResetType == RESET_TYPE_NORMAL) {
3015 return RESET_TYPE_NORMAL;
3016 } else if (TxResetType == RESET_TYPE_SILENT ||
3017 RxResetType == RESET_TYPE_SILENT) {
3018 RT_TRACE(COMP_RESET, "%s():silent reset\n", __func__);
3019 return RESET_TYPE_SILENT;
3021 return RESET_TYPE_NORESET;
3025 static void rtl8192_cancel_deferred_work(struct r8192_priv *priv);
3026 static int _rtl8192_up(struct net_device *dev);
3027 static int rtl8192_close(struct net_device *dev);
3029 static void CamRestoreAllEntry(struct net_device *dev)
3032 struct r8192_priv *priv = ieee80211_priv(dev);
3033 u8 *MacAddr = priv->ieee80211->current_network.bssid;
3035 static u8 CAM_CONST_ADDR[4][6] = {
3036 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
3037 {0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
3038 {0x00, 0x00, 0x00, 0x00, 0x00, 0x02},
3039 {0x00, 0x00, 0x00, 0x00, 0x00, 0x03} };
3040 static u8 CAM_CONST_BROAD[] = {
3041 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
3043 RT_TRACE(COMP_SEC, "%s:\n", __func__);
3045 if ((priv->ieee80211->pairwise_key_type == KEY_TYPE_WEP40) ||
3046 (priv->ieee80211->pairwise_key_type == KEY_TYPE_WEP104)) {
3047 for (EntryId = 0; EntryId < 4; EntryId++) {
3048 MacAddr = CAM_CONST_ADDR[EntryId];
3049 setKey(dev, EntryId, EntryId,
3050 priv->ieee80211->pairwise_key_type,
3054 } else if (priv->ieee80211->pairwise_key_type == KEY_TYPE_TKIP) {
3055 if (priv->ieee80211->iw_mode == IW_MODE_ADHOC)
3056 setKey(dev, 4, 0, priv->ieee80211->pairwise_key_type,
3057 (u8 *)dev->dev_addr, 0, NULL);
3059 setKey(dev, 4, 0, priv->ieee80211->pairwise_key_type,
3061 } else if (priv->ieee80211->pairwise_key_type == KEY_TYPE_CCMP) {
3062 if (priv->ieee80211->iw_mode == IW_MODE_ADHOC)
3063 setKey(dev, 4, 0, priv->ieee80211->pairwise_key_type,
3064 (u8 *)dev->dev_addr, 0, NULL);
3066 setKey(dev, 4, 0, priv->ieee80211->pairwise_key_type,
3070 if (priv->ieee80211->group_key_type == KEY_TYPE_TKIP) {
3071 MacAddr = CAM_CONST_BROAD;
3072 for (EntryId = 1; EntryId < 4; EntryId++) {
3073 setKey(dev, EntryId, EntryId,
3074 priv->ieee80211->group_key_type,
3077 if (priv->ieee80211->iw_mode == IW_MODE_ADHOC)
3078 setKey(dev, 0, 0, priv->ieee80211->group_key_type,
3079 CAM_CONST_ADDR[0], 0, NULL);
3080 } else if (priv->ieee80211->group_key_type == KEY_TYPE_CCMP) {
3081 MacAddr = CAM_CONST_BROAD;
3082 for (EntryId = 1; EntryId < 4; EntryId++) {
3083 setKey(dev, EntryId, EntryId,
3084 priv->ieee80211->group_key_type,
3088 if (priv->ieee80211->iw_mode == IW_MODE_ADHOC)
3089 setKey(dev, 0, 0, priv->ieee80211->group_key_type,
3090 CAM_CONST_ADDR[0], 0, NULL);
3094 /* This function is used to fix Tx/Rx stop bug temporarily.
3095 * This function will do "system reset" to NIC when Tx or Rx is stuck.
3096 * The method checking Tx/Rx stuck of this function is supported by FW,
3097 * which reports Tx and Rx counter to register 0x128 and 0x130.
3099 static void rtl819x_ifsilentreset(struct net_device *dev)
3101 struct r8192_priv *priv = ieee80211_priv(dev);
3103 int reset_status = 0;
3104 struct ieee80211_device *ieee = priv->ieee80211;
3106 /* If we need to check CCK stop, please uncomment this line. */
3107 /* bStuck = Adapter->HalFunc.CheckHWStopHandler(Adapter); */
3109 if (priv->ResetProgress == RESET_TYPE_NORESET) {
3112 RT_TRACE(COMP_RESET, "=========>Reset progress!!\n");
3114 /* Set the variable for reset. */
3115 priv->ResetProgress = RESET_TYPE_SILENT;
3116 mutex_lock(&priv->wx_mutex);
3117 if (priv->up == 0) {
3119 "%s():the driver is not up! return\n",
3121 mutex_unlock(&priv->wx_mutex);
3125 RT_TRACE(COMP_RESET,
3126 "%s():======>start to down the driver\n",
3129 rtl8192_rtx_disable(dev);
3130 rtl8192_cancel_deferred_work(priv);
3132 del_timer_sync(&priv->watch_dog_timer);
3134 ieee->sync_scan_hurryup = 1;
3135 if (ieee->state == IEEE80211_LINKED) {
3136 mutex_lock(&ieee->wx_mutex);
3137 netdev_dbg(dev, "ieee->state is IEEE80211_LINKED\n");
3138 ieee80211_stop_send_beacons(priv->ieee80211);
3139 del_timer_sync(&ieee->associate_timer);
3140 cancel_delayed_work(&ieee->associate_retry_wq);
3141 ieee80211_stop_scan(ieee);
3142 netif_carrier_off(dev);
3143 mutex_unlock(&ieee->wx_mutex);
3145 netdev_dbg(dev, "ieee->state is NOT LINKED\n");
3146 ieee80211_softmac_stop_protocol(priv->ieee80211);
3148 mutex_unlock(&priv->wx_mutex);
3149 RT_TRACE(COMP_RESET,
3150 "%s():<==========down process is finished\n",
3152 RT_TRACE(COMP_RESET,
3153 "%s():===========>start up the driver\n",
3155 reset_status = _rtl8192_up(dev);
3157 RT_TRACE(COMP_RESET,
3158 "%s():<===========up process is finished\n",
3160 if (reset_status == -EAGAIN) {
3161 if (reset_times < 3) {
3166 " ERR!!! %s(): Reset Failed!!\n",
3170 ieee->is_silent_reset = 1;
3171 EnableHWSecurityConfig8192(dev);
3172 if (ieee->state == IEEE80211_LINKED &&
3173 ieee->iw_mode == IW_MODE_INFRA) {
3174 ieee->set_chan(ieee->dev,
3175 ieee->current_network.channel);
3177 queue_work(ieee->wq, &ieee->associate_complete_wq);
3179 } else if (ieee->state == IEEE80211_LINKED &&
3180 ieee->iw_mode == IW_MODE_ADHOC) {
3181 ieee->set_chan(ieee->dev,
3182 ieee->current_network.channel);
3183 ieee->link_change(ieee->dev);
3185 ieee80211_start_send_beacons(ieee);
3187 if (ieee->data_hard_resume)
3188 ieee->data_hard_resume(ieee->dev);
3189 netif_carrier_on(ieee->dev);
3192 CamRestoreAllEntry(dev);
3194 priv->ResetProgress = RESET_TYPE_NORESET;
3195 priv->reset_count++;
3197 priv->bForcedSilentReset = false;
3198 priv->bResetInProgress = false;
3200 /* For test --> force write UFWP. */
3201 write_nic_byte(dev, UFWP, 1);
3202 RT_TRACE(COMP_RESET,
3203 "Reset finished!! ====>[%d]\n",
3208 static void rtl819x_update_rxcounts(struct r8192_priv *priv, u32 *TotalRxBcnNum,
3209 u32 *TotalRxDataNum)
3215 *TotalRxDataNum = 0;
3217 SlotIndex = (priv->ieee80211->LinkDetectInfo.SlotIndex++) %
3218 (priv->ieee80211->LinkDetectInfo.SlotNum);
3219 priv->ieee80211->LinkDetectInfo.RxBcnNum[SlotIndex] =
3220 priv->ieee80211->LinkDetectInfo.NumRecvBcnInPeriod;
3221 priv->ieee80211->LinkDetectInfo.RxDataNum[SlotIndex] =
3222 priv->ieee80211->LinkDetectInfo.NumRecvDataInPeriod;
3223 for (i = 0; i < priv->ieee80211->LinkDetectInfo.SlotNum; i++) {
3224 *TotalRxBcnNum += priv->ieee80211->LinkDetectInfo.RxBcnNum[i];
3225 *TotalRxDataNum += priv->ieee80211->LinkDetectInfo.RxDataNum[i];
3229 static void rtl819x_watchdog_wqcallback(struct work_struct *work)
3231 struct delayed_work *dwork = to_delayed_work(work);
3232 struct r8192_priv *priv = container_of(dwork,
3233 struct r8192_priv, watch_dog_wq);
3234 struct net_device *dev = priv->ieee80211->dev;
3235 struct ieee80211_device *ieee = priv->ieee80211;
3236 RESET_TYPE ResetType = RESET_TYPE_NORESET;
3237 static u8 check_reset_cnt;
3238 bool bBusyTraffic = false;
3239 u32 TotalRxBcnNum = 0;
3240 u32 TotalRxDataNum = 0;
3244 hal_dm_watchdog(dev);
3246 /* to get busy traffic condition */
3247 if (ieee->state == IEEE80211_LINKED) {
3248 if (ieee->LinkDetectInfo.NumRxOkInPeriod > 666 ||
3249 ieee->LinkDetectInfo.NumTxOkInPeriod > 666) {
3250 bBusyTraffic = true;
3252 ieee->LinkDetectInfo.NumRxOkInPeriod = 0;
3253 ieee->LinkDetectInfo.NumTxOkInPeriod = 0;
3254 ieee->LinkDetectInfo.bBusyTraffic = bBusyTraffic;
3256 /* for AP roaming */
3257 if (priv->ieee80211->state == IEEE80211_LINKED &&
3258 priv->ieee80211->iw_mode == IW_MODE_INFRA) {
3259 rtl819x_update_rxcounts(priv, &TotalRxBcnNum, &TotalRxDataNum);
3260 if ((TotalRxBcnNum + TotalRxDataNum) == 0) {
3262 if (rfState == eRfOff)
3263 RT_TRACE(COMP_ERR, "========>%s()\n", __func__);
3266 "===>%s(): AP is power off, connect another one\n",
3268 priv->ieee80211->state = IEEE80211_ASSOCIATING;
3269 notify_wx_assoc_event(priv->ieee80211);
3270 RemovePeerTS(priv->ieee80211,
3271 priv->ieee80211->current_network.bssid);
3272 priv->ieee80211->link_change(dev);
3273 queue_work(priv->ieee80211->wq,
3274 &priv->ieee80211->associate_procedure_wq);
3277 priv->ieee80211->LinkDetectInfo.NumRecvBcnInPeriod = 0;
3278 priv->ieee80211->LinkDetectInfo.NumRecvDataInPeriod = 0;
3279 /* check if reset the driver */
3280 if (check_reset_cnt++ >= 3) {
3281 ResetType = rtl819x_ifcheck_resetornot(dev);
3282 check_reset_cnt = 3;
3284 /* This is control by OID set in Pomelo */
3285 if ((priv->force_reset) || (priv->ResetProgress == RESET_TYPE_NORESET &&
3286 (priv->bForcedSilentReset ||
3287 (!priv->bDisableNormalResetCheck && ResetType == RESET_TYPE_SILENT)))) {
3288 RT_TRACE(COMP_RESET,
3289 "%s():priv->force_reset is %d,priv->ResetProgress is %d, priv->bForcedSilentReset is %d,priv->bDisableNormalResetCheck is %d,ResetType is %d\n",
3290 __func__, priv->force_reset, priv->ResetProgress,
3291 priv->bForcedSilentReset,
3292 priv->bDisableNormalResetCheck, ResetType);
3293 rtl819x_ifsilentreset(dev);
3295 priv->force_reset = false;
3296 priv->bForcedSilentReset = false;
3297 priv->bResetInProgress = false;
3298 RT_TRACE(COMP_TRACE, " <==RtUsbCheckForHangWorkItemCallback()\n");
3301 static void watch_dog_timer_callback(struct timer_list *t)
3303 struct r8192_priv *priv = from_timer(priv, t, watch_dog_timer);
3305 schedule_delayed_work(&priv->watch_dog_wq, 0);
3306 mod_timer(&priv->watch_dog_timer,
3307 jiffies + msecs_to_jiffies(IEEE80211_WATCH_DOG_TIME));
3310 static int _rtl8192_up(struct net_device *dev)
3312 struct r8192_priv *priv = ieee80211_priv(dev);
3313 int init_status = 0;
3316 priv->ieee80211->ieee_up = 1;
3317 RT_TRACE(COMP_INIT, "Bringing up iface");
3318 init_status = rtl8192_adapter_start(dev);
3320 RT_TRACE(COMP_ERR, "ERR!!! %s(): initialization failed!\n",
3322 priv->up = priv->ieee80211->ieee_up = 0;
3325 RT_TRACE(COMP_INIT, "start adapter finished\n");
3326 rtl8192_rx_enable(dev);
3327 if (priv->ieee80211->state != IEEE80211_LINKED)
3328 ieee80211_softmac_start_protocol(priv->ieee80211);
3329 ieee80211_reset_queue(priv->ieee80211);
3330 watch_dog_timer_callback(&priv->watch_dog_timer);
3331 if (!netif_queue_stopped(dev))
3332 netif_start_queue(dev);
3334 netif_wake_queue(dev);
3339 static int rtl8192_open(struct net_device *dev)
3341 struct r8192_priv *priv = ieee80211_priv(dev);
3344 mutex_lock(&priv->wx_mutex);
3345 ret = rtl8192_up(dev);
3346 mutex_unlock(&priv->wx_mutex);
3350 int rtl8192_up(struct net_device *dev)
3352 struct r8192_priv *priv = ieee80211_priv(dev);
3357 return _rtl8192_up(dev);
3360 static int rtl8192_close(struct net_device *dev)
3362 struct r8192_priv *priv = ieee80211_priv(dev);
3365 mutex_lock(&priv->wx_mutex);
3367 ret = rtl8192_down(dev);
3369 mutex_unlock(&priv->wx_mutex);
3374 int rtl8192_down(struct net_device *dev)
3376 struct r8192_priv *priv = ieee80211_priv(dev);
3383 priv->ieee80211->ieee_up = 0;
3384 RT_TRACE(COMP_DOWN, "==========>%s()\n", __func__);
3386 if (!netif_queue_stopped(dev))
3387 netif_stop_queue(dev);
3389 rtl8192_rtx_disable(dev);
3391 /* Tx related queue release */
3392 for (i = 0; i < MAX_QUEUE_SIZE; i++)
3393 skb_queue_purge(&priv->ieee80211->skb_waitQ[i]);
3394 for (i = 0; i < MAX_QUEUE_SIZE; i++)
3395 skb_queue_purge(&priv->ieee80211->skb_aggQ[i]);
3397 for (i = 0; i < MAX_QUEUE_SIZE; i++)
3398 skb_queue_purge(&priv->ieee80211->skb_drv_aggQ[i]);
3400 /* as cancel_delayed_work will del work->timer, so if work is not
3401 * defined as struct delayed_work, it will corrupt
3403 rtl8192_cancel_deferred_work(priv);
3405 del_timer_sync(&priv->watch_dog_timer);
3407 ieee80211_softmac_stop_protocol(priv->ieee80211);
3408 memset(&priv->ieee80211->current_network, 0,
3409 offsetof(struct ieee80211_network, list));
3410 RT_TRACE(COMP_DOWN, "<==========%s()\n", __func__);
3415 void rtl8192_commit(struct net_device *dev)
3417 struct r8192_priv *priv = ieee80211_priv(dev);
3418 int reset_status = 0;
3424 rtl8192_cancel_deferred_work(priv);
3425 del_timer_sync(&priv->watch_dog_timer);
3427 ieee80211_softmac_stop_protocol(priv->ieee80211);
3429 rtl8192_rtx_disable(dev);
3430 reset_status = _rtl8192_up(dev);
3433 static void rtl8192_restart(struct work_struct *work)
3435 struct r8192_priv *priv = container_of(work, struct r8192_priv,
3437 struct net_device *dev = priv->ieee80211->dev;
3439 mutex_lock(&priv->wx_mutex);
3441 rtl8192_commit(dev);
3443 mutex_unlock(&priv->wx_mutex);
3446 static void r8192_set_multicast(struct net_device *dev)
3448 struct r8192_priv *priv = ieee80211_priv(dev);
3453 promisc = (dev->flags & IFF_PROMISC) ? 1 : 0;
3455 if (promisc != priv->promisc)
3457 priv->promisc = promisc;
3460 static int r8192_set_mac_adr(struct net_device *dev, void *mac)
3462 struct r8192_priv *priv = ieee80211_priv(dev);
3463 struct sockaddr *addr = mac;
3465 mutex_lock(&priv->wx_mutex);
3467 ether_addr_copy(dev->dev_addr, addr->sa_data);
3469 schedule_work(&priv->reset_wq);
3470 mutex_unlock(&priv->wx_mutex);
3475 /* based on ipw2200 driver */
3476 static int rtl8192_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3478 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
3479 struct iwreq *wrq = (struct iwreq *)rq;
3481 struct ieee80211_device *ieee = priv->ieee80211;
3483 u8 broadcast_addr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
3484 struct iw_point *p = &wrq->u.data;
3485 struct ieee_param *ipw = NULL;
3487 mutex_lock(&priv->wx_mutex);
3489 if (p->length < sizeof(struct ieee_param) || !p->pointer) {
3494 ipw = memdup_user(p->pointer, p->length);
3501 case RTL_IOCTL_WPA_SUPPLICANT:
3502 /* parse here for HW security */
3503 if (ipw->cmd == IEEE_CMD_SET_ENCRYPTION) {
3504 if (ipw->u.crypt.set_tx) {
3505 if (strcmp(ipw->u.crypt.alg, "CCMP") == 0) {
3506 ieee->pairwise_key_type = KEY_TYPE_CCMP;
3507 } else if (strcmp(ipw->u.crypt.alg, "TKIP") == 0) {
3508 ieee->pairwise_key_type = KEY_TYPE_TKIP;
3509 } else if (strcmp(ipw->u.crypt.alg, "WEP") == 0) {
3510 if (ipw->u.crypt.key_len == 13)
3511 ieee->pairwise_key_type = KEY_TYPE_WEP104;
3512 else if (ipw->u.crypt.key_len == 5)
3513 ieee->pairwise_key_type = KEY_TYPE_WEP40;
3515 ieee->pairwise_key_type = KEY_TYPE_NA;
3518 if (ieee->pairwise_key_type) {
3519 memcpy((u8 *)key, ipw->u.crypt.key, 16);
3520 EnableHWSecurityConfig8192(dev);
3521 /* We fill both index entry and 4th
3522 * entry for pairwise key as in IPW
3523 * interface, adhoc will only get here,
3524 * so we need index entry for its
3525 * default key serching!
3527 setKey(dev, 4, ipw->u.crypt.idx,
3528 ieee->pairwise_key_type,
3529 (u8 *)ieee->ap_mac_addr,
3531 if (ieee->auth_mode != 2)
3532 setKey(dev, ipw->u.crypt.idx,
3534 ieee->pairwise_key_type,
3535 (u8 *)ieee->ap_mac_addr,
3539 memcpy((u8 *)key, ipw->u.crypt.key, 16);
3540 if (strcmp(ipw->u.crypt.alg, "CCMP") == 0) {
3541 ieee->group_key_type = KEY_TYPE_CCMP;
3542 } else if (strcmp(ipw->u.crypt.alg, "TKIP") == 0) {
3543 ieee->group_key_type = KEY_TYPE_TKIP;
3544 } else if (strcmp(ipw->u.crypt.alg, "WEP") == 0) {
3545 if (ipw->u.crypt.key_len == 13)
3546 ieee->group_key_type = KEY_TYPE_WEP104;
3547 else if (ipw->u.crypt.key_len == 5)
3548 ieee->group_key_type = KEY_TYPE_WEP40;
3550 ieee->group_key_type = KEY_TYPE_NA;
3553 if (ieee->group_key_type) {
3554 setKey(dev, ipw->u.crypt.idx,
3558 ieee->group_key_type,
3568 ret = ieee80211_wpa_supplicant_ioctl(priv->ieee80211,
3579 mutex_unlock(&priv->wx_mutex);
3583 static u8 HwRateToMRate90(bool bIsHT, u8 rate)
3595 case DESC90_RATE5_5M:
3596 ret_rate = MGN_5_5M;
3598 case DESC90_RATE11M:
3607 case DESC90_RATE12M:
3610 case DESC90_RATE18M:
3613 case DESC90_RATE24M:
3616 case DESC90_RATE36M:
3619 case DESC90_RATE48M:
3622 case DESC90_RATE54M:
3629 "%s: Non supported Rate [%x], bIsHT = %d!!!\n",
3630 __func__, rate, bIsHT);
3636 case DESC90_RATEMCS0:
3637 ret_rate = MGN_MCS0;
3639 case DESC90_RATEMCS1:
3640 ret_rate = MGN_MCS1;
3642 case DESC90_RATEMCS2:
3643 ret_rate = MGN_MCS2;
3645 case DESC90_RATEMCS3:
3646 ret_rate = MGN_MCS3;
3648 case DESC90_RATEMCS4:
3649 ret_rate = MGN_MCS4;
3651 case DESC90_RATEMCS5:
3652 ret_rate = MGN_MCS5;
3654 case DESC90_RATEMCS6:
3655 ret_rate = MGN_MCS6;
3657 case DESC90_RATEMCS7:
3658 ret_rate = MGN_MCS7;
3660 case DESC90_RATEMCS8:
3661 ret_rate = MGN_MCS8;
3663 case DESC90_RATEMCS9:
3664 ret_rate = MGN_MCS9;
3666 case DESC90_RATEMCS10:
3667 ret_rate = MGN_MCS10;
3669 case DESC90_RATEMCS11:
3670 ret_rate = MGN_MCS11;
3672 case DESC90_RATEMCS12:
3673 ret_rate = MGN_MCS12;
3675 case DESC90_RATEMCS13:
3676 ret_rate = MGN_MCS13;
3678 case DESC90_RATEMCS14:
3679 ret_rate = MGN_MCS14;
3681 case DESC90_RATEMCS15:
3682 ret_rate = MGN_MCS15;
3684 case DESC90_RATEMCS32:
3685 ret_rate = 0x80 | 0x20;
3691 "%s: Non supported Rate [%x], bIsHT = %d!!!\n",
3692 __func__, rate, bIsHT);
3701 * Function: UpdateRxPktTimeStamp
3702 * Overview: Record the TSF time stamp when receiving a packet
3710 * (pRfd->Status.TimeStampHigh is updated)
3711 * (pRfd->Status.TimeStampLow is updated)
3715 static void UpdateRxPktTimeStamp8190(struct net_device *dev,
3716 struct ieee80211_rx_stats *stats)
3718 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
3720 if (stats->bIsAMPDU && !stats->bFirstMPDU) {
3721 stats->mac_time[0] = priv->LastRxDescTSFLow;
3722 stats->mac_time[1] = priv->LastRxDescTSFHigh;
3724 priv->LastRxDescTSFLow = stats->mac_time[0];
3725 priv->LastRxDescTSFHigh = stats->mac_time[1];
3730 static long rtl819x_translate_todbm(u8 signal_strength_index)
3732 long signal_power; /* in dBm. */
3734 /* Translate to dBm (x=0.5y-95). */
3735 signal_power = (long)((signal_strength_index + 1) >> 1);
3738 return signal_power;
3741 /* We can not declare RSSI/EVM total value of sliding window to
3742 * be a local static. Otherwise, it may increase when we return from S3/S4. The
3743 * value will be kept in memory or disk. Declare the value in the adaptor
3744 * and it will be reinitialized when returned from S3/S4.
3746 static void rtl8192_process_phyinfo(struct r8192_priv *priv, u8 *buffer,
3747 struct ieee80211_rx_stats *pprevious_stats,
3748 struct ieee80211_rx_stats *pcurrent_stats)
3750 bool bcheck = false;
3752 u32 nspatial_stream, tmp_val;
3753 static u32 slide_rssi_index, slide_rssi_statistics;
3754 static u32 slide_evm_index, slide_evm_statistics;
3755 static u32 last_rssi, last_evm;
3757 static u32 slide_beacon_adc_pwdb_index;
3758 static u32 slide_beacon_adc_pwdb_statistics;
3759 static u32 last_beacon_adc_pwdb;
3761 struct rtl_80211_hdr_3addr *hdr;
3765 hdr = (struct rtl_80211_hdr_3addr *)buffer;
3766 sc = le16_to_cpu(hdr->seq_ctl);
3767 seq = WLAN_GET_SEQ_SEQ(sc);
3768 /* to record the sequence number */
3769 pcurrent_stats->Seq_Num = seq;
3771 /* Check whether we should take the previous packet into accounting */
3772 if (!pprevious_stats->bIsAMPDU) {
3773 /* if previous packet is not aggregated packet */
3777 if (slide_rssi_statistics++ >= PHY_RSSI_SLID_WIN_MAX) {
3778 slide_rssi_statistics = PHY_RSSI_SLID_WIN_MAX;
3779 last_rssi = priv->stats.slide_signal_strength[slide_rssi_index];
3780 priv->stats.slide_rssi_total -= last_rssi;
3782 priv->stats.slide_rssi_total += pprevious_stats->SignalStrength;
3784 priv->stats.slide_signal_strength[slide_rssi_index++] =
3785 pprevious_stats->SignalStrength;
3786 if (slide_rssi_index >= PHY_RSSI_SLID_WIN_MAX)
3787 slide_rssi_index = 0;
3789 /* <1> Showed on UI for user, in dbm */
3790 tmp_val = priv->stats.slide_rssi_total / slide_rssi_statistics;
3791 priv->stats.signal_strength = rtl819x_translate_todbm((u8)tmp_val);
3792 pcurrent_stats->rssi = priv->stats.signal_strength;
3794 /* If the previous packet does not match the criteria, neglect it */
3795 if (!pprevious_stats->bPacketMatchBSSID) {
3796 if (!pprevious_stats->bToSelfBA)
3803 /* only rtl8190 supported
3804 * rtl8190_process_cck_rxpathsel(priv,pprevious_stats);
3808 priv->stats.num_process_phyinfo++;
3810 /* record the general signal strength to the sliding window. */
3812 /* <2> Showed on UI for engineering
3813 * hardware does not provide rssi information for each rf path in CCK
3815 if (!pprevious_stats->bIsCCK &&
3816 (pprevious_stats->bPacketToSelf || pprevious_stats->bToSelfBA)) {
3817 for (rfpath = RF90_PATH_A; rfpath < priv->NumTotalRFPath; rfpath++) {
3818 if (!rtl8192_phy_CheckIsLegalRFPath(priv->ieee80211->dev,
3821 if (priv->stats.rx_rssi_percentage[rfpath] == 0)
3822 priv->stats.rx_rssi_percentage[rfpath] =
3823 pprevious_stats->RxMIMOSignalStrength[rfpath];
3824 if (pprevious_stats->RxMIMOSignalStrength[rfpath] > priv->stats.rx_rssi_percentage[rfpath]) {
3825 priv->stats.rx_rssi_percentage[rfpath] =
3826 ((priv->stats.rx_rssi_percentage[rfpath] * (RX_SMOOTH_FACTOR - 1)) +
3827 (pprevious_stats->RxMIMOSignalStrength[rfpath])) / (RX_SMOOTH_FACTOR);
3828 priv->stats.rx_rssi_percentage[rfpath] = priv->stats.rx_rssi_percentage[rfpath] + 1;
3830 priv->stats.rx_rssi_percentage[rfpath] =
3831 ((priv->stats.rx_rssi_percentage[rfpath] * (RX_SMOOTH_FACTOR - 1)) +
3832 (pprevious_stats->RxMIMOSignalStrength[rfpath])) / (RX_SMOOTH_FACTOR);
3835 "priv->stats.rx_rssi_percentage[rfPath] = %d\n",
3836 priv->stats.rx_rssi_percentage[rfpath]);
3841 RT_TRACE(COMP_RXDESC, "Smooth %s PWDB = %d\n",
3842 pprevious_stats->bIsCCK ? "CCK" : "OFDM",
3843 pprevious_stats->RxPWDBAll);
3845 if (pprevious_stats->bPacketBeacon) {
3846 /* record the beacon pwdb to the sliding window. */
3847 if (slide_beacon_adc_pwdb_statistics++ >= PHY_Beacon_RSSI_SLID_WIN_MAX) {
3848 slide_beacon_adc_pwdb_statistics = PHY_Beacon_RSSI_SLID_WIN_MAX;
3849 last_beacon_adc_pwdb = priv->stats.Slide_Beacon_pwdb[slide_beacon_adc_pwdb_index];
3850 priv->stats.Slide_Beacon_Total -= last_beacon_adc_pwdb;
3852 priv->stats.Slide_Beacon_Total += pprevious_stats->RxPWDBAll;
3853 priv->stats.Slide_Beacon_pwdb[slide_beacon_adc_pwdb_index] = pprevious_stats->RxPWDBAll;
3854 slide_beacon_adc_pwdb_index++;
3855 if (slide_beacon_adc_pwdb_index >= PHY_Beacon_RSSI_SLID_WIN_MAX)
3856 slide_beacon_adc_pwdb_index = 0;
3857 pprevious_stats->RxPWDBAll = priv->stats.Slide_Beacon_Total / slide_beacon_adc_pwdb_statistics;
3858 if (pprevious_stats->RxPWDBAll >= 3)
3859 pprevious_stats->RxPWDBAll -= 3;
3862 RT_TRACE(COMP_RXDESC, "Smooth %s PWDB = %d\n",
3863 pprevious_stats->bIsCCK ? "CCK" : "OFDM",
3864 pprevious_stats->RxPWDBAll);
3866 if (pprevious_stats->bPacketToSelf ||
3867 pprevious_stats->bPacketBeacon ||
3868 pprevious_stats->bToSelfBA) {
3869 if (priv->undecorated_smoothed_pwdb < 0)
3871 priv->undecorated_smoothed_pwdb =
3872 pprevious_stats->RxPWDBAll;
3873 if (pprevious_stats->RxPWDBAll > (u32)priv->undecorated_smoothed_pwdb) {
3874 priv->undecorated_smoothed_pwdb =
3875 (((priv->undecorated_smoothed_pwdb) * (RX_SMOOTH_FACTOR - 1)) +
3876 (pprevious_stats->RxPWDBAll)) / (RX_SMOOTH_FACTOR);
3877 priv->undecorated_smoothed_pwdb = priv->undecorated_smoothed_pwdb + 1;
3879 priv->undecorated_smoothed_pwdb =
3880 (((priv->undecorated_smoothed_pwdb) * (RX_SMOOTH_FACTOR - 1)) +
3881 (pprevious_stats->RxPWDBAll)) / (RX_SMOOTH_FACTOR);
3886 /* record the general EVM to the sliding window. */
3887 if (pprevious_stats->SignalQuality) {
3888 if (pprevious_stats->bPacketToSelf ||
3889 pprevious_stats->bPacketBeacon ||
3890 pprevious_stats->bToSelfBA) {
3891 if (slide_evm_statistics++ >= PHY_RSSI_SLID_WIN_MAX) {
3892 slide_evm_statistics = PHY_RSSI_SLID_WIN_MAX;
3893 last_evm = priv->stats.slide_evm[slide_evm_index];
3894 priv->stats.slide_evm_total -= last_evm;
3897 priv->stats.slide_evm_total +=
3898 pprevious_stats->SignalQuality;
3900 priv->stats.slide_evm[slide_evm_index++] =
3901 pprevious_stats->SignalQuality;
3902 if (slide_evm_index >= PHY_RSSI_SLID_WIN_MAX)
3903 slide_evm_index = 0;
3905 /* <1> Showed on UI for user, in percentage. */
3906 tmp_val = priv->stats.slide_evm_total /
3907 slide_evm_statistics;
3908 priv->stats.signal_quality = tmp_val;
3909 /* Showed on UI for user in Windows Vista,
3912 priv->stats.last_signal_strength_inpercent = tmp_val;
3915 /* <2> Showed on UI for engineering */
3916 if (pprevious_stats->bPacketToSelf ||
3917 pprevious_stats->bPacketBeacon ||
3918 pprevious_stats->bToSelfBA) {
3919 for (nspatial_stream = 0; nspatial_stream < 2; nspatial_stream++) { /* 2 spatial stream */
3920 if (pprevious_stats->RxMIMOSignalQuality[nspatial_stream] != -1) {
3921 if (priv->stats.rx_evm_percentage[nspatial_stream] == 0) /* initialize */
3922 priv->stats.rx_evm_percentage[nspatial_stream] = pprevious_stats->RxMIMOSignalQuality[nspatial_stream];
3923 priv->stats.rx_evm_percentage[nspatial_stream] =
3924 ((priv->stats.rx_evm_percentage[nspatial_stream] * (RX_SMOOTH_FACTOR - 1)) +
3925 (pprevious_stats->RxMIMOSignalQuality[nspatial_stream] * 1)) / (RX_SMOOTH_FACTOR);
3932 /*-----------------------------------------------------------------------------
3933 * Function: rtl819x_query_rxpwrpercentage()
3937 * Input: char antpower
3941 * Return: 0-100 percentage
3942 *---------------------------------------------------------------------------
3944 static u8 rtl819x_query_rxpwrpercentage(s8 antpower)
3946 if ((antpower <= -100) || (antpower >= 20))
3948 else if (antpower >= 0)
3951 return 100 + antpower;
3953 } /* QueryRxPwrPercentage */
3955 static u8 rtl819x_evm_dbtopercentage(s8 value)
3965 ret_val = 0 - ret_val;
3972 /* We want good-looking for signal strength/quality */
3973 static long rtl819x_signal_scale_mapping(long currsig)
3977 /* Step 1. Scale mapping. */
3978 if (currsig >= 61 && currsig <= 100)
3979 retsig = 90 + ((currsig - 60) / 4);
3980 else if (currsig >= 41 && currsig <= 60)
3981 retsig = 78 + ((currsig - 40) / 2);
3982 else if (currsig >= 31 && currsig <= 40)
3983 retsig = 66 + (currsig - 30);
3984 else if (currsig >= 21 && currsig <= 30)
3985 retsig = 54 + (currsig - 20);
3986 else if (currsig >= 5 && currsig <= 20)
3987 retsig = 42 + (((currsig - 5) * 2) / 3);
3988 else if (currsig == 4)
3990 else if (currsig == 3)
3992 else if (currsig == 2)
3994 else if (currsig == 1)
4002 static inline bool rx_hal_is_cck_rate(struct rx_drvinfo_819x_usb *pdrvinfo)
4007 switch (pdrvinfo->RxRate) {
4010 case DESC90_RATE5_5M:
4011 case DESC90_RATE11M:
4018 static void rtl8192_query_rxphystatus(struct r8192_priv *priv,
4019 struct ieee80211_rx_stats *pstats,
4020 struct rx_drvinfo_819x_usb *pdrvinfo,
4021 struct ieee80211_rx_stats *precord_stats,
4022 bool bpacket_match_bssid,
4023 bool bpacket_toself,
4027 phy_sts_ofdm_819xusb_t *pofdm_buf;
4028 phy_sts_cck_819xusb_t *pcck_buf;
4029 struct phy_ofdm_rx_status_rxsc_sgien_exintfflag *prxsc;
4031 u8 i, max_spatial_stream, tmp_rxsnr, tmp_rxevm, rxsc_sgien_exflg;
4032 s8 rx_pwr[4], rx_pwr_all = 0;
4033 s8 rx_snrX, rx_evmX;
4035 u32 RSSI, total_rssi = 0;
4040 priv->stats.numqry_phystatus++;
4042 is_cck_rate = rx_hal_is_cck_rate(pdrvinfo);
4044 /* Record it for next packet processing */
4045 memset(precord_stats, 0, sizeof(struct ieee80211_rx_stats));
4046 pstats->bPacketMatchBSSID =
4047 precord_stats->bPacketMatchBSSID = bpacket_match_bssid;
4048 pstats->bPacketToSelf = precord_stats->bPacketToSelf = bpacket_toself;
4049 pstats->bIsCCK = precord_stats->bIsCCK = is_cck_rate;
4050 pstats->bPacketBeacon = precord_stats->bPacketBeacon = bPacketBeacon;
4051 pstats->bToSelfBA = precord_stats->bToSelfBA = bToSelfBA;
4053 prxpkt = (u8 *)pdrvinfo;
4055 /* Move pointer to the 16th bytes. Phy status start address. */
4056 prxpkt += sizeof(struct rx_drvinfo_819x_usb);
4058 /* Initial the cck and ofdm buffer pointer */
4059 pcck_buf = (phy_sts_cck_819xusb_t *)prxpkt;
4060 pofdm_buf = (phy_sts_ofdm_819xusb_t *)prxpkt;
4062 pstats->RxMIMOSignalQuality[0] = -1;
4063 pstats->RxMIMOSignalQuality[1] = -1;
4064 precord_stats->RxMIMOSignalQuality[0] = -1;
4065 precord_stats->RxMIMOSignalQuality[1] = -1;
4068 /* (1)Hardware does not provide RSSI for CCK */
4070 /* (2)PWDB, Average PWDB calculated by hardware
4071 * (for rate adaptive)
4075 priv->stats.numqry_phystatusCCK++;
4077 if (!priv->bCckHighPower) {
4078 report = pcck_buf->cck_agc_rpt & 0xc0;
4082 rx_pwr_all = -35 - (pcck_buf->cck_agc_rpt & 0x3e);
4085 rx_pwr_all = -23 - (pcck_buf->cck_agc_rpt & 0x3e);
4088 rx_pwr_all = -11 - (pcck_buf->cck_agc_rpt & 0x3e);
4091 rx_pwr_all = 6 - (pcck_buf->cck_agc_rpt & 0x3e);
4095 report = pcck_buf->cck_agc_rpt & 0x60;
4099 rx_pwr_all = -35 - ((pcck_buf->cck_agc_rpt & 0x1f) << 1);
4102 rx_pwr_all = -23 - ((pcck_buf->cck_agc_rpt & 0x1f) << 1);
4105 rx_pwr_all = -11 - ((pcck_buf->cck_agc_rpt & 0x1f) << 1);
4108 rx_pwr_all = 6 - ((pcck_buf->cck_agc_rpt & 0x1f) << 1);
4113 pwdb_all = rtl819x_query_rxpwrpercentage(rx_pwr_all);
4114 pstats->RxPWDBAll = precord_stats->RxPWDBAll = pwdb_all;
4115 pstats->RecvSignalPower = pwdb_all;
4117 /* (3) Get Signal Quality (EVM) */
4119 if (pstats->RxPWDBAll > 40) {
4122 sq = pcck_buf->sq_rpt;
4124 if (pcck_buf->sq_rpt > 64)
4126 else if (pcck_buf->sq_rpt < 20)
4129 sq = ((64 - sq) * 100) / 44;
4131 pstats->SignalQuality = precord_stats->SignalQuality = sq;
4132 pstats->RxMIMOSignalQuality[0] =
4133 precord_stats->RxMIMOSignalQuality[0] = sq;
4134 pstats->RxMIMOSignalQuality[1] =
4135 precord_stats->RxMIMOSignalQuality[1] = -1;
4138 priv->stats.numqry_phystatusHT++;
4140 /* (1)Get RSSI for HT rate */
4141 for (i = RF90_PATH_A; i < priv->NumTotalRFPath; i++) {
4142 /* We will judge RF RX path now. */
4143 if (priv->brfpath_rxenable[i])
4148 if (!rtl8192_phy_CheckIsLegalRFPath(priv->ieee80211->dev, i))
4152 ((pofdm_buf->trsw_gain_X[i] & 0x3F) * 2) - 106;
4154 /* Get Rx snr value in DB */
4155 tmp_rxsnr = pofdm_buf->rxsnr_X[i];
4156 rx_snrX = (s8)(tmp_rxsnr);
4158 priv->stats.rxSNRdB[i] = (long)rx_snrX;
4160 /* Translate DBM to percentage. */
4161 RSSI = rtl819x_query_rxpwrpercentage(rx_pwr[i]);
4164 /* Record Signal Strength for next packet */
4165 pstats->RxMIMOSignalStrength[i] = (u8)RSSI;
4166 precord_stats->RxMIMOSignalStrength[i] = (u8)RSSI;
4169 /* (2)PWDB, Average PWDB calculated by hardware
4170 * (for rate adaptive)
4172 rx_pwr_all = (((pofdm_buf->pwdb_all) >> 1) & 0x7f) - 106;
4173 pwdb_all = rtl819x_query_rxpwrpercentage(rx_pwr_all);
4175 pstats->RxPWDBAll = precord_stats->RxPWDBAll = pwdb_all;
4176 pstats->RxPower = precord_stats->RxPower = rx_pwr_all;
4178 /* (3)EVM of HT rate */
4179 if (pdrvinfo->RxHT && pdrvinfo->RxRate >= DESC90_RATEMCS8 &&
4180 pdrvinfo->RxRate <= DESC90_RATEMCS15)
4181 /* both spatial stream make sense */
4182 max_spatial_stream = 2;
4184 /* only spatial stream 1 makes sense */
4185 max_spatial_stream = 1;
4187 for (i = 0; i < max_spatial_stream; i++) {
4188 tmp_rxevm = pofdm_buf->rxevm_X[i];
4189 rx_evmX = (s8)(tmp_rxevm);
4191 /* Do not use shift operation like "rx_evmX >>= 1"
4192 * because the compiler of free build environment will
4193 * set the most significant bit to "zero" when doing
4194 * shifting operation which may change a negative value
4195 * to positive one, then the dbm value (which is
4196 * supposed to be negative) is not correct anymore.
4198 rx_evmX /= 2; /* dbm */
4200 evm = rtl819x_evm_dbtopercentage(rx_evmX);
4202 /* Fill value in RFD, Get the first spatial
4205 pstats->SignalQuality =
4206 precord_stats->SignalQuality =
4208 pstats->RxMIMOSignalQuality[i] =
4209 precord_stats->RxMIMOSignalQuality[i] =
4213 /* record rx statistics for debug */
4214 rxsc_sgien_exflg = pofdm_buf->rxsc_sgien_exflg;
4215 prxsc = (struct phy_ofdm_rx_status_rxsc_sgien_exintfflag *)
4217 if (pdrvinfo->BW) /* 40M channel */
4218 priv->stats.received_bwtype[1 + prxsc->rxsc]++;
4219 else /* 20M channel */
4220 priv->stats.received_bwtype[0]++;
4223 /* UI BSS List signal strength(in percentage), make it good looking,
4224 * from 0~100. It is assigned to the BSS List in
4225 * GetValueFromBeaconOrProbeRsp().
4228 pstats->SignalStrength =
4229 precord_stats->SignalStrength =
4230 (u8)(rtl819x_signal_scale_mapping((long)pwdb_all));
4232 /* We can judge RX path number now. */
4233 if (rf_rx_num != 0) {
4234 pstats->SignalStrength =
4235 precord_stats->SignalStrength =
4236 (u8)(rtl819x_signal_scale_mapping((long)(total_rssi /= rf_rx_num)));
4239 } /* QueryRxPhyStatus8190Pci */
4241 static void rtl8192_record_rxdesc_forlateruse(struct ieee80211_rx_stats *psrc_stats,
4242 struct ieee80211_rx_stats *ptarget_stats)
4244 ptarget_stats->bIsAMPDU = psrc_stats->bIsAMPDU;
4245 ptarget_stats->bFirstMPDU = psrc_stats->bFirstMPDU;
4246 ptarget_stats->Seq_Num = psrc_stats->Seq_Num;
4249 static void TranslateRxSignalStuff819xUsb(struct sk_buff *skb,
4250 struct ieee80211_rx_stats *pstats,
4251 struct rx_drvinfo_819x_usb *pdrvinfo)
4253 /* TODO: We must only check packet for current MAC address.
4256 struct rtl8192_rx_info *info = (struct rtl8192_rx_info *)skb->cb;
4257 struct net_device *dev = info->dev;
4258 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
4259 bool bpacket_match_bssid, bpacket_toself;
4260 bool bPacketBeacon = false, bToSelfBA = false;
4261 static struct ieee80211_rx_stats previous_stats;
4262 struct rtl_80211_hdr_3addr *hdr;
4265 /* Get Signal Quality for only RX data queue (but not command queue) */
4270 /* Get MAC frame start address. */
4271 tmp_buf = (u8 *)skb->data;
4273 hdr = (struct rtl_80211_hdr_3addr *)tmp_buf;
4274 fc = le16_to_cpu(hdr->frame_ctl);
4275 type = WLAN_FC_GET_TYPE(fc);
4276 praddr = hdr->addr1;
4278 /* Check if the received packet is acceptable. */
4279 bpacket_match_bssid = (type != IEEE80211_FTYPE_CTL) &&
4280 (ether_addr_equal(priv->ieee80211->current_network.bssid, (fc & IEEE80211_FCTL_TODS) ? hdr->addr1 : (fc & IEEE80211_FCTL_FROMDS) ? hdr->addr2 : hdr->addr3))
4281 && (!pstats->bHwError) && (!pstats->bCRC) && (!pstats->bICV);
4282 bpacket_toself = bpacket_match_bssid &
4283 (ether_addr_equal(praddr, priv->ieee80211->dev->dev_addr));
4285 if (WLAN_FC_GET_FRAMETYPE(fc) == IEEE80211_STYPE_BEACON)
4286 bPacketBeacon = true;
4287 if (WLAN_FC_GET_FRAMETYPE(fc) == IEEE80211_STYPE_BLOCKACK) {
4288 if ((ether_addr_equal(praddr, dev->dev_addr)))
4292 if (bpacket_match_bssid)
4293 priv->stats.numpacket_matchbssid++;
4295 priv->stats.numpacket_toself++;
4296 /* Process PHY information for previous packet (RSSI/PWDB/EVM)
4297 * Because phy information is contained in the last packet of AMPDU
4298 * only, so driver should process phy information of previous packet
4300 rtl8192_process_phyinfo(priv, tmp_buf, &previous_stats, pstats);
4301 rtl8192_query_rxphystatus(priv, pstats, pdrvinfo, &previous_stats,
4302 bpacket_match_bssid, bpacket_toself,
4303 bPacketBeacon, bToSelfBA);
4304 rtl8192_record_rxdesc_forlateruse(pstats, &previous_stats);
4308 * Function: UpdateReceivedRateHistogramStatistics
4309 * Overview: Record the received data rate
4312 * struct net_device *dev
4313 * struct ieee80211_rx_stats *stats
4317 * (priv->stats.ReceivedRateHistogram[] is updated)
4322 UpdateReceivedRateHistogramStatistics8190(struct net_device *dev,
4323 struct ieee80211_rx_stats *stats)
4325 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
4326 /* 0: Total, 1:OK, 2:CRC, 3:ICV */
4329 /* 1: short preamble/GI, 0: long preamble/GI */
4330 u32 preamble_guardinterval;
4334 else if (stats->bICV)
4337 if (stats->bShortPreamble)
4338 preamble_guardinterval = 1; /* short */
4340 preamble_guardinterval = 0; /* long */
4342 switch (stats->rate) {
4356 /* Legacy OFDM rate */
4381 /* 11n High throughput rate */
4434 priv->stats.received_preamble_GI[preamble_guardinterval][rateIndex]++;
4435 priv->stats.received_rate_histogram[0][rateIndex]++; /* total */
4436 priv->stats.received_rate_histogram[rcvType][rateIndex]++;
4439 static void query_rxdesc_status(struct sk_buff *skb,
4440 struct ieee80211_rx_stats *stats,
4441 bool bIsRxAggrSubframe)
4443 struct rtl8192_rx_info *info = (struct rtl8192_rx_info *)skb->cb;
4444 struct net_device *dev = info->dev;
4445 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
4446 struct rx_drvinfo_819x_usb *driver_info = NULL;
4448 /* Get Rx Descriptor Information */
4449 struct rx_desc_819x_usb *desc = (struct rx_desc_819x_usb *)skb->data;
4451 stats->Length = desc->Length;
4452 stats->RxDrvInfoSize = desc->RxDrvInfoSize;
4453 stats->RxBufShift = 0;
4454 stats->bICV = desc->ICV;
4455 stats->bCRC = desc->CRC32;
4456 stats->bHwError = stats->bCRC | stats->bICV;
4457 /* RTL8190 set this bit to indicate that Hw does not decrypt packet */
4458 stats->Decrypted = !desc->SWDec;
4460 if ((priv->ieee80211->pHTInfo->bCurrentHTSupport) &&
4461 (priv->ieee80211->pairwise_key_type == KEY_TYPE_CCMP))
4462 stats->bHwError = false;
4464 stats->bHwError = stats->bCRC | stats->bICV;
4466 if (stats->Length < 24 || stats->Length > MAX_8192U_RX_SIZE)
4467 stats->bHwError |= 1;
4468 /* Get Driver Info */
4469 /* TODO: Need to verify it on FGPA platform
4470 * Driver info are written to the RxBuffer following rx desc
4472 if (stats->RxDrvInfoSize != 0) {
4473 driver_info = (struct rx_drvinfo_819x_usb *)(skb->data
4474 + sizeof(struct rx_desc_819x_usb)
4479 if (!stats->bHwError) {
4482 ret_rate = HwRateToMRate90(driver_info->RxHT,
4483 driver_info->RxRate);
4484 if (ret_rate == 0xff) {
4485 /* Abnormal Case: Receive CRC OK packet with Rx
4486 * descriptor indicating non supported rate.
4487 * Special Error Handling here
4490 stats->bHwError = 1;
4491 /* Set 1M rate by default */
4492 stats->rate = MGN_1M;
4494 stats->rate = ret_rate;
4500 stats->bShortPreamble = driver_info->SPLCP;
4502 UpdateReceivedRateHistogramStatistics8190(dev, stats);
4504 stats->bIsAMPDU = (driver_info->PartAggr == 1);
4505 stats->bFirstMPDU = (driver_info->PartAggr == 1) &&
4506 (driver_info->FirstAGGR == 1);
4507 stats->TimeStampLow = driver_info->TSFL;
4509 UpdateRxPktTimeStamp8190(dev, stats);
4512 if (driver_info->FirstAGGR == 1 || driver_info->PartAggr == 1)
4513 RT_TRACE(COMP_RXDESC,
4514 "driver_info->FirstAGGR = %d, driver_info->PartAggr = %d\n",
4515 driver_info->FirstAGGR, driver_info->PartAggr);
4518 skb_pull(skb, sizeof(struct rx_desc_819x_usb));
4519 /* Get Total offset of MPDU Frame Body */
4520 if ((stats->RxBufShift + stats->RxDrvInfoSize) > 0) {
4522 skb_pull(skb, stats->RxBufShift + stats->RxDrvInfoSize);
4526 stats->RxIs40MHzPacket = driver_info->BW;
4527 TranslateRxSignalStuff819xUsb(skb, stats, driver_info);
4531 static void rtl8192_rx_nomal(struct sk_buff *skb)
4533 struct rtl8192_rx_info *info = (struct rtl8192_rx_info *)skb->cb;
4534 struct net_device *dev = info->dev;
4535 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
4536 struct ieee80211_rx_stats stats = {
4538 .noise = 0x100 - 98,
4540 .freq = IEEE80211_24GHZ_BAND,
4543 struct rtl_80211_hdr_1addr *ieee80211_hdr = NULL;
4544 bool unicast_packet = false;
4546 /* 20 is for ps-poll */
4547 if ((skb->len >= (20 + sizeof(struct rx_desc_819x_usb))) && (skb->len < RX_URB_SIZE)) {
4548 /* first packet should not contain Rx aggregation header */
4549 query_rxdesc_status(skb, &stats, false);
4551 /* hardware related info */
4552 /* Process the MPDU received */
4553 skb_trim(skb, skb->len - 4/*sCrcLng*/);
4555 rx_pkt_len = skb->len;
4556 ieee80211_hdr = (struct rtl_80211_hdr_1addr *)skb->data;
4557 unicast_packet = false;
4558 if (is_broadcast_ether_addr(ieee80211_hdr->addr1)) {
4560 } else if (is_multicast_ether_addr(ieee80211_hdr->addr1)) {
4563 /* unicast packet */
4564 unicast_packet = true;
4567 if (!ieee80211_rx(priv->ieee80211, skb, &stats)) {
4568 dev_kfree_skb_any(skb);
4570 priv->stats.rxoktotal++;
4572 priv->stats.rxbytesunicast += rx_pkt_len;
4575 priv->stats.rxurberr++;
4576 netdev_dbg(dev, "actual_length: %d\n", skb->len);
4577 dev_kfree_skb_any(skb);
4581 static void rtl819xusb_process_received_packet(struct net_device *dev,
4582 struct ieee80211_rx_stats *pstats)
4584 struct r8192_priv *priv = ieee80211_priv(dev);
4586 /* Get shifted bytes of Starting address of 802.11 header. */
4587 pstats->virtual_address += get_rxpacket_shiftbytes_819xusb(pstats);
4588 #ifdef TODO /* about HCT */
4589 if (!Adapter->bInHctTest)
4590 CountRxErrStatistics(Adapter, pRfd);
4592 #ifdef ENABLE_PS /* for adding ps function in future */
4593 RT_RF_POWER_STATE rtState;
4594 /* When RF is off, we should not count the packet for hw/sw synchronize
4595 * reason, ie. there may be a duration while sw switch is changed and
4596 * hw switch is being changed.
4598 Adapter->HalFunc.GetHwRegHandler(Adapter, HW_VAR_RF_STATE,
4600 if (rtState == eRfOff)
4603 priv->stats.rxframgment++;
4606 RmMonitorSignalStrength(Adapter, pRfd);
4608 /* We have to release RFD and return if rx pkt is cmd pkt. */
4609 if (rtl819xusb_rx_command_packet(dev, pstats))
4617 static void query_rx_cmdpkt_desc_status(struct sk_buff *skb,
4618 struct ieee80211_rx_stats *stats)
4620 struct rx_desc_819x_usb *desc = (struct rx_desc_819x_usb *)skb->data;
4622 /* Get Rx Descriptor Information */
4623 stats->virtual_address = (u8 *)skb->data;
4624 stats->Length = desc->Length;
4625 stats->RxDrvInfoSize = 0;
4626 stats->RxBufShift = 0;
4627 stats->packetlength = stats->Length - scrclng;
4628 stats->fraglength = stats->packetlength;
4629 stats->fragoffset = 0;
4630 stats->ntotalfrag = 1;
4633 static void rtl8192_rx_cmd(struct sk_buff *skb)
4635 struct rtl8192_rx_info *info = (struct rtl8192_rx_info *)skb->cb;
4636 struct net_device *dev = info->dev;
4638 struct ieee80211_rx_stats stats = {
4640 .noise = 0x100 - 98,
4642 .freq = IEEE80211_24GHZ_BAND,
4645 if ((skb->len >= (20 + sizeof(struct rx_desc_819x_usb))) && (skb->len < RX_URB_SIZE)) {
4646 query_rx_cmdpkt_desc_status(skb, &stats);
4647 /* prfd->queue_id = 1; */
4649 /* Process the command packet received. */
4651 rtl819xusb_process_received_packet(dev, &stats);
4653 dev_kfree_skb_any(skb);
4657 static void rtl8192_irq_rx_tasklet(unsigned long data)
4659 struct r8192_priv *priv = (struct r8192_priv *)data;
4660 struct sk_buff *skb;
4661 struct rtl8192_rx_info *info;
4663 while (NULL != (skb = skb_dequeue(&priv->skb_queue))) {
4664 info = (struct rtl8192_rx_info *)skb->cb;
4665 switch (info->out_pipe) {
4666 /* Nomal packet pipe */
4668 priv->IrpPendingCount--;
4669 rtl8192_rx_nomal(skb);
4672 /* Command packet pipe */
4674 RT_TRACE(COMP_RECV, "command in-pipe index(%d)\n",
4677 rtl8192_rx_cmd(skb);
4680 default: /* should never get here! */
4681 RT_TRACE(COMP_ERR, "Unknown in-pipe index(%d)\n",
4689 static const struct net_device_ops rtl8192_netdev_ops = {
4690 .ndo_open = rtl8192_open,
4691 .ndo_stop = rtl8192_close,
4692 .ndo_get_stats = rtl8192_stats,
4693 .ndo_tx_timeout = tx_timeout,
4694 .ndo_do_ioctl = rtl8192_ioctl,
4695 .ndo_set_rx_mode = r8192_set_multicast,
4696 .ndo_set_mac_address = r8192_set_mac_adr,
4697 .ndo_validate_addr = eth_validate_addr,
4698 .ndo_start_xmit = ieee80211_xmit,
4701 /****************************************************************************
4702 * ---------------------------- USB_STUFF---------------------------
4703 *****************************************************************************/
4705 static int rtl8192_usb_probe(struct usb_interface *intf,
4706 const struct usb_device_id *id)
4708 struct net_device *dev = NULL;
4709 struct r8192_priv *priv = NULL;
4710 struct usb_device *udev = interface_to_usbdev(intf);
4713 RT_TRACE(COMP_INIT, "Oops: i'm coming\n");
4715 dev = alloc_ieee80211(sizeof(struct r8192_priv));
4719 usb_set_intfdata(intf, dev);
4720 SET_NETDEV_DEV(dev, &intf->dev);
4721 priv = ieee80211_priv(dev);
4722 priv->ieee80211 = netdev_priv(dev);
4725 dev->netdev_ops = &rtl8192_netdev_ops;
4727 dev->wireless_handlers = &r8192_wx_handlers_def;
4729 dev->type = ARPHRD_ETHER;
4731 dev->watchdog_timeo = HZ * 3;
4733 if (dev_alloc_name(dev, ifname) < 0) {
4735 "Oops: devname already taken! Trying wlan%%d...\n");
4737 dev_alloc_name(dev, ifname);
4740 RT_TRACE(COMP_INIT, "Driver probe completed1\n");
4741 if (rtl8192_init(dev) != 0) {
4742 RT_TRACE(COMP_ERR, "Initialization failed");
4746 netif_carrier_off(dev);
4747 netif_stop_queue(dev);
4749 ret = register_netdev(dev);
4753 RT_TRACE(COMP_INIT, "dev name=======> %s\n", dev->name);
4754 rtl8192_proc_init_one(dev);
4756 RT_TRACE(COMP_INIT, "Driver probe completed\n");
4762 kfree(priv->pFirmware);
4763 priv->pFirmware = NULL;
4764 rtl8192_usb_deleteendpoints(dev);
4766 free_ieee80211(dev);
4768 RT_TRACE(COMP_ERR, "wlan driver load failed\n");
4772 /* detach all the work and timer structure declared or inititialize
4773 * in r8192U_init function.
4775 static void rtl8192_cancel_deferred_work(struct r8192_priv *priv)
4777 cancel_work_sync(&priv->reset_wq);
4778 cancel_delayed_work(&priv->watch_dog_wq);
4779 cancel_delayed_work(&priv->update_beacon_wq);
4780 cancel_work_sync(&priv->qos_activate);
4783 static void rtl8192_usb_disconnect(struct usb_interface *intf)
4785 struct net_device *dev = usb_get_intfdata(intf);
4786 struct r8192_priv *priv = ieee80211_priv(dev);
4788 unregister_netdev(dev);
4790 RT_TRACE(COMP_DOWN, "=============>wlan driver to be removed\n");
4791 rtl8192_proc_remove_one(dev);
4794 kfree(priv->pFirmware);
4795 priv->pFirmware = NULL;
4796 rtl8192_usb_deleteendpoints(dev);
4797 usleep_range(10000, 11000);
4798 free_ieee80211(dev);
4800 RT_TRACE(COMP_DOWN, "wlan driver removed\n");
4803 static int __init rtl8192_usb_module_init(void)
4807 #ifdef CONFIG_IEEE80211_DEBUG
4808 ret = ieee80211_debug_init();
4810 pr_err("ieee80211_debug_init() failed %d\n", ret);
4814 ret = ieee80211_crypto_init();
4816 pr_err("ieee80211_crypto_init() failed %d\n", ret);
4820 ret = ieee80211_crypto_tkip_init();
4822 pr_err("ieee80211_crypto_tkip_init() failed %d\n", ret);
4826 ret = ieee80211_crypto_ccmp_init();
4828 pr_err("ieee80211_crypto_ccmp_init() failed %d\n", ret);
4832 ret = ieee80211_crypto_wep_init();
4834 pr_err("ieee80211_crypto_wep_init() failed %d\n", ret);
4838 pr_info("\nLinux kernel driver for RTL8192 based WLAN cards\n");
4839 pr_info("Copyright (c) 2007-2008, Realsil Wlan\n");
4840 RT_TRACE(COMP_INIT, "Initializing module");
4841 RT_TRACE(COMP_INIT, "Wireless extensions version %d", WIRELESS_EXT);
4842 rtl8192_proc_module_init();
4843 return usb_register(&rtl8192_usb_driver);
4846 static void __exit rtl8192_usb_module_exit(void)
4848 usb_deregister(&rtl8192_usb_driver);
4850 RT_TRACE(COMP_DOWN, "Exiting");
4853 void EnableHWSecurityConfig8192(struct net_device *dev)
4855 u8 SECR_value = 0x0;
4856 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
4857 struct ieee80211_device *ieee = priv->ieee80211;
4859 SECR_value = SCR_TxEncEnable | SCR_RxDecEnable;
4860 if (((ieee->pairwise_key_type == KEY_TYPE_WEP40) || (ieee->pairwise_key_type == KEY_TYPE_WEP104)) && (priv->ieee80211->auth_mode != 2)) {
4861 SECR_value |= SCR_RxUseDK;
4862 SECR_value |= SCR_TxUseDK;
4863 } else if ((ieee->iw_mode == IW_MODE_ADHOC) && (ieee->pairwise_key_type & (KEY_TYPE_CCMP | KEY_TYPE_TKIP))) {
4864 SECR_value |= SCR_RxUseDK;
4865 SECR_value |= SCR_TxUseDK;
4867 /* add HWSec active enable here.
4868 * default using hwsec. when peer AP is in N mode only and
4869 * pairwise_key_type is none_aes(which HT_IOT_ACT_PURE_N_MODE indicates
4870 * it), use software security. when peer AP is in b,g,n mode mixed and
4871 * pairwise_key_type is none_aes, use g mode hw security.
4874 ieee->hwsec_active = 1;
4876 /* add hwsec_support flag to totol control hw_sec on/off */
4877 if ((ieee->pHTInfo->IOTAction & HT_IOT_ACT_PURE_N_MODE) || !hwwep) {
4878 ieee->hwsec_active = 0;
4879 SECR_value &= ~SCR_RxDecEnable;
4881 RT_TRACE(COMP_SEC, "%s:, hwsec:%d, pairwise_key:%d, SECR_value:%x\n",
4882 __func__, ieee->hwsec_active, ieee->pairwise_key_type,
4884 write_nic_byte(dev, SECR, SECR_value);
4887 void setKey(struct net_device *dev, u8 EntryNo, u8 KeyIndex, u16 KeyType,
4888 u8 *MacAddr, u8 DefaultKey, u32 *KeyContent)
4890 u32 TargetCommand = 0;
4891 u32 TargetContent = 0;
4895 if (EntryNo >= TOTAL_CAM_ENTRY)
4896 RT_TRACE(COMP_ERR, "cam entry exceeds in setKey()\n");
4899 "====>to setKey(), dev:%p, EntryNo:%d, KeyIndex:%d, KeyType:%d, MacAddr%pM\n",
4900 dev, EntryNo, KeyIndex, KeyType, MacAddr);
4903 usConfig |= BIT(15) | (KeyType << 2);
4905 usConfig |= BIT(15) | (KeyType << 2) | KeyIndex;
4907 for (i = 0; i < CAM_CONTENT_COUNT; i++) {
4908 TargetCommand = i + CAM_CONTENT_COUNT * EntryNo;
4909 TargetCommand |= BIT(31) | BIT(16);
4911 if (i == 0) { /* MAC|Config */
4912 TargetContent = (u32)(*(MacAddr + 0)) << 16 |
4913 (u32)(*(MacAddr + 1)) << 24 |
4916 write_nic_dword(dev, WCAMI, TargetContent);
4917 write_nic_dword(dev, RWCAM, TargetCommand);
4918 } else if (i == 1) { /* MAC */
4919 TargetContent = (u32)(*(MacAddr + 2)) |
4920 (u32)(*(MacAddr + 3)) << 8 |
4921 (u32)(*(MacAddr + 4)) << 16 |
4922 (u32)(*(MacAddr + 5)) << 24;
4923 write_nic_dword(dev, WCAMI, TargetContent);
4924 write_nic_dword(dev, RWCAM, TargetCommand);
4928 write_nic_dword(dev, WCAMI,
4929 *(KeyContent + i - 2));
4930 write_nic_dword(dev, RWCAM, TargetCommand);
4936 /***************************************************************************
4937 * ------------------- module init / exit stubs ----------------
4938 ****************************************************************************/
4939 module_init(rtl8192_usb_module_init);
4940 module_exit(rtl8192_usb_module_exit);