staging: rtl8192e: Remove more unused struct members
[deliverable/linux.git] / drivers / staging / rtl8192e / r8192E_core.c
1 /******************************************************************************
2 * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
3 * Linux device driver for RTL8190P / RTL8192E
4 *
5 * Based on the r8180 driver, which is:
6 * Copyright 2004-2005 Andrea Merello <andreamrl@tiscali.it>, et al.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19 *
20 * The full GNU General Public License is included in this distribution in the
21 * file called LICENSE.
22 *
23 * Contact Information:
24 * Jerry chuang <wlanfae@realtek.com>
25 */
26
27
28 #undef RX_DONT_PASS_UL
29 #undef DEBUG_EPROM
30 #undef DEBUG_RX_VERBOSE
31 #undef DUMMY_RX
32 #undef DEBUG_ZERO_RX
33 #undef DEBUG_RX_SKB
34 #undef DEBUG_TX_FRAG
35 #undef DEBUG_RX_FRAG
36 #undef DEBUG_TX_FILLDESC
37 #undef DEBUG_TX
38 #undef DEBUG_IRQ
39 #undef DEBUG_RX
40 #undef DEBUG_RXALLOC
41 #undef DEBUG_REGISTERS
42 #undef DEBUG_RING
43 #undef DEBUG_IRQ_TASKLET
44 #undef DEBUG_TX_ALLOC
45 #undef DEBUG_TX_DESC
46
47 //#define CONFIG_RTL8192_IO_MAP
48 #include <linux/vmalloc.h>
49 #include <linux/slab.h>
50 #include <asm/uaccess.h>
51 #include "r8192E_hw.h"
52 #include "r8192E.h"
53 #include "r8190_rtl8256.h" /* RTL8225 Radio frontend */
54 #include "r8180_93cx6.h" /* Card EEPROM */
55 #include "r8192E_wx.h"
56 #include "r819xE_phy.h" //added by WB 4.30.2008
57 #include "r819xE_phyreg.h"
58 #include "r819xE_cmdpkt.h"
59 #include "r8192E_dm.h"
60
61 #ifdef CONFIG_PM
62 #include "r8192_pm.h"
63 #endif
64
65 #ifdef ENABLE_DOT11D
66 #include "ieee80211/dot11d.h"
67 #endif
68
69 //set here to open your trace code. //WB
70 u32 rt_global_debug_component =
71 // COMP_INIT |
72 // COMP_EPROM |
73 // COMP_PHY |
74 // COMP_RF |
75 // COMP_FIRMWARE |
76 // COMP_TRACE |
77 // COMP_DOWN |
78 // COMP_SWBW |
79 // COMP_SEC |
80 // COMP_QOS |
81 // COMP_RATE |
82 // COMP_RECV |
83 // COMP_SEND |
84 // COMP_POWER |
85 // COMP_EVENTS |
86 // COMP_RESET |
87 // COMP_CMDPKT |
88 // COMP_POWER_TRACKING |
89 // COMP_INTR |
90 COMP_ERR ; //always open err flags on
91
92 static DEFINE_PCI_DEVICE_TABLE(rtl8192_pci_id_tbl) = {
93 #ifdef RTL8190P
94 /* Realtek */
95 /* Dlink */
96 { PCI_DEVICE(0x10ec, 0x8190) },
97 /* Corega */
98 { PCI_DEVICE(0x07aa, 0x0045) },
99 { PCI_DEVICE(0x07aa, 0x0046) },
100 #else
101 /* Realtek */
102 { PCI_DEVICE(0x10ec, 0x8192) },
103
104 /* Corega */
105 { PCI_DEVICE(0x07aa, 0x0044) },
106 { PCI_DEVICE(0x07aa, 0x0047) },
107 #endif
108 {}
109 };
110
111 static char ifname[IFNAMSIZ] = "wlan%d";
112 static int hwwep = 1; //default use hw. set 0 to use software security
113 static int channels = 0x3fff;
114
115 MODULE_LICENSE("GPL");
116 MODULE_VERSION("V 1.1");
117 MODULE_DEVICE_TABLE(pci, rtl8192_pci_id_tbl);
118 //MODULE_AUTHOR("Andrea Merello <andreamrl@tiscali.it>");
119 MODULE_DESCRIPTION("Linux driver for Realtek RTL819x WiFi cards");
120
121
122 module_param_string(ifname, ifname, sizeof(ifname), S_IRUGO|S_IWUSR);
123 module_param(hwwep,int, S_IRUGO|S_IWUSR);
124 module_param(channels,int, S_IRUGO|S_IWUSR);
125
126 MODULE_PARM_DESC(ifname," Net interface name, wlan%d=default");
127 MODULE_PARM_DESC(hwwep," Try to use hardware WEP support. Still broken and not available on all cards");
128 MODULE_PARM_DESC(channels," Channel bitmask for specific locales. NYI");
129
130 static int __devinit rtl8192_pci_probe(struct pci_dev *pdev,
131 const struct pci_device_id *id);
132 static void __devexit rtl8192_pci_disconnect(struct pci_dev *pdev);
133
134 static struct pci_driver rtl8192_pci_driver = {
135 .name = RTL819xE_MODULE_NAME, /* Driver name */
136 .id_table = rtl8192_pci_id_tbl, /* PCI_ID table */
137 .probe = rtl8192_pci_probe, /* probe fn */
138 .remove = __devexit_p(rtl8192_pci_disconnect), /* remove fn */
139 #ifdef CONFIG_PM
140 .suspend = rtl8192E_suspend, /* PM suspend fn */
141 .resume = rtl8192E_resume, /* PM resume fn */
142 #else
143 .suspend = NULL, /* PM suspend fn */
144 .resume = NULL, /* PM resume fn */
145 #endif
146 };
147
148 static void rtl8192_start_beacon(struct net_device *dev);
149 static void rtl8192_stop_beacon(struct net_device *dev);
150 static void rtl819x_watchdog_wqcallback(struct work_struct *work);
151 static void rtl8192_irq_rx_tasklet(struct r8192_priv *priv);
152 static void rtl8192_irq_tx_tasklet(struct r8192_priv *priv);
153 static void rtl8192_prepare_beacon(struct r8192_priv *priv);
154 static irqreturn_t rtl8192_interrupt(int irq, void *netdev);
155 static void rtl819xE_tx_cmd(struct net_device *dev, struct sk_buff *skb);
156 static void rtl8192_update_ratr_table(struct net_device* dev);
157 static void rtl8192_restart(struct work_struct *work);
158 static void watch_dog_timer_callback(unsigned long data);
159 static int _rtl8192_up(struct net_device *dev);
160 static void rtl8192_cancel_deferred_work(struct r8192_priv* priv);
161
162 #ifdef ENABLE_DOT11D
163
164 typedef struct _CHANNEL_LIST
165 {
166 u8 Channel[32];
167 u8 Len;
168 }CHANNEL_LIST, *PCHANNEL_LIST;
169
170 static const CHANNEL_LIST ChannelPlan[] = {
171 {{1,2,3,4,5,6,7,8,9,10,11,36,40,44,48,52,56,60,64,149,153,157,161,165},24}, //FCC
172 {{1,2,3,4,5,6,7,8,9,10,11},11}, //IC
173 {{1,2,3,4,5,6,7,8,9,10,11,12,13,36,40,44,48,52,56,60,64},21}, //ETSI
174 {{1,2,3,4,5,6,7,8,9,10,11,12,13},13}, //Spain. Change to ETSI.
175 {{1,2,3,4,5,6,7,8,9,10,11,12,13},13}, //France. Change to ETSI.
176 {{1,2,3,4,5,6,7,8,9,10,11,12,13,14,36,40,44,48,52,56,60,64},22}, //MKK //MKK
177 {{1,2,3,4,5,6,7,8,9,10,11,12,13,14,36,40,44,48,52,56,60,64},22},//MKK1
178 {{1,2,3,4,5,6,7,8,9,10,11,12,13},13}, //Israel.
179 {{1,2,3,4,5,6,7,8,9,10,11,12,13,14,36,40,44,48,52,56,60,64},22}, // For 11a , TELEC
180 {{1,2,3,4,5,6,7,8,9,10,11,12,13,14,36,40,44,48,52,56,60,64}, 22}, //MIC
181 {{1,2,3,4,5,6,7,8,9,10,11,12,13,14},14} //For Global Domain. 1-11:active scan, 12-14 passive scan. //+YJ, 080626
182 };
183
184 static void rtl819x_set_channel_map(u8 channel_plan, struct r8192_priv* priv)
185 {
186 int i, max_chan=-1, min_chan=-1;
187 struct ieee80211_device* ieee = priv->ieee80211;
188 switch (channel_plan)
189 {
190 case COUNTRY_CODE_FCC:
191 case COUNTRY_CODE_IC:
192 case COUNTRY_CODE_ETSI:
193 case COUNTRY_CODE_SPAIN:
194 case COUNTRY_CODE_FRANCE:
195 case COUNTRY_CODE_MKK:
196 case COUNTRY_CODE_MKK1:
197 case COUNTRY_CODE_ISRAEL:
198 case COUNTRY_CODE_TELEC:
199 case COUNTRY_CODE_MIC:
200 {
201 Dot11d_Init(ieee);
202 ieee->bGlobalDomain = false;
203 //acturally 8225 & 8256 rf chip only support B,G,24N mode
204 if ((priv->rf_chip == RF_8225) || (priv->rf_chip == RF_8256))
205 {
206 min_chan = 1;
207 max_chan = 14;
208 }
209 else
210 {
211 RT_TRACE(COMP_ERR, "unknown rf chip, can't set channel map in function:%s()\n", __FUNCTION__);
212 }
213 if (ChannelPlan[channel_plan].Len != 0){
214 // Clear old channel map
215 memset(GET_DOT11D_INFO(ieee)->channel_map, 0, sizeof(GET_DOT11D_INFO(ieee)->channel_map));
216 // Set new channel map
217 for (i=0;i<ChannelPlan[channel_plan].Len;i++)
218 {
219 if (ChannelPlan[channel_plan].Channel[i] < min_chan || ChannelPlan[channel_plan].Channel[i] > max_chan)
220 break;
221 GET_DOT11D_INFO(ieee)->channel_map[ChannelPlan[channel_plan].Channel[i]] = 1;
222 }
223 }
224 break;
225 }
226 case COUNTRY_CODE_GLOBAL_DOMAIN:
227 {
228 GET_DOT11D_INFO(ieee)->bEnabled = 0; //this flag enabled to follow 11d country IE setting, otherwise, it shall follow global domain setting
229 Dot11d_Reset(ieee);
230 ieee->bGlobalDomain = true;
231 break;
232 }
233 default:
234 break;
235 }
236 }
237 #endif
238
239 static inline bool rx_hal_is_cck_rate(prx_fwinfo_819x_pci pdrvinfo)
240 {
241 return (pdrvinfo->RxRate == DESC90_RATE1M ||
242 pdrvinfo->RxRate == DESC90_RATE2M ||
243 pdrvinfo->RxRate == DESC90_RATE5_5M ||
244 pdrvinfo->RxRate == DESC90_RATE11M) &&
245 !pdrvinfo->RxHT;
246 }
247
248 void CamResetAllEntry(struct net_device *dev)
249 {
250 write_nic_dword(dev, RWCAM, BIT31|BIT30);
251 }
252
253
254 void write_cam(struct net_device *dev, u8 addr, u32 data)
255 {
256 write_nic_dword(dev, WCAMI, data);
257 write_nic_dword(dev, RWCAM, BIT31|BIT16|(addr&0xff) );
258 }
259 u32 read_cam(struct net_device *dev, u8 addr)
260 {
261 write_nic_dword(dev, RWCAM, 0x80000000|(addr&0xff) );
262 return read_nic_dword(dev, 0xa8);
263 }
264
265 #ifdef CONFIG_RTL8180_IO_MAP
266
267 u8 read_nic_byte(struct net_device *dev, int x)
268 {
269 return 0xff&inb(dev->base_addr +x);
270 }
271
272 u32 read_nic_dword(struct net_device *dev, int x)
273 {
274 return inl(dev->base_addr +x);
275 }
276
277 u16 read_nic_word(struct net_device *dev, int x)
278 {
279 return inw(dev->base_addr +x);
280 }
281
282 void write_nic_byte(struct net_device *dev, int x,u8 y)
283 {
284 outb(y&0xff,dev->base_addr +x);
285 }
286
287 void write_nic_word(struct net_device *dev, int x,u16 y)
288 {
289 outw(y,dev->base_addr +x);
290 }
291
292 void write_nic_dword(struct net_device *dev, int x,u32 y)
293 {
294 outl(y,dev->base_addr +x);
295 }
296
297 #else /* RTL_IO_MAP */
298
299 u8 read_nic_byte(struct net_device *dev, int x)
300 {
301 return 0xff&readb((u8*)dev->mem_start +x);
302 }
303
304 u32 read_nic_dword(struct net_device *dev, int x)
305 {
306 return readl((u8*)dev->mem_start +x);
307 }
308
309 u16 read_nic_word(struct net_device *dev, int x)
310 {
311 return readw((u8*)dev->mem_start +x);
312 }
313
314 void write_nic_byte(struct net_device *dev, int x,u8 y)
315 {
316 writeb(y,(u8*)dev->mem_start +x);
317 udelay(20);
318 }
319
320 void write_nic_dword(struct net_device *dev, int x,u32 y)
321 {
322 writel(y,(u8*)dev->mem_start +x);
323 udelay(20);
324 }
325
326 void write_nic_word(struct net_device *dev, int x,u16 y)
327 {
328 writew(y,(u8*)dev->mem_start +x);
329 udelay(20);
330 }
331
332 #endif /* RTL_IO_MAP */
333
334 u8 rtl8192e_ap_sec_type(struct ieee80211_device *ieee)
335 {
336 static const u8 ccmp_ie[4] = {0x00,0x50,0xf2,0x04};
337 static const u8 ccmp_rsn_ie[4] = {0x00, 0x0f, 0xac, 0x04};
338 int wpa_ie_len= ieee->wpa_ie_len;
339 struct ieee80211_crypt_data* crypt;
340 int encrypt;
341
342 crypt = ieee->crypt[ieee->tx_keyidx];
343
344 encrypt = (ieee->current_network.capability & WLAN_CAPABILITY_PRIVACY) ||
345 (ieee->host_encrypt && crypt && crypt->ops &&
346 (0 == strcmp(crypt->ops->name,"WEP")));
347
348 /* simply judge */
349 if(encrypt && (wpa_ie_len == 0)) {
350 // wep encryption, no N mode setting */
351 return SEC_ALG_WEP;
352 } else if((wpa_ie_len != 0)) {
353 // parse pairwise key type */
354 if (((ieee->wpa_ie[0] == 0xdd) && (!memcmp(&(ieee->wpa_ie[14]),ccmp_ie,4))) ||
355 ((ieee->wpa_ie[0] == 0x30) && (!memcmp(&ieee->wpa_ie[10],ccmp_rsn_ie, 4))))
356 return SEC_ALG_CCMP;
357 else
358 return SEC_ALG_TKIP;
359 } else {
360 return SEC_ALG_NONE;
361 }
362 }
363
364 void
365 rtl8192e_SetHwReg(struct net_device *dev,u8 variable,u8* val)
366 {
367 struct r8192_priv* priv = ieee80211_priv(dev);
368
369 switch(variable)
370 {
371
372 case HW_VAR_BSSID:
373 write_nic_dword(dev, BSSIDR, ((u32*)(val))[0]);
374 write_nic_word(dev, BSSIDR+2, ((u16*)(val+2))[0]);
375 break;
376
377 case HW_VAR_MEDIA_STATUS:
378 {
379 RT_OP_MODE OpMode = *((RT_OP_MODE *)(val));
380 u8 btMsr = read_nic_byte(dev, MSR);
381
382 btMsr &= 0xfc;
383
384 switch(OpMode)
385 {
386 case RT_OP_MODE_INFRASTRUCTURE:
387 btMsr |= MSR_INFRA;
388 break;
389
390 case RT_OP_MODE_IBSS:
391 btMsr |= MSR_ADHOC;
392 break;
393
394 case RT_OP_MODE_AP:
395 btMsr |= MSR_AP;
396 break;
397
398 default:
399 btMsr |= MSR_NOLINK;
400 break;
401 }
402
403 write_nic_byte(dev, MSR, btMsr);
404 }
405 break;
406
407 case HW_VAR_CHECK_BSSID:
408 {
409 u32 RegRCR, Type;
410
411 Type = ((u8*)(val))[0];
412 RegRCR = read_nic_dword(dev,RCR);
413 priv->ReceiveConfig = RegRCR;
414
415 if (Type == true)
416 RegRCR |= (RCR_CBSSID);
417 else if (Type == false)
418 RegRCR &= (~RCR_CBSSID);
419
420 write_nic_dword(dev, RCR,RegRCR);
421 priv->ReceiveConfig = RegRCR;
422
423 }
424 break;
425
426 case HW_VAR_SLOT_TIME:
427 {
428 priv->slot_time = val[0];
429 write_nic_byte(dev, SLOT_TIME, val[0]);
430
431 }
432 break;
433
434 case HW_VAR_ACK_PREAMBLE:
435 {
436 u32 regTmp = 0;
437 priv->short_preamble = (bool)(*(u8*)val );
438 regTmp = priv->basic_rate;
439 if (priv->short_preamble)
440 regTmp |= BRSR_AckShortPmb;
441 write_nic_dword(dev, RRSR, regTmp);
442 }
443 break;
444
445 case HW_VAR_CPU_RST:
446 write_nic_dword(dev, CPU_GEN, ((u32*)(val))[0]);
447 break;
448
449 default:
450 break;
451 }
452
453 }
454
455 static struct proc_dir_entry *rtl8192_proc = NULL;
456
457 static int proc_get_stats_ap(char *page, char **start,
458 off_t offset, int count,
459 int *eof, void *data)
460 {
461 struct net_device *dev = data;
462 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
463 struct ieee80211_device *ieee = priv->ieee80211;
464 struct ieee80211_network *target;
465 int len = 0;
466
467 list_for_each_entry(target, &ieee->network_list, list) {
468
469 len += snprintf(page + len, count - len,
470 "%s ", target->ssid);
471
472 if(target->wpa_ie_len>0 || target->rsn_ie_len>0){
473 len += snprintf(page + len, count - len,
474 "WPA\n");
475 }
476 else{
477 len += snprintf(page + len, count - len,
478 "non_WPA\n");
479 }
480
481 }
482
483 *eof = 1;
484 return len;
485 }
486
487 static int proc_get_registers(char *page, char **start,
488 off_t offset, int count,
489 int *eof, void *data)
490 {
491 struct net_device *dev = data;
492 int len = 0;
493 int i,n;
494 int max=0xff;
495
496 /* This dump the current register page */
497 len += snprintf(page + len, count - len,
498 "\n####################page 0##################\n ");
499
500 for(n=0;n<=max;)
501 {
502 len += snprintf(page + len, count - len,
503 "\nD: %2x > ",n);
504
505 for(i=0;i<16 && n<=max;i++,n++)
506 len += snprintf(page + len, count - len,
507 "%2x ",read_nic_byte(dev,n));
508 }
509 len += snprintf(page + len, count - len,"\n");
510 len += snprintf(page + len, count - len,
511 "\n####################page 1##################\n ");
512 for(n=0;n<=max;)
513 {
514 len += snprintf(page + len, count - len,
515 "\nD: %2x > ",n);
516
517 for(i=0;i<16 && n<=max;i++,n++)
518 len += snprintf(page + len, count - len,
519 "%2x ",read_nic_byte(dev,0x100|n));
520 }
521
522 len += snprintf(page + len, count - len,
523 "\n####################page 3##################\n ");
524 for(n=0;n<=max;)
525 {
526 len += snprintf(page + len, count - len,
527 "\nD: %2x > ",n);
528
529 for(i=0;i<16 && n<=max;i++,n++)
530 len += snprintf(page + len, count - len,
531 "%2x ",read_nic_byte(dev,0x300|n));
532 }
533
534 *eof = 1;
535 return len;
536
537 }
538
539 static int proc_get_stats_tx(char *page, char **start,
540 off_t offset, int count,
541 int *eof, void *data)
542 {
543 struct net_device *dev = data;
544 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
545
546 int len = 0;
547
548 len += snprintf(page + len, count - len,
549 "TX VI priority ok int: %lu\n"
550 // "TX VI priority error int: %lu\n"
551 "TX VO priority ok int: %lu\n"
552 // "TX VO priority error int: %lu\n"
553 "TX BE priority ok int: %lu\n"
554 // "TX BE priority error int: %lu\n"
555 "TX BK priority ok int: %lu\n"
556 // "TX BK priority error int: %lu\n"
557 "TX MANAGE priority ok int: %lu\n"
558 // "TX MANAGE priority error int: %lu\n"
559 "TX BEACON priority ok int: %lu\n"
560 "TX BEACON priority error int: %lu\n"
561 "TX CMDPKT priority ok int: %lu\n"
562 // "TX high priority ok int: %lu\n"
563 // "TX high priority failed error int: %lu\n"
564 // "TX queue resume: %lu\n"
565 "TX queue stopped?: %d\n"
566 "TX fifo overflow: %lu\n"
567 // "TX beacon: %lu\n"
568 // "TX VI queue: %d\n"
569 // "TX VO queue: %d\n"
570 // "TX BE queue: %d\n"
571 // "TX BK queue: %d\n"
572 // "TX HW queue: %d\n"
573 // "TX VI dropped: %lu\n"
574 // "TX VO dropped: %lu\n"
575 // "TX BE dropped: %lu\n"
576 // "TX BK dropped: %lu\n"
577 "TX total data packets %lu\n"
578 "TX total data bytes :%lu\n",
579 // "TX beacon aborted: %lu\n",
580 priv->stats.txviokint,
581 // priv->stats.txvierr,
582 priv->stats.txvookint,
583 // priv->stats.txvoerr,
584 priv->stats.txbeokint,
585 // priv->stats.txbeerr,
586 priv->stats.txbkokint,
587 // priv->stats.txbkerr,
588 priv->stats.txmanageokint,
589 // priv->stats.txmanageerr,
590 priv->stats.txbeaconokint,
591 priv->stats.txbeaconerr,
592 priv->stats.txcmdpktokint,
593 // priv->stats.txhpokint,
594 // priv->stats.txhperr,
595 // priv->stats.txresumed,
596 netif_queue_stopped(dev),
597 priv->stats.txoverflow,
598 // priv->stats.txbeacon,
599 // read_nic_byte(dev, TXFIFOCOUNT),
600 // priv->stats.txvidrop,
601 // priv->stats.txvodrop,
602 priv->ieee80211->stats.tx_packets,
603 priv->ieee80211->stats.tx_bytes
604
605
606 // priv->stats.txbedrop,
607 // priv->stats.txbkdrop
608 // priv->stats.txdatapkt
609 // priv->stats.txbeaconerr
610 );
611
612 *eof = 1;
613 return len;
614 }
615
616
617
618 static int proc_get_stats_rx(char *page, char **start,
619 off_t offset, int count,
620 int *eof, void *data)
621 {
622 struct net_device *dev = data;
623 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
624
625 int len = 0;
626
627 len += snprintf(page + len, count - len,
628 "RX packets: %lu\n"
629 "RX desc err: %lu\n"
630 "RX rx overflow error: %lu\n"
631 "RX invalid urb error: %lu\n",
632 priv->stats.rxint,
633 priv->stats.rxrdu,
634 priv->stats.rxoverflow,
635 priv->stats.rxurberr);
636
637 *eof = 1;
638 return len;
639 }
640
641 static void rtl8192_proc_module_init(void)
642 {
643 RT_TRACE(COMP_INIT, "Initializing proc filesystem");
644 rtl8192_proc=create_proc_entry(RTL819xE_MODULE_NAME, S_IFDIR, init_net.proc_net);
645 }
646
647
648 static void rtl8192_proc_module_remove(void)
649 {
650 remove_proc_entry(RTL819xE_MODULE_NAME, init_net.proc_net);
651 }
652
653
654 static void rtl8192_proc_remove_one(struct net_device *dev)
655 {
656 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
657
658 printk("dev name=======> %s\n",dev->name);
659
660 if (priv->dir_dev) {
661 // remove_proc_entry("stats-hw", priv->dir_dev);
662 remove_proc_entry("stats-tx", priv->dir_dev);
663 remove_proc_entry("stats-rx", priv->dir_dev);
664 // remove_proc_entry("stats-ieee", priv->dir_dev);
665 remove_proc_entry("stats-ap", priv->dir_dev);
666 remove_proc_entry("registers", priv->dir_dev);
667 // remove_proc_entry("cck-registers",priv->dir_dev);
668 // remove_proc_entry("ofdm-registers",priv->dir_dev);
669 //remove_proc_entry(dev->name, rtl8192_proc);
670 remove_proc_entry("wlan0", rtl8192_proc);
671 priv->dir_dev = NULL;
672 }
673 }
674
675
676 static void rtl8192_proc_init_one(struct net_device *dev)
677 {
678 struct proc_dir_entry *e;
679 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
680 priv->dir_dev = create_proc_entry(dev->name,
681 S_IFDIR | S_IRUGO | S_IXUGO,
682 rtl8192_proc);
683 if (!priv->dir_dev) {
684 RT_TRACE(COMP_ERR, "Unable to initialize /proc/net/rtl8192/%s\n",
685 dev->name);
686 return;
687 }
688 e = create_proc_read_entry("stats-rx", S_IFREG | S_IRUGO,
689 priv->dir_dev, proc_get_stats_rx, dev);
690
691 if (!e) {
692 RT_TRACE(COMP_ERR,"Unable to initialize "
693 "/proc/net/rtl8192/%s/stats-rx\n",
694 dev->name);
695 }
696
697
698 e = create_proc_read_entry("stats-tx", S_IFREG | S_IRUGO,
699 priv->dir_dev, proc_get_stats_tx, dev);
700
701 if (!e) {
702 RT_TRACE(COMP_ERR, "Unable to initialize "
703 "/proc/net/rtl8192/%s/stats-tx\n",
704 dev->name);
705 }
706
707 e = create_proc_read_entry("stats-ap", S_IFREG | S_IRUGO,
708 priv->dir_dev, proc_get_stats_ap, dev);
709
710 if (!e) {
711 RT_TRACE(COMP_ERR, "Unable to initialize "
712 "/proc/net/rtl8192/%s/stats-ap\n",
713 dev->name);
714 }
715
716 e = create_proc_read_entry("registers", S_IFREG | S_IRUGO,
717 priv->dir_dev, proc_get_registers, dev);
718 if (!e) {
719 RT_TRACE(COMP_ERR, "Unable to initialize "
720 "/proc/net/rtl8192/%s/registers\n",
721 dev->name);
722 }
723 }
724
725 short check_nic_enough_desc(struct net_device *dev, int prio)
726 {
727 struct r8192_priv *priv = ieee80211_priv(dev);
728 struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
729
730 /* for now we reserve two free descriptor as a safety boundary
731 * between the tail and the head
732 */
733 return (ring->entries - skb_queue_len(&ring->queue) >= 2);
734 }
735
736 static void tx_timeout(struct net_device *dev)
737 {
738 struct r8192_priv *priv = ieee80211_priv(dev);
739
740 schedule_work(&priv->reset_wq);
741 printk("TXTIMEOUT");
742 }
743
744 static void rtl8192_irq_enable(struct net_device *dev)
745 {
746 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
747 priv->irq_enabled = 1;
748 write_nic_dword(dev,INTA_MASK, priv->irq_mask);
749 }
750
751 void rtl8192_irq_disable(struct net_device *dev)
752 {
753 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
754
755 write_nic_dword(dev,INTA_MASK,0);
756 priv->irq_enabled = 0;
757 }
758
759 void rtl8192_update_msr(struct net_device *dev)
760 {
761 struct r8192_priv *priv = ieee80211_priv(dev);
762 u8 msr;
763
764 msr = read_nic_byte(dev, MSR);
765 msr &= ~ MSR_LINK_MASK;
766
767 /* do not change in link_state != WLAN_LINK_ASSOCIATED.
768 * msr must be updated if the state is ASSOCIATING.
769 * this is intentional and make sense for ad-hoc and
770 * master (see the create BSS/IBSS func)
771 */
772 if (priv->ieee80211->state == IEEE80211_LINKED){
773
774 if (priv->ieee80211->iw_mode == IW_MODE_INFRA)
775 msr |= (MSR_LINK_MANAGED<<MSR_LINK_SHIFT);
776 else if (priv->ieee80211->iw_mode == IW_MODE_ADHOC)
777 msr |= (MSR_LINK_ADHOC<<MSR_LINK_SHIFT);
778 else if (priv->ieee80211->iw_mode == IW_MODE_MASTER)
779 msr |= (MSR_LINK_MASTER<<MSR_LINK_SHIFT);
780
781 }else
782 msr |= (MSR_LINK_NONE<<MSR_LINK_SHIFT);
783
784 write_nic_byte(dev, MSR, msr);
785 }
786
787 void rtl8192_set_chan(struct net_device *dev,short ch)
788 {
789 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
790
791 priv->chan = ch;
792
793 /* need to implement rf set channel here WB */
794
795 if (priv->rf_set_chan)
796 priv->rf_set_chan(dev, priv->chan);
797 }
798
799 void rtl8192_rx_enable(struct net_device *dev)
800 {
801 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
802
803 write_nic_dword(dev, RDQDA,priv->rx_ring_dma);
804 }
805
806 /* the TX_DESC_BASE setting is according to the following queue index
807 * BK_QUEUE ===> 0
808 * BE_QUEUE ===> 1
809 * VI_QUEUE ===> 2
810 * VO_QUEUE ===> 3
811 * HCCA_QUEUE ===> 4
812 * TXCMD_QUEUE ===> 5
813 * MGNT_QUEUE ===> 6
814 * HIGH_QUEUE ===> 7
815 * BEACON_QUEUE ===> 8
816 * */
817 static const u32 TX_DESC_BASE[] = {BKQDA, BEQDA, VIQDA, VOQDA, HCCAQDA, CQDA, MQDA, HQDA, BQDA};
818 void rtl8192_tx_enable(struct net_device *dev)
819 {
820 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
821 u32 i;
822
823 for (i = 0; i < MAX_TX_QUEUE_COUNT; i++)
824 write_nic_dword(dev, TX_DESC_BASE[i], priv->tx_ring[i].dma);
825
826 ieee80211_reset_queue(priv->ieee80211);
827 }
828
829
830 static void rtl8192_free_rx_ring(struct net_device *dev)
831 {
832 struct r8192_priv *priv = ieee80211_priv(dev);
833 int i;
834
835 for (i = 0; i < priv->rxringcount; i++) {
836 struct sk_buff *skb = priv->rx_buf[i];
837 if (!skb)
838 continue;
839
840 pci_unmap_single(priv->pdev,
841 *((dma_addr_t *)skb->cb),
842 priv->rxbuffersize, PCI_DMA_FROMDEVICE);
843 kfree_skb(skb);
844 }
845
846 pci_free_consistent(priv->pdev, sizeof(*priv->rx_ring) * priv->rxringcount,
847 priv->rx_ring, priv->rx_ring_dma);
848 priv->rx_ring = NULL;
849 }
850
851 static void rtl8192_free_tx_ring(struct net_device *dev, unsigned int prio)
852 {
853 struct r8192_priv *priv = ieee80211_priv(dev);
854 struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
855
856 while (skb_queue_len(&ring->queue)) {
857 tx_desc_819x_pci *entry = &ring->desc[ring->idx];
858 struct sk_buff *skb = __skb_dequeue(&ring->queue);
859
860 pci_unmap_single(priv->pdev, le32_to_cpu(entry->TxBuffAddr),
861 skb->len, PCI_DMA_TODEVICE);
862 kfree_skb(skb);
863 ring->idx = (ring->idx + 1) % ring->entries;
864 }
865
866 pci_free_consistent(priv->pdev, sizeof(*ring->desc)*ring->entries,
867 ring->desc, ring->dma);
868 ring->desc = NULL;
869 }
870
871 void PHY_SetRtl8192eRfOff(struct net_device* dev)
872 {
873 //disable RF-Chip A/B
874 rtl8192_setBBreg(dev, rFPGA0_XA_RFInterfaceOE, BIT4, 0x0);
875 //analog to digital off, for power save
876 rtl8192_setBBreg(dev, rFPGA0_AnalogParameter4, 0x300, 0x0);
877 //digital to analog off, for power save
878 rtl8192_setBBreg(dev, rFPGA0_AnalogParameter1, 0x18, 0x0);
879 //rx antenna off
880 rtl8192_setBBreg(dev, rOFDM0_TRxPathEnable, 0xf, 0x0);
881 //rx antenna off
882 rtl8192_setBBreg(dev, rOFDM1_TRxPathEnable, 0xf, 0x0);
883 //analog to digital part2 off, for power save
884 rtl8192_setBBreg(dev, rFPGA0_AnalogParameter1, 0x60, 0x0);
885 rtl8192_setBBreg(dev, rFPGA0_AnalogParameter1, 0x4, 0x0);
886 // Analog parameter!!Change bias and Lbus control.
887 write_nic_byte(dev, ANAPAR_FOR_8192PciE, 0x07);
888
889 }
890
891 void rtl8192_halt_adapter(struct net_device *dev, bool reset)
892 {
893 struct r8192_priv *priv = ieee80211_priv(dev);
894 int i;
895 u8 OpMode;
896 u32 ulRegRead;
897
898 OpMode = RT_OP_MODE_NO_LINK;
899 priv->ieee80211->SetHwRegHandler(dev, HW_VAR_MEDIA_STATUS, &OpMode);
900
901 if (!priv->ieee80211->bSupportRemoteWakeUp) {
902 /*
903 * disable tx/rx. In 8185 we write 0x10 (Reset bit),
904 * but here we make reference to WMAC and wirte 0x0
905 */
906 write_nic_byte(dev, CMDR, 0);
907 }
908
909 mdelay(20);
910
911 if (!reset) {
912 mdelay(150);
913
914 #ifdef RTL8192E
915 priv->bHwRfOffAction = 2;
916 #endif
917
918 /*
919 * Call MgntActSet_RF_State instead to
920 * prevent RF config race condition.
921 */
922 if (!priv->ieee80211->bSupportRemoteWakeUp) {
923 PHY_SetRtl8192eRfOff(dev);
924 ulRegRead = read_nic_dword(dev,CPU_GEN);
925 ulRegRead |= CPU_GEN_SYSTEM_RESET;
926 write_nic_dword(dev,CPU_GEN, ulRegRead);
927 } else {
928 /* for WOL */
929 write_nic_dword(dev, WFCRC0, 0xffffffff);
930 write_nic_dword(dev, WFCRC1, 0xffffffff);
931 write_nic_dword(dev, WFCRC2, 0xffffffff);
932
933 /* Write PMR register */
934 write_nic_byte(dev, PMR, 0x5);
935 /* Disable tx, enanble rx */
936 write_nic_byte(dev, MacBlkCtrl, 0xa);
937 }
938 }
939
940 for(i = 0; i < MAX_QUEUE_SIZE; i++) {
941 skb_queue_purge(&priv->ieee80211->skb_waitQ [i]);
942 }
943 for(i = 0; i < MAX_QUEUE_SIZE; i++) {
944 skb_queue_purge(&priv->ieee80211->skb_aggQ [i]);
945 }
946
947 skb_queue_purge(&priv->skb_queue);
948 }
949
950 static const u16 rtl_rate[] = {10,20,55,110,60,90,120,180,240,360,480,540};
951 inline u16 rtl8192_rate2rate(short rate)
952 {
953 if (rate >11) return 0;
954 return rtl_rate[rate];
955 }
956
957 static void rtl8192_data_hard_stop(struct net_device *dev)
958 {
959 }
960
961 static void rtl8192_data_hard_resume(struct net_device *dev)
962 {
963 }
964
965 /*
966 * this function TX data frames when the ieee80211 stack requires this.
967 * It checks also if we need to stop the ieee tx queue, eventually do it
968 */
969 static void rtl8192_hard_data_xmit(struct sk_buff *skb, struct net_device *dev, int rate)
970 {
971 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
972 int ret;
973 cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
974 u8 queue_index = tcb_desc->queue_index;
975
976 /* shall not be referred by command packet */
977 BUG_ON(queue_index == TXCMD_QUEUE);
978
979 if (priv->bHwRadioOff || (!priv->up))
980 {
981 kfree_skb(skb);
982 return;
983 }
984
985 memcpy(skb->cb, &dev, sizeof(dev));
986
987 skb_push(skb, priv->ieee80211->tx_headroom);
988 ret = rtl8192_tx(dev, skb);
989 if (ret != 0) {
990 kfree_skb(skb);
991 }
992
993 if (queue_index != MGNT_QUEUE) {
994 priv->ieee80211->stats.tx_bytes += (skb->len - priv->ieee80211->tx_headroom);
995 priv->ieee80211->stats.tx_packets++;
996 }
997 }
998
999 /*
1000 * This is a rough attempt to TX a frame
1001 * This is called by the ieee 80211 stack to TX management frames.
1002 * If the ring is full packet are dropped (for data frame the queue
1003 * is stopped before this can happen).
1004 */
1005 static int rtl8192_hard_start_xmit(struct sk_buff *skb,struct net_device *dev)
1006 {
1007 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
1008 int ret;
1009 cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1010 u8 queue_index = tcb_desc->queue_index;
1011
1012 if (queue_index != TXCMD_QUEUE) {
1013 if (priv->bHwRadioOff || (!priv->up))
1014 {
1015 kfree_skb(skb);
1016 return 0;
1017 }
1018 }
1019
1020 memcpy(skb->cb, &dev, sizeof(dev));
1021 if (queue_index == TXCMD_QUEUE) {
1022 rtl819xE_tx_cmd(dev, skb);
1023 ret = 0;
1024 return ret;
1025 } else {
1026 tcb_desc->RATRIndex = 7;
1027 tcb_desc->bTxDisableRateFallBack = 1;
1028 tcb_desc->bTxUseDriverAssingedRate = 1;
1029 tcb_desc->bTxEnableFwCalcDur = 1;
1030 skb_push(skb, priv->ieee80211->tx_headroom);
1031 ret = rtl8192_tx(dev, skb);
1032 if (ret != 0) {
1033 kfree_skb(skb);
1034 }
1035 }
1036
1037 return ret;
1038 }
1039
1040
1041 static void rtl8192_tx_isr(struct net_device *dev, int prio)
1042 {
1043 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
1044 struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
1045
1046 while (skb_queue_len(&ring->queue)) {
1047 tx_desc_819x_pci *entry = &ring->desc[ring->idx];
1048 struct sk_buff *skb;
1049
1050 /*
1051 * beacon packet will only use the first descriptor defaultly,
1052 * and the OWN may not be cleared by the hardware
1053 */
1054 if (prio != BEACON_QUEUE) {
1055 if (entry->OWN)
1056 return;
1057 ring->idx = (ring->idx + 1) % ring->entries;
1058 }
1059
1060 skb = __skb_dequeue(&ring->queue);
1061 pci_unmap_single(priv->pdev, le32_to_cpu(entry->TxBuffAddr),
1062 skb->len, PCI_DMA_TODEVICE);
1063
1064 kfree_skb(skb);
1065 }
1066 if (prio == MGNT_QUEUE) {
1067 if (priv->ieee80211->ack_tx_to_ieee) {
1068 if (rtl8192_is_tx_queue_empty(dev)) {
1069 priv->ieee80211->ack_tx_to_ieee = 0;
1070 ieee80211_ps_tx_ack(priv->ieee80211, 1);
1071 }
1072 }
1073 }
1074
1075 if (prio != BEACON_QUEUE) {
1076 /* try to deal with the pending packets */
1077 tasklet_schedule(&priv->irq_tx_tasklet);
1078 }
1079 }
1080
1081 static void rtl8192_stop_beacon(struct net_device *dev)
1082 {
1083 }
1084
1085 static void rtl8192_config_rate(struct net_device* dev, u16* rate_config)
1086 {
1087 struct r8192_priv *priv = ieee80211_priv(dev);
1088 struct ieee80211_network *net;
1089 u8 i=0, basic_rate = 0;
1090 net = & priv->ieee80211->current_network;
1091
1092 for (i=0; i<net->rates_len; i++)
1093 {
1094 basic_rate = net->rates[i]&0x7f;
1095 switch(basic_rate)
1096 {
1097 case MGN_1M: *rate_config |= RRSR_1M; break;
1098 case MGN_2M: *rate_config |= RRSR_2M; break;
1099 case MGN_5_5M: *rate_config |= RRSR_5_5M; break;
1100 case MGN_11M: *rate_config |= RRSR_11M; break;
1101 case MGN_6M: *rate_config |= RRSR_6M; break;
1102 case MGN_9M: *rate_config |= RRSR_9M; break;
1103 case MGN_12M: *rate_config |= RRSR_12M; break;
1104 case MGN_18M: *rate_config |= RRSR_18M; break;
1105 case MGN_24M: *rate_config |= RRSR_24M; break;
1106 case MGN_36M: *rate_config |= RRSR_36M; break;
1107 case MGN_48M: *rate_config |= RRSR_48M; break;
1108 case MGN_54M: *rate_config |= RRSR_54M; break;
1109 }
1110 }
1111 for (i=0; i<net->rates_ex_len; i++)
1112 {
1113 basic_rate = net->rates_ex[i]&0x7f;
1114 switch(basic_rate)
1115 {
1116 case MGN_1M: *rate_config |= RRSR_1M; break;
1117 case MGN_2M: *rate_config |= RRSR_2M; break;
1118 case MGN_5_5M: *rate_config |= RRSR_5_5M; break;
1119 case MGN_11M: *rate_config |= RRSR_11M; break;
1120 case MGN_6M: *rate_config |= RRSR_6M; break;
1121 case MGN_9M: *rate_config |= RRSR_9M; break;
1122 case MGN_12M: *rate_config |= RRSR_12M; break;
1123 case MGN_18M: *rate_config |= RRSR_18M; break;
1124 case MGN_24M: *rate_config |= RRSR_24M; break;
1125 case MGN_36M: *rate_config |= RRSR_36M; break;
1126 case MGN_48M: *rate_config |= RRSR_48M; break;
1127 case MGN_54M: *rate_config |= RRSR_54M; break;
1128 }
1129 }
1130 }
1131
1132
1133 #define SHORT_SLOT_TIME 9
1134 #define NON_SHORT_SLOT_TIME 20
1135
1136 static void rtl8192_update_cap(struct net_device* dev, u16 cap)
1137 {
1138 u32 tmp = 0;
1139 struct r8192_priv *priv = ieee80211_priv(dev);
1140 struct ieee80211_network *net = &priv->ieee80211->current_network;
1141 priv->short_preamble = cap & WLAN_CAPABILITY_SHORT_PREAMBLE;
1142 tmp = priv->basic_rate;
1143 if (priv->short_preamble)
1144 tmp |= BRSR_AckShortPmb;
1145 write_nic_dword(dev, RRSR, tmp);
1146
1147 if (net->mode & (IEEE_G|IEEE_N_24G))
1148 {
1149 u8 slot_time = 0;
1150 if ((cap & WLAN_CAPABILITY_SHORT_SLOT)&&(!priv->ieee80211->pHTInfo->bCurrentRT2RTLongSlotTime))
1151 {//short slot time
1152 slot_time = SHORT_SLOT_TIME;
1153 }
1154 else //long slot time
1155 slot_time = NON_SHORT_SLOT_TIME;
1156 priv->slot_time = slot_time;
1157 write_nic_byte(dev, SLOT_TIME, slot_time);
1158 }
1159
1160 }
1161
1162 static void rtl8192_net_update(struct net_device *dev)
1163 {
1164 struct r8192_priv *priv = ieee80211_priv(dev);
1165 struct ieee80211_network *net;
1166 u16 BcnTimeCfg = 0, BcnCW = 6, BcnIFS = 0xf;
1167 u16 rate_config = 0;
1168 net = &priv->ieee80211->current_network;
1169
1170 /* update Basic rate: RR, BRSR */
1171 rtl8192_config_rate(dev, &rate_config);
1172
1173 /*
1174 * Select RRSR (in Legacy-OFDM and CCK)
1175 * For 8190, we select only 24M, 12M, 6M, 11M, 5.5M,
1176 * 2M, and 1M from the Basic rate.
1177 * We do not use other rates.
1178 */
1179 priv->basic_rate = rate_config &= 0x15f;
1180
1181 /* BSSID */
1182 write_nic_dword(dev, BSSIDR, ((u32 *)net->bssid)[0]);
1183 write_nic_word(dev, BSSIDR+4, ((u16 *)net->bssid)[2]);
1184
1185 if (priv->ieee80211->iw_mode == IW_MODE_ADHOC)
1186 {
1187 write_nic_word(dev, ATIMWND, 2);
1188 write_nic_word(dev, BCN_DMATIME, 256);
1189 write_nic_word(dev, BCN_INTERVAL, net->beacon_interval);
1190 /*
1191 * BIT15 of BCN_DRV_EARLY_INT will indicate
1192 * whether software beacon or hw beacon is applied.
1193 */
1194 write_nic_word(dev, BCN_DRV_EARLY_INT, 10);
1195 write_nic_byte(dev, BCN_ERR_THRESH, 100);
1196
1197 BcnTimeCfg |= (BcnCW<<BCN_TCFG_CW_SHIFT);
1198 /* TODO: BcnIFS may required to be changed on ASIC */
1199 BcnTimeCfg |= BcnIFS<<BCN_TCFG_IFS;
1200 write_nic_word(dev, BCN_TCFG, BcnTimeCfg);
1201 }
1202 }
1203
1204 void rtl819xE_tx_cmd(struct net_device *dev, struct sk_buff *skb)
1205 {
1206 struct r8192_priv *priv = ieee80211_priv(dev);
1207 struct rtl8192_tx_ring *ring;
1208 tx_desc_819x_pci *entry;
1209 unsigned int idx;
1210 dma_addr_t mapping;
1211 cb_desc *tcb_desc;
1212 unsigned long flags;
1213
1214 ring = &priv->tx_ring[TXCMD_QUEUE];
1215 mapping = pci_map_single(priv->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
1216
1217 spin_lock_irqsave(&priv->irq_th_lock,flags);
1218 idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
1219 entry = &ring->desc[idx];
1220
1221 tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1222 memset(entry,0,12);
1223 entry->LINIP = tcb_desc->bLastIniPkt;
1224 entry->FirstSeg = 1;//first segment
1225 entry->LastSeg = 1; //last segment
1226 if(tcb_desc->bCmdOrInit == DESC_PACKET_TYPE_INIT) {
1227 entry->CmdInit = DESC_PACKET_TYPE_INIT;
1228 } else {
1229 entry->CmdInit = DESC_PACKET_TYPE_NORMAL;
1230 entry->Offset = sizeof(TX_FWINFO_8190PCI) + 8;
1231 entry->PktSize = (u16)(tcb_desc->pkt_size + entry->Offset);
1232 entry->QueueSelect = QSLT_CMD;
1233 entry->TxFWInfoSize = 0x08;
1234 entry->RATid = (u8)DESC_PACKET_TYPE_INIT;
1235 }
1236 entry->TxBufferSize = skb->len;
1237 entry->TxBuffAddr = cpu_to_le32(mapping);
1238 entry->OWN = 1;
1239
1240 #ifdef JOHN_DUMP_TXDESC
1241 { int i;
1242 tx_desc_819x_pci *entry1 = &ring->desc[0];
1243 unsigned int *ptr= (unsigned int *)entry1;
1244 printk("<Tx descriptor>:\n");
1245 for (i = 0; i < 8; i++)
1246 printk("%8x ", ptr[i]);
1247 printk("\n");
1248 }
1249 #endif
1250 __skb_queue_tail(&ring->queue, skb);
1251 spin_unlock_irqrestore(&priv->irq_th_lock,flags);
1252
1253 write_nic_byte(dev, TPPoll, TPPoll_CQ);
1254
1255 return;
1256 }
1257
1258 /*
1259 * Mapping Software/Hardware descriptor queue id to "Queue Select Field"
1260 * in TxFwInfo data structure
1261 */
1262 static u8 MapHwQueueToFirmwareQueue(u8 QueueID)
1263 {
1264 u8 QueueSelect = 0;
1265
1266 switch (QueueID) {
1267 case BE_QUEUE:
1268 QueueSelect = QSLT_BE;
1269 break;
1270
1271 case BK_QUEUE:
1272 QueueSelect = QSLT_BK;
1273 break;
1274
1275 case VO_QUEUE:
1276 QueueSelect = QSLT_VO;
1277 break;
1278
1279 case VI_QUEUE:
1280 QueueSelect = QSLT_VI;
1281 break;
1282
1283 case MGNT_QUEUE:
1284 QueueSelect = QSLT_MGNT;
1285 break;
1286
1287 case BEACON_QUEUE:
1288 QueueSelect = QSLT_BEACON;
1289 break;
1290
1291 case TXCMD_QUEUE:
1292 QueueSelect = QSLT_CMD;
1293 break;
1294
1295 case HIGH_QUEUE:
1296 default:
1297 RT_TRACE(COMP_ERR, "Impossible Queue Selection: %d\n", QueueID);
1298 break;
1299 }
1300 return QueueSelect;
1301 }
1302
1303 static u8 MRateToHwRate8190Pci(u8 rate)
1304 {
1305 u8 ret = DESC90_RATE1M;
1306
1307 switch(rate) {
1308 case MGN_1M: ret = DESC90_RATE1M; break;
1309 case MGN_2M: ret = DESC90_RATE2M; break;
1310 case MGN_5_5M: ret = DESC90_RATE5_5M; break;
1311 case MGN_11M: ret = DESC90_RATE11M; break;
1312 case MGN_6M: ret = DESC90_RATE6M; break;
1313 case MGN_9M: ret = DESC90_RATE9M; break;
1314 case MGN_12M: ret = DESC90_RATE12M; break;
1315 case MGN_18M: ret = DESC90_RATE18M; break;
1316 case MGN_24M: ret = DESC90_RATE24M; break;
1317 case MGN_36M: ret = DESC90_RATE36M; break;
1318 case MGN_48M: ret = DESC90_RATE48M; break;
1319 case MGN_54M: ret = DESC90_RATE54M; break;
1320
1321 // HT rate since here
1322 case MGN_MCS0: ret = DESC90_RATEMCS0; break;
1323 case MGN_MCS1: ret = DESC90_RATEMCS1; break;
1324 case MGN_MCS2: ret = DESC90_RATEMCS2; break;
1325 case MGN_MCS3: ret = DESC90_RATEMCS3; break;
1326 case MGN_MCS4: ret = DESC90_RATEMCS4; break;
1327 case MGN_MCS5: ret = DESC90_RATEMCS5; break;
1328 case MGN_MCS6: ret = DESC90_RATEMCS6; break;
1329 case MGN_MCS7: ret = DESC90_RATEMCS7; break;
1330 case MGN_MCS8: ret = DESC90_RATEMCS8; break;
1331 case MGN_MCS9: ret = DESC90_RATEMCS9; break;
1332 case MGN_MCS10: ret = DESC90_RATEMCS10; break;
1333 case MGN_MCS11: ret = DESC90_RATEMCS11; break;
1334 case MGN_MCS12: ret = DESC90_RATEMCS12; break;
1335 case MGN_MCS13: ret = DESC90_RATEMCS13; break;
1336 case MGN_MCS14: ret = DESC90_RATEMCS14; break;
1337 case MGN_MCS15: ret = DESC90_RATEMCS15; break;
1338 case (0x80|0x20): ret = DESC90_RATEMCS32; break;
1339
1340 default: break;
1341 }
1342 return ret;
1343 }
1344
1345
1346 static u8 QueryIsShort(u8 TxHT, u8 TxRate, cb_desc *tcb_desc)
1347 {
1348 u8 tmp_Short;
1349
1350 tmp_Short = (TxHT==1)?((tcb_desc->bUseShortGI)?1:0):((tcb_desc->bUseShortPreamble)?1:0);
1351
1352 if(TxHT==1 && TxRate != DESC90_RATEMCS15)
1353 tmp_Short = 0;
1354
1355 return tmp_Short;
1356 }
1357
1358 /*
1359 * The tx procedure is just as following,
1360 * skb->cb will contain all the following information,
1361 * priority, morefrag, rate, &dev.
1362 */
1363 short rtl8192_tx(struct net_device *dev, struct sk_buff* skb)
1364 {
1365 struct r8192_priv *priv = ieee80211_priv(dev);
1366 struct rtl8192_tx_ring *ring;
1367 unsigned long flags;
1368 cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1369 tx_desc_819x_pci *pdesc = NULL;
1370 TX_FWINFO_8190PCI *pTxFwInfo = NULL;
1371 dma_addr_t mapping;
1372 bool multi_addr = false, broad_addr = false, uni_addr = false;
1373 u8 *pda_addr = NULL;
1374 int idx;
1375
1376 if (priv->bdisable_nic) {
1377 RT_TRACE(COMP_ERR, "Nic is disabled! Can't tx packet len=%d qidx=%d!!!\n",
1378 skb->len, tcb_desc->queue_index);
1379 return skb->len;
1380 }
1381
1382 #ifdef ENABLE_LPS
1383 priv->ieee80211->bAwakePktSent = true;
1384 #endif
1385
1386 mapping = pci_map_single(priv->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
1387
1388 /* collect the tx packets statitcs */
1389 pda_addr = ((u8 *)skb->data) + sizeof(TX_FWINFO_8190PCI);
1390 if (is_multicast_ether_addr(pda_addr))
1391 multi_addr = true;
1392 else if (is_broadcast_ether_addr(pda_addr))
1393 broad_addr = true;
1394 else
1395 uni_addr = true;
1396
1397 if (uni_addr)
1398 priv->stats.txbytesunicast += (u8)(skb->len) - sizeof(TX_FWINFO_8190PCI);
1399 else if (multi_addr)
1400 priv->stats.txbytesmulticast += (u8)(skb->len) - sizeof(TX_FWINFO_8190PCI);
1401 else
1402 priv->stats.txbytesbroadcast += (u8)(skb->len) - sizeof(TX_FWINFO_8190PCI);
1403
1404 /* fill tx firmware */
1405 pTxFwInfo = (PTX_FWINFO_8190PCI)skb->data;
1406 memset(pTxFwInfo, 0, sizeof(TX_FWINFO_8190PCI));
1407 pTxFwInfo->TxHT = (tcb_desc->data_rate&0x80) ? 1 : 0;
1408 pTxFwInfo->TxRate = MRateToHwRate8190Pci((u8)tcb_desc->data_rate);
1409 pTxFwInfo->EnableCPUDur = tcb_desc->bTxEnableFwCalcDur;
1410 pTxFwInfo->Short = QueryIsShort(pTxFwInfo->TxHT, pTxFwInfo->TxRate, tcb_desc);
1411
1412 /* Aggregation related */
1413 if (tcb_desc->bAMPDUEnable) {
1414 pTxFwInfo->AllowAggregation = 1;
1415 pTxFwInfo->RxMF = tcb_desc->ampdu_factor;
1416 pTxFwInfo->RxAMD = tcb_desc->ampdu_density;
1417 } else {
1418 pTxFwInfo->AllowAggregation = 0;
1419 pTxFwInfo->RxMF = 0;
1420 pTxFwInfo->RxAMD = 0;
1421 }
1422
1423 /* Protection mode related */
1424 pTxFwInfo->RtsEnable = (tcb_desc->bRTSEnable) ? 1 : 0;
1425 pTxFwInfo->CtsEnable = (tcb_desc->bCTSEnable) ? 1 : 0;
1426 pTxFwInfo->RtsSTBC = (tcb_desc->bRTSSTBC) ? 1 : 0;
1427 pTxFwInfo->RtsHT = (tcb_desc->rts_rate&0x80) ? 1 : 0;
1428 pTxFwInfo->RtsRate = MRateToHwRate8190Pci((u8)tcb_desc->rts_rate);
1429 pTxFwInfo->RtsBandwidth = 0;
1430 pTxFwInfo->RtsSubcarrier = tcb_desc->RTSSC;
1431 pTxFwInfo->RtsShort = (pTxFwInfo->RtsHT == 0) ? (tcb_desc->bRTSUseShortPreamble ? 1 : 0) : (tcb_desc->bRTSUseShortGI? 1 : 0);
1432
1433 /* Set Bandwidth and sub-channel settings. */
1434 if (priv->CurrentChannelBW == HT_CHANNEL_WIDTH_20_40) {
1435 if (tcb_desc->bPacketBW) {
1436 pTxFwInfo->TxBandwidth = 1;
1437 #ifdef RTL8190P
1438 pTxFwInfo->TxSubCarrier = 3;
1439 #else
1440 /* use duplicated mode */
1441 pTxFwInfo->TxSubCarrier = 0;
1442 #endif
1443 } else {
1444 pTxFwInfo->TxBandwidth = 0;
1445 pTxFwInfo->TxSubCarrier = priv->nCur40MhzPrimeSC;
1446 }
1447 } else {
1448 pTxFwInfo->TxBandwidth = 0;
1449 pTxFwInfo->TxSubCarrier = 0;
1450 }
1451
1452 spin_lock_irqsave(&priv->irq_th_lock, flags);
1453 ring = &priv->tx_ring[tcb_desc->queue_index];
1454 if (tcb_desc->queue_index != BEACON_QUEUE)
1455 idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
1456 else
1457 idx = 0;
1458
1459 pdesc = &ring->desc[idx];
1460 if ((pdesc->OWN == 1) && (tcb_desc->queue_index != BEACON_QUEUE)) {
1461 RT_TRACE(COMP_ERR, "No more TX desc@%d, ring->idx = %d,idx = %d,%x",
1462 tcb_desc->queue_index, ring->idx, idx, skb->len);
1463 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1464 return skb->len;
1465 }
1466
1467 /* fill tx descriptor */
1468 memset(pdesc, 0, 12);
1469
1470 /*DWORD 0*/
1471 pdesc->LINIP = 0;
1472 pdesc->CmdInit = 1;
1473 pdesc->Offset = sizeof(TX_FWINFO_8190PCI) + 8; /* We must add 8!! */
1474 pdesc->PktSize = (u16)skb->len-sizeof(TX_FWINFO_8190PCI);
1475
1476 /*DWORD 1*/
1477 pdesc->SecCAMID = 0;
1478 pdesc->RATid = tcb_desc->RATRIndex;
1479
1480 pdesc->NoEnc = 1;
1481 pdesc->SecType = 0x0;
1482 if (tcb_desc->bHwSec) {
1483 switch (priv->ieee80211->pairwise_key_type) {
1484 case KEY_TYPE_WEP40:
1485 case KEY_TYPE_WEP104:
1486 pdesc->SecType = 0x1;
1487 pdesc->NoEnc = 0;
1488 break;
1489 case KEY_TYPE_TKIP:
1490 pdesc->SecType = 0x2;
1491 pdesc->NoEnc = 0;
1492 break;
1493 case KEY_TYPE_CCMP:
1494 pdesc->SecType = 0x3;
1495 pdesc->NoEnc = 0;
1496 break;
1497 case KEY_TYPE_NA:
1498 pdesc->SecType = 0x0;
1499 pdesc->NoEnc = 1;
1500 break;
1501 }
1502 }
1503
1504 /* Set Packet ID */
1505 pdesc->PktId = 0x0;
1506
1507 pdesc->QueueSelect = MapHwQueueToFirmwareQueue(tcb_desc->queue_index);
1508 pdesc->TxFWInfoSize = sizeof(TX_FWINFO_8190PCI);
1509
1510 pdesc->DISFB = tcb_desc->bTxDisableRateFallBack;
1511 pdesc->USERATE = tcb_desc->bTxUseDriverAssingedRate;
1512
1513 pdesc->FirstSeg = 1;
1514 pdesc->LastSeg = 1;
1515 pdesc->TxBufferSize = skb->len;
1516
1517 pdesc->TxBuffAddr = cpu_to_le32(mapping);
1518 __skb_queue_tail(&ring->queue, skb);
1519 pdesc->OWN = 1;
1520 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1521 dev->trans_start = jiffies;
1522 write_nic_word(dev, TPPoll, 0x01<<tcb_desc->queue_index);
1523 return 0;
1524 }
1525
1526 static short rtl8192_alloc_rx_desc_ring(struct net_device *dev)
1527 {
1528 struct r8192_priv *priv = ieee80211_priv(dev);
1529 rx_desc_819x_pci *entry = NULL;
1530 int i;
1531
1532 priv->rx_ring = pci_alloc_consistent(priv->pdev,
1533 sizeof(*priv->rx_ring) * priv->rxringcount, &priv->rx_ring_dma);
1534
1535 if (!priv->rx_ring || (unsigned long)priv->rx_ring & 0xFF) {
1536 RT_TRACE(COMP_ERR,"Cannot allocate RX ring\n");
1537 return -ENOMEM;
1538 }
1539
1540 memset(priv->rx_ring, 0, sizeof(*priv->rx_ring) * priv->rxringcount);
1541 priv->rx_idx = 0;
1542
1543 for (i = 0; i < priv->rxringcount; i++) {
1544 struct sk_buff *skb = dev_alloc_skb(priv->rxbuffersize);
1545 dma_addr_t *mapping;
1546 entry = &priv->rx_ring[i];
1547 if (!skb)
1548 return 0;
1549 priv->rx_buf[i] = skb;
1550 mapping = (dma_addr_t *)skb->cb;
1551 *mapping = pci_map_single(priv->pdev, skb_tail_pointer(skb),
1552 priv->rxbuffersize, PCI_DMA_FROMDEVICE);
1553
1554 entry->BufferAddress = cpu_to_le32(*mapping);
1555
1556 entry->Length = priv->rxbuffersize;
1557 entry->OWN = 1;
1558 }
1559
1560 entry->EOR = 1;
1561 return 0;
1562 }
1563
1564 static int rtl8192_alloc_tx_desc_ring(struct net_device *dev,
1565 unsigned int prio, unsigned int entries)
1566 {
1567 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
1568 tx_desc_819x_pci *ring;
1569 dma_addr_t dma;
1570 int i;
1571
1572 ring = pci_alloc_consistent(priv->pdev, sizeof(*ring) * entries, &dma);
1573 if (!ring || (unsigned long)ring & 0xFF) {
1574 RT_TRACE(COMP_ERR, "Cannot allocate TX ring (prio = %d)\n", prio);
1575 return -ENOMEM;
1576 }
1577
1578 memset(ring, 0, sizeof(*ring)*entries);
1579 priv->tx_ring[prio].desc = ring;
1580 priv->tx_ring[prio].dma = dma;
1581 priv->tx_ring[prio].idx = 0;
1582 priv->tx_ring[prio].entries = entries;
1583 skb_queue_head_init(&priv->tx_ring[prio].queue);
1584
1585 for (i = 0; i < entries; i++)
1586 ring[i].NextDescAddress =
1587 cpu_to_le32((u32)dma + ((i + 1) % entries) * sizeof(*ring));
1588
1589 return 0;
1590 }
1591
1592 static short rtl8192_pci_initdescring(struct net_device *dev)
1593 {
1594 u32 ret;
1595 int i;
1596 struct r8192_priv *priv = ieee80211_priv(dev);
1597
1598 ret = rtl8192_alloc_rx_desc_ring(dev);
1599 if (ret)
1600 return ret;
1601
1602 /* general process for other queue */
1603 for (i = 0; i < MAX_TX_QUEUE_COUNT; i++) {
1604 ret = rtl8192_alloc_tx_desc_ring(dev, i, priv->txringcount);
1605 if (ret)
1606 goto err_free_rings;
1607 }
1608
1609 return 0;
1610
1611 err_free_rings:
1612 rtl8192_free_rx_ring(dev);
1613 for (i = 0; i < MAX_TX_QUEUE_COUNT; i++)
1614 if (priv->tx_ring[i].desc)
1615 rtl8192_free_tx_ring(dev, i);
1616 return 1;
1617 }
1618
1619 static void rtl8192_pci_resetdescring(struct net_device *dev)
1620 {
1621 struct r8192_priv *priv = ieee80211_priv(dev);
1622 int i;
1623
1624 /* force the rx_idx to the first one */
1625 if(priv->rx_ring) {
1626 rx_desc_819x_pci *entry = NULL;
1627 for (i = 0; i < priv->rxringcount; i++) {
1628 entry = &priv->rx_ring[i];
1629 entry->OWN = 1;
1630 }
1631 priv->rx_idx = 0;
1632 }
1633
1634 /* after reset, release previous pending packet, and force the
1635 * tx idx to the first one */
1636 for (i = 0; i < MAX_TX_QUEUE_COUNT; i++) {
1637 if (priv->tx_ring[i].desc) {
1638 struct rtl8192_tx_ring *ring = &priv->tx_ring[i];
1639
1640 while (skb_queue_len(&ring->queue)) {
1641 tx_desc_819x_pci *entry = &ring->desc[ring->idx];
1642 struct sk_buff *skb = __skb_dequeue(&ring->queue);
1643
1644 pci_unmap_single(priv->pdev, le32_to_cpu(entry->TxBuffAddr),
1645 skb->len, PCI_DMA_TODEVICE);
1646 kfree_skb(skb);
1647 ring->idx = (ring->idx + 1) % ring->entries;
1648 }
1649 ring->idx = 0;
1650 }
1651 }
1652 }
1653
1654 static void rtl8192_link_change(struct net_device *dev)
1655 {
1656 struct r8192_priv *priv = ieee80211_priv(dev);
1657 struct ieee80211_device* ieee = priv->ieee80211;
1658 //write_nic_word(dev, BCN_INTR_ITV, net->beacon_interval);
1659 if (ieee->state == IEEE80211_LINKED)
1660 {
1661 rtl8192_net_update(dev);
1662 rtl8192_update_ratr_table(dev);
1663 #if 1
1664 //add this as in pure N mode, wep encryption will use software way, but there is no chance to set this as wep will not set group key in wext. WB.2008.07.08
1665 if ((KEY_TYPE_WEP40 == ieee->pairwise_key_type) || (KEY_TYPE_WEP104 == ieee->pairwise_key_type))
1666 EnableHWSecurityConfig8192(dev);
1667 #endif
1668 }
1669 else
1670 {
1671 write_nic_byte(dev, 0x173, 0);
1672 }
1673 /*update timing params*/
1674 //rtl8192_set_chan(dev, priv->chan);
1675 //MSR
1676 rtl8192_update_msr(dev);
1677
1678 // 2007/10/16 MH MAC Will update TSF according to all received beacon, so we have
1679 // // To set CBSSID bit when link with any AP or STA.
1680 if (ieee->iw_mode == IW_MODE_INFRA || ieee->iw_mode == IW_MODE_ADHOC)
1681 {
1682 u32 reg = 0;
1683 reg = read_nic_dword(dev, RCR);
1684 if (priv->ieee80211->state == IEEE80211_LINKED)
1685 priv->ReceiveConfig = reg |= RCR_CBSSID;
1686 else
1687 priv->ReceiveConfig = reg &= ~RCR_CBSSID;
1688 write_nic_dword(dev, RCR, reg);
1689 }
1690 }
1691
1692
1693 static const struct ieee80211_qos_parameters def_qos_parameters = {
1694 {3,3,3,3},/* cw_min */
1695 {7,7,7,7},/* cw_max */
1696 {2,2,2,2},/* aifs */
1697 {0,0,0,0},/* flags */
1698 {0,0,0,0} /* tx_op_limit */
1699 };
1700
1701 static void rtl8192_update_beacon(struct work_struct * work)
1702 {
1703 struct r8192_priv *priv = container_of(work, struct r8192_priv, update_beacon_wq.work);
1704 struct net_device *dev = priv->ieee80211->dev;
1705 struct ieee80211_device* ieee = priv->ieee80211;
1706 struct ieee80211_network* net = &ieee->current_network;
1707
1708 if (ieee->pHTInfo->bCurrentHTSupport)
1709 HTUpdateSelfAndPeerSetting(ieee, net);
1710 ieee->pHTInfo->bCurrentRT2RTLongSlotTime = net->bssht.bdRT2RTLongSlotTime;
1711 rtl8192_update_cap(dev, net->capability);
1712 }
1713
1714 /*
1715 * background support to run QoS activate functionality
1716 */
1717 static const int WDCAPARA_ADD[] = {EDCAPARA_BE,EDCAPARA_BK,EDCAPARA_VI,EDCAPARA_VO};
1718 static void rtl8192_qos_activate(struct work_struct * work)
1719 {
1720 struct r8192_priv *priv = container_of(work, struct r8192_priv, qos_activate);
1721 struct net_device *dev = priv->ieee80211->dev;
1722 struct ieee80211_qos_parameters *qos_parameters = &priv->ieee80211->current_network.qos_data.parameters;
1723 u8 mode = priv->ieee80211->current_network.mode;
1724 u8 u1bAIFS;
1725 u32 u4bAcParam;
1726 int i;
1727
1728 mutex_lock(&priv->mutex);
1729 if(priv->ieee80211->state != IEEE80211_LINKED)
1730 goto success;
1731 RT_TRACE(COMP_QOS,"qos active process with associate response received\n");
1732 /* It better set slot time at first */
1733 /* For we just support b/g mode at present, let the slot time at 9/20 selection */
1734 /* update the ac parameter to related registers */
1735 for(i = 0; i < QOS_QUEUE_NUM; i++) {
1736 //Mode G/A: slotTimeTimer = 9; Mode B: 20
1737 u1bAIFS = qos_parameters->aifs[i] * ((mode&(IEEE_G|IEEE_N_24G)) ?9:20) + aSifsTime;
1738 u4bAcParam = ((((u32)(qos_parameters->tx_op_limit[i]))<< AC_PARAM_TXOP_LIMIT_OFFSET)|
1739 (((u32)(qos_parameters->cw_max[i]))<< AC_PARAM_ECW_MAX_OFFSET)|
1740 (((u32)(qos_parameters->cw_min[i]))<< AC_PARAM_ECW_MIN_OFFSET)|
1741 ((u32)u1bAIFS << AC_PARAM_AIFS_OFFSET));
1742 //printk("===>u4bAcParam:%x, ", u4bAcParam);
1743 write_nic_dword(dev, WDCAPARA_ADD[i], u4bAcParam);
1744 //write_nic_dword(dev, WDCAPARA_ADD[i], 0x005e4332);
1745 }
1746
1747 success:
1748 mutex_unlock(&priv->mutex);
1749 }
1750
1751 static int rtl8192_qos_handle_probe_response(struct r8192_priv *priv,
1752 int active_network,
1753 struct ieee80211_network *network)
1754 {
1755 int ret = 0;
1756 u32 size = sizeof(struct ieee80211_qos_parameters);
1757
1758 if(priv->ieee80211->state !=IEEE80211_LINKED)
1759 return ret;
1760
1761 if ((priv->ieee80211->iw_mode != IW_MODE_INFRA))
1762 return ret;
1763
1764 if (network->flags & NETWORK_HAS_QOS_MASK) {
1765 if (active_network &&
1766 (network->flags & NETWORK_HAS_QOS_PARAMETERS))
1767 network->qos_data.active = network->qos_data.supported;
1768
1769 if ((network->qos_data.active == 1) && (active_network == 1) &&
1770 (network->flags & NETWORK_HAS_QOS_PARAMETERS) &&
1771 (network->qos_data.old_param_count !=
1772 network->qos_data.param_count)) {
1773 network->qos_data.old_param_count =
1774 network->qos_data.param_count;
1775 queue_work(priv->priv_wq, &priv->qos_activate);
1776 RT_TRACE (COMP_QOS, "QoS parameters change call "
1777 "qos_activate\n");
1778 }
1779 } else {
1780 memcpy(&priv->ieee80211->current_network.qos_data.parameters,
1781 &def_qos_parameters, size);
1782
1783 if ((network->qos_data.active == 1) && (active_network == 1)) {
1784 queue_work(priv->priv_wq, &priv->qos_activate);
1785 RT_TRACE(COMP_QOS, "QoS was disabled call qos_activate \n");
1786 }
1787 network->qos_data.active = 0;
1788 network->qos_data.supported = 0;
1789 }
1790
1791 return 0;
1792 }
1793
1794 /* handle manage frame frame beacon and probe response */
1795 static int rtl8192_handle_beacon(struct net_device * dev,
1796 struct ieee80211_beacon * beacon,
1797 struct ieee80211_network * network)
1798 {
1799 struct r8192_priv *priv = ieee80211_priv(dev);
1800
1801 rtl8192_qos_handle_probe_response(priv,1,network);
1802
1803 queue_delayed_work(priv->priv_wq, &priv->update_beacon_wq, 0);
1804 return 0;
1805
1806 }
1807
1808 /*
1809 * handling the beaconing responses. if we get different QoS setting
1810 * off the network from the associated setting, adjust the QoS setting
1811 */
1812 static int rtl8192_qos_association_resp(struct r8192_priv *priv,
1813 struct ieee80211_network *network)
1814 {
1815 int ret = 0;
1816 unsigned long flags;
1817 u32 size = sizeof(struct ieee80211_qos_parameters);
1818 int set_qos_param = 0;
1819
1820 if ((priv == NULL) || (network == NULL))
1821 return ret;
1822
1823 if (priv->ieee80211->state != IEEE80211_LINKED)
1824 return ret;
1825
1826 if ((priv->ieee80211->iw_mode != IW_MODE_INFRA))
1827 return ret;
1828
1829 spin_lock_irqsave(&priv->ieee80211->lock, flags);
1830 if (network->flags & NETWORK_HAS_QOS_PARAMETERS) {
1831 memcpy(&priv->ieee80211->current_network.qos_data.parameters,
1832 &network->qos_data.parameters,
1833 sizeof(struct ieee80211_qos_parameters));
1834 priv->ieee80211->current_network.qos_data.active = 1;
1835 set_qos_param = 1;
1836 /* update qos parameter for current network */
1837 priv->ieee80211->current_network.qos_data.old_param_count =
1838 priv->ieee80211->current_network.qos_data.param_count;
1839 priv->ieee80211->current_network.qos_data.param_count =
1840 network->qos_data.param_count;
1841
1842 } else {
1843 memcpy(&priv->ieee80211->current_network.qos_data.parameters,
1844 &def_qos_parameters, size);
1845 priv->ieee80211->current_network.qos_data.active = 0;
1846 priv->ieee80211->current_network.qos_data.supported = 0;
1847 set_qos_param = 1;
1848 }
1849
1850 spin_unlock_irqrestore(&priv->ieee80211->lock, flags);
1851
1852 RT_TRACE(COMP_QOS, "%s: network->flags = %d,%d\n", __FUNCTION__,
1853 network->flags, priv->ieee80211->current_network.qos_data.active);
1854 if (set_qos_param == 1)
1855 queue_work(priv->priv_wq, &priv->qos_activate);
1856
1857 return ret;
1858 }
1859
1860
1861 static int rtl8192_handle_assoc_response(struct net_device *dev,
1862 struct ieee80211_assoc_response_frame *resp,
1863 struct ieee80211_network *network)
1864 {
1865 struct r8192_priv *priv = ieee80211_priv(dev);
1866 rtl8192_qos_association_resp(priv, network);
1867 return 0;
1868 }
1869
1870
1871 /* updateRATRTabel for MCS only. Basic rate is not implemented. */
1872 static void rtl8192_update_ratr_table(struct net_device* dev)
1873 {
1874 struct r8192_priv* priv = ieee80211_priv(dev);
1875 struct ieee80211_device* ieee = priv->ieee80211;
1876 u8* pMcsRate = ieee->dot11HTOperationalRateSet;
1877 u32 ratr_value = 0;
1878 u8 rate_index = 0;
1879
1880 rtl8192_config_rate(dev, (u16*)(&ratr_value));
1881 ratr_value |= (*(u16*)(pMcsRate)) << 12;
1882
1883 switch (ieee->mode)
1884 {
1885 case IEEE_A:
1886 ratr_value &= 0x00000FF0;
1887 break;
1888 case IEEE_B:
1889 ratr_value &= 0x0000000F;
1890 break;
1891 case IEEE_G:
1892 ratr_value &= 0x00000FF7;
1893 break;
1894 case IEEE_N_24G:
1895 case IEEE_N_5G:
1896 if (ieee->pHTInfo->PeerMimoPs == 0) //MIMO_PS_STATIC
1897 ratr_value &= 0x0007F007;
1898 else{
1899 if (priv->rf_type == RF_1T2R)
1900 ratr_value &= 0x000FF007;
1901 else
1902 ratr_value &= 0x0F81F007;
1903 }
1904 break;
1905 default:
1906 break;
1907 }
1908 ratr_value &= 0x0FFFFFFF;
1909 if(ieee->pHTInfo->bCurTxBW40MHz && ieee->pHTInfo->bCurShortGI40MHz){
1910 ratr_value |= 0x80000000;
1911 }else if(!ieee->pHTInfo->bCurTxBW40MHz && ieee->pHTInfo->bCurShortGI20MHz){
1912 ratr_value |= 0x80000000;
1913 }
1914 write_nic_dword(dev, RATR0+rate_index*4, ratr_value);
1915 write_nic_byte(dev, UFWP, 1);
1916 }
1917
1918 static bool GetNmodeSupportBySecCfg8190Pci(struct net_device*dev)
1919 {
1920 struct r8192_priv *priv = ieee80211_priv(dev);
1921 struct ieee80211_device *ieee = priv->ieee80211;
1922
1923 return !(ieee->rtllib_ap_sec_type &&
1924 (ieee->rtllib_ap_sec_type(ieee)&(SEC_ALG_WEP|SEC_ALG_TKIP)));
1925 }
1926
1927 static void rtl8192_refresh_supportrate(struct r8192_priv* priv)
1928 {
1929 struct ieee80211_device* ieee = priv->ieee80211;
1930 //we donot consider set support rate for ABG mode, only HT MCS rate is set here.
1931 if (ieee->mode == WIRELESS_MODE_N_24G || ieee->mode == WIRELESS_MODE_N_5G)
1932 {
1933 memcpy(ieee->Regdot11HTOperationalRateSet, ieee->RegHTSuppRateSet, 16);
1934 //RT_DEBUG_DATA(COMP_INIT, ieee->RegHTSuppRateSet, 16);
1935 //RT_DEBUG_DATA(COMP_INIT, ieee->Regdot11HTOperationalRateSet, 16);
1936 }
1937 else
1938 memset(ieee->Regdot11HTOperationalRateSet, 0, 16);
1939 }
1940
1941 static u8 rtl8192_getSupportedWireleeMode(struct net_device*dev)
1942 {
1943 struct r8192_priv *priv = ieee80211_priv(dev);
1944 u8 ret = 0;
1945 switch(priv->rf_chip)
1946 {
1947 case RF_8225:
1948 case RF_8256:
1949 case RF_PSEUDO_11N:
1950 ret = (WIRELESS_MODE_N_24G|WIRELESS_MODE_G|WIRELESS_MODE_B);
1951 break;
1952 case RF_8258:
1953 ret = (WIRELESS_MODE_A|WIRELESS_MODE_N_5G);
1954 break;
1955 default:
1956 ret = WIRELESS_MODE_B;
1957 break;
1958 }
1959 return ret;
1960 }
1961
1962 static void rtl8192_SetWirelessMode(struct net_device* dev, u8 wireless_mode)
1963 {
1964 struct r8192_priv *priv = ieee80211_priv(dev);
1965 u8 bSupportMode = rtl8192_getSupportedWireleeMode(dev);
1966
1967 #if 1
1968 if ((wireless_mode == WIRELESS_MODE_AUTO) || ((wireless_mode&bSupportMode)==0))
1969 {
1970 if(bSupportMode & WIRELESS_MODE_N_24G)
1971 {
1972 wireless_mode = WIRELESS_MODE_N_24G;
1973 }
1974 else if(bSupportMode & WIRELESS_MODE_N_5G)
1975 {
1976 wireless_mode = WIRELESS_MODE_N_5G;
1977 }
1978 else if((bSupportMode & WIRELESS_MODE_A))
1979 {
1980 wireless_mode = WIRELESS_MODE_A;
1981 }
1982 else if((bSupportMode & WIRELESS_MODE_G))
1983 {
1984 wireless_mode = WIRELESS_MODE_G;
1985 }
1986 else if((bSupportMode & WIRELESS_MODE_B))
1987 {
1988 wireless_mode = WIRELESS_MODE_B;
1989 }
1990 else{
1991 RT_TRACE(COMP_ERR, "%s(), No valid wireless mode supported, SupportedWirelessMode(%x)!!!\n", __FUNCTION__,bSupportMode);
1992 wireless_mode = WIRELESS_MODE_B;
1993 }
1994 }
1995 #ifdef TO_DO_LIST //// TODO: this function doesn't work well at this time, we should wait for FPGA
1996 ActUpdateChannelAccessSetting( pAdapter, pHalData->CurrentWirelessMode, &pAdapter->MgntInfo.Info8185.ChannelAccessSetting );
1997 #endif
1998 priv->ieee80211->mode = wireless_mode;
1999
2000 if ((wireless_mode == WIRELESS_MODE_N_24G) || (wireless_mode == WIRELESS_MODE_N_5G))
2001 priv->ieee80211->pHTInfo->bEnableHT = 1;
2002 else
2003 priv->ieee80211->pHTInfo->bEnableHT = 0;
2004 RT_TRACE(COMP_INIT, "Current Wireless Mode is %x\n", wireless_mode);
2005 rtl8192_refresh_supportrate(priv);
2006 #endif
2007
2008 }
2009
2010 static bool GetHalfNmodeSupportByAPs819xPci(struct net_device* dev)
2011 {
2012 struct r8192_priv* priv = ieee80211_priv(dev);
2013 struct ieee80211_device* ieee = priv->ieee80211;
2014
2015 return ieee->bHalfWirelessN24GMode;
2016 }
2017
2018 short rtl8192_is_tx_queue_empty(struct net_device *dev)
2019 {
2020 int i=0;
2021 struct r8192_priv *priv = ieee80211_priv(dev);
2022 for (i=0; i<=MGNT_QUEUE; i++)
2023 {
2024 if ((i== TXCMD_QUEUE) || (i == HCCA_QUEUE) )
2025 continue;
2026 if (skb_queue_len(&(&priv->tx_ring[i])->queue) > 0){
2027 printk("===>tx queue is not empty:%d, %d\n", i, skb_queue_len(&(&priv->tx_ring[i])->queue));
2028 return 0;
2029 }
2030 }
2031 return 1;
2032 }
2033
2034 static void rtl8192_hw_sleep_down(struct net_device *dev)
2035 {
2036 struct r8192_priv *priv = ieee80211_priv(dev);
2037 unsigned long flags = 0;
2038
2039 spin_lock_irqsave(&priv->rf_ps_lock,flags);
2040 if (priv->RFChangeInProgress) {
2041 spin_unlock_irqrestore(&priv->rf_ps_lock,flags);
2042 RT_TRACE(COMP_RF, "rtl8192_hw_sleep_down(): RF Change in progress! \n");
2043 printk("rtl8192_hw_sleep_down(): RF Change in progress!\n");
2044 return;
2045 }
2046 spin_unlock_irqrestore(&priv->rf_ps_lock,flags);
2047
2048 MgntActSet_RF_State(dev, eRfSleep, RF_CHANGE_BY_PS);
2049 }
2050
2051 static void rtl8192_hw_sleep_wq (struct work_struct *work)
2052 {
2053 struct delayed_work *dwork = container_of(work,struct delayed_work,work);
2054 struct ieee80211_device *ieee = container_of(dwork,struct ieee80211_device,hw_sleep_wq);
2055 struct net_device *dev = ieee->dev;
2056
2057 rtl8192_hw_sleep_down(dev);
2058 }
2059
2060 static void rtl8192_hw_wakeup(struct net_device* dev)
2061 {
2062 struct r8192_priv *priv = ieee80211_priv(dev);
2063 unsigned long flags = 0;
2064
2065 spin_lock_irqsave(&priv->rf_ps_lock,flags);
2066 if (priv->RFChangeInProgress) {
2067 spin_unlock_irqrestore(&priv->rf_ps_lock,flags);
2068 RT_TRACE(COMP_RF, "rtl8192_hw_wakeup(): RF Change in progress! \n");
2069 printk("rtl8192_hw_wakeup(): RF Change in progress! schedule wake up task again\n");
2070 queue_delayed_work(priv->ieee80211->wq,&priv->ieee80211->hw_wakeup_wq,MSECS(10));//PowerSave is not supported if kernel version is below 2.6.20
2071 return;
2072 }
2073 spin_unlock_irqrestore(&priv->rf_ps_lock,flags);
2074
2075 MgntActSet_RF_State(dev, eRfOn, RF_CHANGE_BY_PS);
2076 }
2077
2078 void rtl8192_hw_wakeup_wq (struct work_struct *work)
2079 {
2080 struct delayed_work *dwork = container_of(work,struct delayed_work,work);
2081 struct ieee80211_device *ieee = container_of(dwork,struct ieee80211_device,hw_wakeup_wq);
2082 struct net_device *dev = ieee->dev;
2083 rtl8192_hw_wakeup(dev);
2084
2085 }
2086
2087 #define MIN_SLEEP_TIME 50
2088 #define MAX_SLEEP_TIME 10000
2089 static void rtl8192_hw_to_sleep(struct net_device *dev, u32 th, u32 tl)
2090 {
2091 struct r8192_priv *priv = ieee80211_priv(dev);
2092
2093 u32 rb = jiffies;
2094 unsigned long flags;
2095
2096 spin_lock_irqsave(&priv->ps_lock,flags);
2097
2098 // Writing HW register with 0 equals to disable
2099 // the timer, that is not really what we want
2100 //
2101 tl -= MSECS(8+16+7);
2102
2103 // If the interval in witch we are requested to sleep is too
2104 // short then give up and remain awake
2105 // when we sleep after send null frame, the timer will be too short to sleep.
2106 //
2107 if(((tl>=rb)&& (tl-rb) <= MSECS(MIN_SLEEP_TIME))
2108 ||((rb>tl)&& (rb-tl) < MSECS(MIN_SLEEP_TIME))) {
2109 spin_unlock_irqrestore(&priv->ps_lock,flags);
2110 printk("too short to sleep::%x, %x, %lx\n",tl, rb, MSECS(MIN_SLEEP_TIME));
2111 return;
2112 }
2113
2114 if(((tl > rb) && ((tl-rb) > MSECS(MAX_SLEEP_TIME)))||
2115 ((tl < rb) && (tl>MSECS(69)) && ((rb-tl) > MSECS(MAX_SLEEP_TIME)))||
2116 ((tl<rb)&&(tl<MSECS(69))&&((tl+0xffffffff-rb)>MSECS(MAX_SLEEP_TIME)))) {
2117 printk("========>too long to sleep:%x, %x, %lx\n", tl, rb, MSECS(MAX_SLEEP_TIME));
2118 spin_unlock_irqrestore(&priv->ps_lock,flags);
2119 return;
2120 }
2121 {
2122 u32 tmp = (tl>rb)?(tl-rb):(rb-tl);
2123 queue_delayed_work(priv->ieee80211->wq,
2124 &priv->ieee80211->hw_wakeup_wq,tmp);
2125 //PowerSave not supported when kernel version less 2.6.20
2126 }
2127 queue_delayed_work(priv->ieee80211->wq,
2128 (void *)&priv->ieee80211->hw_sleep_wq,0);
2129 spin_unlock_irqrestore(&priv->ps_lock,flags);
2130
2131 }
2132
2133 static void rtl8192_init_priv_variable(struct net_device* dev)
2134 {
2135 struct r8192_priv *priv = ieee80211_priv(dev);
2136 u8 i;
2137 PRT_POWER_SAVE_CONTROL pPSC = (PRT_POWER_SAVE_CONTROL)(&(priv->ieee80211->PowerSaveControl));
2138
2139 // Default Halt the NIC if RF is OFF.
2140 pPSC->RegRfPsLevel |= RT_RF_OFF_LEVL_HALT_NIC;
2141 pPSC->RegRfPsLevel |= RT_RF_OFF_LEVL_CLK_REQ;
2142 pPSC->RegRfPsLevel |= RT_RF_OFF_LEVL_ASPM;
2143 pPSC->RegRfPsLevel |= RT_RF_LPS_LEVEL_ASPM;
2144 pPSC->bLeisurePs = true;
2145 pPSC->RegMaxLPSAwakeIntvl = 5;
2146 priv->bHwRadioOff = false;
2147
2148 priv->being_init_adapter = false;
2149 priv->txringcount = 64;//32;
2150 priv->rxbuffersize = 9100;//2048;//1024;
2151 priv->rxringcount = MAX_RX_COUNT;//64;
2152 priv->irq_enabled=0;
2153 priv->card_8192 = NIC_8192E;
2154 priv->rx_skb_complete = 1;
2155 priv->chan = 1; //set to channel 1
2156 priv->RegWirelessMode = WIRELESS_MODE_AUTO;
2157 priv->RegChannelPlan = 0xf;
2158 priv->nrxAMPDU_size = 0;
2159 priv->nrxAMPDU_aggr_num = 0;
2160 priv->last_rxdesc_tsf_high = 0;
2161 priv->last_rxdesc_tsf_low = 0;
2162 priv->ieee80211->mode = WIRELESS_MODE_AUTO; //SET AUTO
2163 priv->ieee80211->iw_mode = IW_MODE_INFRA;
2164 priv->ieee80211->ieee_up=0;
2165 priv->retry_rts = DEFAULT_RETRY_RTS;
2166 priv->retry_data = DEFAULT_RETRY_DATA;
2167 priv->ieee80211->rts = DEFAULT_RTS_THRESHOLD;
2168 priv->ieee80211->rate = 110; //11 mbps
2169 priv->ieee80211->short_slot = 1;
2170 priv->promisc = (dev->flags & IFF_PROMISC) ? 1:0;
2171 priv->bcck_in_ch14 = false;
2172 priv->bfsync_processing = false;
2173 priv->CCKPresentAttentuation = 0;
2174 priv->rfa_txpowertrackingindex = 0;
2175 priv->rfc_txpowertrackingindex = 0;
2176 priv->CckPwEnl = 6;
2177 //added by amy for silent reset
2178 priv->ResetProgress = RESET_TYPE_NORESET;
2179 priv->bForcedSilentReset = 0;
2180 priv->bDisableNormalResetCheck = false;
2181 priv->force_reset = false;
2182 //added by amy for power save
2183 priv->RegRfOff = 0;
2184 priv->ieee80211->RfOffReason = 0;
2185 priv->RFChangeInProgress = false;
2186 priv->bHwRfOffAction = 0;
2187 priv->SetRFPowerStateInProgress = false;
2188 priv->ieee80211->PowerSaveControl.bInactivePs = true;
2189 priv->ieee80211->PowerSaveControl.bIPSModeBackup = false;
2190 //just for debug
2191 priv->txpower_checkcnt = 0;
2192 priv->thermal_readback_index =0;
2193 priv->txpower_tracking_callback_cnt = 0;
2194 priv->ccktxpower_adjustcnt_ch14 = 0;
2195 priv->ccktxpower_adjustcnt_not_ch14 = 0;
2196
2197 priv->ieee80211->current_network.beacon_interval = DEFAULT_BEACONINTERVAL;
2198 priv->ieee80211->iw_mode = IW_MODE_INFRA;
2199 priv->ieee80211->softmac_features = IEEE_SOFTMAC_SCAN |
2200 IEEE_SOFTMAC_ASSOCIATE | IEEE_SOFTMAC_PROBERQ |
2201 IEEE_SOFTMAC_PROBERS | IEEE_SOFTMAC_TX_QUEUE;/* |
2202 IEEE_SOFTMAC_BEACONS;*///added by amy 080604 //| //IEEE_SOFTMAC_SINGLE_QUEUE;
2203
2204 priv->ieee80211->active_scan = 1;
2205 priv->ieee80211->modulation = IEEE80211_CCK_MODULATION | IEEE80211_OFDM_MODULATION;
2206 priv->ieee80211->host_encrypt = 1;
2207 priv->ieee80211->host_decrypt = 1;
2208 //priv->ieee80211->start_send_beacons = NULL;//rtl819xusb_beacon_tx;//-by amy 080604
2209 //priv->ieee80211->stop_send_beacons = NULL;//rtl8192_beacon_stop;//-by amy 080604
2210 priv->ieee80211->start_send_beacons = rtl8192_start_beacon;//+by david 081107
2211 priv->ieee80211->stop_send_beacons = rtl8192_stop_beacon;//+by david 081107
2212 priv->ieee80211->softmac_hard_start_xmit = rtl8192_hard_start_xmit;
2213 priv->ieee80211->set_chan = rtl8192_set_chan;
2214 priv->ieee80211->link_change = rtl8192_link_change;
2215 priv->ieee80211->softmac_data_hard_start_xmit = rtl8192_hard_data_xmit;
2216 priv->ieee80211->data_hard_stop = rtl8192_data_hard_stop;
2217 priv->ieee80211->data_hard_resume = rtl8192_data_hard_resume;
2218 priv->ieee80211->init_wmmparam_flag = 0;
2219 priv->ieee80211->fts = DEFAULT_FRAG_THRESHOLD;
2220 priv->ieee80211->check_nic_enough_desc = check_nic_enough_desc;
2221 priv->ieee80211->tx_headroom = sizeof(TX_FWINFO_8190PCI);
2222 priv->ieee80211->qos_support = 1;
2223 priv->ieee80211->dot11PowerSaveMode = 0;
2224 //added by WB
2225 // priv->ieee80211->SwChnlByTimerHandler = rtl8192_phy_SwChnl;
2226 priv->ieee80211->SetBWModeHandler = rtl8192_SetBWMode;
2227 priv->ieee80211->handle_assoc_response = rtl8192_handle_assoc_response;
2228 priv->ieee80211->handle_beacon = rtl8192_handle_beacon;
2229
2230 priv->ieee80211->sta_wake_up = rtl8192_hw_wakeup;
2231 // priv->ieee80211->ps_request_tx_ack = rtl8192_rq_tx_ack;
2232 priv->ieee80211->enter_sleep_state = rtl8192_hw_to_sleep;
2233 priv->ieee80211->ps_is_queue_empty = rtl8192_is_tx_queue_empty;
2234 //added by david
2235 priv->ieee80211->GetNmodeSupportBySecCfg = GetNmodeSupportBySecCfg8190Pci;
2236 priv->ieee80211->SetWirelessMode = rtl8192_SetWirelessMode;
2237 priv->ieee80211->GetHalfNmodeSupportByAPsHandler = GetHalfNmodeSupportByAPs819xPci;
2238
2239 //added by amy
2240 priv->ieee80211->InitialGainHandler = InitialGain819xPci;
2241
2242 #ifdef ENABLE_IPS
2243 priv->ieee80211->ieee80211_ips_leave_wq = ieee80211_ips_leave_wq;
2244 priv->ieee80211->ieee80211_ips_leave = ieee80211_ips_leave;
2245 #endif
2246 #ifdef ENABLE_LPS
2247 priv->ieee80211->LeisurePSLeave = LeisurePSLeave;
2248 #endif
2249
2250 priv->ieee80211->SetHwRegHandler = rtl8192e_SetHwReg;
2251 priv->ieee80211->rtllib_ap_sec_type = rtl8192e_ap_sec_type;
2252
2253 priv->ShortRetryLimit = 0x30;
2254 priv->LongRetryLimit = 0x30;
2255
2256 priv->ReceiveConfig = RCR_ADD3 |
2257 RCR_AMF | RCR_ADF | //accept management/data
2258 RCR_AICV | //accept control frame for SW AP needs PS-poll, 2005.07.07, by rcnjko.
2259 RCR_AB | RCR_AM | RCR_APM | //accept BC/MC/UC
2260 RCR_AAP | ((u32)7<<RCR_MXDMA_OFFSET) |
2261 ((u32)7 << RCR_FIFO_OFFSET) | RCR_ONLYERLPKT;
2262
2263 priv->irq_mask = (u32)(IMR_ROK | IMR_VODOK | IMR_VIDOK | IMR_BEDOK | IMR_BKDOK |
2264 IMR_HCCADOK | IMR_MGNTDOK | IMR_COMDOK | IMR_HIGHDOK |
2265 IMR_BDOK | IMR_RXCMDOK | IMR_TIMEOUT0 | IMR_RDU | IMR_RXFOVW |
2266 IMR_TXFOVW | IMR_BcnInt | IMR_TBDOK | IMR_TBDER);
2267
2268 priv->pFirmware = vzalloc(sizeof(rt_firmware));
2269
2270 /* rx related queue */
2271 skb_queue_head_init(&priv->skb_queue);
2272
2273 /* Tx related queue */
2274 for(i = 0; i < MAX_QUEUE_SIZE; i++) {
2275 skb_queue_head_init(&priv->ieee80211->skb_waitQ [i]);
2276 }
2277 for(i = 0; i < MAX_QUEUE_SIZE; i++) {
2278 skb_queue_head_init(&priv->ieee80211->skb_aggQ [i]);
2279 }
2280 priv->rf_set_chan = rtl8192_phy_SwChnl;
2281 }
2282
2283 static void rtl8192_init_priv_lock(struct r8192_priv* priv)
2284 {
2285 spin_lock_init(&priv->tx_lock);
2286 spin_lock_init(&priv->irq_th_lock);
2287 spin_lock_init(&priv->rf_ps_lock);
2288 spin_lock_init(&priv->ps_lock);
2289 sema_init(&priv->wx_sem,1);
2290 sema_init(&priv->rf_sem,1);
2291 mutex_init(&priv->mutex);
2292 }
2293
2294 /* init tasklet and wait_queue here */
2295 #define DRV_NAME "wlan0"
2296 static void rtl8192_init_priv_task(struct net_device* dev)
2297 {
2298 struct r8192_priv *priv = ieee80211_priv(dev);
2299
2300 #ifdef PF_SYNCTHREAD
2301 priv->priv_wq = create_workqueue(DRV_NAME,0);
2302 #else
2303 priv->priv_wq = create_workqueue(DRV_NAME);
2304 #endif
2305
2306 #ifdef ENABLE_IPS
2307 INIT_WORK(&priv->ieee80211->ips_leave_wq, (void*)IPSLeave_wq);
2308 #endif
2309
2310 // INIT_WORK(&priv->reset_wq, (void(*)(void*)) rtl8192_restart);
2311 INIT_WORK(&priv->reset_wq, rtl8192_restart);
2312 // INIT_DELAYED_WORK(&priv->watch_dog_wq, hal_dm_watchdog);
2313 INIT_DELAYED_WORK(&priv->watch_dog_wq, rtl819x_watchdog_wqcallback);
2314 INIT_DELAYED_WORK(&priv->txpower_tracking_wq, dm_txpower_trackingcallback);
2315 INIT_DELAYED_WORK(&priv->rfpath_check_wq, dm_rf_pathcheck_workitemcallback);
2316 INIT_DELAYED_WORK(&priv->update_beacon_wq, rtl8192_update_beacon);
2317 //INIT_WORK(&priv->SwChnlWorkItem, rtl8192_SwChnl_WorkItem);
2318 //INIT_WORK(&priv->SetBWModeWorkItem, rtl8192_SetBWModeWorkItem);
2319 INIT_WORK(&priv->qos_activate, rtl8192_qos_activate);
2320 INIT_DELAYED_WORK(&priv->ieee80211->hw_wakeup_wq,(void*) rtl8192_hw_wakeup_wq);
2321 INIT_DELAYED_WORK(&priv->ieee80211->hw_sleep_wq,(void*) rtl8192_hw_sleep_wq);
2322
2323 tasklet_init(&priv->irq_rx_tasklet,
2324 (void(*)(unsigned long))rtl8192_irq_rx_tasklet,
2325 (unsigned long)priv);
2326 tasklet_init(&priv->irq_tx_tasklet,
2327 (void(*)(unsigned long))rtl8192_irq_tx_tasklet,
2328 (unsigned long)priv);
2329 tasklet_init(&priv->irq_prepare_beacon_tasklet,
2330 (void(*)(unsigned long))rtl8192_prepare_beacon,
2331 (unsigned long)priv);
2332 }
2333
2334 static void rtl8192_get_eeprom_size(struct net_device* dev)
2335 {
2336 u16 curCR = 0;
2337 struct r8192_priv *priv = ieee80211_priv(dev);
2338 RT_TRACE(COMP_INIT, "===========>%s()\n", __FUNCTION__);
2339 curCR = read_nic_dword(dev, EPROM_CMD);
2340 RT_TRACE(COMP_INIT, "read from Reg Cmd9346CR(%x):%x\n", EPROM_CMD, curCR);
2341 //whether need I consider BIT5?
2342 priv->epromtype = (curCR & EPROM_CMD_9356SEL) ? EPROM_93c56 : EPROM_93c46;
2343 RT_TRACE(COMP_INIT, "<===========%s(), epromtype:%d\n", __FUNCTION__, priv->epromtype);
2344 }
2345
2346 /*
2347 * used to swap endian. as ntohl & htonl are not
2348 * neccessary to swap endian, so use this instead.
2349 */
2350 static inline u16 endian_swap(u16* data)
2351 {
2352 u16 tmp = *data;
2353 *data = (tmp >> 8) | (tmp << 8);
2354 return *data;
2355 }
2356
2357 /*
2358 * Adapter->EEPROMAddressSize should be set before this function call.
2359 * EEPROM address size can be got through GetEEPROMSize8185()
2360 */
2361 static void rtl8192_read_eeprom_info(struct net_device* dev)
2362 {
2363 struct r8192_priv *priv = ieee80211_priv(dev);
2364
2365 u8 tempval;
2366 #ifdef RTL8192E
2367 u8 ICVer8192, ICVer8256;
2368 #endif
2369 u16 i,usValue, IC_Version;
2370 u16 EEPROMId;
2371 #ifdef RTL8190P
2372 u8 offset;
2373 u8 EepromTxPower[100];
2374 #endif
2375 u8 bMac_Tmp_Addr[6] = {0x00, 0xe0, 0x4c, 0x00, 0x00, 0x01};
2376 RT_TRACE(COMP_INIT, "====> rtl8192_read_eeprom_info\n");
2377
2378
2379 // TODO: I don't know if we need to apply EF function to EEPROM read function
2380
2381 //2 Read EEPROM ID to make sure autoload is success
2382 EEPROMId = eprom_read(dev, 0);
2383 if( EEPROMId != RTL8190_EEPROM_ID )
2384 {
2385 RT_TRACE(COMP_ERR, "EEPROM ID is invalid:%x, %x\n", EEPROMId, RTL8190_EEPROM_ID);
2386 priv->AutoloadFailFlag=true;
2387 }
2388 else
2389 {
2390 priv->AutoloadFailFlag=false;
2391 }
2392
2393 //
2394 // Assign Chip Version ID
2395 //
2396 // Read IC Version && Channel Plan
2397 if(!priv->AutoloadFailFlag)
2398 {
2399 // VID, PID
2400 priv->eeprom_vid = eprom_read(dev, (EEPROM_VID >> 1));
2401 priv->eeprom_did = eprom_read(dev, (EEPROM_DID >> 1));
2402
2403 usValue = eprom_read(dev, (u16)(EEPROM_Customer_ID>>1)) >> 8 ;
2404 priv->eeprom_CustomerID = (u8)( usValue & 0xff);
2405 usValue = eprom_read(dev, (EEPROM_ICVersion_ChannelPlan>>1));
2406 priv->eeprom_ChannelPlan = usValue&0xff;
2407 IC_Version = ((usValue&0xff00)>>8);
2408
2409 #ifdef RTL8190P
2410 priv->card_8192_version = (VERSION_8190)(IC_Version);
2411 #else
2412 #ifdef RTL8192E
2413 ICVer8192 = (IC_Version&0xf); //bit0~3; 1:A cut, 2:B cut, 3:C cut...
2414 ICVer8256 = ((IC_Version&0xf0)>>4);//bit4~6, bit7 reserved for other RF chip; 1:A cut, 2:B cut, 3:C cut...
2415 RT_TRACE(COMP_INIT, "\nICVer8192 = 0x%x\n", ICVer8192);
2416 RT_TRACE(COMP_INIT, "\nICVer8256 = 0x%x\n", ICVer8256);
2417 if(ICVer8192 == 0x2) //B-cut
2418 {
2419 if(ICVer8256 == 0x5) //E-cut
2420 priv->card_8192_version= VERSION_8190_BE;
2421 }
2422 #endif
2423 #endif
2424 switch(priv->card_8192_version)
2425 {
2426 case VERSION_8190_BD:
2427 case VERSION_8190_BE:
2428 break;
2429 default:
2430 priv->card_8192_version = VERSION_8190_BD;
2431 break;
2432 }
2433 RT_TRACE(COMP_INIT, "\nIC Version = 0x%x\n", priv->card_8192_version);
2434 }
2435 else
2436 {
2437 priv->card_8192_version = VERSION_8190_BD;
2438 priv->eeprom_vid = 0;
2439 priv->eeprom_did = 0;
2440 priv->eeprom_CustomerID = 0;
2441 priv->eeprom_ChannelPlan = 0;
2442 RT_TRACE(COMP_INIT, "\nIC Version = 0x%x\n", 0xff);
2443 }
2444
2445 RT_TRACE(COMP_INIT, "EEPROM VID = 0x%4x\n", priv->eeprom_vid);
2446 RT_TRACE(COMP_INIT, "EEPROM DID = 0x%4x\n", priv->eeprom_did);
2447 RT_TRACE(COMP_INIT,"EEPROM Customer ID: 0x%2x\n", priv->eeprom_CustomerID);
2448
2449 //2 Read Permanent MAC address
2450 if(!priv->AutoloadFailFlag)
2451 {
2452 for(i = 0; i < 6; i += 2)
2453 {
2454 usValue = eprom_read(dev, (u16) ((EEPROM_NODE_ADDRESS_BYTE_0+i)>>1));
2455 *(u16*)(&dev->dev_addr[i]) = usValue;
2456 }
2457 } else {
2458 // when auto load failed, the last address byte set to be a random one.
2459 // added by david woo.2007/11/7
2460 memcpy(dev->dev_addr, bMac_Tmp_Addr, 6);
2461 }
2462
2463 RT_TRACE(COMP_INIT, "Permanent Address = %pM\n", dev->dev_addr);
2464
2465 //2 TX Power Check EEPROM Fail or not
2466 if(priv->card_8192_version > VERSION_8190_BD) {
2467 priv->bTXPowerDataReadFromEEPORM = true;
2468 } else {
2469 priv->bTXPowerDataReadFromEEPORM = false;
2470 }
2471
2472 // 2007/11/15 MH 8190PCI Default=2T4R, 8192PCIE default=1T2R
2473 priv->rf_type = RTL819X_DEFAULT_RF_TYPE;
2474
2475 if(priv->card_8192_version > VERSION_8190_BD)
2476 {
2477 // Read RF-indication and Tx Power gain index diff of legacy to HT OFDM rate.
2478 if(!priv->AutoloadFailFlag)
2479 {
2480 tempval = (eprom_read(dev, (EEPROM_RFInd_PowerDiff>>1))) & 0xff;
2481 priv->EEPROMLegacyHTTxPowerDiff = tempval & 0xf; // bit[3:0]
2482
2483 if (tempval&0x80) //RF-indication, bit[7]
2484 priv->rf_type = RF_1T2R;
2485 else
2486 priv->rf_type = RF_2T4R;
2487 }
2488 else
2489 {
2490 priv->EEPROMLegacyHTTxPowerDiff = EEPROM_Default_LegacyHTTxPowerDiff;
2491 }
2492 RT_TRACE(COMP_INIT, "EEPROMLegacyHTTxPowerDiff = %d\n",
2493 priv->EEPROMLegacyHTTxPowerDiff);
2494
2495 // Read ThermalMeter from EEPROM
2496 if(!priv->AutoloadFailFlag)
2497 {
2498 priv->EEPROMThermalMeter = (u8)(((eprom_read(dev, (EEPROM_ThermalMeter>>1))) & 0xff00)>>8);
2499 }
2500 else
2501 {
2502 priv->EEPROMThermalMeter = EEPROM_Default_ThermalMeter;
2503 }
2504 RT_TRACE(COMP_INIT, "ThermalMeter = %d\n", priv->EEPROMThermalMeter);
2505 //vivi, for tx power track
2506 priv->TSSI_13dBm = priv->EEPROMThermalMeter *100;
2507
2508 if(priv->epromtype == EPROM_93c46)
2509 {
2510 // Read antenna tx power offset of B/C/D to A and CrystalCap from EEPROM
2511 if(!priv->AutoloadFailFlag)
2512 {
2513 usValue = eprom_read(dev, (EEPROM_TxPwDiff_CrystalCap>>1));
2514 priv->EEPROMAntPwDiff = (usValue&0x0fff);
2515 priv->EEPROMCrystalCap = (u8)((usValue&0xf000)>>12);
2516 }
2517 else
2518 {
2519 priv->EEPROMAntPwDiff = EEPROM_Default_AntTxPowerDiff;
2520 priv->EEPROMCrystalCap = EEPROM_Default_TxPwDiff_CrystalCap;
2521 }
2522 RT_TRACE(COMP_INIT, "EEPROMAntPwDiff = %d\n", priv->EEPROMAntPwDiff);
2523 RT_TRACE(COMP_INIT, "EEPROMCrystalCap = %d\n", priv->EEPROMCrystalCap);
2524
2525 //
2526 // Get per-channel Tx Power Level
2527 //
2528 for(i=0; i<14; i+=2)
2529 {
2530 if(!priv->AutoloadFailFlag)
2531 {
2532 usValue = eprom_read(dev, (u16) ((EEPROM_TxPwIndex_CCK+i)>>1) );
2533 }
2534 else
2535 {
2536 usValue = EEPROM_Default_TxPower;
2537 }
2538 *((u16*)(&priv->EEPROMTxPowerLevelCCK[i])) = usValue;
2539 RT_TRACE(COMP_INIT,"CCK Tx Power Level, Index %d = 0x%02x\n", i, priv->EEPROMTxPowerLevelCCK[i]);
2540 RT_TRACE(COMP_INIT, "CCK Tx Power Level, Index %d = 0x%02x\n", i+1, priv->EEPROMTxPowerLevelCCK[i+1]);
2541 }
2542 for(i=0; i<14; i+=2)
2543 {
2544 if(!priv->AutoloadFailFlag)
2545 {
2546 usValue = eprom_read(dev, (u16) ((EEPROM_TxPwIndex_OFDM_24G+i)>>1) );
2547 }
2548 else
2549 {
2550 usValue = EEPROM_Default_TxPower;
2551 }
2552 *((u16*)(&priv->EEPROMTxPowerLevelOFDM24G[i])) = usValue;
2553 RT_TRACE(COMP_INIT, "OFDM 2.4G Tx Power Level, Index %d = 0x%02x\n", i, priv->EEPROMTxPowerLevelOFDM24G[i]);
2554 RT_TRACE(COMP_INIT, "OFDM 2.4G Tx Power Level, Index %d = 0x%02x\n", i+1, priv->EEPROMTxPowerLevelOFDM24G[i+1]);
2555 }
2556 }
2557 else if(priv->epromtype== EPROM_93c56)
2558 {
2559 #ifdef RTL8190P
2560 // Read CrystalCap from EEPROM
2561 if(!priv->AutoloadFailFlag)
2562 {
2563 priv->EEPROMAntPwDiff = EEPROM_Default_AntTxPowerDiff;
2564 priv->EEPROMCrystalCap = (u8)(((eprom_read(dev, (EEPROM_C56_CrystalCap>>1))) & 0xf000)>>12);
2565 }
2566 else
2567 {
2568 priv->EEPROMAntPwDiff = EEPROM_Default_AntTxPowerDiff;
2569 priv->EEPROMCrystalCap = EEPROM_Default_TxPwDiff_CrystalCap;
2570 }
2571 RT_TRACE(COMP_INIT,"EEPROMAntPwDiff = %d\n", priv->EEPROMAntPwDiff);
2572 RT_TRACE(COMP_INIT, "EEPROMCrystalCap = %d\n", priv->EEPROMCrystalCap);
2573
2574 // Get Tx Power Level by Channel
2575 if(!priv->AutoloadFailFlag)
2576 {
2577 // Read Tx power of Channel 1 ~ 14 from EEPROM.
2578 for(i = 0; i < 12; i+=2)
2579 {
2580 if (i <6)
2581 offset = EEPROM_C56_RfA_CCK_Chnl1_TxPwIndex + i;
2582 else
2583 offset = EEPROM_C56_RfC_CCK_Chnl1_TxPwIndex + i - 6;
2584 usValue = eprom_read(dev, (offset>>1));
2585 *((u16*)(&EepromTxPower[i])) = usValue;
2586 }
2587
2588 for(i = 0; i < 12; i++)
2589 {
2590 if (i <= 2)
2591 priv->EEPROMRfACCKChnl1TxPwLevel[i] = EepromTxPower[i];
2592 else if ((i >=3 )&&(i <= 5))
2593 priv->EEPROMRfAOfdmChnlTxPwLevel[i-3] = EepromTxPower[i];
2594 else if ((i >=6 )&&(i <= 8))
2595 priv->EEPROMRfCCCKChnl1TxPwLevel[i-6] = EepromTxPower[i];
2596 else
2597 priv->EEPROMRfCOfdmChnlTxPwLevel[i-9] = EepromTxPower[i];
2598 }
2599 }
2600 else
2601 {
2602 priv->EEPROMRfACCKChnl1TxPwLevel[0] = EEPROM_Default_TxPowerLevel;
2603 priv->EEPROMRfACCKChnl1TxPwLevel[1] = EEPROM_Default_TxPowerLevel;
2604 priv->EEPROMRfACCKChnl1TxPwLevel[2] = EEPROM_Default_TxPowerLevel;
2605
2606 priv->EEPROMRfAOfdmChnlTxPwLevel[0] = EEPROM_Default_TxPowerLevel;
2607 priv->EEPROMRfAOfdmChnlTxPwLevel[1] = EEPROM_Default_TxPowerLevel;
2608 priv->EEPROMRfAOfdmChnlTxPwLevel[2] = EEPROM_Default_TxPowerLevel;
2609
2610 priv->EEPROMRfCCCKChnl1TxPwLevel[0] = EEPROM_Default_TxPowerLevel;
2611 priv->EEPROMRfCCCKChnl1TxPwLevel[1] = EEPROM_Default_TxPowerLevel;
2612 priv->EEPROMRfCCCKChnl1TxPwLevel[2] = EEPROM_Default_TxPowerLevel;
2613
2614 priv->EEPROMRfCOfdmChnlTxPwLevel[0] = EEPROM_Default_TxPowerLevel;
2615 priv->EEPROMRfCOfdmChnlTxPwLevel[1] = EEPROM_Default_TxPowerLevel;
2616 priv->EEPROMRfCOfdmChnlTxPwLevel[2] = EEPROM_Default_TxPowerLevel;
2617 }
2618 RT_TRACE(COMP_INIT, "priv->EEPROMRfACCKChnl1TxPwLevel[0] = 0x%x\n", priv->EEPROMRfACCKChnl1TxPwLevel[0]);
2619 RT_TRACE(COMP_INIT, "priv->EEPROMRfACCKChnl1TxPwLevel[1] = 0x%x\n", priv->EEPROMRfACCKChnl1TxPwLevel[1]);
2620 RT_TRACE(COMP_INIT, "priv->EEPROMRfACCKChnl1TxPwLevel[2] = 0x%x\n", priv->EEPROMRfACCKChnl1TxPwLevel[2]);
2621 RT_TRACE(COMP_INIT, "priv->EEPROMRfAOfdmChnlTxPwLevel[0] = 0x%x\n", priv->EEPROMRfAOfdmChnlTxPwLevel[0]);
2622 RT_TRACE(COMP_INIT, "priv->EEPROMRfAOfdmChnlTxPwLevel[1] = 0x%x\n", priv->EEPROMRfAOfdmChnlTxPwLevel[1]);
2623 RT_TRACE(COMP_INIT, "priv->EEPROMRfAOfdmChnlTxPwLevel[2] = 0x%x\n", priv->EEPROMRfAOfdmChnlTxPwLevel[2]);
2624 RT_TRACE(COMP_INIT, "priv->EEPROMRfCCCKChnl1TxPwLevel[0] = 0x%x\n", priv->EEPROMRfCCCKChnl1TxPwLevel[0]);
2625 RT_TRACE(COMP_INIT, "priv->EEPROMRfCCCKChnl1TxPwLevel[1] = 0x%x\n", priv->EEPROMRfCCCKChnl1TxPwLevel[1]);
2626 RT_TRACE(COMP_INIT, "priv->EEPROMRfCCCKChnl1TxPwLevel[2] = 0x%x\n", priv->EEPROMRfCCCKChnl1TxPwLevel[2]);
2627 RT_TRACE(COMP_INIT, "priv->EEPROMRfCOfdmChnlTxPwLevel[0] = 0x%x\n", priv->EEPROMRfCOfdmChnlTxPwLevel[0]);
2628 RT_TRACE(COMP_INIT, "priv->EEPROMRfCOfdmChnlTxPwLevel[1] = 0x%x\n", priv->EEPROMRfCOfdmChnlTxPwLevel[1]);
2629 RT_TRACE(COMP_INIT, "priv->EEPROMRfCOfdmChnlTxPwLevel[2] = 0x%x\n", priv->EEPROMRfCOfdmChnlTxPwLevel[2]);
2630 #endif
2631
2632 }
2633 //
2634 // Update HAL variables.
2635 //
2636 if(priv->epromtype == EPROM_93c46)
2637 {
2638 for(i=0; i<14; i++)
2639 {
2640 priv->TxPowerLevelCCK[i] = priv->EEPROMTxPowerLevelCCK[i];
2641 priv->TxPowerLevelOFDM24G[i] = priv->EEPROMTxPowerLevelOFDM24G[i];
2642 }
2643 priv->LegacyHTTxPowerDiff = priv->EEPROMLegacyHTTxPowerDiff;
2644 // Antenna B gain offset to antenna A, bit0~3
2645 priv->AntennaTxPwDiff[0] = (priv->EEPROMAntPwDiff & 0xf);
2646 // Antenna C gain offset to antenna A, bit4~7
2647 priv->AntennaTxPwDiff[1] = ((priv->EEPROMAntPwDiff & 0xf0)>>4);
2648 // Antenna D gain offset to antenna A, bit8~11
2649 priv->AntennaTxPwDiff[2] = ((priv->EEPROMAntPwDiff & 0xf00)>>8);
2650 // CrystalCap, bit12~15
2651 priv->CrystalCap = priv->EEPROMCrystalCap;
2652 // ThermalMeter, bit0~3 for RFIC1, bit4~7 for RFIC2
2653 priv->ThermalMeter[0] = (priv->EEPROMThermalMeter & 0xf);
2654 priv->ThermalMeter[1] = ((priv->EEPROMThermalMeter & 0xf0)>>4);
2655 }
2656 else if(priv->epromtype == EPROM_93c56)
2657 {
2658 //char cck_pwr_diff_a=0, cck_pwr_diff_c=0;
2659
2660 //cck_pwr_diff_a = pHalData->EEPROMRfACCKChnl7TxPwLevel - pHalData->EEPROMRfAOfdmChnlTxPwLevel[1];
2661 //cck_pwr_diff_c = pHalData->EEPROMRfCCCKChnl7TxPwLevel - pHalData->EEPROMRfCOfdmChnlTxPwLevel[1];
2662 for(i=0; i<3; i++) // channel 1~3 use the same Tx Power Level.
2663 {
2664 priv->TxPowerLevelCCK_A[i] = priv->EEPROMRfACCKChnl1TxPwLevel[0];
2665 priv->TxPowerLevelOFDM24G_A[i] = priv->EEPROMRfAOfdmChnlTxPwLevel[0];
2666 priv->TxPowerLevelCCK_C[i] = priv->EEPROMRfCCCKChnl1TxPwLevel[0];
2667 priv->TxPowerLevelOFDM24G_C[i] = priv->EEPROMRfCOfdmChnlTxPwLevel[0];
2668 }
2669 for(i=3; i<9; i++) // channel 4~9 use the same Tx Power Level
2670 {
2671 priv->TxPowerLevelCCK_A[i] = priv->EEPROMRfACCKChnl1TxPwLevel[1];
2672 priv->TxPowerLevelOFDM24G_A[i] = priv->EEPROMRfAOfdmChnlTxPwLevel[1];
2673 priv->TxPowerLevelCCK_C[i] = priv->EEPROMRfCCCKChnl1TxPwLevel[1];
2674 priv->TxPowerLevelOFDM24G_C[i] = priv->EEPROMRfCOfdmChnlTxPwLevel[1];
2675 }
2676 for(i=9; i<14; i++) // channel 10~14 use the same Tx Power Level
2677 {
2678 priv->TxPowerLevelCCK_A[i] = priv->EEPROMRfACCKChnl1TxPwLevel[2];
2679 priv->TxPowerLevelOFDM24G_A[i] = priv->EEPROMRfAOfdmChnlTxPwLevel[2];
2680 priv->TxPowerLevelCCK_C[i] = priv->EEPROMRfCCCKChnl1TxPwLevel[2];
2681 priv->TxPowerLevelOFDM24G_C[i] = priv->EEPROMRfCOfdmChnlTxPwLevel[2];
2682 }
2683 for(i=0; i<14; i++)
2684 RT_TRACE(COMP_INIT, "priv->TxPowerLevelCCK_A[%d] = 0x%x\n", i, priv->TxPowerLevelCCK_A[i]);
2685 for(i=0; i<14; i++)
2686 RT_TRACE(COMP_INIT,"priv->TxPowerLevelOFDM24G_A[%d] = 0x%x\n", i, priv->TxPowerLevelOFDM24G_A[i]);
2687 for(i=0; i<14; i++)
2688 RT_TRACE(COMP_INIT, "priv->TxPowerLevelCCK_C[%d] = 0x%x\n", i, priv->TxPowerLevelCCK_C[i]);
2689 for(i=0; i<14; i++)
2690 RT_TRACE(COMP_INIT, "priv->TxPowerLevelOFDM24G_C[%d] = 0x%x\n", i, priv->TxPowerLevelOFDM24G_C[i]);
2691 priv->LegacyHTTxPowerDiff = priv->EEPROMLegacyHTTxPowerDiff;
2692 priv->AntennaTxPwDiff[0] = 0;
2693 priv->AntennaTxPwDiff[1] = 0;
2694 priv->AntennaTxPwDiff[2] = 0;
2695 priv->CrystalCap = priv->EEPROMCrystalCap;
2696 // ThermalMeter, bit0~3 for RFIC1, bit4~7 for RFIC2
2697 priv->ThermalMeter[0] = (priv->EEPROMThermalMeter & 0xf);
2698 priv->ThermalMeter[1] = ((priv->EEPROMThermalMeter & 0xf0)>>4);
2699 }
2700 }
2701
2702 if(priv->rf_type == RF_1T2R)
2703 {
2704 RT_TRACE(COMP_INIT, "\n1T2R config\n");
2705 }
2706 else if (priv->rf_type == RF_2T4R)
2707 {
2708 RT_TRACE(COMP_INIT, "\n2T4R config\n");
2709 }
2710
2711 // 2008/01/16 MH We can only know RF type in the function. So we have to init
2712 // DIG RATR table again.
2713 init_rate_adaptive(dev);
2714
2715 //1 Make a copy for following variables and we can change them if we want
2716
2717 priv->rf_chip= RF_8256;
2718
2719 if(priv->RegChannelPlan == 0xf)
2720 {
2721 priv->ChannelPlan = priv->eeprom_ChannelPlan;
2722 }
2723 else
2724 {
2725 priv->ChannelPlan = priv->RegChannelPlan;
2726 }
2727
2728 //
2729 // Used PID and DID to Set CustomerID
2730 //
2731 if( priv->eeprom_vid == 0x1186 && priv->eeprom_did == 0x3304 )
2732 {
2733 priv->CustomerID = RT_CID_DLINK;
2734 }
2735
2736 switch(priv->eeprom_CustomerID)
2737 {
2738 case EEPROM_CID_DEFAULT:
2739 priv->CustomerID = RT_CID_DEFAULT;
2740 break;
2741 case EEPROM_CID_CAMEO:
2742 priv->CustomerID = RT_CID_819x_CAMEO;
2743 break;
2744 case EEPROM_CID_RUNTOP:
2745 priv->CustomerID = RT_CID_819x_RUNTOP;
2746 break;
2747 case EEPROM_CID_NetCore:
2748 priv->CustomerID = RT_CID_819x_Netcore;
2749 break;
2750 case EEPROM_CID_TOSHIBA: // Merge by Jacken, 2008/01/31
2751 priv->CustomerID = RT_CID_TOSHIBA;
2752 if(priv->eeprom_ChannelPlan&0x80)
2753 priv->ChannelPlan = priv->eeprom_ChannelPlan&0x7f;
2754 else
2755 priv->ChannelPlan = 0x0;
2756 RT_TRACE(COMP_INIT, "Toshiba ChannelPlan = 0x%x\n",
2757 priv->ChannelPlan);
2758 break;
2759 case EEPROM_CID_Nettronix:
2760 priv->CustomerID = RT_CID_Nettronix;
2761 break;
2762 case EEPROM_CID_Pronet:
2763 priv->CustomerID = RT_CID_PRONET;
2764 break;
2765 case EEPROM_CID_DLINK:
2766 priv->CustomerID = RT_CID_DLINK;
2767 break;
2768
2769 case EEPROM_CID_WHQL:
2770 //Adapter->bInHctTest = TRUE;//do not supported
2771
2772 //priv->bSupportTurboMode = FALSE;
2773 //priv->bAutoTurboBy8186 = FALSE;
2774
2775 //pMgntInfo->PowerSaveControl.bInactivePs = FALSE;
2776 //pMgntInfo->PowerSaveControl.bIPSModeBackup = FALSE;
2777 //pMgntInfo->PowerSaveControl.bLeisurePs = FALSE;
2778
2779 break;
2780 default:
2781 // value from RegCustomerID
2782 break;
2783 }
2784
2785 //Avoid the channel plan array overflow, by Bruce, 2007-08-27.
2786 if(priv->ChannelPlan > CHANNEL_PLAN_LEN - 1)
2787 priv->ChannelPlan = 0; //FCC
2788
2789 switch(priv->CustomerID)
2790 {
2791 case RT_CID_DEFAULT:
2792 #ifdef RTL8190P
2793 priv->LedStrategy = HW_LED;
2794 #else
2795 #ifdef RTL8192E
2796 priv->LedStrategy = SW_LED_MODE1;
2797 #endif
2798 #endif
2799 break;
2800
2801 case RT_CID_819x_CAMEO:
2802 priv->LedStrategy = SW_LED_MODE2;
2803 break;
2804
2805 case RT_CID_819x_RUNTOP:
2806 priv->LedStrategy = SW_LED_MODE3;
2807 break;
2808
2809 case RT_CID_819x_Netcore:
2810 priv->LedStrategy = SW_LED_MODE4;
2811 break;
2812
2813 case RT_CID_Nettronix:
2814 priv->LedStrategy = SW_LED_MODE5;
2815 break;
2816
2817 case RT_CID_PRONET:
2818 priv->LedStrategy = SW_LED_MODE6;
2819 break;
2820
2821 case RT_CID_TOSHIBA: //Modify by Jacken 2008/01/31
2822 // Do nothing.
2823 //break;
2824
2825 default:
2826 #ifdef RTL8190P
2827 priv->LedStrategy = HW_LED;
2828 #else
2829 #ifdef RTL8192E
2830 priv->LedStrategy = SW_LED_MODE1;
2831 #endif
2832 #endif
2833 break;
2834 }
2835
2836
2837 if( priv->eeprom_vid == 0x1186 && priv->eeprom_did == 0x3304)
2838 priv->ieee80211->bSupportRemoteWakeUp = true;
2839 else
2840 priv->ieee80211->bSupportRemoteWakeUp = false;
2841
2842
2843 RT_TRACE(COMP_INIT, "RegChannelPlan(%d)\n", priv->RegChannelPlan);
2844 RT_TRACE(COMP_INIT, "ChannelPlan = %d \n", priv->ChannelPlan);
2845 RT_TRACE(COMP_INIT, "LedStrategy = %d \n", priv->LedStrategy);
2846 RT_TRACE(COMP_TRACE, "<==== ReadAdapterInfo\n");
2847
2848 return ;
2849 }
2850
2851
2852 static short rtl8192_get_channel_map(struct net_device * dev)
2853 {
2854 struct r8192_priv *priv = ieee80211_priv(dev);
2855 #ifdef ENABLE_DOT11D
2856 if(priv->ChannelPlan> COUNTRY_CODE_GLOBAL_DOMAIN){
2857 printk("rtl8180_init:Error channel plan! Set to default.\n");
2858 priv->ChannelPlan= 0;
2859 }
2860 RT_TRACE(COMP_INIT, "Channel plan is %d\n",priv->ChannelPlan);
2861
2862 rtl819x_set_channel_map(priv->ChannelPlan, priv);
2863 #else
2864 int ch,i;
2865 //Set Default Channel Plan
2866 if(!channels){
2867 DMESG("No channels, aborting");
2868 return -1;
2869 }
2870 ch=channels;
2871 priv->ChannelPlan= 0;//hikaru
2872 // set channels 1..14 allowed in given locale
2873 for (i=1; i<=14; i++) {
2874 (priv->ieee80211->channel_map)[i] = (u8)(ch & 0x01);
2875 ch >>= 1;
2876 }
2877 #endif
2878 return 0;
2879 }
2880
2881 static short rtl8192_init(struct net_device *dev)
2882 {
2883 struct r8192_priv *priv = ieee80211_priv(dev);
2884 memset(&(priv->stats),0,sizeof(struct Stats));
2885 rtl8192_init_priv_variable(dev);
2886 rtl8192_init_priv_lock(priv);
2887 rtl8192_init_priv_task(dev);
2888 rtl8192_get_eeprom_size(dev);
2889 rtl8192_read_eeprom_info(dev);
2890 rtl8192_get_channel_map(dev);
2891 init_hal_dm(dev);
2892 init_timer(&priv->watch_dog_timer);
2893 priv->watch_dog_timer.data = (unsigned long)dev;
2894 priv->watch_dog_timer.function = watch_dog_timer_callback;
2895 if (request_irq(dev->irq, rtl8192_interrupt, IRQF_SHARED, dev->name, dev)) {
2896 printk("Error allocating IRQ %d",dev->irq);
2897 return -1;
2898 }else{
2899 priv->irq=dev->irq;
2900 printk("IRQ %d",dev->irq);
2901 }
2902 if(rtl8192_pci_initdescring(dev)!=0){
2903 printk("Endopoints initialization failed");
2904 return -1;
2905 }
2906
2907 //rtl8192_rx_enable(dev);
2908 //rtl8192_adapter_start(dev);
2909 return 0;
2910 }
2911
2912 /*
2913 * Actually only set RRSR, RATR and BW_OPMODE registers
2914 * not to do all the hw config as its name says
2915 * This part need to modified according to the rate set we filtered
2916 */
2917 static void rtl8192_hwconfig(struct net_device* dev)
2918 {
2919 u32 regRATR = 0, regRRSR = 0;
2920 u8 regBwOpMode = 0, regTmp = 0;
2921 struct r8192_priv *priv = ieee80211_priv(dev);
2922
2923 // Set RRSR, RATR, and BW_OPMODE registers
2924 //
2925 switch(priv->ieee80211->mode)
2926 {
2927 case WIRELESS_MODE_B:
2928 regBwOpMode = BW_OPMODE_20MHZ;
2929 regRATR = RATE_ALL_CCK;
2930 regRRSR = RATE_ALL_CCK;
2931 break;
2932 case WIRELESS_MODE_A:
2933 regBwOpMode = BW_OPMODE_5G |BW_OPMODE_20MHZ;
2934 regRATR = RATE_ALL_OFDM_AG;
2935 regRRSR = RATE_ALL_OFDM_AG;
2936 break;
2937 case WIRELESS_MODE_G:
2938 regBwOpMode = BW_OPMODE_20MHZ;
2939 regRATR = RATE_ALL_CCK | RATE_ALL_OFDM_AG;
2940 regRRSR = RATE_ALL_CCK | RATE_ALL_OFDM_AG;
2941 break;
2942 case WIRELESS_MODE_AUTO:
2943 case WIRELESS_MODE_N_24G:
2944 // It support CCK rate by default.
2945 // CCK rate will be filtered out only when associated AP does not support it.
2946 regBwOpMode = BW_OPMODE_20MHZ;
2947 regRATR = RATE_ALL_CCK | RATE_ALL_OFDM_AG | RATE_ALL_OFDM_1SS | RATE_ALL_OFDM_2SS;
2948 regRRSR = RATE_ALL_CCK | RATE_ALL_OFDM_AG;
2949 break;
2950 case WIRELESS_MODE_N_5G:
2951 regBwOpMode = BW_OPMODE_5G;
2952 regRATR = RATE_ALL_OFDM_AG | RATE_ALL_OFDM_1SS | RATE_ALL_OFDM_2SS;
2953 regRRSR = RATE_ALL_OFDM_AG;
2954 break;
2955 }
2956
2957 write_nic_byte(dev, BW_OPMODE, regBwOpMode);
2958 {
2959 u32 ratr_value = 0;
2960 ratr_value = regRATR;
2961 if (priv->rf_type == RF_1T2R)
2962 {
2963 ratr_value &= ~(RATE_ALL_OFDM_2SS);
2964 }
2965 write_nic_dword(dev, RATR0, ratr_value);
2966 write_nic_byte(dev, UFWP, 1);
2967 }
2968 regTmp = read_nic_byte(dev, 0x313);
2969 regRRSR = ((regTmp) << 24) | (regRRSR & 0x00ffffff);
2970 write_nic_dword(dev, RRSR, regRRSR);
2971
2972 //
2973 // Set Retry Limit here
2974 //
2975 write_nic_word(dev, RETRY_LIMIT,
2976 priv->ShortRetryLimit << RETRY_LIMIT_SHORT_SHIFT |
2977 priv->LongRetryLimit << RETRY_LIMIT_LONG_SHIFT);
2978 // Set Contention Window here
2979
2980 // Set Tx AGC
2981
2982 // Set Tx Antenna including Feedback control
2983
2984 // Set Auto Rate fallback control
2985
2986
2987 }
2988
2989
2990 static RT_STATUS rtl8192_adapter_start(struct net_device *dev)
2991 {
2992 struct r8192_priv *priv = ieee80211_priv(dev);
2993 // struct ieee80211_device *ieee = priv->ieee80211;
2994 u32 ulRegRead;
2995 RT_STATUS rtStatus = RT_STATUS_SUCCESS;
2996 //u8 eRFPath;
2997 u8 tmpvalue;
2998 #ifdef RTL8192E
2999 u8 ICVersion,SwitchingRegulatorOutput;
3000 #endif
3001 bool bfirmwareok = true;
3002 #ifdef RTL8190P
3003 u8 ucRegRead;
3004 #endif
3005 u32 tmpRegA, tmpRegC, TempCCk;
3006 int i =0;
3007
3008 RT_TRACE(COMP_INIT, "====>%s()\n", __FUNCTION__);
3009 priv->being_init_adapter = true;
3010 rtl8192_pci_resetdescring(dev);
3011 // 2007/11/02 MH Before initalizing RF. We can not use FW to do RF-R/W.
3012 priv->Rf_Mode = RF_OP_By_SW_3wire;
3013 #ifdef RTL8192E
3014 //dPLL on
3015 if(priv->ResetProgress == RESET_TYPE_NORESET)
3016 {
3017 write_nic_byte(dev, ANAPAR, 0x37);
3018 // Accordign to designer's explain, LBUS active will never > 10ms. We delay 10ms
3019 // Joseph increae the time to prevent firmware download fail
3020 mdelay(500);
3021 }
3022 #endif
3023 //PlatformSleepUs(10000);
3024 // For any kind of InitializeAdapter process, we shall use system now!!
3025 priv->pFirmware->firmware_status = FW_STATUS_0_INIT;
3026
3027 // Set to eRfoff in order not to count receive count.
3028 if(priv->RegRfOff == TRUE)
3029 priv->ieee80211->eRFPowerState = eRfOff;
3030
3031 //
3032 //3 //Config CPUReset Register
3033 //3//
3034 //3 Firmware Reset Or Not
3035 ulRegRead = read_nic_dword(dev, CPU_GEN);
3036 if(priv->pFirmware->firmware_status == FW_STATUS_0_INIT)
3037 { //called from MPInitialized. do nothing
3038 ulRegRead |= CPU_GEN_SYSTEM_RESET;
3039 }else if(priv->pFirmware->firmware_status == FW_STATUS_5_READY)
3040 ulRegRead |= CPU_GEN_FIRMWARE_RESET; // Called from MPReset
3041 else
3042 RT_TRACE(COMP_ERR, "ERROR in %s(): undefined firmware state(%d)\n", __FUNCTION__, priv->pFirmware->firmware_status);
3043
3044 #ifdef RTL8190P
3045 //2008.06.03, for WOL 90 hw bug
3046 ulRegRead &= (~(CPU_GEN_GPIO_UART));
3047 #endif
3048
3049 write_nic_dword(dev, CPU_GEN, ulRegRead);
3050 //mdelay(100);
3051
3052 #ifdef RTL8192E
3053
3054 //3//
3055 //3 //Fix the issue of E-cut high temperature issue
3056 //3//
3057 // TODO: E cut only
3058 ICVersion = read_nic_byte(dev, IC_VERRSION);
3059 if(ICVersion >= 0x4) //E-cut only
3060 {
3061 // HW SD suggest that we should not wirte this register too often, so driver
3062 // should readback this register. This register will be modified only when
3063 // power on reset
3064 SwitchingRegulatorOutput = read_nic_byte(dev, SWREGULATOR);
3065 if(SwitchingRegulatorOutput != 0xb8)
3066 {
3067 write_nic_byte(dev, SWREGULATOR, 0xa8);
3068 mdelay(1);
3069 write_nic_byte(dev, SWREGULATOR, 0xb8);
3070 }
3071 }
3072 #endif
3073
3074
3075 //3//
3076 //3// Initialize BB before MAC
3077 //3//
3078 RT_TRACE(COMP_INIT, "BB Config Start!\n");
3079 rtStatus = rtl8192_BBConfig(dev);
3080 if(rtStatus != RT_STATUS_SUCCESS)
3081 {
3082 RT_TRACE(COMP_ERR, "BB Config failed\n");
3083 return rtStatus;
3084 }
3085 RT_TRACE(COMP_INIT,"BB Config Finished!\n");
3086
3087 //3//Set Loopback mode or Normal mode
3088 //3//
3089 //2006.12.13 by emily. Note!We should not merge these two CPU_GEN register writings
3090 // because setting of System_Reset bit reset MAC to default transmission mode.
3091 //Loopback mode or not
3092 priv->LoopbackMode = RTL819X_NO_LOOPBACK;
3093 //priv->LoopbackMode = RTL819X_MAC_LOOPBACK;
3094 if(priv->ResetProgress == RESET_TYPE_NORESET)
3095 {
3096 ulRegRead = read_nic_dword(dev, CPU_GEN);
3097 if(priv->LoopbackMode == RTL819X_NO_LOOPBACK)
3098 {
3099 ulRegRead = ((ulRegRead & CPU_GEN_NO_LOOPBACK_MSK) | CPU_GEN_NO_LOOPBACK_SET);
3100 }
3101 else if (priv->LoopbackMode == RTL819X_MAC_LOOPBACK )
3102 {
3103 ulRegRead |= CPU_CCK_LOOPBACK;
3104 }
3105 else
3106 {
3107 RT_TRACE(COMP_ERR,"Serious error: wrong loopback mode setting\n");
3108 }
3109
3110 //2008.06.03, for WOL
3111 //ulRegRead &= (~(CPU_GEN_GPIO_UART));
3112 write_nic_dword(dev, CPU_GEN, ulRegRead);
3113
3114 // 2006.11.29. After reset cpu, we sholud wait for a second, otherwise, it may fail to write registers. Emily
3115 udelay(500);
3116 }
3117 //3Set Hardware(Do nothing now)
3118 rtl8192_hwconfig(dev);
3119 //2=======================================================
3120 // Common Setting for all of the FPGA platform. (part 1)
3121 //2=======================================================
3122 // If there is changes, please make sure it applies to all of the FPGA version
3123 //3 Turn on Tx/Rx
3124 write_nic_byte(dev, CMDR, CR_RE|CR_TE);
3125
3126 //2Set Tx dma burst
3127 #ifdef RTL8190P
3128 write_nic_byte(dev, PCIF, ((MXDMA2_NoLimit<<MXDMA2_RX_SHIFT) |
3129 (MXDMA2_NoLimit<<MXDMA2_TX_SHIFT) |
3130 (1<<MULRW_SHIFT)));
3131 #else
3132 #ifdef RTL8192E
3133 write_nic_byte(dev, PCIF, ((MXDMA2_NoLimit<<MXDMA2_RX_SHIFT) |
3134 (MXDMA2_NoLimit<<MXDMA2_TX_SHIFT) ));
3135 #endif
3136 #endif
3137 //set IDR0 here
3138 write_nic_dword(dev, MAC0, ((u32*)dev->dev_addr)[0]);
3139 write_nic_word(dev, MAC4, ((u16*)(dev->dev_addr + 4))[0]);
3140 //set RCR
3141 write_nic_dword(dev, RCR, priv->ReceiveConfig);
3142
3143 //3 Initialize Number of Reserved Pages in Firmware Queue
3144 #ifdef TO_DO_LIST
3145 if(priv->bInHctTest)
3146 {
3147 PlatformEFIOWrite4Byte(Adapter, RQPN1, NUM_OF_PAGE_IN_FW_QUEUE_BK_DTM << RSVD_FW_QUEUE_PAGE_BK_SHIFT |
3148 NUM_OF_PAGE_IN_FW_QUEUE_BE_DTM << RSVD_FW_QUEUE_PAGE_BE_SHIFT |
3149 NUM_OF_PAGE_IN_FW_QUEUE_VI_DTM << RSVD_FW_QUEUE_PAGE_VI_SHIFT |
3150 NUM_OF_PAGE_IN_FW_QUEUE_VO_DTM <<RSVD_FW_QUEUE_PAGE_VO_SHIFT);
3151 PlatformEFIOWrite4Byte(Adapter, RQPN2, NUM_OF_PAGE_IN_FW_QUEUE_MGNT << RSVD_FW_QUEUE_PAGE_MGNT_SHIFT);
3152 PlatformEFIOWrite4Byte(Adapter, RQPN3, APPLIED_RESERVED_QUEUE_IN_FW|
3153 NUM_OF_PAGE_IN_FW_QUEUE_BCN<<RSVD_FW_QUEUE_PAGE_BCN_SHIFT|
3154 NUM_OF_PAGE_IN_FW_QUEUE_PUB_DTM<<RSVD_FW_QUEUE_PAGE_PUB_SHIFT);
3155 }
3156 else
3157 #endif
3158 {
3159 write_nic_dword(dev, RQPN1, NUM_OF_PAGE_IN_FW_QUEUE_BK << RSVD_FW_QUEUE_PAGE_BK_SHIFT |
3160 NUM_OF_PAGE_IN_FW_QUEUE_BE << RSVD_FW_QUEUE_PAGE_BE_SHIFT |
3161 NUM_OF_PAGE_IN_FW_QUEUE_VI << RSVD_FW_QUEUE_PAGE_VI_SHIFT |
3162 NUM_OF_PAGE_IN_FW_QUEUE_VO <<RSVD_FW_QUEUE_PAGE_VO_SHIFT);
3163 write_nic_dword(dev, RQPN2, NUM_OF_PAGE_IN_FW_QUEUE_MGNT << RSVD_FW_QUEUE_PAGE_MGNT_SHIFT);
3164 write_nic_dword(dev, RQPN3, APPLIED_RESERVED_QUEUE_IN_FW|
3165 NUM_OF_PAGE_IN_FW_QUEUE_BCN<<RSVD_FW_QUEUE_PAGE_BCN_SHIFT|
3166 NUM_OF_PAGE_IN_FW_QUEUE_PUB<<RSVD_FW_QUEUE_PAGE_PUB_SHIFT);
3167 }
3168
3169 rtl8192_tx_enable(dev);
3170 rtl8192_rx_enable(dev);
3171 //3Set Response Rate Setting Register
3172 // CCK rate is supported by default.
3173 // CCK rate will be filtered out only when associated AP does not support it.
3174 ulRegRead = (0xFFF00000 & read_nic_dword(dev, RRSR)) | RATE_ALL_OFDM_AG | RATE_ALL_CCK;
3175 write_nic_dword(dev, RRSR, ulRegRead);
3176 write_nic_dword(dev, RATR0+4*7, (RATE_ALL_OFDM_AG | RATE_ALL_CCK));
3177
3178 //2Set AckTimeout
3179 // TODO: (it value is only for FPGA version). need to be changed!!2006.12.18, by Emily
3180 write_nic_byte(dev, ACK_TIMEOUT, 0x30);
3181
3182 //rtl8192_actset_wirelessmode(dev,priv->RegWirelessMode);
3183 if(priv->ResetProgress == RESET_TYPE_NORESET)
3184 rtl8192_SetWirelessMode(dev, priv->ieee80211->mode);
3185 //-----------------------------------------------------------------------------
3186 // Set up security related. 070106, by rcnjko:
3187 // 1. Clear all H/W keys.
3188 // 2. Enable H/W encryption/decryption.
3189 //-----------------------------------------------------------------------------
3190 CamResetAllEntry(dev);
3191 {
3192 u8 SECR_value = 0x0;
3193 SECR_value |= SCR_TxEncEnable;
3194 SECR_value |= SCR_RxDecEnable;
3195 SECR_value |= SCR_NoSKMC;
3196 write_nic_byte(dev, SECR, SECR_value);
3197 }
3198 //3Beacon related
3199 write_nic_word(dev, ATIMWND, 2);
3200 write_nic_word(dev, BCN_INTERVAL, 100);
3201 for (i=0; i<QOS_QUEUE_NUM; i++)
3202 write_nic_dword(dev, WDCAPARA_ADD[i], 0x005e4332);
3203 //
3204 // Switching regulator controller: This is set temporarily.
3205 // It's not sure if this can be removed in the future.
3206 // PJ advised to leave it by default.
3207 //
3208 write_nic_byte(dev, 0xbe, 0xc0);
3209
3210 //2=======================================================
3211 // Set PHY related configuration defined in MAC register bank
3212 //2=======================================================
3213 rtl8192_phy_configmac(dev);
3214
3215 if (priv->card_8192_version > (u8) VERSION_8190_BD) {
3216 rtl8192_phy_getTxPower(dev);
3217 rtl8192_phy_setTxPower(dev, priv->chan);
3218 }
3219
3220 //if D or C cut
3221 tmpvalue = read_nic_byte(dev, IC_VERRSION);
3222 priv->IC_Cut = tmpvalue;
3223 RT_TRACE(COMP_INIT, "priv->IC_Cut = 0x%x\n", priv->IC_Cut);
3224 if(priv->IC_Cut >= IC_VersionCut_D)
3225 {
3226 //pHalData->bDcut = TRUE;
3227 if(priv->IC_Cut == IC_VersionCut_D)
3228 RT_TRACE(COMP_INIT, "D-cut\n");
3229 if(priv->IC_Cut == IC_VersionCut_E)
3230 {
3231 RT_TRACE(COMP_INIT, "E-cut\n");
3232 // HW SD suggest that we should not wirte this register too often, so driver
3233 // should readback this register. This register will be modified only when
3234 // power on reset
3235 }
3236 }
3237 else
3238 {
3239 //pHalData->bDcut = FALSE;
3240 RT_TRACE(COMP_INIT, "Before C-cut\n");
3241 }
3242
3243 #if 1
3244 //Firmware download
3245 RT_TRACE(COMP_INIT, "Load Firmware!\n");
3246 bfirmwareok = init_firmware(dev);
3247 if(bfirmwareok != true) {
3248 rtStatus = RT_STATUS_FAILURE;
3249 return rtStatus;
3250 }
3251 RT_TRACE(COMP_INIT, "Load Firmware finished!\n");
3252 #endif
3253 //RF config
3254 if(priv->ResetProgress == RESET_TYPE_NORESET)
3255 {
3256 RT_TRACE(COMP_INIT, "RF Config Started!\n");
3257 rtStatus = rtl8192_phy_RFConfig(dev);
3258 if(rtStatus != RT_STATUS_SUCCESS)
3259 {
3260 RT_TRACE(COMP_ERR, "RF Config failed\n");
3261 return rtStatus;
3262 }
3263 RT_TRACE(COMP_INIT, "RF Config Finished!\n");
3264 }
3265 rtl8192_phy_updateInitGain(dev);
3266
3267 /*---- Set CCK and OFDM Block "ON"----*/
3268 rtl8192_setBBreg(dev, rFPGA0_RFMOD, bCCKEn, 0x1);
3269 rtl8192_setBBreg(dev, rFPGA0_RFMOD, bOFDMEn, 0x1);
3270
3271 #ifdef RTL8192E
3272 //Enable Led
3273 write_nic_byte(dev, 0x87, 0x0);
3274 #endif
3275 #ifdef RTL8190P
3276 //2008.06.03, for WOL
3277 ucRegRead = read_nic_byte(dev, GPE);
3278 ucRegRead |= BIT0;
3279 write_nic_byte(dev, GPE, ucRegRead);
3280
3281 ucRegRead = read_nic_byte(dev, GPO);
3282 ucRegRead &= ~BIT0;
3283 write_nic_byte(dev, GPO, ucRegRead);
3284 #endif
3285
3286 //2=======================================================
3287 // RF Power Save
3288 //2=======================================================
3289 #ifdef ENABLE_IPS
3290
3291 {
3292 if(priv->RegRfOff == TRUE)
3293 { // User disable RF via registry.
3294 RT_TRACE((COMP_INIT|COMP_RF|COMP_POWER), "%s(): Turn off RF for RegRfOff ----------\n",__FUNCTION__);
3295 MgntActSet_RF_State(dev, eRfOff, RF_CHANGE_BY_SW);
3296 #if 0//cosa, ask SD3 willis and he doesn't know what is this for
3297 // Those action will be discard in MgntActSet_RF_State because off the same state
3298 for(eRFPath = 0; eRFPath <pHalData->NumTotalRFPath; eRFPath++)
3299 PHY_SetRFReg(Adapter, (RF90_RADIO_PATH_E)eRFPath, 0x4, 0xC00, 0x0);
3300 #endif
3301 }
3302 else if(priv->ieee80211->RfOffReason > RF_CHANGE_BY_PS)
3303 { // H/W or S/W RF OFF before sleep.
3304 RT_TRACE((COMP_INIT|COMP_RF|COMP_POWER), "%s(): Turn off RF for RfOffReason(%d) ----------\n", __FUNCTION__,priv->ieee80211->RfOffReason);
3305 MgntActSet_RF_State(dev, eRfOff, priv->ieee80211->RfOffReason);
3306 }
3307 else if(priv->ieee80211->RfOffReason >= RF_CHANGE_BY_IPS)
3308 { // H/W or S/W RF OFF before sleep.
3309 RT_TRACE((COMP_INIT|COMP_RF|COMP_POWER), "%s(): Turn off RF for RfOffReason(%d) ----------\n", __FUNCTION__,priv->ieee80211->RfOffReason);
3310 MgntActSet_RF_State(dev, eRfOff, priv->ieee80211->RfOffReason);
3311 }
3312 else
3313 {
3314 RT_TRACE((COMP_INIT|COMP_RF|COMP_POWER), "%s(): RF-ON \n",__FUNCTION__);
3315 priv->ieee80211->eRFPowerState = eRfOn;
3316 priv->ieee80211->RfOffReason = 0;
3317 //DrvIFIndicateCurrentPhyStatus(Adapter);
3318 // LED control
3319 //Adapter->HalFunc.LedControlHandler(Adapter, LED_CTL_POWER_ON);
3320
3321 //
3322 // If inactive power mode is enabled, disable rf while in disconnected state.
3323 // But we should still tell upper layer we are in rf on state.
3324 // 2007.07.16, by shien chang.
3325 //
3326 //if(!Adapter->bInHctTest)
3327 //IPSEnter(Adapter);
3328
3329 }
3330 }
3331 #endif
3332 if(1){
3333 #ifdef RTL8192E
3334 // We can force firmware to do RF-R/W
3335 if(priv->ieee80211->FwRWRF)
3336 priv->Rf_Mode = RF_OP_By_FW;
3337 else
3338 priv->Rf_Mode = RF_OP_By_SW_3wire;
3339 #else
3340 priv->Rf_Mode = RF_OP_By_SW_3wire;
3341 #endif
3342 }
3343 #ifdef RTL8190P
3344 if(priv->ResetProgress == RESET_TYPE_NORESET)
3345 {
3346 dm_initialize_txpower_tracking(dev);
3347
3348 tmpRegA= rtl8192_QueryBBReg(dev,rOFDM0_XATxIQImbalance,bMaskDWord);
3349 tmpRegC= rtl8192_QueryBBReg(dev,rOFDM0_XCTxIQImbalance,bMaskDWord);
3350
3351 if(priv->rf_type == RF_2T4R){
3352 for(i = 0; i<TxBBGainTableLength; i++)
3353 {
3354 if(tmpRegA == priv->txbbgain_table[i].txbbgain_value)
3355 {
3356 priv->rfa_txpowertrackingindex= (u8)i;
3357 priv->rfa_txpowertrackingindex_real= (u8)i;
3358 priv->rfa_txpowertracking_default = priv->rfa_txpowertrackingindex;
3359 break;
3360 }
3361 }
3362 }
3363 for(i = 0; i<TxBBGainTableLength; i++)
3364 {
3365 if(tmpRegC == priv->txbbgain_table[i].txbbgain_value)
3366 {
3367 priv->rfc_txpowertrackingindex= (u8)i;
3368 priv->rfc_txpowertrackingindex_real= (u8)i;
3369 priv->rfc_txpowertracking_default = priv->rfc_txpowertrackingindex;
3370 break;
3371 }
3372 }
3373 TempCCk = rtl8192_QueryBBReg(dev, rCCK0_TxFilter1, bMaskByte2);
3374
3375 for(i=0 ; i<CCKTxBBGainTableLength ; i++)
3376 {
3377 if(TempCCk == priv->cck_txbbgain_table[i].ccktxbb_valuearray[0])
3378 {
3379 priv->CCKPresentAttentuation_20Mdefault =(u8) i;
3380 break;
3381 }
3382 }
3383 priv->CCKPresentAttentuation_40Mdefault = 0;
3384 priv->CCKPresentAttentuation_difference = 0;
3385 priv->CCKPresentAttentuation = priv->CCKPresentAttentuation_20Mdefault;
3386 RT_TRACE(COMP_POWER_TRACKING, "priv->rfa_txpowertrackingindex_initial = %d\n", priv->rfa_txpowertrackingindex);
3387 RT_TRACE(COMP_POWER_TRACKING, "priv->rfa_txpowertrackingindex_real__initial = %d\n", priv->rfa_txpowertrackingindex_real);
3388 RT_TRACE(COMP_POWER_TRACKING, "priv->rfc_txpowertrackingindex_initial = %d\n", priv->rfc_txpowertrackingindex);
3389 RT_TRACE(COMP_POWER_TRACKING, "priv->rfc_txpowertrackingindex_real_initial = %d\n", priv->rfc_txpowertrackingindex_real);
3390 RT_TRACE(COMP_POWER_TRACKING, "priv->CCKPresentAttentuation_difference_initial = %d\n", priv->CCKPresentAttentuation_difference);
3391 RT_TRACE(COMP_POWER_TRACKING, "priv->CCKPresentAttentuation_initial = %d\n", priv->CCKPresentAttentuation);
3392 }
3393 #else
3394 #ifdef RTL8192E
3395 if(priv->ResetProgress == RESET_TYPE_NORESET)
3396 {
3397 dm_initialize_txpower_tracking(dev);
3398
3399 if(priv->IC_Cut >= IC_VersionCut_D)
3400 {
3401 tmpRegA= rtl8192_QueryBBReg(dev,rOFDM0_XATxIQImbalance,bMaskDWord);
3402 tmpRegC= rtl8192_QueryBBReg(dev,rOFDM0_XCTxIQImbalance,bMaskDWord);
3403 for(i = 0; i<TxBBGainTableLength; i++)
3404 {
3405 if(tmpRegA == priv->txbbgain_table[i].txbbgain_value)
3406 {
3407 priv->rfa_txpowertrackingindex= (u8)i;
3408 priv->rfa_txpowertrackingindex_real= (u8)i;
3409 priv->rfa_txpowertracking_default = priv->rfa_txpowertrackingindex;
3410 break;
3411 }
3412 }
3413
3414 TempCCk = rtl8192_QueryBBReg(dev, rCCK0_TxFilter1, bMaskByte2);
3415
3416 for(i=0 ; i<CCKTxBBGainTableLength ; i++)
3417 {
3418 if(TempCCk == priv->cck_txbbgain_table[i].ccktxbb_valuearray[0])
3419 {
3420 priv->CCKPresentAttentuation_20Mdefault =(u8) i;
3421 break;
3422 }
3423 }
3424 priv->CCKPresentAttentuation_40Mdefault = 0;
3425 priv->CCKPresentAttentuation_difference = 0;
3426 priv->CCKPresentAttentuation = priv->CCKPresentAttentuation_20Mdefault;
3427 RT_TRACE(COMP_POWER_TRACKING, "priv->rfa_txpowertrackingindex_initial = %d\n", priv->rfa_txpowertrackingindex);
3428 RT_TRACE(COMP_POWER_TRACKING, "priv->rfa_txpowertrackingindex_real__initial = %d\n", priv->rfa_txpowertrackingindex_real);
3429 RT_TRACE(COMP_POWER_TRACKING, "priv->CCKPresentAttentuation_difference_initial = %d\n", priv->CCKPresentAttentuation_difference);
3430 RT_TRACE(COMP_POWER_TRACKING, "priv->CCKPresentAttentuation_initial = %d\n", priv->CCKPresentAttentuation);
3431 priv->btxpower_tracking = FALSE;//TEMPLY DISABLE
3432 }
3433 }
3434 #endif
3435 #endif
3436 rtl8192_irq_enable(dev);
3437 priv->being_init_adapter = false;
3438 return rtStatus;
3439
3440 }
3441
3442 static void rtl8192_prepare_beacon(struct r8192_priv *priv)
3443 {
3444 struct sk_buff *skb;
3445 //unsigned long flags;
3446 cb_desc *tcb_desc;
3447
3448 skb = ieee80211_get_beacon(priv->ieee80211);
3449 tcb_desc = (cb_desc *)(skb->cb + 8);
3450 //spin_lock_irqsave(&priv->tx_lock,flags);
3451 /* prepare misc info for the beacon xmit */
3452 tcb_desc->queue_index = BEACON_QUEUE;
3453 /* IBSS does not support HT yet, use 1M defaultly */
3454 tcb_desc->data_rate = 2;
3455 tcb_desc->RATRIndex = 7;
3456 tcb_desc->bTxDisableRateFallBack = 1;
3457 tcb_desc->bTxUseDriverAssingedRate = 1;
3458
3459 skb_push(skb, priv->ieee80211->tx_headroom);
3460 if(skb){
3461 rtl8192_tx(priv->ieee80211->dev,skb);
3462 }
3463 //spin_unlock_irqrestore (&priv->tx_lock, flags);
3464 }
3465
3466
3467 /*
3468 * configure registers for beacon tx and enables it via
3469 * rtl8192_beacon_tx_enable(). rtl8192_beacon_tx_disable() might
3470 * be used to stop beacon transmission
3471 */
3472 static void rtl8192_start_beacon(struct net_device *dev)
3473 {
3474 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
3475 struct ieee80211_network *net = &priv->ieee80211->current_network;
3476 u16 BcnTimeCfg = 0;
3477 u16 BcnCW = 6;
3478 u16 BcnIFS = 0xf;
3479
3480 DMESG("Enabling beacon TX");
3481 //rtl8192_prepare_beacon(dev);
3482 rtl8192_irq_disable(dev);
3483 //rtl8192_beacon_tx_enable(dev);
3484
3485 /* ATIM window */
3486 write_nic_word(dev, ATIMWND, 2);
3487
3488 /* Beacon interval (in unit of TU) */
3489 write_nic_word(dev, BCN_INTERVAL, net->beacon_interval);
3490
3491 /*
3492 * DrvErlyInt (in unit of TU).
3493 * (Time to send interrupt to notify driver to c
3494 * hange beacon content)
3495 * */
3496 write_nic_word(dev, BCN_DRV_EARLY_INT, 10);
3497
3498 /*
3499 * BcnDMATIM(in unit of us).
3500 * Indicates the time before TBTT to perform beacon queue DMA
3501 * */
3502 write_nic_word(dev, BCN_DMATIME, 256);
3503
3504 /*
3505 * Force beacon frame transmission even after receiving
3506 * beacon frame from other ad hoc STA
3507 * */
3508 write_nic_byte(dev, BCN_ERR_THRESH, 100);
3509
3510 /* Set CW and IFS */
3511 BcnTimeCfg |= BcnCW<<BCN_TCFG_CW_SHIFT;
3512 BcnTimeCfg |= BcnIFS<<BCN_TCFG_IFS;
3513 write_nic_word(dev, BCN_TCFG, BcnTimeCfg);
3514
3515
3516 /* enable the interrupt for ad-hoc process */
3517 rtl8192_irq_enable(dev);
3518 }
3519
3520 static bool HalTxCheckStuck8190Pci(struct net_device *dev)
3521 {
3522 u16 RegTxCounter = read_nic_word(dev, 0x128);
3523 struct r8192_priv *priv = ieee80211_priv(dev);
3524 bool bStuck = FALSE;
3525 RT_TRACE(COMP_RESET,"%s():RegTxCounter is %d,TxCounter is %d\n",__FUNCTION__,RegTxCounter,priv->TxCounter);
3526 if(priv->TxCounter==RegTxCounter)
3527 bStuck = TRUE;
3528
3529 priv->TxCounter = RegTxCounter;
3530
3531 return bStuck;
3532 }
3533
3534 /*
3535 * Assumption: RT_TX_SPINLOCK is acquired.
3536 */
3537 static RESET_TYPE
3538 TxCheckStuck(struct net_device *dev)
3539 {
3540 struct r8192_priv *priv = ieee80211_priv(dev);
3541 u8 ResetThreshold = NIC_SEND_HANG_THRESHOLD_POWERSAVE;
3542 bool bCheckFwTxCnt = false;
3543
3544 //
3545 // Decide Stuch threshold according to current power save mode
3546 //
3547 switch (priv->ieee80211->dot11PowerSaveMode)
3548 {
3549 // The threshold value may required to be adjusted .
3550 case eActive: // Active/Continuous access.
3551 ResetThreshold = NIC_SEND_HANG_THRESHOLD_NORMAL;
3552 break;
3553 case eMaxPs: // Max power save mode.
3554 ResetThreshold = NIC_SEND_HANG_THRESHOLD_POWERSAVE;
3555 break;
3556 case eFastPs: // Fast power save mode.
3557 ResetThreshold = NIC_SEND_HANG_THRESHOLD_POWERSAVE;
3558 break;
3559 }
3560
3561 #if 1
3562 if(bCheckFwTxCnt)
3563 {
3564 if(HalTxCheckStuck8190Pci(dev))
3565 {
3566 RT_TRACE(COMP_RESET, "TxCheckStuck(): Fw indicates no Tx condition! \n");
3567 return RESET_TYPE_SILENT;
3568 }
3569 }
3570 #endif
3571 return RESET_TYPE_NORESET;
3572 }
3573
3574
3575 static bool HalRxCheckStuck8190Pci(struct net_device *dev)
3576 {
3577 struct r8192_priv *priv = ieee80211_priv(dev);
3578 u16 RegRxCounter = read_nic_word(dev, 0x130);
3579 bool bStuck = FALSE;
3580 static u8 rx_chk_cnt = 0;
3581 RT_TRACE(COMP_RESET,"%s(): RegRxCounter is %d,RxCounter is %d\n",__FUNCTION__,RegRxCounter,priv->RxCounter);
3582 // If rssi is small, we should check rx for long time because of bad rx.
3583 // or maybe it will continuous silent reset every 2 seconds.
3584 rx_chk_cnt++;
3585 if(priv->undecorated_smoothed_pwdb >= (RateAdaptiveTH_High+5))
3586 {
3587 rx_chk_cnt = 0; //high rssi, check rx stuck right now.
3588 }
3589 else if(priv->undecorated_smoothed_pwdb < (RateAdaptiveTH_High+5) &&
3590 ((priv->CurrentChannelBW!=HT_CHANNEL_WIDTH_20&&priv->undecorated_smoothed_pwdb>=RateAdaptiveTH_Low_40M) ||
3591 (priv->CurrentChannelBW==HT_CHANNEL_WIDTH_20&&priv->undecorated_smoothed_pwdb>=RateAdaptiveTH_Low_20M)) )
3592
3593 {
3594 if(rx_chk_cnt < 2)
3595 {
3596 return bStuck;
3597 }
3598 else
3599 {
3600 rx_chk_cnt = 0;
3601 }
3602 }
3603 else if(((priv->CurrentChannelBW!=HT_CHANNEL_WIDTH_20&&priv->undecorated_smoothed_pwdb<RateAdaptiveTH_Low_40M) ||
3604 (priv->CurrentChannelBW==HT_CHANNEL_WIDTH_20&&priv->undecorated_smoothed_pwdb<RateAdaptiveTH_Low_20M)) &&
3605 priv->undecorated_smoothed_pwdb >= VeryLowRSSI)
3606 {
3607 if(rx_chk_cnt < 4)
3608 {
3609 //DbgPrint("RSSI < %d && RSSI >= %d, no check this time \n", RateAdaptiveTH_Low, VeryLowRSSI);
3610 return bStuck;
3611 }
3612 else
3613 {
3614 rx_chk_cnt = 0;
3615 //DbgPrint("RSSI < %d && RSSI >= %d, check this time \n", RateAdaptiveTH_Low, VeryLowRSSI);
3616 }
3617 }
3618 else
3619 {
3620 if(rx_chk_cnt < 8)
3621 {
3622 //DbgPrint("RSSI <= %d, no check this time \n", VeryLowRSSI);
3623 return bStuck;
3624 }
3625 else
3626 {
3627 rx_chk_cnt = 0;
3628 //DbgPrint("RSSI <= %d, check this time \n", VeryLowRSSI);
3629 }
3630 }
3631 if(priv->RxCounter==RegRxCounter)
3632 bStuck = TRUE;
3633
3634 priv->RxCounter = RegRxCounter;
3635
3636 return bStuck;
3637 }
3638
3639 static RESET_TYPE RxCheckStuck(struct net_device *dev)
3640 {
3641
3642 if(HalRxCheckStuck8190Pci(dev))
3643 {
3644 RT_TRACE(COMP_RESET, "RxStuck Condition\n");
3645 return RESET_TYPE_SILENT;
3646 }
3647
3648 return RESET_TYPE_NORESET;
3649 }
3650
3651 static RESET_TYPE
3652 rtl819x_ifcheck_resetornot(struct net_device *dev)
3653 {
3654 struct r8192_priv *priv = ieee80211_priv(dev);
3655 RESET_TYPE TxResetType = RESET_TYPE_NORESET;
3656 RESET_TYPE RxResetType = RESET_TYPE_NORESET;
3657 RT_RF_POWER_STATE rfState;
3658
3659 rfState = priv->ieee80211->eRFPowerState;
3660
3661 TxResetType = TxCheckStuck(dev);
3662 #if 1
3663 if( rfState != eRfOff &&
3664 /*ADAPTER_TEST_STATUS_FLAG(Adapter, ADAPTER_STATUS_FW_DOWNLOAD_FAILURE)) &&*/
3665 (priv->ieee80211->iw_mode != IW_MODE_ADHOC))
3666 {
3667 // If driver is in the status of firmware download failure , driver skips RF initialization and RF is
3668 // in turned off state. Driver should check whether Rx stuck and do silent reset. And
3669 // if driver is in firmware download failure status, driver should initialize RF in the following
3670 // silent reset procedure Emily, 2008.01.21
3671
3672 // Driver should not check RX stuck in IBSS mode because it is required to
3673 // set Check BSSID in order to send beacon, however, if check BSSID is
3674 // set, STA cannot hear any packet a all. Emily, 2008.04.12
3675 RxResetType = RxCheckStuck(dev);
3676 }
3677 #endif
3678
3679 RT_TRACE(COMP_RESET,"%s(): TxResetType is %d, RxResetType is %d\n",__FUNCTION__,TxResetType,RxResetType);
3680 if(TxResetType==RESET_TYPE_NORMAL || RxResetType==RESET_TYPE_NORMAL)
3681 return RESET_TYPE_NORMAL;
3682 else if(TxResetType==RESET_TYPE_SILENT || RxResetType==RESET_TYPE_SILENT)
3683 return RESET_TYPE_SILENT;
3684 else
3685 return RESET_TYPE_NORESET;
3686
3687 }
3688
3689
3690 static void CamRestoreAllEntry(struct net_device *dev)
3691 {
3692 u8 EntryId = 0;
3693 struct r8192_priv *priv = ieee80211_priv(dev);
3694 const u8* MacAddr = priv->ieee80211->current_network.bssid;
3695
3696 static const u8 CAM_CONST_ADDR[4][6] = {
3697 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
3698 {0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
3699 {0x00, 0x00, 0x00, 0x00, 0x00, 0x02},
3700 {0x00, 0x00, 0x00, 0x00, 0x00, 0x03}};
3701 static const u8 CAM_CONST_BROAD[] =
3702 {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
3703
3704 RT_TRACE(COMP_SEC, "CamRestoreAllEntry: \n");
3705
3706
3707 if ((priv->ieee80211->pairwise_key_type == KEY_TYPE_WEP40)||
3708 (priv->ieee80211->pairwise_key_type == KEY_TYPE_WEP104))
3709 {
3710
3711 for(EntryId=0; EntryId<4; EntryId++)
3712 {
3713 {
3714 MacAddr = CAM_CONST_ADDR[EntryId];
3715 setKey(dev,
3716 EntryId ,
3717 EntryId,
3718 priv->ieee80211->pairwise_key_type,
3719 MacAddr,
3720 0,
3721 NULL);
3722 }
3723 }
3724
3725 }
3726 else if(priv->ieee80211->pairwise_key_type == KEY_TYPE_TKIP)
3727 {
3728
3729 {
3730 if(priv->ieee80211->iw_mode == IW_MODE_ADHOC)
3731 setKey(dev,
3732 4,
3733 0,
3734 priv->ieee80211->pairwise_key_type,
3735 (u8*)dev->dev_addr,
3736 0,
3737 NULL);
3738 else
3739 setKey(dev,
3740 4,
3741 0,
3742 priv->ieee80211->pairwise_key_type,
3743 MacAddr,
3744 0,
3745 NULL);
3746 }
3747 }
3748 else if(priv->ieee80211->pairwise_key_type == KEY_TYPE_CCMP)
3749 {
3750
3751 {
3752 if(priv->ieee80211->iw_mode == IW_MODE_ADHOC)
3753 setKey(dev,
3754 4,
3755 0,
3756 priv->ieee80211->pairwise_key_type,
3757 (u8*)dev->dev_addr,
3758 0,
3759 NULL);
3760 else
3761 setKey(dev,
3762 4,
3763 0,
3764 priv->ieee80211->pairwise_key_type,
3765 MacAddr,
3766 0,
3767 NULL);
3768 }
3769 }
3770
3771
3772
3773 if(priv->ieee80211->group_key_type == KEY_TYPE_TKIP)
3774 {
3775 MacAddr = CAM_CONST_BROAD;
3776 for(EntryId=1 ; EntryId<4 ; EntryId++)
3777 {
3778 {
3779 setKey(dev,
3780 EntryId,
3781 EntryId,
3782 priv->ieee80211->group_key_type,
3783 MacAddr,
3784 0,
3785 NULL);
3786 }
3787 }
3788 if(priv->ieee80211->iw_mode == IW_MODE_ADHOC)
3789 setKey(dev,
3790 0,
3791 0,
3792 priv->ieee80211->group_key_type,
3793 CAM_CONST_ADDR[0],
3794 0,
3795 NULL);
3796 }
3797 else if(priv->ieee80211->group_key_type == KEY_TYPE_CCMP)
3798 {
3799 MacAddr = CAM_CONST_BROAD;
3800 for(EntryId=1; EntryId<4 ; EntryId++)
3801 {
3802 {
3803 setKey(dev,
3804 EntryId ,
3805 EntryId,
3806 priv->ieee80211->group_key_type,
3807 MacAddr,
3808 0,
3809 NULL);
3810 }
3811 }
3812
3813 if(priv->ieee80211->iw_mode == IW_MODE_ADHOC)
3814 setKey(dev,
3815 0 ,
3816 0,
3817 priv->ieee80211->group_key_type,
3818 CAM_CONST_ADDR[0],
3819 0,
3820 NULL);
3821 }
3822 }
3823
3824 /*
3825 * This function is used to fix Tx/Rx stop bug temporarily.
3826 * This function will do "system reset" to NIC when Tx or Rx is stuck.
3827 * The method checking Tx/Rx stuck of this function is supported by FW,
3828 * which reports Tx and Rx counter to register 0x128 and 0x130.
3829 */
3830 static void rtl819x_ifsilentreset(struct net_device *dev)
3831 {
3832 struct r8192_priv *priv = ieee80211_priv(dev);
3833 u8 reset_times = 0;
3834 int reset_status = 0;
3835 struct ieee80211_device *ieee = priv->ieee80211;
3836
3837
3838 return;
3839
3840 // 2007.07.20. If we need to check CCK stop, please uncomment this line.
3841 //bStuck = Adapter->HalFunc.CheckHWStopHandler(Adapter);
3842
3843 if(priv->ResetProgress==RESET_TYPE_NORESET)
3844 {
3845 RESET_START:
3846 #ifdef ENABLE_LPS
3847 //LZM for PS-Poll AID issue. 090429
3848 if(priv->ieee80211->state == IEEE80211_LINKED)
3849 LeisurePSLeave(dev);
3850 #endif
3851
3852 RT_TRACE(COMP_RESET,"=========>Reset progress!! \n");
3853
3854 // Set the variable for reset.
3855 priv->ResetProgress = RESET_TYPE_SILENT;
3856 // rtl8192_close(dev);
3857 #if 1
3858 down(&priv->wx_sem);
3859 if(priv->up == 0)
3860 {
3861 RT_TRACE(COMP_ERR,"%s():the driver is not up! return\n",__FUNCTION__);
3862 up(&priv->wx_sem);
3863 return ;
3864 }
3865 priv->up = 0;
3866 RT_TRACE(COMP_RESET,"%s():======>start to down the driver\n",__FUNCTION__);
3867 if(!netif_queue_stopped(dev))
3868 netif_stop_queue(dev);
3869
3870 dm_backup_dynamic_mechanism_state(dev);
3871
3872 rtl8192_irq_disable(dev);
3873 rtl8192_cancel_deferred_work(priv);
3874 deinit_hal_dm(dev);
3875 del_timer_sync(&priv->watch_dog_timer);
3876 ieee->sync_scan_hurryup = 1;
3877 if(ieee->state == IEEE80211_LINKED)
3878 {
3879 down(&ieee->wx_sem);
3880 printk("ieee->state is IEEE80211_LINKED\n");
3881 ieee80211_stop_send_beacons(priv->ieee80211);
3882 del_timer_sync(&ieee->associate_timer);
3883 cancel_delayed_work(&ieee->associate_retry_wq);
3884 ieee80211_stop_scan(ieee);
3885 up(&ieee->wx_sem);
3886 }
3887 else{
3888 printk("ieee->state is NOT LINKED\n");
3889 ieee80211_softmac_stop_protocol(priv->ieee80211,true);
3890 }
3891 rtl8192_halt_adapter(dev, true);
3892 up(&priv->wx_sem);
3893 RT_TRACE(COMP_RESET,"%s():<==========down process is finished\n",__FUNCTION__);
3894 RT_TRACE(COMP_RESET,"%s():===========>start to up the driver\n",__FUNCTION__);
3895 reset_status = _rtl8192_up(dev);
3896
3897 RT_TRACE(COMP_RESET,"%s():<===========up process is finished\n",__FUNCTION__);
3898 if(reset_status == -1)
3899 {
3900 if(reset_times < 3)
3901 {
3902 reset_times++;
3903 goto RESET_START;
3904 }
3905 else
3906 {
3907 RT_TRACE(COMP_ERR," ERR!!! %s(): Reset Failed!!\n",__FUNCTION__);
3908 }
3909 }
3910 #endif
3911 ieee->is_silent_reset = 1;
3912 #if 1
3913 EnableHWSecurityConfig8192(dev);
3914 #if 1
3915 if(ieee->state == IEEE80211_LINKED && ieee->iw_mode == IW_MODE_INFRA)
3916 {
3917 ieee->set_chan(ieee->dev, ieee->current_network.channel);
3918
3919 #if 1
3920 queue_work(ieee->wq, &ieee->associate_complete_wq);
3921 #endif
3922
3923 }
3924 else if(ieee->state == IEEE80211_LINKED && ieee->iw_mode == IW_MODE_ADHOC)
3925 {
3926 ieee->set_chan(ieee->dev, ieee->current_network.channel);
3927 ieee->link_change(ieee->dev);
3928
3929 // notify_wx_assoc_event(ieee);
3930
3931 ieee80211_start_send_beacons(ieee);
3932
3933 if (ieee->data_hard_resume)
3934 ieee->data_hard_resume(ieee->dev);
3935 netif_carrier_on(ieee->dev);
3936 }
3937 #endif
3938
3939 CamRestoreAllEntry(dev);
3940
3941 // Restore the previous setting for all dynamic mechanism
3942 dm_restore_dynamic_mechanism_state(dev);
3943
3944 priv->ResetProgress = RESET_TYPE_NORESET;
3945 priv->reset_count++;
3946
3947 priv->bForcedSilentReset =false;
3948 priv->bResetInProgress = false;
3949
3950 // For test --> force write UFWP.
3951 write_nic_byte(dev, UFWP, 1);
3952 RT_TRACE(COMP_RESET, "Reset finished!! ====>[%d]\n", priv->reset_count);
3953 #endif
3954 }
3955 }
3956
3957 #ifdef ENABLE_IPS
3958 void InactivePsWorkItemCallback(struct net_device *dev)
3959 {
3960 struct r8192_priv *priv = ieee80211_priv(dev);
3961 PRT_POWER_SAVE_CONTROL pPSC = (PRT_POWER_SAVE_CONTROL)(&(priv->ieee80211->PowerSaveControl));
3962
3963 RT_TRACE(COMP_POWER, "InactivePsWorkItemCallback() ---------> \n");
3964 //
3965 // This flag "bSwRfProcessing", indicates the status of IPS procedure, should be set if the IPS workitem
3966 // is really scheduled.
3967 // The old code, sets this flag before scheduling the IPS workitem and however, at the same time the
3968 // previous IPS workitem did not end yet, fails to schedule the current workitem. Thus, bSwRfProcessing
3969 // blocks the IPS procedure of switching RF.
3970 // By Bruce, 2007-12-25.
3971 //
3972 pPSC->bSwRfProcessing = TRUE;
3973
3974 RT_TRACE(COMP_RF, "InactivePsWorkItemCallback(): Set RF to %s.\n",
3975 pPSC->eInactivePowerState == eRfOff?"OFF":"ON");
3976
3977
3978 MgntActSet_RF_State(dev, pPSC->eInactivePowerState, RF_CHANGE_BY_IPS);
3979
3980 //
3981 // To solve CAM values miss in RF OFF, rewrite CAM values after RF ON. By Bruce, 2007-09-20.
3982 //
3983 pPSC->bSwRfProcessing = FALSE;
3984 RT_TRACE(COMP_POWER, "InactivePsWorkItemCallback() <--------- \n");
3985 }
3986
3987 #ifdef ENABLE_LPS
3988 /* Change current and default preamble mode. */
3989 bool MgntActSet_802_11_PowerSaveMode(struct net_device *dev, u8 rtPsMode)
3990 {
3991 struct r8192_priv *priv = ieee80211_priv(dev);
3992
3993 // Currently, we do not change power save mode on IBSS mode.
3994 if(priv->ieee80211->iw_mode == IW_MODE_ADHOC)
3995 {
3996 return false;
3997 }
3998
3999 //
4000 // <RJ_NOTE> If we make HW to fill up the PwrMgt bit for us,
4001 // some AP will not response to our mgnt frames with PwrMgt bit set,
4002 // e.g. cannot associate the AP.
4003 // So I commented out it. 2005.02.16, by rcnjko.
4004 //
4005 // // Change device's power save mode.
4006 // Adapter->HalFunc.SetPSModeHandler( Adapter, rtPsMode );
4007
4008 // Update power save mode configured.
4009 //RT_TRACE(COMP_LPS,"%s(): set ieee->ps = %x\n",__FUNCTION__,rtPsMode);
4010 if(!priv->ps_force) {
4011 priv->ieee80211->ps = rtPsMode;
4012 }
4013
4014 // Awake immediately
4015 if(priv->ieee80211->sta_sleep != 0 && rtPsMode == IEEE80211_PS_DISABLED)
4016 {
4017 unsigned long flags;
4018
4019 //PlatformSetTimer(Adapter, &(pMgntInfo->AwakeTimer), 0);
4020 // Notify the AP we awke.
4021 rtl8192_hw_wakeup(dev);
4022 priv->ieee80211->sta_sleep = 0;
4023
4024 spin_lock_irqsave(&(priv->ieee80211->mgmt_tx_lock), flags);
4025 printk("LPS leave: notify AP we are awaked ++++++++++ SendNullFunctionData\n");
4026 ieee80211_sta_ps_send_null_frame(priv->ieee80211, 0);
4027 spin_unlock_irqrestore(&(priv->ieee80211->mgmt_tx_lock), flags);
4028 }
4029
4030 return true;
4031 }
4032
4033 /* Enter the leisure power save mode. */
4034 void LeisurePSEnter(struct net_device *dev)
4035 {
4036 struct r8192_priv *priv = ieee80211_priv(dev);
4037 PRT_POWER_SAVE_CONTROL pPSC = (PRT_POWER_SAVE_CONTROL)(&(priv->ieee80211->PowerSaveControl));
4038
4039 //RT_TRACE(COMP_PS, "LeisurePSEnter()...\n");
4040 //RT_TRACE(COMP_PS, "pPSC->bLeisurePs = %d, ieee->ps = %d,pPSC->LpsIdleCount is %d,RT_CHECK_FOR_HANG_PERIOD is %d\n",
4041 // pPSC->bLeisurePs, priv->ieee80211->ps,pPSC->LpsIdleCount,RT_CHECK_FOR_HANG_PERIOD);
4042
4043 if(!((priv->ieee80211->iw_mode == IW_MODE_INFRA) &&
4044 (priv->ieee80211->state == IEEE80211_LINKED)) ||
4045 (priv->ieee80211->iw_mode == IW_MODE_ADHOC) ||
4046 (priv->ieee80211->iw_mode == IW_MODE_MASTER))
4047 return;
4048
4049 if (pPSC->bLeisurePs)
4050 {
4051 // Idle for a while if we connect to AP a while ago.
4052 if(pPSC->LpsIdleCount >= RT_CHECK_FOR_HANG_PERIOD) // 4 Sec
4053 {
4054
4055 if(priv->ieee80211->ps == IEEE80211_PS_DISABLED)
4056 {
4057
4058 //RT_TRACE(COMP_LPS, "LeisurePSEnter(): Enter 802.11 power save mode...\n");
4059 MgntActSet_802_11_PowerSaveMode(dev, IEEE80211_PS_MBCAST|IEEE80211_PS_UNICAST);
4060
4061 }
4062 }
4063 else
4064 pPSC->LpsIdleCount++;
4065 }
4066 }
4067
4068
4069 /* Leave leisure power save mode. */
4070 void LeisurePSLeave(struct net_device *dev)
4071 {
4072 struct r8192_priv *priv = ieee80211_priv(dev);
4073 PRT_POWER_SAVE_CONTROL pPSC = (PRT_POWER_SAVE_CONTROL)(&(priv->ieee80211->PowerSaveControl));
4074
4075 if (pPSC->bLeisurePs)
4076 {
4077 if(priv->ieee80211->ps != IEEE80211_PS_DISABLED)
4078 {
4079 // move to lps_wakecomplete()
4080 //RT_TRACE(COMP_LPS, "LeisurePSLeave(): Busy Traffic , Leave 802.11 power save..\n");
4081 MgntActSet_802_11_PowerSaveMode(dev, IEEE80211_PS_DISABLED);
4082
4083 }
4084 }
4085 }
4086 #endif
4087
4088
4089 /* Enter the inactive power save mode. RF will be off */
4090 void
4091 IPSEnter(struct net_device *dev)
4092 {
4093 struct r8192_priv *priv = ieee80211_priv(dev);
4094 PRT_POWER_SAVE_CONTROL pPSC = (PRT_POWER_SAVE_CONTROL)(&(priv->ieee80211->PowerSaveControl));
4095 RT_RF_POWER_STATE rtState;
4096
4097 if (pPSC->bInactivePs)
4098 {
4099 rtState = priv->ieee80211->eRFPowerState;
4100 //
4101 // Added by Bruce, 2007-12-25.
4102 // Do not enter IPS in the following conditions:
4103 // (1) RF is already OFF or Sleep
4104 // (2) bSwRfProcessing (indicates the IPS is still under going)
4105 // (3) Connectted (only disconnected can trigger IPS)
4106 // (4) IBSS (send Beacon)
4107 // (5) AP mode (send Beacon)
4108 //
4109 if (rtState == eRfOn && !pPSC->bSwRfProcessing
4110 && (priv->ieee80211->state != IEEE80211_LINKED) )
4111 {
4112 RT_TRACE(COMP_RF,"IPSEnter(): Turn off RF.\n");
4113 //printk("IPSEnter(): Turn off RF.\n");
4114 pPSC->eInactivePowerState = eRfOff;
4115 // queue_work(priv->priv_wq,&(pPSC->InactivePsWorkItem));
4116 InactivePsWorkItemCallback(dev);
4117 }
4118 }
4119 }
4120
4121 //
4122 // Description:
4123 // Leave the inactive power save mode, RF will be on.
4124 // 2007.08.17, by shien chang.
4125 //
4126 void
4127 IPSLeave(struct net_device *dev)
4128 {
4129 struct r8192_priv *priv = ieee80211_priv(dev);
4130 PRT_POWER_SAVE_CONTROL pPSC = (PRT_POWER_SAVE_CONTROL)(&(priv->ieee80211->PowerSaveControl));
4131 RT_RF_POWER_STATE rtState;
4132
4133 if (pPSC->bInactivePs)
4134 {
4135 rtState = priv->ieee80211->eRFPowerState;
4136 if (rtState != eRfOn && !pPSC->bSwRfProcessing && priv->ieee80211->RfOffReason <= RF_CHANGE_BY_IPS)
4137 {
4138 RT_TRACE(COMP_POWER, "IPSLeave(): Turn on RF.\n");
4139 //printk("IPSLeave(): Turn on RF.\n");
4140 pPSC->eInactivePowerState = eRfOn;
4141 // queue_work(priv->priv_wq,&(pPSC->InactivePsWorkItem));
4142 InactivePsWorkItemCallback(dev);
4143 }
4144 }
4145 }
4146
4147 void IPSLeave_wq(void *data)
4148 {
4149 struct ieee80211_device *ieee = container_of(data,struct ieee80211_device,ips_leave_wq);
4150 struct net_device *dev = ieee->dev;
4151
4152 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
4153 down(&priv->ieee80211->ips_sem);
4154 IPSLeave(dev);
4155 up(&priv->ieee80211->ips_sem);
4156 }
4157
4158 void ieee80211_ips_leave_wq(struct net_device *dev)
4159 {
4160 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
4161 RT_RF_POWER_STATE rtState;
4162 rtState = priv->ieee80211->eRFPowerState;
4163
4164 if(priv->ieee80211->PowerSaveControl.bInactivePs){
4165 if(rtState == eRfOff){
4166 if(priv->ieee80211->RfOffReason > RF_CHANGE_BY_IPS)
4167 {
4168 RT_TRACE(COMP_ERR, "%s(): RF is OFF.\n",__FUNCTION__);
4169 return;
4170 }
4171 else{
4172 printk("=========>%s(): IPSLeave\n",__FUNCTION__);
4173 queue_work(priv->ieee80211->wq,&priv->ieee80211->ips_leave_wq);
4174 }
4175 }
4176 }
4177 }
4178 //added by amy 090331 end
4179 void ieee80211_ips_leave(struct net_device *dev)
4180 {
4181 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
4182 down(&priv->ieee80211->ips_sem);
4183 IPSLeave(dev);
4184 up(&priv->ieee80211->ips_sem);
4185 }
4186 #endif
4187
4188 static void rtl819x_update_rxcounts(
4189 struct r8192_priv *priv,
4190 u32* TotalRxBcnNum,
4191 u32* TotalRxDataNum
4192 )
4193 {
4194 u16 SlotIndex;
4195 u8 i;
4196
4197 *TotalRxBcnNum = 0;
4198 *TotalRxDataNum = 0;
4199
4200 SlotIndex = (priv->ieee80211->LinkDetectInfo.SlotIndex++)%(priv->ieee80211->LinkDetectInfo.SlotNum);
4201 priv->ieee80211->LinkDetectInfo.RxBcnNum[SlotIndex] = priv->ieee80211->LinkDetectInfo.NumRecvBcnInPeriod;
4202 priv->ieee80211->LinkDetectInfo.RxDataNum[SlotIndex] = priv->ieee80211->LinkDetectInfo.NumRecvDataInPeriod;
4203 for( i=0; i<priv->ieee80211->LinkDetectInfo.SlotNum; i++ ){
4204 *TotalRxBcnNum += priv->ieee80211->LinkDetectInfo.RxBcnNum[i];
4205 *TotalRxDataNum += priv->ieee80211->LinkDetectInfo.RxDataNum[i];
4206 }
4207 }
4208
4209
4210 static void rtl819x_watchdog_wqcallback(struct work_struct *work)
4211 {
4212 struct delayed_work *dwork = container_of(work,struct delayed_work,work);
4213 struct r8192_priv *priv = container_of(dwork,struct r8192_priv,watch_dog_wq);
4214 struct net_device *dev = priv->ieee80211->dev;
4215 struct ieee80211_device* ieee = priv->ieee80211;
4216 RESET_TYPE ResetType = RESET_TYPE_NORESET;
4217 static u8 check_reset_cnt=0;
4218 unsigned long flags;
4219 bool bBusyTraffic = false;
4220 static u8 last_time = 0;
4221 bool bEnterPS = false;
4222
4223 if ((!priv->up) || priv->bHwRadioOff)
4224 return;
4225
4226 if(!priv->up)
4227 return;
4228 hal_dm_watchdog(dev);
4229 #ifdef ENABLE_IPS
4230 // printk("watch_dog ENABLE_IPS\n");
4231 if(ieee->actscanning == false){
4232 //printk("%d,%d,%d,%d\n", ieee->eRFPowerState, ieee->is_set_key, ieee->proto_stoppping, ieee->wx_set_enc);
4233 if((ieee->iw_mode == IW_MODE_INFRA) && (ieee->state == IEEE80211_NOLINK) &&
4234 (ieee->eRFPowerState == eRfOn)&&!ieee->is_set_key &&
4235 (!ieee->proto_stoppping) && !ieee->wx_set_enc){
4236 if(ieee->PowerSaveControl.ReturnPoint == IPS_CALLBACK_NONE){
4237 //printk("====================>haha:IPSEnter()\n");
4238 IPSEnter(dev);
4239 //ieee80211_stop_scan(priv->ieee80211);
4240 }
4241 }
4242 }
4243 #endif
4244 {//to get busy traffic condition
4245 if(ieee->state == IEEE80211_LINKED)
4246 {
4247 if( ieee->LinkDetectInfo.NumRxOkInPeriod> 100 ||
4248 ieee->LinkDetectInfo.NumTxOkInPeriod> 100 ) {
4249 bBusyTraffic = true;
4250 }
4251
4252 #ifdef ENABLE_LPS
4253 //added by amy for Leisure PS
4254 if( ((ieee->LinkDetectInfo.NumRxUnicastOkInPeriod + ieee->LinkDetectInfo.NumTxOkInPeriod) > 8 ) ||
4255 (ieee->LinkDetectInfo.NumRxUnicastOkInPeriod > 2) )
4256 {
4257 //printk("ieee->LinkDetectInfo.NumRxUnicastOkInPeriod is %d,ieee->LinkDetectInfo.NumTxOkInPeriod is %d\n",
4258 // ieee->LinkDetectInfo.NumRxUnicastOkInPeriod,ieee->LinkDetectInfo.NumTxOkInPeriod);
4259 bEnterPS= false;
4260 }
4261 else
4262 {
4263 bEnterPS= true;
4264 }
4265
4266 //printk("***bEnterPS = %d\n", bEnterPS);
4267 // LeisurePS only work in infra mode.
4268 if(bEnterPS)
4269 {
4270 LeisurePSEnter(dev);
4271 }
4272 else
4273 {
4274 LeisurePSLeave(dev);
4275 }
4276 #endif
4277
4278 }
4279 else
4280 {
4281 #ifdef ENABLE_LPS
4282 //RT_TRACE(COMP_LPS,"====>no link LPS leave\n");
4283 LeisurePSLeave(dev);
4284 #endif
4285 }
4286
4287 ieee->LinkDetectInfo.NumRxOkInPeriod = 0;
4288 ieee->LinkDetectInfo.NumTxOkInPeriod = 0;
4289 ieee->LinkDetectInfo.NumRxUnicastOkInPeriod = 0;
4290 ieee->LinkDetectInfo.bBusyTraffic = bBusyTraffic;
4291 }
4292
4293
4294 //added by amy for AP roaming
4295 if (1)
4296 {
4297 if(ieee->state == IEEE80211_LINKED && ieee->iw_mode == IW_MODE_INFRA)
4298 {
4299 u32 TotalRxBcnNum = 0;
4300 u32 TotalRxDataNum = 0;
4301
4302 rtl819x_update_rxcounts(priv, &TotalRxBcnNum, &TotalRxDataNum);
4303 if((TotalRxBcnNum+TotalRxDataNum) == 0)
4304 {
4305 if( ieee->eRFPowerState == eRfOff)
4306 RT_TRACE(COMP_ERR,"========>%s()\n",__FUNCTION__);
4307 printk("===>%s(): AP is power off,connect another one\n",__FUNCTION__);
4308 // Dot11d_Reset(dev);
4309 ieee->state = IEEE80211_ASSOCIATING;
4310 notify_wx_assoc_event(priv->ieee80211);
4311 RemovePeerTS(priv->ieee80211,priv->ieee80211->current_network.bssid);
4312 ieee->is_roaming = true;
4313 ieee->is_set_key = false;
4314 ieee->link_change(dev);
4315 queue_work(ieee->wq, &ieee->associate_procedure_wq);
4316 }
4317 }
4318 ieee->LinkDetectInfo.NumRecvBcnInPeriod=0;
4319 ieee->LinkDetectInfo.NumRecvDataInPeriod=0;
4320
4321 }
4322 //check if reset the driver
4323 spin_lock_irqsave(&priv->tx_lock,flags);
4324 if(check_reset_cnt++ >= 3 && !ieee->is_roaming && (last_time != 1))
4325 {
4326 ResetType = rtl819x_ifcheck_resetornot(dev);
4327 check_reset_cnt = 3;
4328 //DbgPrint("Start to check silent reset\n");
4329 }
4330 spin_unlock_irqrestore(&priv->tx_lock,flags);
4331 if(!priv->bDisableNormalResetCheck && ResetType == RESET_TYPE_NORMAL)
4332 {
4333 priv->ResetProgress = RESET_TYPE_NORMAL;
4334 RT_TRACE(COMP_RESET,"%s(): NOMAL RESET\n",__FUNCTION__);
4335 return;
4336 }
4337 /* disable silent reset temply 2008.9.11*/
4338 #if 1
4339 if( ((priv->force_reset) || (!priv->bDisableNormalResetCheck && ResetType==RESET_TYPE_SILENT))) // This is control by OID set in Pomelo
4340 {
4341 last_time = 1;
4342 rtl819x_ifsilentreset(dev);
4343 }
4344 else
4345 last_time = 0;
4346 #endif
4347 priv->force_reset = false;
4348 priv->bForcedSilentReset = false;
4349 priv->bResetInProgress = false;
4350 RT_TRACE(COMP_TRACE, " <==RtUsbCheckForHangWorkItemCallback()\n");
4351
4352 }
4353
4354 void watch_dog_timer_callback(unsigned long data)
4355 {
4356 struct r8192_priv *priv = ieee80211_priv((struct net_device *) data);
4357 queue_delayed_work(priv->priv_wq,&priv->watch_dog_wq,0);
4358 mod_timer(&priv->watch_dog_timer, jiffies + MSECS(IEEE80211_WATCH_DOG_TIME));
4359
4360 }
4361
4362 static int _rtl8192_up(struct net_device *dev)
4363 {
4364 struct r8192_priv *priv = ieee80211_priv(dev);
4365 //int i;
4366 RT_STATUS init_status = RT_STATUS_SUCCESS;
4367 priv->up=1;
4368 priv->ieee80211->ieee_up=1;
4369 priv->bdisable_nic = false; //YJ,add,091111
4370 RT_TRACE(COMP_INIT, "Bringing up iface");
4371
4372 init_status = rtl8192_adapter_start(dev);
4373 if(init_status != RT_STATUS_SUCCESS)
4374 {
4375 RT_TRACE(COMP_ERR,"ERR!!! %s(): initialization is failed!\n",__FUNCTION__);
4376 return -1;
4377 }
4378 RT_TRACE(COMP_INIT, "start adapter finished\n");
4379 #ifdef RTL8192E
4380 if(priv->ieee80211->eRFPowerState!=eRfOn)
4381 MgntActSet_RF_State(dev, eRfOn, priv->ieee80211->RfOffReason);
4382 #endif
4383 if(priv->ieee80211->state != IEEE80211_LINKED)
4384 ieee80211_softmac_start_protocol(priv->ieee80211);
4385 ieee80211_reset_queue(priv->ieee80211);
4386 watch_dog_timer_callback((unsigned long) dev);
4387 if(!netif_queue_stopped(dev))
4388 netif_start_queue(dev);
4389 else
4390 netif_wake_queue(dev);
4391
4392 return 0;
4393 }
4394
4395
4396 static int rtl8192_open(struct net_device *dev)
4397 {
4398 struct r8192_priv *priv = ieee80211_priv(dev);
4399 int ret;
4400
4401 down(&priv->wx_sem);
4402 ret = rtl8192_up(dev);
4403 up(&priv->wx_sem);
4404 return ret;
4405
4406 }
4407
4408
4409 int rtl8192_up(struct net_device *dev)
4410 {
4411 struct r8192_priv *priv = ieee80211_priv(dev);
4412
4413 if (priv->up == 1) return -1;
4414
4415 return _rtl8192_up(dev);
4416 }
4417
4418
4419 static int rtl8192_close(struct net_device *dev)
4420 {
4421 struct r8192_priv *priv = ieee80211_priv(dev);
4422 int ret;
4423
4424 down(&priv->wx_sem);
4425
4426 ret = rtl8192_down(dev);
4427
4428 up(&priv->wx_sem);
4429
4430 return ret;
4431
4432 }
4433
4434 int rtl8192_down(struct net_device *dev)
4435 {
4436 struct r8192_priv *priv = ieee80211_priv(dev);
4437
4438 if (priv->up == 0) return -1;
4439
4440 #ifdef ENABLE_LPS
4441 //LZM for PS-Poll AID issue. 090429
4442 if(priv->ieee80211->state == IEEE80211_LINKED)
4443 LeisurePSLeave(dev);
4444 #endif
4445
4446 priv->up=0;
4447 priv->ieee80211->ieee_up = 0;
4448 RT_TRACE(COMP_DOWN, "==========>%s()\n", __FUNCTION__);
4449 /* FIXME */
4450 if (!netif_queue_stopped(dev))
4451 netif_stop_queue(dev);
4452
4453 rtl8192_irq_disable(dev);
4454 rtl8192_cancel_deferred_work(priv);
4455 deinit_hal_dm(dev);
4456 del_timer_sync(&priv->watch_dog_timer);
4457
4458 ieee80211_softmac_stop_protocol(priv->ieee80211,true);
4459
4460 rtl8192_halt_adapter(dev,false);
4461 memset(&priv->ieee80211->current_network, 0 , offsetof(struct ieee80211_network, list));
4462
4463 RT_TRACE(COMP_DOWN, "<==========%s()\n", __FUNCTION__);
4464
4465 return 0;
4466 }
4467
4468
4469 void rtl8192_commit(struct net_device *dev)
4470 {
4471 struct r8192_priv *priv = ieee80211_priv(dev);
4472
4473 if (priv->up == 0) return ;
4474
4475
4476 ieee80211_softmac_stop_protocol(priv->ieee80211,true);
4477
4478 rtl8192_irq_disable(dev);
4479 rtl8192_halt_adapter(dev,true);
4480 _rtl8192_up(dev);
4481 }
4482
4483 static void rtl8192_restart(struct work_struct *work)
4484 {
4485 struct r8192_priv *priv = container_of(work, struct r8192_priv, reset_wq);
4486 struct net_device *dev = priv->ieee80211->dev;
4487
4488 down(&priv->wx_sem);
4489
4490 rtl8192_commit(dev);
4491
4492 up(&priv->wx_sem);
4493 }
4494
4495 static void r8192_set_multicast(struct net_device *dev)
4496 {
4497 struct r8192_priv *priv = ieee80211_priv(dev);
4498 short promisc;
4499
4500 //down(&priv->wx_sem);
4501
4502 /* FIXME FIXME */
4503
4504 promisc = (dev->flags & IFF_PROMISC) ? 1:0;
4505
4506 if (promisc != priv->promisc) {
4507 ;
4508 // rtl8192_commit(dev);
4509 }
4510
4511 priv->promisc = promisc;
4512
4513 //schedule_work(&priv->reset_wq);
4514 //up(&priv->wx_sem);
4515 }
4516
4517
4518 static int r8192_set_mac_adr(struct net_device *dev, void *mac)
4519 {
4520 struct r8192_priv *priv = ieee80211_priv(dev);
4521 struct sockaddr *addr = mac;
4522
4523 down(&priv->wx_sem);
4524
4525 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
4526
4527 schedule_work(&priv->reset_wq);
4528 up(&priv->wx_sem);
4529
4530 return 0;
4531 }
4532
4533 /* based on ipw2200 driver */
4534 static int rtl8192_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
4535 {
4536 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
4537 struct iwreq *wrq = (struct iwreq *)rq;
4538 int ret=-1;
4539 struct ieee80211_device *ieee = priv->ieee80211;
4540 u32 key[4];
4541 u8 broadcast_addr[6] = {0xff,0xff,0xff,0xff,0xff,0xff};
4542 struct iw_point *p = &wrq->u.data;
4543 struct ieee_param *ipw = NULL;//(struct ieee_param *)wrq->u.data.pointer;
4544
4545 down(&priv->wx_sem);
4546
4547
4548 if (p->length < sizeof(struct ieee_param) || !p->pointer){
4549 ret = -EINVAL;
4550 goto out;
4551 }
4552
4553 ipw = kmalloc(p->length, GFP_KERNEL);
4554 if (ipw == NULL){
4555 ret = -ENOMEM;
4556 goto out;
4557 }
4558 if (copy_from_user(ipw, p->pointer, p->length)) {
4559 kfree(ipw);
4560 ret = -EFAULT;
4561 goto out;
4562 }
4563
4564 switch (cmd) {
4565 case RTL_IOCTL_WPA_SUPPLICANT:
4566 //parse here for HW security
4567 if (ipw->cmd == IEEE_CMD_SET_ENCRYPTION)
4568 {
4569 if (ipw->u.crypt.set_tx)
4570 {
4571 if (strcmp(ipw->u.crypt.alg, "CCMP") == 0)
4572 ieee->pairwise_key_type = KEY_TYPE_CCMP;
4573 else if (strcmp(ipw->u.crypt.alg, "TKIP") == 0)
4574 ieee->pairwise_key_type = KEY_TYPE_TKIP;
4575 else if (strcmp(ipw->u.crypt.alg, "WEP") == 0)
4576 {
4577 if (ipw->u.crypt.key_len == 13)
4578 ieee->pairwise_key_type = KEY_TYPE_WEP104;
4579 else if (ipw->u.crypt.key_len == 5)
4580 ieee->pairwise_key_type = KEY_TYPE_WEP40;
4581 }
4582 else
4583 ieee->pairwise_key_type = KEY_TYPE_NA;
4584
4585 if (ieee->pairwise_key_type)
4586 {
4587 memcpy((u8*)key, ipw->u.crypt.key, 16);
4588 EnableHWSecurityConfig8192(dev);
4589 //we fill both index entry and 4th entry for pairwise key as in IPW interface, adhoc will only get here, so we need index entry for its default key serching!
4590 //added by WB.
4591 setKey(dev, 4, ipw->u.crypt.idx, ieee->pairwise_key_type, (u8*)ieee->ap_mac_addr, 0, key);
4592 if (ieee->auth_mode != 2) //LEAP WEP will never set this.
4593 setKey(dev, ipw->u.crypt.idx, ipw->u.crypt.idx, ieee->pairwise_key_type, (u8*)ieee->ap_mac_addr, 0, key);
4594 }
4595 if ((ieee->pairwise_key_type == KEY_TYPE_CCMP) && ieee->pHTInfo->bCurrentHTSupport){
4596 write_nic_byte(dev, 0x173, 1); //fix aes bug
4597 }
4598
4599 }
4600 else //if (ipw->u.crypt.idx) //group key use idx > 0
4601 {
4602 memcpy((u8*)key, ipw->u.crypt.key, 16);
4603 if (strcmp(ipw->u.crypt.alg, "CCMP") == 0)
4604 ieee->group_key_type= KEY_TYPE_CCMP;
4605 else if (strcmp(ipw->u.crypt.alg, "TKIP") == 0)
4606 ieee->group_key_type = KEY_TYPE_TKIP;
4607 else if (strcmp(ipw->u.crypt.alg, "WEP") == 0)
4608 {
4609 if (ipw->u.crypt.key_len == 13)
4610 ieee->group_key_type = KEY_TYPE_WEP104;
4611 else if (ipw->u.crypt.key_len == 5)
4612 ieee->group_key_type = KEY_TYPE_WEP40;
4613 }
4614 else
4615 ieee->group_key_type = KEY_TYPE_NA;
4616
4617 if (ieee->group_key_type)
4618 {
4619 setKey( dev,
4620 ipw->u.crypt.idx,
4621 ipw->u.crypt.idx, //KeyIndex
4622 ieee->group_key_type, //KeyType
4623 broadcast_addr, //MacAddr
4624 0, //DefaultKey
4625 key); //KeyContent
4626 }
4627 }
4628 }
4629 ret = ieee80211_wpa_supplicant_ioctl(priv->ieee80211, &wrq->u.data);
4630 break;
4631
4632 default:
4633 ret = -EOPNOTSUPP;
4634 break;
4635 }
4636
4637 kfree(ipw);
4638 out:
4639 up(&priv->wx_sem);
4640
4641 return ret;
4642 }
4643
4644 static u8 HwRateToMRate90(bool bIsHT, u8 rate)
4645 {
4646 u8 ret_rate = 0x02;
4647
4648 if(!bIsHT) {
4649 switch(rate) {
4650 case DESC90_RATE1M: ret_rate = MGN_1M; break;
4651 case DESC90_RATE2M: ret_rate = MGN_2M; break;
4652 case DESC90_RATE5_5M: ret_rate = MGN_5_5M; break;
4653 case DESC90_RATE11M: ret_rate = MGN_11M; break;
4654 case DESC90_RATE6M: ret_rate = MGN_6M; break;
4655 case DESC90_RATE9M: ret_rate = MGN_9M; break;
4656 case DESC90_RATE12M: ret_rate = MGN_12M; break;
4657 case DESC90_RATE18M: ret_rate = MGN_18M; break;
4658 case DESC90_RATE24M: ret_rate = MGN_24M; break;
4659 case DESC90_RATE36M: ret_rate = MGN_36M; break;
4660 case DESC90_RATE48M: ret_rate = MGN_48M; break;
4661 case DESC90_RATE54M: ret_rate = MGN_54M; break;
4662
4663 default:
4664 RT_TRACE(COMP_RECV, "HwRateToMRate90(): Non supported Rate [%x], bIsHT = %d!!!\n", rate, bIsHT);
4665 break;
4666 }
4667
4668 } else {
4669 switch(rate) {
4670 case DESC90_RATEMCS0: ret_rate = MGN_MCS0; break;
4671 case DESC90_RATEMCS1: ret_rate = MGN_MCS1; break;
4672 case DESC90_RATEMCS2: ret_rate = MGN_MCS2; break;
4673 case DESC90_RATEMCS3: ret_rate = MGN_MCS3; break;
4674 case DESC90_RATEMCS4: ret_rate = MGN_MCS4; break;
4675 case DESC90_RATEMCS5: ret_rate = MGN_MCS5; break;
4676 case DESC90_RATEMCS6: ret_rate = MGN_MCS6; break;
4677 case DESC90_RATEMCS7: ret_rate = MGN_MCS7; break;
4678 case DESC90_RATEMCS8: ret_rate = MGN_MCS8; break;
4679 case DESC90_RATEMCS9: ret_rate = MGN_MCS9; break;
4680 case DESC90_RATEMCS10: ret_rate = MGN_MCS10; break;
4681 case DESC90_RATEMCS11: ret_rate = MGN_MCS11; break;
4682 case DESC90_RATEMCS12: ret_rate = MGN_MCS12; break;
4683 case DESC90_RATEMCS13: ret_rate = MGN_MCS13; break;
4684 case DESC90_RATEMCS14: ret_rate = MGN_MCS14; break;
4685 case DESC90_RATEMCS15: ret_rate = MGN_MCS15; break;
4686 case DESC90_RATEMCS32: ret_rate = (0x80|0x20); break;
4687
4688 default:
4689 RT_TRACE(COMP_RECV, "HwRateToMRate90(): Non supported Rate [%x], bIsHT = %d!!!\n",rate, bIsHT);
4690 break;
4691 }
4692 }
4693
4694 return ret_rate;
4695 }
4696
4697 /* Record the TSF time stamp when receiving a packet */
4698 static void UpdateRxPktTimeStamp8190 (struct net_device *dev, struct ieee80211_rx_stats *stats)
4699 {
4700 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
4701
4702 if(stats->bIsAMPDU && !stats->bFirstMPDU) {
4703 stats->mac_time[0] = priv->LastRxDescTSFLow;
4704 stats->mac_time[1] = priv->LastRxDescTSFHigh;
4705 } else {
4706 priv->LastRxDescTSFLow = stats->mac_time[0];
4707 priv->LastRxDescTSFHigh = stats->mac_time[1];
4708 }
4709 }
4710
4711 static long rtl819x_translate_todbm(u8 signal_strength_index)// 0-100 index.
4712 {
4713 long signal_power; // in dBm.
4714
4715 // Translate to dBm (x=0.5y-95).
4716 signal_power = (long)((signal_strength_index + 1) >> 1);
4717 signal_power -= 95;
4718
4719 return signal_power;
4720 }
4721
4722 /*
4723 * Update Rx signal related information in the packet reeived
4724 * to RxStats. User application can query RxStats to realize
4725 * current Rx signal status.
4726 *
4727 * In normal operation, user only care about the information of the BSS
4728 * and we shall invoke this function if the packet received is from the BSS.
4729 */
4730 static void
4731 rtl819x_update_rxsignalstatistics8190pci(
4732 struct r8192_priv * priv,
4733 struct ieee80211_rx_stats * pprevious_stats
4734 )
4735 {
4736 int weighting = 0;
4737
4738 //2 <ToDo> Update Rx Statistics (such as signal strength and signal quality).
4739
4740 // Initila state
4741 if(priv->stats.recv_signal_power == 0)
4742 priv->stats.recv_signal_power = pprevious_stats->RecvSignalPower;
4743
4744 // To avoid the past result restricting the statistics sensitivity, weight the current power (5/6) to speed up the
4745 // reaction of smoothed Signal Power.
4746 if(pprevious_stats->RecvSignalPower > priv->stats.recv_signal_power)
4747 weighting = 5;
4748 else if(pprevious_stats->RecvSignalPower < priv->stats.recv_signal_power)
4749 weighting = (-5);
4750 //
4751 // We need more correct power of received packets and the "SignalStrength" of RxStats have been beautified or translated,
4752 // so we record the correct power in Dbm here. By Bruce, 2008-03-07.
4753 //
4754 priv->stats.recv_signal_power = (priv->stats.recv_signal_power * 5 + pprevious_stats->RecvSignalPower + weighting) / 6;
4755 }
4756
4757 static void
4758 rtl8190_process_cck_rxpathsel(
4759 struct r8192_priv * priv,
4760 struct ieee80211_rx_stats * pprevious_stats
4761 )
4762 {
4763 #ifdef RTL8190P //Only 90P 2T4R need to check
4764 char last_cck_adc_pwdb[4]={0,0,0,0};
4765 u8 i;
4766 //cosa add for Rx path selection
4767 if(priv->rf_type == RF_2T4R && DM_RxPathSelTable.Enable)
4768 {
4769 if(pprevious_stats->bIsCCK &&
4770 (pprevious_stats->bPacketToSelf ||pprevious_stats->bPacketBeacon))
4771 {
4772 /* record the cck adc_pwdb to the sliding window. */
4773 if(priv->stats.cck_adc_pwdb.TotalNum++ >= PHY_RSSI_SLID_WIN_MAX)
4774 {
4775 priv->stats.cck_adc_pwdb.TotalNum = PHY_RSSI_SLID_WIN_MAX;
4776 for(i=RF90_PATH_A; i<RF90_PATH_MAX; i++)
4777 {
4778 last_cck_adc_pwdb[i] = priv->stats.cck_adc_pwdb.elements[i][priv->stats.cck_adc_pwdb.index];
4779 priv->stats.cck_adc_pwdb.TotalVal[i] -= last_cck_adc_pwdb[i];
4780 }
4781 }
4782 for(i=RF90_PATH_A; i<RF90_PATH_MAX; i++)
4783 {
4784 priv->stats.cck_adc_pwdb.TotalVal[i] += pprevious_stats->cck_adc_pwdb[i];
4785 priv->stats.cck_adc_pwdb.elements[i][priv->stats.cck_adc_pwdb.index] = pprevious_stats->cck_adc_pwdb[i];
4786 }
4787 priv->stats.cck_adc_pwdb.index++;
4788 if(priv->stats.cck_adc_pwdb.index >= PHY_RSSI_SLID_WIN_MAX)
4789 priv->stats.cck_adc_pwdb.index = 0;
4790
4791 for(i=RF90_PATH_A; i<RF90_PATH_MAX; i++)
4792 {
4793 DM_RxPathSelTable.cck_pwdb_sta[i] = priv->stats.cck_adc_pwdb.TotalVal[i]/priv->stats.cck_adc_pwdb.TotalNum;
4794 }
4795
4796 for(i=RF90_PATH_A; i<RF90_PATH_MAX; i++)
4797 {
4798 if(pprevious_stats->cck_adc_pwdb[i] > (char)priv->undecorated_smoothed_cck_adc_pwdb[i])
4799 {
4800 priv->undecorated_smoothed_cck_adc_pwdb[i] =
4801 ( (priv->undecorated_smoothed_cck_adc_pwdb[i]*(Rx_Smooth_Factor-1)) +
4802 (pprevious_stats->cck_adc_pwdb[i])) /(Rx_Smooth_Factor);
4803 priv->undecorated_smoothed_cck_adc_pwdb[i] = priv->undecorated_smoothed_cck_adc_pwdb[i] + 1;
4804 }
4805 else
4806 {
4807 priv->undecorated_smoothed_cck_adc_pwdb[i] =
4808 ( (priv->undecorated_smoothed_cck_adc_pwdb[i]*(Rx_Smooth_Factor-1)) +
4809 (pprevious_stats->cck_adc_pwdb[i])) /(Rx_Smooth_Factor);
4810 }
4811 }
4812 }
4813 }
4814 #endif
4815 }
4816
4817
4818 /* 2008/01/22 MH We can not delcare RSSI/EVM total value of sliding window to
4819 be a local static. Otherwise, it may increase when we return from S3/S4. The
4820 value will be kept in memory or disk. We must delcare the value in adapter
4821 and it will be reinitialized when return from S3/S4. */
4822 static void rtl8192_process_phyinfo(struct r8192_priv * priv, u8* buffer,struct ieee80211_rx_stats * pprevious_stats, struct ieee80211_rx_stats * pcurrent_stats)
4823 {
4824 bool bcheck = false;
4825 u8 rfpath;
4826 u32 nspatial_stream, tmp_val;
4827 //u8 i;
4828 static u32 slide_rssi_index=0, slide_rssi_statistics=0;
4829 static u32 slide_evm_index=0, slide_evm_statistics=0;
4830 static u32 last_rssi=0, last_evm=0;
4831 //cosa add for rx path selection
4832 // static long slide_cck_adc_pwdb_index=0, slide_cck_adc_pwdb_statistics=0;
4833 // static char last_cck_adc_pwdb[4]={0,0,0,0};
4834 //cosa add for beacon rssi smoothing
4835 static u32 slide_beacon_adc_pwdb_index=0, slide_beacon_adc_pwdb_statistics=0;
4836 static u32 last_beacon_adc_pwdb=0;
4837
4838 struct ieee80211_hdr_3addr *hdr;
4839 u16 sc ;
4840 unsigned int frag,seq;
4841 hdr = (struct ieee80211_hdr_3addr *)buffer;
4842 sc = le16_to_cpu(hdr->seq_ctl);
4843 frag = WLAN_GET_SEQ_FRAG(sc);
4844 seq = WLAN_GET_SEQ_SEQ(sc);
4845 //cosa add 04292008 to record the sequence number
4846 pcurrent_stats->Seq_Num = seq;
4847 //
4848 // Check whether we should take the previous packet into accounting
4849 //
4850 if(!pprevious_stats->bIsAMPDU)
4851 {
4852 // if previous packet is not aggregated packet
4853 bcheck = true;
4854 }else
4855 {
4856 //remve for that we don't use AMPDU to calculate PWDB,because the reported PWDB of some AP is fault.
4857 #if 0
4858 // if previous packet is aggregated packet, and current packet
4859 // (1) is not AMPDU
4860 // (2) is the first packet of one AMPDU
4861 // that means the previous packet is the last one aggregated packet
4862 if( !pcurrent_stats->bIsAMPDU || pcurrent_stats->bFirstMPDU)
4863 bcheck = true;
4864 #endif
4865 }
4866
4867 if(slide_rssi_statistics++ >= PHY_RSSI_SLID_WIN_MAX)
4868 {
4869 slide_rssi_statistics = PHY_RSSI_SLID_WIN_MAX;
4870 last_rssi = priv->stats.slide_signal_strength[slide_rssi_index];
4871 priv->stats.slide_rssi_total -= last_rssi;
4872 }
4873 priv->stats.slide_rssi_total += pprevious_stats->SignalStrength;
4874
4875 priv->stats.slide_signal_strength[slide_rssi_index++] = pprevious_stats->SignalStrength;
4876 if(slide_rssi_index >= PHY_RSSI_SLID_WIN_MAX)
4877 slide_rssi_index = 0;
4878
4879 // <1> Showed on UI for user, in dbm
4880 tmp_val = priv->stats.slide_rssi_total/slide_rssi_statistics;
4881 priv->stats.signal_strength = rtl819x_translate_todbm((u8)tmp_val);
4882 pcurrent_stats->rssi = priv->stats.signal_strength;
4883 //
4884 // If the previous packet does not match the criteria, neglect it
4885 //
4886 if(!pprevious_stats->bPacketMatchBSSID)
4887 {
4888 if(!pprevious_stats->bToSelfBA)
4889 return;
4890 }
4891
4892 if(!bcheck)
4893 return;
4894
4895 rtl8190_process_cck_rxpathsel(priv,pprevious_stats);
4896
4897 //
4898 // Check RSSI
4899 //
4900 priv->stats.num_process_phyinfo++;
4901 #if 0
4902 /* record the general signal strength to the sliding window. */
4903 if(slide_rssi_statistics++ >= PHY_RSSI_SLID_WIN_MAX)
4904 {
4905 slide_rssi_statistics = PHY_RSSI_SLID_WIN_MAX;
4906 last_rssi = priv->stats.slide_signal_strength[slide_rssi_index];
4907 priv->stats.slide_rssi_total -= last_rssi;
4908 }
4909 priv->stats.slide_rssi_total += pprevious_stats->SignalStrength;
4910
4911 priv->stats.slide_signal_strength[slide_rssi_index++] = pprevious_stats->SignalStrength;
4912 if(slide_rssi_index >= PHY_RSSI_SLID_WIN_MAX)
4913 slide_rssi_index = 0;
4914
4915 // <1> Showed on UI for user, in dbm
4916 tmp_val = priv->stats.slide_rssi_total/slide_rssi_statistics;
4917 priv->stats.signal_strength = rtl819x_translate_todbm((u8)tmp_val);
4918
4919 #endif
4920 // <2> Showed on UI for engineering
4921 // hardware does not provide rssi information for each rf path in CCK
4922 if(!pprevious_stats->bIsCCK && pprevious_stats->bPacketToSelf)
4923 {
4924 for (rfpath = RF90_PATH_A; rfpath < RF90_PATH_C; rfpath++)
4925 {
4926 if (!rtl8192_phy_CheckIsLegalRFPath(priv->ieee80211->dev, rfpath))
4927 continue;
4928 RT_TRACE(COMP_DBG,"Jacken -> pPreviousstats->RxMIMOSignalStrength[rfpath] = %d \n" ,pprevious_stats->RxMIMOSignalStrength[rfpath] );
4929 //Fixed by Jacken 2008-03-20
4930 if(priv->stats.rx_rssi_percentage[rfpath] == 0)
4931 {
4932 priv->stats.rx_rssi_percentage[rfpath] = pprevious_stats->RxMIMOSignalStrength[rfpath];
4933 //DbgPrint("MIMO RSSI initialize \n");
4934 }
4935 if(pprevious_stats->RxMIMOSignalStrength[rfpath] > priv->stats.rx_rssi_percentage[rfpath])
4936 {
4937 priv->stats.rx_rssi_percentage[rfpath] =
4938 ( (priv->stats.rx_rssi_percentage[rfpath]*(Rx_Smooth_Factor-1)) +
4939 (pprevious_stats->RxMIMOSignalStrength[rfpath])) /(Rx_Smooth_Factor);
4940 priv->stats.rx_rssi_percentage[rfpath] = priv->stats.rx_rssi_percentage[rfpath] + 1;
4941 }
4942 else
4943 {
4944 priv->stats.rx_rssi_percentage[rfpath] =
4945 ( (priv->stats.rx_rssi_percentage[rfpath]*(Rx_Smooth_Factor-1)) +
4946 (pprevious_stats->RxMIMOSignalStrength[rfpath])) /(Rx_Smooth_Factor);
4947 }
4948 RT_TRACE(COMP_DBG,"Jacken -> priv->RxStats.RxRSSIPercentage[rfPath] = %d \n" ,priv->stats.rx_rssi_percentage[rfpath] );
4949 }
4950 }
4951
4952
4953 //
4954 // Check PWDB.
4955 //
4956 //cosa add for beacon rssi smoothing by average.
4957 if(pprevious_stats->bPacketBeacon)
4958 {
4959 /* record the beacon pwdb to the sliding window. */
4960 if(slide_beacon_adc_pwdb_statistics++ >= PHY_Beacon_RSSI_SLID_WIN_MAX)
4961 {
4962 slide_beacon_adc_pwdb_statistics = PHY_Beacon_RSSI_SLID_WIN_MAX;
4963 last_beacon_adc_pwdb = priv->stats.Slide_Beacon_pwdb[slide_beacon_adc_pwdb_index];
4964 priv->stats.Slide_Beacon_Total -= last_beacon_adc_pwdb;
4965 //DbgPrint("slide_beacon_adc_pwdb_index = %d, last_beacon_adc_pwdb = %d, Adapter->RxStats.Slide_Beacon_Total = %d\n",
4966 // slide_beacon_adc_pwdb_index, last_beacon_adc_pwdb, Adapter->RxStats.Slide_Beacon_Total);
4967 }
4968 priv->stats.Slide_Beacon_Total += pprevious_stats->RxPWDBAll;
4969 priv->stats.Slide_Beacon_pwdb[slide_beacon_adc_pwdb_index] = pprevious_stats->RxPWDBAll;
4970 //DbgPrint("slide_beacon_adc_pwdb_index = %d, pPreviousRfd->Status.RxPWDBAll = %d\n", slide_beacon_adc_pwdb_index, pPreviousRfd->Status.RxPWDBAll);
4971 slide_beacon_adc_pwdb_index++;
4972 if(slide_beacon_adc_pwdb_index >= PHY_Beacon_RSSI_SLID_WIN_MAX)
4973 slide_beacon_adc_pwdb_index = 0;
4974 pprevious_stats->RxPWDBAll = priv->stats.Slide_Beacon_Total/slide_beacon_adc_pwdb_statistics;
4975 if(pprevious_stats->RxPWDBAll >= 3)
4976 pprevious_stats->RxPWDBAll -= 3;
4977 }
4978
4979 RT_TRACE(COMP_RXDESC, "Smooth %s PWDB = %d\n",
4980 pprevious_stats->bIsCCK? "CCK": "OFDM",
4981 pprevious_stats->RxPWDBAll);
4982
4983 if(pprevious_stats->bPacketToSelf || pprevious_stats->bPacketBeacon || pprevious_stats->bToSelfBA)
4984 {
4985 if(priv->undecorated_smoothed_pwdb < 0) // initialize
4986 {
4987 priv->undecorated_smoothed_pwdb = pprevious_stats->RxPWDBAll;
4988 //DbgPrint("First pwdb initialize \n");
4989 }
4990 #if 1
4991 if(pprevious_stats->RxPWDBAll > (u32)priv->undecorated_smoothed_pwdb)
4992 {
4993 priv->undecorated_smoothed_pwdb =
4994 ( ((priv->undecorated_smoothed_pwdb)*(Rx_Smooth_Factor-1)) +
4995 (pprevious_stats->RxPWDBAll)) /(Rx_Smooth_Factor);
4996 priv->undecorated_smoothed_pwdb = priv->undecorated_smoothed_pwdb + 1;
4997 }
4998 else
4999 {
5000 priv->undecorated_smoothed_pwdb =
5001 ( ((priv->undecorated_smoothed_pwdb)*(Rx_Smooth_Factor-1)) +
5002 (pprevious_stats->RxPWDBAll)) /(Rx_Smooth_Factor);
5003 }
5004 #else
5005 //Fixed by Jacken 2008-03-20
5006 if(pPreviousRfd->Status.RxPWDBAll > (u32)pHalData->UndecoratedSmoothedPWDB)
5007 {
5008 pHalData->UndecoratedSmoothedPWDB =
5009 ( ((pHalData->UndecoratedSmoothedPWDB)* 5) + (pPreviousRfd->Status.RxPWDBAll)) / 6;
5010 pHalData->UndecoratedSmoothedPWDB = pHalData->UndecoratedSmoothedPWDB + 1;
5011 }
5012 else
5013 {
5014 pHalData->UndecoratedSmoothedPWDB =
5015 ( ((pHalData->UndecoratedSmoothedPWDB)* 5) + (pPreviousRfd->Status.RxPWDBAll)) / 6;
5016 }
5017 #endif
5018 rtl819x_update_rxsignalstatistics8190pci(priv,pprevious_stats);
5019 }
5020
5021 //
5022 // Check EVM
5023 //
5024 /* record the general EVM to the sliding window. */
5025 if(pprevious_stats->SignalQuality == 0)
5026 {
5027 }
5028 else
5029 {
5030 if(pprevious_stats->bPacketToSelf || pprevious_stats->bPacketBeacon || pprevious_stats->bToSelfBA){
5031 if(slide_evm_statistics++ >= PHY_RSSI_SLID_WIN_MAX){
5032 slide_evm_statistics = PHY_RSSI_SLID_WIN_MAX;
5033 last_evm = priv->stats.slide_evm[slide_evm_index];
5034 priv->stats.slide_evm_total -= last_evm;
5035 }
5036
5037 priv->stats.slide_evm_total += pprevious_stats->SignalQuality;
5038
5039 priv->stats.slide_evm[slide_evm_index++] = pprevious_stats->SignalQuality;
5040 if(slide_evm_index >= PHY_RSSI_SLID_WIN_MAX)
5041 slide_evm_index = 0;
5042
5043 // <1> Showed on UI for user, in percentage.
5044 tmp_val = priv->stats.slide_evm_total/slide_evm_statistics;
5045 priv->stats.signal_quality = tmp_val;
5046 //cosa add 10/11/2007, Showed on UI for user in Windows Vista, for Link quality.
5047 priv->stats.last_signal_strength_inpercent = tmp_val;
5048 }
5049
5050 // <2> Showed on UI for engineering
5051 if(pprevious_stats->bPacketToSelf || pprevious_stats->bPacketBeacon || pprevious_stats->bToSelfBA)
5052 {
5053 for(nspatial_stream = 0; nspatial_stream<2 ; nspatial_stream++) // 2 spatial stream
5054 {
5055 if(pprevious_stats->RxMIMOSignalQuality[nspatial_stream] != -1)
5056 {
5057 if(priv->stats.rx_evm_percentage[nspatial_stream] == 0) // initialize
5058 {
5059 priv->stats.rx_evm_percentage[nspatial_stream] = pprevious_stats->RxMIMOSignalQuality[nspatial_stream];
5060 }
5061 priv->stats.rx_evm_percentage[nspatial_stream] =
5062 ( (priv->stats.rx_evm_percentage[nspatial_stream]* (Rx_Smooth_Factor-1)) +
5063 (pprevious_stats->RxMIMOSignalQuality[nspatial_stream]* 1)) / (Rx_Smooth_Factor);
5064 }
5065 }
5066 }
5067 }
5068
5069 }
5070
5071 static u8 rtl819x_query_rxpwrpercentage(
5072 char antpower
5073 )
5074 {
5075 if ((antpower <= -100) || (antpower >= 20))
5076 {
5077 return 0;
5078 }
5079 else if (antpower >= 0)
5080 {
5081 return 100;
5082 }
5083 else
5084 {
5085 return (100+antpower);
5086 }
5087
5088 }
5089
5090 static u8
5091 rtl819x_evm_dbtopercentage(
5092 char value
5093 )
5094 {
5095 char ret_val;
5096
5097 ret_val = value;
5098
5099 if(ret_val >= 0)
5100 ret_val = 0;
5101 if(ret_val <= -33)
5102 ret_val = -33;
5103 ret_val = 0 - ret_val;
5104 ret_val*=3;
5105 if(ret_val == 99)
5106 ret_val = 100;
5107 return ret_val;
5108 }
5109
5110 /* We want good-looking for signal strength/quality */
5111 static long rtl819x_signal_scale_mapping(long currsig)
5112 {
5113 long retsig;
5114
5115 // Step 1. Scale mapping.
5116 if(currsig >= 61 && currsig <= 100)
5117 {
5118 retsig = 90 + ((currsig - 60) / 4);
5119 }
5120 else if(currsig >= 41 && currsig <= 60)
5121 {
5122 retsig = 78 + ((currsig - 40) / 2);
5123 }
5124 else if(currsig >= 31 && currsig <= 40)
5125 {
5126 retsig = 66 + (currsig - 30);
5127 }
5128 else if(currsig >= 21 && currsig <= 30)
5129 {
5130 retsig = 54 + (currsig - 20);
5131 }
5132 else if(currsig >= 5 && currsig <= 20)
5133 {
5134 retsig = 42 + (((currsig - 5) * 2) / 3);
5135 }
5136 else if(currsig == 4)
5137 {
5138 retsig = 36;
5139 }
5140 else if(currsig == 3)
5141 {
5142 retsig = 27;
5143 }
5144 else if(currsig == 2)
5145 {
5146 retsig = 18;
5147 }
5148 else if(currsig == 1)
5149 {
5150 retsig = 9;
5151 }
5152 else
5153 {
5154 retsig = currsig;
5155 }
5156
5157 return retsig;
5158 }
5159
5160 static void rtl8192_query_rxphystatus(
5161 struct r8192_priv * priv,
5162 struct ieee80211_rx_stats * pstats,
5163 prx_desc_819x_pci pdesc,
5164 prx_fwinfo_819x_pci pdrvinfo,
5165 struct ieee80211_rx_stats * precord_stats,
5166 bool bpacket_match_bssid,
5167 bool bpacket_toself,
5168 bool bPacketBeacon,
5169 bool bToSelfBA
5170 )
5171 {
5172 //PRT_RFD_STATUS pRtRfdStatus = &(pRfd->Status);
5173 phy_sts_ofdm_819xpci_t* pofdm_buf;
5174 phy_sts_cck_819xpci_t * pcck_buf;
5175 phy_ofdm_rx_status_rxsc_sgien_exintfflag* prxsc;
5176 u8 *prxpkt;
5177 u8 i,max_spatial_stream, tmp_rxsnr, tmp_rxevm, rxsc_sgien_exflg;
5178 char rx_pwr[4], rx_pwr_all=0;
5179 //long rx_avg_pwr = 0;
5180 char rx_snrX, rx_evmX;
5181 u8 evm, pwdb_all;
5182 u32 RSSI, total_rssi=0;//, total_evm=0;
5183 // long signal_strength_index = 0;
5184 u8 is_cck_rate=0;
5185 u8 rf_rx_num = 0;
5186
5187 /* 2007/07/04 MH For OFDM RSSI. For high power or not. */
5188 static u8 check_reg824 = 0;
5189 static u32 reg824_bit9 = 0;
5190
5191 priv->stats.numqry_phystatus++;
5192
5193 is_cck_rate = rx_hal_is_cck_rate(pdrvinfo);
5194
5195 // Record it for next packet processing
5196 memset(precord_stats, 0, sizeof(struct ieee80211_rx_stats));
5197 pstats->bPacketMatchBSSID = precord_stats->bPacketMatchBSSID = bpacket_match_bssid;
5198 pstats->bPacketToSelf = precord_stats->bPacketToSelf = bpacket_toself;
5199 pstats->bIsCCK = precord_stats->bIsCCK = is_cck_rate;//RX_HAL_IS_CCK_RATE(pDrvInfo);
5200 pstats->bPacketBeacon = precord_stats->bPacketBeacon = bPacketBeacon;
5201 pstats->bToSelfBA = precord_stats->bToSelfBA = bToSelfBA;
5202 /*2007.08.30 requested by SD3 Jerry */
5203 if(check_reg824 == 0)
5204 {
5205 reg824_bit9 = rtl8192_QueryBBReg(priv->ieee80211->dev, rFPGA0_XA_HSSIParameter2, 0x200);
5206 check_reg824 = 1;
5207 }
5208
5209
5210 prxpkt = (u8*)pdrvinfo;
5211
5212 /* Move pointer to the 16th bytes. Phy status start address. */
5213 prxpkt += sizeof(rx_fwinfo_819x_pci);
5214
5215 /* Initial the cck and ofdm buffer pointer */
5216 pcck_buf = (phy_sts_cck_819xpci_t *)prxpkt;
5217 pofdm_buf = (phy_sts_ofdm_819xpci_t *)prxpkt;
5218
5219 pstats->RxMIMOSignalQuality[0] = -1;
5220 pstats->RxMIMOSignalQuality[1] = -1;
5221 precord_stats->RxMIMOSignalQuality[0] = -1;
5222 precord_stats->RxMIMOSignalQuality[1] = -1;
5223
5224 if(is_cck_rate)
5225 {
5226 //
5227 // (1)Hardware does not provide RSSI for CCK
5228 //
5229
5230 //
5231 // (2)PWDB, Average PWDB cacluated by hardware (for rate adaptive)
5232 //
5233 u8 report;//, cck_agc_rpt;
5234 #ifdef RTL8190P
5235 u8 tmp_pwdb;
5236 char cck_adc_pwdb[4];
5237 #endif
5238 priv->stats.numqry_phystatusCCK++;
5239
5240 #ifdef RTL8190P //Only 90P 2T4R need to check
5241 if(priv->rf_type == RF_2T4R && DM_RxPathSelTable.Enable && bpacket_match_bssid)
5242 {
5243 for(i=RF90_PATH_A; i<RF90_PATH_MAX; i++)
5244 {
5245 tmp_pwdb = pcck_buf->adc_pwdb_X[i];
5246 cck_adc_pwdb[i] = (char)tmp_pwdb;
5247 cck_adc_pwdb[i] /= 2;
5248 pstats->cck_adc_pwdb[i] = precord_stats->cck_adc_pwdb[i] = cck_adc_pwdb[i];
5249 //DbgPrint("RF-%d tmp_pwdb = 0x%x, cck_adc_pwdb = %d", i, tmp_pwdb, cck_adc_pwdb[i]);
5250 }
5251 }
5252 #endif
5253
5254 if(!reg824_bit9)
5255 {
5256 report = pcck_buf->cck_agc_rpt & 0xc0;
5257 report = report>>6;
5258 switch(report)
5259 {
5260 //Fixed by Jacken from Bryant 2008-03-20
5261 //Original value is -38 , -26 , -14 , -2
5262 //Fixed value is -35 , -23 , -11 , 6
5263 case 0x3:
5264 rx_pwr_all = -35 - (pcck_buf->cck_agc_rpt & 0x3e);
5265 break;
5266 case 0x2:
5267 rx_pwr_all = -23 - (pcck_buf->cck_agc_rpt & 0x3e);
5268 break;
5269 case 0x1:
5270 rx_pwr_all = -11 - (pcck_buf->cck_agc_rpt & 0x3e);
5271 break;
5272 case 0x0:
5273 rx_pwr_all = 8 - (pcck_buf->cck_agc_rpt & 0x3e);
5274 break;
5275 }
5276 }
5277 else
5278 {
5279 report = pcck_buf->cck_agc_rpt & 0x60;
5280 report = report>>5;
5281 switch(report)
5282 {
5283 case 0x3:
5284 rx_pwr_all = -35 - ((pcck_buf->cck_agc_rpt & 0x1f)<<1) ;
5285 break;
5286 case 0x2:
5287 rx_pwr_all = -23 - ((pcck_buf->cck_agc_rpt & 0x1f)<<1);
5288 break;
5289 case 0x1:
5290 rx_pwr_all = -11 - ((pcck_buf->cck_agc_rpt & 0x1f)<<1) ;
5291 break;
5292 case 0x0:
5293 rx_pwr_all = -8 - ((pcck_buf->cck_agc_rpt & 0x1f)<<1) ;
5294 break;
5295 }
5296 }
5297
5298 pwdb_all = rtl819x_query_rxpwrpercentage(rx_pwr_all);
5299 pstats->RxPWDBAll = precord_stats->RxPWDBAll = pwdb_all;
5300 pstats->RecvSignalPower = rx_pwr_all;
5301
5302 //
5303 // (3) Get Signal Quality (EVM)
5304 //
5305 if(bpacket_match_bssid)
5306 {
5307 u8 sq;
5308
5309 if(pstats->RxPWDBAll > 40)
5310 {
5311 sq = 100;
5312 }else
5313 {
5314 sq = pcck_buf->sq_rpt;
5315
5316 if(pcck_buf->sq_rpt > 64)
5317 sq = 0;
5318 else if (pcck_buf->sq_rpt < 20)
5319 sq = 100;
5320 else
5321 sq = ((64-sq) * 100) / 44;
5322 }
5323 pstats->SignalQuality = precord_stats->SignalQuality = sq;
5324 pstats->RxMIMOSignalQuality[0] = precord_stats->RxMIMOSignalQuality[0] = sq;
5325 pstats->RxMIMOSignalQuality[1] = precord_stats->RxMIMOSignalQuality[1] = -1;
5326 }
5327 }
5328 else
5329 {
5330 priv->stats.numqry_phystatusHT++;
5331 //
5332 // (1)Get RSSI for HT rate
5333 //
5334 for(i=RF90_PATH_A; i<RF90_PATH_MAX; i++)
5335 {
5336 // 2008/01/30 MH we will judge RF RX path now.
5337 if (priv->brfpath_rxenable[i])
5338 rf_rx_num++;
5339 //else
5340 //continue;
5341
5342 //Fixed by Jacken from Bryant 2008-03-20
5343 //Original value is 106
5344 #ifdef RTL8190P //Modify by Jacken 2008/03/31
5345 rx_pwr[i] = ((pofdm_buf->trsw_gain_X[i]&0x3F)*2) - 106;
5346 #else
5347 rx_pwr[i] = ((pofdm_buf->trsw_gain_X[i]&0x3F)*2) - 110;
5348 #endif
5349
5350 //Get Rx snr value in DB
5351 tmp_rxsnr = pofdm_buf->rxsnr_X[i];
5352 rx_snrX = (char)(tmp_rxsnr);
5353 rx_snrX /= 2;
5354 priv->stats.rxSNRdB[i] = (long)rx_snrX;
5355
5356 /* Translate DBM to percentage. */
5357 RSSI = rtl819x_query_rxpwrpercentage(rx_pwr[i]);
5358 if (priv->brfpath_rxenable[i])
5359 total_rssi += RSSI;
5360
5361 /* Record Signal Strength for next packet */
5362 if(bpacket_match_bssid)
5363 {
5364 pstats->RxMIMOSignalStrength[i] =(u8) RSSI;
5365 precord_stats->RxMIMOSignalStrength[i] =(u8) RSSI;
5366 }
5367 }
5368
5369
5370 //
5371 // (2)PWDB, Average PWDB cacluated by hardware (for rate adaptive)
5372 //
5373 //Fixed by Jacken from Bryant 2008-03-20
5374 //Original value is 106
5375 rx_pwr_all = (((pofdm_buf->pwdb_all ) >> 1 )& 0x7f) -106;
5376 pwdb_all = rtl819x_query_rxpwrpercentage(rx_pwr_all);
5377
5378 pstats->RxPWDBAll = precord_stats->RxPWDBAll = pwdb_all;
5379 pstats->RxPower = precord_stats->RxPower = rx_pwr_all;
5380 pstats->RecvSignalPower = rx_pwr_all;
5381 //
5382 // (3)EVM of HT rate
5383 //
5384 if(pdrvinfo->RxHT && pdrvinfo->RxRate>=DESC90_RATEMCS8 &&
5385 pdrvinfo->RxRate<=DESC90_RATEMCS15)
5386 max_spatial_stream = 2; //both spatial stream make sense
5387 else
5388 max_spatial_stream = 1; //only spatial stream 1 makes sense
5389
5390 for(i=0; i<max_spatial_stream; i++)
5391 {
5392 tmp_rxevm = pofdm_buf->rxevm_X[i];
5393 rx_evmX = (char)(tmp_rxevm);
5394
5395 // Do not use shift operation like "rx_evmX >>= 1" because the compilor of free build environment
5396 // fill most significant bit to "zero" when doing shifting operation which may change a negative
5397 // value to positive one, then the dbm value (which is supposed to be negative) is not correct anymore.
5398 rx_evmX /= 2; //dbm
5399
5400 evm = rtl819x_evm_dbtopercentage(rx_evmX);
5401 #if 0
5402 EVM = SignalScaleMapping(EVM);//make it good looking, from 0~100
5403 #endif
5404 if(bpacket_match_bssid)
5405 {
5406 if(i==0) // Fill value in RFD, Get the first spatial stream only
5407 pstats->SignalQuality = precord_stats->SignalQuality = (u8)(evm & 0xff);
5408 pstats->RxMIMOSignalQuality[i] = precord_stats->RxMIMOSignalQuality[i] = (u8)(evm & 0xff);
5409 }
5410 }
5411
5412
5413 /* record rx statistics for debug */
5414 rxsc_sgien_exflg = pofdm_buf->rxsc_sgien_exflg;
5415 prxsc = (phy_ofdm_rx_status_rxsc_sgien_exintfflag *)&rxsc_sgien_exflg;
5416 if(pdrvinfo->BW) //40M channel
5417 priv->stats.received_bwtype[1+prxsc->rxsc]++;
5418 else //20M channel
5419 priv->stats.received_bwtype[0]++;
5420 }
5421
5422 //UI BSS List signal strength(in percentage), make it good looking, from 0~100.
5423 //It is assigned to the BSS List in GetValueFromBeaconOrProbeRsp().
5424 if(is_cck_rate)
5425 {
5426 pstats->SignalStrength = precord_stats->SignalStrength = (u8)(rtl819x_signal_scale_mapping((long)pwdb_all));//PWDB_ALL;
5427
5428 }
5429 else
5430 {
5431 //pRfd->Status.SignalStrength = pRecordRfd->Status.SignalStrength = (u1Byte)(SignalScaleMapping(total_rssi/=RF90_PATH_MAX));//(u1Byte)(total_rssi/=RF90_PATH_MAX);
5432 // We can judge RX path number now.
5433 if (rf_rx_num != 0)
5434 pstats->SignalStrength = precord_stats->SignalStrength = (u8)(rtl819x_signal_scale_mapping((long)(total_rssi/=rf_rx_num)));
5435 }
5436 }
5437
5438 static void
5439 rtl8192_record_rxdesc_forlateruse(
5440 struct ieee80211_rx_stats * psrc_stats,
5441 struct ieee80211_rx_stats * ptarget_stats
5442 )
5443 {
5444 ptarget_stats->bIsAMPDU = psrc_stats->bIsAMPDU;
5445 ptarget_stats->bFirstMPDU = psrc_stats->bFirstMPDU;
5446 //ptarget_stats->Seq_Num = psrc_stats->Seq_Num;
5447 }
5448
5449
5450
5451 static void TranslateRxSignalStuff819xpci(struct net_device *dev,
5452 struct sk_buff *skb,
5453 struct ieee80211_rx_stats * pstats,
5454 prx_desc_819x_pci pdesc,
5455 prx_fwinfo_819x_pci pdrvinfo)
5456 {
5457 // TODO: We must only check packet for current MAC address. Not finish
5458 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
5459 bool bpacket_match_bssid, bpacket_toself;
5460 bool bPacketBeacon=false, bToSelfBA=false;
5461 static struct ieee80211_rx_stats previous_stats;
5462 struct ieee80211_hdr_3addr *hdr;
5463 u16 fc,type;
5464
5465 // Get Signal Quality for only RX data queue (but not command queue)
5466
5467 u8* tmp_buf;
5468 u8 *praddr;
5469
5470 /* Get MAC frame start address. */
5471 tmp_buf = skb->data;
5472
5473 hdr = (struct ieee80211_hdr_3addr *)tmp_buf;
5474 fc = le16_to_cpu(hdr->frame_ctl);
5475 type = WLAN_FC_GET_TYPE(fc);
5476 praddr = hdr->addr1;
5477
5478 /* Check if the received packet is acceptabe. */
5479 bpacket_match_bssid = ((IEEE80211_FTYPE_CTL != type) &&
5480 (!compare_ether_addr(priv->ieee80211->current_network.bssid, (fc & IEEE80211_FCTL_TODS)? hdr->addr1 : (fc & IEEE80211_FCTL_FROMDS )? hdr->addr2 : hdr->addr3))
5481 && (!pstats->bHwError) && (!pstats->bCRC)&& (!pstats->bICV));
5482 bpacket_toself = bpacket_match_bssid & (!compare_ether_addr(praddr, priv->ieee80211->dev->dev_addr));
5483 #if 1//cosa
5484 if(WLAN_FC_GET_FRAMETYPE(fc)== IEEE80211_STYPE_BEACON)
5485 {
5486 bPacketBeacon = true;
5487 //DbgPrint("Beacon 2, MatchBSSID = %d, ToSelf = %d \n", bPacketMatchBSSID, bPacketToSelf);
5488 }
5489 if(WLAN_FC_GET_FRAMETYPE(fc) == IEEE80211_STYPE_BLOCKACK)
5490 {
5491 if((!compare_ether_addr(praddr,dev->dev_addr)))
5492 bToSelfBA = true;
5493 //DbgPrint("BlockAck, MatchBSSID = %d, ToSelf = %d \n", bPacketMatchBSSID, bPacketToSelf);
5494 }
5495
5496 #endif
5497 if(bpacket_match_bssid)
5498 {
5499 priv->stats.numpacket_matchbssid++;
5500 }
5501 if(bpacket_toself){
5502 priv->stats.numpacket_toself++;
5503 }
5504 //
5505 // Process PHY information for previous packet (RSSI/PWDB/EVM)
5506 //
5507 // Because phy information is contained in the last packet of AMPDU only, so driver
5508 // should process phy information of previous packet
5509 rtl8192_process_phyinfo(priv, tmp_buf,&previous_stats, pstats);
5510 rtl8192_query_rxphystatus(priv, pstats, pdesc, pdrvinfo, &previous_stats, bpacket_match_bssid,
5511 bpacket_toself ,bPacketBeacon, bToSelfBA);
5512 rtl8192_record_rxdesc_forlateruse(pstats, &previous_stats);
5513
5514 }
5515
5516
5517 static void rtl8192_tx_resume(struct net_device *dev)
5518 {
5519 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
5520 struct ieee80211_device *ieee = priv->ieee80211;
5521 struct sk_buff *skb;
5522 int queue_index;
5523
5524 for(queue_index = BK_QUEUE; queue_index < TXCMD_QUEUE;queue_index++) {
5525 while((!skb_queue_empty(&ieee->skb_waitQ[queue_index]))&&
5526 (priv->ieee80211->check_nic_enough_desc(dev,queue_index) > 0)) {
5527 /* 1. dequeue the packet from the wait queue */
5528 skb = skb_dequeue(&ieee->skb_waitQ[queue_index]);
5529 /* 2. tx the packet directly */
5530 ieee->softmac_data_hard_start_xmit(skb,dev,0/* rate useless now*/);
5531 #if 0
5532 if(queue_index!=MGNT_QUEUE) {
5533 ieee->stats.tx_packets++;
5534 ieee->stats.tx_bytes += skb->len;
5535 }
5536 #endif
5537 }
5538 }
5539 }
5540
5541 static void rtl8192_irq_tx_tasklet(struct r8192_priv *priv)
5542 {
5543 rtl8192_tx_resume(priv->ieee80211->dev);
5544 }
5545
5546 /* Record the received data rate */
5547 static void UpdateReceivedRateHistogramStatistics8190(
5548 struct net_device *dev,
5549 struct ieee80211_rx_stats* pstats
5550 )
5551 {
5552 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
5553 u32 rcvType=1; //0: Total, 1:OK, 2:CRC, 3:ICV
5554 u32 rateIndex;
5555 u32 preamble_guardinterval; //1: short preamble/GI, 0: long preamble/GI
5556
5557 if(pstats->bCRC)
5558 rcvType = 2;
5559 else if(pstats->bICV)
5560 rcvType = 3;
5561
5562 if(pstats->bShortPreamble)
5563 preamble_guardinterval = 1;// short
5564 else
5565 preamble_guardinterval = 0;// long
5566
5567 switch(pstats->rate)
5568 {
5569 //
5570 // CCK rate
5571 //
5572 case MGN_1M: rateIndex = 0; break;
5573 case MGN_2M: rateIndex = 1; break;
5574 case MGN_5_5M: rateIndex = 2; break;
5575 case MGN_11M: rateIndex = 3; break;
5576 //
5577 // Legacy OFDM rate
5578 //
5579 case MGN_6M: rateIndex = 4; break;
5580 case MGN_9M: rateIndex = 5; break;
5581 case MGN_12M: rateIndex = 6; break;
5582 case MGN_18M: rateIndex = 7; break;
5583 case MGN_24M: rateIndex = 8; break;
5584 case MGN_36M: rateIndex = 9; break;
5585 case MGN_48M: rateIndex = 10; break;
5586 case MGN_54M: rateIndex = 11; break;
5587 //
5588 // 11n High throughput rate
5589 //
5590 case MGN_MCS0: rateIndex = 12; break;
5591 case MGN_MCS1: rateIndex = 13; break;
5592 case MGN_MCS2: rateIndex = 14; break;
5593 case MGN_MCS3: rateIndex = 15; break;
5594 case MGN_MCS4: rateIndex = 16; break;
5595 case MGN_MCS5: rateIndex = 17; break;
5596 case MGN_MCS6: rateIndex = 18; break;
5597 case MGN_MCS7: rateIndex = 19; break;
5598 case MGN_MCS8: rateIndex = 20; break;
5599 case MGN_MCS9: rateIndex = 21; break;
5600 case MGN_MCS10: rateIndex = 22; break;
5601 case MGN_MCS11: rateIndex = 23; break;
5602 case MGN_MCS12: rateIndex = 24; break;
5603 case MGN_MCS13: rateIndex = 25; break;
5604 case MGN_MCS14: rateIndex = 26; break;
5605 case MGN_MCS15: rateIndex = 27; break;
5606 default: rateIndex = 28; break;
5607 }
5608 priv->stats.received_preamble_GI[preamble_guardinterval][rateIndex]++;
5609 priv->stats.received_rate_histogram[0][rateIndex]++; //total
5610 priv->stats.received_rate_histogram[rcvType][rateIndex]++;
5611 }
5612
5613 static void rtl8192_rx(struct net_device *dev)
5614 {
5615 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
5616 struct ieee80211_hdr_1addr *ieee80211_hdr = NULL;
5617 bool unicast_packet = false;
5618 struct ieee80211_rx_stats stats = {
5619 .signal = 0,
5620 .noise = -98,
5621 .rate = 0,
5622 .freq = IEEE80211_24GHZ_BAND,
5623 };
5624 unsigned int count = priv->rxringcount;
5625
5626 stats.nic_type = NIC_8192E;
5627
5628 while (count--) {
5629 rx_desc_819x_pci *pdesc = &priv->rx_ring[priv->rx_idx];//rx descriptor
5630 struct sk_buff *skb = priv->rx_buf[priv->rx_idx];//rx pkt
5631
5632 if (pdesc->OWN){
5633 /* wait data to be filled by hardware */
5634 return;
5635 } else {
5636 stats.bICV = pdesc->ICV;
5637 stats.bCRC = pdesc->CRC32;
5638 stats.bHwError = pdesc->CRC32 | pdesc->ICV;
5639
5640 stats.Length = pdesc->Length;
5641 if(stats.Length < 24)
5642 stats.bHwError |= 1;
5643
5644 if(stats.bHwError) {
5645 stats.bShift = false;
5646
5647 if(pdesc->CRC32) {
5648 if (pdesc->Length <500)
5649 priv->stats.rxcrcerrmin++;
5650 else if (pdesc->Length >1000)
5651 priv->stats.rxcrcerrmax++;
5652 else
5653 priv->stats.rxcrcerrmid++;
5654 }
5655 goto done;
5656 } else {
5657 prx_fwinfo_819x_pci pDrvInfo = NULL;
5658 struct sk_buff *new_skb = dev_alloc_skb(priv->rxbuffersize);
5659
5660 if (unlikely(!new_skb)) {
5661 goto done;
5662 }
5663
5664 stats.RxDrvInfoSize = pdesc->RxDrvInfoSize;
5665 stats.RxBufShift = ((pdesc->Shift)&0x03);
5666 stats.Decrypted = !pdesc->SWDec;
5667
5668 pci_dma_sync_single_for_cpu(priv->pdev,
5669 *((dma_addr_t *)skb->cb),
5670 priv->rxbuffersize,
5671 PCI_DMA_FROMDEVICE);
5672 skb_put(skb, pdesc->Length);
5673 pDrvInfo = (rx_fwinfo_819x_pci *)(skb->data + stats.RxBufShift);
5674 skb_reserve(skb, stats.RxDrvInfoSize + stats.RxBufShift);
5675
5676 stats.rate = HwRateToMRate90((bool)pDrvInfo->RxHT, (u8)pDrvInfo->RxRate);
5677 stats.bShortPreamble = pDrvInfo->SPLCP;
5678
5679 /* it is debug only. It should be disabled in released driver.
5680 * 2007.1.11 by Emily
5681 * */
5682 UpdateReceivedRateHistogramStatistics8190(dev, &stats);
5683
5684 stats.bIsAMPDU = (pDrvInfo->PartAggr==1);
5685 stats.bFirstMPDU = (pDrvInfo->PartAggr==1) && (pDrvInfo->FirstAGGR==1);
5686
5687 stats.TimeStampLow = pDrvInfo->TSFL;
5688 stats.TimeStampHigh = read_nic_dword(dev, TSFR+4);
5689
5690 UpdateRxPktTimeStamp8190(dev, &stats);
5691
5692 //
5693 // Get Total offset of MPDU Frame Body
5694 //
5695 if((stats.RxBufShift + stats.RxDrvInfoSize) > 0)
5696 stats.bShift = 1;
5697
5698 stats.RxIs40MHzPacket = pDrvInfo->BW;
5699
5700 /* ???? */
5701 TranslateRxSignalStuff819xpci(dev,skb, &stats, pdesc, pDrvInfo);
5702
5703 /* Rx A-MPDU */
5704 if(pDrvInfo->FirstAGGR==1 || pDrvInfo->PartAggr == 1)
5705 RT_TRACE(COMP_RXDESC, "pDrvInfo->FirstAGGR = %d, pDrvInfo->PartAggr = %d\n",
5706 pDrvInfo->FirstAGGR, pDrvInfo->PartAggr);
5707 skb_trim(skb, skb->len - 4/*sCrcLng*/);
5708 /* rx packets statistics */
5709 ieee80211_hdr = (struct ieee80211_hdr_1addr *)skb->data;
5710 unicast_packet = false;
5711
5712 if(is_broadcast_ether_addr(ieee80211_hdr->addr1)) {
5713 //TODO
5714 }else if(is_multicast_ether_addr(ieee80211_hdr->addr1)){
5715 //TODO
5716 }else {
5717 /* unicast packet */
5718 unicast_packet = true;
5719 }
5720
5721 stats.packetlength = stats.Length-4;
5722 stats.fraglength = stats.packetlength;
5723 stats.fragoffset = 0;
5724 stats.ntotalfrag = 1;
5725
5726 if(!ieee80211_rtl_rx(priv->ieee80211, skb, &stats)){
5727 dev_kfree_skb_any(skb);
5728 } else {
5729 priv->stats.rxok++;
5730 if(unicast_packet) {
5731 priv->stats.rxbytesunicast += skb->len;
5732 }
5733 }
5734
5735 pci_unmap_single(priv->pdev, *((dma_addr_t *) skb->cb),
5736 priv->rxbuffersize, PCI_DMA_FROMDEVICE);
5737
5738 skb = new_skb;
5739 priv->rx_buf[priv->rx_idx] = skb;
5740 *((dma_addr_t *) skb->cb) = pci_map_single(priv->pdev, skb_tail_pointer(skb), priv->rxbuffersize, PCI_DMA_FROMDEVICE);
5741 }
5742
5743 }
5744 done:
5745 pdesc->BufferAddress = cpu_to_le32(*((dma_addr_t *)skb->cb));
5746 pdesc->OWN = 1;
5747 pdesc->Length = priv->rxbuffersize;
5748 if (priv->rx_idx == priv->rxringcount-1)
5749 pdesc->EOR = 1;
5750 priv->rx_idx = (priv->rx_idx + 1) % priv->rxringcount;
5751 }
5752
5753 }
5754
5755 static void rtl8192_irq_rx_tasklet(struct r8192_priv *priv)
5756 {
5757 rtl8192_rx(priv->ieee80211->dev);
5758 /* unmask RDU */
5759 write_nic_dword(priv->ieee80211->dev, INTA_MASK,read_nic_dword(priv->ieee80211->dev, INTA_MASK) | IMR_RDU);
5760 }
5761
5762 static const struct net_device_ops rtl8192_netdev_ops = {
5763 .ndo_open = rtl8192_open,
5764 .ndo_stop = rtl8192_close,
5765 .ndo_tx_timeout = tx_timeout,
5766 .ndo_do_ioctl = rtl8192_ioctl,
5767 .ndo_set_multicast_list = r8192_set_multicast,
5768 .ndo_set_mac_address = r8192_set_mac_adr,
5769 .ndo_start_xmit = ieee80211_rtl_xmit,
5770 };
5771
5772 static int __devinit rtl8192_pci_probe(struct pci_dev *pdev,
5773 const struct pci_device_id *id)
5774 {
5775 unsigned long ioaddr = 0;
5776 struct net_device *dev = NULL;
5777 struct r8192_priv *priv= NULL;
5778 u8 unit = 0;
5779 int ret = -ENODEV;
5780
5781 #ifdef CONFIG_RTL8192_IO_MAP
5782 unsigned long pio_start, pio_len, pio_flags;
5783 #else
5784 unsigned long pmem_start, pmem_len, pmem_flags;
5785 #endif //end #ifdef RTL_IO_MAP
5786
5787 RT_TRACE(COMP_INIT,"Configuring chip resources");
5788
5789 if( pci_enable_device (pdev) ){
5790 RT_TRACE(COMP_ERR,"Failed to enable PCI device");
5791 return -EIO;
5792 }
5793
5794 pci_set_master(pdev);
5795 //pci_set_wmi(pdev);
5796 pci_set_dma_mask(pdev, 0xffffff00ULL);
5797 pci_set_consistent_dma_mask(pdev,0xffffff00ULL);
5798 dev = alloc_ieee80211(sizeof(struct r8192_priv));
5799 if (!dev) {
5800 ret = -ENOMEM;
5801 goto fail_free;
5802 }
5803
5804 pci_set_drvdata(pdev, dev);
5805 SET_NETDEV_DEV(dev, &pdev->dev);
5806 priv = ieee80211_priv(dev);
5807 priv->ieee80211 = netdev_priv(dev);
5808 priv->pdev=pdev;
5809 if((pdev->subsystem_vendor == PCI_VENDOR_ID_DLINK)&&(pdev->subsystem_device == 0x3304)){
5810 priv->ieee80211->bSupportRemoteWakeUp = 1;
5811 } else
5812 {
5813 priv->ieee80211->bSupportRemoteWakeUp = 0;
5814 }
5815
5816 #ifdef CONFIG_RTL8192_IO_MAP
5817
5818 pio_start = (unsigned long)pci_resource_start (pdev, 0);
5819 pio_len = (unsigned long)pci_resource_len (pdev, 0);
5820 pio_flags = (unsigned long)pci_resource_flags (pdev, 0);
5821
5822 if (!(pio_flags & IORESOURCE_IO)) {
5823 RT_TRACE(COMP_ERR,"region #0 not a PIO resource, aborting");
5824 goto fail;
5825 }
5826
5827 //DMESG("IO space @ 0x%08lx", pio_start );
5828 if( ! request_region( pio_start, pio_len, RTL819xE_MODULE_NAME ) ){
5829 RT_TRACE(COMP_ERR,"request_region failed!");
5830 goto fail;
5831 }
5832
5833 ioaddr = pio_start;
5834 dev->base_addr = ioaddr; // device I/O address
5835
5836 #else
5837
5838 pmem_start = pci_resource_start(pdev, 1);
5839 pmem_len = pci_resource_len(pdev, 1);
5840 pmem_flags = pci_resource_flags (pdev, 1);
5841
5842 if (!(pmem_flags & IORESOURCE_MEM)) {
5843 RT_TRACE(COMP_ERR,"region #1 not a MMIO resource, aborting");
5844 goto fail;
5845 }
5846
5847 //DMESG("Memory mapped space @ 0x%08lx ", pmem_start);
5848 if( ! request_mem_region(pmem_start, pmem_len, RTL819xE_MODULE_NAME)) {
5849 RT_TRACE(COMP_ERR,"request_mem_region failed!");
5850 goto fail;
5851 }
5852
5853
5854 ioaddr = (unsigned long)ioremap_nocache( pmem_start, pmem_len);
5855 if( ioaddr == (unsigned long)NULL ){
5856 RT_TRACE(COMP_ERR,"ioremap failed!");
5857 // release_mem_region( pmem_start, pmem_len );
5858 goto fail1;
5859 }
5860
5861 dev->mem_start = ioaddr; // shared mem start
5862 dev->mem_end = ioaddr + pci_resource_len(pdev, 0); // shared mem end
5863
5864 #endif //end #ifdef RTL_IO_MAP
5865
5866 /* We disable the RETRY_TIMEOUT register (0x41) to keep
5867 * PCI Tx retries from interfering with C3 CPU state */
5868 pci_write_config_byte(pdev, 0x41, 0x00);
5869
5870
5871 pci_read_config_byte(pdev, 0x05, &unit);
5872 pci_write_config_byte(pdev, 0x05, unit & (~0x04));
5873
5874 dev->irq = pdev->irq;
5875 priv->irq = 0;
5876
5877 dev->netdev_ops = &rtl8192_netdev_ops;
5878 #if 0
5879 dev->open = rtl8192_open;
5880 dev->stop = rtl8192_close;
5881 //dev->hard_start_xmit = rtl8192_8023_hard_start_xmit;
5882 dev->tx_timeout = tx_timeout;
5883 //dev->wireless_handlers = &r8192_wx_handlers_def;
5884 dev->do_ioctl = rtl8192_ioctl;
5885 dev->set_multicast_list = r8192_set_multicast;
5886 dev->set_mac_address = r8192_set_mac_adr;
5887 #endif
5888
5889 //DMESG("Oops: i'm coming\n");
5890 #if WIRELESS_EXT >= 12
5891 #if WIRELESS_EXT < 17
5892 dev->get_wireless_stats = r8192_get_wireless_stats;
5893 #endif
5894 dev->wireless_handlers = (struct iw_handler_def *) &r8192_wx_handlers_def;
5895 #endif
5896 //dev->get_wireless_stats = r8192_get_wireless_stats;
5897 dev->type=ARPHRD_ETHER;
5898
5899 dev->watchdog_timeo = HZ*3; //modified by john, 0805
5900
5901 if (dev_alloc_name(dev, ifname) < 0){
5902 RT_TRACE(COMP_INIT, "Oops: devname already taken! Trying wlan%%d...\n");
5903 strcpy(ifname, "wlan%d");
5904 dev_alloc_name(dev, ifname);
5905 }
5906
5907 RT_TRACE(COMP_INIT, "Driver probe completed1\n");
5908 if(rtl8192_init(dev)!=0){
5909 RT_TRACE(COMP_ERR, "Initialization failed");
5910 goto fail;
5911 }
5912
5913 netif_carrier_off(dev);
5914 netif_stop_queue(dev);
5915
5916 register_netdev(dev);
5917 RT_TRACE(COMP_INIT, "dev name=======> %s\n",dev->name);
5918 rtl8192_proc_init_one(dev);
5919
5920
5921 RT_TRACE(COMP_INIT, "Driver probe completed\n");
5922 return 0;
5923
5924 fail1:
5925
5926 #ifdef CONFIG_RTL8180_IO_MAP
5927
5928 if( dev->base_addr != 0 ){
5929
5930 release_region(dev->base_addr,
5931 pci_resource_len(pdev, 0) );
5932 }
5933 #else
5934 if( dev->mem_start != (unsigned long)NULL ){
5935 iounmap( (void *)dev->mem_start );
5936 release_mem_region( pci_resource_start(pdev, 1),
5937 pci_resource_len(pdev, 1) );
5938 }
5939 #endif //end #ifdef RTL_IO_MAP
5940
5941 fail:
5942 if(dev){
5943
5944 if (priv->irq) {
5945 free_irq(dev->irq, dev);
5946 dev->irq=0;
5947 }
5948 free_ieee80211(dev);
5949 }
5950
5951 fail_free:
5952 pci_disable_device(pdev);
5953
5954 DMESG("wlan driver load failed\n");
5955 pci_set_drvdata(pdev, NULL);
5956 return ret;
5957
5958 }
5959
5960 /* detach all the work and timer structure declared or inititialized
5961 * in r8192_init function.
5962 * */
5963 static void rtl8192_cancel_deferred_work(struct r8192_priv* priv)
5964 {
5965 /* call cancel_work_sync instead of cancel_delayed_work if and only if Linux_version_code
5966 * is or is newer than 2.6.20 and work structure is defined to be struct work_struct.
5967 * Otherwise call cancel_delayed_work is enough.
5968 * FIXME (2.6.20 should 2.6.22, work_struct should not cancel)
5969 * */
5970 cancel_delayed_work(&priv->watch_dog_wq);
5971 cancel_delayed_work(&priv->update_beacon_wq);
5972 cancel_delayed_work(&priv->ieee80211->hw_wakeup_wq);
5973 cancel_delayed_work(&priv->ieee80211->hw_sleep_wq);
5974 #ifdef RTL8192E
5975 cancel_delayed_work(&priv->gpio_change_rf_wq);
5976 #endif
5977 cancel_work_sync(&priv->reset_wq);
5978 cancel_work_sync(&priv->qos_activate);
5979 //cancel_work_sync(&priv->SetBWModeWorkItem);
5980 //cancel_work_sync(&priv->SwChnlWorkItem);
5981
5982 }
5983
5984
5985 static void __devexit rtl8192_pci_disconnect(struct pci_dev *pdev)
5986 {
5987 struct net_device *dev = pci_get_drvdata(pdev);
5988 struct r8192_priv *priv ;
5989
5990 if(dev){
5991
5992 unregister_netdev(dev);
5993
5994 priv=ieee80211_priv(dev);
5995
5996 rtl8192_proc_remove_one(dev);
5997
5998 rtl8192_down(dev);
5999 if (priv->pFirmware)
6000 {
6001 vfree(priv->pFirmware);
6002 priv->pFirmware = NULL;
6003 }
6004 // priv->rf_close(dev);
6005 // rtl8192_usb_deleteendpoints(dev);
6006 destroy_workqueue(priv->priv_wq);
6007 /* redundant with rtl8192_down */
6008 // rtl8192_irq_disable(dev);
6009 // rtl8192_reset(dev);
6010 // mdelay(10);
6011 {
6012 u32 i;
6013 /* free tx/rx rings */
6014 rtl8192_free_rx_ring(dev);
6015 for (i = 0; i < MAX_TX_QUEUE_COUNT; i++) {
6016 rtl8192_free_tx_ring(dev, i);
6017 }
6018 }
6019 if(priv->irq){
6020
6021 printk("Freeing irq %d\n",dev->irq);
6022 free_irq(dev->irq, dev);
6023 priv->irq=0;
6024
6025 }
6026
6027 #ifdef CONFIG_RTL8180_IO_MAP
6028
6029 if( dev->base_addr != 0 ){
6030
6031 release_region(dev->base_addr,
6032 pci_resource_len(pdev, 0) );
6033 }
6034 #else
6035 if( dev->mem_start != (unsigned long)NULL ){
6036 iounmap( (void *)dev->mem_start );
6037 release_mem_region( pci_resource_start(pdev, 1),
6038 pci_resource_len(pdev, 1) );
6039 }
6040 #endif /*end #ifdef RTL_IO_MAP*/
6041 free_ieee80211(dev);
6042
6043 }
6044
6045 pci_disable_device(pdev);
6046 RT_TRACE(COMP_DOWN, "wlan driver removed\n");
6047 }
6048
6049 extern int ieee80211_rtl_init(void);
6050 extern void ieee80211_rtl_exit(void);
6051
6052 static int __init rtl8192_pci_module_init(void)
6053 {
6054 int retval;
6055
6056 retval = ieee80211_rtl_init();
6057 if (retval)
6058 return retval;
6059
6060 printk(KERN_INFO "\nLinux kernel driver for RTL8192 based WLAN cards\n");
6061 printk(KERN_INFO "Copyright (c) 2007-2008, Realsil Wlan\n");
6062 RT_TRACE(COMP_INIT, "Initializing module");
6063 RT_TRACE(COMP_INIT, "Wireless extensions version %d", WIRELESS_EXT);
6064 rtl8192_proc_module_init();
6065 if(0!=pci_register_driver(&rtl8192_pci_driver))
6066 {
6067 DMESG("No device found");
6068 /*pci_unregister_driver (&rtl8192_pci_driver);*/
6069 return -ENODEV;
6070 }
6071 return 0;
6072 }
6073
6074
6075 static void __exit rtl8192_pci_module_exit(void)
6076 {
6077 pci_unregister_driver(&rtl8192_pci_driver);
6078
6079 RT_TRACE(COMP_DOWN, "Exiting");
6080 rtl8192_proc_module_remove();
6081 ieee80211_rtl_exit();
6082 }
6083
6084 static irqreturn_t rtl8192_interrupt(int irq, void *netdev)
6085 {
6086 struct net_device *dev = (struct net_device *) netdev;
6087 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
6088 unsigned long flags;
6089 u32 inta;
6090 irqreturn_t ret = IRQ_HANDLED;
6091
6092 spin_lock_irqsave(&priv->irq_th_lock, flags);
6093
6094 /* We should return IRQ_NONE, but for now let me keep this */
6095 if (priv->irq_enabled == 0)
6096 goto out_unlock;
6097
6098 /* ISR: 4bytes */
6099
6100 inta = read_nic_dword(dev, ISR); /* & priv->IntrMask; */
6101 write_nic_dword(dev, ISR, inta); /* reset int situation */
6102
6103 priv->stats.shints++;
6104 if (!inta) {
6105 /*
6106 * most probably we can safely return IRQ_NONE,
6107 * but for now is better to avoid problems
6108 */
6109 goto out_unlock;
6110 }
6111
6112 if (inta == 0xffff) {
6113 /* HW disappared */
6114 goto out_unlock;
6115 }
6116
6117 priv->stats.ints++;
6118 #ifdef DEBUG_IRQ
6119 DMESG("NIC irq %x",inta);
6120 #endif
6121
6122 if (!netif_running(dev))
6123 goto out_unlock;
6124
6125 if (inta & IMR_TBDOK) {
6126 RT_TRACE(COMP_INTR, "beacon ok interrupt!\n");
6127 rtl8192_tx_isr(dev, BEACON_QUEUE);
6128 priv->stats.txbeaconokint++;
6129 }
6130
6131 if (inta & IMR_TBDER) {
6132 RT_TRACE(COMP_INTR, "beacon ok interrupt!\n");
6133 rtl8192_tx_isr(dev, BEACON_QUEUE);
6134 priv->stats.txbeaconerr++;
6135 }
6136
6137 if (inta & IMR_MGNTDOK ) {
6138 RT_TRACE(COMP_INTR, "Manage ok interrupt!\n");
6139 priv->stats.txmanageokint++;
6140 rtl8192_tx_isr(dev,MGNT_QUEUE);
6141 }
6142
6143 if (inta & IMR_COMDOK)
6144 {
6145 priv->stats.txcmdpktokint++;
6146 rtl8192_tx_isr(dev, TXCMD_QUEUE);
6147 }
6148
6149 if (inta & IMR_ROK) {
6150 #ifdef DEBUG_RX
6151 DMESG("Frame arrived !");
6152 #endif
6153 priv->stats.rxint++;
6154 tasklet_schedule(&priv->irq_rx_tasklet);
6155 }
6156
6157 if (inta & IMR_BcnInt) {
6158 RT_TRACE(COMP_INTR, "prepare beacon for interrupt!\n");
6159 tasklet_schedule(&priv->irq_prepare_beacon_tasklet);
6160 }
6161
6162 if (inta & IMR_RDU) {
6163 RT_TRACE(COMP_INTR, "rx descriptor unavailable!\n");
6164 priv->stats.rxrdu++;
6165 /* reset int situation */
6166 write_nic_dword(dev, INTA_MASK, read_nic_dword(dev, INTA_MASK) & ~IMR_RDU);
6167 tasklet_schedule(&priv->irq_rx_tasklet);
6168 }
6169
6170 if (inta & IMR_RXFOVW) {
6171 RT_TRACE(COMP_INTR, "rx overflow !\n");
6172 priv->stats.rxoverflow++;
6173 tasklet_schedule(&priv->irq_rx_tasklet);
6174 }
6175
6176 if (inta & IMR_TXFOVW)
6177 priv->stats.txoverflow++;
6178
6179 if (inta & IMR_BKDOK) {
6180 RT_TRACE(COMP_INTR, "BK Tx OK interrupt!\n");
6181 priv->stats.txbkokint++;
6182 priv->ieee80211->LinkDetectInfo.NumTxOkInPeriod++;
6183 rtl8192_tx_isr(dev, BK_QUEUE);
6184 }
6185
6186 if (inta & IMR_BEDOK) {
6187 RT_TRACE(COMP_INTR, "BE TX OK interrupt!\n");
6188 priv->stats.txbeokint++;
6189 priv->ieee80211->LinkDetectInfo.NumTxOkInPeriod++;
6190 rtl8192_tx_isr(dev, BE_QUEUE);
6191 }
6192
6193 if (inta & IMR_VIDOK) {
6194 RT_TRACE(COMP_INTR, "VI TX OK interrupt!\n");
6195 priv->stats.txviokint++;
6196 priv->ieee80211->LinkDetectInfo.NumTxOkInPeriod++;
6197 rtl8192_tx_isr(dev, VI_QUEUE);
6198 }
6199
6200 if (inta & IMR_VODOK) {
6201 priv->stats.txvookint++;
6202 priv->ieee80211->LinkDetectInfo.NumTxOkInPeriod++;
6203 rtl8192_tx_isr(dev, VO_QUEUE);
6204 }
6205
6206 out_unlock:
6207 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
6208
6209 return ret;
6210 }
6211
6212 void EnableHWSecurityConfig8192(struct net_device *dev)
6213 {
6214 u8 SECR_value = 0x0;
6215 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
6216 struct ieee80211_device* ieee = priv->ieee80211;
6217
6218 SECR_value = SCR_TxEncEnable | SCR_RxDecEnable;
6219 #if 1
6220 if (((KEY_TYPE_WEP40 == ieee->pairwise_key_type) || (KEY_TYPE_WEP104 == ieee->pairwise_key_type)) && (priv->ieee80211->auth_mode != 2))
6221 {
6222 SECR_value |= SCR_RxUseDK;
6223 SECR_value |= SCR_TxUseDK;
6224 }
6225 else if ((ieee->iw_mode == IW_MODE_ADHOC) && (ieee->pairwise_key_type & (KEY_TYPE_CCMP | KEY_TYPE_TKIP)))
6226 {
6227 SECR_value |= SCR_RxUseDK;
6228 SECR_value |= SCR_TxUseDK;
6229 }
6230
6231 #endif
6232
6233 //add HWSec active enable here.
6234 //default using hwsec. when peer AP is in N mode only and pairwise_key_type is none_aes(which HT_IOT_ACT_PURE_N_MODE indicates it), use software security. when peer AP is in b,g,n mode mixed and pairwise_key_type is none_aes, use g mode hw security. WB on 2008.7.4
6235 ieee->hwsec_active = 1;
6236
6237 if ((ieee->pHTInfo->IOTAction&HT_IOT_ACT_PURE_N_MODE) || !hwwep)//!ieee->hwsec_support) //add hwsec_support flag to totol control hw_sec on/off
6238 {
6239 ieee->hwsec_active = 0;
6240 SECR_value &= ~SCR_RxDecEnable;
6241 }
6242
6243 RT_TRACE(COMP_SEC,"%s:, hwsec:%d, pairwise_key:%d, SECR_value:%x\n", __FUNCTION__,
6244 ieee->hwsec_active, ieee->pairwise_key_type, SECR_value);
6245 {
6246 write_nic_byte(dev, SECR, SECR_value);//SECR_value | SCR_UseDK );
6247 }
6248
6249 }
6250 #define TOTAL_CAM_ENTRY 32
6251 //#define CAM_CONTENT_COUNT 8
6252 void setKey( struct net_device *dev,
6253 u8 EntryNo,
6254 u8 KeyIndex,
6255 u16 KeyType,
6256 const u8 *MacAddr,
6257 u8 DefaultKey,
6258 u32 *KeyContent )
6259 {
6260 u32 TargetCommand = 0;
6261 u32 TargetContent = 0;
6262 u16 usConfig = 0;
6263 u8 i;
6264 #ifdef ENABLE_IPS
6265 struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
6266 RT_RF_POWER_STATE rtState;
6267 rtState = priv->ieee80211->eRFPowerState;
6268 if(priv->ieee80211->PowerSaveControl.bInactivePs){
6269 if(rtState == eRfOff){
6270 if(priv->ieee80211->RfOffReason > RF_CHANGE_BY_IPS)
6271 {
6272 RT_TRACE(COMP_ERR, "%s(): RF is OFF.\n",__FUNCTION__);
6273 //up(&priv->wx_sem);
6274 return ;
6275 }
6276 else{
6277 down(&priv->ieee80211->ips_sem);
6278 IPSLeave(dev);
6279 up(&priv->ieee80211->ips_sem);
6280 }
6281 }
6282 }
6283 priv->ieee80211->is_set_key = true;
6284 #endif
6285 if (EntryNo >= TOTAL_CAM_ENTRY)
6286 RT_TRACE(COMP_ERR, "cam entry exceeds in setKey()\n");
6287
6288 RT_TRACE(COMP_SEC, "====>to setKey(), dev:%p, EntryNo:%d, KeyIndex:%d, KeyType:%d, MacAddr%pM\n", dev,EntryNo, KeyIndex, KeyType, MacAddr);
6289
6290 if (DefaultKey)
6291 usConfig |= BIT15 | (KeyType<<2);
6292 else
6293 usConfig |= BIT15 | (KeyType<<2) | KeyIndex;
6294 // usConfig |= BIT15 | (KeyType<<2) | (DefaultKey<<5) | KeyIndex;
6295
6296
6297 for(i=0 ; i<CAM_CONTENT_COUNT; i++){
6298 TargetCommand = i+CAM_CONTENT_COUNT*EntryNo;
6299 TargetCommand |= BIT31|BIT16;
6300
6301 if(i==0){//MAC|Config
6302 TargetContent = (u32)(*(MacAddr+0)) << 16|
6303 (u32)(*(MacAddr+1)) << 24|
6304 (u32)usConfig;
6305
6306 write_nic_dword(dev, WCAMI, TargetContent);
6307 write_nic_dword(dev, RWCAM, TargetCommand);
6308 // printk("setkey cam =%8x\n", read_cam(dev, i+6*EntryNo));
6309 }
6310 else if(i==1){//MAC
6311 TargetContent = (u32)(*(MacAddr+2)) |
6312 (u32)(*(MacAddr+3)) << 8|
6313 (u32)(*(MacAddr+4)) << 16|
6314 (u32)(*(MacAddr+5)) << 24;
6315 write_nic_dword(dev, WCAMI, TargetContent);
6316 write_nic_dword(dev, RWCAM, TargetCommand);
6317 }
6318 else { //Key Material
6319 if(KeyContent != NULL)
6320 {
6321 write_nic_dword(dev, WCAMI, (u32)(*(KeyContent+i-2)) );
6322 write_nic_dword(dev, RWCAM, TargetCommand);
6323 }
6324 }
6325 }
6326 RT_TRACE(COMP_SEC,"=========>after set key, usconfig:%x\n", usConfig);
6327 }
6328
6329 bool NicIFEnableNIC(struct net_device* dev)
6330 {
6331 RT_STATUS init_status = RT_STATUS_SUCCESS;
6332 struct r8192_priv* priv = ieee80211_priv(dev);
6333 PRT_POWER_SAVE_CONTROL pPSC = (PRT_POWER_SAVE_CONTROL)(&(priv->ieee80211->PowerSaveControl));
6334
6335 //YJ,add,091109
6336 if (priv->up == 0){
6337 RT_TRACE(COMP_ERR, "ERR!!! %s(): Driver is already down!\n",__FUNCTION__);
6338 priv->bdisable_nic = false; //YJ,add,091111
6339 return false;
6340 }
6341 // <1> Reset memory: descriptor, buffer,..
6342 //NicIFResetMemory(Adapter);
6343
6344 // <2> Enable Adapter
6345 //priv->bfirst_init = true;
6346 init_status = rtl8192_adapter_start(dev);
6347 if (init_status != RT_STATUS_SUCCESS) {
6348 RT_TRACE(COMP_ERR,"ERR!!! %s(): initialization is failed!\n",__FUNCTION__);
6349 priv->bdisable_nic = false; //YJ,add,091111
6350 return -1;
6351 }
6352 //printk("start adapter finished\n");
6353 RT_CLEAR_PS_LEVEL(pPSC, RT_RF_OFF_LEVL_HALT_NIC);
6354 //priv->bfirst_init = false;
6355
6356 // <3> Enable Interrupt
6357 rtl8192_irq_enable(dev);
6358 priv->bdisable_nic = false;
6359
6360 return (init_status == RT_STATUS_SUCCESS);
6361 }
6362
6363 bool NicIFDisableNIC(struct net_device* dev)
6364 {
6365 bool status = true;
6366 struct r8192_priv* priv = ieee80211_priv(dev);
6367 u8 tmp_state = 0;
6368 // <1> Disable Interrupt
6369
6370 priv->bdisable_nic = true; //YJ,move,091109
6371 tmp_state = priv->ieee80211->state;
6372
6373 ieee80211_softmac_stop_protocol(priv->ieee80211, false);
6374
6375 priv->ieee80211->state = tmp_state;
6376 rtl8192_cancel_deferred_work(priv);
6377 rtl8192_irq_disable(dev);
6378 // <2> Stop all timer
6379
6380 // <3> Disable Adapter
6381 rtl8192_halt_adapter(dev, false);
6382 // priv->bdisable_nic = true;
6383
6384 return status;
6385 }
6386
6387 module_init(rtl8192_pci_module_init);
6388 module_exit(rtl8192_pci_module_exit);
This page took 0.194144 seconds and 6 git commands to generate.