added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / staging / rtl8187se / r8180_core.c
blob66de5cc8ddf119a85d4b66c4829bfe4c97422b90
1 /*
2 This is part of rtl818x pci OpenSource driver - v 0.1
3 Copyright (C) Andrea Merello 2004-2005 <andreamrl@tiscali.it>
4 Released under the terms of GPL (General Public License)
6 Parts of this driver are based on the GPL part of the official
7 Realtek driver.
9 Parts of this driver are based on the rtl8180 driver skeleton
10 from Patric Schenke & Andres Salomon.
12 Parts of this driver are based on the Intel Pro Wireless 2100 GPL driver.
14 Parts of BB/RF code are derived from David Young rtl8180 netbsd driver.
16 RSSI calc function from 'The Deuce'
18 Some ideas borrowed from the 8139too.c driver included in linux kernel.
20 We (I?) want to thanks the Authors of those projecs and also the
21 Ndiswrapper's project Authors.
23 A big big thanks goes also to Realtek corp. for their help in my attempt to
24 add RTL8185 and RTL8225 support, and to David Young also.
27 #if 0
28 double __floatsidf (int i) { return i; }
29 unsigned int __fixunsdfsi (double d) { return d; }
30 double __adddf3(double a, double b) { return a+b; }
31 double __addsf3(float a, float b) { return a+b; }
32 double __subdf3(double a, double b) { return a-b; }
33 double __extendsfdf2(float a) {return a;}
34 #endif
37 #undef DEBUG_TX_DESC2
38 #undef RX_DONT_PASS_UL
39 #undef DEBUG_EPROM
40 #undef DEBUG_RX_VERBOSE
41 #undef DUMMY_RX
42 #undef DEBUG_ZERO_RX
43 #undef DEBUG_RX_SKB
44 #undef DEBUG_TX_FRAG
45 #undef DEBUG_RX_FRAG
46 #undef DEBUG_TX_FILLDESC
47 #undef DEBUG_TX
48 #undef DEBUG_IRQ
49 #undef DEBUG_RX
50 #undef DEBUG_RXALLOC
51 #undef DEBUG_REGISTERS
52 #undef DEBUG_RING
53 #undef DEBUG_IRQ_TASKLET
54 #undef DEBUG_TX_ALLOC
55 #undef DEBUG_TX_DESC
57 //#define DEBUG_TX
58 //#define DEBUG_TX_DESC2
59 //#define DEBUG_RX
60 //#define DEBUG_RX_SKB
62 //#define CONFIG_RTL8180_IO_MAP
63 #include <linux/syscalls.h>
64 //#include <linux/fcntl.h>
65 //#include <asm/uaccess.h>
66 #include "r8180_hw.h"
67 #include "r8180.h"
68 #include "r8180_sa2400.h" /* PHILIPS Radio frontend */
69 #include "r8180_max2820.h" /* MAXIM Radio frontend */
70 #include "r8180_gct.h" /* GCT Radio frontend */
71 #include "r8180_rtl8225.h" /* RTL8225 Radio frontend */
72 #include "r8180_rtl8255.h" /* RTL8255 Radio frontend */
73 #include "r8180_93cx6.h" /* Card EEPROM */
74 #include "r8180_wx.h"
75 #include "r8180_dm.h"
77 #ifdef CONFIG_RTL8180_PM
78 #include "r8180_pm.h"
79 #endif
81 #ifdef ENABLE_DOT11D
82 #include "dot11d.h"
83 #endif
85 #ifdef CONFIG_RTL8185B
86 //#define CONFIG_RTL8180_IO_MAP
87 #endif
89 #ifndef PCI_VENDOR_ID_BELKIN
90 #define PCI_VENDOR_ID_BELKIN 0x1799
91 #endif
92 #ifndef PCI_VENDOR_ID_DLINK
93 #define PCI_VENDOR_ID_DLINK 0x1186
94 #endif
96 static struct pci_device_id rtl8180_pci_id_tbl[] __devinitdata = {
98 .vendor = PCI_VENDOR_ID_REALTEK,
99 // .device = 0x8180,
100 .device = 0x8199,
101 .subvendor = PCI_ANY_ID,
102 .subdevice = PCI_ANY_ID,
103 .driver_data = 0,
105 #if 0
107 .vendor = PCI_VENDOR_ID_BELKIN,
108 .device = 0x6001,
109 .subvendor = PCI_ANY_ID,
110 .subdevice = PCI_ANY_ID,
111 .driver_data = 1,
113 { /* Belkin F5D6020 v3 */
114 .vendor = PCI_VENDOR_ID_BELKIN,
115 .device = 0x6020,
116 .subvendor = PCI_ANY_ID,
117 .subdevice = PCI_ANY_ID,
118 .driver_data = 2,
120 { /* D-Link DWL-610 */
121 .vendor = PCI_VENDOR_ID_DLINK,
122 .device = 0x3300,
123 .subvendor = PCI_ANY_ID,
124 .subdevice = PCI_ANY_ID,
125 .driver_data = 3,
128 .vendor = PCI_VENDOR_ID_REALTEK,
129 .device = 0x8185,
130 .subvendor = PCI_ANY_ID,
131 .subdevice = PCI_ANY_ID,
132 .driver_data = 4,
134 #endif
136 .vendor = 0,
137 .device = 0,
138 .subvendor = 0,
139 .subdevice = 0,
140 .driver_data = 0,
145 static char* ifname = "wlan%d";
146 static int hwseqnum = 0;
147 //static char* ifname = "ath%d";
148 static int hwwep = 0;
149 static int channels = 0x3fff;
151 #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 )
152 #define cpMacAddr(des,src) ((des)[0]=(src)[0],(des)[1]=(src)[1],(des)[2]=(src)[2],(des)[3]=(src)[3],(des)[4]=(src)[4],(des)[5]=(src)[5])
153 MODULE_LICENSE("GPL");
154 MODULE_DEVICE_TABLE(pci, rtl8180_pci_id_tbl);
155 MODULE_AUTHOR("Andrea Merello <andreamrl@tiscali.it>");
156 MODULE_DESCRIPTION("Linux driver for Realtek RTL8180 / RTL8185 WiFi cards");
161 MODULE_PARM(ifname, "s");
162 MODULE_PARM_DESC(devname," Net interface name, wlan%d=default");
164 MODULE_PARM(hwseqnum,"i");
165 MODULE_PARM_DESC(hwseqnum," Try to use hardware 802.11 header sequence numbers. Zero=default");
167 MODULE_PARM(hwwep,"i");
168 MODULE_PARM_DESC(hwwep," Try to use hardware WEP support. Still broken and not available on all cards");
170 MODULE_PARM(channels,"i");
171 MODULE_PARM_DESC(channels," Channel bitmask for specific locales. NYI");
174 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 9)
175 module_param(ifname, charp, S_IRUGO|S_IWUSR );
176 module_param(hwseqnum,int, S_IRUGO|S_IWUSR);
177 module_param(hwwep,int, S_IRUGO|S_IWUSR);
178 module_param(channels,int, S_IRUGO|S_IWUSR);
179 #else
180 MODULE_PARM(ifname, "s");
181 MODULE_PARM(hwseqnum,"i");
182 MODULE_PARM(hwwep,"i");
183 MODULE_PARM(channels,"i");
184 #endif
186 MODULE_PARM_DESC(devname," Net interface name, wlan%d=default");
187 //MODULE_PARM_DESC(devname," Net interface name, ath%d=default");
188 MODULE_PARM_DESC(hwseqnum," Try to use hardware 802.11 header sequence numbers. Zero=default");
189 MODULE_PARM_DESC(hwwep," Try to use hardware WEP support. Still broken and not available on all cards");
190 MODULE_PARM_DESC(channels," Channel bitmask for specific locales. NYI");
193 static int __devinit rtl8180_pci_probe(struct pci_dev *pdev,
194 const struct pci_device_id *id);
196 static void __devexit rtl8180_pci_remove(struct pci_dev *pdev);
198 static void rtl8180_shutdown (struct pci_dev *pdev)
200 struct net_device *dev = pci_get_drvdata(pdev);
201 dev->stop(dev);
202 pci_disable_device(pdev);
205 static struct pci_driver rtl8180_pci_driver = {
206 .name = RTL8180_MODULE_NAME, /* Driver name */
207 .id_table = rtl8180_pci_id_tbl, /* PCI_ID table */
208 .probe = rtl8180_pci_probe, /* probe fn */
209 .remove = __devexit_p(rtl8180_pci_remove),/* remove fn */
210 #ifdef CONFIG_RTL8180_PM
211 .suspend = rtl8180_suspend, /* PM suspend fn */
212 .resume = rtl8180_resume, /* PM resume fn */
213 #else
214 .suspend = NULL, /* PM suspend fn */
215 .resume = NULL, /* PM resume fn */
216 #endif
217 .shutdown = rtl8180_shutdown,
222 #ifdef CONFIG_RTL8180_IO_MAP
224 u8 read_nic_byte(struct net_device *dev, int x)
226 return 0xff&inb(dev->base_addr +x);
229 u32 read_nic_dword(struct net_device *dev, int x)
231 return inl(dev->base_addr +x);
234 u16 read_nic_word(struct net_device *dev, int x)
236 return inw(dev->base_addr +x);
239 void write_nic_byte(struct net_device *dev, int x,u8 y)
241 outb(y&0xff,dev->base_addr +x);
244 void write_nic_word(struct net_device *dev, int x,u16 y)
246 outw(y,dev->base_addr +x);
249 void write_nic_dword(struct net_device *dev, int x,u32 y)
251 outl(y,dev->base_addr +x);
254 #else /* RTL_IO_MAP */
256 u8 read_nic_byte(struct net_device *dev, int x)
258 return 0xff&readb((u8*)dev->mem_start +x);
261 u32 read_nic_dword(struct net_device *dev, int x)
263 return readl((u8*)dev->mem_start +x);
266 u16 read_nic_word(struct net_device *dev, int x)
268 return readw((u8*)dev->mem_start +x);
271 void write_nic_byte(struct net_device *dev, int x,u8 y)
273 writeb(y,(u8*)dev->mem_start +x);
274 udelay(20);
277 void write_nic_dword(struct net_device *dev, int x,u32 y)
279 writel(y,(u8*)dev->mem_start +x);
280 udelay(20);
283 void write_nic_word(struct net_device *dev, int x,u16 y)
285 writew(y,(u8*)dev->mem_start +x);
286 udelay(20);
289 #endif /* RTL_IO_MAP */
295 inline void force_pci_posting(struct net_device *dev)
297 read_nic_byte(dev,EPROM_CMD);
298 #ifndef CONFIG_RTL8180_IO_MAP
299 mb();
300 #endif
304 irqreturn_t rtl8180_interrupt(int irq, void *netdev, struct pt_regs *regs);
305 void set_nic_rxring(struct net_device *dev);
306 void set_nic_txring(struct net_device *dev);
307 static struct net_device_stats *rtl8180_stats(struct net_device *dev);
308 void rtl8180_commit(struct net_device *dev);
309 void rtl8180_start_tx_beacon(struct net_device *dev);
311 /****************************************************************************
312 -----------------------------PROCFS STUFF-------------------------
313 *****************************************************************************/
315 static struct proc_dir_entry *rtl8180_proc = NULL;
317 static int proc_get_registers(char *page, char **start,
318 off_t offset, int count,
319 int *eof, void *data)
321 struct net_device *dev = data;
322 // struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
324 int len = 0;
325 int i,n;
327 int max=0xff;
329 /* This dump the current register page */
330 for(n=0;n<=max;)
332 //printk( "\nD: %2x> ", n);
333 len += snprintf(page + len, count - len,
334 "\nD: %2x > ",n);
336 for(i=0;i<16 && n<=max;i++,n++)
337 len += snprintf(page + len, count - len,
338 "%2x ",read_nic_byte(dev,n));
340 // printk("%2x ",read_nic_byte(dev,n));
342 len += snprintf(page + len, count - len,"\n");
346 *eof = 1;
347 return len;
351 int get_curr_tx_free_desc(struct net_device *dev, int priority);
353 static int proc_get_stats_hw(char *page, char **start,
354 off_t offset, int count,
355 int *eof, void *data)
357 //struct net_device *dev = data;
358 //struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
360 int len = 0;
361 #ifdef CONFIG_RTL8185B
363 #else
364 len += snprintf(page + len, count - len,
365 "NIC int: %lu\n"
366 "Total int: %lu\n"
367 "--------------------\n"
368 "LP avail desc %d\n"
369 "NP avail desc %d\n"
370 "--------------------\n"
371 "LP phys dma addr %x\n"
372 "LP NIC ptr %x\n"
373 "LP virt 32base %x\n"
374 "LP virt 32tail %x\n"
375 "--------------------\n"
376 "NP phys dma addr %x\n"
377 "NP NIC ptr %x\n"
378 "NP virt 32base %x\n"
379 "NP virt 32tail %x\n"
380 "--------------------\n"
381 "BP phys dma addr %x\n"
382 "BP NIC ptr %x\n"
383 "BP virt 32base %x\n"
384 "BP virt 32tail %x\n",
385 priv->stats.ints,
386 priv->stats.shints,
387 get_curr_tx_free_desc(dev,LOW_PRIORITY),
388 get_curr_tx_free_desc(dev,NORM_PRIORITY),
389 (u32)priv->txvipringdma,
390 read_nic_dword(dev,TLPDA),
391 (u32)priv->txvipring,
392 (u32)priv->txvipringtail,
393 (u32)priv->txvopringdma,
394 read_nic_dword(dev,TNPDA),
395 (u32)priv->txvopring,
396 (u32)priv->txvopringtail,
397 (u32)priv->txbeaconringdma,
398 read_nic_dword(dev,TBDA),
399 (u32)priv->txbeaconring,
400 (u32)priv->txbeaconringtail);
401 #endif
402 *eof = 1;
403 return len;
407 static int proc_get_stats_rx(char *page, char **start,
408 off_t offset, int count,
409 int *eof, void *data)
411 struct net_device *dev = data;
412 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
414 int len = 0;
416 len += snprintf(page + len, count - len,
417 /* "RX descriptor not available: %lu\n"
418 "RX incomplete (missing last descriptor): %lu\n"
419 "RX not data: %lu\n"
420 //"RX descriptor pointer reset: %lu\n"
421 "RX descriptor pointer lost: %lu\n"
422 //"RX pointer workaround: %lu\n"
423 "RX error int: %lu\n"
424 "RX fifo overflow: %lu\n"
425 "RX int: %lu\n"
426 "RX packet: %lu\n"
427 "RX bytes: %lu\n"
428 "RX DMA fail: %lu\n",
429 priv->stats.rxrdu,
430 priv->stats.rxnolast,
431 priv->stats.rxnodata,
432 //priv->stats.rxreset,
433 priv->stats.rxnopointer,
434 //priv->stats.rxwrkaround,
435 priv->stats.rxerr,
436 priv->stats.rxoverflow,
437 priv->stats.rxint,
438 priv->ieee80211->stats.rx_packets,
439 priv->ieee80211->stats.rx_bytes,
440 priv->stats.rxdmafail */
441 "RX OK: %lu\n"
442 "RX Retry: %lu\n"
443 "RX CRC Error(0-500): %lu\n"
444 "RX CRC Error(500-1000): %lu\n"
445 "RX CRC Error(>1000): %lu\n"
446 "RX ICV Error: %lu\n",
447 priv->stats.rxint,
448 priv->stats.rxerr,
449 priv->stats.rxcrcerrmin,
450 priv->stats.rxcrcerrmid,
451 priv->stats.rxcrcerrmax,
452 priv->stats.rxicverr
455 *eof = 1;
456 return len;
459 #if 0
460 static int proc_get_stats_ieee(char *page, char **start,
461 off_t offset, int count,
462 int *eof, void *data)
464 struct net_device *dev = data;
465 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
467 int len = 0;
469 len += snprintf(page + len, count - len,
470 "TXed association requests: %u\n"
471 "TXed authentication requests: %u\n"
472 "RXed successful association response: %u\n"
473 "RXed failed association response: %u\n"
474 "RXed successful authentication response: %u\n"
475 "RXed failed authentication response: %u\n"
476 "Association requests without response: %u\n"
477 "Authentication requests without response: %u\n"
478 "TX probe response: %u\n"
479 "RX probe request: %u\n"
480 "TX probe request: %lu\n"
481 "RX authentication requests: %lu\n"
482 "RX association requests: %lu\n"
483 "Reassociations: %lu\n",
484 priv->ieee80211->ieee_stats.tx_ass,
485 priv->ieee80211->ieee_stats.tx_aut,
486 priv->ieee80211->ieee_stats.rx_ass_ok,
487 priv->ieee80211->ieee_stats.rx_ass_err,
488 priv->ieee80211->ieee_stats.rx_aut_ok,
489 priv->ieee80211->ieee_stats.rx_aut_err,
490 priv->ieee80211->ieee_stats.ass_noresp,
491 priv->ieee80211->ieee_stats.aut_noresp,
492 priv->ieee80211->ieee_stats.tx_probe,
493 priv->ieee80211->ieee_stats.rx_probe,
494 priv->ieee80211->ieee_stats.tx_probe_rq,
495 priv->ieee80211->ieee_stats.rx_auth_rq,
496 priv->ieee80211->ieee_stats.rx_assoc_rq,
497 priv->ieee80211->ieee_stats.reassoc);
499 *eof = 1;
500 return len;
502 #endif
503 #if 0
504 static int proc_get_stats_ap(char *page, char **start,
505 off_t offset, int count,
506 int *eof, void *data)
508 struct net_device *dev = data;
509 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
510 struct mac_htable_t *list;
511 int i;
512 int len = 0;
514 if(priv->ieee80211->iw_mode != IW_MODE_MASTER){
515 len += snprintf(page + len, count - len,
516 "Card is not acting as AP...\n"
518 }else{
519 len += snprintf(page + len, count - len,
520 "List of associated STA:\n"
523 for(i=0;i<MAC_HTABLE_ENTRY;i++)
524 for (list = priv->ieee80211->assoc_htable[i]; list!=NULL; list = list->next){
525 len += snprintf(page + len, count - len,
526 MACSTR"\n",MAC2STR(list->adr));
530 *eof = 1;
531 return len;
533 #endif
535 static int proc_get_stats_tx(char *page, char **start,
536 off_t offset, int count,
537 int *eof, void *data)
539 struct net_device *dev = data;
540 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
542 int len = 0;
543 unsigned long totalOK;
545 totalOK=priv->stats.txnpokint+priv->stats.txhpokint+priv->stats.txlpokint;
546 len += snprintf(page + len, count - len,
547 /* "TX normal priority ok int: %lu\n"
548 "TX normal priority error int: %lu\n"
549 "TX high priority ok int: %lu\n"
550 "TX high priority failed error int: %lu\n"
551 "TX low priority ok int: %lu\n"
552 "TX low priority failed error int: %lu\n"
553 "TX bytes: %lu\n"
554 "TX packets: %lu\n"
555 "TX queue resume: %lu\n"
556 "TX queue stopped?: %d\n"
557 "TX fifo overflow: %lu\n"
558 //"SW TX stop: %lu\n"
559 //"SW TX wake: %lu\n"
560 "TX beacon: %lu\n"
561 "TX beacon aborted: %lu\n",
562 priv->stats.txnpokint,
563 priv->stats.txnperr,
564 priv->stats.txhpokint,
565 priv->stats.txhperr,
566 priv->stats.txlpokint,
567 priv->stats.txlperr,
568 priv->ieee80211->stats.tx_bytes,
569 priv->ieee80211->stats.tx_packets,
570 priv->stats.txresumed,
571 netif_queue_stopped(dev),
572 priv->stats.txoverflow,
573 //priv->ieee80211->ieee_stats.swtxstop,
574 //priv->ieee80211->ieee_stats.swtxawake,
575 priv->stats.txbeacon,
576 priv->stats.txbeaconerr */
577 "TX OK: %lu\n"
578 "TX Error: %lu\n"
579 "TX Retry: %lu\n"
580 "TX beacon OK: %lu\n"
581 "TX beacon error: %lu\n",
582 totalOK,
583 priv->stats.txnperr+priv->stats.txhperr+priv->stats.txlperr,
584 priv->stats.txretry,
585 priv->stats.txbeacon,
586 priv->stats.txbeaconerr
589 *eof = 1;
590 return len;
594 #if WIRELESS_EXT < 17
595 static struct iw_statistics *r8180_get_wireless_stats(struct net_device *dev)
597 struct r8180_priv *priv = ieee80211_priv(dev);
599 return &priv->wstats;
601 #endif
602 void rtl8180_proc_module_init(void)
604 DMESG("Initializing proc filesystem");
605 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
606 rtl8180_proc=create_proc_entry(RTL8180_MODULE_NAME, S_IFDIR, proc_net);
607 #else
608 rtl8180_proc=create_proc_entry(RTL8180_MODULE_NAME, S_IFDIR, init_net.proc_net);
609 #endif
613 void rtl8180_proc_module_remove(void)
615 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
616 remove_proc_entry(RTL8180_MODULE_NAME, proc_net);
617 #else
618 remove_proc_entry(RTL8180_MODULE_NAME, init_net.proc_net);
619 #endif
623 void rtl8180_proc_remove_one(struct net_device *dev)
625 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
626 if (priv->dir_dev) {
627 remove_proc_entry("stats-hw", priv->dir_dev);
628 remove_proc_entry("stats-tx", priv->dir_dev);
629 remove_proc_entry("stats-rx", priv->dir_dev);
630 // remove_proc_entry("stats-ieee", priv->dir_dev);
631 // remove_proc_entry("stats-ap", priv->dir_dev);
632 remove_proc_entry("registers", priv->dir_dev);
633 remove_proc_entry(dev->name, rtl8180_proc);
634 priv->dir_dev = NULL;
639 void rtl8180_proc_init_one(struct net_device *dev)
641 struct proc_dir_entry *e;
642 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
643 priv->dir_dev = create_proc_entry(dev->name,
644 S_IFDIR | S_IRUGO | S_IXUGO,
645 rtl8180_proc);
646 if (!priv->dir_dev) {
647 DMESGE("Unable to initialize /proc/net/rtl8180/%s\n",
648 dev->name);
649 return;
652 e = create_proc_read_entry("stats-hw", S_IFREG | S_IRUGO,
653 priv->dir_dev, proc_get_stats_hw, dev);
655 if (!e) {
656 DMESGE("Unable to initialize "
657 "/proc/net/rtl8180/%s/stats-hw\n",
658 dev->name);
661 e = create_proc_read_entry("stats-rx", S_IFREG | S_IRUGO,
662 priv->dir_dev, proc_get_stats_rx, dev);
664 if (!e) {
665 DMESGE("Unable to initialize "
666 "/proc/net/rtl8180/%s/stats-rx\n",
667 dev->name);
671 e = create_proc_read_entry("stats-tx", S_IFREG | S_IRUGO,
672 priv->dir_dev, proc_get_stats_tx, dev);
674 if (!e) {
675 DMESGE("Unable to initialize "
676 "/proc/net/rtl8180/%s/stats-tx\n",
677 dev->name);
679 #if 0
680 e = create_proc_read_entry("stats-ieee", S_IFREG | S_IRUGO,
681 priv->dir_dev, proc_get_stats_ieee, dev);
683 if (!e) {
684 DMESGE("Unable to initialize "
685 "/proc/net/rtl8180/%s/stats-ieee\n",
686 dev->name);
688 #endif
689 #if 0
690 e = create_proc_read_entry("stats-ap", S_IFREG | S_IRUGO,
691 priv->dir_dev, proc_get_stats_ap, dev);
693 if (!e) {
694 DMESGE("Unable to initialize "
695 "/proc/net/rtl8180/%s/stats-ap\n",
696 dev->name);
698 #endif
700 e = create_proc_read_entry("registers", S_IFREG | S_IRUGO,
701 priv->dir_dev, proc_get_registers, dev);
703 if (!e) {
704 DMESGE("Unable to initialize "
705 "/proc/net/rtl8180/%s/registers\n",
706 dev->name);
709 /****************************************************************************
710 -----------------------------MISC STUFF-------------------------
711 *****************************************************************************/
713 FIXME: check if we can use some standard already-existent
714 data type+functions in kernel
717 short buffer_add(struct buffer **buffer, u32 *buf, dma_addr_t dma,
718 struct buffer **bufferhead)
720 #ifdef DEBUG_RING
721 DMESG("adding buffer to TX/RX struct");
722 #endif
724 struct buffer *tmp;
726 if(! *buffer){
728 *buffer = kmalloc(sizeof(struct buffer),GFP_KERNEL);
730 if (*buffer == NULL) {
731 DMESGE("Failed to kmalloc head of TX/RX struct");
732 return -1;
734 (*buffer)->next=*buffer;
735 (*buffer)->buf=buf;
736 (*buffer)->dma=dma;
737 if(bufferhead !=NULL)
738 (*bufferhead) = (*buffer);
739 return 0;
741 tmp=*buffer;
743 while(tmp->next!=(*buffer)) tmp=tmp->next;
744 if ((tmp->next= kmalloc(sizeof(struct buffer),GFP_KERNEL)) == NULL){
745 DMESGE("Failed to kmalloc TX/RX struct");
746 return -1;
748 tmp->next->buf=buf;
749 tmp->next->dma=dma;
750 tmp->next->next=*buffer;
752 return 0;
756 void buffer_free(struct net_device *dev,struct buffer **buffer,int len,short
757 consistent)
760 struct buffer *tmp,*next;
761 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
762 struct pci_dev *pdev=priv->pdev;
763 //int i;
765 if(! *buffer) return;
767 /*for(tmp=*buffer; tmp->next != *buffer; tmp=tmp->next)
770 tmp=*buffer;
772 next=tmp->next;
773 if(consistent){
774 pci_free_consistent(pdev,len,
775 tmp->buf,tmp->dma);
776 }else{
777 pci_unmap_single(pdev, tmp->dma,
778 len,PCI_DMA_FROMDEVICE);
779 kfree(tmp->buf);
781 kfree(tmp);
782 tmp = next;
784 while(next != *buffer);
786 *buffer=NULL;
790 void print_buffer(u32 *buffer, int len)
792 int i;
793 u8 *buf =(u8*)buffer;
795 printk("ASCII BUFFER DUMP (len: %x):\n",len);
797 for(i=0;i<len;i++)
798 printk("%c",buf[i]);
800 printk("\nBINARY BUFFER DUMP (len: %x):\n",len);
802 for(i=0;i<len;i++)
803 printk("%02x",buf[i]);
805 printk("\n");
809 int get_curr_tx_free_desc(struct net_device *dev, int priority)
811 struct r8180_priv *priv = ieee80211_priv(dev);
812 u32* tail;
813 u32* head;
814 int ret;
816 switch (priority){
817 case MANAGE_PRIORITY:
818 head = priv->txmapringhead;
819 tail = priv->txmapringtail;
820 break;
821 case BK_PRIORITY:
822 head = priv->txbkpringhead;
823 tail = priv->txbkpringtail;
824 break;
825 case BE_PRIORITY:
826 head = priv->txbepringhead;
827 tail = priv->txbepringtail;
828 break;
829 case VI_PRIORITY:
830 head = priv->txvipringhead;
831 tail = priv->txvipringtail;
832 break;
833 case VO_PRIORITY:
834 head = priv->txvopringhead;
835 tail = priv->txvopringtail;
836 break;
837 case HI_PRIORITY:
838 head = priv->txhpringhead;
839 tail = priv->txhpringtail;
840 break;
841 default:
842 return -1;
845 //DMESG("%x %x", head, tail);
847 /* FIXME FIXME FIXME FIXME */
849 #if 0
850 if( head <= tail ) return priv->txringcount-1 - (tail - head)/8;
851 return (head - tail)/8/4;
852 #else
853 if( head <= tail )
854 ret = priv->txringcount - (tail - head)/8;
855 else
856 ret = (head - tail)/8;
858 if(ret > priv->txringcount ) DMESG("BUG");
859 return ret;
860 #endif
864 short check_nic_enought_desc(struct net_device *dev, int priority)
866 struct r8180_priv *priv = ieee80211_priv(dev);
867 struct ieee80211_device *ieee = netdev_priv(dev);
869 int requiredbyte, required;
870 requiredbyte = priv->ieee80211->fts + sizeof(struct ieee80211_header_data);
872 if(ieee->current_network.QoS_Enable) {
873 requiredbyte += 2;
876 required = requiredbyte / (priv->txbuffsize-4);
877 if (requiredbyte % priv->txbuffsize) required++;
878 /* for now we keep two free descriptor as a safety boundary
879 * between the tail and the head
882 return (required+2 < get_curr_tx_free_desc(dev,priority));
886 /* This function is only for debuging purpose */
887 void check_tx_ring(struct net_device *dev, int pri)
889 static int maxlog =3;
890 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
891 u32* tmp;
892 struct buffer *buf;
893 int i;
894 int nic;
895 u32* tail;
896 u32* head;
897 u32* begin;
898 u32 nicbegin;
899 struct buffer* buffer;
901 maxlog --;
902 if (maxlog <0 ) return;
904 switch(pri) {
905 case MANAGE_PRIORITY:
906 tail = priv->txmapringtail;
907 begin = priv->txmapring;
908 head = priv->txmapringhead;
909 nic = read_nic_dword(dev,TX_MANAGEPRIORITY_RING_ADDR);
910 buffer = priv->txmapbufs;
911 nicbegin = priv->txmapringdma;
912 break;
915 case BK_PRIORITY:
916 tail = priv->txbkpringtail;
917 begin = priv->txbkpring;
918 head = priv->txbkpringhead;
919 nic = read_nic_dword(dev,TX_BKPRIORITY_RING_ADDR);
920 buffer = priv->txbkpbufs;
921 nicbegin = priv->txbkpringdma;
922 break;
924 case BE_PRIORITY:
925 tail = priv->txbepringtail;
926 begin = priv->txbepring;
927 head = priv->txbepringhead;
928 nic = read_nic_dword(dev,TX_BEPRIORITY_RING_ADDR);
929 buffer = priv->txbepbufs;
930 nicbegin = priv->txbepringdma;
931 break;
933 case VI_PRIORITY:
934 tail = priv->txvipringtail;
935 begin = priv->txvipring;
936 head = priv->txvipringhead;
937 nic = read_nic_dword(dev,TX_VIPRIORITY_RING_ADDR);
938 buffer = priv->txvipbufs;
939 nicbegin = priv->txvipringdma;
940 break;
943 case VO_PRIORITY:
944 tail = priv->txvopringtail;
945 begin = priv->txvopring;
946 head = priv->txvopringhead;
947 nic = read_nic_dword(dev,TX_VOPRIORITY_RING_ADDR);
948 buffer = priv->txvopbufs;
949 nicbegin = priv->txvopringdma;
950 break;
952 case HI_PRIORITY:
953 tail = priv->txhpringtail;
954 begin = priv->txhpring;
955 head = priv->txhpringhead;
956 nic = read_nic_dword(dev,TX_HIGHPRIORITY_RING_ADDR);
957 buffer = priv->txhpbufs;
958 nicbegin = priv->txhpringdma;
959 break;
961 default:
962 return ;
963 break;
966 if(!priv->txvopbufs)
967 DMESGE ("NIC TX ack, but TX queue corrupted!");
968 else{
970 for(i=0,buf=buffer, tmp=begin;
971 tmp<begin+(priv->txringcount)*8;
972 tmp+=8,buf=buf->next,i++)
974 DMESG("BUF%d %s %x %s. Next : %x",i,
975 *tmp & (1<<31) ? "filled" : "empty",
976 *(buf->buf),
977 *tmp & (1<<15)? "ok": "err", *(tmp+4));
980 DMESG("nic at %d",
981 (nic-nicbegin) / 8 /4);
982 DMESG("tail at %d", ((int)tail - (int)begin) /8 /4);
983 DMESG("head at %d", ((int)head - (int)begin) /8 /4);
984 DMESG("check free desc returns %d", check_nic_enought_desc(dev,pri));
985 DMESG("free desc is %d\n", get_curr_tx_free_desc(dev,pri));
986 //rtl8180_reset(dev);
987 return;
992 /* this function is only for debugging purpose */
993 void check_rxbuf(struct net_device *dev)
995 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
996 u32* tmp;
997 struct buffer *buf;
998 u8 rx_desc_size;
1000 #ifdef CONFIG_RTL8185B
1001 rx_desc_size = 8;
1002 #else
1003 rx_desc_size = 4;
1004 #endif
1006 if(!priv->rxbuffer)
1007 DMESGE ("NIC RX ack, but RX queue corrupted!");
1009 else{
1011 for(buf=priv->rxbuffer, tmp=priv->rxring;
1012 tmp < priv->rxring+(priv->rxringcount)*rx_desc_size;
1013 tmp+=rx_desc_size, buf=buf->next)
1015 DMESG("BUF %s %x",
1016 *tmp & (1<<31) ? "empty" : "filled",
1017 *(buf->buf));
1020 return;
1024 void dump_eprom(struct net_device *dev)
1026 int i;
1027 for(i=0; i<63; i++)
1028 DMESG("EEPROM addr %x : %x", i, eprom_read(dev,i));
1032 void rtl8180_dump_reg(struct net_device *dev)
1034 int i;
1035 int n;
1036 int max=0xff;
1038 DMESG("Dumping NIC register map");
1040 for(n=0;n<=max;)
1042 printk( "\nD: %2x> ", n);
1043 for(i=0;i<16 && n<=max;i++,n++)
1044 printk("%2x ",read_nic_byte(dev,n));
1046 printk("\n");
1050 void fix_tx_fifo(struct net_device *dev)
1052 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1053 u32 *tmp;
1054 int i;
1055 #ifdef DEBUG_TX_ALLOC
1056 DMESG("FIXING TX FIFOs");
1057 #endif
1058 for (tmp=priv->txmapring, i=0;
1059 i < priv->txringcount;
1060 tmp+=8, i++){
1061 *tmp = *tmp &~ (1<<31);
1064 for (tmp=priv->txbkpring, i=0;
1065 i < priv->txringcount;
1066 tmp+=8, i++) {
1067 *tmp = *tmp &~ (1<<31);
1070 for (tmp=priv->txbepring, i=0;
1071 i < priv->txringcount;
1072 tmp+=8, i++){
1073 *tmp = *tmp &~ (1<<31);
1075 for (tmp=priv->txvipring, i=0;
1076 i < priv->txringcount;
1077 tmp+=8, i++) {
1078 *tmp = *tmp &~ (1<<31);
1081 for (tmp=priv->txvopring, i=0;
1082 i < priv->txringcount;
1083 tmp+=8, i++){
1084 *tmp = *tmp &~ (1<<31);
1087 for (tmp=priv->txhpring, i=0;
1088 i < priv->txringcount;
1089 tmp+=8,i++){
1090 *tmp = *tmp &~ (1<<31);
1093 for (tmp=priv->txbeaconring, i=0;
1094 i < priv->txbeaconcount;
1095 tmp+=8, i++){
1096 *tmp = *tmp &~ (1<<31);
1098 #ifdef DEBUG_TX_ALLOC
1099 DMESG("TX FIFOs FIXED");
1100 #endif
1101 priv->txmapringtail = priv->txmapring;
1102 priv->txmapringhead = priv->txmapring;
1103 priv->txmapbufstail = priv->txmapbufs;
1105 priv->txbkpringtail = priv->txbkpring;
1106 priv->txbkpringhead = priv->txbkpring;
1107 priv->txbkpbufstail = priv->txbkpbufs;
1109 priv->txbepringtail = priv->txbepring;
1110 priv->txbepringhead = priv->txbepring;
1111 priv->txbepbufstail = priv->txbepbufs;
1113 priv->txvipringtail = priv->txvipring;
1114 priv->txvipringhead = priv->txvipring;
1115 priv->txvipbufstail = priv->txvipbufs;
1117 priv->txvopringtail = priv->txvopring;
1118 priv->txvopringhead = priv->txvopring;
1119 priv->txvopbufstail = priv->txvopbufs;
1121 priv->txhpringtail = priv->txhpring;
1122 priv->txhpringhead = priv->txhpring;
1123 priv->txhpbufstail = priv->txhpbufs;
1125 priv->txbeaconringtail = priv->txbeaconring;
1126 priv->txbeaconbufstail = priv->txbeaconbufs;
1127 set_nic_txring(dev);
1129 ieee80211_reset_queue(priv->ieee80211);
1130 priv->ack_tx_to_ieee = 0;
1134 void fix_rx_fifo(struct net_device *dev)
1136 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1137 u32 *tmp;
1138 struct buffer *rxbuf;
1139 u8 rx_desc_size;
1141 #ifdef CONFIG_RTL8185B
1142 rx_desc_size = 8; // 4*8 = 32 bytes
1143 #else
1144 rx_desc_size = 4;
1145 #endif
1147 #ifdef DEBUG_RXALLOC
1148 DMESG("FIXING RX FIFO");
1149 check_rxbuf(dev);
1150 #endif
1152 for (tmp=priv->rxring, rxbuf=priv->rxbufferhead;
1153 (tmp < (priv->rxring)+(priv->rxringcount)*rx_desc_size);
1154 tmp+=rx_desc_size,rxbuf=rxbuf->next){
1155 *(tmp+2) = rxbuf->dma;
1156 *tmp=*tmp &~ 0xfff;
1157 *tmp=*tmp | priv->rxbuffersize;
1158 *tmp |= (1<<31);
1161 #ifdef DEBUG_RXALLOC
1162 DMESG("RX FIFO FIXED");
1163 check_rxbuf(dev);
1164 #endif
1166 priv->rxringtail=priv->rxring;
1167 priv->rxbuffer=priv->rxbufferhead;
1168 priv->rx_skb_complete=1;
1169 set_nic_rxring(dev);
1173 /****************************************************************************
1174 ------------------------------HW STUFF---------------------------
1175 *****************************************************************************/
1177 unsigned char QUALITY_MAP[] = {
1178 0x64, 0x64, 0x64, 0x63, 0x63, 0x62, 0x62, 0x61,
1179 0x61, 0x60, 0x60, 0x5f, 0x5f, 0x5e, 0x5d, 0x5c,
1180 0x5b, 0x5a, 0x59, 0x57, 0x56, 0x54, 0x52, 0x4f,
1181 0x4c, 0x49, 0x45, 0x41, 0x3c, 0x37, 0x31, 0x29,
1182 0x24, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
1183 0x22, 0x22, 0x21, 0x21, 0x21, 0x21, 0x21, 0x20,
1184 0x20, 0x20, 0x20, 0x1f, 0x1f, 0x1e, 0x1e, 0x1e,
1185 0x1d, 0x1d, 0x1c, 0x1c, 0x1b, 0x1a, 0x19, 0x19,
1186 0x18, 0x17, 0x16, 0x15, 0x14, 0x12, 0x11, 0x0f,
1187 0x0e, 0x0c, 0x0a, 0x08, 0x06, 0x04, 0x01, 0x00
1190 unsigned char STRENGTH_MAP[] = {
1191 0x64, 0x64, 0x63, 0x62, 0x61, 0x60, 0x5f, 0x5e,
1192 0x5d, 0x5c, 0x5b, 0x5a, 0x57, 0x54, 0x52, 0x50,
1193 0x4e, 0x4c, 0x4a, 0x48, 0x46, 0x44, 0x41, 0x3f,
1194 0x3c, 0x3a, 0x37, 0x36, 0x36, 0x1c, 0x1c, 0x1b,
1195 0x1b, 0x1a, 0x1a, 0x19, 0x19, 0x18, 0x18, 0x17,
1196 0x17, 0x16, 0x16, 0x15, 0x15, 0x14, 0x14, 0x13,
1197 0x13, 0x12, 0x12, 0x11, 0x11, 0x10, 0x10, 0x0f,
1198 0x0f, 0x0e, 0x0e, 0x0d, 0x0d, 0x0c, 0x0c, 0x0b,
1199 0x0b, 0x0a, 0x0a, 0x09, 0x09, 0x08, 0x08, 0x07,
1200 0x07, 0x06, 0x06, 0x05, 0x04, 0x03, 0x02, 0x00
1203 void rtl8180_RSSI_calc(struct net_device *dev, u8 *rssi, u8 *qual){
1204 //void Mlme_UpdateRssiSQ(struct net_device *dev, u8 *rssi, u8 *qual){
1205 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1206 u32 temp;
1207 u32 temp2;
1208 u32 temp3;
1209 u32 lsb;
1210 u32 q;
1211 u32 orig_qual;
1212 u8 _rssi;
1214 q = *qual;
1215 orig_qual = *qual;
1216 _rssi = 0; // avoid gcc complains..
1218 if (q <= 0x4e) {
1219 temp = QUALITY_MAP[q];
1220 } else {
1221 if( q & 0x80 ) {
1222 temp = 0x32;
1223 } else {
1224 temp = 1;
1228 *qual = temp;
1229 temp2 = *rssi;
1231 switch(priv->rf_chip){
1232 case RFCHIPID_RFMD:
1233 lsb = temp2 & 1;
1234 temp2 &= 0x7e;
1235 if ( !lsb || !(temp2 <= 0x3c) ) {
1236 temp2 = 0x64;
1237 } else {
1238 temp2 = 100 * temp2 / 0x3c;
1240 *rssi = temp2 & 0xff;
1241 _rssi = temp2 & 0xff;
1242 break;
1243 case RFCHIPID_INTERSIL:
1244 lsb = temp2;
1245 temp2 &= 0xfffffffe;
1246 temp2 *= 251;
1247 temp3 = temp2;
1248 temp2 <<= 6;
1249 temp3 += temp2;
1250 temp3 <<= 1;
1251 temp2 = 0x4950df;
1252 temp2 -= temp3;
1253 lsb &= 1;
1254 if ( temp2 <= 0x3e0000 ) {
1255 if ( temp2 < 0xffef0000 )
1256 temp2 = 0xffef0000;
1257 } else {
1258 temp2 = 0x3e0000;
1260 if ( !lsb ) {
1261 temp2 -= 0xf0000;
1262 } else {
1263 temp2 += 0xf0000;
1266 temp3 = 0x4d0000;
1267 temp3 -= temp2;
1268 temp3 *= 100;
1269 temp3 = temp3 / 0x6d;
1270 temp3 >>= 0x10;
1271 _rssi = temp3 & 0xff;
1272 *rssi = temp3 & 0xff;
1273 break;
1274 case RFCHIPID_GCT:
1275 lsb = temp2 & 1;
1276 temp2 &= 0x7e;
1277 if ( ! lsb || !(temp2 <= 0x3c) ){
1278 temp2 = 0x64;
1279 } else {
1280 temp2 = (100 * temp2) / 0x3c;
1282 *rssi = temp2 & 0xff;
1283 _rssi = temp2 & 0xff;
1284 break;
1285 case RFCHIPID_PHILIPS:
1286 if( orig_qual <= 0x4e ){
1287 _rssi = STRENGTH_MAP[orig_qual];
1288 *rssi = _rssi;
1289 } else {
1290 orig_qual -= 0x80;
1291 if ( !orig_qual ){
1292 _rssi = 1;
1293 *rssi = 1;
1294 } else {
1295 _rssi = 0x32;
1296 *rssi = 0x32;
1299 break;
1301 /* case 4 */
1302 case RFCHIPID_MAXIM:
1303 lsb = temp2 & 1;
1304 temp2 &= 0x7e;
1305 temp2 >>= 1;
1306 temp2 += 0x42;
1307 if( lsb != 0 ){
1308 temp2 += 0xa;
1310 *rssi = temp2 & 0xff;
1311 _rssi = temp2 & 0xff;
1312 break;
1315 if ( _rssi < 0x64 ){
1316 if ( _rssi == 0 ) {
1317 *rssi = 1;
1319 } else {
1320 *rssi = 0x64;
1323 return;
1327 void rtl8180_irq_enable(struct net_device *dev)
1329 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1330 priv->irq_enabled = 1;
1332 write_nic_word(dev,INTA_MASK,INTA_RXOK | INTA_RXDESCERR | INTA_RXOVERFLOW |\
1333 INTA_TXOVERFLOW | INTA_HIPRIORITYDESCERR | INTA_HIPRIORITYDESCOK |\
1334 INTA_NORMPRIORITYDESCERR | INTA_NORMPRIORITYDESCOK |\
1335 INTA_LOWPRIORITYDESCERR | INTA_LOWPRIORITYDESCOK | INTA_TIMEOUT);
1337 write_nic_word(dev,INTA_MASK, priv->irq_mask);
1341 void rtl8180_irq_disable(struct net_device *dev)
1343 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1345 #ifdef CONFIG_RTL8185B
1346 write_nic_dword(dev,IMR,0);
1347 #else
1348 write_nic_word(dev,INTA_MASK,0);
1349 #endif
1350 force_pci_posting(dev);
1351 priv->irq_enabled = 0;
1355 void rtl8180_set_mode(struct net_device *dev,int mode)
1357 u8 ecmd;
1358 ecmd=read_nic_byte(dev, EPROM_CMD);
1359 ecmd=ecmd &~ EPROM_CMD_OPERATING_MODE_MASK;
1360 ecmd=ecmd | (mode<<EPROM_CMD_OPERATING_MODE_SHIFT);
1361 ecmd=ecmd &~ (1<<EPROM_CS_SHIFT);
1362 ecmd=ecmd &~ (1<<EPROM_CK_SHIFT);
1363 write_nic_byte(dev, EPROM_CMD, ecmd);
1366 void rtl8180_adapter_start(struct net_device *dev);
1367 void rtl8180_beacon_tx_enable(struct net_device *dev);
1369 void rtl8180_update_msr(struct net_device *dev)
1371 struct r8180_priv *priv = ieee80211_priv(dev);
1372 u8 msr;
1373 u32 rxconf;
1375 msr = read_nic_byte(dev, MSR);
1376 msr &= ~ MSR_LINK_MASK;
1378 rxconf=read_nic_dword(dev,RX_CONF);
1380 if(priv->ieee80211->state == IEEE80211_LINKED)
1382 if(priv->ieee80211->iw_mode == IW_MODE_ADHOC)
1383 msr |= (MSR_LINK_ADHOC<<MSR_LINK_SHIFT);
1384 else if (priv->ieee80211->iw_mode == IW_MODE_MASTER)
1385 msr |= (MSR_LINK_MASTER<<MSR_LINK_SHIFT);
1386 else if (priv->ieee80211->iw_mode == IW_MODE_INFRA)
1387 msr |= (MSR_LINK_MANAGED<<MSR_LINK_SHIFT);
1388 else
1389 msr |= (MSR_LINK_NONE<<MSR_LINK_SHIFT);
1390 rxconf |= (1<<RX_CHECK_BSSID_SHIFT);
1392 }else {
1393 msr |= (MSR_LINK_NONE<<MSR_LINK_SHIFT);
1394 rxconf &= ~(1<<RX_CHECK_BSSID_SHIFT);
1397 write_nic_byte(dev, MSR, msr);
1398 write_nic_dword(dev, RX_CONF, rxconf);
1404 void rtl8180_set_chan(struct net_device *dev,short ch)
1406 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1408 if((ch > 14) || (ch < 1))
1410 printk("In %s: Invalid chnanel %d\n", __func__, ch);
1411 return;
1414 priv->chan=ch;
1415 //printk("in %s:channel is %d\n",__func__,ch);
1416 priv->rf_set_chan(dev,priv->chan);
1421 void rtl8180_rx_enable(struct net_device *dev)
1423 u8 cmd;
1424 u32 rxconf;
1425 /* for now we accept data, management & ctl frame*/
1426 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1428 rxconf=read_nic_dword(dev,RX_CONF);
1429 rxconf = rxconf &~ MAC_FILTER_MASK;
1430 rxconf = rxconf | (1<<ACCEPT_MNG_FRAME_SHIFT);
1431 rxconf = rxconf | (1<<ACCEPT_DATA_FRAME_SHIFT);
1432 rxconf = rxconf | (1<<ACCEPT_BCAST_FRAME_SHIFT);
1433 rxconf = rxconf | (1<<ACCEPT_MCAST_FRAME_SHIFT);
1434 // rxconf = rxconf | (1<<ACCEPT_CRCERR_FRAME_SHIFT);
1435 if (dev->flags & IFF_PROMISC) DMESG ("NIC in promisc mode");
1437 if(priv->ieee80211->iw_mode == IW_MODE_MONITOR || \
1438 dev->flags & IFF_PROMISC){
1439 rxconf = rxconf | (1<<ACCEPT_ALLMAC_FRAME_SHIFT);
1440 }else{
1441 rxconf = rxconf | (1<<ACCEPT_NICMAC_FRAME_SHIFT);
1442 if(priv->card_8185 == 0)
1443 rxconf = rxconf | (1<<RX_CHECK_BSSID_SHIFT);
1446 /*if(priv->ieee80211->iw_mode == IW_MODE_MASTER){
1447 rxconf = rxconf | (1<<ACCEPT_ALLMAC_FRAME_SHIFT);
1448 rxconf = rxconf | (1<<RX_CHECK_BSSID_SHIFT);
1451 if(priv->ieee80211->iw_mode == IW_MODE_MONITOR){
1452 rxconf = rxconf | (1<<ACCEPT_CTL_FRAME_SHIFT);
1453 rxconf = rxconf | (1<<ACCEPT_ICVERR_FRAME_SHIFT);
1454 rxconf = rxconf | (1<<ACCEPT_PWR_FRAME_SHIFT);
1457 if( priv->crcmon == 1 && priv->ieee80211->iw_mode == IW_MODE_MONITOR)
1458 rxconf = rxconf | (1<<ACCEPT_CRCERR_FRAME_SHIFT);
1460 //if(!priv->card_8185){
1461 rxconf = rxconf &~ RX_FIFO_THRESHOLD_MASK;
1462 rxconf = rxconf | (RX_FIFO_THRESHOLD_NONE<<RX_FIFO_THRESHOLD_SHIFT);
1465 rxconf = rxconf | (1<<RX_AUTORESETPHY_SHIFT);
1466 rxconf = rxconf &~ MAX_RX_DMA_MASK;
1467 rxconf = rxconf | (MAX_RX_DMA_2048<<MAX_RX_DMA_SHIFT);
1469 //if(!priv->card_8185)
1470 rxconf = rxconf | RCR_ONLYERLPKT;
1472 rxconf = rxconf &~ RCR_CS_MASK;
1473 if(!priv->card_8185)
1474 rxconf |= (priv->rcr_csense<<RCR_CS_SHIFT);
1475 // rxconf &=~ 0xfff00000;
1476 // rxconf |= 0x90100000;//9014f76f;
1477 write_nic_dword(dev, RX_CONF, rxconf);
1479 fix_rx_fifo(dev);
1481 #ifdef DEBUG_RX
1482 DMESG("rxconf: %x %x",rxconf ,read_nic_dword(dev,RX_CONF));
1483 #endif
1484 cmd=read_nic_byte(dev,CMD);
1485 write_nic_byte(dev,CMD,cmd | (1<<CMD_RX_ENABLE_SHIFT));
1487 /* In rtl8139 driver seems that DMA threshold has to be written
1488 * after enabling RX, so we rewrite RX_CONFIG register
1490 //mdelay(100);
1491 // write_nic_dword(dev, RX_CONF, rxconf);
1496 void set_nic_txring(struct net_device *dev)
1498 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1499 // DMESG("ring %x %x", priv->txlpringdma,read_nic_dword(dev,TLPDA));
1501 write_nic_dword(dev, TX_MANAGEPRIORITY_RING_ADDR, priv->txmapringdma);
1502 // DMESG("ring %x %x", priv->txlpringdma,read_nic_dword(dev,TLPDA));
1503 write_nic_dword(dev, TX_BKPRIORITY_RING_ADDR, priv->txbkpringdma);
1504 // DMESG("ring %x %x", priv->txlpringdma,read_nic_dword(dev,TLPDA));
1505 write_nic_dword(dev, TX_BEPRIORITY_RING_ADDR, priv->txbepringdma);
1506 // DMESG("ring %x %x", priv->txlpringdma,read_nic_dword(dev,TLPDA));
1507 write_nic_dword(dev, TX_VIPRIORITY_RING_ADDR, priv->txvipringdma);
1508 // DMESG("ring %x %x", priv->txlpringdma,read_nic_dword(dev,TLPDA));
1509 write_nic_dword(dev, TX_VOPRIORITY_RING_ADDR, priv->txvopringdma);
1510 // DMESG("ring %x %x", priv->txlpringdma,read_nic_dword(dev,TLPDA));
1511 write_nic_dword(dev, TX_HIGHPRIORITY_RING_ADDR, priv->txhpringdma);
1512 // DMESG("ring %x %x", priv->txlpringdma,read_nic_dword(dev,TLPDA));
1514 write_nic_dword(dev, TX_BEACON_RING_ADDR, priv->txbeaconringdma);
1518 void rtl8180_conttx_enable(struct net_device *dev)
1520 u32 txconf;
1521 txconf = read_nic_dword(dev,TX_CONF);
1522 txconf = txconf &~ TX_LOOPBACK_MASK;
1523 txconf = txconf | (TX_LOOPBACK_CONTINUE <<TX_LOOPBACK_SHIFT);
1524 write_nic_dword(dev,TX_CONF,txconf);
1528 void rtl8180_conttx_disable(struct net_device *dev)
1530 u32 txconf;
1531 txconf = read_nic_dword(dev,TX_CONF);
1532 txconf = txconf &~ TX_LOOPBACK_MASK;
1533 txconf = txconf | (TX_LOOPBACK_NONE <<TX_LOOPBACK_SHIFT);
1534 write_nic_dword(dev,TX_CONF,txconf);
1538 void rtl8180_tx_enable(struct net_device *dev)
1540 u8 cmd;
1541 u8 tx_agc_ctl;
1542 u8 byte;
1543 u32 txconf;
1544 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1545 txconf= read_nic_dword(dev,TX_CONF);
1548 if(priv->card_8185){
1551 byte = read_nic_byte(dev,CW_CONF);
1552 byte &= ~(1<<CW_CONF_PERPACKET_CW_SHIFT);
1553 byte &= ~(1<<CW_CONF_PERPACKET_RETRY_SHIFT);
1554 write_nic_byte(dev, CW_CONF, byte);
1556 tx_agc_ctl = read_nic_byte(dev, TX_AGC_CTL);
1557 tx_agc_ctl &= ~(1<<TX_AGC_CTL_PERPACKET_GAIN_SHIFT);
1558 tx_agc_ctl &= ~(1<<TX_AGC_CTL_PERPACKET_ANTSEL_SHIFT);
1559 tx_agc_ctl |=(1<<TX_AGC_CTL_FEEDBACK_ANT);
1560 write_nic_byte(dev, TX_AGC_CTL, tx_agc_ctl);
1562 write_nic_word(dev, 0x5e, 0x01);
1563 force_pci_posting(dev);
1564 mdelay(1);
1565 write_nic_word(dev, 0xfe, 0x10);
1566 force_pci_posting(dev);
1567 mdelay(1);
1568 write_nic_word(dev, 0x5e, 0x00);
1569 force_pci_posting(dev);
1570 mdelay(1);
1572 write_nic_byte(dev, 0xec, 0x3f); /* Disable early TX */
1575 if(priv->card_8185){
1577 txconf = txconf &~ (1<<TCR_PROBE_NOTIMESTAMP_SHIFT);
1579 }else{
1581 if(hwseqnum)
1582 txconf= txconf &~ (1<<TX_CONF_HEADER_AUTOICREMENT_SHIFT);
1583 else
1584 txconf= txconf | (1<<TX_CONF_HEADER_AUTOICREMENT_SHIFT);
1587 txconf = txconf &~ TX_LOOPBACK_MASK;
1588 txconf = txconf | (TX_LOOPBACK_NONE <<TX_LOOPBACK_SHIFT);
1589 txconf = txconf &~ TCR_DPRETRY_MASK;
1590 txconf = txconf &~ TCR_RTSRETRY_MASK;
1591 txconf = txconf | (priv->retry_data<<TX_DPRETRY_SHIFT);
1592 txconf = txconf | (priv->retry_rts<<TX_RTSRETRY_SHIFT);
1593 txconf = txconf &~ (1<<TX_NOCRC_SHIFT);
1595 if(priv->card_8185){
1596 if(priv->hw_plcp_len)
1597 txconf = txconf &~ TCR_PLCP_LEN;
1598 else
1599 txconf = txconf | TCR_PLCP_LEN;
1600 }else{
1601 txconf = txconf &~ TCR_SAT;
1603 txconf = txconf &~ TCR_MXDMA_MASK;
1604 txconf = txconf | (TCR_MXDMA_2048<<TCR_MXDMA_SHIFT);
1605 txconf = txconf | TCR_CWMIN;
1606 txconf = txconf | TCR_DISCW;
1608 // if(priv->ieee80211->hw_wep)
1609 // txconf=txconf &~ (1<<TX_NOICV_SHIFT);
1610 // else
1611 txconf=txconf | (1<<TX_NOICV_SHIFT);
1613 write_nic_dword(dev,TX_CONF,txconf);
1616 fix_tx_fifo(dev);
1618 #ifdef DEBUG_TX
1619 DMESG("txconf: %x %x",txconf,read_nic_dword(dev,TX_CONF));
1620 #endif
1622 cmd=read_nic_byte(dev,CMD);
1623 write_nic_byte(dev,CMD,cmd | (1<<CMD_TX_ENABLE_SHIFT));
1625 // mdelay(100);
1626 write_nic_dword(dev,TX_CONF,txconf);
1627 // #endif
1629 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
1630 write_nic_byte(dev, TX_DMA_POLLING, priv->dma_poll_mask);
1631 rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
1636 void rtl8180_beacon_tx_enable(struct net_device *dev)
1638 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1640 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
1641 #ifdef CONFIG_RTL8185B
1642 priv->dma_poll_stop_mask &= ~(TPPOLLSTOP_BQ);
1643 write_nic_byte(dev,TPPollStop, priv->dma_poll_mask);
1644 #else
1645 priv->dma_poll_mask &=~(1<<TX_DMA_STOP_BEACON_SHIFT);
1646 write_nic_byte(dev,TX_DMA_POLLING,priv->dma_poll_mask);
1647 #endif
1648 rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
1652 void rtl8180_beacon_tx_disable(struct net_device *dev)
1654 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1656 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
1657 #ifdef CONFIG_RTL8185B
1658 priv->dma_poll_stop_mask |= TPPOLLSTOP_BQ;
1659 write_nic_byte(dev,TPPollStop, priv->dma_poll_stop_mask);
1660 #else
1661 priv->dma_poll_mask |= (1<<TX_DMA_STOP_BEACON_SHIFT);
1662 write_nic_byte(dev,TX_DMA_POLLING,priv->dma_poll_mask);
1663 #endif
1664 rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
1669 void rtl8180_rtx_disable(struct net_device *dev)
1671 u8 cmd;
1672 struct r8180_priv *priv = ieee80211_priv(dev);
1674 cmd=read_nic_byte(dev,CMD);
1675 write_nic_byte(dev, CMD, cmd &~ \
1676 ((1<<CMD_RX_ENABLE_SHIFT)|(1<<CMD_TX_ENABLE_SHIFT)));
1677 force_pci_posting(dev);
1678 mdelay(10);
1679 /*while (read_nic_byte(dev,CMD) & (1<<CMD_RX_ENABLE_SHIFT))
1680 udelay(10);
1683 if(!priv->rx_skb_complete)
1684 dev_kfree_skb_any(priv->rx_skb);
1687 #if 0
1688 int alloc_tx_beacon_desc_ring(struct net_device *dev, int count)
1690 int i;
1691 u32 *tmp;
1692 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1694 priv->txbeaconring = (u32*)pci_alloc_consistent(priv->pdev,
1695 sizeof(u32)*8*count,
1696 &priv->txbeaconringdma);
1697 if (!priv->txbeaconring) return -1;
1698 for (tmp=priv->txbeaconring,i=0;i<count;i++){
1699 *tmp = *tmp &~ (1<<31); // descriptor empty, owned by the drv
1701 *(tmp+2) = (u32)dma_tmp;
1702 *(tmp+3) = bufsize;
1704 if(i+1<count)
1705 *(tmp+4) = (u32)priv->txbeaconringdma+((i+1)*8*4);
1706 else
1707 *(tmp+4) = (u32)priv->txbeaconringdma;
1709 tmp=tmp+8;
1711 return 0;
1713 #endif
1715 short alloc_tx_desc_ring(struct net_device *dev, int bufsize, int count,
1716 int addr)
1718 int i;
1719 u32 *desc;
1720 u32 *tmp;
1721 dma_addr_t dma_desc, dma_tmp;
1722 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1723 struct pci_dev *pdev = priv->pdev;
1724 void *buf;
1726 if((bufsize & 0xfff) != bufsize) {
1727 DMESGE ("TX buffer allocation too large");
1728 return 0;
1730 desc = (u32*)pci_alloc_consistent(pdev,
1731 sizeof(u32)*8*count+256, &dma_desc);
1732 if(desc==NULL) return -1;
1733 if(dma_desc & 0xff){
1736 * descriptor's buffer must be 256 byte aligned
1737 * we shouldn't be here, since we set DMA mask !
1739 DMESGW("Fixing TX alignment");
1740 desc = (u32*)((u8*)desc + 256);
1741 #if (defined(CONFIG_HIGHMEM64G) || defined(CONFIG_64BIT_PHYS_ADDR))
1742 desc = (u32*)((u64)desc &~ 0xff);
1743 dma_desc = (dma_addr_t)((u8*)dma_desc + 256);
1744 dma_desc = (dma_addr_t)((u64)dma_desc &~ 0xff);
1745 #else
1746 desc = (u32*)((u32)desc &~ 0xff);
1747 dma_desc = (dma_addr_t)((u8*)dma_desc + 256);
1748 dma_desc = (dma_addr_t)((u32)dma_desc &~ 0xff);
1749 #endif
1751 tmp=desc;
1752 for (i=0;i<count;i++)
1754 buf = (void*)pci_alloc_consistent(pdev,bufsize,&dma_tmp);
1755 if (buf == NULL) return -ENOMEM;
1757 switch(addr) {
1758 #if 0
1759 case TX_NORMPRIORITY_RING_ADDR:
1760 if(-1 == buffer_add(&(priv->txnpbufs),buf,dma_tmp,NULL)){
1761 DMESGE("Unable to allocate mem for buffer NP");
1762 return -ENOMEM;
1764 break;
1766 case TX_LOWPRIORITY_RING_ADDR:
1767 if(-1 == buffer_add(&(priv->txlpbufs),buf,dma_tmp,NULL)){
1768 DMESGE("Unable to allocate mem for buffer LP");
1769 return -ENOMEM;
1771 break;
1773 case TX_HIGHPRIORITY_RING_ADDR:
1774 if(-1 == buffer_add(&(priv->txhpbufs),buf,dma_tmp,NULL)){
1775 DMESGE("Unable to allocate mem for buffer HP");
1776 return -ENOMEM;
1778 break;
1779 #else
1780 case TX_MANAGEPRIORITY_RING_ADDR:
1781 if(-1 == buffer_add(&(priv->txmapbufs),buf,dma_tmp,NULL)){
1782 DMESGE("Unable to allocate mem for buffer NP");
1783 return -ENOMEM;
1785 break;
1787 case TX_BKPRIORITY_RING_ADDR:
1788 if(-1 == buffer_add(&(priv->txbkpbufs),buf,dma_tmp,NULL)){
1789 DMESGE("Unable to allocate mem for buffer LP");
1790 return -ENOMEM;
1792 break;
1793 case TX_BEPRIORITY_RING_ADDR:
1794 if(-1 == buffer_add(&(priv->txbepbufs),buf,dma_tmp,NULL)){
1795 DMESGE("Unable to allocate mem for buffer NP");
1796 return -ENOMEM;
1798 break;
1800 case TX_VIPRIORITY_RING_ADDR:
1801 if(-1 == buffer_add(&(priv->txvipbufs),buf,dma_tmp,NULL)){
1802 DMESGE("Unable to allocate mem for buffer LP");
1803 return -ENOMEM;
1805 break;
1806 case TX_VOPRIORITY_RING_ADDR:
1807 if(-1 == buffer_add(&(priv->txvopbufs),buf,dma_tmp,NULL)){
1808 DMESGE("Unable to allocate mem for buffer NP");
1809 return -ENOMEM;
1811 break;
1812 #endif
1813 case TX_HIGHPRIORITY_RING_ADDR:
1814 if(-1 == buffer_add(&(priv->txhpbufs),buf,dma_tmp,NULL)){
1815 DMESGE("Unable to allocate mem for buffer HP");
1816 return -ENOMEM;
1818 break;
1819 case TX_BEACON_RING_ADDR:
1820 if(-1 == buffer_add(&(priv->txbeaconbufs),buf,dma_tmp,NULL)){
1821 DMESGE("Unable to allocate mem for buffer BP");
1822 return -ENOMEM;
1824 break;
1826 *tmp = *tmp &~ (1<<31); // descriptor empty, owned by the drv
1827 *(tmp+2) = (u32)dma_tmp;
1828 *(tmp+3) = bufsize;
1830 if(i+1<count)
1831 *(tmp+4) = (u32)dma_desc+((i+1)*8*4);
1832 else
1833 *(tmp+4) = (u32)dma_desc;
1835 tmp=tmp+8;
1838 switch(addr) {
1839 case TX_MANAGEPRIORITY_RING_ADDR:
1840 priv->txmapringdma=dma_desc;
1841 priv->txmapring=desc;
1842 break;
1844 case TX_BKPRIORITY_RING_ADDR:
1845 priv->txbkpringdma=dma_desc;
1846 priv->txbkpring=desc;
1847 break;
1849 case TX_BEPRIORITY_RING_ADDR:
1850 priv->txbepringdma=dma_desc;
1851 priv->txbepring=desc;
1852 break;
1854 case TX_VIPRIORITY_RING_ADDR:
1855 priv->txvipringdma=dma_desc;
1856 priv->txvipring=desc;
1857 break;
1859 case TX_VOPRIORITY_RING_ADDR:
1860 priv->txvopringdma=dma_desc;
1861 priv->txvopring=desc;
1862 break;
1864 case TX_HIGHPRIORITY_RING_ADDR:
1865 priv->txhpringdma=dma_desc;
1866 priv->txhpring=desc;
1867 break;
1869 case TX_BEACON_RING_ADDR:
1870 priv->txbeaconringdma=dma_desc;
1871 priv->txbeaconring=desc;
1872 break;
1876 #ifdef DEBUG_TX
1877 DMESG("Tx dma physical address: %x",dma_desc);
1878 #endif
1880 return 0;
1884 void free_tx_desc_rings(struct net_device *dev)
1887 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1888 struct pci_dev *pdev=priv->pdev;
1889 int count = priv->txringcount;
1891 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1892 priv->txmapring, priv->txmapringdma);
1893 buffer_free(dev,&(priv->txmapbufs),priv->txbuffsize,1);
1895 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1896 priv->txbkpring, priv->txbkpringdma);
1897 buffer_free(dev,&(priv->txbkpbufs),priv->txbuffsize,1);
1899 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1900 priv->txbepring, priv->txbepringdma);
1901 buffer_free(dev,&(priv->txbepbufs),priv->txbuffsize,1);
1903 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1904 priv->txvipring, priv->txvipringdma);
1905 buffer_free(dev,&(priv->txvipbufs),priv->txbuffsize,1);
1907 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1908 priv->txvopring, priv->txvopringdma);
1909 buffer_free(dev,&(priv->txvopbufs),priv->txbuffsize,1);
1911 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1912 priv->txhpring, priv->txhpringdma);
1913 buffer_free(dev,&(priv->txhpbufs),priv->txbuffsize,1);
1915 count = priv->txbeaconcount;
1916 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1917 priv->txbeaconring, priv->txbeaconringdma);
1918 buffer_free(dev,&(priv->txbeaconbufs),priv->txbuffsize,1);
1921 #if 0
1922 void free_beacon_desc_ring(struct net_device *dev,int count)
1925 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1926 struct pci_dev *pdev=priv->pdev;
1928 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1929 priv->txbeaconring, priv->txbeaconringdma);
1931 if (priv->beacon_buf)
1932 pci_free_consistent(priv->pdev,
1933 priv->master_beaconsize,priv->beacon_buf,priv->beacondmabuf);
1936 #endif
1937 void free_rx_desc_ring(struct net_device *dev)
1939 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1940 struct pci_dev *pdev = priv->pdev;
1942 int count = priv->rxringcount;
1944 #ifdef CONFIG_RTL8185B
1945 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1946 priv->rxring, priv->rxringdma);
1947 #else
1948 pci_free_consistent(pdev, sizeof(u32)*4*count+256,
1949 priv->rxring, priv->rxringdma);
1950 #endif
1952 buffer_free(dev,&(priv->rxbuffer),priv->rxbuffersize,0);
1956 short alloc_rx_desc_ring(struct net_device *dev, u16 bufsize, int count)
1958 int i;
1959 u32 *desc;
1960 u32 *tmp;
1961 dma_addr_t dma_desc,dma_tmp;
1962 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1963 struct pci_dev *pdev=priv->pdev;
1964 void *buf;
1965 u8 rx_desc_size;
1967 #ifdef CONFIG_RTL8185B
1968 rx_desc_size = 8; // 4*8 = 32 bytes
1969 #else
1970 rx_desc_size = 4;
1971 #endif
1973 if((bufsize & 0xfff) != bufsize){
1974 DMESGE ("RX buffer allocation too large");
1975 return -1;
1978 desc = (u32*)pci_alloc_consistent(pdev,sizeof(u32)*rx_desc_size*count+256,
1979 &dma_desc);
1981 if(dma_desc & 0xff){
1984 * descriptor's buffer must be 256 byte aligned
1985 * should never happen since we specify the DMA mask
1988 DMESGW("Fixing RX alignment");
1989 desc = (u32*)((u8*)desc + 256);
1990 #if (defined(CONFIG_HIGHMEM64G) || defined(CONFIG_64BIT_PHYS_ADDR))
1991 desc = (u32*)((u64)desc &~ 0xff);
1992 dma_desc = (dma_addr_t)((u8*)dma_desc + 256);
1993 dma_desc = (dma_addr_t)((u64)dma_desc &~ 0xff);
1994 #else
1995 desc = (u32*)((u32)desc &~ 0xff);
1996 dma_desc = (dma_addr_t)((u8*)dma_desc + 256);
1997 dma_desc = (dma_addr_t)((u32)dma_desc &~ 0xff);
1998 #endif
2001 priv->rxring=desc;
2002 priv->rxringdma=dma_desc;
2003 tmp=desc;
2005 for (i=0;i<count;i++){
2007 if ((buf= kmalloc(bufsize * sizeof(u8),GFP_ATOMIC)) == NULL){
2008 DMESGE("Failed to kmalloc RX buffer");
2009 return -1;
2012 dma_tmp = pci_map_single(pdev,buf,bufsize * sizeof(u8),
2013 PCI_DMA_FROMDEVICE);
2015 #ifdef DEBUG_ZERO_RX
2016 int j;
2017 for(j=0;j<bufsize;j++) ((u8*)buf)[i] = 0;
2018 #endif
2020 //buf = (void*)pci_alloc_consistent(pdev,bufsize,&dma_tmp);
2021 if(-1 == buffer_add(&(priv->rxbuffer), buf,dma_tmp,
2022 &(priv->rxbufferhead))){
2023 DMESGE("Unable to allocate mem RX buf");
2024 return -1;
2026 *tmp = 0; //zero pads the header of the descriptor
2027 *tmp = *tmp |( bufsize&0xfff);
2028 *(tmp+2) = (u32)dma_tmp;
2029 *tmp = *tmp |(1<<31); // descriptor void, owned by the NIC
2031 #ifdef DEBUG_RXALLOC
2032 DMESG("Alloc %x size buffer, DMA mem @ %x, virtual mem @ %x",
2033 (u32)(bufsize&0xfff), (u32)dma_tmp, (u32)buf);
2034 #endif
2036 tmp=tmp+rx_desc_size;
2039 *(tmp-rx_desc_size) = *(tmp-rx_desc_size) | (1<<30); // this is the last descriptor
2042 #ifdef DEBUG_RXALLOC
2043 DMESG("RX DMA physical address: %x",dma_desc);
2044 #endif
2046 return 0;
2050 void set_nic_rxring(struct net_device *dev)
2052 u8 pgreg;
2053 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
2055 //rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
2057 pgreg=read_nic_byte(dev, PGSELECT);
2058 write_nic_byte(dev, PGSELECT, pgreg &~ (1<<PGSELECT_PG_SHIFT));
2060 //rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
2062 write_nic_dword(dev, RXRING_ADDR,priv->rxringdma);
2066 void rtl8180_reset(struct net_device *dev)
2068 //u32 txconf = 0x80e00707; //FIXME: Make me understandable
2069 u8 cr;
2071 //write_nic_dword(dev,TX_CONF,txconf);
2073 rtl8180_irq_disable(dev);
2075 cr=read_nic_byte(dev,CMD);
2076 cr = cr & 2;
2077 cr = cr | (1<<CMD_RST_SHIFT);
2078 write_nic_byte(dev,CMD,cr);
2080 force_pci_posting(dev);
2082 mdelay(200);
2084 if(read_nic_byte(dev,CMD) & (1<<CMD_RST_SHIFT))
2085 DMESGW("Card reset timeout!");
2086 else
2087 DMESG("Card successfully reset");
2089 //#ifndef CONFIG_RTL8185B
2090 rtl8180_set_mode(dev,EPROM_CMD_LOAD);
2091 force_pci_posting(dev);
2092 mdelay(200);
2093 //#endif
2096 inline u16 ieeerate2rtlrate(int rate)
2098 switch(rate){
2099 case 10:
2100 return 0;
2101 case 20:
2102 return 1;
2103 case 55:
2104 return 2;
2105 case 110:
2106 return 3;
2107 case 60:
2108 return 4;
2109 case 90:
2110 return 5;
2111 case 120:
2112 return 6;
2113 case 180:
2114 return 7;
2115 case 240:
2116 return 8;
2117 case 360:
2118 return 9;
2119 case 480:
2120 return 10;
2121 case 540:
2122 return 11;
2123 default:
2124 return 3;
2129 static u16 rtl_rate[] = {10,20,55,110,60,90,120,180,240,360,480,540,720};
2130 inline u16 rtl8180_rate2rate(short rate)
2132 if (rate >12) return 10;
2133 return rtl_rate[rate];
2135 inline u8 rtl8180_IsWirelessBMode(u16 rate)
2137 if( ((rate <= 110) && (rate != 60) && (rate != 90)) || (rate == 220) )
2138 return 1;
2139 else return 0;
2141 u16 N_DBPSOfRate(u16 DataRate);
2142 u16 ComputeTxTime(
2143 u16 FrameLength,
2144 u16 DataRate,
2145 u8 bManagementFrame,
2146 u8 bShortPreamble
2149 u16 FrameTime;
2150 u16 N_DBPS;
2151 u16 Ceiling;
2153 if( rtl8180_IsWirelessBMode(DataRate) )
2155 if( bManagementFrame || !bShortPreamble || DataRate == 10 )
2156 { // long preamble
2157 FrameTime = (u16)(144+48+(FrameLength*8/(DataRate/10)));
2159 else
2160 { // Short preamble
2161 FrameTime = (u16)(72+24+(FrameLength*8/(DataRate/10)));
2163 if( ( FrameLength*8 % (DataRate/10) ) != 0 ) //Get the Ceilling
2164 FrameTime ++;
2165 } else { //802.11g DSSS-OFDM PLCP length field calculation.
2166 N_DBPS = N_DBPSOfRate(DataRate);
2167 Ceiling = (16 + 8*FrameLength + 6) / N_DBPS
2168 + (((16 + 8*FrameLength + 6) % N_DBPS) ? 1 : 0);
2169 FrameTime = (u16)(16 + 4 + 4*Ceiling + 6);
2171 return FrameTime;
2173 u16 N_DBPSOfRate(u16 DataRate)
2175 u16 N_DBPS = 24;
2177 switch(DataRate)
2179 case 60:
2180 N_DBPS = 24;
2181 break;
2183 case 90:
2184 N_DBPS = 36;
2185 break;
2187 case 120:
2188 N_DBPS = 48;
2189 break;
2191 case 180:
2192 N_DBPS = 72;
2193 break;
2195 case 240:
2196 N_DBPS = 96;
2197 break;
2199 case 360:
2200 N_DBPS = 144;
2201 break;
2203 case 480:
2204 N_DBPS = 192;
2205 break;
2207 case 540:
2208 N_DBPS = 216;
2209 break;
2211 default:
2212 break;
2215 return N_DBPS;
2218 //{by amy 080312
2220 // Description:
2221 // For Netgear case, they want good-looking singal strength.
2222 // 2004.12.05, by rcnjko.
2224 long
2225 NetgearSignalStrengthTranslate(
2226 long LastSS,
2227 long CurrSS
2230 long RetSS;
2232 // Step 1. Scale mapping.
2233 if(CurrSS >= 71 && CurrSS <= 100)
2235 RetSS = 90 + ((CurrSS - 70) / 3);
2237 else if(CurrSS >= 41 && CurrSS <= 70)
2239 RetSS = 78 + ((CurrSS - 40) / 3);
2241 else if(CurrSS >= 31 && CurrSS <= 40)
2243 RetSS = 66 + (CurrSS - 30);
2245 else if(CurrSS >= 21 && CurrSS <= 30)
2247 RetSS = 54 + (CurrSS - 20);
2249 else if(CurrSS >= 5 && CurrSS <= 20)
2251 RetSS = 42 + (((CurrSS - 5) * 2) / 3);
2253 else if(CurrSS == 4)
2255 RetSS = 36;
2257 else if(CurrSS == 3)
2259 RetSS = 27;
2261 else if(CurrSS == 2)
2263 RetSS = 18;
2265 else if(CurrSS == 1)
2267 RetSS = 9;
2269 else
2271 RetSS = CurrSS;
2273 //RT_TRACE(COMP_DBG, DBG_LOUD, ("##### After Mapping: LastSS: %d, CurrSS: %d, RetSS: %d\n", LastSS, CurrSS, RetSS));
2275 // Step 2. Smoothing.
2276 if(LastSS > 0)
2278 RetSS = ((LastSS * 5) + (RetSS)+ 5) / 6;
2280 //RT_TRACE(COMP_DBG, DBG_LOUD, ("$$$$$ After Smoothing: LastSS: %d, CurrSS: %d, RetSS: %d\n", LastSS, CurrSS, RetSS));
2282 return RetSS;
2285 // Description:
2286 // Translate 0-100 signal strength index into dBm.
2288 long
2289 TranslateToDbm8185(
2290 u8 SignalStrengthIndex // 0-100 index.
2293 long SignalPower; // in dBm.
2295 // Translate to dBm (x=0.5y-95).
2296 SignalPower = (long)((SignalStrengthIndex + 1) >> 1);
2297 SignalPower -= 95;
2299 return SignalPower;
2302 // Description:
2303 // Perform signal smoothing for dynamic mechanism.
2304 // This is different with PerformSignalSmoothing8185 in smoothing fomula.
2305 // No dramatic adjustion is apply because dynamic mechanism need some degree
2306 // of correctness. Ported from 8187B.
2307 // 2007-02-26, by Bruce.
2309 void
2310 PerformUndecoratedSignalSmoothing8185(
2311 struct r8180_priv *priv,
2312 bool bCckRate
2317 // Determin the current packet is CCK rate.
2318 priv->bCurCCKPkt = bCckRate;
2320 if(priv->UndecoratedSmoothedSS >= 0)
2322 priv->UndecoratedSmoothedSS = ( (priv->UndecoratedSmoothedSS * 5) + (priv->SignalStrength * 10) ) / 6;
2324 else
2326 priv->UndecoratedSmoothedSS = priv->SignalStrength * 10;
2329 priv->UndercorateSmoothedRxPower = ( (priv->UndercorateSmoothedRxPower * 50) + (priv->RxPower* 11)) / 60;
2331 // printk("Sommthing SignalSterngth (%d) => UndecoratedSmoothedSS (%d)\n", priv->SignalStrength, priv->UndecoratedSmoothedSS);
2332 // printk("Sommthing RxPower (%d) => UndecoratedRxPower (%d)\n", priv->RxPower, priv->UndercorateSmoothedRxPower);
2334 //if(priv->CurCCKRSSI >= 0 && bCckRate)
2335 if(bCckRate)
2337 priv->CurCCKRSSI = priv->RSSI;
2339 else
2341 priv->CurCCKRSSI = 0;
2344 // Boundary checking.
2345 // TODO: The overflow condition does happen, if we want to fix,
2346 // we shall recalculate thresholds first.
2347 if(priv->UndecoratedSmoothedSS > 100)
2349 // printk("UndecoratedSmoothedSS(%d) overflow, SignalStrength(%d)\n", priv->UndecoratedSmoothedSS, priv->SignalStrength);
2351 if(priv->UndecoratedSmoothedSS < 0)
2353 // printk("UndecoratedSmoothedSS(%d) underflow, SignalStrength(%d)\n", priv->UndecoratedSmoothedSS, priv->SignalStrength);
2358 //by amy 080312}
2360 /* This is rough RX isr handling routine*/
2361 void rtl8180_rx(struct net_device *dev)
2363 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
2364 struct sk_buff *tmp_skb;
2366 //struct sk_buff *skb;
2367 short first,last;
2368 u32 len;
2369 int lastlen;
2370 unsigned char quality, signal;
2371 u8 rate;
2372 //u32 *prism_hdr;
2373 u32 *tmp,*tmp2;
2374 u8 rx_desc_size;
2375 u8 padding;
2376 //u32 count=0;
2377 char rxpower = 0;
2378 u32 RXAGC = 0;
2379 long RxAGC_dBm = 0;
2380 u8 LNA=0, BB=0;
2381 u8 LNA_gain[4]={02, 17, 29, 39};
2382 u8 Antenna = 0;
2383 struct ieee80211_hdr *hdr;//by amy
2384 u16 fc,type;
2385 u8 bHwError = 0,bCRC = 0,bICV = 0;
2386 //bHwError = 0;
2387 //bCRC = 0;
2388 //bICV = 0;
2389 bool bCckRate = false;
2390 u8 RSSI = 0;
2391 long SignalStrengthIndex = 0;//+by amy 080312
2392 // u8 SignalStrength = 0;
2393 struct ieee80211_rx_stats stats = {
2394 .signal = 0,
2395 .noise = -98,
2396 .rate = 0,
2397 // .mac_time = jiffies,
2398 .freq = IEEE80211_24GHZ_BAND,
2401 #ifdef CONFIG_RTL8185B
2402 stats.nic_type = NIC_8185B;
2403 rx_desc_size = 8;
2405 #else
2406 stats.nic_type = NIC_8185;
2407 rx_desc_size = 4;
2408 #endif
2409 //printk("receive frame!%d\n",count++);
2410 //if (!priv->rxbuffer) DMESG ("EE: NIC RX ack, but RX queue corrupted!");
2411 //else {
2413 if ((*(priv->rxringtail)) & (1<<31)) {
2415 /* we have got an RX int, but the descriptor
2416 * we are pointing is empty*/
2418 priv->stats.rxnodata++;
2419 priv->ieee80211->stats.rx_errors++;
2421 /* if (! *(priv->rxring) & (1<<31)) {
2423 priv->stats.rxreset++;
2424 priv->rxringtail=priv->rxring;
2425 priv->rxbuffer=priv->rxbufferhead;
2427 }else{*/
2429 #if 0
2430 /* Maybe it is possible that the NIC has skipped some descriptors or
2431 * it has reset its internal pointer to the beginning of the ring
2432 * we search for the first filled descriptor in the ring, or we break
2433 * putting again the pointer in the old location if we do not found any.
2434 * This is quite dangerous, what does happen if the nic writes
2435 * two descriptor (say A and B) when we have just checked the descriptor
2436 * A and we are going to check the descriptor B..This might happen if the
2437 * interrupt was dummy, there was not really filled descriptors and
2438 * the NIC didn't lose pointer
2441 //priv->stats.rxwrkaround++;
2443 tmp = priv->rxringtail;
2444 while (*(priv->rxringtail) & (1<<31)){
2446 priv->rxringtail+=4;
2448 if(priv->rxringtail >=
2449 (priv->rxring)+(priv->rxringcount )*4)
2450 priv->rxringtail=priv->rxring;
2452 priv->rxbuffer=(priv->rxbuffer->next);
2454 if(priv->rxringtail == tmp ){
2455 //DMESG("EE: Could not find RX pointer");
2456 priv->stats.rxnopointer++;
2457 break;
2460 #else
2462 tmp2 = NULL;
2463 tmp = priv->rxringtail;
2465 if(tmp == priv->rxring)
2466 //tmp = priv->rxring + (priv->rxringcount )*rx_desc_size; xiong-2006-11-15
2467 tmp = priv->rxring + (priv->rxringcount - 1)*rx_desc_size;
2468 else
2469 tmp -= rx_desc_size;
2471 if(! (*tmp & (1<<31)))
2472 tmp2 = tmp;
2473 }while(tmp != priv->rxring);
2475 if(tmp2) priv->rxringtail = tmp2;
2476 #endif
2480 /* while there are filled descriptors */
2481 while(!(*(priv->rxringtail) & (1<<31))){
2482 if(*(priv->rxringtail) & (1<<26))
2483 DMESGW("RX buffer overflow");
2484 if(*(priv->rxringtail) & (1<<12))
2485 priv->stats.rxicverr++;
2487 if(*(priv->rxringtail) & (1<<27)){
2488 priv->stats.rxdmafail++;
2489 //DMESG("EE: RX DMA FAILED at buffer pointed by descriptor %x",(u32)priv->rxringtail);
2490 goto drop;
2493 pci_dma_sync_single_for_cpu(priv->pdev,
2494 priv->rxbuffer->dma,
2495 priv->rxbuffersize * \
2496 sizeof(u8),
2497 PCI_DMA_FROMDEVICE);
2499 first = *(priv->rxringtail) & (1<<29) ? 1:0;
2500 if(first) priv->rx_prevlen=0;
2502 last = *(priv->rxringtail) & (1<<28) ? 1:0;
2503 if(last){
2504 lastlen=((*priv->rxringtail) &0xfff);
2506 /* if the last descriptor (that should
2507 * tell us the total packet len) tell
2508 * us something less than the descriptors
2509 * len we had until now, then there is some
2510 * problem..
2511 * workaround to prevent kernel panic
2513 if(lastlen < priv->rx_prevlen)
2514 len=0;
2515 else
2516 len=lastlen-priv->rx_prevlen;
2518 if(*(priv->rxringtail) & (1<<13)) {
2519 //lastlen=((*priv->rxringtail) &0xfff);
2520 if ((*(priv->rxringtail) & 0xfff) <500)
2521 priv->stats.rxcrcerrmin++;
2522 else if ((*(priv->rxringtail) & 0x0fff) >1000)
2523 priv->stats.rxcrcerrmax++;
2524 else
2525 priv->stats.rxcrcerrmid++;
2529 }else{
2530 len = priv->rxbuffersize;
2533 #ifdef CONFIG_RTL8185B
2534 if(first && last) {
2535 padding = ((*(priv->rxringtail+3))&(0x04000000))>>26;
2536 }else if(first) {
2537 padding = ((*(priv->rxringtail+3))&(0x04000000))>>26;
2538 if(padding) {
2539 len -= 2;
2541 }else {
2542 padding = 0;
2544 #ifdef CONFIG_RTL818X_S
2545 padding = 0;
2546 #endif
2547 #endif
2548 priv->rx_prevlen+=len;
2550 if(priv->rx_prevlen > MAX_FRAG_THRESHOLD + 100){
2551 /* HW is probably passing several buggy frames
2552 * without FD or LD flag set.
2553 * Throw this garbage away to prevent skb
2554 * memory exausting
2556 if(!priv->rx_skb_complete)
2557 dev_kfree_skb_any(priv->rx_skb);
2558 priv->rx_skb_complete = 1;
2561 #ifdef DEBUG_RX_FRAG
2562 DMESG("Iteration.. len %x",len);
2563 if(first) DMESG ("First descriptor");
2564 if(last) DMESG("Last descriptor");
2566 #endif
2567 #ifdef DEBUG_RX_VERBOSE
2568 print_buffer( priv->rxbuffer->buf, len);
2569 #endif
2571 #ifdef CONFIG_RTL8185B
2572 signal=(unsigned char)(((*(priv->rxringtail+3))& (0x00ff0000))>>16);
2573 signal=(signal&0xfe)>>1; // Modify by hikaru 6.6
2575 quality=(unsigned char)((*(priv->rxringtail+3)) & (0xff));
2577 stats.mac_time[0] = *(priv->rxringtail+1);
2578 stats.mac_time[1] = *(priv->rxringtail+2);
2579 rxpower =((char)(((*(priv->rxringtail+4))& (0x00ff0000))>>16))/2 - 42;
2580 RSSI = ((u8)(((*(priv->rxringtail+3)) & (0x0000ff00))>> 8)) & (0x7f);
2582 #else
2583 signal=((*(priv->rxringtail+1))& (0xff0000))>>16;
2584 signal=(signal&0xfe)>>1; // Modify by hikaru 6.6
2586 quality=((*(priv->rxringtail+1)) & (0xff));
2588 stats.mac_time[0] = *(priv->rxringtail+2);
2589 stats.mac_time[1] = *(priv->rxringtail+3);
2590 #endif
2591 rate=((*(priv->rxringtail)) &
2592 ((1<<23)|(1<<22)|(1<<21)|(1<<20)))>>20;
2594 stats.rate = rtl8180_rate2rate(rate);
2595 //DMESG("%d",rate);
2596 Antenna = (((*(priv->rxringtail +3))& (0x00008000)) == 0 )? 0:1 ;
2597 // printk("in rtl8180_rx():Antenna is %d\n",Antenna);
2598 //by amy for antenna
2599 if(!rtl8180_IsWirelessBMode(stats.rate))
2600 { // OFDM rate.
2602 RxAGC_dBm = rxpower+1; //bias
2604 else
2605 { // CCK rate.
2606 RxAGC_dBm = signal;//bit 0 discard
2608 LNA = (u8) (RxAGC_dBm & 0x60 ) >> 5 ; //bit 6~ bit 5
2609 BB = (u8) (RxAGC_dBm & 0x1F); // bit 4 ~ bit 0
2611 RxAGC_dBm = -( LNA_gain[LNA] + (BB *2) ); //Pin_11b=-(LNA_gain+BB_gain) (dBm)
2613 RxAGC_dBm +=4; //bias
2616 if(RxAGC_dBm & 0x80) //absolute value
2617 RXAGC= ~(RxAGC_dBm)+1;
2618 bCckRate = rtl8180_IsWirelessBMode(stats.rate);
2619 // Translate RXAGC into 1-100.
2620 if(!rtl8180_IsWirelessBMode(stats.rate))
2621 { // OFDM rate.
2622 if(RXAGC>90)
2623 RXAGC=90;
2624 else if(RXAGC<25)
2625 RXAGC=25;
2626 RXAGC=(90-RXAGC)*100/65;
2628 else
2629 { // CCK rate.
2630 if(RXAGC>95)
2631 RXAGC=95;
2632 else if(RXAGC<30)
2633 RXAGC=30;
2634 RXAGC=(95-RXAGC)*100/65;
2636 priv->SignalStrength = (u8)RXAGC;
2637 priv->RecvSignalPower = RxAGC_dBm ; // It can use directly by SD3 CMLin
2638 priv->RxPower = rxpower;
2639 priv->RSSI = RSSI;
2640 //{by amy 080312
2641 // SQ translation formular is provided by SD3 DZ. 2006.06.27, by rcnjko.
2642 if(quality >= 127)
2643 quality = 1;//0; //0 will cause epc to show signal zero , walk aroud now;
2644 else if(quality < 27)
2645 quality = 100;
2646 else
2647 quality = 127 - quality;
2648 priv->SignalQuality = quality;
2649 if(!priv->card_8185)
2650 printk("check your card type\n");
2652 stats.signal = (u8)quality;//priv->wstats.qual.level = priv->SignalStrength;
2653 stats.signalstrength = RXAGC;
2654 if(stats.signalstrength > 100)
2655 stats.signalstrength = 100;
2656 stats.signalstrength = (stats.signalstrength * 70)/100 + 30;
2657 // printk("==========================>rx : RXAGC is %d,signalstrength is %d\n",RXAGC,stats.signalstrength);
2658 stats.rssi = priv->wstats.qual.qual = priv->SignalQuality;
2659 stats.noise = priv->wstats.qual.noise = 100 - priv ->wstats.qual.qual;
2660 //by amy 080312}
2661 bHwError = (((*(priv->rxringtail))& (0x00000fff)) == 4080)| (((*(priv->rxringtail))& (0x04000000)) != 0 )
2662 | (((*(priv->rxringtail))& (0x08000000)) != 0 )| (((~(*(priv->rxringtail)))& (0x10000000)) != 0 )| (((~(*(priv->rxringtail)))& (0x20000000)) != 0 );
2663 bCRC = ((*(priv->rxringtail)) & (0x00002000)) >> 13;
2664 bICV = ((*(priv->rxringtail)) & (0x00001000)) >> 12;
2665 hdr = (struct ieee80211_hdr *)priv->rxbuffer->buf;
2666 fc = le16_to_cpu(hdr->frame_ctl);
2667 type = WLAN_FC_GET_TYPE(fc);
2669 if((IEEE80211_FTYPE_CTL != type) &&
2670 (eqMacAddr(priv->ieee80211->current_network.bssid, (fc & IEEE80211_FCTL_TODS)? hdr->addr1 : (fc & IEEE80211_FCTL_FROMDS )? hdr->addr2 : hdr->addr3))
2671 && (!bHwError) && (!bCRC)&& (!bICV))
2673 //by amy 080312
2674 // Perform signal smoothing for dynamic mechanism on demand.
2675 // This is different with PerformSignalSmoothing8185 in smoothing fomula.
2676 // No dramatic adjustion is apply because dynamic mechanism need some degree
2677 // of correctness. 2007.01.23, by shien chang.
2678 PerformUndecoratedSignalSmoothing8185(priv,bCckRate);
2680 // For good-looking singal strength.
2682 SignalStrengthIndex = NetgearSignalStrengthTranslate(
2683 priv->LastSignalStrengthInPercent,
2684 priv->SignalStrength);
2686 priv->LastSignalStrengthInPercent = SignalStrengthIndex;
2687 priv->Stats_SignalStrength = TranslateToDbm8185((u8)SignalStrengthIndex);
2689 // We need more correct power of received packets and the "SignalStrength" of RxStats is beautified,
2690 // so we record the correct power here.
2692 priv->Stats_SignalQuality =(long) (priv->Stats_SignalQuality * 5 + (long)priv->SignalQuality + 5) / 6;
2693 priv->Stats_RecvSignalPower = (long)(priv->Stats_RecvSignalPower * 5 + priv->RecvSignalPower -1) / 6;
2695 // Figure out which antenna that received the lasted packet.
2696 priv->LastRxPktAntenna = Antenna ? 1 : 0; // 0: aux, 1: main.
2697 //by amy 080312
2698 SwAntennaDiversityRxOk8185(dev, priv->SignalStrength);
2701 //by amy for antenna
2708 #ifndef DUMMY_RX
2709 if(first){
2710 if(!priv->rx_skb_complete){
2711 /* seems that HW sometimes fails to reiceve and
2712 doesn't provide the last descriptor */
2713 #ifdef DEBUG_RX_SKB
2714 DMESG("going to free incomplete skb");
2715 #endif
2716 dev_kfree_skb_any(priv->rx_skb);
2717 priv->stats.rxnolast++;
2718 #ifdef DEBUG_RX_SKB
2719 DMESG("free incomplete skb OK");
2720 #endif
2722 /* support for prism header has been originally added by Christian */
2723 if(priv->prism_hdr && priv->ieee80211->iw_mode == IW_MODE_MONITOR){
2725 #if 0
2726 priv->rx_skb = dev_alloc_skb(len+2+PRISM_HDR_SIZE);
2727 if(! priv->rx_skb) goto drop;
2729 prism_hdr = (u32*) skb_put(priv->rx_skb,PRISM_HDR_SIZE);
2730 prism_hdr[0]=htonl(0x80211001); //version
2731 prism_hdr[1]=htonl(0x40); //length
2732 prism_hdr[2]=htonl(stats.mac_time[1]); //mactime (HIGH)
2733 prism_hdr[3]=htonl(stats.mac_time[0]); //mactime (LOW)
2734 rdtsc(prism_hdr[5], prism_hdr[4]); //hostime (LOW+HIGH)
2735 prism_hdr[4]=htonl(prism_hdr[4]); //Byte-Order aendern
2736 prism_hdr[5]=htonl(prism_hdr[5]); //Byte-Order aendern
2737 prism_hdr[6]=0x00; //phytype
2738 prism_hdr[7]=htonl(priv->chan); //channel
2739 prism_hdr[8]=htonl(stats.rate); //datarate
2740 prism_hdr[9]=0x00; //antenna
2741 prism_hdr[10]=0x00; //priority
2742 prism_hdr[11]=0x00; //ssi_type
2743 prism_hdr[12]=htonl(stats.signal); //ssi_signal
2744 prism_hdr[13]=htonl(stats.noise); //ssi_noise
2745 prism_hdr[14]=0x00; //preamble
2746 prism_hdr[15]=0x00; //encoding
2748 #endif
2749 }else{
2750 priv->rx_skb = dev_alloc_skb(len+2);
2751 if( !priv->rx_skb) goto drop;
2752 #ifdef DEBUG_RX_SKB
2753 DMESG("Alloc initial skb %x",len+2);
2754 #endif
2757 priv->rx_skb_complete=0;
2758 priv->rx_skb->dev=dev;
2759 }else{
2760 /* if we are here we should have already RXed
2761 * the first frame.
2762 * If we get here and the skb is not allocated then
2763 * we have just throw out garbage (skb not allocated)
2764 * and we are still rxing garbage....
2766 if(!priv->rx_skb_complete){
2768 tmp_skb= dev_alloc_skb(priv->rx_skb->len +len+2);
2770 if(!tmp_skb) goto drop;
2772 tmp_skb->dev=dev;
2773 #ifdef DEBUG_RX_SKB
2774 DMESG("Realloc skb %x",len+2);
2775 #endif
2777 #ifdef DEBUG_RX_SKB
2778 DMESG("going copy prev frag %x",priv->rx_skb->len);
2779 #endif
2780 memcpy(skb_put(tmp_skb,priv->rx_skb->len),
2781 priv->rx_skb->data,
2782 priv->rx_skb->len);
2783 #ifdef DEBUG_RX_SKB
2784 DMESG("skb copy prev frag complete");
2785 #endif
2787 dev_kfree_skb_any(priv->rx_skb);
2788 #ifdef DEBUG_RX_SKB
2789 DMESG("prev skb free ok");
2790 #endif
2792 priv->rx_skb=tmp_skb;
2795 #ifdef DEBUG_RX_SKB
2796 DMESG("going to copy current payload %x",len);
2797 #endif
2798 if(!priv->rx_skb_complete) {
2799 #ifdef CONFIG_RTL8185B
2800 if(padding) {
2801 memcpy(skb_put(priv->rx_skb,len),
2802 (((unsigned char *)priv->rxbuffer->buf) + 2),len);
2803 } else {
2804 #endif
2805 memcpy(skb_put(priv->rx_skb,len),
2806 priv->rxbuffer->buf,len);
2807 #ifdef CONFIG_RTL8185B
2809 #endif
2811 #ifdef DEBUG_RX_SKB
2812 DMESG("current fragment skb copy complete");
2813 #endif
2815 if(last && !priv->rx_skb_complete){
2817 #ifdef DEBUG_RX_SKB
2818 DMESG("Got last fragment");
2819 #endif
2821 if(priv->rx_skb->len > 4)
2822 skb_trim(priv->rx_skb,priv->rx_skb->len-4);
2823 #ifdef DEBUG_RX_SKB
2824 DMESG("yanked out crc, passing to the upper layer");
2825 #endif
2827 #ifndef RX_DONT_PASS_UL
2828 if(!ieee80211_rx(priv->ieee80211,
2829 priv->rx_skb, &stats)){
2830 #ifdef DEBUG_RX
2831 DMESGW("Packet not consumed");
2832 #endif
2833 #endif // RX_DONT_PASS_UL
2835 dev_kfree_skb_any(priv->rx_skb);
2836 #ifndef RX_DONT_PASS_UL
2838 #endif
2839 #ifdef DEBUG_RX
2840 else{
2841 DMESG("Rcv frag");
2843 #endif
2844 priv->rx_skb_complete=1;
2847 #endif //DUMMY_RX
2849 pci_dma_sync_single_for_device(priv->pdev,
2850 priv->rxbuffer->dma,
2851 priv->rxbuffersize * \
2852 sizeof(u8),
2853 PCI_DMA_FROMDEVICE);
2856 drop: // this is used when we have not enought mem
2858 /* restore the descriptor */
2859 *(priv->rxringtail+2)=priv->rxbuffer->dma;
2860 *(priv->rxringtail)=*(priv->rxringtail) &~ 0xfff;
2861 *(priv->rxringtail)=
2862 *(priv->rxringtail) | priv->rxbuffersize;
2864 *(priv->rxringtail)=
2865 *(priv->rxringtail) | (1<<31);
2866 //^empty descriptor
2868 //wmb();
2870 #ifdef DEBUG_RX
2871 DMESG("Current descriptor: %x",(u32)priv->rxringtail);
2872 #endif
2873 //unsigned long flags;
2874 //spin_lock_irqsave(&priv->irq_lock,flags);
2876 priv->rxringtail+=rx_desc_size;
2877 if(priv->rxringtail >=
2878 (priv->rxring)+(priv->rxringcount )*rx_desc_size)
2879 priv->rxringtail=priv->rxring;
2881 //spin_unlock_irqrestore(&priv->irq_lock,flags);
2884 priv->rxbuffer=(priv->rxbuffer->next);
2890 // if(get_curr_tx_free_desc(dev,priority))
2891 // ieee80211_sta_ps_sleep(priv->ieee80211, &tmp, &tmp2);
2898 void rtl8180_dma_kick(struct net_device *dev, int priority)
2900 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
2902 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
2905 switch(priority){
2907 case LOW_PRIORITY:
2909 write_nic_byte(dev,TX_DMA_POLLING,
2910 (1<< TX_DMA_POLLING_LOWPRIORITY_SHIFT) |
2911 priv->dma_poll_mask);
2912 break;
2914 case NORM_PRIORITY:
2916 write_nic_byte(dev,TX_DMA_POLLING,
2917 (1<< TX_DMA_POLLING_NORMPRIORITY_SHIFT) |
2918 priv->dma_poll_mask);
2919 break;
2921 case HI_PRIORITY:
2923 write_nic_byte(dev,TX_DMA_POLLING,
2924 (1<< TX_DMA_POLLING_HIPRIORITY_SHIFT) |
2925 priv->dma_poll_mask);
2926 break;
2930 write_nic_byte(dev, TX_DMA_POLLING,
2931 (1 << (priority + 1)) | priv->dma_poll_mask);
2932 rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
2934 force_pci_posting(dev);
2937 #if 0
2938 void rtl8180_tx_queues_stop(struct net_device *dev)
2940 //struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
2941 u8 dma_poll_mask = (1<<TX_DMA_STOP_LOWPRIORITY_SHIFT);
2942 dma_poll_mask |= (1<<TX_DMA_STOP_HIPRIORITY_SHIFT);
2943 dma_poll_mask |= (1<<TX_DMA_STOP_NORMPRIORITY_SHIFT);
2944 dma_poll_mask |= (1<<TX_DMA_STOP_BEACON_SHIFT);
2946 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
2947 write_nic_byte(dev,TX_DMA_POLLING,dma_poll_mask);
2948 rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
2950 #endif
2952 void rtl8180_data_hard_stop(struct net_device *dev)
2954 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
2956 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
2957 #ifdef CONFIG_RTL8185B
2958 priv->dma_poll_stop_mask |= TPPOLLSTOP_AC_VIQ;
2959 write_nic_byte(dev,TPPollStop, priv->dma_poll_stop_mask);
2960 #else
2961 priv->dma_poll_mask |= (1<<TX_DMA_STOP_LOWPRIORITY_SHIFT);
2962 write_nic_byte(dev,TX_DMA_POLLING,priv->dma_poll_mask);
2963 #endif
2964 rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
2968 void rtl8180_data_hard_resume(struct net_device *dev)
2970 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
2972 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
2973 #ifdef CONFIG_RTL8185B
2974 priv->dma_poll_stop_mask &= ~(TPPOLLSTOP_AC_VIQ);
2975 write_nic_byte(dev,TPPollStop, priv->dma_poll_stop_mask);
2976 #else
2977 priv->dma_poll_mask &= ~(1<<TX_DMA_STOP_LOWPRIORITY_SHIFT);
2978 write_nic_byte(dev,TX_DMA_POLLING,priv->dma_poll_mask);
2979 #endif
2980 rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
2984 /* this function TX data frames when the ieee80211 stack requires this.
2985 * It checks also if we need to stop the ieee tx queue, eventually do it
2987 void rtl8180_hard_data_xmit(struct sk_buff *skb,struct net_device *dev, int
2988 rate)
2990 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
2991 int mode;
2992 struct ieee80211_hdr_3addr *h = (struct ieee80211_hdr_3addr *) skb->data;
2993 short morefrag = (h->frame_ctl) & IEEE80211_FCTL_MOREFRAGS;
2994 unsigned long flags;
2995 int priority;
2996 //static int count = 0;
2998 mode = priv->ieee80211->iw_mode;
3000 rate = ieeerate2rtlrate(rate);
3002 * This function doesn't require lock because we make
3003 * sure it's called with the tx_lock already acquired.
3004 * this come from the kernel's hard_xmit callback (trought
3005 * the ieee stack, or from the try_wake_queue (again trought
3006 * the ieee stack.
3008 #ifdef CONFIG_RTL8185B
3009 priority = AC2Q(skb->priority);
3010 #else
3011 priority = LOW_PRIORITY;
3012 #endif
3013 spin_lock_irqsave(&priv->tx_lock,flags);
3015 if(priv->ieee80211->bHwRadioOff)
3017 spin_unlock_irqrestore(&priv->tx_lock,flags);
3019 return;
3022 //printk(KERN_WARNING "priority = %d@%d\n", priority, count++);
3023 if (!check_nic_enought_desc(dev, priority)){
3024 //DMESG("Error: no descriptor left by previous TX (avail %d) ",
3025 // get_curr_tx_free_desc(dev, priority));
3026 DMESGW("Error: no descriptor left by previous TX (avail %d) ",
3027 get_curr_tx_free_desc(dev, priority));
3028 //printk(KERN_WARNING "==============================================================> \n");
3029 ieee80211_stop_queue(priv->ieee80211);
3031 rtl8180_tx(dev, skb->data, skb->len, priority, morefrag,0,rate);
3032 if (!check_nic_enought_desc(dev, priority))
3033 ieee80211_stop_queue(priv->ieee80211);
3035 //dev_kfree_skb_any(skb);
3036 spin_unlock_irqrestore(&priv->tx_lock,flags);
3040 /* This is a rough attempt to TX a frame
3041 * This is called by the ieee 80211 stack to TX management frames.
3042 * If the ring is full packet are dropped (for data frame the queue
3043 * is stopped before this can happen). For this reason it is better
3044 * if the descriptors are larger than the largest management frame
3045 * we intend to TX: i'm unsure what the HW does if it will not found
3046 * the last fragment of a frame because it has been dropped...
3047 * Since queues for Management and Data frames are different we
3048 * might use a different lock than tx_lock (for example mgmt_tx_lock)
3050 /* these function may loops if invoked with 0 descriptors or 0 len buffer*/
3051 int rtl8180_hard_start_xmit(struct sk_buff *skb,struct net_device *dev)
3053 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
3055 unsigned long flags;
3057 int priority;
3059 #ifdef CONFIG_RTL8185B
3060 priority = MANAGE_PRIORITY;
3061 #else
3062 priority = NORM_PRIORITY;
3063 #endif
3065 spin_lock_irqsave(&priv->tx_lock,flags);
3067 if(priv->ieee80211->bHwRadioOff)
3069 spin_unlock_irqrestore(&priv->tx_lock,flags);
3071 dev_kfree_skb_any(skb);
3072 return 0;
3075 rtl8180_tx(dev, skb->data, skb->len, priority,
3076 0, 0,ieeerate2rtlrate(priv->ieee80211->basic_rate));
3078 priv->ieee80211->stats.tx_bytes+=skb->len;
3079 priv->ieee80211->stats.tx_packets++;
3080 spin_unlock_irqrestore(&priv->tx_lock,flags);
3082 dev_kfree_skb_any(skb);
3083 return 0;
3086 // longpre 144+48 shortpre 72+24
3087 u16 rtl8180_len2duration(u32 len, short rate,short* ext)
3089 u16 duration;
3090 u16 drift;
3091 *ext=0;
3093 switch(rate){
3094 case 0://1mbps
3095 *ext=0;
3096 duration = ((len+4)<<4) /0x2;
3097 drift = ((len+4)<<4) % 0x2;
3098 if(drift ==0 ) break;
3099 duration++;
3100 break;
3102 case 1://2mbps
3103 *ext=0;
3104 duration = ((len+4)<<4) /0x4;
3105 drift = ((len+4)<<4) % 0x4;
3106 if(drift ==0 ) break;
3107 duration++;
3108 break;
3110 case 2: //5.5mbps
3111 *ext=0;
3112 duration = ((len+4)<<4) /0xb;
3113 drift = ((len+4)<<4) % 0xb;
3114 if(drift ==0 )
3115 break;
3116 duration++;
3117 break;
3119 default:
3120 case 3://11mbps
3121 *ext=0;
3122 duration = ((len+4)<<4) /0x16;
3123 drift = ((len+4)<<4) % 0x16;
3124 if(drift ==0 )
3125 break;
3126 duration++;
3127 if(drift > 6)
3128 break;
3129 *ext=1;
3130 break;
3133 return duration;
3137 void rtl8180_prepare_beacon(struct net_device *dev)
3140 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
3142 struct sk_buff *skb;
3144 u16 word = read_nic_word(dev, BcnItv);
3145 word &= ~BcnItv_BcnItv; // clear Bcn_Itv
3146 word |= cpu_to_le16(priv->ieee80211->current_network.beacon_interval);//0x64;
3147 write_nic_word(dev, BcnItv, word);
3150 skb = ieee80211_get_beacon(priv->ieee80211);
3151 if(skb){
3152 rtl8180_tx(dev,skb->data,skb->len,BEACON_PRIORITY,
3153 0,0,ieeerate2rtlrate(priv->ieee80211->basic_rate));
3154 dev_kfree_skb_any(skb);
3156 #if 0
3157 //DMESG("size %x",len);
3158 if(*tail & (1<<31)){
3160 //DMESG("No more beacon TX desc");
3161 return ;
3164 //while(! *tail & (1<<31)){
3165 *tail= 0; // zeroes header
3167 *tail = *tail| (1<<29) ; //fist segment of the packet
3168 *tail = (*tail) | (1<<28); // last segment
3169 // *tail = *tail | (1<<18); // this is a beacon frame
3170 *(tail+3)=*(tail+3) &~ 0xfff;
3171 *(tail+3)=*(tail+3) | len; // buffer lenght
3172 *tail = *tail |len;
3173 // zeroes the second 32-bits dword of the descriptor
3174 *(tail+1)= 0;
3175 *tail = *tail | (rate << 24);
3177 duration = rtl8180_len2duration(len,rate,&ext);
3179 *(tail+1) = *(tail+1) | ((duration & 0x7fff)<<16);
3181 *tail = *tail | (1<<31);
3182 //^ descriptor ready to be txed
3183 if((tail - begin)/8 == priv->txbeaconcount-1)
3184 tail=begin;
3185 else
3186 tail=tail+8;
3188 #endif
3191 /* This function do the real dirty work: it enqueues a TX command
3192 * descriptor in the ring buffer, copyes the frame in a TX buffer
3193 * and kicks the NIC to ensure it does the DMA transfer.
3195 short rtl8180_tx(struct net_device *dev, u8* txbuf, int len, int priority,
3196 short morefrag, short descfrag, int rate)
3198 struct r8180_priv *priv = ieee80211_priv(dev);
3199 u32 *tail,*temp_tail;
3200 u32 *begin;
3201 u32 *buf;
3202 int i;
3203 int remain;
3204 int buflen;
3205 int count;
3206 //u16 AckCtsTime;
3207 //u16 FrameTime;
3208 u16 duration;
3209 short ext;
3210 struct buffer* buflist;
3211 //unsigned long flags;
3212 #ifdef CONFIG_RTL8185B
3213 struct ieee80211_hdr_3addr *frag_hdr = (struct ieee80211_hdr_3addr *)txbuf;
3214 u8 dest[ETH_ALEN];
3215 u8 bUseShortPreamble = 0;
3216 u8 bCTSEnable = 0;
3217 u8 bRTSEnable = 0;
3218 //u16 RTSRate = 22;
3219 //u8 RetryLimit = 0;
3220 u16 Duration = 0;
3221 u16 RtsDur = 0;
3222 u16 ThisFrameTime = 0;
3223 u16 TxDescDuration = 0;
3224 u8 ownbit_flag = false; //added by david woo for sync Tx, 2007.12.14
3225 #endif
3227 switch(priority) {
3228 case MANAGE_PRIORITY:
3229 tail=priv->txmapringtail;
3230 begin=priv->txmapring;
3231 buflist = priv->txmapbufstail;
3232 count = priv->txringcount;
3233 break;
3235 case BK_PRIORITY:
3236 tail=priv->txbkpringtail;
3237 begin=priv->txbkpring;
3238 buflist = priv->txbkpbufstail;
3239 count = priv->txringcount;
3240 break;
3242 case BE_PRIORITY:
3243 tail=priv->txbepringtail;
3244 begin=priv->txbepring;
3245 buflist = priv->txbepbufstail;
3246 count = priv->txringcount;
3247 break;
3249 case VI_PRIORITY:
3250 tail=priv->txvipringtail;
3251 begin=priv->txvipring;
3252 buflist = priv->txvipbufstail;
3253 count = priv->txringcount;
3254 break;
3256 case VO_PRIORITY:
3257 tail=priv->txvopringtail;
3258 begin=priv->txvopring;
3259 buflist = priv->txvopbufstail;
3260 count = priv->txringcount;
3261 break;
3263 case HI_PRIORITY:
3264 tail=priv->txhpringtail;
3265 begin=priv->txhpring;
3266 buflist = priv->txhpbufstail;
3267 count = priv->txringcount;
3268 break;
3270 case BEACON_PRIORITY:
3271 tail=priv->txbeaconringtail;
3272 begin=priv->txbeaconring;
3273 buflist = priv->txbeaconbufstail;
3274 count = priv->txbeaconcount;
3275 break;
3277 default:
3278 return -1;
3279 break;
3282 //printk("in rtl8180_tx(): rate is %d\n",priv->ieee80211->rate);
3283 #if 1
3284 memcpy(&dest, frag_hdr->addr1, ETH_ALEN);
3285 if (is_multicast_ether_addr(dest) ||
3286 is_broadcast_ether_addr(dest))
3288 Duration = 0;
3289 RtsDur = 0;
3290 bRTSEnable = 0;
3291 bCTSEnable = 0;
3293 ThisFrameTime = ComputeTxTime(len + sCrcLng, rtl8180_rate2rate(rate), 0, bUseShortPreamble);
3294 TxDescDuration = ThisFrameTime;
3295 } else {// Unicast packet
3296 //u8 AckRate;
3297 u16 AckTime;
3299 //YJ,add,080828,for Keep alive
3300 priv->NumTxUnicast++;
3302 // Figure out ACK rate according to BSS basic rate and Tx rate, 2006.03.08 by rcnjko.
3303 //AckRate = ComputeAckRate( pMgntInfo->mBrates, (u1Byte)(pTcb->DataRate) );
3304 // Figure out ACK time according to the AckRate and assume long preamble is used on receiver, 2006.03.08, by rcnjko.
3305 //AckTime = ComputeTxTime( sAckCtsLng/8, AckRate, FALSE, FALSE);
3306 //For simplicity, just use the 1M basic rate
3307 //AckTime = ComputeTxTime(14, 540,0, 0); // AckCTSLng = 14 use 1M bps send
3308 AckTime = ComputeTxTime(14, 10,0, 0); // AckCTSLng = 14 use 1M bps send
3309 //AckTime = ComputeTxTime(14, 2,false, false); // AckCTSLng = 14 use 1M bps send
3311 if ( ((len + sCrcLng) > priv->rts) && priv->rts )
3312 { // RTS/CTS.
3313 u16 RtsTime, CtsTime;
3314 //u16 CtsRate;
3315 bRTSEnable = 1;
3316 bCTSEnable = 0;
3318 // Rate and time required for RTS.
3319 RtsTime = ComputeTxTime( sAckCtsLng/8,priv->ieee80211->basic_rate, 0, 0);
3320 // Rate and time required for CTS.
3321 CtsTime = ComputeTxTime(14, 10,0, 0); // AckCTSLng = 14 use 1M bps send
3323 // Figure out time required to transmit this frame.
3324 ThisFrameTime = ComputeTxTime(len + sCrcLng,
3325 rtl8180_rate2rate(rate),
3327 bUseShortPreamble);
3329 // RTS-CTS-ThisFrame-ACK.
3330 RtsDur = CtsTime + ThisFrameTime + AckTime + 3*aSifsTime;
3332 TxDescDuration = RtsTime + RtsDur;
3334 else {// Normal case.
3335 bCTSEnable = 0;
3336 bRTSEnable = 0;
3337 RtsDur = 0;
3339 ThisFrameTime = ComputeTxTime(len + sCrcLng, rtl8180_rate2rate(rate), 0, bUseShortPreamble);
3340 TxDescDuration = ThisFrameTime + aSifsTime + AckTime;
3343 if(!(frag_hdr->frame_ctl & IEEE80211_FCTL_MOREFRAGS)) { //no more fragment
3344 // ThisFrame-ACK.
3345 Duration = aSifsTime + AckTime;
3346 } else { // One or more fragments remained.
3347 u16 NextFragTime;
3348 NextFragTime = ComputeTxTime( len + sCrcLng, //pretend following packet length equal current packet
3349 rtl8180_rate2rate(rate),
3351 bUseShortPreamble );
3353 //ThisFrag-ACk-NextFrag-ACK.
3354 Duration = NextFragTime + 3*aSifsTime + 2*AckTime;
3357 } // End of Unicast packet
3359 frag_hdr->duration_id = Duration;
3360 #endif
3362 buflen=priv->txbuffsize;
3363 remain=len;
3364 temp_tail = tail;
3365 //printk("================================>buflen = %d, remain = %d!\n", buflen,remain);
3366 while(remain!=0){
3367 #ifdef DEBUG_TX_FRAG
3368 DMESG("TX iteration");
3369 #endif
3370 #ifdef DEBUG_TX
3371 DMESG("TX: filling descriptor %x",(u32)tail);
3372 #endif
3373 mb();
3374 if(!buflist){
3375 DMESGE("TX buffer error, cannot TX frames. pri %d.", priority);
3376 //spin_unlock_irqrestore(&priv->tx_lock,flags);
3377 return -1;
3379 buf=buflist->buf;
3381 if( (*tail & (1<<31)) && (priority != BEACON_PRIORITY)){
3383 DMESGW("No more TX desc, returning %x of %x",
3384 remain,len);
3385 priv->stats.txrdu++;
3386 #ifdef DEBUG_TX_DESC
3387 check_tx_ring(dev,priority);
3388 // netif_stop_queue(dev);
3389 // netif_carrier_off(dev);
3390 #endif
3391 // spin_unlock_irqrestore(&priv->tx_lock,flags);
3393 return remain;
3397 *tail= 0; // zeroes header
3398 *(tail+1) = 0;
3399 *(tail+3) = 0;
3400 *(tail+5) = 0;
3401 *(tail+6) = 0;
3402 *(tail+7) = 0;
3404 if(priv->card_8185){
3405 //FIXME: this should be triggered by HW encryption parameters.
3406 *tail |= (1<<15); //no encrypt
3407 // *tail |= (1<<30); //raise int when completed
3409 // *tail = *tail | (1<<16);
3410 if(remain==len && !descfrag) {
3411 ownbit_flag = false; //added by david woo,2007.12.14
3412 #ifdef DEBUG_TX_FRAG
3413 DMESG("First descriptor");
3414 #endif
3415 *tail = *tail| (1<<29) ; //fist segment of the packet
3416 *tail = *tail |(len);
3417 } else {
3418 ownbit_flag = true;
3421 for(i=0;i<buflen&& remain >0;i++,remain--){
3422 ((u8*)buf)[i]=txbuf[i]; //copy data into descriptor pointed DMAble buffer
3423 if(remain == 4 && i+4 >= buflen) break;
3424 /* ensure the last desc has at least 4 bytes payload */
3427 txbuf = txbuf + i;
3428 *(tail+3)=*(tail+3) &~ 0xfff;
3429 *(tail+3)=*(tail+3) | i; // buffer lenght
3430 // Use short preamble or not
3431 if (priv->ieee80211->current_network.capability&WLAN_CAPABILITY_SHORT_PREAMBLE)
3432 if (priv->plcp_preamble_mode==1 && rate!=0) // short mode now, not long!
3433 // *tail |= (1<<16); // enable short preamble mode.
3435 #ifdef CONFIG_RTL8185B
3436 if(bCTSEnable) {
3437 *tail |= (1<<18);
3440 if(bRTSEnable) //rts enable
3442 *tail |= ((ieeerate2rtlrate(priv->ieee80211->basic_rate))<<19);//RTS RATE
3443 *tail |= (1<<23);//rts enable
3444 *(tail+1) |=(RtsDur&0xffff);//RTS Duration
3446 *(tail+3) |= ((TxDescDuration&0xffff)<<16); //DURATION
3447 // *(tail+3) |= (0xe6<<16);
3448 *(tail+5) |= (11<<8);//(priv->retry_data<<8); //retry lim ;
3449 #else
3450 //Use RTS or not
3451 #ifdef CONFIG_RTL8187B
3452 if ( (len>priv->rts) && priv->rts && priority!=MANAGE_PRIORITY){
3453 #else
3454 if ( (len>priv->rts) && priv->rts && priority==LOW_PRIORITY){
3455 #endif
3456 *tail |= (1<<23); //enalbe RTS function
3457 *tail |= (0<<19); //use 1M bps send RTS packet
3458 AckCtsTime = ComputeTxTime(14, 10,0, 0); // AckCTSLng = 14 use 1M bps send
3459 FrameTime = ComputeTxTime(len + 4, rtl8180_rate2rate(rate), 0, *tail&(1<<16));
3460 // RTS/CTS time is calculate as follow
3461 duration = FrameTime + 3*10 + 2*AckCtsTime; //10us is the SifsTime;
3462 *(tail+1) |= duration; //Need to edit here! ----hikaru
3463 }else{
3464 *(tail+1)= 0; // zeroes the second 32-bits dword of the descriptor
3466 #endif
3468 *tail = *tail | ((rate&0xf) << 24);
3469 //DMESG("rate %d",rate);
3471 if(priv->card_8185){
3473 #if 0
3474 *(tail+5)&= ~(1<<24); /* tx ant 0 */
3476 *(tail+5) &= ~(1<<23); /* random tx agc 23-16 */
3477 *(tail+5) |= (1<<22)|(1<<21)|(1<<20)|(1<<19)|(1<<18)|(1<<17)|(1<<16);
3479 *(tail+5) &=
3480 ~((1<<15)|(1<<14)|(1<<13)|(1<<12)|(1<<11)|(1<<10)|(1<<9)|(1<<8));
3481 *(tail+5) |= (7<<8); // Max retry limit
3483 *(tail+5) &= ~((1<<7)|(1<<6)|(1<<5)|(1<<4)|(1<<3)|(1<<2)|(1<<1)|(1<<0));
3484 *(tail+5) |= (8<<4); // Max contention window
3485 *(tail+6) |= 4; // Min contention window
3486 #endif
3487 // *(tail+5) = 0;
3490 /* hw_plcp_len is not used for rtl8180 chip */
3491 /* FIXME */
3492 if(priv->card_8185 == 0 || !priv->hw_plcp_len){
3494 duration = rtl8180_len2duration(len,
3495 rate,&ext);
3498 #ifdef DEBUG_TX
3499 DMESG("PLCP duration %d",duration );
3500 //DMESG("drift %d",drift);
3501 DMESG("extension %s", (ext==1) ? "on":"off");
3502 #endif
3503 *(tail+1) = *(tail+1) | ((duration & 0x7fff)<<16);
3504 if(ext) *(tail+1) = *(tail+1) |(1<<31); //plcp length extension
3507 if(morefrag) *tail = (*tail) | (1<<17); // more fragment
3508 if(!remain) *tail = (*tail) | (1<<28); // last segment of frame
3510 #ifdef DEBUG_TX_FRAG
3511 if(!remain)DMESG("Last descriptor");
3512 if(morefrag)DMESG("More frag");
3513 #endif
3514 *(tail+5) = *(tail+5)|(2<<27);
3515 *(tail+7) = *(tail+7)|(1<<4);
3517 wmb();
3518 if(ownbit_flag)
3520 *tail = *tail | (1<<31); // descriptor ready to be txed
3523 #ifdef DEBUG_TX_DESC2
3524 printk("tx desc is:\n");
3525 DMESG("%8x %8x %8x %8x %8x %8x %8x %8x", tail[0], tail[1], tail[2], tail[3],
3526 tail[4], tail[5], tail[6], tail[7]);
3527 #endif
3529 if((tail - begin)/8 == count-1)
3530 tail=begin;
3532 else
3533 tail=tail+8;
3535 buflist=buflist->next;
3537 mb();
3539 switch(priority) {
3540 case MANAGE_PRIORITY:
3541 priv->txmapringtail=tail;
3542 priv->txmapbufstail=buflist;
3543 break;
3545 case BK_PRIORITY:
3546 priv->txbkpringtail=tail;
3547 priv->txbkpbufstail=buflist;
3548 break;
3550 case BE_PRIORITY:
3551 priv->txbepringtail=tail;
3552 priv->txbepbufstail=buflist;
3553 break;
3555 case VI_PRIORITY:
3556 priv->txvipringtail=tail;
3557 priv->txvipbufstail=buflist;
3558 break;
3560 case VO_PRIORITY:
3561 priv->txvopringtail=tail;
3562 priv->txvopbufstail=buflist;
3563 break;
3565 case HI_PRIORITY:
3566 priv->txhpringtail=tail;
3567 priv->txhpbufstail = buflist;
3568 break;
3570 case BEACON_PRIORITY:
3571 /* the HW seems to be happy with the 1st
3572 * descriptor filled and the 2nd empty...
3573 * So always update descriptor 1 and never
3574 * touch 2nd
3576 // priv->txbeaconringtail=tail;
3577 // priv->txbeaconbufstail=buflist;
3579 break;
3583 //rtl8180_dma_kick(dev,priority);
3585 *temp_tail = *temp_tail | (1<<31); // descriptor ready to be txed
3586 rtl8180_dma_kick(dev,priority);
3587 //spin_unlock_irqrestore(&priv->tx_lock,flags);
3589 return 0;
3594 void rtl8180_irq_rx_tasklet(struct r8180_priv * priv);
3597 void rtl8180_link_change(struct net_device *dev)
3599 struct r8180_priv *priv = ieee80211_priv(dev);
3600 u16 beacon_interval;
3602 struct ieee80211_network *net = &priv->ieee80211->current_network;
3603 // rtl8180_adapter_start(dev);
3604 rtl8180_update_msr(dev);
3607 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
3609 write_nic_dword(dev,BSSID,((u32*)net->bssid)[0]);
3610 write_nic_word(dev,BSSID+4,((u16*)net->bssid)[2]);
3613 beacon_interval = read_nic_dword(dev,BEACON_INTERVAL);
3614 beacon_interval &= ~ BEACON_INTERVAL_MASK;
3615 beacon_interval |= net->beacon_interval;
3616 write_nic_dword(dev, BEACON_INTERVAL, beacon_interval);
3618 rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
3622 u16 atim = read_nic_dword(dev,ATIM);
3623 u16 = u16 &~ ATIM_MASK;
3624 u16 = u16 | beacon->atim;
3626 #if 0
3627 if (net->capability & WLAN_CAPABILITY_PRIVACY) {
3628 if (priv->hw_wep) {
3629 DMESG("Enabling hardware WEP support");
3630 rtl8180_set_hw_wep(dev);
3631 priv->ieee80211->host_encrypt=0;
3632 priv->ieee80211->host_decrypt=0;
3634 #ifndef CONFIG_IEEE80211_NOWEP
3635 else {
3636 priv->ieee80211->host_encrypt=1;
3637 priv->ieee80211->host_decrypt=1;
3639 #endif
3641 #ifndef CONFIG_IEEE80211_NOWEP
3642 else{
3643 priv->ieee80211->host_encrypt=0;
3644 priv->ieee80211->host_decrypt=0;
3646 #endif
3647 #endif
3650 if(priv->card_8185)
3651 rtl8180_set_chan(dev, priv->chan);
3656 void rtl8180_rq_tx_ack(struct net_device *dev){
3658 struct r8180_priv *priv = ieee80211_priv(dev);
3659 // printk("====================>%s\n",__func__);
3660 write_nic_byte(dev,CONFIG4,read_nic_byte(dev,CONFIG4)|CONFIG4_PWRMGT);
3661 priv->ack_tx_to_ieee = 1;
3664 short rtl8180_is_tx_queue_empty(struct net_device *dev){
3666 struct r8180_priv *priv = ieee80211_priv(dev);
3667 u32* d;
3669 for (d = priv->txmapring;
3670 d < priv->txmapring + priv->txringcount;d+=8)
3671 if(*d & (1<<31)) return 0;
3673 for (d = priv->txbkpring;
3674 d < priv->txbkpring + priv->txringcount;d+=8)
3675 if(*d & (1<<31)) return 0;
3677 for (d = priv->txbepring;
3678 d < priv->txbepring + priv->txringcount;d+=8)
3679 if(*d & (1<<31)) return 0;
3681 for (d = priv->txvipring;
3682 d < priv->txvipring + priv->txringcount;d+=8)
3683 if(*d & (1<<31)) return 0;
3685 for (d = priv->txvopring;
3686 d < priv->txvopring + priv->txringcount;d+=8)
3687 if(*d & (1<<31)) return 0;
3689 for (d = priv->txhpring;
3690 d < priv->txhpring + priv->txringcount;d+=8)
3691 if(*d & (1<<31)) return 0;
3692 return 1;
3694 /* FIXME FIXME 5msecs is random */
3695 #define HW_WAKE_DELAY 5
3697 void rtl8180_hw_wakeup(struct net_device *dev)
3699 unsigned long flags;
3701 struct r8180_priv *priv = ieee80211_priv(dev);
3703 spin_lock_irqsave(&priv->ps_lock,flags);
3704 //DMESG("Waken up!");
3705 write_nic_byte(dev,CONFIG4,read_nic_byte(dev,CONFIG4)&~CONFIG4_PWRMGT);
3707 if(priv->rf_wakeup)
3708 priv->rf_wakeup(dev);
3709 // mdelay(HW_WAKE_DELAY);
3710 spin_unlock_irqrestore(&priv->ps_lock,flags);
3713 void rtl8180_hw_sleep_down(struct net_device *dev)
3715 unsigned long flags;
3717 struct r8180_priv *priv = ieee80211_priv(dev);
3719 spin_lock_irqsave(&priv->ps_lock,flags);
3720 //DMESG("Sleep!");
3722 if(priv->rf_sleep)
3723 priv->rf_sleep(dev);
3724 spin_unlock_irqrestore(&priv->ps_lock,flags);
3728 void rtl8180_hw_sleep(struct net_device *dev, u32 th, u32 tl)
3731 struct r8180_priv *priv = ieee80211_priv(dev);
3733 u32 rb = jiffies;
3734 unsigned long flags;
3736 spin_lock_irqsave(&priv->ps_lock,flags);
3738 /* Writing HW register with 0 equals to disable
3739 * the timer, that is not really what we want
3741 tl -= MSECS(4+16+7);
3743 //if(tl == 0) tl = 1;
3745 /* FIXME HACK FIXME HACK */
3746 // force_pci_posting(dev);
3747 //mdelay(1);
3749 // rb = read_nic_dword(dev, TSFTR);
3751 /* If the interval in witch we are requested to sleep is too
3752 * short then give up and remain awake
3754 if(((tl>=rb)&& (tl-rb) <= MSECS(MIN_SLEEP_TIME))
3755 ||((rb>tl)&& (rb-tl) < MSECS(MIN_SLEEP_TIME))) {
3756 spin_unlock_irqrestore(&priv->ps_lock,flags);
3757 printk("too short to sleep\n");
3758 return;
3761 // write_nic_dword(dev, TimerInt, tl);
3762 // rb = read_nic_dword(dev, TSFTR);
3764 u32 tmp = (tl>rb)?(tl-rb):(rb-tl);
3765 // if (tl<rb)
3767 //lzm,add,080828
3768 priv->DozePeriodInPast2Sec += jiffies_to_msecs(tmp);
3770 queue_delayed_work(priv->ieee80211->wq, &priv->ieee80211->hw_wakeup_wq, tmp); //as tl may be less than rb
3772 /* if we suspect the TimerInt is gone beyond tl
3773 * while setting it, then give up
3775 #if 1
3776 if(((tl > rb) && ((tl-rb) > MSECS(MAX_SLEEP_TIME)))||
3777 ((tl < rb) && ((rb-tl) > MSECS(MAX_SLEEP_TIME)))) {
3778 spin_unlock_irqrestore(&priv->ps_lock,flags);
3779 return;
3781 #endif
3782 // if(priv->rf_sleep)
3783 // priv->rf_sleep(dev);
3785 queue_work(priv->ieee80211->wq, (void *)&priv->ieee80211->hw_sleep_wq);
3786 spin_unlock_irqrestore(&priv->ps_lock,flags);
3790 //void rtl8180_wmm_param_update(struct net_device *dev,u8 *ac_param)
3791 #if LINUX_VERSION_CODE >=KERNEL_VERSION(2,6,20)
3792 void rtl8180_wmm_param_update(struct work_struct * work)
3794 struct ieee80211_device * ieee = container_of(work, struct ieee80211_device,wmm_param_update_wq);
3795 //struct r8180_priv *priv = (struct r8180_priv*)(ieee->priv);
3796 struct net_device *dev = ieee->dev;
3797 #else
3798 void rtl8180_wmm_param_update(struct ieee80211_device *ieee)
3800 struct net_device *dev = ieee->dev;
3801 struct r8180_priv *priv = ieee80211_priv(dev);
3802 #endif
3803 u8 *ac_param = (u8 *)(ieee->current_network.wmm_param);
3804 u8 mode = ieee->current_network.mode;
3805 AC_CODING eACI;
3806 AC_PARAM AcParam;
3807 PAC_PARAM pAcParam;
3808 u8 i;
3810 #ifndef CONFIG_RTL8185B
3811 //for legacy 8185 keep the PARAM unchange.
3812 return;
3813 #else
3814 if(!ieee->current_network.QoS_Enable){
3815 //legacy ac_xx_param update
3816 AcParam.longData = 0;
3817 AcParam.f.AciAifsn.f.AIFSN = 2; // Follow 802.11 DIFS.
3818 AcParam.f.AciAifsn.f.ACM = 0;
3819 AcParam.f.Ecw.f.ECWmin = 3; // Follow 802.11 CWmin.
3820 AcParam.f.Ecw.f.ECWmax = 7; // Follow 802.11 CWmax.
3821 AcParam.f.TXOPLimit = 0;
3822 for(eACI = 0; eACI < AC_MAX; eACI++){
3823 AcParam.f.AciAifsn.f.ACI = (u8)eACI;
3825 u8 u1bAIFS;
3826 u32 u4bAcParam;
3827 pAcParam = (PAC_PARAM)(&AcParam);
3828 // Retrive paramters to udpate.
3829 u1bAIFS = pAcParam->f.AciAifsn.f.AIFSN *(((mode&IEEE_G) == IEEE_G)?9:20) + aSifsTime;
3830 u4bAcParam = ((((u32)(pAcParam->f.TXOPLimit))<<AC_PARAM_TXOP_LIMIT_OFFSET)|
3831 (((u32)(pAcParam->f.Ecw.f.ECWmax))<<AC_PARAM_ECW_MAX_OFFSET)|
3832 (((u32)(pAcParam->f.Ecw.f.ECWmin))<<AC_PARAM_ECW_MIN_OFFSET)|
3833 (((u32)u1bAIFS) << AC_PARAM_AIFS_OFFSET));
3834 switch(eACI){
3835 case AC1_BK:
3836 write_nic_dword(dev, AC_BK_PARAM, u4bAcParam);
3837 break;
3839 case AC0_BE:
3840 write_nic_dword(dev, AC_BE_PARAM, u4bAcParam);
3841 break;
3843 case AC2_VI:
3844 write_nic_dword(dev, AC_VI_PARAM, u4bAcParam);
3845 break;
3847 case AC3_VO:
3848 write_nic_dword(dev, AC_VO_PARAM, u4bAcParam);
3849 break;
3851 default:
3852 printk(KERN_WARNING "SetHwReg8185():invalid ACI: %d!\n", eACI);
3853 break;
3857 return;
3860 for(i = 0; i < AC_MAX; i++){
3861 //AcParam.longData = 0;
3862 pAcParam = (AC_PARAM * )ac_param;
3864 AC_CODING eACI;
3865 u8 u1bAIFS;
3866 u32 u4bAcParam;
3868 // Retrive paramters to udpate.
3869 eACI = pAcParam->f.AciAifsn.f.ACI;
3870 //Mode G/A: slotTimeTimer = 9; Mode B: 20
3871 u1bAIFS = pAcParam->f.AciAifsn.f.AIFSN * (((mode&IEEE_G) == IEEE_G)?9:20) + aSifsTime;
3872 u4bAcParam = ( (((u32)(pAcParam->f.TXOPLimit)) << AC_PARAM_TXOP_LIMIT_OFFSET) |
3873 (((u32)(pAcParam->f.Ecw.f.ECWmax)) << AC_PARAM_ECW_MAX_OFFSET) |
3874 (((u32)(pAcParam->f.Ecw.f.ECWmin)) << AC_PARAM_ECW_MIN_OFFSET) |
3875 (((u32)u1bAIFS) << AC_PARAM_AIFS_OFFSET));
3877 switch(eACI){
3878 case AC1_BK:
3879 write_nic_dword(dev, AC_BK_PARAM, u4bAcParam);
3880 break;
3882 case AC0_BE:
3883 write_nic_dword(dev, AC_BE_PARAM, u4bAcParam);
3884 break;
3886 case AC2_VI:
3887 write_nic_dword(dev, AC_VI_PARAM, u4bAcParam);
3888 break;
3890 case AC3_VO:
3891 write_nic_dword(dev, AC_VO_PARAM, u4bAcParam);
3892 break;
3894 default:
3895 printk(KERN_WARNING "SetHwReg8185(): invalid ACI: %d !\n", eACI);
3896 break;
3899 ac_param += (sizeof(AC_PARAM));
3901 #endif
3904 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
3905 void rtl8180_tx_irq_wq(struct work_struct *work);
3906 #else
3907 void rtl8180_tx_irq_wq(struct net_device *dev);
3908 #endif
3913 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
3914 void rtl8180_restart_wq(struct work_struct *work);
3915 //void rtl8180_rq_tx_ack(struct work_struct *work);
3916 #else
3917 void rtl8180_restart_wq(struct net_device *dev);
3918 //void rtl8180_rq_tx_ack(struct net_device *dev);
3919 #endif
3920 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
3921 void rtl8180_watch_dog_wq(struct work_struct *work);
3922 #else
3923 void rtl8180_watch_dog_wq(struct net_device *dev);
3924 #endif
3925 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
3926 void rtl8180_hw_wakeup_wq(struct work_struct *work);
3927 #else
3928 void rtl8180_hw_wakeup_wq(struct net_device *dev);
3929 #endif
3931 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
3932 void rtl8180_hw_sleep_wq(struct work_struct *work);
3933 #else
3934 void rtl8180_hw_sleep_wq(struct net_device *dev);
3935 #endif
3939 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
3940 void rtl8180_sw_antenna_wq(struct work_struct *work);
3941 #else
3942 void rtl8180_sw_antenna_wq(struct net_device *dev);
3943 #endif
3944 void rtl8180_watch_dog(struct net_device *dev);
3945 void watch_dog_adaptive(unsigned long data)
3947 struct r8180_priv* priv = ieee80211_priv((struct net_device *)data);
3948 // DMESG("---->watch_dog_adaptive()\n");
3949 if(!priv->up)
3951 DMESG("<----watch_dog_adaptive():driver is not up!\n");
3952 return;
3955 // queue_work(priv->ieee80211->wq,&priv->ieee80211->watch_dog_wq);
3956 //{by amy 080312
3957 #if 1
3958 // Tx High Power Mechanism.
3959 #ifdef HIGH_POWER
3960 if(CheckHighPower((struct net_device *)data))
3962 queue_work(priv->ieee80211->wq, (void *)&priv->ieee80211->tx_pw_wq);
3964 #endif
3966 #ifdef CONFIG_RTL818X_S
3967 // Tx Power Tracking on 87SE.
3968 #ifdef TX_TRACK
3969 //if( priv->bTxPowerTrack ) //lzm mod 080826
3970 if( CheckTxPwrTracking((struct net_device *)data));
3971 TxPwrTracking87SE((struct net_device *)data);
3972 #endif
3973 #endif
3975 // Perform DIG immediately.
3976 #ifdef SW_DIG
3977 if(CheckDig((struct net_device *)data) == true)
3979 queue_work(priv->ieee80211->wq, (void *)&priv->ieee80211->hw_dig_wq);
3981 #endif
3982 #endif
3983 //by amy 080312}
3984 rtl8180_watch_dog((struct net_device *)data);
3987 queue_work(priv->ieee80211->wq, (void *)&priv->ieee80211->GPIOChangeRFWorkItem);
3989 priv->watch_dog_timer.expires = jiffies + MSECS(IEEE80211_WATCH_DOG_TIME);
3990 add_timer(&priv->watch_dog_timer);
3991 // DMESG("<----watch_dog_adaptive()\n");
3994 #ifdef ENABLE_DOT11D
3996 static CHANNEL_LIST ChannelPlan[] = {
3997 {{1,2,3,4,5,6,7,8,9,10,11,36,40,44,48,52,56,60,64},19}, //FCC
3998 {{1,2,3,4,5,6,7,8,9,10,11},11}, //IC
3999 {{1,2,3,4,5,6,7,8,9,10,11,12,13,36,40,44,48,52,56,60,64},21}, //ETSI
4000 {{1,2,3,4,5,6,7,8,9,10,11,12,13,36,40,44,48,52,56,60,64},21}, //Spain. Change to ETSI.
4001 {{1,2,3,4,5,6,7,8,9,10,11,12,13,36,40,44,48,52,56,60,64},21}, //France. Change to ETSI.
4002 {{14,36,40,44,48,52,56,60,64},9}, //MKK
4003 {{1,2,3,4,5,6,7,8,9,10,11,12,13,14, 36,40,44,48,52,56,60,64},22},//MKK1
4004 {{1,2,3,4,5,6,7,8,9,10,11,12,13,36,40,44,48,52,56,60,64},21}, //Israel.
4005 {{1,2,3,4,5,6,7,8,9,10,11,12,13,34,38,42,46},17}, // For 11a , TELEC
4006 {{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
4007 {{1,2,3,4,5,6,7,8,9,10,11,12,13},13} //world wide 13: ch1~ch11 active scan, ch12~13 passive //lzm add 080826
4010 static void rtl8180_set_channel_map(u8 channel_plan, struct ieee80211_device *ieee)
4012 int i;
4014 //lzm add 080826
4015 ieee->MinPassiveChnlNum=MAX_CHANNEL_NUMBER+1;
4016 ieee->IbssStartChnl=0;
4018 switch (channel_plan)
4020 case COUNTRY_CODE_FCC:
4021 case COUNTRY_CODE_IC:
4022 case COUNTRY_CODE_ETSI:
4023 case COUNTRY_CODE_SPAIN:
4024 case COUNTRY_CODE_FRANCE:
4025 case COUNTRY_CODE_MKK:
4026 case COUNTRY_CODE_MKK1:
4027 case COUNTRY_CODE_ISRAEL:
4028 case COUNTRY_CODE_TELEC:
4030 Dot11d_Init(ieee);
4031 ieee->bGlobalDomain = false;
4032 if (ChannelPlan[channel_plan].Len != 0){
4033 // Clear old channel map
4034 memset(GET_DOT11D_INFO(ieee)->channel_map, 0, sizeof(GET_DOT11D_INFO(ieee)->channel_map));
4035 // Set new channel map
4036 for (i=0;i<ChannelPlan[channel_plan].Len;i++)
4038 if(ChannelPlan[channel_plan].Channel[i] <= 14)
4039 GET_DOT11D_INFO(ieee)->channel_map[ChannelPlan[channel_plan].Channel[i]] = 1;
4042 break;
4044 case COUNTRY_CODE_GLOBAL_DOMAIN:
4046 GET_DOT11D_INFO(ieee)->bEnabled = 0;
4047 Dot11d_Reset(ieee);
4048 ieee->bGlobalDomain = true;
4049 break;
4051 case COUNTRY_CODE_WORLD_WIDE_13_INDEX://lzm add 080826
4053 ieee->MinPassiveChnlNum=12;
4054 ieee->IbssStartChnl= 10;
4055 break;
4057 default:
4059 Dot11d_Init(ieee);
4060 ieee->bGlobalDomain = false;
4061 memset(GET_DOT11D_INFO(ieee)->channel_map, 0, sizeof(GET_DOT11D_INFO(ieee)->channel_map));
4062 for (i=1;i<=14;i++)
4064 GET_DOT11D_INFO(ieee)->channel_map[i] = 1;
4066 break;
4070 #endif
4072 //Add for RF power on power off by lizhaoming 080512
4073 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
4074 void GPIOChangeRFWorkItemCallBack(struct work_struct *work);
4075 #else
4076 void GPIOChangeRFWorkItemCallBack(struct ieee80211_device *ieee);
4077 #endif
4079 //YJ,add,080828
4080 static void rtl8180_statistics_init(struct Stats *pstats)
4082 memset(pstats, 0, sizeof(struct Stats));
4084 static void rtl8180_link_detect_init(plink_detect_t plink_detect)
4086 memset(plink_detect, 0, sizeof(link_detect_t));
4087 plink_detect->SlotNum = DEFAULT_SLOT_NUM;
4089 //YJ,add,080828,end
4091 short rtl8180_init(struct net_device *dev)
4093 struct r8180_priv *priv = ieee80211_priv(dev);
4094 u16 word;
4095 u16 version;
4096 u8 hw_version;
4097 //u8 config3;
4098 u32 usValue;
4099 u16 tmpu16;
4100 int i, j;
4102 #ifdef ENABLE_DOT11D
4103 #if 0
4104 for(i=0;i<0xFF;i++) {
4105 if(i%16 == 0)
4106 printk("\n[%x]: ", i/16);
4107 printk("\t%4.4x", eprom_read(dev,i));
4109 #endif
4110 priv->channel_plan = eprom_read(dev, EEPROM_COUNTRY_CODE>>1) & 0xFF;
4111 if(priv->channel_plan > COUNTRY_CODE_GLOBAL_DOMAIN){
4112 printk("rtl8180_init:Error channel plan! Set to default.\n");
4113 priv->channel_plan = 0;
4115 //priv->channel_plan = 9; //Global Domain
4117 DMESG("Channel plan is %d\n",priv->channel_plan);
4118 rtl8180_set_channel_map(priv->channel_plan, priv->ieee80211);
4119 #else
4120 int ch;
4121 //Set Default Channel Plan
4122 if(!channels){
4123 DMESG("No channels, aborting");
4124 return -1;
4126 ch=channels;
4127 priv->channel_plan = 0;//hikaru
4128 // set channels 1..14 allowed in given locale
4129 for (i=1; i<=14; i++) {
4130 (priv->ieee80211->channel_map)[i] = (u8)(ch & 0x01);
4131 ch >>= 1;
4133 #endif
4135 //memcpy(priv->stats,0,sizeof(struct Stats));
4137 //FIXME: these constants are placed in a bad pleace.
4138 priv->txbuffsize = 2048;//1024;
4139 priv->txringcount = 32;//32;
4140 priv->rxbuffersize = 2048;//1024;
4141 priv->rxringcount = 64;//32;
4142 priv->txbeaconcount = 2;
4143 priv->rx_skb_complete = 1;
4144 //priv->txnp_pending.ispending=0;
4145 /* ^^ the SKB does not containt a partial RXed
4146 * packet (is empty)
4149 #ifdef CONFIG_RTL8185B
4150 #ifdef CONFIG_RTL818X_S
4151 priv->RegThreeWireMode = HW_THREE_WIRE_SI;
4152 #else
4153 priv->RegThreeWireMode = SW_THREE_WIRE;
4154 #endif
4155 #endif
4157 //Add for RF power on power off by lizhaoming 080512
4158 priv->RFChangeInProgress = false;
4159 priv->SetRFPowerStateInProgress = false;
4160 priv->RFProgType = 0;
4161 priv->bInHctTest = false;
4163 priv->irq_enabled=0;
4165 //YJ,modified,080828
4166 #if 0
4167 priv->stats.rxdmafail=0;
4168 priv->stats.txrdu=0;
4169 priv->stats.rxrdu=0;
4170 priv->stats.rxnolast=0;
4171 priv->stats.rxnodata=0;
4172 //priv->stats.rxreset=0;
4173 //priv->stats.rxwrkaround=0;
4174 priv->stats.rxnopointer=0;
4175 priv->stats.txnperr=0;
4176 priv->stats.txresumed=0;
4177 priv->stats.rxerr=0;
4178 priv->stats.rxoverflow=0;
4179 priv->stats.rxint=0;
4180 priv->stats.txnpokint=0;
4181 priv->stats.txhpokint=0;
4182 priv->stats.txhperr=0;
4183 priv->stats.ints=0;
4184 priv->stats.shints=0;
4185 priv->stats.txoverflow=0;
4186 priv->stats.txbeacon=0;
4187 priv->stats.txbeaconerr=0;
4188 priv->stats.txlperr=0;
4189 priv->stats.txlpokint=0;
4190 priv->stats.txretry=0;//tony 20060601
4191 priv->stats.rxcrcerrmin=0;
4192 priv->stats.rxcrcerrmid=0;
4193 priv->stats.rxcrcerrmax=0;
4194 priv->stats.rxicverr=0;
4195 #else
4196 rtl8180_statistics_init(&priv->stats);
4197 rtl8180_link_detect_init(&priv->link_detect);
4198 #endif
4199 //YJ,modified,080828,end
4202 priv->ack_tx_to_ieee = 0;
4203 priv->ieee80211->current_network.beacon_interval = DEFAULT_BEACONINTERVAL;
4204 priv->ieee80211->iw_mode = IW_MODE_INFRA;
4205 priv->ieee80211->softmac_features = IEEE_SOFTMAC_SCAN |
4206 IEEE_SOFTMAC_ASSOCIATE | IEEE_SOFTMAC_PROBERQ |
4207 IEEE_SOFTMAC_PROBERS | IEEE_SOFTMAC_TX_QUEUE;
4208 priv->ieee80211->active_scan = 1;
4209 priv->ieee80211->rate = 110; //11 mbps
4210 priv->ieee80211->modulation = IEEE80211_CCK_MODULATION;
4211 priv->ieee80211->host_encrypt = 1;
4212 priv->ieee80211->host_decrypt = 1;
4213 priv->ieee80211->sta_wake_up = rtl8180_hw_wakeup;
4214 priv->ieee80211->ps_request_tx_ack = rtl8180_rq_tx_ack;
4215 priv->ieee80211->enter_sleep_state = rtl8180_hw_sleep;
4216 priv->ieee80211->ps_is_queue_empty = rtl8180_is_tx_queue_empty;
4218 priv->hw_wep = hwwep;
4219 priv->prism_hdr=0;
4220 priv->dev=dev;
4221 priv->retry_rts = DEFAULT_RETRY_RTS;
4222 priv->retry_data = DEFAULT_RETRY_DATA;
4223 priv->RFChangeInProgress = false;
4224 priv->SetRFPowerStateInProgress = false;
4225 priv->RFProgType = 0;
4226 priv->bInHctTest = false;
4227 priv->bInactivePs = true;//false;
4228 priv->ieee80211->bInactivePs = priv->bInactivePs;
4229 priv->bSwRfProcessing = false;
4230 priv->eRFPowerState = eRfOff;
4231 priv->RfOffReason = 0;
4232 priv->LedStrategy = SW_LED_MODE0;
4233 //priv->NumRxOkInPeriod = 0; //YJ,del,080828
4234 //priv->NumTxOkInPeriod = 0; //YJ,del,080828
4235 priv->TxPollingTimes = 0;//lzm add 080826
4236 priv->bLeisurePs = true;
4237 priv->dot11PowerSaveMode = eActive;
4238 //by amy for antenna
4239 priv->AdMinCheckPeriod = 5;
4240 priv->AdMaxCheckPeriod = 10;
4241 // Lower signal strength threshold to fit the HW participation in antenna diversity. +by amy 080312
4242 priv->AdMaxRxSsThreshold = 30;//60->30
4243 priv->AdRxSsThreshold = 20;//50->20
4244 priv->AdCheckPeriod = priv->AdMinCheckPeriod;
4245 priv->AdTickCount = 0;
4246 priv->AdRxSignalStrength = -1;
4247 priv->RegSwAntennaDiversityMechanism = 0;
4248 priv->RegDefaultAntenna = 0;
4249 priv->SignalStrength = 0;
4250 priv->AdRxOkCnt = 0;
4251 priv->CurrAntennaIndex = 0;
4252 priv->AdRxSsBeforeSwitched = 0;
4253 init_timer(&priv->SwAntennaDiversityTimer);
4254 priv->SwAntennaDiversityTimer.data = (unsigned long)dev;
4255 priv->SwAntennaDiversityTimer.function = (void *)SwAntennaDiversityTimerCallback;
4256 //by amy for antenna
4257 //{by amy 080312
4258 priv->bDigMechanism = 1;
4259 priv->InitialGain = 6;
4260 priv->bXtalCalibration = false;
4261 priv->XtalCal_Xin = 0;
4262 priv->XtalCal_Xout = 0;
4263 priv->bTxPowerTrack = false;
4264 priv->ThermalMeter = 0;
4265 priv->FalseAlarmRegValue = 0;
4266 priv->RegDigOfdmFaUpTh = 0xc; // Upper threhold of OFDM false alarm, which is used in DIG.
4267 priv->DIG_NumberFallbackVote = 0;
4268 priv->DIG_NumberUpgradeVote = 0;
4269 priv->LastSignalStrengthInPercent = 0;
4270 priv->Stats_SignalStrength = 0;
4271 priv->LastRxPktAntenna = 0;
4272 priv->SignalQuality = 0; // in 0-100 index.
4273 priv->Stats_SignalQuality = 0;
4274 priv->RecvSignalPower = 0; // in dBm.
4275 priv->Stats_RecvSignalPower = 0;
4276 priv->AdMainAntennaRxOkCnt = 0;
4277 priv->AdAuxAntennaRxOkCnt = 0;
4278 priv->bHWAdSwitched = false;
4279 priv->bRegHighPowerMechanism = true;
4280 priv->RegHiPwrUpperTh = 77;
4281 priv->RegHiPwrLowerTh = 75;
4282 priv->RegRSSIHiPwrUpperTh = 70;
4283 priv->RegRSSIHiPwrLowerTh = 20;
4284 priv->bCurCCKPkt = false;
4285 priv->UndecoratedSmoothedSS = -1;
4286 priv->bToUpdateTxPwr = false;
4287 priv->CurCCKRSSI = 0;
4288 priv->RxPower = 0;
4289 priv->RSSI = 0;
4290 //YJ,add,080828
4291 priv->NumTxOkTotal = 0;
4292 priv->NumTxUnicast = 0;
4293 priv->keepAliveLevel = DEFAULT_KEEP_ALIVE_LEVEL;
4294 priv->PowerProfile = POWER_PROFILE_AC;
4295 //YJ,add,080828,end
4296 //by amy for rate adaptive
4297 priv->CurrRetryCnt=0;
4298 priv->LastRetryCnt=0;
4299 priv->LastTxokCnt=0;
4300 priv->LastRxokCnt=0;
4301 priv->LastRetryRate=0;
4302 priv->bTryuping=0;
4303 priv->CurrTxRate=0;
4304 priv->CurrRetryRate=0;
4305 priv->TryupingCount=0;
4306 priv->TryupingCountNoData=0;
4307 priv->TryDownCountLowData=0;
4308 priv->LastTxOKBytes=0;
4309 priv->LastFailTxRate=0;
4310 priv->LastFailTxRateSS=0;
4311 priv->FailTxRateCount=0;
4312 priv->LastTxThroughput=0;
4313 priv->NumTxOkBytesTotal=0;
4314 priv->ForcedDataRate = 0;
4315 priv->RegBModeGainStage = 1;
4317 //by amy for rate adaptive
4318 //by amy 080312}
4319 priv->promisc = (dev->flags & IFF_PROMISC) ? 1:0;
4320 spin_lock_init(&priv->irq_lock);
4321 spin_lock_init(&priv->irq_th_lock);
4322 spin_lock_init(&priv->tx_lock);
4323 spin_lock_init(&priv->ps_lock);
4324 spin_lock_init(&priv->rf_ps_lock);
4325 sema_init(&priv->wx_sem,1);
4326 sema_init(&priv->rf_state,1);
4327 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
4328 INIT_WORK(&priv->reset_wq,(void*) rtl8180_restart_wq);
4329 INIT_WORK(&priv->tx_irq_wq,(void*) rtl8180_tx_irq_wq);
4330 INIT_DELAYED_WORK(&priv->ieee80211->hw_wakeup_wq,(void*) rtl8180_hw_wakeup_wq);
4331 INIT_DELAYED_WORK(&priv->ieee80211->hw_sleep_wq,(void*) rtl8180_hw_sleep_wq);
4332 //INIT_DELAYED_WORK(&priv->ieee80211->watch_dog_wq,(void*) rtl8180_watch_dog_wq);
4333 //INIT_DELAYED_WORK(&priv->ieee80211->sw_antenna_wq,(void*) rtl8180_sw_antenna_wq);
4334 INIT_WORK(&priv->ieee80211->wmm_param_update_wq,(void*) rtl8180_wmm_param_update);
4335 INIT_DELAYED_WORK(&priv->ieee80211->rate_adapter_wq,(void*)rtl8180_rate_adapter);//+by amy 080312
4336 INIT_DELAYED_WORK(&priv->ieee80211->hw_dig_wq,(void*)rtl8180_hw_dig_wq);//+by amy 080312
4337 INIT_DELAYED_WORK(&priv->ieee80211->tx_pw_wq,(void*)rtl8180_tx_pw_wq);//+by amy 080312
4339 //add for RF power on power off by lizhaoming 080512
4340 INIT_DELAYED_WORK(&priv->ieee80211->GPIOChangeRFWorkItem,(void*) GPIOChangeRFWorkItemCallBack);
4341 #else
4342 INIT_WORK(&priv->reset_wq,(void*) rtl8180_restart_wq,dev);
4343 INIT_WORK(&priv->tx_irq_wq,(void*) rtl8180_tx_irq_wq,dev);
4344 //INIT_WORK(&priv->ieee80211->watch_dog_wq,(void*) rtl8180_watch_dog_wq,dev);
4345 INIT_WORK(&priv->ieee80211->hw_wakeup_wq,(void*) rtl8180_hw_wakeup_wq,dev);
4346 INIT_WORK(&priv->ieee80211->hw_sleep_wq,(void*) rtl8180_hw_sleep_wq,dev);
4347 //INIT_WORK(&priv->ieee80211->sw_antenna_wq,(void*) rtl8180_sw_antenna_wq,dev);
4348 INIT_WORK(&priv->ieee80211->wmm_param_update_wq,(void*) rtl8180_wmm_param_update,priv->ieee80211);
4349 INIT_WORK(&priv->ieee80211->rate_adapter_wq,(void*)rtl8180_rate_adapter,dev);//+by amy 080312
4350 INIT_WORK(&priv->ieee80211->hw_dig_wq,(void*)rtl8180_hw_dig_wq,dev);//+by amy 080312
4351 INIT_WORK(&priv->ieee80211->tx_pw_wq,(void*)rtl8180_tx_pw_wq,dev);//+by amy 080312
4353 //add for RF power on power off by lizhaoming 080512
4354 INIT_WORK(&priv->ieee80211->GPIOChangeRFWorkItem,(void*) GPIOChangeRFWorkItemCallBack, priv->ieee80211);
4355 #endif
4356 //INIT_WORK(&priv->reset_wq,(void*) rtl8180_restart_wq,dev);
4358 tasklet_init(&priv->irq_rx_tasklet,
4359 (void(*)(unsigned long)) rtl8180_irq_rx_tasklet,
4360 (unsigned long)priv);
4361 //by amy
4362 init_timer(&priv->watch_dog_timer);
4363 priv->watch_dog_timer.data = (unsigned long)dev;
4364 priv->watch_dog_timer.function = watch_dog_adaptive;
4365 //by amy
4367 //{by amy 080312
4368 //by amy for rate adaptive
4369 init_timer(&priv->rateadapter_timer);
4370 priv->rateadapter_timer.data = (unsigned long)dev;
4371 priv->rateadapter_timer.function = timer_rate_adaptive;
4372 priv->RateAdaptivePeriod= RATE_ADAPTIVE_TIMER_PERIOD;
4373 priv->bEnhanceTxPwr=false;
4374 //by amy for rate adaptive
4375 //by amy 080312}
4376 //priv->ieee80211->func =
4377 // kmalloc(sizeof(struct ieee80211_helper_functions),GFP_KERNEL);
4378 //memset(priv->ieee80211->func, 0,
4379 // sizeof(struct ieee80211_helper_functions));
4381 priv->ieee80211->softmac_hard_start_xmit = rtl8180_hard_start_xmit;
4382 priv->ieee80211->set_chan = rtl8180_set_chan;
4383 priv->ieee80211->link_change = rtl8180_link_change;
4384 priv->ieee80211->softmac_data_hard_start_xmit = rtl8180_hard_data_xmit;
4385 priv->ieee80211->data_hard_stop = rtl8180_data_hard_stop;
4386 priv->ieee80211->data_hard_resume = rtl8180_data_hard_resume;
4388 priv->ieee80211->init_wmmparam_flag = 0;
4390 priv->ieee80211->start_send_beacons = rtl8180_start_tx_beacon;
4391 priv->ieee80211->stop_send_beacons = rtl8180_beacon_tx_disable;
4392 priv->ieee80211->fts = DEFAULT_FRAG_THRESHOLD;
4394 #ifdef CONFIG_RTL8185B
4395 priv->MWIEnable = 0;
4397 priv->ShortRetryLimit = 7;
4398 priv->LongRetryLimit = 7;
4399 priv->EarlyRxThreshold = 7;
4401 priv->CSMethod = (0x01 << 29);
4403 priv->TransmitConfig =
4404 1<<TCR_DurProcMode_OFFSET | //for RTL8185B, duration setting by HW
4405 (7<<TCR_MXDMA_OFFSET) | // Max DMA Burst Size per Tx DMA Burst, 7: reservied.
4406 (priv->ShortRetryLimit<<TCR_SRL_OFFSET) | // Short retry limit
4407 (priv->LongRetryLimit<<TCR_LRL_OFFSET) | // Long retry limit
4408 (0 ? TCR_SAT : 0); // FALSE: HW provies PLCP length and LENGEXT, TURE: SW proiveds them
4410 priv->ReceiveConfig =
4411 #ifdef CONFIG_RTL818X_S
4412 #else
4413 priv->CSMethod |
4414 #endif
4415 // RCR_ENMARP |
4416 RCR_AMF | RCR_ADF | //accept management/data
4417 RCR_ACF | //accept control frame for SW AP needs PS-poll, 2005.07.07, by rcnjko.
4418 RCR_AB | RCR_AM | RCR_APM | //accept BC/MC/UC
4419 //RCR_AICV | RCR_ACRC32 | //accept ICV/CRC error packet
4420 (7<<RCR_MXDMA_OFFSET) | // Max DMA Burst Size per Rx DMA Burst, 7: unlimited.
4421 (priv->EarlyRxThreshold<<RCR_FIFO_OFFSET) | // Rx FIFO Threshold, 7: No Rx threshold.
4422 (priv->EarlyRxThreshold == 7 ? RCR_ONLYERLPKT:0);
4424 priv->IntrMask = IMR_TMGDOK | IMR_TBDER | IMR_THPDER |
4425 IMR_THPDER | IMR_THPDOK |
4426 IMR_TVODER | IMR_TVODOK |
4427 IMR_TVIDER | IMR_TVIDOK |
4428 IMR_TBEDER | IMR_TBEDOK |
4429 IMR_TBKDER | IMR_TBKDOK |
4430 IMR_RDU | // To handle the defragmentation not enough Rx descriptors case. Annie, 2006-03-27.
4431 IMR_RER | IMR_ROK |
4432 IMR_RQoSOK; // <NOTE> ROK and RQoSOK are mutually exclusive, so, we must handle RQoSOK interrupt to receive QoS frames, 2005.12.09, by rcnjko.
4434 priv->InitialGain = 6;
4435 #endif
4437 hw_version =( read_nic_dword(dev, TCR) & TCR_HWVERID_MASK)>>TCR_HWVERID_SHIFT;
4439 switch (hw_version){
4440 #ifdef CONFIG_RTL8185B
4441 case HW_VERID_R8185B_B:
4442 #ifdef CONFIG_RTL818X_S
4443 priv->card_8185 = VERSION_8187S_C;
4444 DMESG("MAC controller is a RTL8187SE b/g");
4445 priv->phy_ver = 2;
4446 break;
4447 #else
4448 DMESG("MAC controller is a RTL8185B b/g");
4449 priv->card_8185 = 3;
4450 priv->phy_ver = 2;
4451 break;
4452 #endif
4453 #endif
4454 case HW_VERID_R8185_ABC:
4455 DMESG("MAC controller is a RTL8185 b/g");
4456 priv->card_8185 = 1;
4457 /* you should not find a card with 8225 PHY ver < C*/
4458 priv->phy_ver = 2;
4459 break;
4461 case HW_VERID_R8185_D:
4462 DMESG("MAC controller is a RTL8185 b/g (V. D)");
4463 priv->card_8185 = 2;
4464 /* you should not find a card with 8225 PHY ver < C*/
4465 priv->phy_ver = 2;
4466 break;
4468 case HW_VERID_R8180_ABCD:
4469 DMESG("MAC controller is a RTL8180");
4470 priv->card_8185 = 0;
4471 break;
4473 case HW_VERID_R8180_F:
4474 DMESG("MAC controller is a RTL8180 (v. F)");
4475 priv->card_8185 = 0;
4476 break;
4478 default:
4479 DMESGW("MAC chip not recognized: version %x. Assuming RTL8180",hw_version);
4480 priv->card_8185 = 0;
4481 break;
4484 if(priv->card_8185){
4485 priv->ieee80211->modulation |= IEEE80211_OFDM_MODULATION;
4486 priv->ieee80211->short_slot = 1;
4488 /* you should not found any 8185 Ver B Card */
4489 priv->card_8185_Bversion = 0;
4491 #ifdef CONFIG_RTL8185B
4492 #ifdef CONFIG_RTL818X_S
4493 // just for sync 85
4494 priv->card_type = PCI;
4495 DMESG("This is a PCI NIC");
4496 #else
4497 config3 = read_nic_byte(dev, CONFIG3);
4498 if(config3 & 0x8){
4499 priv->card_type = CARDBUS;
4500 DMESG("This is a CARDBUS NIC");
4502 else if( config3 & 0x4){
4503 priv->card_type = MINIPCI;
4504 DMESG("This is a MINI-PCI NIC");
4505 }else{
4506 priv->card_type = PCI;
4507 DMESG("This is a PCI NIC");
4509 #endif
4510 #endif
4511 priv->enable_gpio0 = 0;
4513 //by amy for antenna
4514 #ifdef CONFIG_RTL8185B
4515 usValue = eprom_read(dev, EEPROM_SW_REVD_OFFSET);
4516 DMESG("usValue is 0x%x\n",usValue);
4517 #ifdef CONFIG_RTL818X_S
4518 //3Read AntennaDiversity
4519 // SW Antenna Diversity.
4520 if( (usValue & EEPROM_SW_AD_MASK) != EEPROM_SW_AD_ENABLE )
4522 priv->EEPROMSwAntennaDiversity = false;
4523 //printk("EEPROM Disable SW Antenna Diversity\n");
4525 else
4527 priv->EEPROMSwAntennaDiversity = true;
4528 //printk("EEPROM Enable SW Antenna Diversity\n");
4530 // Default Antenna to use.
4531 if( (usValue & EEPROM_DEF_ANT_MASK) != EEPROM_DEF_ANT_1 )
4533 priv->EEPROMDefaultAntenna1 = false;
4534 //printk("EEPROM Default Antenna 0\n");
4536 else
4538 priv->EEPROMDefaultAntenna1 = true;
4539 //printk("EEPROM Default Antenna 1\n");
4543 // Antenna diversity mechanism. Added by Roger, 2007.11.05.
4545 if( priv->RegSwAntennaDiversityMechanism == 0 ) // Auto
4546 {// 0: default from EEPROM.
4547 priv->bSwAntennaDiverity = priv->EEPROMSwAntennaDiversity;
4549 else
4550 {// 1:disable antenna diversity, 2: enable antenna diversity.
4551 priv->bSwAntennaDiverity = ((priv->RegSwAntennaDiversityMechanism == 1)? false : true);
4553 //printk("bSwAntennaDiverity = %d\n", priv->bSwAntennaDiverity);
4557 // Default antenna settings. Added by Roger, 2007.11.05.
4559 if( priv->RegDefaultAntenna == 0)
4560 {// 0: default from EEPROM.
4561 priv->bDefaultAntenna1 = priv->EEPROMDefaultAntenna1;
4563 else
4564 {// 1: main, 2: aux.
4565 priv->bDefaultAntenna1 = ((priv->RegDefaultAntenna== 2) ? true : false);
4567 //printk("bDefaultAntenna1 = %d\n", priv->bDefaultAntenna1);
4568 #endif
4569 #endif
4570 //by amy for antenna
4571 /* rtl8185 can calc plcp len in HW.*/
4572 priv->hw_plcp_len = 1;
4574 priv->plcp_preamble_mode = 2;
4575 /*the eeprom type is stored in RCR register bit #6 */
4576 if (RCR_9356SEL & read_nic_dword(dev, RCR)){
4577 priv->epromtype=EPROM_93c56;
4578 //DMESG("Reported EEPROM chip is a 93c56 (2Kbit)");
4579 }else{
4580 priv->epromtype=EPROM_93c46;
4581 //DMESG("Reported EEPROM chip is a 93c46 (1Kbit)");
4584 dev->get_stats = rtl8180_stats;
4586 dev->dev_addr[0]=eprom_read(dev,MAC_ADR) & 0xff;
4587 dev->dev_addr[1]=(eprom_read(dev,MAC_ADR) & 0xff00)>>8;
4588 dev->dev_addr[2]=eprom_read(dev,MAC_ADR+1) & 0xff;
4589 dev->dev_addr[3]=(eprom_read(dev,MAC_ADR+1) & 0xff00)>>8;
4590 dev->dev_addr[4]=eprom_read(dev,MAC_ADR+2) & 0xff;
4591 dev->dev_addr[5]=(eprom_read(dev,MAC_ADR+2) & 0xff00)>>8;
4592 //DMESG("Card MAC address is "MAC_FMT, MAC_ARG(dev->dev_addr));
4595 for(i=1,j=0; i<14; i+=2,j++){
4597 word = eprom_read(dev,EPROM_TXPW_CH1_2 + j);
4598 priv->chtxpwr[i]=word & 0xff;
4599 priv->chtxpwr[i+1]=(word & 0xff00)>>8;
4600 #ifdef DEBUG_EPROM
4601 DMESG("tx word %x:%x",j,word);
4602 DMESG("ch %d pwr %x",i,priv->chtxpwr[i]);
4603 DMESG("ch %d pwr %x",i+1,priv->chtxpwr[i+1]);
4604 #endif
4606 if(priv->card_8185){
4607 for(i=1,j=0; i<14; i+=2,j++){
4609 word = eprom_read(dev,EPROM_TXPW_OFDM_CH1_2 + j);
4610 priv->chtxpwr_ofdm[i]=word & 0xff;
4611 priv->chtxpwr_ofdm[i+1]=(word & 0xff00)>>8;
4612 #ifdef DEBUG_EPROM
4613 DMESG("ofdm tx word %x:%x",j,word);
4614 DMESG("ofdm ch %d pwr %x",i,priv->chtxpwr_ofdm[i]);
4615 DMESG("ofdm ch %d pwr %x",i+1,priv->chtxpwr_ofdm[i+1]);
4616 #endif
4619 //{by amy 080312
4620 //3Read crystal calibtration and thermal meter indication on 87SE.
4622 // By SD3 SY's request. Added by Roger, 2007.12.11.
4624 tmpu16 = eprom_read(dev, EEPROM_RSV>>1);
4626 //printk("ReadAdapterInfo8185(): EEPROM_RSV(%04x)\n", tmpu16);
4628 // Crystal calibration for Xin and Xout resp.
4629 priv->XtalCal_Xout = tmpu16 & EEPROM_XTAL_CAL_XOUT_MASK; // 0~7.5pF
4630 priv->XtalCal_Xin = (tmpu16 & EEPROM_XTAL_CAL_XIN_MASK)>>4; // 0~7.5pF
4631 if((tmpu16 & EEPROM_XTAL_CAL_ENABLE)>>12)
4632 priv->bXtalCalibration = true;
4634 // Thermal meter reference indication.
4635 priv->ThermalMeter = (u8)((tmpu16 & EEPROM_THERMAL_METER_MASK)>>8);
4636 if((tmpu16 & EEPROM_THERMAL_METER_ENABLE)>>13)
4637 priv->bTxPowerTrack = true;
4639 //by amy 080312}
4640 #ifdef CONFIG_RTL8185B
4641 word = eprom_read(dev,EPROM_TXPW_BASE);
4642 priv->cck_txpwr_base = word & 0xf;
4643 priv->ofdm_txpwr_base = (word>>4) & 0xf;
4644 #endif
4646 version = eprom_read(dev,EPROM_VERSION);
4647 DMESG("EEPROM version %x",version);
4648 if( (!priv->card_8185) && version < 0x0101){
4649 DMESG ("EEPROM version too old, assuming defaults");
4650 DMESG ("If you see this message *plase* send your \
4651 DMESG output to andreamrl@tiscali.it THANKS");
4652 priv->digphy=1;
4653 priv->antb=0;
4654 priv->diversity=1;
4655 priv->cs_treshold=0xc;
4656 priv->rcr_csense=1;
4657 priv->rf_chip=RFCHIPID_PHILIPS;
4658 }else{
4659 if(!priv->card_8185){
4660 u8 rfparam = eprom_read(dev,RF_PARAM);
4661 DMESG("RfParam: %x",rfparam);
4663 priv->digphy = rfparam & (1<<RF_PARAM_DIGPHY_SHIFT) ? 0:1;
4664 priv->antb = rfparam & (1<<RF_PARAM_ANTBDEFAULT_SHIFT) ? 1:0;
4666 priv->rcr_csense = (rfparam & RF_PARAM_CARRIERSENSE_MASK) >>
4667 RF_PARAM_CARRIERSENSE_SHIFT;
4669 priv->diversity =
4670 (read_nic_byte(dev,CONFIG2)&(1<<CONFIG2_ANTENNA_SHIFT)) ? 1:0;
4671 }else{
4672 priv->rcr_csense = 3;
4675 priv->cs_treshold = (eprom_read(dev,ENERGY_TRESHOLD)&0xff00) >>8;
4677 priv->rf_chip = 0xff & eprom_read(dev,RFCHIPID);
4680 #ifdef CONFIG_RTL8185B
4681 #ifdef CONFIG_RTL818X_S
4682 priv->rf_chip = RF_ZEBRA4;
4683 priv->rf_sleep = rtl8225z4_rf_sleep;
4684 priv->rf_wakeup = rtl8225z4_rf_wakeup;
4685 #else
4686 priv->rf_chip = RF_ZEBRA2;
4687 #endif
4688 //DMESG("Card reports RF frontend Realtek 8225z2");
4689 //DMESGW("This driver has EXPERIMENTAL support for this chipset.");
4690 //DMESGW("use it with care and at your own risk and");
4691 DMESGW("**PLEASE** REPORT SUCCESSFUL/UNSUCCESSFUL TO Realtek!");
4693 priv->rf_close = rtl8225z2_rf_close;
4694 priv->rf_init = rtl8225z2_rf_init;
4695 priv->rf_set_chan = rtl8225z2_rf_set_chan;
4696 priv->rf_set_sens = NULL;
4697 //priv->rf_sleep = rtl8225_rf_sleep;
4698 //priv->rf_wakeup = rtl8225_rf_wakeup;
4700 #else
4701 /* check RF frontend chipset */
4702 switch (priv->rf_chip) {
4704 case RFCHIPID_RTL8225:
4706 if(priv->card_8185){
4707 DMESG("Card reports RF frontend Realtek 8225");
4708 DMESGW("This driver has EXPERIMENTAL support for this chipset.");
4709 DMESGW("use it with care and at your own risk and");
4710 DMESGW("**PLEASE** REPORT SUCCESS/INSUCCESS TO andreamrl@tiscali.it");
4712 priv->rf_close = rtl8225_rf_close;
4713 priv->rf_init = rtl8225_rf_init;
4714 priv->rf_set_chan = rtl8225_rf_set_chan;
4715 priv->rf_set_sens = NULL;
4716 priv->rf_sleep = rtl8225_rf_sleep;
4717 priv->rf_wakeup = rtl8225_rf_wakeup;
4719 }else{
4720 DMESGW("Detected RTL8225 radio on a card recognized as RTL8180");
4721 DMESGW("This could not be... something went wrong....");
4722 return -ENODEV;
4724 break;
4726 case RFCHIPID_RTL8255:
4727 if(priv->card_8185){
4728 DMESG("Card reports RF frontend Realtek 8255");
4729 DMESGW("This driver has EXPERIMENTAL support for this chipset.");
4730 DMESGW("use it with care and at your own risk and");
4731 DMESGW("**PLEASE** REPORT SUCCESS/INSUCCESS TO andreamrl@tiscali.it");
4733 priv->rf_close = rtl8255_rf_close;
4734 priv->rf_init = rtl8255_rf_init;
4735 priv->rf_set_chan = rtl8255_rf_set_chan;
4736 priv->rf_set_sens = NULL;
4737 priv->rf_sleep = NULL;
4738 priv->rf_wakeup = NULL;
4740 }else{
4741 DMESGW("Detected RTL8255 radio on a card recognized as RTL8180");
4742 DMESGW("This could not be... something went wrong....");
4743 return -ENODEV;
4745 break;
4748 case RFCHIPID_INTERSIL:
4749 DMESGW("Card reports RF frontend by Intersil.");
4750 DMESGW("This driver has NO support for this chipset.");
4751 return -ENODEV;
4752 break;
4754 case RFCHIPID_RFMD:
4755 DMESGW("Card reports RF frontend by RFMD.");
4756 DMESGW("This driver has NO support for this chipset.");
4757 return -ENODEV;
4758 break;
4760 case RFCHIPID_GCT:
4761 DMESGW("Card reports RF frontend by GCT.");
4762 DMESGW("This driver has EXPERIMENTAL support for this chipset.");
4763 DMESGW("use it with care and at your own risk and");
4764 DMESGW("**PLEASE** REPORT SUCCESS/INSUCCESS TO andreamrl@tiscali.it");
4765 priv->rf_close = gct_rf_close;
4766 priv->rf_init = gct_rf_init;
4767 priv->rf_set_chan = gct_rf_set_chan;
4768 priv->rf_set_sens = NULL;
4769 priv->rf_sleep = NULL;
4770 priv->rf_wakeup = NULL;
4771 break;
4773 case RFCHIPID_MAXIM:
4774 DMESGW("Card reports RF frontend by MAXIM.");
4775 DMESGW("This driver has EXPERIMENTAL support for this chipset.");
4776 DMESGW("use it with care and at your own risk and");
4777 DMESGW("**PLEASE** REPORT SUCCESS/INSUCCESS TO andreamrl@tiscali.it");
4778 priv->rf_close = maxim_rf_close;
4779 priv->rf_init = maxim_rf_init;
4780 priv->rf_set_chan = maxim_rf_set_chan;
4781 priv->rf_set_sens = NULL;
4782 priv->rf_sleep = NULL;
4783 priv->rf_wakeup = NULL;
4784 break;
4786 case RFCHIPID_PHILIPS:
4787 DMESG("Card reports RF frontend by Philips.");
4788 DMESG("OK! Philips SA2400 radio chipset is supported.");
4789 priv->rf_close = sa2400_rf_close;
4790 priv->rf_init = sa2400_rf_init;
4791 priv->rf_set_chan = sa2400_rf_set_chan;
4792 priv->rf_set_sens = sa2400_rf_set_sens;
4793 priv->sens = SA2400_RF_DEF_SENS; /* default sensitivity */
4794 priv->max_sens = SA2400_RF_MAX_SENS; /* maximum sensitivity */
4795 priv->rf_sleep = NULL;
4796 priv->rf_wakeup = NULL;
4798 if(priv->digphy){
4799 DMESGW("Digital PHY found");
4800 DMESGW("Philips DIGITAL PHY is untested! *Please*\
4801 report success/failure to <andreamrl@tiscali.it>");
4802 }else{
4803 DMESG ("Analog PHY found");
4806 break;
4808 default:
4809 DMESGW("Unknown RF module %x",priv->rf_chip);
4810 DMESGW("Exiting...");
4811 return -1;
4814 #endif
4817 if(!priv->card_8185){
4818 if(priv->antb)
4819 DMESG ("Antenna B is default antenna");
4820 else
4821 DMESG ("Antenna A is default antenna");
4823 if(priv->diversity)
4824 DMESG ("Antenna diversity is enabled");
4825 else
4826 DMESG("Antenna diversity is disabled");
4828 DMESG("Carrier sense %d",priv->rcr_csense);
4831 if (0!=alloc_rx_desc_ring(dev, priv->rxbuffersize, priv->rxringcount))
4832 return -ENOMEM;
4834 if (0!=alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txringcount,
4835 TX_MANAGEPRIORITY_RING_ADDR))
4836 return -ENOMEM;
4838 if (0!=alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txringcount,
4839 TX_BKPRIORITY_RING_ADDR))
4840 return -ENOMEM;
4842 if (0!=alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txringcount,
4843 TX_BEPRIORITY_RING_ADDR))
4844 return -ENOMEM;
4846 if (0!=alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txringcount,
4847 TX_VIPRIORITY_RING_ADDR))
4848 return -ENOMEM;
4850 if (0!=alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txringcount,
4851 TX_VOPRIORITY_RING_ADDR))
4852 return -ENOMEM;
4854 if (0!=alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txringcount,
4855 TX_HIGHPRIORITY_RING_ADDR))
4856 return -ENOMEM;
4858 if (0!=alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txbeaconcount,
4859 TX_BEACON_RING_ADDR))
4860 return -ENOMEM;
4863 //priv->beacon_buf=NULL;
4865 if(!priv->card_8185){
4867 if(read_nic_byte(dev, CONFIG0) & (1<<CONFIG0_WEP40_SHIFT))
4868 DMESG ("40-bit WEP is supported in hardware");
4869 else
4870 DMESG ("40-bit WEP is NOT supported in hardware");
4872 if(read_nic_byte(dev,CONFIG0) & (1<<CONFIG0_WEP104_SHIFT))
4873 DMESG ("104-bit WEP is supported in hardware");
4874 else
4875 DMESG ("104-bit WEP is NOT supported in hardware");
4877 #if !defined(SA_SHIRQ)
4878 if(request_irq(dev->irq, (void *)rtl8180_interrupt, IRQF_SHARED, dev->name, dev)){
4879 #else
4880 if(request_irq(dev->irq, (void *)rtl8180_interrupt, SA_SHIRQ, dev->name, dev)){
4881 #endif
4882 DMESGE("Error allocating IRQ %d",dev->irq);
4883 return -1;
4884 }else{
4885 priv->irq=dev->irq;
4886 DMESG("IRQ %d",dev->irq);
4889 #ifdef DEBUG_EPROM
4890 dump_eprom(dev);
4891 #endif
4893 return 0;
4898 void rtl8180_no_hw_wep(struct net_device *dev)
4900 struct r8180_priv *priv = ieee80211_priv(dev);
4902 if(!priv->card_8185)
4904 u8 security;
4906 security = read_nic_byte(dev, SECURITY);
4907 security &=~(1<<SECURITY_WEP_TX_ENABLE_SHIFT);
4908 security &=~(1<<SECURITY_WEP_RX_ENABLE_SHIFT);
4910 write_nic_byte(dev, SECURITY, security);
4912 }else{
4914 //FIXME!!!
4917 write_nic_dword(dev,TX_CONF,read_nic_dword(dev,TX_CONF) |
4918 (1<<TX_NOICV_SHIFT) );
4920 // priv->ieee80211->hw_wep=0;
4924 void rtl8180_set_hw_wep(struct net_device *dev)
4926 struct r8180_priv *priv = ieee80211_priv(dev);
4927 u8 pgreg;
4928 u8 security;
4929 u32 key0_word4;
4931 pgreg=read_nic_byte(dev, PGSELECT);
4932 write_nic_byte(dev, PGSELECT, pgreg &~ (1<<PGSELECT_PG_SHIFT));
4934 key0_word4 = read_nic_dword(dev, KEY0+4+4+4);
4935 key0_word4 &= ~ 0xff;
4936 key0_word4 |= priv->key0[3]& 0xff;
4937 write_nic_dword(dev,KEY0,(priv->key0[0]));
4938 write_nic_dword(dev,KEY0+4,(priv->key0[1]));
4939 write_nic_dword(dev,KEY0+4+4,(priv->key0[2]));
4940 write_nic_dword(dev,KEY0+4+4+4,(key0_word4));
4943 TX_CONF,read_nic_dword(dev,TX_CONF) &~(1<<TX_NOICV_SHIFT));
4946 security = read_nic_byte(dev,SECURITY);
4947 security |= (1<<SECURITY_WEP_TX_ENABLE_SHIFT);
4948 security |= (1<<SECURITY_WEP_RX_ENABLE_SHIFT);
4949 security &= ~ SECURITY_ENCRYP_MASK;
4950 security |= (SECURITY_ENCRYP_104<<SECURITY_ENCRYP_SHIFT);
4952 write_nic_byte(dev, SECURITY, security);
4954 DMESG("key %x %x %x %x",read_nic_dword(dev,KEY0+4+4+4),
4955 read_nic_dword(dev,KEY0+4+4),read_nic_dword(dev,KEY0+4),
4956 read_nic_dword(dev,KEY0));
4958 //priv->ieee80211->hw_wep=1;
4962 void rtl8185_rf_pins_enable(struct net_device *dev)
4964 // u16 tmp;
4965 // tmp = read_nic_word(dev, RFPinsEnable);
4966 write_nic_word(dev, RFPinsEnable, 0x1fff);// | tmp);
4967 // write_nic_word(dev, RFPinsEnable,7 | tmp);
4971 void rtl8185_set_anaparam2(struct net_device *dev, u32 a)
4973 u8 conf3;
4975 rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
4977 conf3 = read_nic_byte(dev, CONFIG3);
4978 write_nic_byte(dev, CONFIG3, conf3 | (1<<CONFIG3_ANAPARAM_W_SHIFT));
4979 write_nic_dword(dev, ANAPARAM2, a);
4981 conf3 = read_nic_byte(dev, CONFIG3);
4982 write_nic_byte(dev, CONFIG3, conf3 &~(1<<CONFIG3_ANAPARAM_W_SHIFT));
4983 rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
4988 void rtl8180_set_anaparam(struct net_device *dev, u32 a)
4990 u8 conf3;
4992 rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
4994 conf3 = read_nic_byte(dev, CONFIG3);
4995 write_nic_byte(dev, CONFIG3, conf3 | (1<<CONFIG3_ANAPARAM_W_SHIFT));
4996 write_nic_dword(dev, ANAPARAM, a);
4998 conf3 = read_nic_byte(dev, CONFIG3);
4999 write_nic_byte(dev, CONFIG3, conf3 &~(1<<CONFIG3_ANAPARAM_W_SHIFT));
5000 rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
5004 void rtl8185_tx_antenna(struct net_device *dev, u8 ant)
5006 write_nic_byte(dev, TX_ANTENNA, ant);
5007 force_pci_posting(dev);
5008 mdelay(1);
5012 void rtl8185_write_phy(struct net_device *dev, u8 adr, u32 data)
5014 //u8 phyr;
5015 u32 phyw;
5016 //int i;
5018 adr |= 0x80;
5020 phyw= ((data<<8) | adr);
5021 #if 0
5023 write_nic_dword(dev, PHY_ADR, phyw);
5025 //read_nic_dword(dev, PHY_ADR);
5026 for(i=0;i<10;i++){
5027 write_nic_dword(dev, PHY_ADR, 0xffffff7f & phyw);
5028 phyr = read_nic_byte(dev, PHY_READ);
5029 if(phyr == (data&0xff)) break;
5032 #else
5033 // Note that, we must write 0xff7c after 0x7d-0x7f to write BB register.
5034 write_nic_byte(dev, 0x7f, ((phyw & 0xff000000) >> 24));
5035 write_nic_byte(dev, 0x7e, ((phyw & 0x00ff0000) >> 16));
5036 write_nic_byte(dev, 0x7d, ((phyw & 0x0000ff00) >> 8));
5037 write_nic_byte(dev, 0x7c, ((phyw & 0x000000ff) ));
5038 #endif
5039 /* this is ok to fail when we write AGC table. check for AGC table might be
5040 * done by masking with 0x7f instead of 0xff
5042 //if(phyr != (data&0xff)) DMESGW("Phy write timeout %x %x %x", phyr, data,adr);
5046 inline void write_phy_ofdm (struct net_device *dev, u8 adr, u32 data)
5048 data = data & 0xff;
5049 rtl8185_write_phy(dev, adr, data);
5053 void write_phy_cck (struct net_device *dev, u8 adr, u32 data)
5055 data = data & 0xff;
5056 rtl8185_write_phy(dev, adr, data | 0x10000);
5060 /* 70*3 = 210 ms
5061 * I hope this is enougth
5063 #define MAX_PHY 70
5064 void write_phy(struct net_device *dev, u8 adr, u8 data)
5066 u32 phy;
5067 int i;
5069 phy = 0xff0000;
5070 phy |= adr;
5071 phy |= 0x80; /* this should enable writing */
5072 phy |= (data<<8);
5074 //PHY_ADR, PHY_R and PHY_W are contig and treated as one dword
5075 write_nic_dword(dev,PHY_ADR, phy);
5077 phy= 0xffff00;
5078 phy |= adr;
5080 write_nic_dword(dev,PHY_ADR, phy);
5081 for(i=0;i<MAX_PHY;i++){
5082 phy=read_nic_dword(dev,PHY_ADR);
5083 phy= phy & 0xff0000;
5084 phy= phy >> 16;
5085 if(phy == data){ //SUCCESS!
5086 force_pci_posting(dev);
5087 mdelay(3); //random value
5088 #ifdef DEBUG_BB
5089 DMESG("Phy wr %x,%x",adr,data);
5090 #endif
5091 return;
5092 }else{
5093 force_pci_posting(dev);
5094 mdelay(3); //random value
5097 DMESGW ("Phy writing %x %x failed!", adr,data);
5100 void rtl8185_set_rate(struct net_device *dev)
5102 int i;
5103 u16 word;
5104 int basic_rate,min_rr_rate,max_rr_rate;
5106 // struct r8180_priv *priv = ieee80211_priv(dev);
5108 //if (ieee80211_is_54g(priv->ieee80211->current_network) &&
5109 // priv->ieee80211->state == IEEE80211_LINKED){
5110 basic_rate = ieeerate2rtlrate(240);
5111 min_rr_rate = ieeerate2rtlrate(60);
5112 max_rr_rate = ieeerate2rtlrate(240);
5115 // }else{
5116 // basic_rate = ieeerate2rtlrate(20);
5117 // min_rr_rate = ieeerate2rtlrate(10);
5118 // max_rr_rate = ieeerate2rtlrate(110);
5119 // }
5121 write_nic_byte(dev, RESP_RATE,
5122 max_rr_rate<<MAX_RESP_RATE_SHIFT| min_rr_rate<<MIN_RESP_RATE_SHIFT);
5124 word = read_nic_word(dev, BRSR);
5125 word &= ~BRSR_MBR_8185;
5128 for(i=0;i<=basic_rate;i++)
5129 word |= (1<<i);
5131 write_nic_word(dev, BRSR, word);
5132 //DMESG("RR:%x BRSR: %x", read_nic_byte(dev,RESP_RATE),read_nic_word(dev,BRSR));
5137 void rtl8180_adapter_start(struct net_device *dev)
5139 struct r8180_priv *priv = ieee80211_priv(dev);
5140 u32 anaparam;
5141 u16 word;
5142 u8 config3;
5143 // int i;
5145 rtl8180_rtx_disable(dev);
5146 rtl8180_reset(dev);
5148 /* seems that 0xffff or 0xafff will cause
5149 * HW interrupt line crash
5152 //priv->irq_mask = 0xafff;
5153 // priv->irq_mask = 0x4fcf;
5155 /* enable beacon timeout, beacon TX ok and err
5156 * LP tx ok and err, HP TX ok and err, NP TX ok and err,
5157 * RX ok and ERR, and GP timer */
5158 priv->irq_mask = 0x6fcf;
5160 priv->dma_poll_mask = 0;
5162 rtl8180_beacon_tx_disable(dev);
5164 if(priv->card_type == CARDBUS ){
5165 config3=read_nic_byte(dev, CONFIG3);
5166 write_nic_byte(dev,CONFIG3,config3 | CONFIG3_FuncRegEn);
5167 write_nic_word(dev,FEMR, FEMR_INTR | FEMR_WKUP | FEMR_GWAKE |
5168 read_nic_word(dev, FEMR));
5170 rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
5171 write_nic_dword(dev, MAC0, ((u32*)dev->dev_addr)[0]);
5172 write_nic_word(dev, MAC4, ((u32*)dev->dev_addr)[1] & 0xffff );
5173 rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
5175 rtl8180_update_msr(dev);
5177 if(!priv->card_8185){
5178 anaparam = eprom_read(dev,EPROM_ANAPARAM_ADDRLWORD);
5179 anaparam |= eprom_read(dev,EPROM_ANAPARAM_ADDRHWORD)<<16;
5181 rtl8180_set_anaparam(dev,anaparam);
5183 /* These might be unnecessary since we do in rx_enable / tx_enable */
5184 fix_rx_fifo(dev);
5185 fix_tx_fifo(dev);
5186 /*set_nic_rxring(dev);
5187 set_nic_txring(dev);*/
5189 rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
5192 The following is very strange. seems to be that 1 means test mode,
5193 but we need to acknolwledges the nic when a packet is ready
5194 altought we set it to 0
5197 write_nic_byte(dev,
5198 CONFIG2, read_nic_byte(dev,CONFIG2) &~\
5199 (1<<CONFIG2_DMA_POLLING_MODE_SHIFT));
5200 //^the nic isn't in test mode
5201 if(priv->card_8185)
5202 write_nic_byte(dev,
5203 CONFIG2, read_nic_byte(dev,CONFIG2)|(1<<4));
5205 rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
5207 write_nic_dword(dev,INT_TIMEOUT,0);
5208 #ifdef DEBUG_REGISTERS
5209 rtl8180_dump_reg(dev);
5210 #endif
5212 if(!priv->card_8185)
5215 experimental - this might be needed to calibrate AGC,
5216 anyway it shouldn't hurt
5218 write_nic_byte(dev, CONFIG5,
5219 read_nic_byte(dev, CONFIG5) | (1<<AGCRESET_SHIFT));
5220 read_nic_byte(dev, CONFIG5);
5221 udelay(15);
5222 write_nic_byte(dev, CONFIG5,
5223 read_nic_byte(dev, CONFIG5) &~ (1<<AGCRESET_SHIFT));
5224 }else{
5226 write_nic_byte(dev, WPA_CONFIG, 0);
5227 //write_nic_byte(dev, TESTR, 0xd);
5230 rtl8180_no_hw_wep(dev);
5232 if(priv->card_8185){
5233 rtl8185_set_rate(dev);
5234 write_nic_byte(dev, RATE_FALLBACK, 0x81);
5235 // write_nic_byte(dev, 0xdf, 0x15);
5236 }else{
5237 word = read_nic_word(dev, BRSR);
5238 word &= ~BRSR_MBR;
5239 word &= ~BRSR_BPLCP;
5240 word |= ieeerate2rtlrate(priv->ieee80211->basic_rate);
5241 //by amy
5242 word |= 0x0f;
5243 //by amy
5244 write_nic_word(dev, BRSR, word);
5248 if(priv->card_8185){
5249 write_nic_byte(dev, GP_ENABLE,read_nic_byte(dev, GP_ENABLE) & ~(1<<6));
5251 //FIXME cfg 3 ClkRun enable - isn't it ReadOnly ?
5252 rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
5253 write_nic_byte(dev,CONFIG3, read_nic_byte(dev, CONFIG3)
5254 |(1<<CONFIG3_CLKRUN_SHIFT));
5255 rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
5259 priv->rf_init(dev);
5261 if(priv->rf_set_sens != NULL)
5262 priv->rf_set_sens(dev,priv->sens);
5263 rtl8180_irq_enable(dev);
5265 netif_start_queue(dev);
5266 /*DMESG ("lfree %d",get_curr_tx_free_desc(dev,LOW_PRIORITY));
5268 DMESG ("nfree %d",get_curr_tx_free_desc(dev,NORM_PRIORITY));
5270 DMESG ("hfree %d",get_curr_tx_free_desc(dev,HI_PRIORITY));
5271 if(check_nic_enought_desc(dev,NORM_PRIORITY)) DMESG("NORM OK");
5272 if(check_nic_enought_desc(dev,HI_PRIORITY)) DMESG("HI OK");
5273 if(check_nic_enought_desc(dev,LOW_PRIORITY)) DMESG("LOW OK");*/
5278 /* this configures registers for beacon tx and enables it via
5279 * rtl8180_beacon_tx_enable(). rtl8180_beacon_tx_disable() might
5280 * be used to stop beacon transmission
5282 void rtl8180_start_tx_beacon(struct net_device *dev)
5284 // struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
5285 u16 word;
5286 // DMESG("ring %x %x", priv->txlpringdma,read_nic_dword(dev,TLPDA));
5288 DMESG("Enabling beacon TX");
5289 //write_nic_byte(dev, 0x42,0xe6);// TCR
5290 // set_nic_txring(dev);
5291 // fix_tx_fifo(dev);
5292 rtl8180_prepare_beacon(dev);
5293 rtl8180_irq_disable(dev);
5294 rtl8180_beacon_tx_enable(dev);
5295 #if 0
5296 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
5297 //write_nic_byte(dev,0x9d,0x20); //DMA Poll
5298 //write_nic_word(dev,0x7a,0);
5299 //write_nic_word(dev,0x7a,0x8000);
5301 #if 0
5302 word = read_nic_word(dev, BcnItv);
5303 word &= ~BcnItv_BcnItv; // clear Bcn_Itv
5304 word |= priv->ieee80211->current_network.beacon_interval;//0x64;
5305 write_nic_word(dev, BcnItv, word);
5306 #endif
5307 #endif
5308 word = read_nic_word(dev, AtimWnd) &~ AtimWnd_AtimWnd;
5309 write_nic_word(dev, AtimWnd,word);// word |=
5310 //priv->ieee80211->current_network.atim_window);
5312 word = read_nic_word(dev, BintrItv);
5313 word &= ~BintrItv_BintrItv;
5314 word |= 1000;/*priv->ieee80211->current_network.beacon_interval *
5315 ((priv->txbeaconcount > 1)?(priv->txbeaconcount-1):1);
5316 // FIXME: check if correct ^^ worked with 0x3e8;
5318 write_nic_word(dev, BintrItv, word);
5321 rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
5323 // rtl8180_beacon_tx_enable(dev);
5324 #ifdef CONFIG_RTL8185B
5325 rtl8185b_irq_enable(dev);
5326 #else
5327 rtl8180_irq_enable(dev);
5328 #endif
5329 /* VV !!!!!!!!!! VV*/
5331 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
5332 write_nic_byte(dev,0x9d,0x00);
5333 rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
5335 // DMESG("ring %x %x", priv->txlpringdma,read_nic_dword(dev,TLPDA));
5341 /***************************************************************************
5342 -------------------------------NET STUFF---------------------------
5343 ***************************************************************************/
5344 static struct net_device_stats *rtl8180_stats(struct net_device *dev)
5346 struct r8180_priv *priv = ieee80211_priv(dev);
5348 return &priv->ieee80211->stats;
5351 // Change current and default preamble mode.
5352 // 2005.01.06, by rcnjko.
5354 bool
5355 MgntActSet_802_11_PowerSaveMode(
5356 struct r8180_priv *priv,
5357 RT_PS_MODE rtPsMode
5361 // Currently, we do not change power save mode on IBSS mode.
5362 if(priv->ieee80211->iw_mode == IW_MODE_ADHOC)
5364 return false;
5368 // <RJ_NOTE> If we make HW to fill up the PwrMgt bit for us,
5369 // some AP will not response to our mgnt frames with PwrMgt bit set,
5370 // e.g. cannot associate the AP.
5371 // So I commented out it. 2005.02.16, by rcnjko.
5373 // // Change device's power save mode.
5374 // Adapter->HalFunc.SetPSModeHandler( Adapter, rtPsMode );
5376 // Update power save mode configured.
5377 // priv->dot11PowerSaveMode = rtPsMode;
5378 priv->ieee80211->ps = rtPsMode;
5379 // Determine ListenInterval.
5380 #if 0
5381 if(priv->dot11PowerSaveMode == eMaxPs)
5383 priv->ieee80211->ListenInterval = 10;
5385 else
5387 priv->ieee80211->ListenInterval = 2;
5389 #endif
5390 return true;
5393 //================================================================================
5394 // Leisure Power Save in linked state.
5395 //================================================================================
5398 // Description:
5399 // Enter the leisure power save mode.
5401 void
5402 LeisurePSEnter(
5403 struct r8180_priv *priv
5406 if (priv->bLeisurePs)
5408 if (priv->ieee80211->ps == IEEE80211_PS_DISABLED)
5410 //printk("----Enter PS\n");
5411 MgntActSet_802_11_PowerSaveMode(priv, IEEE80211_PS_MBCAST|IEEE80211_PS_UNICAST);//IEEE80211_PS_ENABLE
5418 // Description:
5419 // Leave the leisure power save mode.
5421 void
5422 LeisurePSLeave(
5423 struct r8180_priv *priv
5426 if (priv->bLeisurePs)
5428 if (priv->ieee80211->ps != IEEE80211_PS_DISABLED)
5430 //printk("----Leave PS\n");
5431 MgntActSet_802_11_PowerSaveMode(priv, IEEE80211_PS_DISABLED);
5435 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
5436 void rtl8180_hw_wakeup_wq (struct work_struct *work)
5438 // struct r8180_priv *priv = container_of(work, struct r8180_priv, watch_dog_wq);
5439 // struct ieee80211_device * ieee = (struct ieee80211_device*)
5440 // container_of(work, struct ieee80211_device, watch_dog_wq);
5441 struct delayed_work *dwork = container_of(work,struct delayed_work,work);
5442 struct ieee80211_device *ieee = container_of(dwork,struct ieee80211_device,hw_wakeup_wq);
5443 struct net_device *dev = ieee->dev;
5444 #else
5445 void rtl8180_hw_wakeup_wq(struct net_device *dev)
5447 struct r8180_priv *priv = ieee80211_priv(dev);
5448 #endif
5450 // printk("dev is %d\n",dev);
5451 // printk("&*&(^*(&(&=========>%s()\n", __func__);
5452 rtl8180_hw_wakeup(dev);
5456 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
5457 void rtl8180_hw_sleep_wq (struct work_struct *work)
5459 // struct r8180_priv *priv = container_of(work, struct r8180_priv, watch_dog_wq);
5460 // struct ieee80211_device * ieee = (struct ieee80211_device*)
5461 // container_of(work, struct ieee80211_device, watch_dog_wq);
5462 struct delayed_work *dwork = container_of(work,struct delayed_work,work);
5463 struct ieee80211_device *ieee = container_of(dwork,struct ieee80211_device,hw_sleep_wq);
5464 struct net_device *dev = ieee->dev;
5465 #else
5466 void rtl8180_hw_sleep_wq(struct net_device *dev)
5468 struct r8180_priv *priv = ieee80211_priv(dev);
5469 #endif
5471 rtl8180_hw_sleep_down(dev);
5474 //YJ,add,080828,for KeepAlive
5475 static void MgntLinkKeepAlive(struct r8180_priv *priv )
5477 if (priv->keepAliveLevel == 0)
5478 return;
5480 if(priv->ieee80211->state == IEEE80211_LINKED)
5483 // Keep-Alive.
5485 //printk("LastTx:%d Tx:%d LastRx:%d Rx:%ld Idle:%d\n",priv->link_detect.LastNumTxUnicast,priv->NumTxUnicast, priv->link_detect.LastNumRxUnicast, priv->ieee80211->NumRxUnicast, priv->link_detect.IdleCount);
5487 if ( (priv->keepAliveLevel== 2) ||
5488 (priv->link_detect.LastNumTxUnicast == priv->NumTxUnicast &&
5489 priv->link_detect.LastNumRxUnicast == priv->ieee80211->NumRxUnicast )
5492 priv->link_detect.IdleCount++;
5495 // Send a Keep-Alive packet packet to AP if we had been idle for a while.
5497 if(priv->link_detect.IdleCount >= ((KEEP_ALIVE_INTERVAL / CHECK_FOR_HANG_PERIOD)-1) )
5499 priv->link_detect.IdleCount = 0;
5500 ieee80211_sta_ps_send_null_frame(priv->ieee80211, false);
5503 else
5505 priv->link_detect.IdleCount = 0;
5507 priv->link_detect.LastNumTxUnicast = priv->NumTxUnicast;
5508 priv->link_detect.LastNumRxUnicast = priv->ieee80211->NumRxUnicast;
5511 //YJ,add,080828,for KeepAlive,end
5513 static u8 read_acadapter_file(char *filename);
5514 void rtl8180_watch_dog(struct net_device *dev)
5516 struct r8180_priv *priv = ieee80211_priv(dev);
5517 bool bEnterPS = false;
5518 bool bBusyTraffic = false;
5519 u32 TotalRxNum = 0;
5520 u16 SlotIndex = 0;
5521 u16 i = 0;
5522 #ifdef ENABLE_IPS
5523 if(priv->ieee80211->actscanning == false){
5524 if((priv->ieee80211->iw_mode != IW_MODE_ADHOC) && (priv->ieee80211->state == IEEE80211_NOLINK) && (priv->ieee80211->beinretry == false) && (priv->eRFPowerState == eRfOn)){
5525 IPSEnter(dev);
5528 #endif
5529 //YJ,add,080828,for link state check
5530 if((priv->ieee80211->state == IEEE80211_LINKED) && (priv->ieee80211->iw_mode == IW_MODE_INFRA)){
5531 SlotIndex = (priv->link_detect.SlotIndex++) % priv->link_detect.SlotNum;
5532 priv->link_detect.RxFrameNum[SlotIndex] = priv->ieee80211->NumRxDataInPeriod + priv->ieee80211->NumRxBcnInPeriod;
5533 for( i=0; i<priv->link_detect.SlotNum; i++ )
5534 TotalRxNum+= priv->link_detect.RxFrameNum[i];
5535 //printk("&&&&&=== TotalRxNum = %d\n", TotalRxNum);
5536 if(TotalRxNum == 0){
5537 priv->ieee80211->state = IEEE80211_ASSOCIATING;
5538 queue_work(priv->ieee80211->wq, &priv->ieee80211->associate_procedure_wq);
5542 //YJ,add,080828,for KeepAlive
5543 MgntLinkKeepAlive(priv);
5545 //YJ,add,080828,for LPS
5546 #ifdef ENABLE_LPS
5547 if(priv->PowerProfile == POWER_PROFILE_BATTERY )
5549 //Turn on LeisurePS on battery power
5550 //printk("!!!!!On battery power\n");
5551 priv->bLeisurePs = true;
5553 else if(priv->PowerProfile == POWER_PROFILE_AC )
5555 // Turn off LeisurePS on AC power
5556 //printk("----On AC power\n");
5557 LeisurePSLeave(priv);
5558 priv->bLeisurePs= false;
5560 #endif
5562 #if 0
5563 #ifndef ENABLE_LPS
5564 if(priv->ieee80211->state == IEEE80211_LINKED){
5565 if( priv->NumRxOkInPeriod> 666 ||
5566 priv->NumTxOkInPeriod > 666 ) {
5567 bBusyTraffic = true;
5569 if((priv->ieee80211->NumRxData + priv->NumTxOkInPeriod)<8) {
5570 bEnterPS= true;
5572 if(bEnterPS) {
5573 LeisurePSEnter(priv);
5575 else {
5576 LeisurePSLeave(priv);
5579 else {
5580 LeisurePSLeave(priv);
5582 #endif
5583 priv->NumRxOkInPeriod = 0;
5584 priv->NumTxOkInPeriod = 0;
5585 priv->ieee80211->NumRxData = 0;
5586 #else
5587 #ifdef ENABLE_LPS
5588 if(priv->ieee80211->state == IEEE80211_LINKED){
5589 priv->link_detect.NumRxOkInPeriod = priv->ieee80211->NumRxDataInPeriod;
5590 //printk("TxOk=%d RxOk=%d\n", priv->link_detect.NumTxOkInPeriod, priv->link_detect.NumRxOkInPeriod);
5591 if( priv->link_detect.NumRxOkInPeriod> 666 ||
5592 priv->link_detect.NumTxOkInPeriod> 666 ) {
5593 bBusyTraffic = true;
5595 if(((priv->link_detect.NumRxOkInPeriod + priv->link_detect.NumTxOkInPeriod) > 8)
5596 || (priv->link_detect.NumRxOkInPeriod > 2)) {
5597 bEnterPS= false;
5599 else {
5600 bEnterPS= true;
5603 if(bEnterPS) {
5604 LeisurePSEnter(priv);
5606 else {
5607 LeisurePSLeave(priv);
5610 else{
5611 LeisurePSLeave(priv);
5613 #endif
5614 priv->link_detect.bBusyTraffic = bBusyTraffic;
5615 priv->link_detect.NumRxOkInPeriod = 0;
5616 priv->link_detect.NumTxOkInPeriod = 0;
5617 priv->ieee80211->NumRxDataInPeriod = 0;
5618 priv->ieee80211->NumRxBcnInPeriod = 0;
5619 #endif
5621 int _rtl8180_up(struct net_device *dev)
5623 struct r8180_priv *priv = ieee80211_priv(dev);
5624 //int i;
5626 priv->up=1;
5628 DMESG("Bringing up iface");
5629 #ifdef CONFIG_RTL8185B
5630 rtl8185b_adapter_start(dev);
5631 rtl8185b_rx_enable(dev);
5632 rtl8185b_tx_enable(dev);
5633 #else
5634 rtl8180_adapter_start(dev);
5635 rtl8180_rx_enable(dev);
5636 rtl8180_tx_enable(dev);
5637 #endif
5638 #ifdef ENABLE_IPS
5639 if(priv->bInactivePs){
5640 if(priv->ieee80211->iw_mode == IW_MODE_ADHOC)
5641 IPSLeave(dev);
5643 #endif
5644 //by amy 080312
5645 #ifdef RATE_ADAPT
5646 timer_rate_adaptive((unsigned long)dev);
5647 #endif
5648 //by amy 080312
5649 watch_dog_adaptive((unsigned long)dev);
5650 #ifdef SW_ANTE
5651 if(priv->bSwAntennaDiverity)
5652 SwAntennaDiversityTimerCallback(dev);
5653 #endif
5654 // IPSEnter(dev);
5655 ieee80211_softmac_start_protocol(priv->ieee80211);
5657 //Add for RF power on power off by lizhaoming 080512
5658 // priv->eRFPowerState = eRfOn;
5659 // printk("\n--------Start queue_work:GPIOChangeRFWorkItem");
5660 // queue_delayed_work(priv->ieee80211->wq,&priv->ieee80211->GPIOChangeRFWorkItem,1000);
5662 return 0;
5666 int rtl8180_open(struct net_device *dev)
5668 struct r8180_priv *priv = ieee80211_priv(dev);
5669 int ret;
5671 down(&priv->wx_sem);
5672 ret = rtl8180_up(dev);
5673 up(&priv->wx_sem);
5674 return ret;
5679 int rtl8180_up(struct net_device *dev)
5681 struct r8180_priv *priv = ieee80211_priv(dev);
5683 if (priv->up == 1) return -1;
5685 return _rtl8180_up(dev);
5689 int rtl8180_close(struct net_device *dev)
5691 struct r8180_priv *priv = ieee80211_priv(dev);
5692 int ret;
5694 down(&priv->wx_sem);
5695 ret = rtl8180_down(dev);
5696 up(&priv->wx_sem);
5698 return ret;
5702 int rtl8180_down(struct net_device *dev)
5704 struct r8180_priv *priv = ieee80211_priv(dev);
5706 if (priv->up == 0) return -1;
5708 priv->up=0;
5710 ieee80211_softmac_stop_protocol(priv->ieee80211);
5711 /* FIXME */
5712 if (!netif_queue_stopped(dev))
5713 netif_stop_queue(dev);
5714 rtl8180_rtx_disable(dev);
5715 rtl8180_irq_disable(dev);
5716 del_timer_sync(&priv->watch_dog_timer);
5717 //cancel_delayed_work(&priv->ieee80211->watch_dog_wq);
5718 //{by amy 080312
5719 del_timer_sync(&priv->rateadapter_timer);
5720 cancel_delayed_work(&priv->ieee80211->rate_adapter_wq);
5721 //by amy 080312}
5722 cancel_delayed_work(&priv->ieee80211->hw_wakeup_wq);
5723 cancel_delayed_work(&priv->ieee80211->hw_sleep_wq);
5724 cancel_delayed_work(&priv->ieee80211->hw_dig_wq);
5725 cancel_delayed_work(&priv->ieee80211->tx_pw_wq);
5726 del_timer_sync(&priv->SwAntennaDiversityTimer);
5727 SetZebraRFPowerState8185(dev,eRfOff);
5728 //ieee80211_softmac_stop_protocol(priv->ieee80211);
5729 memset(&(priv->ieee80211->current_network),0,sizeof(struct ieee80211_network));
5730 priv->ieee80211->state = IEEE80211_NOLINK;
5731 return 0;
5734 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
5735 void rtl8180_restart_wq(struct work_struct *work)
5737 struct r8180_priv *priv = container_of(work, struct r8180_priv, reset_wq);
5738 struct net_device *dev = priv->dev;
5739 #else
5740 void rtl8180_restart_wq(struct net_device *dev)
5742 struct r8180_priv *priv = ieee80211_priv(dev);
5743 #endif
5744 down(&priv->wx_sem);
5746 rtl8180_commit(dev);
5748 up(&priv->wx_sem);
5751 void rtl8180_restart(struct net_device *dev)
5753 struct r8180_priv *priv = ieee80211_priv(dev);
5754 //rtl8180_commit(dev);
5755 schedule_work(&priv->reset_wq);
5756 //DMESG("TXTIMEOUT");
5760 void rtl8180_commit(struct net_device *dev)
5762 struct r8180_priv *priv = ieee80211_priv(dev);
5764 if (priv->up == 0) return ;
5765 //+by amy 080312
5766 del_timer_sync(&priv->watch_dog_timer);
5767 //cancel_delayed_work(&priv->ieee80211->watch_dog_wq);
5768 //{by amy 080312
5769 //by amy for rate adaptive
5770 del_timer_sync(&priv->rateadapter_timer);
5771 cancel_delayed_work(&priv->ieee80211->rate_adapter_wq);
5772 //by amy for rate adaptive
5773 //by amy 080312}
5774 cancel_delayed_work(&priv->ieee80211->hw_wakeup_wq);
5775 cancel_delayed_work(&priv->ieee80211->hw_sleep_wq);
5776 cancel_delayed_work(&priv->ieee80211->hw_dig_wq);
5777 cancel_delayed_work(&priv->ieee80211->tx_pw_wq);
5778 del_timer_sync(&priv->SwAntennaDiversityTimer);
5779 ieee80211_softmac_stop_protocol(priv->ieee80211);
5780 rtl8180_irq_disable(dev);
5781 rtl8180_rtx_disable(dev);
5782 _rtl8180_up(dev);
5786 static void r8180_set_multicast(struct net_device *dev)
5788 struct r8180_priv *priv = ieee80211_priv(dev);
5789 short promisc;
5791 //down(&priv->wx_sem);
5793 promisc = (dev->flags & IFF_PROMISC) ? 1:0;
5795 if (promisc != priv->promisc)
5796 rtl8180_restart(dev);
5798 priv->promisc = promisc;
5800 //up(&priv->wx_sem);
5803 #if 0
5804 /* this is called by the kernel when it needs to TX a 802.3 encapsulated frame*/
5805 int rtl8180_8023_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
5807 struct r8180_priv *priv = ieee80211_priv(dev);
5808 int ret;
5809 unsigned long flags;
5811 spin_lock_irqsave(&priv->tx_lock,flags);
5812 ret = ieee80211_r8180_8023_hardstartxmit(skb,priv->ieee80211);
5813 spin_unlock_irqrestore(&priv->tx_lock,flags);
5814 return ret;
5816 #endif
5818 int r8180_set_mac_adr(struct net_device *dev, void *mac)
5820 struct r8180_priv *priv = ieee80211_priv(dev);
5821 struct sockaddr *addr = mac;
5823 down(&priv->wx_sem);
5825 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
5827 if(priv->ieee80211->iw_mode == IW_MODE_MASTER)
5828 memcpy(priv->ieee80211->current_network.bssid, dev->dev_addr, ETH_ALEN);
5830 if (priv->up) {
5831 rtl8180_down(dev);
5832 rtl8180_up(dev);
5835 up(&priv->wx_sem);
5837 return 0;
5840 /* based on ipw2200 driver */
5841 int rtl8180_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
5843 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
5845 struct iwreq *wrq = (struct iwreq *) rq;
5846 int ret=-1;
5847 switch (cmd) {
5848 case RTL_IOCTL_WPA_SUPPLICANT:
5849 ret = ieee80211_wpa_supplicant_ioctl(priv->ieee80211, &wrq->u.data);
5850 return ret;
5852 default:
5853 return -EOPNOTSUPP;
5856 return -EOPNOTSUPP;
5861 /****************************************************************************
5862 -----------------------------PCI STUFF---------------------------
5863 *****************************************************************************/
5866 static int __devinit rtl8180_pci_probe(struct pci_dev *pdev,
5867 const struct pci_device_id *id)
5869 unsigned long ioaddr = 0;
5870 struct net_device *dev = NULL;
5871 struct r8180_priv *priv= NULL;
5872 //u8 *ptr;
5873 u8 unit = 0;
5875 #ifdef CONFIG_RTL8180_IO_MAP
5876 unsigned long pio_start, pio_len, pio_flags;
5877 #else
5878 unsigned long pmem_start, pmem_len, pmem_flags;
5879 #endif //end #ifdef RTL_IO_MAP
5881 DMESG("Configuring chip resources");
5883 if( pci_enable_device (pdev) ){
5884 DMESG("Failed to enable PCI device");
5885 return -EIO;
5888 pci_set_master(pdev);
5889 //pci_set_wmi(pdev);
5890 pci_set_dma_mask(pdev, 0xffffff00ULL);
5891 pci_set_consistent_dma_mask(pdev,0xffffff00ULL);
5892 dev = alloc_ieee80211(sizeof(struct r8180_priv));
5893 if (!dev)
5894 return -ENOMEM;
5895 priv = ieee80211_priv(dev);
5896 priv->ieee80211 = netdev_priv(dev);
5898 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
5899 SET_MODULE_OWNER(dev);
5900 #endif
5901 pci_set_drvdata(pdev, dev);
5902 SET_NETDEV_DEV(dev, &pdev->dev);
5904 priv = ieee80211_priv(dev);
5905 // memset(priv,0,sizeof(struct r8180_priv));
5906 priv->pdev=pdev;
5909 #ifdef CONFIG_RTL8180_IO_MAP
5911 pio_start = (unsigned long)pci_resource_start (pdev, 0);
5912 pio_len = (unsigned long)pci_resource_len (pdev, 0);
5913 pio_flags = (unsigned long)pci_resource_flags (pdev, 0);
5915 if (!(pio_flags & IORESOURCE_IO)) {
5916 DMESG("region #0 not a PIO resource, aborting");
5917 goto fail;
5920 //DMESG("IO space @ 0x%08lx", pio_start );
5921 if( ! request_region( pio_start, pio_len, RTL8180_MODULE_NAME ) ){
5922 DMESG("request_region failed!");
5923 goto fail;
5926 ioaddr = pio_start;
5927 dev->base_addr = ioaddr; // device I/O address
5929 #else
5931 pmem_start = pci_resource_start(pdev, 1);
5932 pmem_len = pci_resource_len(pdev, 1);
5933 pmem_flags = pci_resource_flags (pdev, 1);
5935 if (!(pmem_flags & IORESOURCE_MEM)) {
5936 DMESG("region #1 not a MMIO resource, aborting");
5937 goto fail;
5940 //DMESG("Memory mapped space @ 0x%08lx ", pmem_start);
5941 if( ! request_mem_region(pmem_start, pmem_len, RTL8180_MODULE_NAME)) {
5942 DMESG("request_mem_region failed!");
5943 goto fail;
5947 ioaddr = (unsigned long)ioremap_nocache( pmem_start, pmem_len);
5948 if( ioaddr == (unsigned long)NULL ){
5949 DMESG("ioremap failed!");
5950 // release_mem_region( pmem_start, pmem_len );
5951 goto fail1;
5954 dev->mem_start = ioaddr; // shared mem start
5955 dev->mem_end = ioaddr + pci_resource_len(pdev, 0); // shared mem end
5957 #endif //end #ifdef RTL_IO_MAP
5959 #ifdef CONFIG_RTL8185B
5960 //pci_read_config_byte(pdev, 0x05, ptr);
5961 //pci_write_config_byte(pdev, 0x05, (*ptr) & (~0x04));
5962 pci_read_config_byte(pdev, 0x05, &unit);
5963 pci_write_config_byte(pdev, 0x05, unit & (~0x04));
5964 #endif
5966 dev->irq = pdev->irq;
5967 priv->irq = 0;
5969 dev->open = rtl8180_open;
5970 dev->stop = rtl8180_close;
5971 //dev->hard_start_xmit = ieee80211_xmit;
5972 dev->tx_timeout = rtl8180_restart;
5973 dev->wireless_handlers = &r8180_wx_handlers_def;
5974 dev->do_ioctl = rtl8180_ioctl;
5975 dev->set_multicast_list = r8180_set_multicast;
5976 dev->set_mac_address = r8180_set_mac_adr;
5978 #if WIRELESS_EXT >= 12
5979 #if WIRELESS_EXT < 17
5980 dev->get_wireless_stats = r8180_get_wireless_stats;
5981 #endif
5982 dev->wireless_handlers = (struct iw_handler_def *) &r8180_wx_handlers_def;
5983 #endif
5985 dev->type=ARPHRD_ETHER;
5986 dev->watchdog_timeo = HZ*3; //added by david woo, 2007.12.13
5988 if (dev_alloc_name(dev, ifname) < 0){
5989 DMESG("Oops: devname already taken! Trying wlan%%d...\n");
5990 ifname = "wlan%d";
5991 // ifname = "ath%d";
5992 dev_alloc_name(dev, ifname);
5996 if(rtl8180_init(dev)!=0){
5997 DMESG("Initialization failed");
5998 goto fail1;
6001 netif_carrier_off(dev);
6003 register_netdev(dev);
6005 rtl8180_proc_init_one(dev);
6007 DMESG("Driver probe completed\n");
6008 return 0;
6010 fail1:
6012 #ifdef CONFIG_RTL8180_IO_MAP
6014 if( dev->base_addr != 0 ){
6016 release_region(dev->base_addr,
6017 pci_resource_len(pdev, 0) );
6019 #else
6020 if( dev->mem_start != (unsigned long)NULL ){
6021 iounmap( (void *)dev->mem_start );
6022 release_mem_region( pci_resource_start(pdev, 1),
6023 pci_resource_len(pdev, 1) );
6025 #endif //end #ifdef RTL_IO_MAP
6028 fail:
6029 if(dev){
6031 if (priv->irq) {
6032 free_irq(dev->irq, dev);
6033 dev->irq=0;
6035 free_ieee80211(dev);
6038 pci_disable_device(pdev);
6040 DMESG("wlan driver load failed\n");
6041 pci_set_drvdata(pdev, NULL);
6042 return -ENODEV;
6047 static void __devexit rtl8180_pci_remove(struct pci_dev *pdev)
6049 struct r8180_priv *priv;
6050 struct net_device *dev = pci_get_drvdata(pdev);
6051 if(dev){
6053 unregister_netdev(dev);
6055 priv=ieee80211_priv(dev);
6057 rtl8180_proc_remove_one(dev);
6058 rtl8180_down(dev);
6059 priv->rf_close(dev);
6060 rtl8180_reset(dev);
6061 //rtl8180_rtx_disable(dev);
6062 //rtl8180_irq_disable(dev);
6063 mdelay(10);
6064 //write_nic_word(dev,INTA,read_nic_word(dev,INTA));
6065 //force_pci_posting(dev);
6066 //mdelay(10);
6068 if(priv->irq){
6070 DMESG("Freeing irq %d",dev->irq);
6071 free_irq(dev->irq, dev);
6072 priv->irq=0;
6076 free_rx_desc_ring(dev);
6077 free_tx_desc_rings(dev);
6078 // free_beacon_desc_ring(dev,priv->txbeaconcount);
6080 #ifdef CONFIG_RTL8180_IO_MAP
6082 if( dev->base_addr != 0 ){
6084 release_region(dev->base_addr,
6085 pci_resource_len(pdev, 0) );
6087 #else
6088 if( dev->mem_start != (unsigned long)NULL ){
6089 iounmap( (void *)dev->mem_start );
6090 release_mem_region( pci_resource_start(pdev, 1),
6091 pci_resource_len(pdev, 1) );
6093 #endif /*end #ifdef RTL_IO_MAP*/
6095 free_ieee80211(dev);
6097 pci_disable_device(pdev);
6099 DMESG("wlan driver removed\n");
6103 /* fun with the built-in ieee80211 stack... */
6104 extern int ieee80211_crypto_init(void);
6105 extern void ieee80211_crypto_deinit(void);
6106 extern int ieee80211_crypto_tkip_init(void);
6107 extern void ieee80211_crypto_tkip_exit(void);
6108 extern int ieee80211_crypto_ccmp_init(void);
6109 extern void ieee80211_crypto_ccmp_exit(void);
6110 extern int ieee80211_crypto_wep_init(void);
6111 extern void ieee80211_crypto_wep_exit(void);
6113 static int __init rtl8180_pci_module_init(void)
6115 int ret;
6117 ret = ieee80211_crypto_init();
6118 if (ret) {
6119 printk(KERN_ERR "ieee80211_crypto_init() failed %d\n", ret);
6120 return ret;
6122 ret = ieee80211_crypto_tkip_init();
6123 if (ret) {
6124 printk(KERN_ERR "ieee80211_crypto_tkip_init() failed %d\n", ret);
6125 return ret;
6127 ret = ieee80211_crypto_ccmp_init();
6128 if (ret) {
6129 printk(KERN_ERR "ieee80211_crypto_ccmp_init() failed %d\n", ret);
6130 return ret;
6132 ret = ieee80211_crypto_wep_init();
6133 if (ret) {
6134 printk(KERN_ERR "ieee80211_crypto_wep_init() failed %d\n", ret);
6135 return ret;
6138 printk(KERN_INFO "\nLinux kernel driver for RTL8180 \
6139 / RTL8185 based WLAN cards\n");
6140 printk(KERN_INFO "Copyright (c) 2004-2005, Andrea Merello\n");
6141 DMESG("Initializing module");
6142 DMESG("Wireless extensions version %d", WIRELESS_EXT);
6143 rtl8180_proc_module_init();
6145 #if(LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22))
6146 if(0!=pci_module_init(&rtl8180_pci_driver))
6147 #else
6148 if(0!=pci_register_driver(&rtl8180_pci_driver))
6149 #endif
6150 //if(0!=pci_module_init(&rtl8180_pci_driver))
6152 DMESG("No device found");
6153 /*pci_unregister_driver (&rtl8180_pci_driver);*/
6154 return -ENODEV;
6156 return 0;
6160 static void __exit rtl8180_pci_module_exit(void)
6162 pci_unregister_driver (&rtl8180_pci_driver);
6163 rtl8180_proc_module_remove();
6164 ieee80211_crypto_tkip_exit();
6165 ieee80211_crypto_ccmp_exit();
6166 ieee80211_crypto_wep_exit();
6167 ieee80211_crypto_deinit();
6168 DMESG("Exiting");
6172 void rtl8180_try_wake_queue(struct net_device *dev, int pri)
6174 unsigned long flags;
6175 short enough_desc;
6176 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
6178 spin_lock_irqsave(&priv->tx_lock,flags);
6179 enough_desc = check_nic_enought_desc(dev,pri);
6180 spin_unlock_irqrestore(&priv->tx_lock,flags);
6182 if(enough_desc)
6183 ieee80211_wake_queue(priv->ieee80211);
6186 /*****************************************************************************
6187 -----------------------------IRQ STUFF---------------------------
6188 ******************************************************************************/
6190 void rtl8180_tx_isr(struct net_device *dev, int pri,short error)
6192 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
6194 u32 *tail; //tail virtual addr
6195 u32 *head; //head virtual addr
6196 u32 *begin;//start of ring virtual addr
6197 u32 *nicv; //nic pointer virtual addr
6198 // u32 *txdv; //packet just TXed
6199 u32 nic; //nic pointer physical addr
6200 u32 nicbegin;// start of ring physical addr
6201 // short txed;
6202 unsigned long flag;
6203 /* physical addr are ok on 32 bits since we set DMA mask*/
6205 int offs;
6206 int j,i;
6207 int hd;
6208 if (error) priv->stats.txretry++; //tony 20060601
6209 spin_lock_irqsave(&priv->tx_lock,flag);
6210 switch(pri) {
6211 case MANAGE_PRIORITY:
6212 tail = priv->txmapringtail;
6213 begin = priv->txmapring;
6214 head = priv->txmapringhead;
6215 nic = read_nic_dword(dev,TX_MANAGEPRIORITY_RING_ADDR);
6216 nicbegin = priv->txmapringdma;
6217 break;
6219 case BK_PRIORITY:
6220 tail = priv->txbkpringtail;
6221 begin = priv->txbkpring;
6222 head = priv->txbkpringhead;
6223 nic = read_nic_dword(dev,TX_BKPRIORITY_RING_ADDR);
6224 nicbegin = priv->txbkpringdma;
6225 break;
6227 case BE_PRIORITY:
6228 tail = priv->txbepringtail;
6229 begin = priv->txbepring;
6230 head = priv->txbepringhead;
6231 nic = read_nic_dword(dev,TX_BEPRIORITY_RING_ADDR);
6232 nicbegin = priv->txbepringdma;
6233 break;
6235 case VI_PRIORITY:
6236 tail = priv->txvipringtail;
6237 begin = priv->txvipring;
6238 head = priv->txvipringhead;
6239 nic = read_nic_dword(dev,TX_VIPRIORITY_RING_ADDR);
6240 nicbegin = priv->txvipringdma;
6241 break;
6243 case VO_PRIORITY:
6244 tail = priv->txvopringtail;
6245 begin = priv->txvopring;
6246 head = priv->txvopringhead;
6247 nic = read_nic_dword(dev,TX_VOPRIORITY_RING_ADDR);
6248 nicbegin = priv->txvopringdma;
6249 break;
6251 case HI_PRIORITY:
6252 tail = priv->txhpringtail;
6253 begin = priv->txhpring;
6254 head = priv->txhpringhead;
6255 nic = read_nic_dword(dev,TX_HIGHPRIORITY_RING_ADDR);
6256 nicbegin = priv->txhpringdma;
6257 break;
6259 default:
6260 spin_unlock_irqrestore(&priv->tx_lock,flag);
6261 return ;
6263 /* DMESG("%x %s %x %x",((int)nic & 0xfff)/8/4,
6264 *(priv->txnpring + ((int)nic&0xfff)/4/8) & (1<<31) ? "filled" : "empty",
6265 (priv->txnpringtail - priv->txnpring)/8,(priv->txnpringhead -
6266 priv->txnpring)/8);
6268 //nicv = (u32*) ((nic - nicbegin) + (int)begin);
6269 nicv = (u32*) ((nic - nicbegin) + (u8*)begin);
6270 if((head <= tail && (nicv > tail || nicv < head)) ||
6271 (head > tail && (nicv > tail && nicv < head))){
6273 DMESGW("nic has lost pointer");
6274 #ifdef DEBUG_TX_DESC
6275 //check_tx_ring(dev,NORM_PRIORITY);
6276 check_tx_ring(dev,pri);
6277 #endif
6278 spin_unlock_irqrestore(&priv->tx_lock,flag);
6279 rtl8180_restart(dev);
6280 return;
6283 /* we check all the descriptors between the head and the nic,
6284 * but not the currenly pointed by the nic (the next to be txed)
6285 * and the previous of the pointed (might be in process ??)
6287 //if (head == nic) return;
6288 //DMESG("%x %x",head,nic);
6289 offs = (nic - nicbegin);
6290 //DMESG("%x %x %x",nic ,(u32)nicbegin, (int)nic -nicbegin);
6292 offs = offs / 8 /4;
6294 hd = (head - begin) /8;
6296 if(offs >= hd)
6297 j = offs - hd;
6298 else
6299 j = offs + (priv->txringcount -1 -hd);
6300 // j= priv->txringcount -1- (hd - offs);
6302 j-=2;
6303 if(j<0) j=0;
6306 for(i=0;i<j;i++)
6308 // printk("+++++++++++++check status desc\n");
6309 if((*head) & (1<<31))
6310 break;
6311 if(((*head)&(0x10000000)) != 0){
6312 // printk("++++++++++++++last desc,retry count is %d\n",((*head) & (0x000000ff)));
6313 priv->CurrRetryCnt += (u16)((*head) & (0x000000ff));
6314 #if 1
6315 if(!error)
6317 priv->NumTxOkTotal++;
6318 // printk("NumTxOkTotal is %d\n",priv->NumTxOkTotal++);
6320 #endif
6321 // printk("in function %s:curr_retry_count is %d\n",__func__,((*head) & (0x000000ff)));
6323 if(!error){
6324 priv->NumTxOkBytesTotal += (*(head+3)) & (0x00000fff);
6326 // printk("in function %s:curr_txokbyte_count is %d\n",__func__,(*(head+3)) & (0x00000fff));
6327 *head = *head &~ (1<<31);
6329 if((head - begin)/8 == priv->txringcount-1)
6330 head=begin;
6332 else
6333 head+=8;
6335 #if 0
6336 if(nicv == begin)
6337 txdv = begin + (priv->txringcount -1)*8;
6338 else
6339 txdv = nicv - 8;
6341 txed = !(txdv[0] &(1<<31));
6343 if(txed){
6344 if(!(txdv[0] & (1<<15))) error = 1;
6345 //if(!(txdv[0] & (1<<30))) error = 1;
6346 if(error)DMESG("%x",txdv[0]);
6348 #endif
6349 //DMESG("%x",txdv[0]);
6350 /* the head has been moved to the last certainly TXed
6351 * (or at least processed by the nic) packet.
6352 * The driver take forcefully owning of all these packets
6353 * If the packet previous of the nic pointer has been
6354 * processed this doesn't matter: it will be checked
6355 * here at the next round. Anyway if no more packet are
6356 * TXed no memory leak occour at all.
6359 switch(pri) {
6360 case MANAGE_PRIORITY:
6361 priv->txmapringhead = head;
6362 //printk("1==========================================> priority check!\n");
6363 if(priv->ack_tx_to_ieee){
6364 // try to implement power-save mode 2008.1.22
6365 // printk("2==========================================> priority check!\n");
6366 #if 1
6367 if(rtl8180_is_tx_queue_empty(dev)){
6368 // printk("tx queue empty, after send null sleep packet, try to sleep !\n");
6369 priv->ack_tx_to_ieee = 0;
6370 ieee80211_ps_tx_ack(priv->ieee80211,!error);
6372 #endif
6374 break;
6376 case BK_PRIORITY:
6377 priv->txbkpringhead = head;
6378 break;
6380 case BE_PRIORITY:
6381 priv->txbepringhead = head;
6382 break;
6384 case VI_PRIORITY:
6385 priv->txvipringhead = head;
6386 break;
6388 case VO_PRIORITY:
6389 priv->txvopringhead = head;
6390 break;
6392 case HI_PRIORITY:
6393 priv->txhpringhead = head;
6394 break;
6397 /*DMESG("%x %x %x", (priv->txnpringhead - priv->txnpring) /8 ,
6398 (priv->txnpringtail - priv->txnpring) /8,
6399 offs );
6402 spin_unlock_irqrestore(&priv->tx_lock,flag);
6406 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
6407 void rtl8180_tx_irq_wq(struct work_struct *work)
6409 //struct r8180_priv *priv = container_of(work, struct r8180_priv, reset_wq);
6410 struct delayed_work *dwork = container_of(work,struct delayed_work,work);
6411 struct ieee80211_device * ieee = (struct ieee80211_device*)
6412 container_of(dwork, struct ieee80211_device, watch_dog_wq);
6413 struct net_device *dev = ieee->dev;
6414 #else
6415 void rtl8180_tx_irq_wq(struct net_device *dev)
6417 //struct r8180_priv *priv = ieee80211_priv(dev);
6418 #endif
6419 rtl8180_tx_isr(dev,MANAGE_PRIORITY,0);
6421 irqreturn_t rtl8180_interrupt(int irq, void *netdev, struct pt_regs *regs)
6423 struct net_device *dev = (struct net_device *) netdev;
6424 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
6425 unsigned long flags;
6426 u32 inta;
6428 /* We should return IRQ_NONE, but for now let me keep this */
6429 if(priv->irq_enabled == 0) return IRQ_HANDLED;
6431 spin_lock_irqsave(&priv->irq_th_lock,flags);
6433 #ifdef CONFIG_RTL8185B
6434 //ISR: 4bytes
6435 inta = read_nic_dword(dev, ISR);// & priv->IntrMask;
6436 write_nic_dword(dev,ISR,inta); // reset int situation
6437 #else
6438 inta = read_nic_word(dev,INTA) & priv->irq_mask;
6439 write_nic_word(dev,INTA,inta); // reset int situation
6440 #endif
6442 priv->stats.shints++;
6444 //DMESG("Enter interrupt, ISR value = 0x%08x", inta);
6446 if(!inta){
6447 spin_unlock_irqrestore(&priv->irq_th_lock,flags);
6448 return IRQ_HANDLED;
6450 most probably we can safely return IRQ_NONE,
6451 but for now is better to avoid problems
6455 if(inta == 0xffff){
6456 /* HW disappared */
6457 spin_unlock_irqrestore(&priv->irq_th_lock,flags);
6458 return IRQ_HANDLED;
6461 priv->stats.ints++;
6462 #ifdef DEBUG_IRQ
6463 DMESG("NIC irq %x",inta);
6464 #endif
6465 //priv->irqpending = inta;
6468 if(!netif_running(dev)) {
6469 spin_unlock_irqrestore(&priv->irq_th_lock,flags);
6470 return IRQ_HANDLED;
6473 if(inta & ISR_TimeOut){
6474 write_nic_dword(dev, TimerInt, 0);
6475 //DMESG("=================>waking up");
6476 // rtl8180_hw_wakeup(dev);
6479 if(inta & ISR_TBDOK){
6480 priv->stats.txbeacon++;
6483 if(inta & ISR_TBDER){
6484 priv->stats.txbeaconerr++;
6487 if(inta & IMR_TMGDOK ) {
6488 // priv->NumTxOkTotal++;
6489 rtl8180_tx_isr(dev,MANAGE_PRIORITY,0);
6490 // schedule_work(&priv->tx_irq_wq);
6494 if(inta & ISR_THPDER){
6495 #ifdef DEBUG_TX
6496 DMESG ("TX high priority ERR");
6497 #endif
6498 priv->stats.txhperr++;
6499 rtl8180_tx_isr(dev,HI_PRIORITY,1);
6500 priv->ieee80211->stats.tx_errors++;
6503 if(inta & ISR_THPDOK){ //High priority tx ok
6504 #ifdef DEBUG_TX
6505 DMESG ("TX high priority OK");
6506 #endif
6507 // priv->NumTxOkTotal++;
6508 //priv->NumTxOkInPeriod++; //YJ,del,080828
6509 priv->link_detect.NumTxOkInPeriod++; //YJ,add,080828
6510 priv->stats.txhpokint++;
6511 rtl8180_tx_isr(dev,HI_PRIORITY,0);
6514 if(inta & ISR_RER) {
6515 priv->stats.rxerr++;
6516 #ifdef DEBUG_RX
6517 DMESGW("RX error int");
6518 #endif
6520 #ifdef CONFIG_RTL8185B
6521 if(inta & ISR_TBKDER){ //corresponding to BK_PRIORITY
6522 priv->stats.txbkperr++;
6523 priv->ieee80211->stats.tx_errors++;
6524 #ifdef DEBUG_TX
6525 DMESGW("TX bkp error int");
6526 #endif
6527 //tasklet_schedule(&priv->irq_tx_tasklet);
6528 rtl8180_tx_isr(dev,BK_PRIORITY,1);
6529 rtl8180_try_wake_queue(dev, BE_PRIORITY);
6532 if(inta & ISR_TBEDER){ //corresponding to BE_PRIORITY
6533 priv->stats.txbeperr++;
6534 priv->ieee80211->stats.tx_errors++;
6535 #ifdef DEBUG_TX
6536 DMESGW("TX bep error int");
6537 #endif
6538 rtl8180_tx_isr(dev,BE_PRIORITY,1);
6539 //tasklet_schedule(&priv->irq_tx_tasklet);
6540 rtl8180_try_wake_queue(dev, BE_PRIORITY);
6542 #endif
6543 if(inta & ISR_TNPDER){ //corresponding to VO_PRIORITY
6544 priv->stats.txnperr++;
6545 priv->ieee80211->stats.tx_errors++;
6546 #ifdef DEBUG_TX
6547 DMESGW("TX np error int");
6548 #endif
6549 //tasklet_schedule(&priv->irq_tx_tasklet);
6550 rtl8180_tx_isr(dev,NORM_PRIORITY,1);
6551 #ifdef CONFIG_RTL8185B
6552 rtl8180_try_wake_queue(dev, NORM_PRIORITY);
6553 #endif
6556 if(inta & ISR_TLPDER){ //corresponding to VI_PRIORITY
6557 priv->stats.txlperr++;
6558 priv->ieee80211->stats.tx_errors++;
6559 #ifdef DEBUG_TX
6560 DMESGW("TX lp error int");
6561 #endif
6562 rtl8180_tx_isr(dev,LOW_PRIORITY,1);
6563 //tasklet_schedule(&priv->irq_tx_tasklet);
6564 rtl8180_try_wake_queue(dev, LOW_PRIORITY);
6567 if(inta & ISR_ROK){
6568 #ifdef DEBUG_RX
6569 DMESG("Frame arrived !");
6570 #endif
6571 //priv->NumRxOkInPeriod++; //YJ,del,080828
6572 priv->stats.rxint++;
6573 tasklet_schedule(&priv->irq_rx_tasklet);
6576 if(inta & ISR_RQoSOK ){
6577 #ifdef DEBUG_RX
6578 DMESG("QoS Frame arrived !");
6579 #endif
6580 //priv->NumRxOkInPeriod++; //YJ,del,080828
6581 priv->stats.rxint++;
6582 tasklet_schedule(&priv->irq_rx_tasklet);
6584 if(inta & ISR_BcnInt) {
6585 //DMESG("Preparing Beacons");
6586 rtl8180_prepare_beacon(dev);
6589 if(inta & ISR_RDU){
6590 //#ifdef DEBUG_RX
6591 DMESGW("No RX descriptor available");
6592 priv->stats.rxrdu++;
6593 //#endif
6594 tasklet_schedule(&priv->irq_rx_tasklet);
6595 /*queue_work(priv->workqueue ,&priv->restart_work);*/
6598 if(inta & ISR_RXFOVW){
6599 #ifdef DEBUG_RX
6600 DMESGW("RX fifo overflow");
6601 #endif
6602 priv->stats.rxoverflow++;
6603 tasklet_schedule(&priv->irq_rx_tasklet);
6604 //queue_work(priv->workqueue ,&priv->restart_work);
6607 if(inta & ISR_TXFOVW) priv->stats.txoverflow++;
6609 if(inta & ISR_TNPDOK){ //Normal priority tx ok
6610 #ifdef DEBUG_TX
6611 DMESG ("TX normal priority OK");
6612 #endif
6613 // priv->NumTxOkTotal++;
6614 //priv->NumTxOkInPeriod++; //YJ,del,080828
6615 priv->link_detect.NumTxOkInPeriod++; //YJ,add,080828
6616 // priv->ieee80211->stats.tx_packets++;
6617 priv->stats.txnpokint++;
6618 rtl8180_tx_isr(dev,NORM_PRIORITY,0);
6621 if(inta & ISR_TLPDOK){ //Low priority tx ok
6622 #ifdef DEBUG_TX
6623 DMESG ("TX low priority OK");
6624 #endif
6625 // priv->NumTxOkTotal++;
6626 //priv->NumTxOkInPeriod++; //YJ,del,080828
6627 priv->link_detect.NumTxOkInPeriod++; //YJ,add,080828
6628 // priv->ieee80211->stats.tx_packets++;
6629 priv->stats.txlpokint++;
6630 rtl8180_tx_isr(dev,LOW_PRIORITY,0);
6631 rtl8180_try_wake_queue(dev, LOW_PRIORITY);
6634 #ifdef CONFIG_RTL8185B
6635 if(inta & ISR_TBKDOK){ //corresponding to BK_PRIORITY
6636 priv->stats.txbkpokint++;
6637 #ifdef DEBUG_TX
6638 DMESGW("TX bk priority ok");
6639 #endif
6640 // priv->NumTxOkTotal++;
6641 //priv->NumTxOkInPeriod++; //YJ,del,080828
6642 priv->link_detect.NumTxOkInPeriod++; //YJ,add,080828
6643 rtl8180_tx_isr(dev,BK_PRIORITY,0);
6644 rtl8180_try_wake_queue(dev, BE_PRIORITY);
6647 if(inta & ISR_TBEDOK){ //corresponding to BE_PRIORITY
6648 priv->stats.txbeperr++;
6649 #ifdef DEBUG_TX
6650 DMESGW("TX be priority ok");
6651 #endif
6652 // priv->NumTxOkTotal++;
6653 //priv->NumTxOkInPeriod++; //YJ,del,080828
6654 priv->link_detect.NumTxOkInPeriod++; //YJ,add,080828
6655 rtl8180_tx_isr(dev,BE_PRIORITY,0);
6656 rtl8180_try_wake_queue(dev, BE_PRIORITY);
6658 #endif
6659 force_pci_posting(dev);
6660 spin_unlock_irqrestore(&priv->irq_th_lock,flags);
6662 return IRQ_HANDLED;
6666 void rtl8180_irq_rx_tasklet(struct r8180_priv* priv)
6668 // unsigned long flags;
6670 /* spin_lock_irqsave(&priv->irq_lock, flags);
6671 priv->irq_mask &=~IMR_ROK;
6672 priv->irq_mask &=~IMR_RDU;
6674 rtl8180_irq_enable(priv->dev);
6675 spin_unlock_irqrestore(&priv->irq_lock, flags);
6677 rtl8180_rx(priv->dev);
6679 /* spin_lock_irqsave(&priv->irq_lock, flags);
6680 priv->irq_mask |= IMR_ROK;
6681 priv->irq_mask |= IMR_RDU;
6682 rtl8180_irq_enable(priv->dev);
6683 spin_unlock_irqrestore(&priv->irq_lock, flags);
6687 /****************************************************************************
6688 lizhaoming--------------------------- RF power on/power off -----------------
6689 *****************************************************************************/
6691 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
6692 void GPIOChangeRFWorkItemCallBack(struct work_struct *work)
6694 //struct delayed_work *dwork = container_of(work, struct delayed_work, work);
6695 struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, GPIOChangeRFWorkItem.work);
6696 struct net_device *dev = ieee->dev;
6697 struct r8180_priv *priv = ieee80211_priv(dev);
6698 #else
6699 void GPIOChangeRFWorkItemCallBack(struct ieee80211_device *ieee)
6701 struct net_device *dev = ieee->dev;
6702 struct r8180_priv *priv = ieee80211_priv(dev);
6703 #endif
6705 //u16 tmp2byte;
6706 u8 btPSR;
6707 u8 btConfig0;
6708 RT_RF_POWER_STATE eRfPowerStateToSet;
6709 bool bActuallySet=false;
6711 char *argv[3];
6712 static char *RadioPowerPath = "/etc/acpi/events/RadioPower.sh";
6713 static char *envp[] = {"HOME=/", "TERM=linux", "PATH=/usr/bin:/bin", NULL};
6714 static int readf_count = 0;
6715 //printk("============>%s in \n", __func__);
6717 #ifdef ENABLE_LPS
6718 if(readf_count % 10 == 0)
6719 priv->PowerProfile = read_acadapter_file("/proc/acpi/ac_adapter/AC0/state");
6721 readf_count = (readf_count+1)%0xffff;
6722 #endif
6723 #if 0
6724 if(priv->up == 0)//driver stopped
6726 printk("\nDo nothing...");
6727 goto out;
6729 else
6730 #endif
6732 // We should turn off LED before polling FF51[4].
6734 //Turn off LED.
6735 btPSR = read_nic_byte(dev, PSR);
6736 write_nic_byte(dev, PSR, (btPSR & ~BIT3));
6738 //It need to delay 4us suggested by Jong, 2008-01-16
6739 udelay(4);
6741 //HW radio On/Off according to the value of FF51[4](config0)
6742 btConfig0 = btPSR = read_nic_byte(dev, CONFIG0);
6744 //Turn on LED.
6745 write_nic_byte(dev, PSR, btPSR| BIT3);
6747 eRfPowerStateToSet = (btConfig0 & BIT4) ? eRfOn : eRfOff;
6749 if((priv->ieee80211->bHwRadioOff == true) && (eRfPowerStateToSet == eRfOn))
6751 priv->ieee80211->bHwRadioOff = false;
6752 bActuallySet = true;
6754 else if((priv->ieee80211->bHwRadioOff == false) && (eRfPowerStateToSet == eRfOff))
6756 priv->ieee80211->bHwRadioOff = true;
6757 bActuallySet = true;
6760 if(bActuallySet)
6762 MgntActSet_RF_State(dev, eRfPowerStateToSet, RF_CHANGE_BY_HW);
6764 /* To update the UI status for Power status changed */
6765 if(priv->ieee80211->bHwRadioOff == true)
6766 argv[1] = "RFOFF";
6767 else{
6768 //if(!priv->RfOffReason)
6769 argv[1] = "RFON";
6770 //else
6771 // argv[1] = "RFOFF";
6773 argv[0] = RadioPowerPath;
6774 argv[2] = NULL;
6776 call_usermodehelper(RadioPowerPath,argv,envp,1);
6783 static u8 read_acadapter_file(char *filename)
6785 //#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21))
6786 #if 0
6787 int fd;
6788 char buf[1];
6789 char ret[50];
6790 int i = 0;
6791 int n = 0;
6792 mm_segment_t old_fs = get_fs();
6793 set_fs(KERNEL_DS);
6795 fd = sys_open(filename, O_RDONLY, 0);
6796 if (fd >= 0) {
6797 while (sys_read(fd, buf, 1) == 1)
6799 i++;
6800 if(i>10)
6802 if(buf[0]!=' ')
6804 ret[n]=buf[0];
6805 n++;
6809 sys_close(fd);
6811 ret[n]='\0';
6812 // printk("%s \n", ret);
6813 set_fs(old_fs);
6815 if(strncmp(ret, "off-line",8) == 0)
6817 return 1;
6819 #endif
6820 return 0;
6823 /***************************************************************************
6824 ------------------- module init / exit stubs ----------------
6825 ****************************************************************************/
6826 module_init(rtl8180_pci_module_init);
6827 module_exit(rtl8180_pci_module_exit);