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
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.
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;}
38 #undef RX_DONT_PASS_UL
40 #undef DEBUG_RX_VERBOSE
46 #undef DEBUG_TX_FILLDESC
51 #undef DEBUG_REGISTERS
53 #undef DEBUG_IRQ_TASKLET
58 //#define DEBUG_TX_DESC2
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>
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 */
77 #ifdef CONFIG_RTL8180_PM
82 #include "ieee80211/dot11d.h"
85 #ifdef CONFIG_RTL8185B
86 //#define CONFIG_RTL8180_IO_MAP
89 #ifndef PCI_VENDOR_ID_BELKIN
90 #define PCI_VENDOR_ID_BELKIN 0x1799
92 #ifndef PCI_VENDOR_ID_DLINK
93 #define PCI_VENDOR_ID_DLINK 0x1186
96 static struct pci_device_id rtl8180_pci_id_tbl[] __devinitdata = {
98 .vendor = PCI_VENDOR_ID_REALTEK,
101 .subvendor = PCI_ANY_ID,
102 .subdevice = PCI_ANY_ID,
107 .vendor = PCI_VENDOR_ID_BELKIN,
109 .subvendor = PCI_ANY_ID,
110 .subdevice = PCI_ANY_ID,
113 { /* Belkin F5D6020 v3 */
114 .vendor = PCI_VENDOR_ID_BELKIN,
116 .subvendor = PCI_ANY_ID,
117 .subdevice = PCI_ANY_ID,
120 { /* D-Link DWL-610 */
121 .vendor = PCI_VENDOR_ID_DLINK,
123 .subvendor = PCI_ANY_ID,
124 .subdevice = PCI_ANY_ID,
128 .vendor = PCI_VENDOR_ID_REALTEK,
130 .subvendor = PCI_ANY_ID,
131 .subdevice = PCI_ANY_ID,
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 module_param(ifname, charp, S_IRUGO|S_IWUSR );
175 module_param(hwseqnum,int, S_IRUGO|S_IWUSR);
176 module_param(hwwep,int, S_IRUGO|S_IWUSR);
177 module_param(channels,int, S_IRUGO|S_IWUSR);
179 MODULE_PARM_DESC(devname," Net interface name, wlan%d=default");
180 //MODULE_PARM_DESC(devname," Net interface name, ath%d=default");
181 MODULE_PARM_DESC(hwseqnum," Try to use hardware 802.11 header sequence numbers. Zero=default");
182 MODULE_PARM_DESC(hwwep," Try to use hardware WEP support. Still broken and not available on all cards");
183 MODULE_PARM_DESC(channels," Channel bitmask for specific locales. NYI");
186 static int __devinit rtl8180_pci_probe(struct pci_dev *pdev,
187 const struct pci_device_id *id);
189 static void __devexit rtl8180_pci_remove(struct pci_dev *pdev);
191 static void rtl8180_shutdown (struct pci_dev *pdev)
193 struct net_device *dev = pci_get_drvdata(pdev);
194 if (dev->netdev_ops->ndo_stop)
195 dev->netdev_ops->ndo_stop(dev);
196 pci_disable_device(pdev);
199 static struct pci_driver rtl8180_pci_driver = {
200 .name = RTL8180_MODULE_NAME, /* Driver name */
201 .id_table = rtl8180_pci_id_tbl, /* PCI_ID table */
202 .probe = rtl8180_pci_probe, /* probe fn */
203 .remove = __devexit_p(rtl8180_pci_remove),/* remove fn */
204 #ifdef CONFIG_RTL8180_PM
205 .suspend = rtl8180_suspend, /* PM suspend fn */
206 .resume = rtl8180_resume, /* PM resume fn */
208 .suspend = NULL, /* PM suspend fn */
209 .resume = NULL, /* PM resume fn */
211 .shutdown = rtl8180_shutdown,
216 #ifdef CONFIG_RTL8180_IO_MAP
218 u8 read_nic_byte(struct net_device *dev, int x)
220 return 0xff&inb(dev->base_addr +x);
223 u32 read_nic_dword(struct net_device *dev, int x)
225 return inl(dev->base_addr +x);
228 u16 read_nic_word(struct net_device *dev, int x)
230 return inw(dev->base_addr +x);
233 void write_nic_byte(struct net_device *dev, int x,u8 y)
235 outb(y&0xff,dev->base_addr +x);
238 void write_nic_word(struct net_device *dev, int x,u16 y)
240 outw(y,dev->base_addr +x);
243 void write_nic_dword(struct net_device *dev, int x,u32 y)
245 outl(y,dev->base_addr +x);
248 #else /* RTL_IO_MAP */
250 u8 read_nic_byte(struct net_device *dev, int x)
252 return 0xff&readb((u8*)dev->mem_start +x);
255 u32 read_nic_dword(struct net_device *dev, int x)
257 return readl((u8*)dev->mem_start +x);
260 u16 read_nic_word(struct net_device *dev, int x)
262 return readw((u8*)dev->mem_start +x);
265 void write_nic_byte(struct net_device *dev, int x,u8 y)
267 writeb(y,(u8*)dev->mem_start +x);
271 void write_nic_dword(struct net_device *dev, int x,u32 y)
273 writel(y,(u8*)dev->mem_start +x);
277 void write_nic_word(struct net_device *dev, int x,u16 y)
279 writew(y,(u8*)dev->mem_start +x);
283 #endif /* RTL_IO_MAP */
289 inline void force_pci_posting(struct net_device *dev)
291 read_nic_byte(dev,EPROM_CMD);
292 #ifndef CONFIG_RTL8180_IO_MAP
298 irqreturn_t rtl8180_interrupt(int irq, void *netdev, struct pt_regs *regs);
299 void set_nic_rxring(struct net_device *dev);
300 void set_nic_txring(struct net_device *dev);
301 static struct net_device_stats *rtl8180_stats(struct net_device *dev);
302 void rtl8180_commit(struct net_device *dev);
303 void rtl8180_start_tx_beacon(struct net_device *dev);
305 /****************************************************************************
306 -----------------------------PROCFS STUFF-------------------------
307 *****************************************************************************/
309 static struct proc_dir_entry *rtl8180_proc = NULL;
311 static int proc_get_registers(char *page, char **start,
312 off_t offset, int count,
313 int *eof, void *data)
315 struct net_device *dev = data;
316 // struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
323 /* This dump the current register page */
326 //printk( "\nD: %2x> ", n);
327 len += snprintf(page + len, count - len,
330 for(i=0;i<16 && n<=max;i++,n++)
331 len += snprintf(page + len, count - len,
332 "%2x ",read_nic_byte(dev,n));
334 // printk("%2x ",read_nic_byte(dev,n));
336 len += snprintf(page + len, count - len,"\n");
345 int get_curr_tx_free_desc(struct net_device *dev, int priority);
347 static int proc_get_stats_hw(char *page, char **start,
348 off_t offset, int count,
349 int *eof, void *data)
351 //struct net_device *dev = data;
352 //struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
355 #ifdef CONFIG_RTL8185B
358 len += snprintf(page + len, count - len,
361 "--------------------\n"
364 "--------------------\n"
365 "LP phys dma addr %x\n"
367 "LP virt 32base %x\n"
368 "LP virt 32tail %x\n"
369 "--------------------\n"
370 "NP phys dma addr %x\n"
372 "NP virt 32base %x\n"
373 "NP virt 32tail %x\n"
374 "--------------------\n"
375 "BP phys dma addr %x\n"
377 "BP virt 32base %x\n"
378 "BP virt 32tail %x\n",
381 get_curr_tx_free_desc(dev,LOW_PRIORITY),
382 get_curr_tx_free_desc(dev,NORM_PRIORITY),
383 (u32)priv->txvipringdma,
384 read_nic_dword(dev,TLPDA),
385 (u32)priv->txvipring,
386 (u32)priv->txvipringtail,
387 (u32)priv->txvopringdma,
388 read_nic_dword(dev,TNPDA),
389 (u32)priv->txvopring,
390 (u32)priv->txvopringtail,
391 (u32)priv->txbeaconringdma,
392 read_nic_dword(dev,TBDA),
393 (u32)priv->txbeaconring,
394 (u32)priv->txbeaconringtail);
401 static int proc_get_stats_rx(char *page, char **start,
402 off_t offset, int count,
403 int *eof, void *data)
405 struct net_device *dev = data;
406 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
410 len += snprintf(page + len, count - len,
411 /* "RX descriptor not available: %lu\n"
412 "RX incomplete (missing last descriptor): %lu\n"
414 //"RX descriptor pointer reset: %lu\n"
415 "RX descriptor pointer lost: %lu\n"
416 //"RX pointer workaround: %lu\n"
417 "RX error int: %lu\n"
418 "RX fifo overflow: %lu\n"
422 "RX DMA fail: %lu\n",
424 priv->stats.rxnolast,
425 priv->stats.rxnodata,
426 //priv->stats.rxreset,
427 priv->stats.rxnopointer,
428 //priv->stats.rxwrkaround,
430 priv->stats.rxoverflow,
432 priv->ieee80211->stats.rx_packets,
433 priv->ieee80211->stats.rx_bytes,
434 priv->stats.rxdmafail */
437 "RX CRC Error(0-500): %lu\n"
438 "RX CRC Error(500-1000): %lu\n"
439 "RX CRC Error(>1000): %lu\n"
440 "RX ICV Error: %lu\n",
443 priv->stats.rxcrcerrmin,
444 priv->stats.rxcrcerrmid,
445 priv->stats.rxcrcerrmax,
454 static int proc_get_stats_ieee(char *page, char **start,
455 off_t offset, int count,
456 int *eof, void *data)
458 struct net_device *dev = data;
459 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
463 len += snprintf(page + len, count - len,
464 "TXed association requests: %u\n"
465 "TXed authentication requests: %u\n"
466 "RXed successful association response: %u\n"
467 "RXed failed association response: %u\n"
468 "RXed successful authentication response: %u\n"
469 "RXed failed authentication response: %u\n"
470 "Association requests without response: %u\n"
471 "Authentication requests without response: %u\n"
472 "TX probe response: %u\n"
473 "RX probe request: %u\n"
474 "TX probe request: %lu\n"
475 "RX authentication requests: %lu\n"
476 "RX association requests: %lu\n"
477 "Reassociations: %lu\n",
478 priv->ieee80211->ieee_stats.tx_ass,
479 priv->ieee80211->ieee_stats.tx_aut,
480 priv->ieee80211->ieee_stats.rx_ass_ok,
481 priv->ieee80211->ieee_stats.rx_ass_err,
482 priv->ieee80211->ieee_stats.rx_aut_ok,
483 priv->ieee80211->ieee_stats.rx_aut_err,
484 priv->ieee80211->ieee_stats.ass_noresp,
485 priv->ieee80211->ieee_stats.aut_noresp,
486 priv->ieee80211->ieee_stats.tx_probe,
487 priv->ieee80211->ieee_stats.rx_probe,
488 priv->ieee80211->ieee_stats.tx_probe_rq,
489 priv->ieee80211->ieee_stats.rx_auth_rq,
490 priv->ieee80211->ieee_stats.rx_assoc_rq,
491 priv->ieee80211->ieee_stats.reassoc);
498 static int proc_get_stats_ap(char *page, char **start,
499 off_t offset, int count,
500 int *eof, void *data)
502 struct net_device *dev = data;
503 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
504 struct mac_htable_t *list;
508 if(priv->ieee80211->iw_mode != IW_MODE_MASTER){
509 len += snprintf(page + len, count - len,
510 "Card is not acting as AP...\n"
513 len += snprintf(page + len, count - len,
514 "List of associated STA:\n"
517 for(i=0;i<MAC_HTABLE_ENTRY;i++)
518 for (list = priv->ieee80211->assoc_htable[i]; list!=NULL; list = list->next){
519 len += snprintf(page + len, count - len,
520 MACSTR"\n",MAC2STR(list->adr));
529 static int proc_get_stats_tx(char *page, char **start,
530 off_t offset, int count,
531 int *eof, void *data)
533 struct net_device *dev = data;
534 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
537 unsigned long totalOK;
539 totalOK=priv->stats.txnpokint+priv->stats.txhpokint+priv->stats.txlpokint;
540 len += snprintf(page + len, count - len,
541 /* "TX normal priority ok int: %lu\n"
542 "TX normal priority error int: %lu\n"
543 "TX high priority ok int: %lu\n"
544 "TX high priority failed error int: %lu\n"
545 "TX low priority ok int: %lu\n"
546 "TX low priority failed error int: %lu\n"
549 "TX queue resume: %lu\n"
550 "TX queue stopped?: %d\n"
551 "TX fifo overflow: %lu\n"
552 //"SW TX stop: %lu\n"
553 //"SW TX wake: %lu\n"
555 "TX beacon aborted: %lu\n",
556 priv->stats.txnpokint,
558 priv->stats.txhpokint,
560 priv->stats.txlpokint,
562 priv->ieee80211->stats.tx_bytes,
563 priv->ieee80211->stats.tx_packets,
564 priv->stats.txresumed,
565 netif_queue_stopped(dev),
566 priv->stats.txoverflow,
567 //priv->ieee80211->ieee_stats.swtxstop,
568 //priv->ieee80211->ieee_stats.swtxawake,
569 priv->stats.txbeacon,
570 priv->stats.txbeaconerr */
574 "TX beacon OK: %lu\n"
575 "TX beacon error: %lu\n",
577 priv->stats.txnperr+priv->stats.txhperr+priv->stats.txlperr,
579 priv->stats.txbeacon,
580 priv->stats.txbeaconerr
588 #if WIRELESS_EXT < 17
589 static struct iw_statistics *r8180_get_wireless_stats(struct net_device *dev)
591 struct r8180_priv *priv = ieee80211_priv(dev);
593 return &priv->wstats;
596 void rtl8180_proc_module_init(void)
598 DMESG("Initializing proc filesystem");
599 rtl8180_proc=create_proc_entry(RTL8180_MODULE_NAME, S_IFDIR, init_net.proc_net);
603 void rtl8180_proc_module_remove(void)
605 remove_proc_entry(RTL8180_MODULE_NAME, init_net.proc_net);
609 void rtl8180_proc_remove_one(struct net_device *dev)
611 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
613 remove_proc_entry("stats-hw", priv->dir_dev);
614 remove_proc_entry("stats-tx", priv->dir_dev);
615 remove_proc_entry("stats-rx", priv->dir_dev);
616 // remove_proc_entry("stats-ieee", priv->dir_dev);
617 // remove_proc_entry("stats-ap", priv->dir_dev);
618 remove_proc_entry("registers", priv->dir_dev);
619 remove_proc_entry(dev->name, rtl8180_proc);
620 priv->dir_dev = NULL;
625 void rtl8180_proc_init_one(struct net_device *dev)
627 struct proc_dir_entry *e;
628 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
629 priv->dir_dev = rtl8180_proc;
630 if (!priv->dir_dev) {
631 DMESGE("Unable to initialize /proc/net/r8180/%s\n",
636 e = create_proc_read_entry("stats-hw", S_IFREG | S_IRUGO,
637 priv->dir_dev, proc_get_stats_hw, dev);
640 DMESGE("Unable to initialize "
641 "/proc/net/r8180/%s/stats-hw\n",
645 e = create_proc_read_entry("stats-rx", S_IFREG | S_IRUGO,
646 priv->dir_dev, proc_get_stats_rx, dev);
649 DMESGE("Unable to initialize "
650 "/proc/net/r8180/%s/stats-rx\n",
655 e = create_proc_read_entry("stats-tx", S_IFREG | S_IRUGO,
656 priv->dir_dev, proc_get_stats_tx, dev);
659 DMESGE("Unable to initialize "
660 "/proc/net/r8180/%s/stats-tx\n",
664 e = create_proc_read_entry("stats-ieee", S_IFREG | S_IRUGO,
665 priv->dir_dev, proc_get_stats_ieee, dev);
668 DMESGE("Unable to initialize "
669 "/proc/net/rtl8180/%s/stats-ieee\n",
674 e = create_proc_read_entry("stats-ap", S_IFREG | S_IRUGO,
675 priv->dir_dev, proc_get_stats_ap, dev);
678 DMESGE("Unable to initialize "
679 "/proc/net/rtl8180/%s/stats-ap\n",
684 e = create_proc_read_entry("registers", S_IFREG | S_IRUGO,
685 priv->dir_dev, proc_get_registers, dev);
688 DMESGE("Unable to initialize "
689 "/proc/net/r8180/%s/registers\n",
693 /****************************************************************************
694 -----------------------------MISC STUFF-------------------------
695 *****************************************************************************/
697 FIXME: check if we can use some standard already-existent
698 data type+functions in kernel
701 short buffer_add(struct buffer **buffer, u32 *buf, dma_addr_t dma,
702 struct buffer **bufferhead)
705 DMESG("adding buffer to TX/RX struct");
712 *buffer = kmalloc(sizeof(struct buffer),GFP_KERNEL);
714 if (*buffer == NULL) {
715 DMESGE("Failed to kmalloc head of TX/RX struct");
718 (*buffer)->next=*buffer;
721 if(bufferhead !=NULL)
722 (*bufferhead) = (*buffer);
727 while(tmp->next!=(*buffer)) tmp=tmp->next;
728 if ((tmp->next= kmalloc(sizeof(struct buffer),GFP_KERNEL)) == NULL){
729 DMESGE("Failed to kmalloc TX/RX struct");
734 tmp->next->next=*buffer;
740 void buffer_free(struct net_device *dev,struct buffer **buffer,int len,short
744 struct buffer *tmp,*next;
745 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
746 struct pci_dev *pdev=priv->pdev;
749 if(! *buffer) return;
751 /*for(tmp=*buffer; tmp->next != *buffer; tmp=tmp->next)
758 pci_free_consistent(pdev,len,
761 pci_unmap_single(pdev, tmp->dma,
762 len,PCI_DMA_FROMDEVICE);
768 while(next != *buffer);
774 void print_buffer(u32 *buffer, int len)
777 u8 *buf =(u8*)buffer;
779 printk("ASCII BUFFER DUMP (len: %x):\n",len);
784 printk("\nBINARY BUFFER DUMP (len: %x):\n",len);
787 printk("%02x",buf[i]);
793 int get_curr_tx_free_desc(struct net_device *dev, int priority)
795 struct r8180_priv *priv = ieee80211_priv(dev);
801 case MANAGE_PRIORITY:
802 head = priv->txmapringhead;
803 tail = priv->txmapringtail;
806 head = priv->txbkpringhead;
807 tail = priv->txbkpringtail;
810 head = priv->txbepringhead;
811 tail = priv->txbepringtail;
814 head = priv->txvipringhead;
815 tail = priv->txvipringtail;
818 head = priv->txvopringhead;
819 tail = priv->txvopringtail;
822 head = priv->txhpringhead;
823 tail = priv->txhpringtail;
829 //DMESG("%x %x", head, tail);
831 /* FIXME FIXME FIXME FIXME */
834 if( head <= tail ) return priv->txringcount-1 - (tail - head)/8;
835 return (head - tail)/8/4;
838 ret = priv->txringcount - (tail - head)/8;
840 ret = (head - tail)/8;
842 if(ret > priv->txringcount ) DMESG("BUG");
848 short check_nic_enought_desc(struct net_device *dev, int priority)
850 struct r8180_priv *priv = ieee80211_priv(dev);
851 struct ieee80211_device *ieee = netdev_priv(dev);
853 int requiredbyte, required;
854 requiredbyte = priv->ieee80211->fts + sizeof(struct ieee80211_header_data);
856 if(ieee->current_network.QoS_Enable) {
860 required = requiredbyte / (priv->txbuffsize-4);
861 if (requiredbyte % priv->txbuffsize) required++;
862 /* for now we keep two free descriptor as a safety boundary
863 * between the tail and the head
866 return (required+2 < get_curr_tx_free_desc(dev,priority));
870 /* This function is only for debuging purpose */
871 void check_tx_ring(struct net_device *dev, int pri)
873 static int maxlog =3;
874 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
883 struct buffer* buffer;
886 if (maxlog <0 ) return;
889 case MANAGE_PRIORITY:
890 tail = priv->txmapringtail;
891 begin = priv->txmapring;
892 head = priv->txmapringhead;
893 nic = read_nic_dword(dev,TX_MANAGEPRIORITY_RING_ADDR);
894 buffer = priv->txmapbufs;
895 nicbegin = priv->txmapringdma;
900 tail = priv->txbkpringtail;
901 begin = priv->txbkpring;
902 head = priv->txbkpringhead;
903 nic = read_nic_dword(dev,TX_BKPRIORITY_RING_ADDR);
904 buffer = priv->txbkpbufs;
905 nicbegin = priv->txbkpringdma;
909 tail = priv->txbepringtail;
910 begin = priv->txbepring;
911 head = priv->txbepringhead;
912 nic = read_nic_dword(dev,TX_BEPRIORITY_RING_ADDR);
913 buffer = priv->txbepbufs;
914 nicbegin = priv->txbepringdma;
918 tail = priv->txvipringtail;
919 begin = priv->txvipring;
920 head = priv->txvipringhead;
921 nic = read_nic_dword(dev,TX_VIPRIORITY_RING_ADDR);
922 buffer = priv->txvipbufs;
923 nicbegin = priv->txvipringdma;
928 tail = priv->txvopringtail;
929 begin = priv->txvopring;
930 head = priv->txvopringhead;
931 nic = read_nic_dword(dev,TX_VOPRIORITY_RING_ADDR);
932 buffer = priv->txvopbufs;
933 nicbegin = priv->txvopringdma;
937 tail = priv->txhpringtail;
938 begin = priv->txhpring;
939 head = priv->txhpringhead;
940 nic = read_nic_dword(dev,TX_HIGHPRIORITY_RING_ADDR);
941 buffer = priv->txhpbufs;
942 nicbegin = priv->txhpringdma;
951 DMESGE ("NIC TX ack, but TX queue corrupted!");
954 for(i=0,buf=buffer, tmp=begin;
955 tmp<begin+(priv->txringcount)*8;
956 tmp+=8,buf=buf->next,i++)
958 DMESG("BUF%d %s %x %s. Next : %x",i,
959 *tmp & (1<<31) ? "filled" : "empty",
961 *tmp & (1<<15)? "ok": "err", *(tmp+4));
969 /* this function is only for debugging purpose */
970 void check_rxbuf(struct net_device *dev)
972 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
977 #ifdef CONFIG_RTL8185B
984 DMESGE ("NIC RX ack, but RX queue corrupted!");
988 for(buf=priv->rxbuffer, tmp=priv->rxring;
989 tmp < priv->rxring+(priv->rxringcount)*rx_desc_size;
990 tmp+=rx_desc_size, buf=buf->next)
993 *tmp & (1<<31) ? "empty" : "filled",
1001 void dump_eprom(struct net_device *dev)
1005 DMESG("EEPROM addr %x : %x", i, eprom_read(dev,i));
1009 void rtl8180_dump_reg(struct net_device *dev)
1015 DMESG("Dumping NIC register map");
1019 printk( "\nD: %2x> ", n);
1020 for(i=0;i<16 && n<=max;i++,n++)
1021 printk("%2x ",read_nic_byte(dev,n));
1027 void fix_tx_fifo(struct net_device *dev)
1029 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1032 #ifdef DEBUG_TX_ALLOC
1033 DMESG("FIXING TX FIFOs");
1035 for (tmp=priv->txmapring, i=0;
1036 i < priv->txringcount;
1038 *tmp = *tmp &~ (1<<31);
1041 for (tmp=priv->txbkpring, i=0;
1042 i < priv->txringcount;
1044 *tmp = *tmp &~ (1<<31);
1047 for (tmp=priv->txbepring, i=0;
1048 i < priv->txringcount;
1050 *tmp = *tmp &~ (1<<31);
1052 for (tmp=priv->txvipring, i=0;
1053 i < priv->txringcount;
1055 *tmp = *tmp &~ (1<<31);
1058 for (tmp=priv->txvopring, i=0;
1059 i < priv->txringcount;
1061 *tmp = *tmp &~ (1<<31);
1064 for (tmp=priv->txhpring, i=0;
1065 i < priv->txringcount;
1067 *tmp = *tmp &~ (1<<31);
1070 for (tmp=priv->txbeaconring, i=0;
1071 i < priv->txbeaconcount;
1073 *tmp = *tmp &~ (1<<31);
1075 #ifdef DEBUG_TX_ALLOC
1076 DMESG("TX FIFOs FIXED");
1078 priv->txmapringtail = priv->txmapring;
1079 priv->txmapringhead = priv->txmapring;
1080 priv->txmapbufstail = priv->txmapbufs;
1082 priv->txbkpringtail = priv->txbkpring;
1083 priv->txbkpringhead = priv->txbkpring;
1084 priv->txbkpbufstail = priv->txbkpbufs;
1086 priv->txbepringtail = priv->txbepring;
1087 priv->txbepringhead = priv->txbepring;
1088 priv->txbepbufstail = priv->txbepbufs;
1090 priv->txvipringtail = priv->txvipring;
1091 priv->txvipringhead = priv->txvipring;
1092 priv->txvipbufstail = priv->txvipbufs;
1094 priv->txvopringtail = priv->txvopring;
1095 priv->txvopringhead = priv->txvopring;
1096 priv->txvopbufstail = priv->txvopbufs;
1098 priv->txhpringtail = priv->txhpring;
1099 priv->txhpringhead = priv->txhpring;
1100 priv->txhpbufstail = priv->txhpbufs;
1102 priv->txbeaconringtail = priv->txbeaconring;
1103 priv->txbeaconbufstail = priv->txbeaconbufs;
1104 set_nic_txring(dev);
1106 ieee80211_reset_queue(priv->ieee80211);
1107 priv->ack_tx_to_ieee = 0;
1111 void fix_rx_fifo(struct net_device *dev)
1113 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1115 struct buffer *rxbuf;
1118 #ifdef CONFIG_RTL8185B
1119 rx_desc_size = 8; // 4*8 = 32 bytes
1124 #ifdef DEBUG_RXALLOC
1125 DMESG("FIXING RX FIFO");
1129 for (tmp=priv->rxring, rxbuf=priv->rxbufferhead;
1130 (tmp < (priv->rxring)+(priv->rxringcount)*rx_desc_size);
1131 tmp+=rx_desc_size,rxbuf=rxbuf->next){
1132 *(tmp+2) = rxbuf->dma;
1134 *tmp=*tmp | priv->rxbuffersize;
1138 #ifdef DEBUG_RXALLOC
1139 DMESG("RX FIFO FIXED");
1143 priv->rxringtail=priv->rxring;
1144 priv->rxbuffer=priv->rxbufferhead;
1145 priv->rx_skb_complete=1;
1146 set_nic_rxring(dev);
1150 /****************************************************************************
1151 ------------------------------HW STUFF---------------------------
1152 *****************************************************************************/
1154 unsigned char QUALITY_MAP[] = {
1155 0x64, 0x64, 0x64, 0x63, 0x63, 0x62, 0x62, 0x61,
1156 0x61, 0x60, 0x60, 0x5f, 0x5f, 0x5e, 0x5d, 0x5c,
1157 0x5b, 0x5a, 0x59, 0x57, 0x56, 0x54, 0x52, 0x4f,
1158 0x4c, 0x49, 0x45, 0x41, 0x3c, 0x37, 0x31, 0x29,
1159 0x24, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
1160 0x22, 0x22, 0x21, 0x21, 0x21, 0x21, 0x21, 0x20,
1161 0x20, 0x20, 0x20, 0x1f, 0x1f, 0x1e, 0x1e, 0x1e,
1162 0x1d, 0x1d, 0x1c, 0x1c, 0x1b, 0x1a, 0x19, 0x19,
1163 0x18, 0x17, 0x16, 0x15, 0x14, 0x12, 0x11, 0x0f,
1164 0x0e, 0x0c, 0x0a, 0x08, 0x06, 0x04, 0x01, 0x00
1167 unsigned char STRENGTH_MAP[] = {
1168 0x64, 0x64, 0x63, 0x62, 0x61, 0x60, 0x5f, 0x5e,
1169 0x5d, 0x5c, 0x5b, 0x5a, 0x57, 0x54, 0x52, 0x50,
1170 0x4e, 0x4c, 0x4a, 0x48, 0x46, 0x44, 0x41, 0x3f,
1171 0x3c, 0x3a, 0x37, 0x36, 0x36, 0x1c, 0x1c, 0x1b,
1172 0x1b, 0x1a, 0x1a, 0x19, 0x19, 0x18, 0x18, 0x17,
1173 0x17, 0x16, 0x16, 0x15, 0x15, 0x14, 0x14, 0x13,
1174 0x13, 0x12, 0x12, 0x11, 0x11, 0x10, 0x10, 0x0f,
1175 0x0f, 0x0e, 0x0e, 0x0d, 0x0d, 0x0c, 0x0c, 0x0b,
1176 0x0b, 0x0a, 0x0a, 0x09, 0x09, 0x08, 0x08, 0x07,
1177 0x07, 0x06, 0x06, 0x05, 0x04, 0x03, 0x02, 0x00
1180 void rtl8180_RSSI_calc(struct net_device *dev, u8 *rssi, u8 *qual){
1181 //void Mlme_UpdateRssiSQ(struct net_device *dev, u8 *rssi, u8 *qual){
1182 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1193 _rssi = 0; // avoid gcc complains..
1196 temp = QUALITY_MAP[q];
1208 switch(priv->rf_chip){
1212 if ( !lsb || !(temp2 <= 0x3c) ) {
1215 temp2 = 100 * temp2 / 0x3c;
1217 *rssi = temp2 & 0xff;
1218 _rssi = temp2 & 0xff;
1220 case RFCHIPID_INTERSIL:
1222 temp2 &= 0xfffffffe;
1231 if ( temp2 <= 0x3e0000 ) {
1232 if ( temp2 < 0xffef0000 )
1246 temp3 = temp3 / 0x6d;
1248 _rssi = temp3 & 0xff;
1249 *rssi = temp3 & 0xff;
1254 if ( ! lsb || !(temp2 <= 0x3c) ){
1257 temp2 = (100 * temp2) / 0x3c;
1259 *rssi = temp2 & 0xff;
1260 _rssi = temp2 & 0xff;
1262 case RFCHIPID_PHILIPS:
1263 if( orig_qual <= 0x4e ){
1264 _rssi = STRENGTH_MAP[orig_qual];
1279 case RFCHIPID_MAXIM:
1287 *rssi = temp2 & 0xff;
1288 _rssi = temp2 & 0xff;
1292 if ( _rssi < 0x64 ){
1304 void rtl8180_irq_enable(struct net_device *dev)
1306 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1307 priv->irq_enabled = 1;
1309 write_nic_word(dev,INTA_MASK,INTA_RXOK | INTA_RXDESCERR | INTA_RXOVERFLOW |\
1310 INTA_TXOVERFLOW | INTA_HIPRIORITYDESCERR | INTA_HIPRIORITYDESCOK |\
1311 INTA_NORMPRIORITYDESCERR | INTA_NORMPRIORITYDESCOK |\
1312 INTA_LOWPRIORITYDESCERR | INTA_LOWPRIORITYDESCOK | INTA_TIMEOUT);
1314 write_nic_word(dev,INTA_MASK, priv->irq_mask);
1318 void rtl8180_irq_disable(struct net_device *dev)
1320 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1322 #ifdef CONFIG_RTL8185B
1323 write_nic_dword(dev,IMR,0);
1325 write_nic_word(dev,INTA_MASK,0);
1327 force_pci_posting(dev);
1328 priv->irq_enabled = 0;
1332 void rtl8180_set_mode(struct net_device *dev,int mode)
1335 ecmd=read_nic_byte(dev, EPROM_CMD);
1336 ecmd=ecmd &~ EPROM_CMD_OPERATING_MODE_MASK;
1337 ecmd=ecmd | (mode<<EPROM_CMD_OPERATING_MODE_SHIFT);
1338 ecmd=ecmd &~ (1<<EPROM_CS_SHIFT);
1339 ecmd=ecmd &~ (1<<EPROM_CK_SHIFT);
1340 write_nic_byte(dev, EPROM_CMD, ecmd);
1343 void rtl8180_adapter_start(struct net_device *dev);
1344 void rtl8180_beacon_tx_enable(struct net_device *dev);
1346 void rtl8180_update_msr(struct net_device *dev)
1348 struct r8180_priv *priv = ieee80211_priv(dev);
1352 msr = read_nic_byte(dev, MSR);
1353 msr &= ~ MSR_LINK_MASK;
1355 rxconf=read_nic_dword(dev,RX_CONF);
1357 if(priv->ieee80211->state == IEEE80211_LINKED)
1359 if(priv->ieee80211->iw_mode == IW_MODE_ADHOC)
1360 msr |= (MSR_LINK_ADHOC<<MSR_LINK_SHIFT);
1361 else if (priv->ieee80211->iw_mode == IW_MODE_MASTER)
1362 msr |= (MSR_LINK_MASTER<<MSR_LINK_SHIFT);
1363 else if (priv->ieee80211->iw_mode == IW_MODE_INFRA)
1364 msr |= (MSR_LINK_MANAGED<<MSR_LINK_SHIFT);
1366 msr |= (MSR_LINK_NONE<<MSR_LINK_SHIFT);
1367 rxconf |= (1<<RX_CHECK_BSSID_SHIFT);
1370 msr |= (MSR_LINK_NONE<<MSR_LINK_SHIFT);
1371 rxconf &= ~(1<<RX_CHECK_BSSID_SHIFT);
1374 write_nic_byte(dev, MSR, msr);
1375 write_nic_dword(dev, RX_CONF, rxconf);
1381 void rtl8180_set_chan(struct net_device *dev,short ch)
1383 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1385 if((ch > 14) || (ch < 1))
1387 printk("In %s: Invalid chnanel %d\n", __func__, ch);
1392 //printk("in %s:channel is %d\n",__func__,ch);
1393 priv->rf_set_chan(dev,priv->chan);
1398 void rtl8180_rx_enable(struct net_device *dev)
1402 /* for now we accept data, management & ctl frame*/
1403 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1405 rxconf=read_nic_dword(dev,RX_CONF);
1406 rxconf = rxconf &~ MAC_FILTER_MASK;
1407 rxconf = rxconf | (1<<ACCEPT_MNG_FRAME_SHIFT);
1408 rxconf = rxconf | (1<<ACCEPT_DATA_FRAME_SHIFT);
1409 rxconf = rxconf | (1<<ACCEPT_BCAST_FRAME_SHIFT);
1410 rxconf = rxconf | (1<<ACCEPT_MCAST_FRAME_SHIFT);
1411 // rxconf = rxconf | (1<<ACCEPT_CRCERR_FRAME_SHIFT);
1412 if (dev->flags & IFF_PROMISC) DMESG ("NIC in promisc mode");
1414 if(priv->ieee80211->iw_mode == IW_MODE_MONITOR || \
1415 dev->flags & IFF_PROMISC){
1416 rxconf = rxconf | (1<<ACCEPT_ALLMAC_FRAME_SHIFT);
1418 rxconf = rxconf | (1<<ACCEPT_NICMAC_FRAME_SHIFT);
1419 if(priv->card_8185 == 0)
1420 rxconf = rxconf | (1<<RX_CHECK_BSSID_SHIFT);
1423 /*if(priv->ieee80211->iw_mode == IW_MODE_MASTER){
1424 rxconf = rxconf | (1<<ACCEPT_ALLMAC_FRAME_SHIFT);
1425 rxconf = rxconf | (1<<RX_CHECK_BSSID_SHIFT);
1428 if(priv->ieee80211->iw_mode == IW_MODE_MONITOR){
1429 rxconf = rxconf | (1<<ACCEPT_CTL_FRAME_SHIFT);
1430 rxconf = rxconf | (1<<ACCEPT_ICVERR_FRAME_SHIFT);
1431 rxconf = rxconf | (1<<ACCEPT_PWR_FRAME_SHIFT);
1434 if( priv->crcmon == 1 && priv->ieee80211->iw_mode == IW_MODE_MONITOR)
1435 rxconf = rxconf | (1<<ACCEPT_CRCERR_FRAME_SHIFT);
1437 //if(!priv->card_8185){
1438 rxconf = rxconf &~ RX_FIFO_THRESHOLD_MASK;
1439 rxconf = rxconf | (RX_FIFO_THRESHOLD_NONE<<RX_FIFO_THRESHOLD_SHIFT);
1442 rxconf = rxconf | (1<<RX_AUTORESETPHY_SHIFT);
1443 rxconf = rxconf &~ MAX_RX_DMA_MASK;
1444 rxconf = rxconf | (MAX_RX_DMA_2048<<MAX_RX_DMA_SHIFT);
1446 //if(!priv->card_8185)
1447 rxconf = rxconf | RCR_ONLYERLPKT;
1449 rxconf = rxconf &~ RCR_CS_MASK;
1450 if(!priv->card_8185)
1451 rxconf |= (priv->rcr_csense<<RCR_CS_SHIFT);
1452 // rxconf &=~ 0xfff00000;
1453 // rxconf |= 0x90100000;//9014f76f;
1454 write_nic_dword(dev, RX_CONF, rxconf);
1459 DMESG("rxconf: %x %x",rxconf ,read_nic_dword(dev,RX_CONF));
1461 cmd=read_nic_byte(dev,CMD);
1462 write_nic_byte(dev,CMD,cmd | (1<<CMD_RX_ENABLE_SHIFT));
1464 /* In rtl8139 driver seems that DMA threshold has to be written
1465 * after enabling RX, so we rewrite RX_CONFIG register
1468 // write_nic_dword(dev, RX_CONF, rxconf);
1473 void set_nic_txring(struct net_device *dev)
1475 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1476 // DMESG("ring %x %x", priv->txlpringdma,read_nic_dword(dev,TLPDA));
1478 write_nic_dword(dev, TX_MANAGEPRIORITY_RING_ADDR, priv->txmapringdma);
1479 // DMESG("ring %x %x", priv->txlpringdma,read_nic_dword(dev,TLPDA));
1480 write_nic_dword(dev, TX_BKPRIORITY_RING_ADDR, priv->txbkpringdma);
1481 // DMESG("ring %x %x", priv->txlpringdma,read_nic_dword(dev,TLPDA));
1482 write_nic_dword(dev, TX_BEPRIORITY_RING_ADDR, priv->txbepringdma);
1483 // DMESG("ring %x %x", priv->txlpringdma,read_nic_dword(dev,TLPDA));
1484 write_nic_dword(dev, TX_VIPRIORITY_RING_ADDR, priv->txvipringdma);
1485 // DMESG("ring %x %x", priv->txlpringdma,read_nic_dword(dev,TLPDA));
1486 write_nic_dword(dev, TX_VOPRIORITY_RING_ADDR, priv->txvopringdma);
1487 // DMESG("ring %x %x", priv->txlpringdma,read_nic_dword(dev,TLPDA));
1488 write_nic_dword(dev, TX_HIGHPRIORITY_RING_ADDR, priv->txhpringdma);
1489 // DMESG("ring %x %x", priv->txlpringdma,read_nic_dword(dev,TLPDA));
1491 write_nic_dword(dev, TX_BEACON_RING_ADDR, priv->txbeaconringdma);
1495 void rtl8180_conttx_enable(struct net_device *dev)
1498 txconf = read_nic_dword(dev,TX_CONF);
1499 txconf = txconf &~ TX_LOOPBACK_MASK;
1500 txconf = txconf | (TX_LOOPBACK_CONTINUE <<TX_LOOPBACK_SHIFT);
1501 write_nic_dword(dev,TX_CONF,txconf);
1505 void rtl8180_conttx_disable(struct net_device *dev)
1508 txconf = read_nic_dword(dev,TX_CONF);
1509 txconf = txconf &~ TX_LOOPBACK_MASK;
1510 txconf = txconf | (TX_LOOPBACK_NONE <<TX_LOOPBACK_SHIFT);
1511 write_nic_dword(dev,TX_CONF,txconf);
1515 void rtl8180_tx_enable(struct net_device *dev)
1521 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1522 txconf= read_nic_dword(dev,TX_CONF);
1525 if(priv->card_8185){
1528 byte = read_nic_byte(dev,CW_CONF);
1529 byte &= ~(1<<CW_CONF_PERPACKET_CW_SHIFT);
1530 byte &= ~(1<<CW_CONF_PERPACKET_RETRY_SHIFT);
1531 write_nic_byte(dev, CW_CONF, byte);
1533 tx_agc_ctl = read_nic_byte(dev, TX_AGC_CTL);
1534 tx_agc_ctl &= ~(1<<TX_AGC_CTL_PERPACKET_GAIN_SHIFT);
1535 tx_agc_ctl &= ~(1<<TX_AGC_CTL_PERPACKET_ANTSEL_SHIFT);
1536 tx_agc_ctl |=(1<<TX_AGC_CTL_FEEDBACK_ANT);
1537 write_nic_byte(dev, TX_AGC_CTL, tx_agc_ctl);
1539 write_nic_word(dev, 0x5e, 0x01);
1540 force_pci_posting(dev);
1542 write_nic_word(dev, 0xfe, 0x10);
1543 force_pci_posting(dev);
1545 write_nic_word(dev, 0x5e, 0x00);
1546 force_pci_posting(dev);
1549 write_nic_byte(dev, 0xec, 0x3f); /* Disable early TX */
1552 if(priv->card_8185){
1554 txconf = txconf &~ (1<<TCR_PROBE_NOTIMESTAMP_SHIFT);
1559 txconf= txconf &~ (1<<TX_CONF_HEADER_AUTOICREMENT_SHIFT);
1561 txconf= txconf | (1<<TX_CONF_HEADER_AUTOICREMENT_SHIFT);
1564 txconf = txconf &~ TX_LOOPBACK_MASK;
1565 txconf = txconf | (TX_LOOPBACK_NONE <<TX_LOOPBACK_SHIFT);
1566 txconf = txconf &~ TCR_DPRETRY_MASK;
1567 txconf = txconf &~ TCR_RTSRETRY_MASK;
1568 txconf = txconf | (priv->retry_data<<TX_DPRETRY_SHIFT);
1569 txconf = txconf | (priv->retry_rts<<TX_RTSRETRY_SHIFT);
1570 txconf = txconf &~ (1<<TX_NOCRC_SHIFT);
1572 if(priv->card_8185){
1573 if(priv->hw_plcp_len)
1574 txconf = txconf &~ TCR_PLCP_LEN;
1576 txconf = txconf | TCR_PLCP_LEN;
1578 txconf = txconf &~ TCR_SAT;
1580 txconf = txconf &~ TCR_MXDMA_MASK;
1581 txconf = txconf | (TCR_MXDMA_2048<<TCR_MXDMA_SHIFT);
1582 txconf = txconf | TCR_CWMIN;
1583 txconf = txconf | TCR_DISCW;
1585 // if(priv->ieee80211->hw_wep)
1586 // txconf=txconf &~ (1<<TX_NOICV_SHIFT);
1588 txconf=txconf | (1<<TX_NOICV_SHIFT);
1590 write_nic_dword(dev,TX_CONF,txconf);
1596 DMESG("txconf: %x %x",txconf,read_nic_dword(dev,TX_CONF));
1599 cmd=read_nic_byte(dev,CMD);
1600 write_nic_byte(dev,CMD,cmd | (1<<CMD_TX_ENABLE_SHIFT));
1603 write_nic_dword(dev,TX_CONF,txconf);
1606 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
1607 write_nic_byte(dev, TX_DMA_POLLING, priv->dma_poll_mask);
1608 rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
1613 void rtl8180_beacon_tx_enable(struct net_device *dev)
1615 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1617 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
1618 #ifdef CONFIG_RTL8185B
1619 priv->dma_poll_stop_mask &= ~(TPPOLLSTOP_BQ);
1620 write_nic_byte(dev,TPPollStop, priv->dma_poll_mask);
1622 priv->dma_poll_mask &=~(1<<TX_DMA_STOP_BEACON_SHIFT);
1623 write_nic_byte(dev,TX_DMA_POLLING,priv->dma_poll_mask);
1625 rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
1629 void rtl8180_beacon_tx_disable(struct net_device *dev)
1631 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1633 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
1634 #ifdef CONFIG_RTL8185B
1635 priv->dma_poll_stop_mask |= TPPOLLSTOP_BQ;
1636 write_nic_byte(dev,TPPollStop, priv->dma_poll_stop_mask);
1638 priv->dma_poll_mask |= (1<<TX_DMA_STOP_BEACON_SHIFT);
1639 write_nic_byte(dev,TX_DMA_POLLING,priv->dma_poll_mask);
1641 rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
1646 void rtl8180_rtx_disable(struct net_device *dev)
1649 struct r8180_priv *priv = ieee80211_priv(dev);
1651 cmd=read_nic_byte(dev,CMD);
1652 write_nic_byte(dev, CMD, cmd &~ \
1653 ((1<<CMD_RX_ENABLE_SHIFT)|(1<<CMD_TX_ENABLE_SHIFT)));
1654 force_pci_posting(dev);
1656 /*while (read_nic_byte(dev,CMD) & (1<<CMD_RX_ENABLE_SHIFT))
1660 if(!priv->rx_skb_complete)
1661 dev_kfree_skb_any(priv->rx_skb);
1665 int alloc_tx_beacon_desc_ring(struct net_device *dev, int count)
1669 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1671 priv->txbeaconring = (u32*)pci_alloc_consistent(priv->pdev,
1672 sizeof(u32)*8*count,
1673 &priv->txbeaconringdma);
1674 if (!priv->txbeaconring) return -1;
1675 for (tmp=priv->txbeaconring,i=0;i<count;i++){
1676 *tmp = *tmp &~ (1<<31); // descriptor empty, owned by the drv
1678 *(tmp+2) = (u32)dma_tmp;
1682 *(tmp+4) = (u32)priv->txbeaconringdma+((i+1)*8*4);
1684 *(tmp+4) = (u32)priv->txbeaconringdma;
1692 short alloc_tx_desc_ring(struct net_device *dev, int bufsize, int count,
1698 dma_addr_t dma_desc, dma_tmp;
1699 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1700 struct pci_dev *pdev = priv->pdev;
1703 if((bufsize & 0xfff) != bufsize) {
1704 DMESGE ("TX buffer allocation too large");
1707 desc = (u32*)pci_alloc_consistent(pdev,
1708 sizeof(u32)*8*count+256, &dma_desc);
1709 if(desc==NULL) return -1;
1710 if(dma_desc & 0xff){
1713 * descriptor's buffer must be 256 byte aligned
1714 * we shouldn't be here, since we set DMA mask !
1716 WARN(1, "DMA buffer is not aligned\n");
1719 for (i=0;i<count;i++)
1721 buf = (void*)pci_alloc_consistent(pdev,bufsize,&dma_tmp);
1722 if (buf == NULL) return -ENOMEM;
1726 case TX_NORMPRIORITY_RING_ADDR:
1727 if(-1 == buffer_add(&(priv->txnpbufs),buf,dma_tmp,NULL)){
1728 DMESGE("Unable to allocate mem for buffer NP");
1733 case TX_LOWPRIORITY_RING_ADDR:
1734 if(-1 == buffer_add(&(priv->txlpbufs),buf,dma_tmp,NULL)){
1735 DMESGE("Unable to allocate mem for buffer LP");
1740 case TX_HIGHPRIORITY_RING_ADDR:
1741 if(-1 == buffer_add(&(priv->txhpbufs),buf,dma_tmp,NULL)){
1742 DMESGE("Unable to allocate mem for buffer HP");
1747 case TX_MANAGEPRIORITY_RING_ADDR:
1748 if(-1 == buffer_add(&(priv->txmapbufs),buf,dma_tmp,NULL)){
1749 DMESGE("Unable to allocate mem for buffer NP");
1754 case TX_BKPRIORITY_RING_ADDR:
1755 if(-1 == buffer_add(&(priv->txbkpbufs),buf,dma_tmp,NULL)){
1756 DMESGE("Unable to allocate mem for buffer LP");
1760 case TX_BEPRIORITY_RING_ADDR:
1761 if(-1 == buffer_add(&(priv->txbepbufs),buf,dma_tmp,NULL)){
1762 DMESGE("Unable to allocate mem for buffer NP");
1767 case TX_VIPRIORITY_RING_ADDR:
1768 if(-1 == buffer_add(&(priv->txvipbufs),buf,dma_tmp,NULL)){
1769 DMESGE("Unable to allocate mem for buffer LP");
1773 case TX_VOPRIORITY_RING_ADDR:
1774 if(-1 == buffer_add(&(priv->txvopbufs),buf,dma_tmp,NULL)){
1775 DMESGE("Unable to allocate mem for buffer NP");
1780 case TX_HIGHPRIORITY_RING_ADDR:
1781 if(-1 == buffer_add(&(priv->txhpbufs),buf,dma_tmp,NULL)){
1782 DMESGE("Unable to allocate mem for buffer HP");
1786 case TX_BEACON_RING_ADDR:
1787 if(-1 == buffer_add(&(priv->txbeaconbufs),buf,dma_tmp,NULL)){
1788 DMESGE("Unable to allocate mem for buffer BP");
1793 *tmp = *tmp &~ (1<<31); // descriptor empty, owned by the drv
1794 *(tmp+2) = (u32)dma_tmp;
1798 *(tmp+4) = (u32)dma_desc+((i+1)*8*4);
1800 *(tmp+4) = (u32)dma_desc;
1806 case TX_MANAGEPRIORITY_RING_ADDR:
1807 priv->txmapringdma=dma_desc;
1808 priv->txmapring=desc;
1811 case TX_BKPRIORITY_RING_ADDR:
1812 priv->txbkpringdma=dma_desc;
1813 priv->txbkpring=desc;
1816 case TX_BEPRIORITY_RING_ADDR:
1817 priv->txbepringdma=dma_desc;
1818 priv->txbepring=desc;
1821 case TX_VIPRIORITY_RING_ADDR:
1822 priv->txvipringdma=dma_desc;
1823 priv->txvipring=desc;
1826 case TX_VOPRIORITY_RING_ADDR:
1827 priv->txvopringdma=dma_desc;
1828 priv->txvopring=desc;
1831 case TX_HIGHPRIORITY_RING_ADDR:
1832 priv->txhpringdma=dma_desc;
1833 priv->txhpring=desc;
1836 case TX_BEACON_RING_ADDR:
1837 priv->txbeaconringdma=dma_desc;
1838 priv->txbeaconring=desc;
1844 DMESG("Tx dma physical address: %x",dma_desc);
1851 void free_tx_desc_rings(struct net_device *dev)
1854 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1855 struct pci_dev *pdev=priv->pdev;
1856 int count = priv->txringcount;
1858 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1859 priv->txmapring, priv->txmapringdma);
1860 buffer_free(dev,&(priv->txmapbufs),priv->txbuffsize,1);
1862 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1863 priv->txbkpring, priv->txbkpringdma);
1864 buffer_free(dev,&(priv->txbkpbufs),priv->txbuffsize,1);
1866 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1867 priv->txbepring, priv->txbepringdma);
1868 buffer_free(dev,&(priv->txbepbufs),priv->txbuffsize,1);
1870 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1871 priv->txvipring, priv->txvipringdma);
1872 buffer_free(dev,&(priv->txvipbufs),priv->txbuffsize,1);
1874 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1875 priv->txvopring, priv->txvopringdma);
1876 buffer_free(dev,&(priv->txvopbufs),priv->txbuffsize,1);
1878 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1879 priv->txhpring, priv->txhpringdma);
1880 buffer_free(dev,&(priv->txhpbufs),priv->txbuffsize,1);
1882 count = priv->txbeaconcount;
1883 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1884 priv->txbeaconring, priv->txbeaconringdma);
1885 buffer_free(dev,&(priv->txbeaconbufs),priv->txbuffsize,1);
1889 void free_beacon_desc_ring(struct net_device *dev,int count)
1892 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1893 struct pci_dev *pdev=priv->pdev;
1895 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1896 priv->txbeaconring, priv->txbeaconringdma);
1898 if (priv->beacon_buf)
1899 pci_free_consistent(priv->pdev,
1900 priv->master_beaconsize,priv->beacon_buf,priv->beacondmabuf);
1904 void free_rx_desc_ring(struct net_device *dev)
1906 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1907 struct pci_dev *pdev = priv->pdev;
1909 int count = priv->rxringcount;
1911 #ifdef CONFIG_RTL8185B
1912 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1913 priv->rxring, priv->rxringdma);
1915 pci_free_consistent(pdev, sizeof(u32)*4*count+256,
1916 priv->rxring, priv->rxringdma);
1919 buffer_free(dev,&(priv->rxbuffer),priv->rxbuffersize,0);
1923 short alloc_rx_desc_ring(struct net_device *dev, u16 bufsize, int count)
1928 dma_addr_t dma_desc,dma_tmp;
1929 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1930 struct pci_dev *pdev=priv->pdev;
1934 #ifdef CONFIG_RTL8185B
1935 rx_desc_size = 8; // 4*8 = 32 bytes
1940 if((bufsize & 0xfff) != bufsize){
1941 DMESGE ("RX buffer allocation too large");
1945 desc = (u32*)pci_alloc_consistent(pdev,sizeof(u32)*rx_desc_size*count+256,
1948 if(dma_desc & 0xff){
1951 * descriptor's buffer must be 256 byte aligned
1952 * should never happen since we specify the DMA mask
1954 WARN(1, "DMA buffer is not aligned\n");
1958 priv->rxringdma=dma_desc;
1961 for (i=0;i<count;i++){
1963 if ((buf= kmalloc(bufsize * sizeof(u8),GFP_ATOMIC)) == NULL){
1964 DMESGE("Failed to kmalloc RX buffer");
1968 dma_tmp = pci_map_single(pdev,buf,bufsize * sizeof(u8),
1969 PCI_DMA_FROMDEVICE);
1971 #ifdef DEBUG_ZERO_RX
1973 for(j=0;j<bufsize;j++) ((u8*)buf)[i] = 0;
1976 //buf = (void*)pci_alloc_consistent(pdev,bufsize,&dma_tmp);
1977 if(-1 == buffer_add(&(priv->rxbuffer), buf,dma_tmp,
1978 &(priv->rxbufferhead))){
1979 DMESGE("Unable to allocate mem RX buf");
1982 *tmp = 0; //zero pads the header of the descriptor
1983 *tmp = *tmp |( bufsize&0xfff);
1984 *(tmp+2) = (u32)dma_tmp;
1985 *tmp = *tmp |(1<<31); // descriptor void, owned by the NIC
1987 #ifdef DEBUG_RXALLOC
1988 DMESG("Alloc %x size buffer, DMA mem @ %x, virtual mem @ %x",
1989 (u32)(bufsize&0xfff), (u32)dma_tmp, (u32)buf);
1992 tmp=tmp+rx_desc_size;
1995 *(tmp-rx_desc_size) = *(tmp-rx_desc_size) | (1<<30); // this is the last descriptor
1998 #ifdef DEBUG_RXALLOC
1999 DMESG("RX DMA physical address: %x",dma_desc);
2006 void set_nic_rxring(struct net_device *dev)
2009 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
2011 //rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
2013 pgreg=read_nic_byte(dev, PGSELECT);
2014 write_nic_byte(dev, PGSELECT, pgreg &~ (1<<PGSELECT_PG_SHIFT));
2016 //rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
2018 write_nic_dword(dev, RXRING_ADDR,priv->rxringdma);
2022 void rtl8180_reset(struct net_device *dev)
2024 //u32 txconf = 0x80e00707; //FIXME: Make me understandable
2027 //write_nic_dword(dev,TX_CONF,txconf);
2029 rtl8180_irq_disable(dev);
2031 cr=read_nic_byte(dev,CMD);
2033 cr = cr | (1<<CMD_RST_SHIFT);
2034 write_nic_byte(dev,CMD,cr);
2036 force_pci_posting(dev);
2040 if(read_nic_byte(dev,CMD) & (1<<CMD_RST_SHIFT))
2041 DMESGW("Card reset timeout!");
2043 DMESG("Card successfully reset");
2045 //#ifndef CONFIG_RTL8185B
2046 rtl8180_set_mode(dev,EPROM_CMD_LOAD);
2047 force_pci_posting(dev);
2052 inline u16 ieeerate2rtlrate(int rate)
2085 static u16 rtl_rate[] = {10,20,55,110,60,90,120,180,240,360,480,540,720};
2086 inline u16 rtl8180_rate2rate(short rate)
2088 if (rate >12) return 10;
2089 return rtl_rate[rate];
2091 inline u8 rtl8180_IsWirelessBMode(u16 rate)
2093 if( ((rate <= 110) && (rate != 60) && (rate != 90)) || (rate == 220) )
2097 u16 N_DBPSOfRate(u16 DataRate);
2101 u8 bManagementFrame,
2109 if( rtl8180_IsWirelessBMode(DataRate) )
2111 if( bManagementFrame || !bShortPreamble || DataRate == 10 )
2113 FrameTime = (u16)(144+48+(FrameLength*8/(DataRate/10)));
2117 FrameTime = (u16)(72+24+(FrameLength*8/(DataRate/10)));
2119 if( ( FrameLength*8 % (DataRate/10) ) != 0 ) //Get the Ceilling
2121 } else { //802.11g DSSS-OFDM PLCP length field calculation.
2122 N_DBPS = N_DBPSOfRate(DataRate);
2123 Ceiling = (16 + 8*FrameLength + 6) / N_DBPS
2124 + (((16 + 8*FrameLength + 6) % N_DBPS) ? 1 : 0);
2125 FrameTime = (u16)(16 + 4 + 4*Ceiling + 6);
2129 u16 N_DBPSOfRate(u16 DataRate)
2177 // For Netgear case, they want good-looking singal strength.
2178 // 2004.12.05, by rcnjko.
2181 NetgearSignalStrengthTranslate(
2188 // Step 1. Scale mapping.
2189 if(CurrSS >= 71 && CurrSS <= 100)
2191 RetSS = 90 + ((CurrSS - 70) / 3);
2193 else if(CurrSS >= 41 && CurrSS <= 70)
2195 RetSS = 78 + ((CurrSS - 40) / 3);
2197 else if(CurrSS >= 31 && CurrSS <= 40)
2199 RetSS = 66 + (CurrSS - 30);
2201 else if(CurrSS >= 21 && CurrSS <= 30)
2203 RetSS = 54 + (CurrSS - 20);
2205 else if(CurrSS >= 5 && CurrSS <= 20)
2207 RetSS = 42 + (((CurrSS - 5) * 2) / 3);
2209 else if(CurrSS == 4)
2213 else if(CurrSS == 3)
2217 else if(CurrSS == 2)
2221 else if(CurrSS == 1)
2229 //RT_TRACE(COMP_DBG, DBG_LOUD, ("##### After Mapping: LastSS: %d, CurrSS: %d, RetSS: %d\n", LastSS, CurrSS, RetSS));
2231 // Step 2. Smoothing.
2234 RetSS = ((LastSS * 5) + (RetSS)+ 5) / 6;
2236 //RT_TRACE(COMP_DBG, DBG_LOUD, ("$$$$$ After Smoothing: LastSS: %d, CurrSS: %d, RetSS: %d\n", LastSS, CurrSS, RetSS));
2242 // Translate 0-100 signal strength index into dBm.
2246 u8 SignalStrengthIndex // 0-100 index.
2249 long SignalPower; // in dBm.
2251 // Translate to dBm (x=0.5y-95).
2252 SignalPower = (long)((SignalStrengthIndex + 1) >> 1);
2259 // Perform signal smoothing for dynamic mechanism.
2260 // This is different with PerformSignalSmoothing8185 in smoothing fomula.
2261 // No dramatic adjustion is apply because dynamic mechanism need some degree
2262 // of correctness. Ported from 8187B.
2263 // 2007-02-26, by Bruce.
2266 PerformUndecoratedSignalSmoothing8185(
2267 struct r8180_priv *priv,
2273 // Determin the current packet is CCK rate.
2274 priv->bCurCCKPkt = bCckRate;
2276 if(priv->UndecoratedSmoothedSS >= 0)
2278 priv->UndecoratedSmoothedSS = ( (priv->UndecoratedSmoothedSS * 5) + (priv->SignalStrength * 10) ) / 6;
2282 priv->UndecoratedSmoothedSS = priv->SignalStrength * 10;
2285 priv->UndercorateSmoothedRxPower = ( (priv->UndercorateSmoothedRxPower * 50) + (priv->RxPower* 11)) / 60;
2287 // printk("Sommthing SignalSterngth (%d) => UndecoratedSmoothedSS (%d)\n", priv->SignalStrength, priv->UndecoratedSmoothedSS);
2288 // printk("Sommthing RxPower (%d) => UndecoratedRxPower (%d)\n", priv->RxPower, priv->UndercorateSmoothedRxPower);
2290 //if(priv->CurCCKRSSI >= 0 && bCckRate)
2293 priv->CurCCKRSSI = priv->RSSI;
2297 priv->CurCCKRSSI = 0;
2300 // Boundary checking.
2301 // TODO: The overflow condition does happen, if we want to fix,
2302 // we shall recalculate thresholds first.
2303 if(priv->UndecoratedSmoothedSS > 100)
2305 // printk("UndecoratedSmoothedSS(%d) overflow, SignalStrength(%d)\n", priv->UndecoratedSmoothedSS, priv->SignalStrength);
2307 if(priv->UndecoratedSmoothedSS < 0)
2309 // printk("UndecoratedSmoothedSS(%d) underflow, SignalStrength(%d)\n", priv->UndecoratedSmoothedSS, priv->SignalStrength);
2316 /* This is rough RX isr handling routine*/
2317 void rtl8180_rx(struct net_device *dev)
2319 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
2320 struct sk_buff *tmp_skb;
2322 //struct sk_buff *skb;
2326 unsigned char quality, signal;
2337 u8 LNA_gain[4]={02, 17, 29, 39};
2339 struct ieee80211_hdr *hdr;//by amy
2341 u8 bHwError = 0,bCRC = 0,bICV = 0;
2345 bool bCckRate = false;
2347 long SignalStrengthIndex = 0;//+by amy 080312
2348 // u8 SignalStrength = 0;
2349 struct ieee80211_rx_stats stats = {
2353 // .mac_time = jiffies,
2354 .freq = IEEE80211_24GHZ_BAND,
2357 #ifdef CONFIG_RTL8185B
2358 stats.nic_type = NIC_8185B;
2362 stats.nic_type = NIC_8185;
2365 //printk("receive frame!%d\n",count++);
2366 //if (!priv->rxbuffer) DMESG ("EE: NIC RX ack, but RX queue corrupted!");
2369 if ((*(priv->rxringtail)) & (1<<31)) {
2371 /* we have got an RX int, but the descriptor
2372 * we are pointing is empty*/
2374 priv->stats.rxnodata++;
2375 priv->ieee80211->stats.rx_errors++;
2377 /* if (! *(priv->rxring) & (1<<31)) {
2379 priv->stats.rxreset++;
2380 priv->rxringtail=priv->rxring;
2381 priv->rxbuffer=priv->rxbufferhead;
2386 /* Maybe it is possible that the NIC has skipped some descriptors or
2387 * it has reset its internal pointer to the beginning of the ring
2388 * we search for the first filled descriptor in the ring, or we break
2389 * putting again the pointer in the old location if we do not found any.
2390 * This is quite dangerous, what does happen if the nic writes
2391 * two descriptor (say A and B) when we have just checked the descriptor
2392 * A and we are going to check the descriptor B..This might happen if the
2393 * interrupt was dummy, there was not really filled descriptors and
2394 * the NIC didn't lose pointer
2397 //priv->stats.rxwrkaround++;
2399 tmp = priv->rxringtail;
2400 while (*(priv->rxringtail) & (1<<31)){
2402 priv->rxringtail+=4;
2404 if(priv->rxringtail >=
2405 (priv->rxring)+(priv->rxringcount )*4)
2406 priv->rxringtail=priv->rxring;
2408 priv->rxbuffer=(priv->rxbuffer->next);
2410 if(priv->rxringtail == tmp ){
2411 //DMESG("EE: Could not find RX pointer");
2412 priv->stats.rxnopointer++;
2419 tmp = priv->rxringtail;
2421 if(tmp == priv->rxring)
2422 //tmp = priv->rxring + (priv->rxringcount )*rx_desc_size; xiong-2006-11-15
2423 tmp = priv->rxring + (priv->rxringcount - 1)*rx_desc_size;
2425 tmp -= rx_desc_size;
2427 if(! (*tmp & (1<<31)))
2429 }while(tmp != priv->rxring);
2431 if(tmp2) priv->rxringtail = tmp2;
2436 /* while there are filled descriptors */
2437 while(!(*(priv->rxringtail) & (1<<31))){
2438 if(*(priv->rxringtail) & (1<<26))
2439 DMESGW("RX buffer overflow");
2440 if(*(priv->rxringtail) & (1<<12))
2441 priv->stats.rxicverr++;
2443 if(*(priv->rxringtail) & (1<<27)){
2444 priv->stats.rxdmafail++;
2445 //DMESG("EE: RX DMA FAILED at buffer pointed by descriptor %x",(u32)priv->rxringtail);
2449 pci_dma_sync_single_for_cpu(priv->pdev,
2450 priv->rxbuffer->dma,
2451 priv->rxbuffersize * \
2453 PCI_DMA_FROMDEVICE);
2455 first = *(priv->rxringtail) & (1<<29) ? 1:0;
2456 if(first) priv->rx_prevlen=0;
2458 last = *(priv->rxringtail) & (1<<28) ? 1:0;
2460 lastlen=((*priv->rxringtail) &0xfff);
2462 /* if the last descriptor (that should
2463 * tell us the total packet len) tell
2464 * us something less than the descriptors
2465 * len we had until now, then there is some
2467 * workaround to prevent kernel panic
2469 if(lastlen < priv->rx_prevlen)
2472 len=lastlen-priv->rx_prevlen;
2474 if(*(priv->rxringtail) & (1<<13)) {
2475 //lastlen=((*priv->rxringtail) &0xfff);
2476 if ((*(priv->rxringtail) & 0xfff) <500)
2477 priv->stats.rxcrcerrmin++;
2478 else if ((*(priv->rxringtail) & 0x0fff) >1000)
2479 priv->stats.rxcrcerrmax++;
2481 priv->stats.rxcrcerrmid++;
2486 len = priv->rxbuffersize;
2489 #ifdef CONFIG_RTL8185B
2491 padding = ((*(priv->rxringtail+3))&(0x04000000))>>26;
2493 padding = ((*(priv->rxringtail+3))&(0x04000000))>>26;
2500 #ifdef CONFIG_RTL818X_S
2504 priv->rx_prevlen+=len;
2506 if(priv->rx_prevlen > MAX_FRAG_THRESHOLD + 100){
2507 /* HW is probably passing several buggy frames
2508 * without FD or LD flag set.
2509 * Throw this garbage away to prevent skb
2512 if(!priv->rx_skb_complete)
2513 dev_kfree_skb_any(priv->rx_skb);
2514 priv->rx_skb_complete = 1;
2517 #ifdef DEBUG_RX_FRAG
2518 DMESG("Iteration.. len %x",len);
2519 if(first) DMESG ("First descriptor");
2520 if(last) DMESG("Last descriptor");
2523 #ifdef DEBUG_RX_VERBOSE
2524 print_buffer( priv->rxbuffer->buf, len);
2527 #ifdef CONFIG_RTL8185B
2528 signal=(unsigned char)(((*(priv->rxringtail+3))& (0x00ff0000))>>16);
2529 signal=(signal&0xfe)>>1; // Modify by hikaru 6.6
2531 quality=(unsigned char)((*(priv->rxringtail+3)) & (0xff));
2533 stats.mac_time[0] = *(priv->rxringtail+1);
2534 stats.mac_time[1] = *(priv->rxringtail+2);
2535 rxpower =((char)(((*(priv->rxringtail+4))& (0x00ff0000))>>16))/2 - 42;
2536 RSSI = ((u8)(((*(priv->rxringtail+3)) & (0x0000ff00))>> 8)) & (0x7f);
2539 signal=((*(priv->rxringtail+1))& (0xff0000))>>16;
2540 signal=(signal&0xfe)>>1; // Modify by hikaru 6.6
2542 quality=((*(priv->rxringtail+1)) & (0xff));
2544 stats.mac_time[0] = *(priv->rxringtail+2);
2545 stats.mac_time[1] = *(priv->rxringtail+3);
2547 rate=((*(priv->rxringtail)) &
2548 ((1<<23)|(1<<22)|(1<<21)|(1<<20)))>>20;
2550 stats.rate = rtl8180_rate2rate(rate);
2552 Antenna = (((*(priv->rxringtail +3))& (0x00008000)) == 0 )? 0:1 ;
2553 // printk("in rtl8180_rx():Antenna is %d\n",Antenna);
2554 //by amy for antenna
2555 if(!rtl8180_IsWirelessBMode(stats.rate))
2558 RxAGC_dBm = rxpower+1; //bias
2562 RxAGC_dBm = signal;//bit 0 discard
2564 LNA = (u8) (RxAGC_dBm & 0x60 ) >> 5 ; //bit 6~ bit 5
2565 BB = (u8) (RxAGC_dBm & 0x1F); // bit 4 ~ bit 0
2567 RxAGC_dBm = -( LNA_gain[LNA] + (BB *2) ); //Pin_11b=-(LNA_gain+BB_gain) (dBm)
2569 RxAGC_dBm +=4; //bias
2572 if(RxAGC_dBm & 0x80) //absolute value
2573 RXAGC= ~(RxAGC_dBm)+1;
2574 bCckRate = rtl8180_IsWirelessBMode(stats.rate);
2575 // Translate RXAGC into 1-100.
2576 if(!rtl8180_IsWirelessBMode(stats.rate))
2582 RXAGC=(90-RXAGC)*100/65;
2590 RXAGC=(95-RXAGC)*100/65;
2592 priv->SignalStrength = (u8)RXAGC;
2593 priv->RecvSignalPower = RxAGC_dBm ; // It can use directly by SD3 CMLin
2594 priv->RxPower = rxpower;
2597 // SQ translation formular is provided by SD3 DZ. 2006.06.27, by rcnjko.
2599 quality = 1;//0; //0 will cause epc to show signal zero , walk aroud now;
2600 else if(quality < 27)
2603 quality = 127 - quality;
2604 priv->SignalQuality = quality;
2605 if(!priv->card_8185)
2606 printk("check your card type\n");
2608 stats.signal = (u8)quality;//priv->wstats.qual.level = priv->SignalStrength;
2609 stats.signalstrength = RXAGC;
2610 if(stats.signalstrength > 100)
2611 stats.signalstrength = 100;
2612 stats.signalstrength = (stats.signalstrength * 70)/100 + 30;
2613 // printk("==========================>rx : RXAGC is %d,signalstrength is %d\n",RXAGC,stats.signalstrength);
2614 stats.rssi = priv->wstats.qual.qual = priv->SignalQuality;
2615 stats.noise = priv->wstats.qual.noise = 100 - priv ->wstats.qual.qual;
2617 bHwError = (((*(priv->rxringtail))& (0x00000fff)) == 4080)| (((*(priv->rxringtail))& (0x04000000)) != 0 )
2618 | (((*(priv->rxringtail))& (0x08000000)) != 0 )| (((~(*(priv->rxringtail)))& (0x10000000)) != 0 )| (((~(*(priv->rxringtail)))& (0x20000000)) != 0 );
2619 bCRC = ((*(priv->rxringtail)) & (0x00002000)) >> 13;
2620 bICV = ((*(priv->rxringtail)) & (0x00001000)) >> 12;
2621 hdr = (struct ieee80211_hdr *)priv->rxbuffer->buf;
2622 fc = le16_to_cpu(hdr->frame_ctl);
2623 type = WLAN_FC_GET_TYPE(fc);
2625 if((IEEE80211_FTYPE_CTL != type) &&
2626 (eqMacAddr(priv->ieee80211->current_network.bssid, (fc & IEEE80211_FCTL_TODS)? hdr->addr1 : (fc & IEEE80211_FCTL_FROMDS )? hdr->addr2 : hdr->addr3))
2627 && (!bHwError) && (!bCRC)&& (!bICV))
2630 // Perform signal smoothing for dynamic mechanism on demand.
2631 // This is different with PerformSignalSmoothing8185 in smoothing fomula.
2632 // No dramatic adjustion is apply because dynamic mechanism need some degree
2633 // of correctness. 2007.01.23, by shien chang.
2634 PerformUndecoratedSignalSmoothing8185(priv,bCckRate);
2636 // For good-looking singal strength.
2638 SignalStrengthIndex = NetgearSignalStrengthTranslate(
2639 priv->LastSignalStrengthInPercent,
2640 priv->SignalStrength);
2642 priv->LastSignalStrengthInPercent = SignalStrengthIndex;
2643 priv->Stats_SignalStrength = TranslateToDbm8185((u8)SignalStrengthIndex);
2645 // We need more correct power of received packets and the "SignalStrength" of RxStats is beautified,
2646 // so we record the correct power here.
2648 priv->Stats_SignalQuality =(long) (priv->Stats_SignalQuality * 5 + (long)priv->SignalQuality + 5) / 6;
2649 priv->Stats_RecvSignalPower = (long)(priv->Stats_RecvSignalPower * 5 + priv->RecvSignalPower -1) / 6;
2651 // Figure out which antenna that received the lasted packet.
2652 priv->LastRxPktAntenna = Antenna ? 1 : 0; // 0: aux, 1: main.
2654 SwAntennaDiversityRxOk8185(dev, priv->SignalStrength);
2657 //by amy for antenna
2666 if(!priv->rx_skb_complete){
2667 /* seems that HW sometimes fails to reiceve and
2668 doesn't provide the last descriptor */
2670 DMESG("going to free incomplete skb");
2672 dev_kfree_skb_any(priv->rx_skb);
2673 priv->stats.rxnolast++;
2675 DMESG("free incomplete skb OK");
2678 /* support for prism header has been originally added by Christian */
2679 if(priv->prism_hdr && priv->ieee80211->iw_mode == IW_MODE_MONITOR){
2682 priv->rx_skb = dev_alloc_skb(len+2+PRISM_HDR_SIZE);
2683 if(! priv->rx_skb) goto drop;
2685 prism_hdr = (u32*) skb_put(priv->rx_skb,PRISM_HDR_SIZE);
2686 prism_hdr[0]=htonl(0x80211001); //version
2687 prism_hdr[1]=htonl(0x40); //length
2688 prism_hdr[2]=htonl(stats.mac_time[1]); //mactime (HIGH)
2689 prism_hdr[3]=htonl(stats.mac_time[0]); //mactime (LOW)
2690 rdtsc(prism_hdr[5], prism_hdr[4]); //hostime (LOW+HIGH)
2691 prism_hdr[4]=htonl(prism_hdr[4]); //Byte-Order aendern
2692 prism_hdr[5]=htonl(prism_hdr[5]); //Byte-Order aendern
2693 prism_hdr[6]=0x00; //phytype
2694 prism_hdr[7]=htonl(priv->chan); //channel
2695 prism_hdr[8]=htonl(stats.rate); //datarate
2696 prism_hdr[9]=0x00; //antenna
2697 prism_hdr[10]=0x00; //priority
2698 prism_hdr[11]=0x00; //ssi_type
2699 prism_hdr[12]=htonl(stats.signal); //ssi_signal
2700 prism_hdr[13]=htonl(stats.noise); //ssi_noise
2701 prism_hdr[14]=0x00; //preamble
2702 prism_hdr[15]=0x00; //encoding
2706 priv->rx_skb = dev_alloc_skb(len+2);
2707 if( !priv->rx_skb) goto drop;
2709 DMESG("Alloc initial skb %x",len+2);
2713 priv->rx_skb_complete=0;
2714 priv->rx_skb->dev=dev;
2716 /* if we are here we should have already RXed
2718 * If we get here and the skb is not allocated then
2719 * we have just throw out garbage (skb not allocated)
2720 * and we are still rxing garbage....
2722 if(!priv->rx_skb_complete){
2724 tmp_skb= dev_alloc_skb(priv->rx_skb->len +len+2);
2726 if(!tmp_skb) goto drop;
2730 DMESG("Realloc skb %x",len+2);
2734 DMESG("going copy prev frag %x",priv->rx_skb->len);
2736 memcpy(skb_put(tmp_skb,priv->rx_skb->len),
2740 DMESG("skb copy prev frag complete");
2743 dev_kfree_skb_any(priv->rx_skb);
2745 DMESG("prev skb free ok");
2748 priv->rx_skb=tmp_skb;
2752 DMESG("going to copy current payload %x",len);
2754 if(!priv->rx_skb_complete) {
2755 #ifdef CONFIG_RTL8185B
2757 memcpy(skb_put(priv->rx_skb,len),
2758 (((unsigned char *)priv->rxbuffer->buf) + 2),len);
2761 memcpy(skb_put(priv->rx_skb,len),
2762 priv->rxbuffer->buf,len);
2763 #ifdef CONFIG_RTL8185B
2768 DMESG("current fragment skb copy complete");
2771 if(last && !priv->rx_skb_complete){
2774 DMESG("Got last fragment");
2777 if(priv->rx_skb->len > 4)
2778 skb_trim(priv->rx_skb,priv->rx_skb->len-4);
2780 DMESG("yanked out crc, passing to the upper layer");
2783 #ifndef RX_DONT_PASS_UL
2784 if(!ieee80211_rx(priv->ieee80211,
2785 priv->rx_skb, &stats)){
2787 DMESGW("Packet not consumed");
2789 #endif // RX_DONT_PASS_UL
2791 dev_kfree_skb_any(priv->rx_skb);
2792 #ifndef RX_DONT_PASS_UL
2800 priv->rx_skb_complete=1;
2805 pci_dma_sync_single_for_device(priv->pdev,
2806 priv->rxbuffer->dma,
2807 priv->rxbuffersize * \
2809 PCI_DMA_FROMDEVICE);
2812 drop: // this is used when we have not enought mem
2814 /* restore the descriptor */
2815 *(priv->rxringtail+2)=priv->rxbuffer->dma;
2816 *(priv->rxringtail)=*(priv->rxringtail) &~ 0xfff;
2817 *(priv->rxringtail)=
2818 *(priv->rxringtail) | priv->rxbuffersize;
2820 *(priv->rxringtail)=
2821 *(priv->rxringtail) | (1<<31);
2827 DMESG("Current descriptor: %x",(u32)priv->rxringtail);
2829 //unsigned long flags;
2830 //spin_lock_irqsave(&priv->irq_lock,flags);
2832 priv->rxringtail+=rx_desc_size;
2833 if(priv->rxringtail >=
2834 (priv->rxring)+(priv->rxringcount )*rx_desc_size)
2835 priv->rxringtail=priv->rxring;
2837 //spin_unlock_irqrestore(&priv->irq_lock,flags);
2840 priv->rxbuffer=(priv->rxbuffer->next);
2846 // if(get_curr_tx_free_desc(dev,priority))
2847 // ieee80211_sta_ps_sleep(priv->ieee80211, &tmp, &tmp2);
2854 void rtl8180_dma_kick(struct net_device *dev, int priority)
2856 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
2858 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
2865 write_nic_byte(dev,TX_DMA_POLLING,
2866 (1<< TX_DMA_POLLING_LOWPRIORITY_SHIFT) |
2867 priv->dma_poll_mask);
2872 write_nic_byte(dev,TX_DMA_POLLING,
2873 (1<< TX_DMA_POLLING_NORMPRIORITY_SHIFT) |
2874 priv->dma_poll_mask);
2879 write_nic_byte(dev,TX_DMA_POLLING,
2880 (1<< TX_DMA_POLLING_HIPRIORITY_SHIFT) |
2881 priv->dma_poll_mask);
2886 write_nic_byte(dev, TX_DMA_POLLING,
2887 (1 << (priority + 1)) | priv->dma_poll_mask);
2888 rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
2890 force_pci_posting(dev);
2894 void rtl8180_tx_queues_stop(struct net_device *dev)
2896 //struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
2897 u8 dma_poll_mask = (1<<TX_DMA_STOP_LOWPRIORITY_SHIFT);
2898 dma_poll_mask |= (1<<TX_DMA_STOP_HIPRIORITY_SHIFT);
2899 dma_poll_mask |= (1<<TX_DMA_STOP_NORMPRIORITY_SHIFT);
2900 dma_poll_mask |= (1<<TX_DMA_STOP_BEACON_SHIFT);
2902 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
2903 write_nic_byte(dev,TX_DMA_POLLING,dma_poll_mask);
2904 rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
2908 void rtl8180_data_hard_stop(struct net_device *dev)
2910 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
2912 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
2913 #ifdef CONFIG_RTL8185B
2914 priv->dma_poll_stop_mask |= TPPOLLSTOP_AC_VIQ;
2915 write_nic_byte(dev,TPPollStop, priv->dma_poll_stop_mask);
2917 priv->dma_poll_mask |= (1<<TX_DMA_STOP_LOWPRIORITY_SHIFT);
2918 write_nic_byte(dev,TX_DMA_POLLING,priv->dma_poll_mask);
2920 rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
2924 void rtl8180_data_hard_resume(struct net_device *dev)
2926 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
2928 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
2929 #ifdef CONFIG_RTL8185B
2930 priv->dma_poll_stop_mask &= ~(TPPOLLSTOP_AC_VIQ);
2931 write_nic_byte(dev,TPPollStop, priv->dma_poll_stop_mask);
2933 priv->dma_poll_mask &= ~(1<<TX_DMA_STOP_LOWPRIORITY_SHIFT);
2934 write_nic_byte(dev,TX_DMA_POLLING,priv->dma_poll_mask);
2936 rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
2940 /* this function TX data frames when the ieee80211 stack requires this.
2941 * It checks also if we need to stop the ieee tx queue, eventually do it
2943 void rtl8180_hard_data_xmit(struct sk_buff *skb,struct net_device *dev, int
2946 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
2948 struct ieee80211_hdr_3addr *h = (struct ieee80211_hdr_3addr *) skb->data;
2949 short morefrag = (h->frame_ctl) & IEEE80211_FCTL_MOREFRAGS;
2950 unsigned long flags;
2952 //static int count = 0;
2954 mode = priv->ieee80211->iw_mode;
2956 rate = ieeerate2rtlrate(rate);
2958 * This function doesn't require lock because we make
2959 * sure it's called with the tx_lock already acquired.
2960 * this come from the kernel's hard_xmit callback (trought
2961 * the ieee stack, or from the try_wake_queue (again trought
2964 #ifdef CONFIG_RTL8185B
2965 priority = AC2Q(skb->priority);
2967 priority = LOW_PRIORITY;
2969 spin_lock_irqsave(&priv->tx_lock,flags);
2971 if(priv->ieee80211->bHwRadioOff)
2973 spin_unlock_irqrestore(&priv->tx_lock,flags);
2978 //printk(KERN_WARNING "priority = %d@%d\n", priority, count++);
2979 if (!check_nic_enought_desc(dev, priority)){
2980 //DMESG("Error: no descriptor left by previous TX (avail %d) ",
2981 // get_curr_tx_free_desc(dev, priority));
2982 DMESGW("Error: no descriptor left by previous TX (avail %d) ",
2983 get_curr_tx_free_desc(dev, priority));
2984 //printk(KERN_WARNING "==============================================================> \n");
2985 ieee80211_stop_queue(priv->ieee80211);
2987 rtl8180_tx(dev, skb->data, skb->len, priority, morefrag,0,rate);
2988 if (!check_nic_enought_desc(dev, priority))
2989 ieee80211_stop_queue(priv->ieee80211);
2991 //dev_kfree_skb_any(skb);
2992 spin_unlock_irqrestore(&priv->tx_lock,flags);
2996 /* This is a rough attempt to TX a frame
2997 * This is called by the ieee 80211 stack to TX management frames.
2998 * If the ring is full packet are dropped (for data frame the queue
2999 * is stopped before this can happen). For this reason it is better
3000 * if the descriptors are larger than the largest management frame
3001 * we intend to TX: i'm unsure what the HW does if it will not found
3002 * the last fragment of a frame because it has been dropped...
3003 * Since queues for Management and Data frames are different we
3004 * might use a different lock than tx_lock (for example mgmt_tx_lock)
3006 /* these function may loops if invoked with 0 descriptors or 0 len buffer*/
3007 int rtl8180_hard_start_xmit(struct sk_buff *skb,struct net_device *dev)
3009 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
3011 unsigned long flags;
3015 #ifdef CONFIG_RTL8185B
3016 priority = MANAGE_PRIORITY;
3018 priority = NORM_PRIORITY;
3021 spin_lock_irqsave(&priv->tx_lock,flags);
3023 if(priv->ieee80211->bHwRadioOff)
3025 spin_unlock_irqrestore(&priv->tx_lock,flags);
3027 dev_kfree_skb_any(skb);
3028 return NETDEV_TX_OK;
3031 rtl8180_tx(dev, skb->data, skb->len, priority,
3032 0, 0,ieeerate2rtlrate(priv->ieee80211->basic_rate));
3034 priv->ieee80211->stats.tx_bytes+=skb->len;
3035 priv->ieee80211->stats.tx_packets++;
3036 spin_unlock_irqrestore(&priv->tx_lock,flags);
3038 dev_kfree_skb_any(skb);
3039 return NETDEV_TX_OK;
3042 // longpre 144+48 shortpre 72+24
3043 u16 rtl8180_len2duration(u32 len, short rate,short* ext)
3052 duration = ((len+4)<<4) /0x2;
3053 drift = ((len+4)<<4) % 0x2;
3054 if(drift ==0 ) break;
3060 duration = ((len+4)<<4) /0x4;
3061 drift = ((len+4)<<4) % 0x4;
3062 if(drift ==0 ) break;
3068 duration = ((len+4)<<4) /0xb;
3069 drift = ((len+4)<<4) % 0xb;
3078 duration = ((len+4)<<4) /0x16;
3079 drift = ((len+4)<<4) % 0x16;
3093 void rtl8180_prepare_beacon(struct net_device *dev)
3096 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
3098 struct sk_buff *skb;
3100 u16 word = read_nic_word(dev, BcnItv);
3101 word &= ~BcnItv_BcnItv; // clear Bcn_Itv
3102 word |= cpu_to_le16(priv->ieee80211->current_network.beacon_interval);//0x64;
3103 write_nic_word(dev, BcnItv, word);
3106 skb = ieee80211_get_beacon(priv->ieee80211);
3108 rtl8180_tx(dev,skb->data,skb->len,BEACON_PRIORITY,
3109 0,0,ieeerate2rtlrate(priv->ieee80211->basic_rate));
3110 dev_kfree_skb_any(skb);
3113 //DMESG("size %x",len);
3114 if(*tail & (1<<31)){
3116 //DMESG("No more beacon TX desc");
3120 //while(! (*tail & (1<<31))){
3121 *tail= 0; // zeroes header
3123 *tail = *tail| (1<<29) ; //fist segment of the packet
3124 *tail = (*tail) | (1<<28); // last segment
3125 // *tail = *tail | (1<<18); // this is a beacon frame
3126 *(tail+3)=*(tail+3) &~ 0xfff;
3127 *(tail+3)=*(tail+3) | len; // buffer lenght
3129 // zeroes the second 32-bits dword of the descriptor
3131 *tail = *tail | (rate << 24);
3133 duration = rtl8180_len2duration(len,rate,&ext);
3135 *(tail+1) = *(tail+1) | ((duration & 0x7fff)<<16);
3137 *tail = *tail | (1<<31);
3138 //^ descriptor ready to be txed
3139 if((tail - begin)/8 == priv->txbeaconcount-1)
3147 /* This function do the real dirty work: it enqueues a TX command
3148 * descriptor in the ring buffer, copyes the frame in a TX buffer
3149 * and kicks the NIC to ensure it does the DMA transfer.
3151 short rtl8180_tx(struct net_device *dev, u8* txbuf, int len, int priority,
3152 short morefrag, short descfrag, int rate)
3154 struct r8180_priv *priv = ieee80211_priv(dev);
3155 u32 *tail,*temp_tail;
3166 struct buffer* buflist;
3167 //unsigned long flags;
3168 #ifdef CONFIG_RTL8185B
3169 struct ieee80211_hdr_3addr *frag_hdr = (struct ieee80211_hdr_3addr *)txbuf;
3171 u8 bUseShortPreamble = 0;
3175 //u8 RetryLimit = 0;
3178 u16 ThisFrameTime = 0;
3179 u16 TxDescDuration = 0;
3180 u8 ownbit_flag = false; //added by david woo for sync Tx, 2007.12.14
3184 case MANAGE_PRIORITY:
3185 tail=priv->txmapringtail;
3186 begin=priv->txmapring;
3187 buflist = priv->txmapbufstail;
3188 count = priv->txringcount;
3192 tail=priv->txbkpringtail;
3193 begin=priv->txbkpring;
3194 buflist = priv->txbkpbufstail;
3195 count = priv->txringcount;
3199 tail=priv->txbepringtail;
3200 begin=priv->txbepring;
3201 buflist = priv->txbepbufstail;
3202 count = priv->txringcount;
3206 tail=priv->txvipringtail;
3207 begin=priv->txvipring;
3208 buflist = priv->txvipbufstail;
3209 count = priv->txringcount;
3213 tail=priv->txvopringtail;
3214 begin=priv->txvopring;
3215 buflist = priv->txvopbufstail;
3216 count = priv->txringcount;
3220 tail=priv->txhpringtail;
3221 begin=priv->txhpring;
3222 buflist = priv->txhpbufstail;
3223 count = priv->txringcount;
3226 case BEACON_PRIORITY:
3227 tail=priv->txbeaconringtail;
3228 begin=priv->txbeaconring;
3229 buflist = priv->txbeaconbufstail;
3230 count = priv->txbeaconcount;
3238 //printk("in rtl8180_tx(): rate is %d\n",priv->ieee80211->rate);
3240 memcpy(&dest, frag_hdr->addr1, ETH_ALEN);
3241 if (is_multicast_ether_addr(dest) ||
3242 is_broadcast_ether_addr(dest))
3249 ThisFrameTime = ComputeTxTime(len + sCrcLng, rtl8180_rate2rate(rate), 0, bUseShortPreamble);
3250 TxDescDuration = ThisFrameTime;
3251 } else {// Unicast packet
3255 //YJ,add,080828,for Keep alive
3256 priv->NumTxUnicast++;
3258 // Figure out ACK rate according to BSS basic rate and Tx rate, 2006.03.08 by rcnjko.
3259 //AckRate = ComputeAckRate( pMgntInfo->mBrates, (u1Byte)(pTcb->DataRate) );
3260 // Figure out ACK time according to the AckRate and assume long preamble is used on receiver, 2006.03.08, by rcnjko.
3261 //AckTime = ComputeTxTime( sAckCtsLng/8, AckRate, FALSE, FALSE);
3262 //For simplicity, just use the 1M basic rate
3263 //AckTime = ComputeTxTime(14, 540,0, 0); // AckCTSLng = 14 use 1M bps send
3264 AckTime = ComputeTxTime(14, 10,0, 0); // AckCTSLng = 14 use 1M bps send
3265 //AckTime = ComputeTxTime(14, 2,false, false); // AckCTSLng = 14 use 1M bps send
3267 if ( ((len + sCrcLng) > priv->rts) && priv->rts )
3269 u16 RtsTime, CtsTime;
3274 // Rate and time required for RTS.
3275 RtsTime = ComputeTxTime( sAckCtsLng/8,priv->ieee80211->basic_rate, 0, 0);
3276 // Rate and time required for CTS.
3277 CtsTime = ComputeTxTime(14, 10,0, 0); // AckCTSLng = 14 use 1M bps send
3279 // Figure out time required to transmit this frame.
3280 ThisFrameTime = ComputeTxTime(len + sCrcLng,
3281 rtl8180_rate2rate(rate),
3285 // RTS-CTS-ThisFrame-ACK.
3286 RtsDur = CtsTime + ThisFrameTime + AckTime + 3*aSifsTime;
3288 TxDescDuration = RtsTime + RtsDur;
3290 else {// Normal case.
3295 ThisFrameTime = ComputeTxTime(len + sCrcLng, rtl8180_rate2rate(rate), 0, bUseShortPreamble);
3296 TxDescDuration = ThisFrameTime + aSifsTime + AckTime;
3299 if(!(frag_hdr->frame_ctl & IEEE80211_FCTL_MOREFRAGS)) { //no more fragment
3301 Duration = aSifsTime + AckTime;
3302 } else { // One or more fragments remained.
3304 NextFragTime = ComputeTxTime( len + sCrcLng, //pretend following packet length equal current packet
3305 rtl8180_rate2rate(rate),
3307 bUseShortPreamble );
3309 //ThisFrag-ACk-NextFrag-ACK.
3310 Duration = NextFragTime + 3*aSifsTime + 2*AckTime;
3313 } // End of Unicast packet
3315 frag_hdr->duration_id = Duration;
3318 buflen=priv->txbuffsize;
3321 //printk("================================>buflen = %d, remain = %d!\n", buflen,remain);
3323 #ifdef DEBUG_TX_FRAG
3324 DMESG("TX iteration");
3327 DMESG("TX: filling descriptor %x",(u32)tail);
3331 DMESGE("TX buffer error, cannot TX frames. pri %d.", priority);
3332 //spin_unlock_irqrestore(&priv->tx_lock,flags);
3337 if( (*tail & (1<<31)) && (priority != BEACON_PRIORITY)){
3339 DMESGW("No more TX desc, returning %x of %x",
3341 priv->stats.txrdu++;
3342 #ifdef DEBUG_TX_DESC
3343 check_tx_ring(dev,priority);
3344 // netif_stop_queue(dev);
3345 // netif_carrier_off(dev);
3347 // spin_unlock_irqrestore(&priv->tx_lock,flags);
3353 *tail= 0; // zeroes header
3360 if(priv->card_8185){
3361 //FIXME: this should be triggered by HW encryption parameters.
3362 *tail |= (1<<15); //no encrypt
3363 // *tail |= (1<<30); //raise int when completed
3365 // *tail = *tail | (1<<16);
3366 if(remain==len && !descfrag) {
3367 ownbit_flag = false; //added by david woo,2007.12.14
3368 #ifdef DEBUG_TX_FRAG
3369 DMESG("First descriptor");
3371 *tail = *tail| (1<<29) ; //fist segment of the packet
3372 *tail = *tail |(len);
3377 for(i=0;i<buflen&& remain >0;i++,remain--){
3378 ((u8*)buf)[i]=txbuf[i]; //copy data into descriptor pointed DMAble buffer
3379 if(remain == 4 && i+4 >= buflen) break;
3380 /* ensure the last desc has at least 4 bytes payload */
3384 *(tail+3)=*(tail+3) &~ 0xfff;
3385 *(tail+3)=*(tail+3) | i; // buffer lenght
3386 // Use short preamble or not
3387 if (priv->ieee80211->current_network.capability&WLAN_CAPABILITY_SHORT_PREAMBLE)
3388 if (priv->plcp_preamble_mode==1 && rate!=0) // short mode now, not long!
3389 // *tail |= (1<<16); // enable short preamble mode.
3391 #ifdef CONFIG_RTL8185B
3396 if(bRTSEnable) //rts enable
3398 *tail |= ((ieeerate2rtlrate(priv->ieee80211->basic_rate))<<19);//RTS RATE
3399 *tail |= (1<<23);//rts enable
3400 *(tail+1) |=(RtsDur&0xffff);//RTS Duration
3402 *(tail+3) |= ((TxDescDuration&0xffff)<<16); //DURATION
3403 // *(tail+3) |= (0xe6<<16);
3404 *(tail+5) |= (11<<8);//(priv->retry_data<<8); //retry lim ;
3407 #ifdef CONFIG_RTL8187B
3408 if ( (len>priv->rts) && priv->rts && priority!=MANAGE_PRIORITY){
3410 if ( (len>priv->rts) && priv->rts && priority==LOW_PRIORITY){
3412 *tail |= (1<<23); //enalbe RTS function
3413 *tail |= (0<<19); //use 1M bps send RTS packet
3414 AckCtsTime = ComputeTxTime(14, 10,0, 0); // AckCTSLng = 14 use 1M bps send
3415 FrameTime = ComputeTxTime(len + 4, rtl8180_rate2rate(rate), 0, *tail&(1<<16));
3416 // RTS/CTS time is calculate as follow
3417 duration = FrameTime + 3*10 + 2*AckCtsTime; //10us is the SifsTime;
3418 *(tail+1) |= duration; //Need to edit here! ----hikaru
3420 *(tail+1)= 0; // zeroes the second 32-bits dword of the descriptor
3424 *tail = *tail | ((rate&0xf) << 24);
3425 //DMESG("rate %d",rate);
3427 if(priv->card_8185){
3430 *(tail+5)&= ~(1<<24); /* tx ant 0 */
3432 *(tail+5) &= ~(1<<23); /* random tx agc 23-16 */
3433 *(tail+5) |= (1<<22)|(1<<21)|(1<<20)|(1<<19)|(1<<18)|(1<<17)|(1<<16);
3436 ~((1<<15)|(1<<14)|(1<<13)|(1<<12)|(1<<11)|(1<<10)|(1<<9)|(1<<8));
3437 *(tail+5) |= (7<<8); // Max retry limit
3439 *(tail+5) &= ~((1<<7)|(1<<6)|(1<<5)|(1<<4)|(1<<3)|(1<<2)|(1<<1)|(1<<0));
3440 *(tail+5) |= (8<<4); // Max contention window
3441 *(tail+6) |= 4; // Min contention window
3446 /* hw_plcp_len is not used for rtl8180 chip */
3448 if(priv->card_8185 == 0 || !priv->hw_plcp_len){
3450 duration = rtl8180_len2duration(len,
3455 DMESG("PLCP duration %d",duration );
3456 //DMESG("drift %d",drift);
3457 DMESG("extension %s", (ext==1) ? "on":"off");
3459 *(tail+1) = *(tail+1) | ((duration & 0x7fff)<<16);
3460 if(ext) *(tail+1) = *(tail+1) |(1<<31); //plcp length extension
3463 if(morefrag) *tail = (*tail) | (1<<17); // more fragment
3464 if(!remain) *tail = (*tail) | (1<<28); // last segment of frame
3466 #ifdef DEBUG_TX_FRAG
3467 if(!remain)DMESG("Last descriptor");
3468 if(morefrag)DMESG("More frag");
3470 *(tail+5) = *(tail+5)|(2<<27);
3471 *(tail+7) = *(tail+7)|(1<<4);
3476 *tail = *tail | (1<<31); // descriptor ready to be txed
3479 #ifdef DEBUG_TX_DESC2
3480 printk("tx desc is:\n");
3481 DMESG("%8x %8x %8x %8x %8x %8x %8x %8x", tail[0], tail[1], tail[2], tail[3],
3482 tail[4], tail[5], tail[6], tail[7]);
3485 if((tail - begin)/8 == count-1)
3491 buflist=buflist->next;
3496 case MANAGE_PRIORITY:
3497 priv->txmapringtail=tail;
3498 priv->txmapbufstail=buflist;
3502 priv->txbkpringtail=tail;
3503 priv->txbkpbufstail=buflist;
3507 priv->txbepringtail=tail;
3508 priv->txbepbufstail=buflist;
3512 priv->txvipringtail=tail;
3513 priv->txvipbufstail=buflist;
3517 priv->txvopringtail=tail;
3518 priv->txvopbufstail=buflist;
3522 priv->txhpringtail=tail;
3523 priv->txhpbufstail = buflist;
3526 case BEACON_PRIORITY:
3527 /* the HW seems to be happy with the 1st
3528 * descriptor filled and the 2nd empty...
3529 * So always update descriptor 1 and never
3532 // priv->txbeaconringtail=tail;
3533 // priv->txbeaconbufstail=buflist;
3539 //rtl8180_dma_kick(dev,priority);
3541 *temp_tail = *temp_tail | (1<<31); // descriptor ready to be txed
3542 rtl8180_dma_kick(dev,priority);
3543 //spin_unlock_irqrestore(&priv->tx_lock,flags);
3550 void rtl8180_irq_rx_tasklet(struct r8180_priv * priv);
3553 void rtl8180_link_change(struct net_device *dev)
3555 struct r8180_priv *priv = ieee80211_priv(dev);
3556 u16 beacon_interval;
3558 struct ieee80211_network *net = &priv->ieee80211->current_network;
3559 // rtl8180_adapter_start(dev);
3560 rtl8180_update_msr(dev);
3563 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
3565 write_nic_dword(dev,BSSID,((u32*)net->bssid)[0]);
3566 write_nic_word(dev,BSSID+4,((u16*)net->bssid)[2]);
3569 beacon_interval = read_nic_dword(dev,BEACON_INTERVAL);
3570 beacon_interval &= ~ BEACON_INTERVAL_MASK;
3571 beacon_interval |= net->beacon_interval;
3572 write_nic_dword(dev, BEACON_INTERVAL, beacon_interval);
3574 rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
3578 u16 atim = read_nic_dword(dev,ATIM);
3579 u16 = u16 &~ ATIM_MASK;
3580 u16 = u16 | beacon->atim;
3583 if (net->capability & WLAN_CAPABILITY_PRIVACY) {
3585 DMESG("Enabling hardware WEP support");
3586 rtl8180_set_hw_wep(dev);
3587 priv->ieee80211->host_encrypt=0;
3588 priv->ieee80211->host_decrypt=0;
3590 #ifndef CONFIG_IEEE80211_NOWEP
3592 priv->ieee80211->host_encrypt=1;
3593 priv->ieee80211->host_decrypt=1;
3597 #ifndef CONFIG_IEEE80211_NOWEP
3599 priv->ieee80211->host_encrypt=0;
3600 priv->ieee80211->host_decrypt=0;
3607 rtl8180_set_chan(dev, priv->chan);
3612 void rtl8180_rq_tx_ack(struct net_device *dev){
3614 struct r8180_priv *priv = ieee80211_priv(dev);
3615 // printk("====================>%s\n",__func__);
3616 write_nic_byte(dev,CONFIG4,read_nic_byte(dev,CONFIG4)|CONFIG4_PWRMGT);
3617 priv->ack_tx_to_ieee = 1;
3620 short rtl8180_is_tx_queue_empty(struct net_device *dev){
3622 struct r8180_priv *priv = ieee80211_priv(dev);
3625 for (d = priv->txmapring;
3626 d < priv->txmapring + priv->txringcount;d+=8)
3627 if(*d & (1<<31)) return 0;
3629 for (d = priv->txbkpring;
3630 d < priv->txbkpring + priv->txringcount;d+=8)
3631 if(*d & (1<<31)) return 0;
3633 for (d = priv->txbepring;
3634 d < priv->txbepring + priv->txringcount;d+=8)
3635 if(*d & (1<<31)) return 0;
3637 for (d = priv->txvipring;
3638 d < priv->txvipring + priv->txringcount;d+=8)
3639 if(*d & (1<<31)) return 0;
3641 for (d = priv->txvopring;
3642 d < priv->txvopring + priv->txringcount;d+=8)
3643 if(*d & (1<<31)) return 0;
3645 for (d = priv->txhpring;
3646 d < priv->txhpring + priv->txringcount;d+=8)
3647 if(*d & (1<<31)) return 0;
3650 /* FIXME FIXME 5msecs is random */
3651 #define HW_WAKE_DELAY 5
3653 void rtl8180_hw_wakeup(struct net_device *dev)
3655 unsigned long flags;
3657 struct r8180_priv *priv = ieee80211_priv(dev);
3659 spin_lock_irqsave(&priv->ps_lock,flags);
3660 //DMESG("Waken up!");
3661 write_nic_byte(dev,CONFIG4,read_nic_byte(dev,CONFIG4)&~CONFIG4_PWRMGT);
3664 priv->rf_wakeup(dev);
3665 // mdelay(HW_WAKE_DELAY);
3666 spin_unlock_irqrestore(&priv->ps_lock,flags);
3669 void rtl8180_hw_sleep_down(struct net_device *dev)
3671 unsigned long flags;
3673 struct r8180_priv *priv = ieee80211_priv(dev);
3675 spin_lock_irqsave(&priv->ps_lock,flags);
3679 priv->rf_sleep(dev);
3680 spin_unlock_irqrestore(&priv->ps_lock,flags);
3684 void rtl8180_hw_sleep(struct net_device *dev, u32 th, u32 tl)
3687 struct r8180_priv *priv = ieee80211_priv(dev);
3690 unsigned long flags;
3692 spin_lock_irqsave(&priv->ps_lock,flags);
3694 /* Writing HW register with 0 equals to disable
3695 * the timer, that is not really what we want
3697 tl -= MSECS(4+16+7);
3699 //if(tl == 0) tl = 1;
3701 /* FIXME HACK FIXME HACK */
3702 // force_pci_posting(dev);
3705 // rb = read_nic_dword(dev, TSFTR);
3707 /* If the interval in witch we are requested to sleep is too
3708 * short then give up and remain awake
3710 if(((tl>=rb)&& (tl-rb) <= MSECS(MIN_SLEEP_TIME))
3711 ||((rb>tl)&& (rb-tl) < MSECS(MIN_SLEEP_TIME))) {
3712 spin_unlock_irqrestore(&priv->ps_lock,flags);
3713 printk("too short to sleep\n");
3717 // write_nic_dword(dev, TimerInt, tl);
3718 // rb = read_nic_dword(dev, TSFTR);
3720 u32 tmp = (tl>rb)?(tl-rb):(rb-tl);
3724 priv->DozePeriodInPast2Sec += jiffies_to_msecs(tmp);
3726 queue_delayed_work(priv->ieee80211->wq, &priv->ieee80211->hw_wakeup_wq, tmp); //as tl may be less than rb
3728 /* if we suspect the TimerInt is gone beyond tl
3729 * while setting it, then give up
3732 if(((tl > rb) && ((tl-rb) > MSECS(MAX_SLEEP_TIME)))||
3733 ((tl < rb) && ((rb-tl) > MSECS(MAX_SLEEP_TIME)))) {
3734 spin_unlock_irqrestore(&priv->ps_lock,flags);
3738 // if(priv->rf_sleep)
3739 // priv->rf_sleep(dev);
3741 queue_work(priv->ieee80211->wq, (void *)&priv->ieee80211->hw_sleep_wq);
3742 spin_unlock_irqrestore(&priv->ps_lock,flags);
3746 //void rtl8180_wmm_param_update(struct net_device *dev,u8 *ac_param)
3747 void rtl8180_wmm_param_update(struct work_struct * work)
3749 struct ieee80211_device * ieee = container_of(work, struct ieee80211_device,wmm_param_update_wq);
3750 //struct r8180_priv *priv = (struct r8180_priv*)(ieee->priv);
3751 struct net_device *dev = ieee->dev;
3752 u8 *ac_param = (u8 *)(ieee->current_network.wmm_param);
3753 u8 mode = ieee->current_network.mode;
3759 #ifndef CONFIG_RTL8185B
3760 //for legacy 8185 keep the PARAM unchange.
3763 if(!ieee->current_network.QoS_Enable){
3764 //legacy ac_xx_param update
3765 AcParam.longData = 0;
3766 AcParam.f.AciAifsn.f.AIFSN = 2; // Follow 802.11 DIFS.
3767 AcParam.f.AciAifsn.f.ACM = 0;
3768 AcParam.f.Ecw.f.ECWmin = 3; // Follow 802.11 CWmin.
3769 AcParam.f.Ecw.f.ECWmax = 7; // Follow 802.11 CWmax.
3770 AcParam.f.TXOPLimit = 0;
3771 for(eACI = 0; eACI < AC_MAX; eACI++){
3772 AcParam.f.AciAifsn.f.ACI = (u8)eACI;
3776 pAcParam = (PAC_PARAM)(&AcParam);
3777 // Retrive paramters to udpate.
3778 u1bAIFS = pAcParam->f.AciAifsn.f.AIFSN *(((mode&IEEE_G) == IEEE_G)?9:20) + aSifsTime;
3779 u4bAcParam = ((((u32)(pAcParam->f.TXOPLimit))<<AC_PARAM_TXOP_LIMIT_OFFSET)|
3780 (((u32)(pAcParam->f.Ecw.f.ECWmax))<<AC_PARAM_ECW_MAX_OFFSET)|
3781 (((u32)(pAcParam->f.Ecw.f.ECWmin))<<AC_PARAM_ECW_MIN_OFFSET)|
3782 (((u32)u1bAIFS) << AC_PARAM_AIFS_OFFSET));
3785 write_nic_dword(dev, AC_BK_PARAM, u4bAcParam);
3789 write_nic_dword(dev, AC_BE_PARAM, u4bAcParam);
3793 write_nic_dword(dev, AC_VI_PARAM, u4bAcParam);
3797 write_nic_dword(dev, AC_VO_PARAM, u4bAcParam);
3801 printk(KERN_WARNING "SetHwReg8185():invalid ACI: %d!\n", eACI);
3809 for(i = 0; i < AC_MAX; i++){
3810 //AcParam.longData = 0;
3811 pAcParam = (AC_PARAM * )ac_param;
3817 // Retrive paramters to udpate.
3818 eACI = pAcParam->f.AciAifsn.f.ACI;
3819 //Mode G/A: slotTimeTimer = 9; Mode B: 20
3820 u1bAIFS = pAcParam->f.AciAifsn.f.AIFSN * (((mode&IEEE_G) == IEEE_G)?9:20) + aSifsTime;
3821 u4bAcParam = ( (((u32)(pAcParam->f.TXOPLimit)) << AC_PARAM_TXOP_LIMIT_OFFSET) |
3822 (((u32)(pAcParam->f.Ecw.f.ECWmax)) << AC_PARAM_ECW_MAX_OFFSET) |
3823 (((u32)(pAcParam->f.Ecw.f.ECWmin)) << AC_PARAM_ECW_MIN_OFFSET) |
3824 (((u32)u1bAIFS) << AC_PARAM_AIFS_OFFSET));
3828 write_nic_dword(dev, AC_BK_PARAM, u4bAcParam);
3832 write_nic_dword(dev, AC_BE_PARAM, u4bAcParam);
3836 write_nic_dword(dev, AC_VI_PARAM, u4bAcParam);
3840 write_nic_dword(dev, AC_VO_PARAM, u4bAcParam);
3844 printk(KERN_WARNING "SetHwReg8185(): invalid ACI: %d !\n", eACI);
3848 ac_param += (sizeof(AC_PARAM));
3853 void rtl8180_tx_irq_wq(struct work_struct *work);
3854 void rtl8180_restart_wq(struct work_struct *work);
3855 //void rtl8180_rq_tx_ack(struct work_struct *work);
3856 void rtl8180_watch_dog_wq(struct work_struct *work);
3857 void rtl8180_hw_wakeup_wq(struct work_struct *work);
3858 void rtl8180_hw_sleep_wq(struct work_struct *work);
3859 void rtl8180_sw_antenna_wq(struct work_struct *work);
3860 void rtl8180_watch_dog(struct net_device *dev);
3862 void watch_dog_adaptive(unsigned long data)
3864 struct r8180_priv* priv = ieee80211_priv((struct net_device *)data);
3865 // DMESG("---->watch_dog_adaptive()\n");
3868 DMESG("<----watch_dog_adaptive():driver is not up!\n");
3872 // queue_work(priv->ieee80211->wq,&priv->ieee80211->watch_dog_wq);
3875 // Tx High Power Mechanism.
3877 if(CheckHighPower((struct net_device *)data))
3879 queue_work(priv->ieee80211->wq, (void *)&priv->ieee80211->tx_pw_wq);
3883 #ifdef CONFIG_RTL818X_S
3884 // Tx Power Tracking on 87SE.
3886 //if( priv->bTxPowerTrack ) //lzm mod 080826
3887 if( CheckTxPwrTracking((struct net_device *)data));
3888 TxPwrTracking87SE((struct net_device *)data);
3892 // Perform DIG immediately.
3894 if(CheckDig((struct net_device *)data) == true)
3896 queue_work(priv->ieee80211->wq, (void *)&priv->ieee80211->hw_dig_wq);
3901 rtl8180_watch_dog((struct net_device *)data);
3904 queue_work(priv->ieee80211->wq, (void *)&priv->ieee80211->GPIOChangeRFWorkItem);
3906 priv->watch_dog_timer.expires = jiffies + MSECS(IEEE80211_WATCH_DOG_TIME);
3907 add_timer(&priv->watch_dog_timer);
3908 // DMESG("<----watch_dog_adaptive()\n");
3911 #ifdef ENABLE_DOT11D
3913 static CHANNEL_LIST ChannelPlan[] = {
3914 {{1,2,3,4,5,6,7,8,9,10,11,36,40,44,48,52,56,60,64},19}, //FCC
3915 {{1,2,3,4,5,6,7,8,9,10,11},11}, //IC
3916 {{1,2,3,4,5,6,7,8,9,10,11,12,13,36,40,44,48,52,56,60,64},21}, //ETSI
3917 {{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.
3918 {{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.
3919 {{14,36,40,44,48,52,56,60,64},9}, //MKK
3920 {{1,2,3,4,5,6,7,8,9,10,11,12,13,14, 36,40,44,48,52,56,60,64},22},//MKK1
3921 {{1,2,3,4,5,6,7,8,9,10,11,12,13,36,40,44,48,52,56,60,64},21}, //Israel.
3922 {{1,2,3,4,5,6,7,8,9,10,11,12,13,34,38,42,46},17}, // For 11a , TELEC
3923 {{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
3924 {{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
3927 static void rtl8180_set_channel_map(u8 channel_plan, struct ieee80211_device *ieee)
3932 ieee->MinPassiveChnlNum=MAX_CHANNEL_NUMBER+1;
3933 ieee->IbssStartChnl=0;
3935 switch (channel_plan)
3937 case COUNTRY_CODE_FCC:
3938 case COUNTRY_CODE_IC:
3939 case COUNTRY_CODE_ETSI:
3940 case COUNTRY_CODE_SPAIN:
3941 case COUNTRY_CODE_FRANCE:
3942 case COUNTRY_CODE_MKK:
3943 case COUNTRY_CODE_MKK1:
3944 case COUNTRY_CODE_ISRAEL:
3945 case COUNTRY_CODE_TELEC:
3948 ieee->bGlobalDomain = false;
3949 if (ChannelPlan[channel_plan].Len != 0){
3950 // Clear old channel map
3951 memset(GET_DOT11D_INFO(ieee)->channel_map, 0, sizeof(GET_DOT11D_INFO(ieee)->channel_map));
3952 // Set new channel map
3953 for (i=0;i<ChannelPlan[channel_plan].Len;i++)
3955 if(ChannelPlan[channel_plan].Channel[i] <= 14)
3956 GET_DOT11D_INFO(ieee)->channel_map[ChannelPlan[channel_plan].Channel[i]] = 1;
3961 case COUNTRY_CODE_GLOBAL_DOMAIN:
3963 GET_DOT11D_INFO(ieee)->bEnabled = 0;
3965 ieee->bGlobalDomain = true;
3968 case COUNTRY_CODE_WORLD_WIDE_13_INDEX://lzm add 080826
3970 ieee->MinPassiveChnlNum=12;
3971 ieee->IbssStartChnl= 10;
3977 ieee->bGlobalDomain = false;
3978 memset(GET_DOT11D_INFO(ieee)->channel_map, 0, sizeof(GET_DOT11D_INFO(ieee)->channel_map));
3981 GET_DOT11D_INFO(ieee)->channel_map[i] = 1;
3989 //Add for RF power on power off by lizhaoming 080512
3990 void GPIOChangeRFWorkItemCallBack(struct work_struct *work);
3993 static void rtl8180_statistics_init(struct Stats *pstats)
3995 memset(pstats, 0, sizeof(struct Stats));
3997 static void rtl8180_link_detect_init(plink_detect_t plink_detect)
3999 memset(plink_detect, 0, sizeof(link_detect_t));
4000 plink_detect->SlotNum = DEFAULT_SLOT_NUM;
4004 short rtl8180_init(struct net_device *dev)
4006 struct r8180_priv *priv = ieee80211_priv(dev);
4015 #ifdef ENABLE_DOT11D
4017 for(i=0;i<0xFF;i++) {
4019 printk("\n[%x]: ", i/16);
4020 printk("\t%4.4x", eprom_read(dev,i));
4023 priv->channel_plan = eprom_read(dev, EEPROM_COUNTRY_CODE>>1) & 0xFF;
4024 if(priv->channel_plan > COUNTRY_CODE_GLOBAL_DOMAIN){
4025 printk("rtl8180_init:Error channel plan! Set to default.\n");
4026 priv->channel_plan = 0;
4028 //priv->channel_plan = 9; //Global Domain
4030 DMESG("Channel plan is %d\n",priv->channel_plan);
4031 rtl8180_set_channel_map(priv->channel_plan, priv->ieee80211);
4034 //Set Default Channel Plan
4036 DMESG("No channels, aborting");
4040 priv->channel_plan = 0;//hikaru
4041 // set channels 1..14 allowed in given locale
4042 for (i=1; i<=14; i++) {
4043 (priv->ieee80211->channel_map)[i] = (u8)(ch & 0x01);
4048 //memcpy(priv->stats,0,sizeof(struct Stats));
4050 //FIXME: these constants are placed in a bad pleace.
4051 priv->txbuffsize = 2048;//1024;
4052 priv->txringcount = 32;//32;
4053 priv->rxbuffersize = 2048;//1024;
4054 priv->rxringcount = 64;//32;
4055 priv->txbeaconcount = 2;
4056 priv->rx_skb_complete = 1;
4057 //priv->txnp_pending.ispending=0;
4058 /* ^^ the SKB does not containt a partial RXed
4062 #ifdef CONFIG_RTL8185B
4063 #ifdef CONFIG_RTL818X_S
4064 priv->RegThreeWireMode = HW_THREE_WIRE_SI;
4066 priv->RegThreeWireMode = SW_THREE_WIRE;
4070 //Add for RF power on power off by lizhaoming 080512
4071 priv->RFChangeInProgress = false;
4072 priv->SetRFPowerStateInProgress = false;
4073 priv->RFProgType = 0;
4074 priv->bInHctTest = false;
4076 priv->irq_enabled=0;
4078 //YJ,modified,080828
4080 priv->stats.rxdmafail=0;
4081 priv->stats.txrdu=0;
4082 priv->stats.rxrdu=0;
4083 priv->stats.rxnolast=0;
4084 priv->stats.rxnodata=0;
4085 //priv->stats.rxreset=0;
4086 //priv->stats.rxwrkaround=0;
4087 priv->stats.rxnopointer=0;
4088 priv->stats.txnperr=0;
4089 priv->stats.txresumed=0;
4090 priv->stats.rxerr=0;
4091 priv->stats.rxoverflow=0;
4092 priv->stats.rxint=0;
4093 priv->stats.txnpokint=0;
4094 priv->stats.txhpokint=0;
4095 priv->stats.txhperr=0;
4097 priv->stats.shints=0;
4098 priv->stats.txoverflow=0;
4099 priv->stats.txbeacon=0;
4100 priv->stats.txbeaconerr=0;
4101 priv->stats.txlperr=0;
4102 priv->stats.txlpokint=0;
4103 priv->stats.txretry=0;//tony 20060601
4104 priv->stats.rxcrcerrmin=0;
4105 priv->stats.rxcrcerrmid=0;
4106 priv->stats.rxcrcerrmax=0;
4107 priv->stats.rxicverr=0;
4109 rtl8180_statistics_init(&priv->stats);
4110 rtl8180_link_detect_init(&priv->link_detect);
4112 //YJ,modified,080828,end
4115 priv->ack_tx_to_ieee = 0;
4116 priv->ieee80211->current_network.beacon_interval = DEFAULT_BEACONINTERVAL;
4117 priv->ieee80211->iw_mode = IW_MODE_INFRA;
4118 priv->ieee80211->softmac_features = IEEE_SOFTMAC_SCAN |
4119 IEEE_SOFTMAC_ASSOCIATE | IEEE_SOFTMAC_PROBERQ |
4120 IEEE_SOFTMAC_PROBERS | IEEE_SOFTMAC_TX_QUEUE;
4121 priv->ieee80211->active_scan = 1;
4122 priv->ieee80211->rate = 110; //11 mbps
4123 priv->ieee80211->modulation = IEEE80211_CCK_MODULATION;
4124 priv->ieee80211->host_encrypt = 1;
4125 priv->ieee80211->host_decrypt = 1;
4126 priv->ieee80211->sta_wake_up = rtl8180_hw_wakeup;
4127 priv->ieee80211->ps_request_tx_ack = rtl8180_rq_tx_ack;
4128 priv->ieee80211->enter_sleep_state = rtl8180_hw_sleep;
4129 priv->ieee80211->ps_is_queue_empty = rtl8180_is_tx_queue_empty;
4131 priv->hw_wep = hwwep;
4134 priv->retry_rts = DEFAULT_RETRY_RTS;
4135 priv->retry_data = DEFAULT_RETRY_DATA;
4136 priv->RFChangeInProgress = false;
4137 priv->SetRFPowerStateInProgress = false;
4138 priv->RFProgType = 0;
4139 priv->bInHctTest = false;
4140 priv->bInactivePs = true;//false;
4141 priv->ieee80211->bInactivePs = priv->bInactivePs;
4142 priv->bSwRfProcessing = false;
4143 priv->eRFPowerState = eRfOff;
4144 priv->RfOffReason = 0;
4145 priv->LedStrategy = SW_LED_MODE0;
4146 //priv->NumRxOkInPeriod = 0; //YJ,del,080828
4147 //priv->NumTxOkInPeriod = 0; //YJ,del,080828
4148 priv->TxPollingTimes = 0;//lzm add 080826
4149 priv->bLeisurePs = true;
4150 priv->dot11PowerSaveMode = eActive;
4151 //by amy for antenna
4152 priv->AdMinCheckPeriod = 5;
4153 priv->AdMaxCheckPeriod = 10;
4154 // Lower signal strength threshold to fit the HW participation in antenna diversity. +by amy 080312
4155 priv->AdMaxRxSsThreshold = 30;//60->30
4156 priv->AdRxSsThreshold = 20;//50->20
4157 priv->AdCheckPeriod = priv->AdMinCheckPeriod;
4158 priv->AdTickCount = 0;
4159 priv->AdRxSignalStrength = -1;
4160 priv->RegSwAntennaDiversityMechanism = 0;
4161 priv->RegDefaultAntenna = 0;
4162 priv->SignalStrength = 0;
4163 priv->AdRxOkCnt = 0;
4164 priv->CurrAntennaIndex = 0;
4165 priv->AdRxSsBeforeSwitched = 0;
4166 init_timer(&priv->SwAntennaDiversityTimer);
4167 priv->SwAntennaDiversityTimer.data = (unsigned long)dev;
4168 priv->SwAntennaDiversityTimer.function = (void *)SwAntennaDiversityTimerCallback;
4169 //by amy for antenna
4171 priv->bDigMechanism = 1;
4172 priv->InitialGain = 6;
4173 priv->bXtalCalibration = false;
4174 priv->XtalCal_Xin = 0;
4175 priv->XtalCal_Xout = 0;
4176 priv->bTxPowerTrack = false;
4177 priv->ThermalMeter = 0;
4178 priv->FalseAlarmRegValue = 0;
4179 priv->RegDigOfdmFaUpTh = 0xc; // Upper threhold of OFDM false alarm, which is used in DIG.
4180 priv->DIG_NumberFallbackVote = 0;
4181 priv->DIG_NumberUpgradeVote = 0;
4182 priv->LastSignalStrengthInPercent = 0;
4183 priv->Stats_SignalStrength = 0;
4184 priv->LastRxPktAntenna = 0;
4185 priv->SignalQuality = 0; // in 0-100 index.
4186 priv->Stats_SignalQuality = 0;
4187 priv->RecvSignalPower = 0; // in dBm.
4188 priv->Stats_RecvSignalPower = 0;
4189 priv->AdMainAntennaRxOkCnt = 0;
4190 priv->AdAuxAntennaRxOkCnt = 0;
4191 priv->bHWAdSwitched = false;
4192 priv->bRegHighPowerMechanism = true;
4193 priv->RegHiPwrUpperTh = 77;
4194 priv->RegHiPwrLowerTh = 75;
4195 priv->RegRSSIHiPwrUpperTh = 70;
4196 priv->RegRSSIHiPwrLowerTh = 20;
4197 priv->bCurCCKPkt = false;
4198 priv->UndecoratedSmoothedSS = -1;
4199 priv->bToUpdateTxPwr = false;
4200 priv->CurCCKRSSI = 0;
4204 priv->NumTxOkTotal = 0;
4205 priv->NumTxUnicast = 0;
4206 priv->keepAliveLevel = DEFAULT_KEEP_ALIVE_LEVEL;
4207 priv->PowerProfile = POWER_PROFILE_AC;
4209 //by amy for rate adaptive
4210 priv->CurrRetryCnt=0;
4211 priv->LastRetryCnt=0;
4212 priv->LastTxokCnt=0;
4213 priv->LastRxokCnt=0;
4214 priv->LastRetryRate=0;
4217 priv->CurrRetryRate=0;
4218 priv->TryupingCount=0;
4219 priv->TryupingCountNoData=0;
4220 priv->TryDownCountLowData=0;
4221 priv->LastTxOKBytes=0;
4222 priv->LastFailTxRate=0;
4223 priv->LastFailTxRateSS=0;
4224 priv->FailTxRateCount=0;
4225 priv->LastTxThroughput=0;
4226 priv->NumTxOkBytesTotal=0;
4227 priv->ForcedDataRate = 0;
4228 priv->RegBModeGainStage = 1;
4230 //by amy for rate adaptive
4232 priv->promisc = (dev->flags & IFF_PROMISC) ? 1:0;
4233 spin_lock_init(&priv->irq_lock);
4234 spin_lock_init(&priv->irq_th_lock);
4235 spin_lock_init(&priv->tx_lock);
4236 spin_lock_init(&priv->ps_lock);
4237 spin_lock_init(&priv->rf_ps_lock);
4238 sema_init(&priv->wx_sem,1);
4239 sema_init(&priv->rf_state,1);
4240 INIT_WORK(&priv->reset_wq,(void*) rtl8180_restart_wq);
4241 INIT_WORK(&priv->tx_irq_wq,(void*) rtl8180_tx_irq_wq);
4242 INIT_DELAYED_WORK(&priv->ieee80211->hw_wakeup_wq,(void*) rtl8180_hw_wakeup_wq);
4243 INIT_DELAYED_WORK(&priv->ieee80211->hw_sleep_wq,(void*) rtl8180_hw_sleep_wq);
4244 //INIT_DELAYED_WORK(&priv->ieee80211->watch_dog_wq,(void*) rtl8180_watch_dog_wq);
4245 //INIT_DELAYED_WORK(&priv->ieee80211->sw_antenna_wq,(void*) rtl8180_sw_antenna_wq);
4246 INIT_WORK(&priv->ieee80211->wmm_param_update_wq,(void*) rtl8180_wmm_param_update);
4247 INIT_DELAYED_WORK(&priv->ieee80211->rate_adapter_wq,(void*)rtl8180_rate_adapter);//+by amy 080312
4248 INIT_DELAYED_WORK(&priv->ieee80211->hw_dig_wq,(void*)rtl8180_hw_dig_wq);//+by amy 080312
4249 INIT_DELAYED_WORK(&priv->ieee80211->tx_pw_wq,(void*)rtl8180_tx_pw_wq);//+by amy 080312
4251 //add for RF power on power off by lizhaoming 080512
4252 INIT_DELAYED_WORK(&priv->ieee80211->GPIOChangeRFWorkItem,(void*) GPIOChangeRFWorkItemCallBack);
4253 //INIT_WORK(&priv->reset_wq,(void*) rtl8180_restart_wq,dev);
4255 tasklet_init(&priv->irq_rx_tasklet,
4256 (void(*)(unsigned long)) rtl8180_irq_rx_tasklet,
4257 (unsigned long)priv);
4259 init_timer(&priv->watch_dog_timer);
4260 priv->watch_dog_timer.data = (unsigned long)dev;
4261 priv->watch_dog_timer.function = watch_dog_adaptive;
4265 //by amy for rate adaptive
4266 init_timer(&priv->rateadapter_timer);
4267 priv->rateadapter_timer.data = (unsigned long)dev;
4268 priv->rateadapter_timer.function = timer_rate_adaptive;
4269 priv->RateAdaptivePeriod= RATE_ADAPTIVE_TIMER_PERIOD;
4270 priv->bEnhanceTxPwr=false;
4271 //by amy for rate adaptive
4273 //priv->ieee80211->func =
4274 // kmalloc(sizeof(struct ieee80211_helper_functions),GFP_KERNEL);
4275 //memset(priv->ieee80211->func, 0,
4276 // sizeof(struct ieee80211_helper_functions));
4278 priv->ieee80211->softmac_hard_start_xmit = rtl8180_hard_start_xmit;
4279 priv->ieee80211->set_chan = rtl8180_set_chan;
4280 priv->ieee80211->link_change = rtl8180_link_change;
4281 priv->ieee80211->softmac_data_hard_start_xmit = rtl8180_hard_data_xmit;
4282 priv->ieee80211->data_hard_stop = rtl8180_data_hard_stop;
4283 priv->ieee80211->data_hard_resume = rtl8180_data_hard_resume;
4285 priv->ieee80211->init_wmmparam_flag = 0;
4287 priv->ieee80211->start_send_beacons = rtl8180_start_tx_beacon;
4288 priv->ieee80211->stop_send_beacons = rtl8180_beacon_tx_disable;
4289 priv->ieee80211->fts = DEFAULT_FRAG_THRESHOLD;
4291 #ifdef CONFIG_RTL8185B
4292 priv->MWIEnable = 0;
4294 priv->ShortRetryLimit = 7;
4295 priv->LongRetryLimit = 7;
4296 priv->EarlyRxThreshold = 7;
4298 priv->CSMethod = (0x01 << 29);
4300 priv->TransmitConfig =
4301 1<<TCR_DurProcMode_OFFSET | //for RTL8185B, duration setting by HW
4302 (7<<TCR_MXDMA_OFFSET) | // Max DMA Burst Size per Tx DMA Burst, 7: reservied.
4303 (priv->ShortRetryLimit<<TCR_SRL_OFFSET) | // Short retry limit
4304 (priv->LongRetryLimit<<TCR_LRL_OFFSET) | // Long retry limit
4305 (0 ? TCR_SAT : 0); // FALSE: HW provies PLCP length and LENGEXT, TURE: SW proiveds them
4307 priv->ReceiveConfig =
4308 #ifdef CONFIG_RTL818X_S
4313 RCR_AMF | RCR_ADF | //accept management/data
4314 RCR_ACF | //accept control frame for SW AP needs PS-poll, 2005.07.07, by rcnjko.
4315 RCR_AB | RCR_AM | RCR_APM | //accept BC/MC/UC
4316 //RCR_AICV | RCR_ACRC32 | //accept ICV/CRC error packet
4317 (7<<RCR_MXDMA_OFFSET) | // Max DMA Burst Size per Rx DMA Burst, 7: unlimited.
4318 (priv->EarlyRxThreshold<<RCR_FIFO_OFFSET) | // Rx FIFO Threshold, 7: No Rx threshold.
4319 (priv->EarlyRxThreshold == 7 ? RCR_ONLYERLPKT:0);
4321 priv->IntrMask = IMR_TMGDOK | IMR_TBDER | IMR_THPDER |
4322 IMR_THPDER | IMR_THPDOK |
4323 IMR_TVODER | IMR_TVODOK |
4324 IMR_TVIDER | IMR_TVIDOK |
4325 IMR_TBEDER | IMR_TBEDOK |
4326 IMR_TBKDER | IMR_TBKDOK |
4327 IMR_RDU | // To handle the defragmentation not enough Rx descriptors case. Annie, 2006-03-27.
4329 IMR_RQoSOK; // <NOTE> ROK and RQoSOK are mutually exclusive, so, we must handle RQoSOK interrupt to receive QoS frames, 2005.12.09, by rcnjko.
4331 priv->InitialGain = 6;
4334 hw_version =( read_nic_dword(dev, TCR) & TCR_HWVERID_MASK)>>TCR_HWVERID_SHIFT;
4336 switch (hw_version){
4337 #ifdef CONFIG_RTL8185B
4338 case HW_VERID_R8185B_B:
4339 #ifdef CONFIG_RTL818X_S
4340 priv->card_8185 = VERSION_8187S_C;
4341 DMESG("MAC controller is a RTL8187SE b/g");
4345 DMESG("MAC controller is a RTL8185B b/g");
4346 priv->card_8185 = 3;
4351 case HW_VERID_R8185_ABC:
4352 DMESG("MAC controller is a RTL8185 b/g");
4353 priv->card_8185 = 1;
4354 /* you should not find a card with 8225 PHY ver < C*/
4358 case HW_VERID_R8185_D:
4359 DMESG("MAC controller is a RTL8185 b/g (V. D)");
4360 priv->card_8185 = 2;
4361 /* you should not find a card with 8225 PHY ver < C*/
4365 case HW_VERID_R8180_ABCD:
4366 DMESG("MAC controller is a RTL8180");
4367 priv->card_8185 = 0;
4370 case HW_VERID_R8180_F:
4371 DMESG("MAC controller is a RTL8180 (v. F)");
4372 priv->card_8185 = 0;
4376 DMESGW("MAC chip not recognized: version %x. Assuming RTL8180",hw_version);
4377 priv->card_8185 = 0;
4381 if(priv->card_8185){
4382 priv->ieee80211->modulation |= IEEE80211_OFDM_MODULATION;
4383 priv->ieee80211->short_slot = 1;
4385 /* you should not found any 8185 Ver B Card */
4386 priv->card_8185_Bversion = 0;
4388 #ifdef CONFIG_RTL8185B
4389 #ifdef CONFIG_RTL818X_S
4391 priv->card_type = PCI;
4392 DMESG("This is a PCI NIC");
4394 config3 = read_nic_byte(dev, CONFIG3);
4396 priv->card_type = CARDBUS;
4397 DMESG("This is a CARDBUS NIC");
4399 else if( config3 & 0x4){
4400 priv->card_type = MINIPCI;
4401 DMESG("This is a MINI-PCI NIC");
4403 priv->card_type = PCI;
4404 DMESG("This is a PCI NIC");
4408 priv->enable_gpio0 = 0;
4410 //by amy for antenna
4411 #ifdef CONFIG_RTL8185B
4412 usValue = eprom_read(dev, EEPROM_SW_REVD_OFFSET);
4413 DMESG("usValue is 0x%x\n",usValue);
4414 #ifdef CONFIG_RTL818X_S
4415 //3Read AntennaDiversity
4416 // SW Antenna Diversity.
4417 if( (usValue & EEPROM_SW_AD_MASK) != EEPROM_SW_AD_ENABLE )
4419 priv->EEPROMSwAntennaDiversity = false;
4420 //printk("EEPROM Disable SW Antenna Diversity\n");
4424 priv->EEPROMSwAntennaDiversity = true;
4425 //printk("EEPROM Enable SW Antenna Diversity\n");
4427 // Default Antenna to use.
4428 if( (usValue & EEPROM_DEF_ANT_MASK) != EEPROM_DEF_ANT_1 )
4430 priv->EEPROMDefaultAntenna1 = false;
4431 //printk("EEPROM Default Antenna 0\n");
4435 priv->EEPROMDefaultAntenna1 = true;
4436 //printk("EEPROM Default Antenna 1\n");
4440 // Antenna diversity mechanism. Added by Roger, 2007.11.05.
4442 if( priv->RegSwAntennaDiversityMechanism == 0 ) // Auto
4443 {// 0: default from EEPROM.
4444 priv->bSwAntennaDiverity = priv->EEPROMSwAntennaDiversity;
4447 {// 1:disable antenna diversity, 2: enable antenna diversity.
4448 priv->bSwAntennaDiverity = ((priv->RegSwAntennaDiversityMechanism == 1)? false : true);
4450 //printk("bSwAntennaDiverity = %d\n", priv->bSwAntennaDiverity);
4454 // Default antenna settings. Added by Roger, 2007.11.05.
4456 if( priv->RegDefaultAntenna == 0)
4457 {// 0: default from EEPROM.
4458 priv->bDefaultAntenna1 = priv->EEPROMDefaultAntenna1;
4461 {// 1: main, 2: aux.
4462 priv->bDefaultAntenna1 = ((priv->RegDefaultAntenna== 2) ? true : false);
4464 //printk("bDefaultAntenna1 = %d\n", priv->bDefaultAntenna1);
4467 //by amy for antenna
4468 /* rtl8185 can calc plcp len in HW.*/
4469 priv->hw_plcp_len = 1;
4471 priv->plcp_preamble_mode = 2;
4472 /*the eeprom type is stored in RCR register bit #6 */
4473 if (RCR_9356SEL & read_nic_dword(dev, RCR)){
4474 priv->epromtype=EPROM_93c56;
4475 //DMESG("Reported EEPROM chip is a 93c56 (2Kbit)");
4477 priv->epromtype=EPROM_93c46;
4478 //DMESG("Reported EEPROM chip is a 93c46 (1Kbit)");
4481 dev->dev_addr[0]=eprom_read(dev,MAC_ADR) & 0xff;
4482 dev->dev_addr[1]=(eprom_read(dev,MAC_ADR) & 0xff00)>>8;
4483 dev->dev_addr[2]=eprom_read(dev,MAC_ADR+1) & 0xff;
4484 dev->dev_addr[3]=(eprom_read(dev,MAC_ADR+1) & 0xff00)>>8;
4485 dev->dev_addr[4]=eprom_read(dev,MAC_ADR+2) & 0xff;
4486 dev->dev_addr[5]=(eprom_read(dev,MAC_ADR+2) & 0xff00)>>8;
4487 //DMESG("Card MAC address is "MAC_FMT, MAC_ARG(dev->dev_addr));
4490 for(i=1,j=0; i<14; i+=2,j++){
4492 word = eprom_read(dev,EPROM_TXPW_CH1_2 + j);
4493 priv->chtxpwr[i]=word & 0xff;
4494 priv->chtxpwr[i+1]=(word & 0xff00)>>8;
4496 DMESG("tx word %x:%x",j,word);
4497 DMESG("ch %d pwr %x",i,priv->chtxpwr[i]);
4498 DMESG("ch %d pwr %x",i+1,priv->chtxpwr[i+1]);
4501 if(priv->card_8185){
4502 for(i=1,j=0; i<14; i+=2,j++){
4504 word = eprom_read(dev,EPROM_TXPW_OFDM_CH1_2 + j);
4505 priv->chtxpwr_ofdm[i]=word & 0xff;
4506 priv->chtxpwr_ofdm[i+1]=(word & 0xff00)>>8;
4508 DMESG("ofdm tx word %x:%x",j,word);
4509 DMESG("ofdm ch %d pwr %x",i,priv->chtxpwr_ofdm[i]);
4510 DMESG("ofdm ch %d pwr %x",i+1,priv->chtxpwr_ofdm[i+1]);
4515 //3Read crystal calibtration and thermal meter indication on 87SE.
4517 // By SD3 SY's request. Added by Roger, 2007.12.11.
4519 tmpu16 = eprom_read(dev, EEPROM_RSV>>1);
4521 //printk("ReadAdapterInfo8185(): EEPROM_RSV(%04x)\n", tmpu16);
4523 // Crystal calibration for Xin and Xout resp.
4524 priv->XtalCal_Xout = tmpu16 & EEPROM_XTAL_CAL_XOUT_MASK; // 0~7.5pF
4525 priv->XtalCal_Xin = (tmpu16 & EEPROM_XTAL_CAL_XIN_MASK)>>4; // 0~7.5pF
4526 if((tmpu16 & EEPROM_XTAL_CAL_ENABLE)>>12)
4527 priv->bXtalCalibration = true;
4529 // Thermal meter reference indication.
4530 priv->ThermalMeter = (u8)((tmpu16 & EEPROM_THERMAL_METER_MASK)>>8);
4531 if((tmpu16 & EEPROM_THERMAL_METER_ENABLE)>>13)
4532 priv->bTxPowerTrack = true;
4535 #ifdef CONFIG_RTL8185B
4536 word = eprom_read(dev,EPROM_TXPW_BASE);
4537 priv->cck_txpwr_base = word & 0xf;
4538 priv->ofdm_txpwr_base = (word>>4) & 0xf;
4541 version = eprom_read(dev,EPROM_VERSION);
4542 DMESG("EEPROM version %x",version);
4543 if( (!priv->card_8185) && version < 0x0101){
4544 DMESG ("EEPROM version too old, assuming defaults");
4545 DMESG ("If you see this message *plase* send your \
4546 DMESG output to andreamrl@tiscali.it THANKS");
4550 priv->cs_treshold=0xc;
4552 priv->rf_chip=RFCHIPID_PHILIPS;
4554 if(!priv->card_8185){
4555 u8 rfparam = eprom_read(dev,RF_PARAM);
4556 DMESG("RfParam: %x",rfparam);
4558 priv->digphy = rfparam & (1<<RF_PARAM_DIGPHY_SHIFT) ? 0:1;
4559 priv->antb = rfparam & (1<<RF_PARAM_ANTBDEFAULT_SHIFT) ? 1:0;
4561 priv->rcr_csense = (rfparam & RF_PARAM_CARRIERSENSE_MASK) >>
4562 RF_PARAM_CARRIERSENSE_SHIFT;
4565 (read_nic_byte(dev,CONFIG2)&(1<<CONFIG2_ANTENNA_SHIFT)) ? 1:0;
4567 priv->rcr_csense = 3;
4570 priv->cs_treshold = (eprom_read(dev,ENERGY_TRESHOLD)&0xff00) >>8;
4572 priv->rf_chip = 0xff & eprom_read(dev,RFCHIPID);
4575 #ifdef CONFIG_RTL8185B
4576 #ifdef CONFIG_RTL818X_S
4577 priv->rf_chip = RF_ZEBRA4;
4578 priv->rf_sleep = rtl8225z4_rf_sleep;
4579 priv->rf_wakeup = rtl8225z4_rf_wakeup;
4581 priv->rf_chip = RF_ZEBRA2;
4583 //DMESG("Card reports RF frontend Realtek 8225z2");
4584 //DMESGW("This driver has EXPERIMENTAL support for this chipset.");
4585 //DMESGW("use it with care and at your own risk and");
4586 DMESGW("**PLEASE** REPORT SUCCESSFUL/UNSUCCESSFUL TO Realtek!");
4588 priv->rf_close = rtl8225z2_rf_close;
4589 priv->rf_init = rtl8225z2_rf_init;
4590 priv->rf_set_chan = rtl8225z2_rf_set_chan;
4591 priv->rf_set_sens = NULL;
4592 //priv->rf_sleep = rtl8225_rf_sleep;
4593 //priv->rf_wakeup = rtl8225_rf_wakeup;
4596 /* check RF frontend chipset */
4597 switch (priv->rf_chip) {
4599 case RFCHIPID_RTL8225:
4601 if(priv->card_8185){
4602 DMESG("Card reports RF frontend Realtek 8225");
4603 DMESGW("This driver has EXPERIMENTAL support for this chipset.");
4604 DMESGW("use it with care and at your own risk and");
4605 DMESGW("**PLEASE** REPORT SUCCESS/INSUCCESS TO andreamrl@tiscali.it");
4607 priv->rf_close = rtl8225_rf_close;
4608 priv->rf_init = rtl8225_rf_init;
4609 priv->rf_set_chan = rtl8225_rf_set_chan;
4610 priv->rf_set_sens = NULL;
4611 priv->rf_sleep = rtl8225_rf_sleep;
4612 priv->rf_wakeup = rtl8225_rf_wakeup;
4615 DMESGW("Detected RTL8225 radio on a card recognized as RTL8180");
4616 DMESGW("This could not be... something went wrong....");
4621 case RFCHIPID_RTL8255:
4622 if(priv->card_8185){
4623 DMESG("Card reports RF frontend Realtek 8255");
4624 DMESGW("This driver has EXPERIMENTAL support for this chipset.");
4625 DMESGW("use it with care and at your own risk and");
4626 DMESGW("**PLEASE** REPORT SUCCESS/INSUCCESS TO andreamrl@tiscali.it");
4628 priv->rf_close = rtl8255_rf_close;
4629 priv->rf_init = rtl8255_rf_init;
4630 priv->rf_set_chan = rtl8255_rf_set_chan;
4631 priv->rf_set_sens = NULL;
4632 priv->rf_sleep = NULL;
4633 priv->rf_wakeup = NULL;
4636 DMESGW("Detected RTL8255 radio on a card recognized as RTL8180");
4637 DMESGW("This could not be... something went wrong....");
4643 case RFCHIPID_INTERSIL:
4644 DMESGW("Card reports RF frontend by Intersil.");
4645 DMESGW("This driver has NO support for this chipset.");
4650 DMESGW("Card reports RF frontend by RFMD.");
4651 DMESGW("This driver has NO support for this chipset.");
4656 DMESGW("Card reports RF frontend by GCT.");
4657 DMESGW("This driver has EXPERIMENTAL support for this chipset.");
4658 DMESGW("use it with care and at your own risk and");
4659 DMESGW("**PLEASE** REPORT SUCCESS/INSUCCESS TO andreamrl@tiscali.it");
4660 priv->rf_close = gct_rf_close;
4661 priv->rf_init = gct_rf_init;
4662 priv->rf_set_chan = gct_rf_set_chan;
4663 priv->rf_set_sens = NULL;
4664 priv->rf_sleep = NULL;
4665 priv->rf_wakeup = NULL;
4668 case RFCHIPID_MAXIM:
4669 DMESGW("Card reports RF frontend by MAXIM.");
4670 DMESGW("This driver has EXPERIMENTAL support for this chipset.");
4671 DMESGW("use it with care and at your own risk and");
4672 DMESGW("**PLEASE** REPORT SUCCESS/INSUCCESS TO andreamrl@tiscali.it");
4673 priv->rf_close = maxim_rf_close;
4674 priv->rf_init = maxim_rf_init;
4675 priv->rf_set_chan = maxim_rf_set_chan;
4676 priv->rf_set_sens = NULL;
4677 priv->rf_sleep = NULL;
4678 priv->rf_wakeup = NULL;
4681 case RFCHIPID_PHILIPS:
4682 DMESG("Card reports RF frontend by Philips.");
4683 DMESG("OK! Philips SA2400 radio chipset is supported.");
4684 priv->rf_close = sa2400_rf_close;
4685 priv->rf_init = sa2400_rf_init;
4686 priv->rf_set_chan = sa2400_rf_set_chan;
4687 priv->rf_set_sens = sa2400_rf_set_sens;
4688 priv->sens = SA2400_RF_DEF_SENS; /* default sensitivity */
4689 priv->max_sens = SA2400_RF_MAX_SENS; /* maximum sensitivity */
4690 priv->rf_sleep = NULL;
4691 priv->rf_wakeup = NULL;
4694 DMESGW("Digital PHY found");
4695 DMESGW("Philips DIGITAL PHY is untested! *Please*\
4696 report success/failure to <andreamrl@tiscali.it>");
4698 DMESG ("Analog PHY found");
4704 DMESGW("Unknown RF module %x",priv->rf_chip);
4705 DMESGW("Exiting...");
4712 if(!priv->card_8185){
4714 DMESG ("Antenna B is default antenna");
4716 DMESG ("Antenna A is default antenna");
4719 DMESG ("Antenna diversity is enabled");
4721 DMESG("Antenna diversity is disabled");
4723 DMESG("Carrier sense %d",priv->rcr_csense);
4726 if (0!=alloc_rx_desc_ring(dev, priv->rxbuffersize, priv->rxringcount))
4729 if (0!=alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txringcount,
4730 TX_MANAGEPRIORITY_RING_ADDR))
4733 if (0!=alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txringcount,
4734 TX_BKPRIORITY_RING_ADDR))
4737 if (0!=alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txringcount,
4738 TX_BEPRIORITY_RING_ADDR))
4741 if (0!=alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txringcount,
4742 TX_VIPRIORITY_RING_ADDR))
4745 if (0!=alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txringcount,
4746 TX_VOPRIORITY_RING_ADDR))
4749 if (0!=alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txringcount,
4750 TX_HIGHPRIORITY_RING_ADDR))
4753 if (0!=alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txbeaconcount,
4754 TX_BEACON_RING_ADDR))
4758 //priv->beacon_buf=NULL;
4760 if(!priv->card_8185){
4762 if(read_nic_byte(dev, CONFIG0) & (1<<CONFIG0_WEP40_SHIFT))
4763 DMESG ("40-bit WEP is supported in hardware");
4765 DMESG ("40-bit WEP is NOT supported in hardware");
4767 if(read_nic_byte(dev,CONFIG0) & (1<<CONFIG0_WEP104_SHIFT))
4768 DMESG ("104-bit WEP is supported in hardware");
4770 DMESG ("104-bit WEP is NOT supported in hardware");
4772 #if !defined(SA_SHIRQ)
4773 if(request_irq(dev->irq, (void *)rtl8180_interrupt, IRQF_SHARED, dev->name, dev)){
4775 if(request_irq(dev->irq, (void *)rtl8180_interrupt, SA_SHIRQ, dev->name, dev)){
4777 DMESGE("Error allocating IRQ %d",dev->irq);
4781 DMESG("IRQ %d",dev->irq);
4793 void rtl8180_no_hw_wep(struct net_device *dev)
4795 struct r8180_priv *priv = ieee80211_priv(dev);
4797 if(!priv->card_8185)
4801 security = read_nic_byte(dev, SECURITY);
4802 security &=~(1<<SECURITY_WEP_TX_ENABLE_SHIFT);
4803 security &=~(1<<SECURITY_WEP_RX_ENABLE_SHIFT);
4805 write_nic_byte(dev, SECURITY, security);
4812 write_nic_dword(dev,TX_CONF,read_nic_dword(dev,TX_CONF) |
4813 (1<<TX_NOICV_SHIFT) );
4815 // priv->ieee80211->hw_wep=0;
4819 void rtl8180_set_hw_wep(struct net_device *dev)
4821 struct r8180_priv *priv = ieee80211_priv(dev);
4826 pgreg=read_nic_byte(dev, PGSELECT);
4827 write_nic_byte(dev, PGSELECT, pgreg &~ (1<<PGSELECT_PG_SHIFT));
4829 key0_word4 = read_nic_dword(dev, KEY0+4+4+4);
4830 key0_word4 &= ~ 0xff;
4831 key0_word4 |= priv->key0[3]& 0xff;
4832 write_nic_dword(dev,KEY0,(priv->key0[0]));
4833 write_nic_dword(dev,KEY0+4,(priv->key0[1]));
4834 write_nic_dword(dev,KEY0+4+4,(priv->key0[2]));
4835 write_nic_dword(dev,KEY0+4+4+4,(key0_word4));
4838 TX_CONF,read_nic_dword(dev,TX_CONF) &~(1<<TX_NOICV_SHIFT));
4841 security = read_nic_byte(dev,SECURITY);
4842 security |= (1<<SECURITY_WEP_TX_ENABLE_SHIFT);
4843 security |= (1<<SECURITY_WEP_RX_ENABLE_SHIFT);
4844 security &= ~ SECURITY_ENCRYP_MASK;
4845 security |= (SECURITY_ENCRYP_104<<SECURITY_ENCRYP_SHIFT);
4847 write_nic_byte(dev, SECURITY, security);
4849 DMESG("key %x %x %x %x",read_nic_dword(dev,KEY0+4+4+4),
4850 read_nic_dword(dev,KEY0+4+4),read_nic_dword(dev,KEY0+4),
4851 read_nic_dword(dev,KEY0));
4853 //priv->ieee80211->hw_wep=1;
4857 void rtl8185_rf_pins_enable(struct net_device *dev)
4860 // tmp = read_nic_word(dev, RFPinsEnable);
4861 write_nic_word(dev, RFPinsEnable, 0x1fff);// | tmp);
4862 // write_nic_word(dev, RFPinsEnable,7 | tmp);
4866 void rtl8185_set_anaparam2(struct net_device *dev, u32 a)
4870 rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
4872 conf3 = read_nic_byte(dev, CONFIG3);
4873 write_nic_byte(dev, CONFIG3, conf3 | (1<<CONFIG3_ANAPARAM_W_SHIFT));
4874 write_nic_dword(dev, ANAPARAM2, a);
4876 conf3 = read_nic_byte(dev, CONFIG3);
4877 write_nic_byte(dev, CONFIG3, conf3 &~(1<<CONFIG3_ANAPARAM_W_SHIFT));
4878 rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
4883 void rtl8180_set_anaparam(struct net_device *dev, u32 a)
4887 rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
4889 conf3 = read_nic_byte(dev, CONFIG3);
4890 write_nic_byte(dev, CONFIG3, conf3 | (1<<CONFIG3_ANAPARAM_W_SHIFT));
4891 write_nic_dword(dev, ANAPARAM, a);
4893 conf3 = read_nic_byte(dev, CONFIG3);
4894 write_nic_byte(dev, CONFIG3, conf3 &~(1<<CONFIG3_ANAPARAM_W_SHIFT));
4895 rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
4899 void rtl8185_tx_antenna(struct net_device *dev, u8 ant)
4901 write_nic_byte(dev, TX_ANTENNA, ant);
4902 force_pci_posting(dev);
4907 void rtl8185_write_phy(struct net_device *dev, u8 adr, u32 data)
4915 phyw= ((data<<8) | adr);
4918 write_nic_dword(dev, PHY_ADR, phyw);
4920 //read_nic_dword(dev, PHY_ADR);
4922 write_nic_dword(dev, PHY_ADR, 0xffffff7f & phyw);
4923 phyr = read_nic_byte(dev, PHY_READ);
4924 if(phyr == (data&0xff)) break;
4928 // Note that, we must write 0xff7c after 0x7d-0x7f to write BB register.
4929 write_nic_byte(dev, 0x7f, ((phyw & 0xff000000) >> 24));
4930 write_nic_byte(dev, 0x7e, ((phyw & 0x00ff0000) >> 16));
4931 write_nic_byte(dev, 0x7d, ((phyw & 0x0000ff00) >> 8));
4932 write_nic_byte(dev, 0x7c, ((phyw & 0x000000ff) ));
4934 /* this is ok to fail when we write AGC table. check for AGC table might be
4935 * done by masking with 0x7f instead of 0xff
4937 //if(phyr != (data&0xff)) DMESGW("Phy write timeout %x %x %x", phyr, data,adr);
4941 inline void write_phy_ofdm (struct net_device *dev, u8 adr, u32 data)
4944 rtl8185_write_phy(dev, adr, data);
4948 void write_phy_cck (struct net_device *dev, u8 adr, u32 data)
4951 rtl8185_write_phy(dev, adr, data | 0x10000);
4956 * I hope this is enougth
4959 void write_phy(struct net_device *dev, u8 adr, u8 data)
4966 phy |= 0x80; /* this should enable writing */
4969 //PHY_ADR, PHY_R and PHY_W are contig and treated as one dword
4970 write_nic_dword(dev,PHY_ADR, phy);
4975 write_nic_dword(dev,PHY_ADR, phy);
4976 for(i=0;i<MAX_PHY;i++){
4977 phy=read_nic_dword(dev,PHY_ADR);
4978 phy= phy & 0xff0000;
4980 if(phy == data){ //SUCCESS!
4981 force_pci_posting(dev);
4982 mdelay(3); //random value
4984 DMESG("Phy wr %x,%x",adr,data);
4988 force_pci_posting(dev);
4989 mdelay(3); //random value
4992 DMESGW ("Phy writing %x %x failed!", adr,data);
4995 void rtl8185_set_rate(struct net_device *dev)
4999 int basic_rate,min_rr_rate,max_rr_rate;
5001 // struct r8180_priv *priv = ieee80211_priv(dev);
5003 //if (ieee80211_is_54g(priv->ieee80211->current_network) &&
5004 // priv->ieee80211->state == IEEE80211_LINKED){
5005 basic_rate = ieeerate2rtlrate(240);
5006 min_rr_rate = ieeerate2rtlrate(60);
5007 max_rr_rate = ieeerate2rtlrate(240);
5011 // basic_rate = ieeerate2rtlrate(20);
5012 // min_rr_rate = ieeerate2rtlrate(10);
5013 // max_rr_rate = ieeerate2rtlrate(110);
5016 write_nic_byte(dev, RESP_RATE,
5017 max_rr_rate<<MAX_RESP_RATE_SHIFT| min_rr_rate<<MIN_RESP_RATE_SHIFT);
5019 word = read_nic_word(dev, BRSR);
5020 word &= ~BRSR_MBR_8185;
5023 for(i=0;i<=basic_rate;i++)
5026 write_nic_word(dev, BRSR, word);
5027 //DMESG("RR:%x BRSR: %x", read_nic_byte(dev,RESP_RATE),read_nic_word(dev,BRSR));
5032 void rtl8180_adapter_start(struct net_device *dev)
5034 struct r8180_priv *priv = ieee80211_priv(dev);
5040 rtl8180_rtx_disable(dev);
5043 /* seems that 0xffff or 0xafff will cause
5044 * HW interrupt line crash
5047 //priv->irq_mask = 0xafff;
5048 // priv->irq_mask = 0x4fcf;
5050 /* enable beacon timeout, beacon TX ok and err
5051 * LP tx ok and err, HP TX ok and err, NP TX ok and err,
5052 * RX ok and ERR, and GP timer */
5053 priv->irq_mask = 0x6fcf;
5055 priv->dma_poll_mask = 0;
5057 rtl8180_beacon_tx_disable(dev);
5059 if(priv->card_type == CARDBUS ){
5060 config3=read_nic_byte(dev, CONFIG3);
5061 write_nic_byte(dev,CONFIG3,config3 | CONFIG3_FuncRegEn);
5062 write_nic_word(dev,FEMR, FEMR_INTR | FEMR_WKUP | FEMR_GWAKE |
5063 read_nic_word(dev, FEMR));
5065 rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
5066 write_nic_dword(dev, MAC0, ((u32*)dev->dev_addr)[0]);
5067 write_nic_word(dev, MAC4, ((u32*)dev->dev_addr)[1] & 0xffff );
5068 rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
5070 rtl8180_update_msr(dev);
5072 if(!priv->card_8185){
5073 anaparam = eprom_read(dev,EPROM_ANAPARAM_ADDRLWORD);
5074 anaparam |= eprom_read(dev,EPROM_ANAPARAM_ADDRHWORD)<<16;
5076 rtl8180_set_anaparam(dev,anaparam);
5078 /* These might be unnecessary since we do in rx_enable / tx_enable */
5081 /*set_nic_rxring(dev);
5082 set_nic_txring(dev);*/
5084 rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
5087 The following is very strange. seems to be that 1 means test mode,
5088 but we need to acknolwledges the nic when a packet is ready
5089 altought we set it to 0
5093 CONFIG2, read_nic_byte(dev,CONFIG2) &~\
5094 (1<<CONFIG2_DMA_POLLING_MODE_SHIFT));
5095 //^the nic isn't in test mode
5098 CONFIG2, read_nic_byte(dev,CONFIG2)|(1<<4));
5100 rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
5102 write_nic_dword(dev,INT_TIMEOUT,0);
5103 #ifdef DEBUG_REGISTERS
5104 rtl8180_dump_reg(dev);
5107 if(!priv->card_8185)
5110 experimental - this might be needed to calibrate AGC,
5111 anyway it shouldn't hurt
5113 write_nic_byte(dev, CONFIG5,
5114 read_nic_byte(dev, CONFIG5) | (1<<AGCRESET_SHIFT));
5115 read_nic_byte(dev, CONFIG5);
5117 write_nic_byte(dev, CONFIG5,
5118 read_nic_byte(dev, CONFIG5) &~ (1<<AGCRESET_SHIFT));
5121 write_nic_byte(dev, WPA_CONFIG, 0);
5122 //write_nic_byte(dev, TESTR, 0xd);
5125 rtl8180_no_hw_wep(dev);
5127 if(priv->card_8185){
5128 rtl8185_set_rate(dev);
5129 write_nic_byte(dev, RATE_FALLBACK, 0x81);
5130 // write_nic_byte(dev, 0xdf, 0x15);
5132 word = read_nic_word(dev, BRSR);
5134 word &= ~BRSR_BPLCP;
5135 word |= ieeerate2rtlrate(priv->ieee80211->basic_rate);
5139 write_nic_word(dev, BRSR, word);
5143 if(priv->card_8185){
5144 write_nic_byte(dev, GP_ENABLE,read_nic_byte(dev, GP_ENABLE) & ~(1<<6));
5146 //FIXME cfg 3 ClkRun enable - isn't it ReadOnly ?
5147 rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
5148 write_nic_byte(dev,CONFIG3, read_nic_byte(dev, CONFIG3)
5149 |(1<<CONFIG3_CLKRUN_SHIFT));
5150 rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
5156 if(priv->rf_set_sens != NULL)
5157 priv->rf_set_sens(dev,priv->sens);
5158 rtl8180_irq_enable(dev);
5160 netif_start_queue(dev);
5161 /*DMESG ("lfree %d",get_curr_tx_free_desc(dev,LOW_PRIORITY));
5163 DMESG ("nfree %d",get_curr_tx_free_desc(dev,NORM_PRIORITY));
5165 DMESG ("hfree %d",get_curr_tx_free_desc(dev,HI_PRIORITY));
5166 if(check_nic_enought_desc(dev,NORM_PRIORITY)) DMESG("NORM OK");
5167 if(check_nic_enought_desc(dev,HI_PRIORITY)) DMESG("HI OK");
5168 if(check_nic_enought_desc(dev,LOW_PRIORITY)) DMESG("LOW OK");*/
5173 /* this configures registers for beacon tx and enables it via
5174 * rtl8180_beacon_tx_enable(). rtl8180_beacon_tx_disable() might
5175 * be used to stop beacon transmission
5177 void rtl8180_start_tx_beacon(struct net_device *dev)
5179 // struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
5181 // DMESG("ring %x %x", priv->txlpringdma,read_nic_dword(dev,TLPDA));
5183 DMESG("Enabling beacon TX");
5184 //write_nic_byte(dev, 0x42,0xe6);// TCR
5185 // set_nic_txring(dev);
5186 // fix_tx_fifo(dev);
5187 rtl8180_prepare_beacon(dev);
5188 rtl8180_irq_disable(dev);
5189 rtl8180_beacon_tx_enable(dev);
5191 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
5192 //write_nic_byte(dev,0x9d,0x20); //DMA Poll
5193 //write_nic_word(dev,0x7a,0);
5194 //write_nic_word(dev,0x7a,0x8000);
5197 word = read_nic_word(dev, BcnItv);
5198 word &= ~BcnItv_BcnItv; // clear Bcn_Itv
5199 word |= priv->ieee80211->current_network.beacon_interval;//0x64;
5200 write_nic_word(dev, BcnItv, word);
5203 word = read_nic_word(dev, AtimWnd) &~ AtimWnd_AtimWnd;
5204 write_nic_word(dev, AtimWnd,word);// word |=
5205 //priv->ieee80211->current_network.atim_window);
5207 word = read_nic_word(dev, BintrItv);
5208 word &= ~BintrItv_BintrItv;
5209 word |= 1000;/*priv->ieee80211->current_network.beacon_interval *
5210 ((priv->txbeaconcount > 1)?(priv->txbeaconcount-1):1);
5211 // FIXME: check if correct ^^ worked with 0x3e8;
5213 write_nic_word(dev, BintrItv, word);
5216 rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
5218 // rtl8180_beacon_tx_enable(dev);
5219 #ifdef CONFIG_RTL8185B
5220 rtl8185b_irq_enable(dev);
5222 rtl8180_irq_enable(dev);
5224 /* VV !!!!!!!!!! VV*/
5226 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
5227 write_nic_byte(dev,0x9d,0x00);
5228 rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
5230 // DMESG("ring %x %x", priv->txlpringdma,read_nic_dword(dev,TLPDA));
5236 /***************************************************************************
5237 -------------------------------NET STUFF---------------------------
5238 ***************************************************************************/
5239 static struct net_device_stats *rtl8180_stats(struct net_device *dev)
5241 struct r8180_priv *priv = ieee80211_priv(dev);
5243 return &priv->ieee80211->stats;
5246 // Change current and default preamble mode.
5247 // 2005.01.06, by rcnjko.
5250 MgntActSet_802_11_PowerSaveMode(
5251 struct r8180_priv *priv,
5256 // Currently, we do not change power save mode on IBSS mode.
5257 if(priv->ieee80211->iw_mode == IW_MODE_ADHOC)
5263 // <RJ_NOTE> If we make HW to fill up the PwrMgt bit for us,
5264 // some AP will not response to our mgnt frames with PwrMgt bit set,
5265 // e.g. cannot associate the AP.
5266 // So I commented out it. 2005.02.16, by rcnjko.
5268 // // Change device's power save mode.
5269 // Adapter->HalFunc.SetPSModeHandler( Adapter, rtPsMode );
5271 // Update power save mode configured.
5272 // priv->dot11PowerSaveMode = rtPsMode;
5273 priv->ieee80211->ps = rtPsMode;
5274 // Determine ListenInterval.
5276 if(priv->dot11PowerSaveMode == eMaxPs)
5278 priv->ieee80211->ListenInterval = 10;
5282 priv->ieee80211->ListenInterval = 2;
5288 //================================================================================
5289 // Leisure Power Save in linked state.
5290 //================================================================================
5294 // Enter the leisure power save mode.
5298 struct r8180_priv *priv
5301 if (priv->bLeisurePs)
5303 if (priv->ieee80211->ps == IEEE80211_PS_DISABLED)
5305 //printk("----Enter PS\n");
5306 MgntActSet_802_11_PowerSaveMode(priv, IEEE80211_PS_MBCAST|IEEE80211_PS_UNICAST);//IEEE80211_PS_ENABLE
5314 // Leave the leisure power save mode.
5318 struct r8180_priv *priv
5321 if (priv->bLeisurePs)
5323 if (priv->ieee80211->ps != IEEE80211_PS_DISABLED)
5325 //printk("----Leave PS\n");
5326 MgntActSet_802_11_PowerSaveMode(priv, IEEE80211_PS_DISABLED);
5331 void rtl8180_hw_wakeup_wq (struct work_struct *work)
5333 // struct r8180_priv *priv = container_of(work, struct r8180_priv, watch_dog_wq);
5334 // struct ieee80211_device * ieee = (struct ieee80211_device*)
5335 // container_of(work, struct ieee80211_device, watch_dog_wq);
5336 struct delayed_work *dwork = to_delayed_work(work);
5337 struct ieee80211_device *ieee = container_of(dwork,struct ieee80211_device,hw_wakeup_wq);
5338 struct net_device *dev = ieee->dev;
5340 // printk("dev is %d\n",dev);
5341 // printk("&*&(^*(&(&=========>%s()\n", __func__);
5342 rtl8180_hw_wakeup(dev);
5346 void rtl8180_hw_sleep_wq (struct work_struct *work)
5348 // struct r8180_priv *priv = container_of(work, struct r8180_priv, watch_dog_wq);
5349 // struct ieee80211_device * ieee = (struct ieee80211_device*)
5350 // container_of(work, struct ieee80211_device, watch_dog_wq);
5351 struct delayed_work *dwork = to_delayed_work(work);
5352 struct ieee80211_device *ieee = container_of(dwork,struct ieee80211_device,hw_sleep_wq);
5353 struct net_device *dev = ieee->dev;
5355 rtl8180_hw_sleep_down(dev);
5358 //YJ,add,080828,for KeepAlive
5359 static void MgntLinkKeepAlive(struct r8180_priv *priv )
5361 if (priv->keepAliveLevel == 0)
5364 if(priv->ieee80211->state == IEEE80211_LINKED)
5369 //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);
5371 if ( (priv->keepAliveLevel== 2) ||
5372 (priv->link_detect.LastNumTxUnicast == priv->NumTxUnicast &&
5373 priv->link_detect.LastNumRxUnicast == priv->ieee80211->NumRxUnicast )
5376 priv->link_detect.IdleCount++;
5379 // Send a Keep-Alive packet packet to AP if we had been idle for a while.
5381 if(priv->link_detect.IdleCount >= ((KEEP_ALIVE_INTERVAL / CHECK_FOR_HANG_PERIOD)-1) )
5383 priv->link_detect.IdleCount = 0;
5384 ieee80211_sta_ps_send_null_frame(priv->ieee80211, false);
5389 priv->link_detect.IdleCount = 0;
5391 priv->link_detect.LastNumTxUnicast = priv->NumTxUnicast;
5392 priv->link_detect.LastNumRxUnicast = priv->ieee80211->NumRxUnicast;
5395 //YJ,add,080828,for KeepAlive,end
5397 static u8 read_acadapter_file(char *filename);
5398 void rtl8180_watch_dog(struct net_device *dev)
5400 struct r8180_priv *priv = ieee80211_priv(dev);
5401 bool bEnterPS = false;
5402 bool bBusyTraffic = false;
5407 if(priv->ieee80211->actscanning == false){
5408 if((priv->ieee80211->iw_mode != IW_MODE_ADHOC) && (priv->ieee80211->state == IEEE80211_NOLINK) && (priv->ieee80211->beinretry == false) && (priv->eRFPowerState == eRfOn)){
5413 //YJ,add,080828,for link state check
5414 if((priv->ieee80211->state == IEEE80211_LINKED) && (priv->ieee80211->iw_mode == IW_MODE_INFRA)){
5415 SlotIndex = (priv->link_detect.SlotIndex++) % priv->link_detect.SlotNum;
5416 priv->link_detect.RxFrameNum[SlotIndex] = priv->ieee80211->NumRxDataInPeriod + priv->ieee80211->NumRxBcnInPeriod;
5417 for( i=0; i<priv->link_detect.SlotNum; i++ )
5418 TotalRxNum+= priv->link_detect.RxFrameNum[i];
5419 //printk("&&&&&=== TotalRxNum = %d\n", TotalRxNum);
5420 if(TotalRxNum == 0){
5421 priv->ieee80211->state = IEEE80211_ASSOCIATING;
5422 queue_work(priv->ieee80211->wq, &priv->ieee80211->associate_procedure_wq);
5426 //YJ,add,080828,for KeepAlive
5427 MgntLinkKeepAlive(priv);
5429 //YJ,add,080828,for LPS
5431 if(priv->PowerProfile == POWER_PROFILE_BATTERY )
5433 //Turn on LeisurePS on battery power
5434 //printk("!!!!!On battery power\n");
5435 priv->bLeisurePs = true;
5437 else if(priv->PowerProfile == POWER_PROFILE_AC )
5439 // Turn off LeisurePS on AC power
5440 //printk("----On AC power\n");
5441 LeisurePSLeave(priv);
5442 priv->bLeisurePs= false;
5448 if(priv->ieee80211->state == IEEE80211_LINKED){
5449 if( priv->NumRxOkInPeriod> 666 ||
5450 priv->NumTxOkInPeriod > 666 ) {
5451 bBusyTraffic = true;
5453 if((priv->ieee80211->NumRxData + priv->NumTxOkInPeriod)<8) {
5457 LeisurePSEnter(priv);
5460 LeisurePSLeave(priv);
5464 LeisurePSLeave(priv);
5467 priv->NumRxOkInPeriod = 0;
5468 priv->NumTxOkInPeriod = 0;
5469 priv->ieee80211->NumRxData = 0;
5472 if(priv->ieee80211->state == IEEE80211_LINKED){
5473 priv->link_detect.NumRxOkInPeriod = priv->ieee80211->NumRxDataInPeriod;
5474 //printk("TxOk=%d RxOk=%d\n", priv->link_detect.NumTxOkInPeriod, priv->link_detect.NumRxOkInPeriod);
5475 if( priv->link_detect.NumRxOkInPeriod> 666 ||
5476 priv->link_detect.NumTxOkInPeriod> 666 ) {
5477 bBusyTraffic = true;
5479 if(((priv->link_detect.NumRxOkInPeriod + priv->link_detect.NumTxOkInPeriod) > 8)
5480 || (priv->link_detect.NumRxOkInPeriod > 2)) {
5488 LeisurePSEnter(priv);
5491 LeisurePSLeave(priv);
5495 LeisurePSLeave(priv);
5498 priv->link_detect.bBusyTraffic = bBusyTraffic;
5499 priv->link_detect.NumRxOkInPeriod = 0;
5500 priv->link_detect.NumTxOkInPeriod = 0;
5501 priv->ieee80211->NumRxDataInPeriod = 0;
5502 priv->ieee80211->NumRxBcnInPeriod = 0;
5505 int _rtl8180_up(struct net_device *dev)
5507 struct r8180_priv *priv = ieee80211_priv(dev);
5512 DMESG("Bringing up iface");
5513 #ifdef CONFIG_RTL8185B
5514 rtl8185b_adapter_start(dev);
5515 rtl8185b_rx_enable(dev);
5516 rtl8185b_tx_enable(dev);
5518 rtl8180_adapter_start(dev);
5519 rtl8180_rx_enable(dev);
5520 rtl8180_tx_enable(dev);
5523 if(priv->bInactivePs){
5524 if(priv->ieee80211->iw_mode == IW_MODE_ADHOC)
5530 timer_rate_adaptive((unsigned long)dev);
5533 watch_dog_adaptive((unsigned long)dev);
5535 if(priv->bSwAntennaDiverity)
5536 SwAntennaDiversityTimerCallback(dev);
5539 ieee80211_softmac_start_protocol(priv->ieee80211);
5541 //Add for RF power on power off by lizhaoming 080512
5542 // priv->eRFPowerState = eRfOn;
5543 // printk("\n--------Start queue_work:GPIOChangeRFWorkItem");
5544 // queue_delayed_work(priv->ieee80211->wq,&priv->ieee80211->GPIOChangeRFWorkItem,1000);
5550 int rtl8180_open(struct net_device *dev)
5552 struct r8180_priv *priv = ieee80211_priv(dev);
5555 down(&priv->wx_sem);
5556 ret = rtl8180_up(dev);
5563 int rtl8180_up(struct net_device *dev)
5565 struct r8180_priv *priv = ieee80211_priv(dev);
5567 if (priv->up == 1) return -1;
5569 return _rtl8180_up(dev);
5573 int rtl8180_close(struct net_device *dev)
5575 struct r8180_priv *priv = ieee80211_priv(dev);
5578 down(&priv->wx_sem);
5579 ret = rtl8180_down(dev);
5586 int rtl8180_down(struct net_device *dev)
5588 struct r8180_priv *priv = ieee80211_priv(dev);
5590 if (priv->up == 0) return -1;
5594 ieee80211_softmac_stop_protocol(priv->ieee80211);
5596 if (!netif_queue_stopped(dev))
5597 netif_stop_queue(dev);
5598 rtl8180_rtx_disable(dev);
5599 rtl8180_irq_disable(dev);
5600 del_timer_sync(&priv->watch_dog_timer);
5601 //cancel_delayed_work(&priv->ieee80211->watch_dog_wq);
5603 del_timer_sync(&priv->rateadapter_timer);
5604 cancel_delayed_work(&priv->ieee80211->rate_adapter_wq);
5606 cancel_delayed_work(&priv->ieee80211->hw_wakeup_wq);
5607 cancel_delayed_work(&priv->ieee80211->hw_sleep_wq);
5608 cancel_delayed_work(&priv->ieee80211->hw_dig_wq);
5609 cancel_delayed_work(&priv->ieee80211->tx_pw_wq);
5610 del_timer_sync(&priv->SwAntennaDiversityTimer);
5611 SetZebraRFPowerState8185(dev,eRfOff);
5612 //ieee80211_softmac_stop_protocol(priv->ieee80211);
5613 memset(&(priv->ieee80211->current_network),0,sizeof(struct ieee80211_network));
5614 priv->ieee80211->state = IEEE80211_NOLINK;
5618 void rtl8180_restart_wq(struct work_struct *work)
5620 struct r8180_priv *priv = container_of(work, struct r8180_priv, reset_wq);
5621 struct net_device *dev = priv->dev;
5623 down(&priv->wx_sem);
5625 rtl8180_commit(dev);
5630 void rtl8180_restart(struct net_device *dev)
5632 struct r8180_priv *priv = ieee80211_priv(dev);
5633 //rtl8180_commit(dev);
5634 schedule_work(&priv->reset_wq);
5635 //DMESG("TXTIMEOUT");
5639 void rtl8180_commit(struct net_device *dev)
5641 struct r8180_priv *priv = ieee80211_priv(dev);
5643 if (priv->up == 0) return ;
5645 del_timer_sync(&priv->watch_dog_timer);
5646 //cancel_delayed_work(&priv->ieee80211->watch_dog_wq);
5648 //by amy for rate adaptive
5649 del_timer_sync(&priv->rateadapter_timer);
5650 cancel_delayed_work(&priv->ieee80211->rate_adapter_wq);
5651 //by amy for rate adaptive
5653 cancel_delayed_work(&priv->ieee80211->hw_wakeup_wq);
5654 cancel_delayed_work(&priv->ieee80211->hw_sleep_wq);
5655 cancel_delayed_work(&priv->ieee80211->hw_dig_wq);
5656 cancel_delayed_work(&priv->ieee80211->tx_pw_wq);
5657 del_timer_sync(&priv->SwAntennaDiversityTimer);
5658 ieee80211_softmac_stop_protocol(priv->ieee80211);
5659 rtl8180_irq_disable(dev);
5660 rtl8180_rtx_disable(dev);
5665 static void r8180_set_multicast(struct net_device *dev)
5667 struct r8180_priv *priv = ieee80211_priv(dev);
5670 //down(&priv->wx_sem);
5672 promisc = (dev->flags & IFF_PROMISC) ? 1:0;
5674 if (promisc != priv->promisc)
5675 rtl8180_restart(dev);
5677 priv->promisc = promisc;
5679 //up(&priv->wx_sem);
5683 /* this is called by the kernel when it needs to TX a 802.3 encapsulated frame*/
5684 int rtl8180_8023_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
5686 struct r8180_priv *priv = ieee80211_priv(dev);
5688 unsigned long flags;
5690 spin_lock_irqsave(&priv->tx_lock,flags);
5691 ret = ieee80211_r8180_8023_hardstartxmit(skb,priv->ieee80211);
5692 spin_unlock_irqrestore(&priv->tx_lock,flags);
5697 int r8180_set_mac_adr(struct net_device *dev, void *mac)
5699 struct r8180_priv *priv = ieee80211_priv(dev);
5700 struct sockaddr *addr = mac;
5702 down(&priv->wx_sem);
5704 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
5706 if(priv->ieee80211->iw_mode == IW_MODE_MASTER)
5707 memcpy(priv->ieee80211->current_network.bssid, dev->dev_addr, ETH_ALEN);
5719 /* based on ipw2200 driver */
5720 int rtl8180_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
5722 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
5724 struct iwreq *wrq = (struct iwreq *) rq;
5727 case RTL_IOCTL_WPA_SUPPLICANT:
5728 ret = ieee80211_wpa_supplicant_ioctl(priv->ieee80211, &wrq->u.data);
5740 /****************************************************************************
5741 -----------------------------PCI STUFF---------------------------
5742 *****************************************************************************/
5744 static const struct net_device_ops rtl8180_netdev_ops = {
5745 .ndo_open = rtl8180_open,
5746 .ndo_stop = rtl8180_close,
5747 .ndo_get_stats = rtl8180_stats,
5748 .ndo_tx_timeout = rtl8180_restart,
5749 .ndo_do_ioctl = rtl8180_ioctl,
5750 .ndo_set_multicast_list = r8180_set_multicast,
5751 .ndo_set_mac_address = r8180_set_mac_adr,
5752 .ndo_validate_addr = eth_validate_addr,
5753 .ndo_change_mtu = eth_change_mtu,
5754 .ndo_start_xmit = ieee80211_xmit,
5757 static int __devinit rtl8180_pci_probe(struct pci_dev *pdev,
5758 const struct pci_device_id *id)
5760 unsigned long ioaddr = 0;
5761 struct net_device *dev = NULL;
5762 struct r8180_priv *priv= NULL;
5766 #ifdef CONFIG_RTL8180_IO_MAP
5767 unsigned long pio_start, pio_len, pio_flags;
5769 unsigned long pmem_start, pmem_len, pmem_flags;
5770 #endif //end #ifdef RTL_IO_MAP
5772 DMESG("Configuring chip resources");
5774 if( pci_enable_device (pdev) ){
5775 DMESG("Failed to enable PCI device");
5779 pci_set_master(pdev);
5780 //pci_set_wmi(pdev);
5781 pci_set_dma_mask(pdev, 0xffffff00ULL);
5782 pci_set_consistent_dma_mask(pdev,0xffffff00ULL);
5783 dev = alloc_ieee80211(sizeof(struct r8180_priv));
5786 priv = ieee80211_priv(dev);
5787 priv->ieee80211 = netdev_priv(dev);
5789 pci_set_drvdata(pdev, dev);
5790 SET_NETDEV_DEV(dev, &pdev->dev);
5792 priv = ieee80211_priv(dev);
5793 // memset(priv,0,sizeof(struct r8180_priv));
5797 #ifdef CONFIG_RTL8180_IO_MAP
5799 pio_start = (unsigned long)pci_resource_start (pdev, 0);
5800 pio_len = (unsigned long)pci_resource_len (pdev, 0);
5801 pio_flags = (unsigned long)pci_resource_flags (pdev, 0);
5803 if (!(pio_flags & IORESOURCE_IO)) {
5804 DMESG("region #0 not a PIO resource, aborting");
5808 //DMESG("IO space @ 0x%08lx", pio_start );
5809 if( ! request_region( pio_start, pio_len, RTL8180_MODULE_NAME ) ){
5810 DMESG("request_region failed!");
5815 dev->base_addr = ioaddr; // device I/O address
5819 pmem_start = pci_resource_start(pdev, 1);
5820 pmem_len = pci_resource_len(pdev, 1);
5821 pmem_flags = pci_resource_flags (pdev, 1);
5823 if (!(pmem_flags & IORESOURCE_MEM)) {
5824 DMESG("region #1 not a MMIO resource, aborting");
5828 //DMESG("Memory mapped space @ 0x%08lx ", pmem_start);
5829 if( ! request_mem_region(pmem_start, pmem_len, RTL8180_MODULE_NAME)) {
5830 DMESG("request_mem_region failed!");
5835 ioaddr = (unsigned long)ioremap_nocache( pmem_start, pmem_len);
5836 if( ioaddr == (unsigned long)NULL ){
5837 DMESG("ioremap failed!");
5838 // release_mem_region( pmem_start, pmem_len );
5842 dev->mem_start = ioaddr; // shared mem start
5843 dev->mem_end = ioaddr + pci_resource_len(pdev, 0); // shared mem end
5845 #endif //end #ifdef RTL_IO_MAP
5847 #ifdef CONFIG_RTL8185B
5848 //pci_read_config_byte(pdev, 0x05, ptr);
5849 //pci_write_config_byte(pdev, 0x05, (*ptr) & (~0x04));
5850 pci_read_config_byte(pdev, 0x05, &unit);
5851 pci_write_config_byte(pdev, 0x05, unit & (~0x04));
5854 dev->irq = pdev->irq;
5857 dev->netdev_ops = &rtl8180_netdev_ops;
5858 dev->wireless_handlers = &r8180_wx_handlers_def;
5860 #if WIRELESS_EXT >= 12
5861 #if WIRELESS_EXT < 17
5862 dev->get_wireless_stats = r8180_get_wireless_stats;
5864 dev->wireless_handlers = (struct iw_handler_def *) &r8180_wx_handlers_def;
5867 dev->type=ARPHRD_ETHER;
5868 dev->watchdog_timeo = HZ*3; //added by david woo, 2007.12.13
5870 if (dev_alloc_name(dev, ifname) < 0){
5871 DMESG("Oops: devname already taken! Trying wlan%%d...\n");
5873 // ifname = "ath%d";
5874 dev_alloc_name(dev, ifname);
5878 if(rtl8180_init(dev)!=0){
5879 DMESG("Initialization failed");
5883 netif_carrier_off(dev);
5885 register_netdev(dev);
5887 rtl8180_proc_init_one(dev);
5889 DMESG("Driver probe completed\n");
5894 #ifdef CONFIG_RTL8180_IO_MAP
5896 if( dev->base_addr != 0 ){
5898 release_region(dev->base_addr,
5899 pci_resource_len(pdev, 0) );
5902 if( dev->mem_start != (unsigned long)NULL ){
5903 iounmap( (void *)dev->mem_start );
5904 release_mem_region( pci_resource_start(pdev, 1),
5905 pci_resource_len(pdev, 1) );
5907 #endif //end #ifdef RTL_IO_MAP
5914 free_irq(dev->irq, dev);
5917 free_ieee80211(dev);
5920 pci_disable_device(pdev);
5922 DMESG("wlan driver load failed\n");
5923 pci_set_drvdata(pdev, NULL);
5929 static void __devexit rtl8180_pci_remove(struct pci_dev *pdev)
5931 struct r8180_priv *priv;
5932 struct net_device *dev = pci_get_drvdata(pdev);
5935 unregister_netdev(dev);
5937 priv=ieee80211_priv(dev);
5939 rtl8180_proc_remove_one(dev);
5941 priv->rf_close(dev);
5943 //rtl8180_rtx_disable(dev);
5944 //rtl8180_irq_disable(dev);
5946 //write_nic_word(dev,INTA,read_nic_word(dev,INTA));
5947 //force_pci_posting(dev);
5952 DMESG("Freeing irq %d",dev->irq);
5953 free_irq(dev->irq, dev);
5958 free_rx_desc_ring(dev);
5959 free_tx_desc_rings(dev);
5960 // free_beacon_desc_ring(dev,priv->txbeaconcount);
5962 #ifdef CONFIG_RTL8180_IO_MAP
5964 if( dev->base_addr != 0 ){
5966 release_region(dev->base_addr,
5967 pci_resource_len(pdev, 0) );
5970 if( dev->mem_start != (unsigned long)NULL ){
5971 iounmap( (void *)dev->mem_start );
5972 release_mem_region( pci_resource_start(pdev, 1),
5973 pci_resource_len(pdev, 1) );
5975 #endif /*end #ifdef RTL_IO_MAP*/
5977 free_ieee80211(dev);
5979 pci_disable_device(pdev);
5981 DMESG("wlan driver removed\n");
5985 /* fun with the built-in ieee80211 stack... */
5986 extern int ieee80211_crypto_init(void);
5987 extern void ieee80211_crypto_deinit(void);
5988 extern int ieee80211_crypto_tkip_init(void);
5989 extern void ieee80211_crypto_tkip_exit(void);
5990 extern int ieee80211_crypto_ccmp_init(void);
5991 extern void ieee80211_crypto_ccmp_exit(void);
5992 extern int ieee80211_crypto_wep_init(void);
5993 extern void ieee80211_crypto_wep_exit(void);
5995 static int __init rtl8180_pci_module_init(void)
5999 ret = ieee80211_crypto_init();
6001 printk(KERN_ERR "ieee80211_crypto_init() failed %d\n", ret);
6004 ret = ieee80211_crypto_tkip_init();
6006 printk(KERN_ERR "ieee80211_crypto_tkip_init() failed %d\n", ret);
6009 ret = ieee80211_crypto_ccmp_init();
6011 printk(KERN_ERR "ieee80211_crypto_ccmp_init() failed %d\n", ret);
6014 ret = ieee80211_crypto_wep_init();
6016 printk(KERN_ERR "ieee80211_crypto_wep_init() failed %d\n", ret);
6020 printk(KERN_INFO "\nLinux kernel driver for RTL8180 \
6021 / RTL8185 based WLAN cards\n");
6022 printk(KERN_INFO "Copyright (c) 2004-2005, Andrea Merello\n");
6023 DMESG("Initializing module");
6024 DMESG("Wireless extensions version %d", WIRELESS_EXT);
6025 rtl8180_proc_module_init();
6027 if(0!=pci_register_driver(&rtl8180_pci_driver))
6028 //if(0!=pci_module_init(&rtl8180_pci_driver))
6030 DMESG("No device found");
6031 /*pci_unregister_driver (&rtl8180_pci_driver);*/
6038 static void __exit rtl8180_pci_module_exit(void)
6040 pci_unregister_driver (&rtl8180_pci_driver);
6041 rtl8180_proc_module_remove();
6042 ieee80211_crypto_tkip_exit();
6043 ieee80211_crypto_ccmp_exit();
6044 ieee80211_crypto_wep_exit();
6045 ieee80211_crypto_deinit();
6050 void rtl8180_try_wake_queue(struct net_device *dev, int pri)
6052 unsigned long flags;
6054 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
6056 spin_lock_irqsave(&priv->tx_lock,flags);
6057 enough_desc = check_nic_enought_desc(dev,pri);
6058 spin_unlock_irqrestore(&priv->tx_lock,flags);
6061 ieee80211_wake_queue(priv->ieee80211);
6064 /*****************************************************************************
6065 -----------------------------IRQ STUFF---------------------------
6066 ******************************************************************************/
6068 void rtl8180_tx_isr(struct net_device *dev, int pri,short error)
6070 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
6072 u32 *tail; //tail virtual addr
6073 u32 *head; //head virtual addr
6074 u32 *begin;//start of ring virtual addr
6075 u32 *nicv; //nic pointer virtual addr
6076 // u32 *txdv; //packet just TXed
6077 u32 nic; //nic pointer physical addr
6078 u32 nicbegin;// start of ring physical addr
6081 /* physical addr are ok on 32 bits since we set DMA mask*/
6086 if (error) priv->stats.txretry++; //tony 20060601
6087 spin_lock_irqsave(&priv->tx_lock,flag);
6089 case MANAGE_PRIORITY:
6090 tail = priv->txmapringtail;
6091 begin = priv->txmapring;
6092 head = priv->txmapringhead;
6093 nic = read_nic_dword(dev,TX_MANAGEPRIORITY_RING_ADDR);
6094 nicbegin = priv->txmapringdma;
6098 tail = priv->txbkpringtail;
6099 begin = priv->txbkpring;
6100 head = priv->txbkpringhead;
6101 nic = read_nic_dword(dev,TX_BKPRIORITY_RING_ADDR);
6102 nicbegin = priv->txbkpringdma;
6106 tail = priv->txbepringtail;
6107 begin = priv->txbepring;
6108 head = priv->txbepringhead;
6109 nic = read_nic_dword(dev,TX_BEPRIORITY_RING_ADDR);
6110 nicbegin = priv->txbepringdma;
6114 tail = priv->txvipringtail;
6115 begin = priv->txvipring;
6116 head = priv->txvipringhead;
6117 nic = read_nic_dword(dev,TX_VIPRIORITY_RING_ADDR);
6118 nicbegin = priv->txvipringdma;
6122 tail = priv->txvopringtail;
6123 begin = priv->txvopring;
6124 head = priv->txvopringhead;
6125 nic = read_nic_dword(dev,TX_VOPRIORITY_RING_ADDR);
6126 nicbegin = priv->txvopringdma;
6130 tail = priv->txhpringtail;
6131 begin = priv->txhpring;
6132 head = priv->txhpringhead;
6133 nic = read_nic_dword(dev,TX_HIGHPRIORITY_RING_ADDR);
6134 nicbegin = priv->txhpringdma;
6138 spin_unlock_irqrestore(&priv->tx_lock,flag);
6141 /* DMESG("%x %s %x %x",((int)nic & 0xfff)/8/4,
6142 *(priv->txnpring + ((int)nic&0xfff)/4/8) & (1<<31) ? "filled" : "empty",
6143 (priv->txnpringtail - priv->txnpring)/8,(priv->txnpringhead -
6146 //nicv = (u32*) ((nic - nicbegin) + (int)begin);
6147 nicv = (u32*) ((nic - nicbegin) + (u8*)begin);
6148 if((head <= tail && (nicv > tail || nicv < head)) ||
6149 (head > tail && (nicv > tail && nicv < head))){
6151 DMESGW("nic has lost pointer");
6152 #ifdef DEBUG_TX_DESC
6153 //check_tx_ring(dev,NORM_PRIORITY);
6154 check_tx_ring(dev,pri);
6156 spin_unlock_irqrestore(&priv->tx_lock,flag);
6157 rtl8180_restart(dev);
6161 /* we check all the descriptors between the head and the nic,
6162 * but not the currenly pointed by the nic (the next to be txed)
6163 * and the previous of the pointed (might be in process ??)
6165 //if (head == nic) return;
6166 //DMESG("%x %x",head,nic);
6167 offs = (nic - nicbegin);
6168 //DMESG("%x %x %x",nic ,(u32)nicbegin, (int)nic -nicbegin);
6172 hd = (head - begin) /8;
6177 j = offs + (priv->txringcount -1 -hd);
6178 // j= priv->txringcount -1- (hd - offs);
6186 // printk("+++++++++++++check status desc\n");
6187 if((*head) & (1<<31))
6189 if(((*head)&(0x10000000)) != 0){
6190 // printk("++++++++++++++last desc,retry count is %d\n",((*head) & (0x000000ff)));
6191 priv->CurrRetryCnt += (u16)((*head) & (0x000000ff));
6195 priv->NumTxOkTotal++;
6196 // printk("NumTxOkTotal is %d\n",priv->NumTxOkTotal++);
6199 // printk("in function %s:curr_retry_count is %d\n",__func__,((*head) & (0x000000ff)));
6202 priv->NumTxOkBytesTotal += (*(head+3)) & (0x00000fff);
6204 // printk("in function %s:curr_txokbyte_count is %d\n",__func__,(*(head+3)) & (0x00000fff));
6205 *head = *head &~ (1<<31);
6207 if((head - begin)/8 == priv->txringcount-1)
6215 txdv = begin + (priv->txringcount -1)*8;
6219 txed = !(txdv[0] &(1<<31));
6222 if(!(txdv[0] & (1<<15))) error = 1;
6223 //if(!(txdv[0] & (1<<30))) error = 1;
6224 if(error)DMESG("%x",txdv[0]);
6227 //DMESG("%x",txdv[0]);
6228 /* the head has been moved to the last certainly TXed
6229 * (or at least processed by the nic) packet.
6230 * The driver take forcefully owning of all these packets
6231 * If the packet previous of the nic pointer has been
6232 * processed this doesn't matter: it will be checked
6233 * here at the next round. Anyway if no more packet are
6234 * TXed no memory leak occour at all.
6238 case MANAGE_PRIORITY:
6239 priv->txmapringhead = head;
6240 //printk("1==========================================> priority check!\n");
6241 if(priv->ack_tx_to_ieee){
6242 // try to implement power-save mode 2008.1.22
6243 // printk("2==========================================> priority check!\n");
6245 if(rtl8180_is_tx_queue_empty(dev)){
6246 // printk("tx queue empty, after send null sleep packet, try to sleep !\n");
6247 priv->ack_tx_to_ieee = 0;
6248 ieee80211_ps_tx_ack(priv->ieee80211,!error);
6255 priv->txbkpringhead = head;
6259 priv->txbepringhead = head;
6263 priv->txvipringhead = head;
6267 priv->txvopringhead = head;
6271 priv->txhpringhead = head;
6275 /*DMESG("%x %x %x", (priv->txnpringhead - priv->txnpring) /8 ,
6276 (priv->txnpringtail - priv->txnpring) /8,
6280 spin_unlock_irqrestore(&priv->tx_lock,flag);
6284 void rtl8180_tx_irq_wq(struct work_struct *work)
6286 //struct r8180_priv *priv = container_of(work, struct r8180_priv, reset_wq);
6287 struct delayed_work *dwork = to_delayed_work(work);
6288 struct ieee80211_device * ieee = (struct ieee80211_device*)
6289 container_of(dwork, struct ieee80211_device, watch_dog_wq);
6290 struct net_device *dev = ieee->dev;
6292 rtl8180_tx_isr(dev,MANAGE_PRIORITY,0);
6294 irqreturn_t rtl8180_interrupt(int irq, void *netdev, struct pt_regs *regs)
6296 struct net_device *dev = (struct net_device *) netdev;
6297 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
6298 unsigned long flags;
6301 /* We should return IRQ_NONE, but for now let me keep this */
6302 if(priv->irq_enabled == 0) return IRQ_HANDLED;
6304 spin_lock_irqsave(&priv->irq_th_lock,flags);
6306 #ifdef CONFIG_RTL8185B
6308 inta = read_nic_dword(dev, ISR);// & priv->IntrMask;
6309 write_nic_dword(dev,ISR,inta); // reset int situation
6311 inta = read_nic_word(dev,INTA) & priv->irq_mask;
6312 write_nic_word(dev,INTA,inta); // reset int situation
6315 priv->stats.shints++;
6317 //DMESG("Enter interrupt, ISR value = 0x%08x", inta);
6320 spin_unlock_irqrestore(&priv->irq_th_lock,flags);
6323 most probably we can safely return IRQ_NONE,
6324 but for now is better to avoid problems
6330 spin_unlock_irqrestore(&priv->irq_th_lock,flags);
6336 DMESG("NIC irq %x",inta);
6338 //priv->irqpending = inta;
6341 if(!netif_running(dev)) {
6342 spin_unlock_irqrestore(&priv->irq_th_lock,flags);
6346 if(inta & ISR_TimeOut){
6347 write_nic_dword(dev, TimerInt, 0);
6348 //DMESG("=================>waking up");
6349 // rtl8180_hw_wakeup(dev);
6352 if(inta & ISR_TBDOK){
6353 priv->stats.txbeacon++;
6356 if(inta & ISR_TBDER){
6357 priv->stats.txbeaconerr++;
6360 if(inta & IMR_TMGDOK ) {
6361 // priv->NumTxOkTotal++;
6362 rtl8180_tx_isr(dev,MANAGE_PRIORITY,0);
6363 // schedule_work(&priv->tx_irq_wq);
6367 if(inta & ISR_THPDER){
6369 DMESG ("TX high priority ERR");
6371 priv->stats.txhperr++;
6372 rtl8180_tx_isr(dev,HI_PRIORITY,1);
6373 priv->ieee80211->stats.tx_errors++;
6376 if(inta & ISR_THPDOK){ //High priority tx ok
6378 DMESG ("TX high priority OK");
6380 // priv->NumTxOkTotal++;
6381 //priv->NumTxOkInPeriod++; //YJ,del,080828
6382 priv->link_detect.NumTxOkInPeriod++; //YJ,add,080828
6383 priv->stats.txhpokint++;
6384 rtl8180_tx_isr(dev,HI_PRIORITY,0);
6387 if(inta & ISR_RER) {
6388 priv->stats.rxerr++;
6390 DMESGW("RX error int");
6393 #ifdef CONFIG_RTL8185B
6394 if(inta & ISR_TBKDER){ //corresponding to BK_PRIORITY
6395 priv->stats.txbkperr++;
6396 priv->ieee80211->stats.tx_errors++;
6398 DMESGW("TX bkp error int");
6400 //tasklet_schedule(&priv->irq_tx_tasklet);
6401 rtl8180_tx_isr(dev,BK_PRIORITY,1);
6402 rtl8180_try_wake_queue(dev, BE_PRIORITY);
6405 if(inta & ISR_TBEDER){ //corresponding to BE_PRIORITY
6406 priv->stats.txbeperr++;
6407 priv->ieee80211->stats.tx_errors++;
6409 DMESGW("TX bep error int");
6411 rtl8180_tx_isr(dev,BE_PRIORITY,1);
6412 //tasklet_schedule(&priv->irq_tx_tasklet);
6413 rtl8180_try_wake_queue(dev, BE_PRIORITY);
6416 if(inta & ISR_TNPDER){ //corresponding to VO_PRIORITY
6417 priv->stats.txnperr++;
6418 priv->ieee80211->stats.tx_errors++;
6420 DMESGW("TX np error int");
6422 //tasklet_schedule(&priv->irq_tx_tasklet);
6423 rtl8180_tx_isr(dev,NORM_PRIORITY,1);
6424 #ifdef CONFIG_RTL8185B
6425 rtl8180_try_wake_queue(dev, NORM_PRIORITY);
6429 if(inta & ISR_TLPDER){ //corresponding to VI_PRIORITY
6430 priv->stats.txlperr++;
6431 priv->ieee80211->stats.tx_errors++;
6433 DMESGW("TX lp error int");
6435 rtl8180_tx_isr(dev,LOW_PRIORITY,1);
6436 //tasklet_schedule(&priv->irq_tx_tasklet);
6437 rtl8180_try_wake_queue(dev, LOW_PRIORITY);
6442 DMESG("Frame arrived !");
6444 //priv->NumRxOkInPeriod++; //YJ,del,080828
6445 priv->stats.rxint++;
6446 tasklet_schedule(&priv->irq_rx_tasklet);
6449 if(inta & ISR_RQoSOK ){
6451 DMESG("QoS Frame arrived !");
6453 //priv->NumRxOkInPeriod++; //YJ,del,080828
6454 priv->stats.rxint++;
6455 tasklet_schedule(&priv->irq_rx_tasklet);
6457 if(inta & ISR_BcnInt) {
6458 //DMESG("Preparing Beacons");
6459 rtl8180_prepare_beacon(dev);
6464 DMESGW("No RX descriptor available");
6465 priv->stats.rxrdu++;
6467 tasklet_schedule(&priv->irq_rx_tasklet);
6468 /*queue_work(priv->workqueue ,&priv->restart_work);*/
6471 if(inta & ISR_RXFOVW){
6473 DMESGW("RX fifo overflow");
6475 priv->stats.rxoverflow++;
6476 tasklet_schedule(&priv->irq_rx_tasklet);
6477 //queue_work(priv->workqueue ,&priv->restart_work);
6480 if(inta & ISR_TXFOVW) priv->stats.txoverflow++;
6482 if(inta & ISR_TNPDOK){ //Normal priority tx ok
6484 DMESG ("TX normal priority OK");
6486 // priv->NumTxOkTotal++;
6487 //priv->NumTxOkInPeriod++; //YJ,del,080828
6488 priv->link_detect.NumTxOkInPeriod++; //YJ,add,080828
6489 // priv->ieee80211->stats.tx_packets++;
6490 priv->stats.txnpokint++;
6491 rtl8180_tx_isr(dev,NORM_PRIORITY,0);
6494 if(inta & ISR_TLPDOK){ //Low priority tx ok
6496 DMESG ("TX low priority OK");
6498 // priv->NumTxOkTotal++;
6499 //priv->NumTxOkInPeriod++; //YJ,del,080828
6500 priv->link_detect.NumTxOkInPeriod++; //YJ,add,080828
6501 // priv->ieee80211->stats.tx_packets++;
6502 priv->stats.txlpokint++;
6503 rtl8180_tx_isr(dev,LOW_PRIORITY,0);
6504 rtl8180_try_wake_queue(dev, LOW_PRIORITY);
6507 #ifdef CONFIG_RTL8185B
6508 if(inta & ISR_TBKDOK){ //corresponding to BK_PRIORITY
6509 priv->stats.txbkpokint++;
6511 DMESGW("TX bk priority ok");
6513 // priv->NumTxOkTotal++;
6514 //priv->NumTxOkInPeriod++; //YJ,del,080828
6515 priv->link_detect.NumTxOkInPeriod++; //YJ,add,080828
6516 rtl8180_tx_isr(dev,BK_PRIORITY,0);
6517 rtl8180_try_wake_queue(dev, BE_PRIORITY);
6520 if(inta & ISR_TBEDOK){ //corresponding to BE_PRIORITY
6521 priv->stats.txbeperr++;
6523 DMESGW("TX be priority ok");
6525 // priv->NumTxOkTotal++;
6526 //priv->NumTxOkInPeriod++; //YJ,del,080828
6527 priv->link_detect.NumTxOkInPeriod++; //YJ,add,080828
6528 rtl8180_tx_isr(dev,BE_PRIORITY,0);
6529 rtl8180_try_wake_queue(dev, BE_PRIORITY);
6532 force_pci_posting(dev);
6533 spin_unlock_irqrestore(&priv->irq_th_lock,flags);
6539 void rtl8180_irq_rx_tasklet(struct r8180_priv* priv)
6541 // unsigned long flags;
6543 /* spin_lock_irqsave(&priv->irq_lock, flags);
6544 priv->irq_mask &=~IMR_ROK;
6545 priv->irq_mask &=~IMR_RDU;
6547 rtl8180_irq_enable(priv->dev);
6548 spin_unlock_irqrestore(&priv->irq_lock, flags);
6550 rtl8180_rx(priv->dev);
6552 /* spin_lock_irqsave(&priv->irq_lock, flags);
6553 priv->irq_mask |= IMR_ROK;
6554 priv->irq_mask |= IMR_RDU;
6555 rtl8180_irq_enable(priv->dev);
6556 spin_unlock_irqrestore(&priv->irq_lock, flags);
6560 /****************************************************************************
6561 lizhaoming--------------------------- RF power on/power off -----------------
6562 *****************************************************************************/
6564 void GPIOChangeRFWorkItemCallBack(struct work_struct *work)
6566 //struct delayed_work *dwork = to_delayed_work(work);
6567 struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, GPIOChangeRFWorkItem.work);
6568 struct net_device *dev = ieee->dev;
6569 struct r8180_priv *priv = ieee80211_priv(dev);
6574 RT_RF_POWER_STATE eRfPowerStateToSet;
6575 bool bActuallySet=false;
6578 static char *RadioPowerPath = "/etc/acpi/events/RadioPower.sh";
6579 static char *envp[] = {"HOME=/", "TERM=linux", "PATH=/usr/bin:/bin", NULL};
6580 static int readf_count = 0;
6581 //printk("============>%s in \n", __func__);
6584 if(readf_count % 10 == 0)
6585 priv->PowerProfile = read_acadapter_file("/proc/acpi/ac_adapter/AC0/state");
6587 readf_count = (readf_count+1)%0xffff;
6590 if(priv->up == 0)//driver stopped
6592 printk("\nDo nothing...");
6598 // We should turn off LED before polling FF51[4].
6601 btPSR = read_nic_byte(dev, PSR);
6602 write_nic_byte(dev, PSR, (btPSR & ~BIT3));
6604 //It need to delay 4us suggested by Jong, 2008-01-16
6607 //HW radio On/Off according to the value of FF51[4](config0)
6608 btConfig0 = btPSR = read_nic_byte(dev, CONFIG0);
6611 write_nic_byte(dev, PSR, btPSR| BIT3);
6613 eRfPowerStateToSet = (btConfig0 & BIT4) ? eRfOn : eRfOff;
6615 if((priv->ieee80211->bHwRadioOff == true) && (eRfPowerStateToSet == eRfOn))
6617 priv->ieee80211->bHwRadioOff = false;
6618 bActuallySet = true;
6620 else if((priv->ieee80211->bHwRadioOff == false) && (eRfPowerStateToSet == eRfOff))
6622 priv->ieee80211->bHwRadioOff = true;
6623 bActuallySet = true;
6628 MgntActSet_RF_State(dev, eRfPowerStateToSet, RF_CHANGE_BY_HW);
6630 /* To update the UI status for Power status changed */
6631 if(priv->ieee80211->bHwRadioOff == true)
6634 //if(!priv->RfOffReason)
6637 // argv[1] = "RFOFF";
6639 argv[0] = RadioPowerPath;
6642 call_usermodehelper(RadioPowerPath,argv,envp,1);
6649 static u8 read_acadapter_file(char *filename)
6657 mm_segment_t old_fs = get_fs();
6660 fd = sys_open(filename, O_RDONLY, 0);
6662 while (sys_read(fd, buf, 1) == 1)
6677 // printk("%s \n", ret);
6680 if(strncmp(ret, "off-line",8) == 0)
6688 /***************************************************************************
6689 ------------------- module init / exit stubs ----------------
6690 ****************************************************************************/
6691 module_init(rtl8180_pci_module_init);
6692 module_exit(rtl8180_pci_module_exit);