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