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.
26 Power management interface routines.
27 Written by Mariusz Matuszek.
30 #undef RX_DONT_PASS_UL
33 #include <linux/syscalls.h>
34 #include <linux/eeprom_93cx6.h>
38 #include "r8180_rtl8225.h" /* RTL8225 Radio frontend */
39 #include "r8180_93cx6.h" /* Card EEPROM */
43 #include "ieee80211/dot11d.h"
45 static struct pci_device_id rtl8180_pci_id_tbl[] __devinitdata = {
47 .vendor = PCI_VENDOR_ID_REALTEK,
49 .subvendor = PCI_ANY_ID,
50 .subdevice = PCI_ANY_ID,
63 static char* ifname = "wlan%d";
64 static int hwseqnum = 0;
66 static int channels = 0x3fff;
68 #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 )
69 #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])
70 MODULE_LICENSE("GPL");
71 MODULE_DEVICE_TABLE(pci, rtl8180_pci_id_tbl);
72 MODULE_AUTHOR("Andrea Merello <andreamrl@tiscali.it>");
73 MODULE_DESCRIPTION("Linux driver for Realtek RTL8180 / RTL8185 WiFi cards");
76 module_param(ifname, charp, S_IRUGO|S_IWUSR );
77 module_param(hwseqnum,int, S_IRUGO|S_IWUSR);
78 module_param(hwwep,int, S_IRUGO|S_IWUSR);
79 module_param(channels,int, S_IRUGO|S_IWUSR);
81 MODULE_PARM_DESC(devname," Net interface name, wlan%d=default");
82 MODULE_PARM_DESC(hwseqnum," Try to use hardware 802.11 header sequence numbers. Zero=default");
83 MODULE_PARM_DESC(hwwep," Try to use hardware WEP support. Still broken and not available on all cards");
84 MODULE_PARM_DESC(channels," Channel bitmask for specific locales. NYI");
87 static int __devinit rtl8180_pci_probe(struct pci_dev *pdev,
88 const struct pci_device_id *id);
90 static void __devexit rtl8180_pci_remove(struct pci_dev *pdev);
92 static void rtl8180_shutdown (struct pci_dev *pdev)
94 struct net_device *dev = pci_get_drvdata(pdev);
95 if (dev->netdev_ops->ndo_stop)
96 dev->netdev_ops->ndo_stop(dev);
97 pci_disable_device(pdev);
100 static int rtl8180_suspend(struct pci_dev *pdev, pm_message_t state)
102 struct net_device *dev = pci_get_drvdata(pdev);
104 if (!netif_running(dev))
105 goto out_pci_suspend;
107 if (dev->netdev_ops->ndo_stop)
108 dev->netdev_ops->ndo_stop(dev);
110 netif_device_detach(dev);
113 pci_save_state(pdev);
114 pci_disable_device(pdev);
115 pci_set_power_state(pdev, pci_choose_state(pdev, state));
119 static int rtl8180_resume(struct pci_dev *pdev)
121 struct net_device *dev = pci_get_drvdata(pdev);
125 pci_set_power_state(pdev, PCI_D0);
127 err = pci_enable_device(pdev);
129 printk(KERN_ERR "%s: pci_enable_device failed on resume\n",
135 pci_restore_state(pdev);
138 * Suspend/Resume resets the PCI configuration space, so we have to
139 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
140 * from interfering with C3 CPU state. pci_restore_state won't help
141 * here since it only restores the first 64 bytes pci config header.
143 pci_read_config_dword(pdev, 0x40, &val);
144 if ((val & 0x0000ff00) != 0)
145 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
147 if (!netif_running(dev))
150 if (dev->netdev_ops->ndo_open)
151 dev->netdev_ops->ndo_open(dev);
153 netif_device_attach(dev);
158 static struct pci_driver rtl8180_pci_driver = {
159 .name = RTL8180_MODULE_NAME,
160 .id_table = rtl8180_pci_id_tbl,
161 .probe = rtl8180_pci_probe,
162 .remove = __devexit_p(rtl8180_pci_remove),
163 .suspend = rtl8180_suspend,
164 .resume = rtl8180_resume,
165 .shutdown = rtl8180_shutdown,
168 u8 read_nic_byte(struct net_device *dev, int x)
170 return 0xff&readb((u8*)dev->mem_start +x);
173 u32 read_nic_dword(struct net_device *dev, int x)
175 return readl((u8*)dev->mem_start +x);
178 u16 read_nic_word(struct net_device *dev, int x)
180 return readw((u8*)dev->mem_start +x);
183 void write_nic_byte(struct net_device *dev, int x,u8 y)
185 writeb(y,(u8*)dev->mem_start +x);
189 void write_nic_dword(struct net_device *dev, int x,u32 y)
191 writel(y,(u8*)dev->mem_start +x);
195 void write_nic_word(struct net_device *dev, int x,u16 y)
197 writew(y,(u8*)dev->mem_start +x);
201 inline void force_pci_posting(struct net_device *dev)
203 read_nic_byte(dev,EPROM_CMD);
207 irqreturn_t rtl8180_interrupt(int irq, void *netdev, struct pt_regs *regs);
208 void set_nic_rxring(struct net_device *dev);
209 void set_nic_txring(struct net_device *dev);
210 static struct net_device_stats *rtl8180_stats(struct net_device *dev);
211 void rtl8180_commit(struct net_device *dev);
212 void rtl8180_start_tx_beacon(struct net_device *dev);
214 static struct proc_dir_entry *rtl8180_proc = NULL;
216 static int proc_get_registers(char *page, char **start,
217 off_t offset, int count,
218 int *eof, void *data)
220 struct net_device *dev = data;
225 /* This dump the current register page */
226 for (n = 0; n <= max;) {
227 len += snprintf(page + len, count - len, "\nD: %2x > ", n);
229 for (i = 0; i < 16 && n <= max; i++, n++)
230 len += snprintf(page + len, count - len, "%2x ",
231 read_nic_byte(dev, n));
233 len += snprintf(page + len, count - len,"\n");
239 int get_curr_tx_free_desc(struct net_device *dev, int priority);
241 static int proc_get_stats_hw(char *page, char **start,
242 off_t offset, int count,
243 int *eof, void *data)
251 static int proc_get_stats_rx(char *page, char **start,
252 off_t offset, int count,
253 int *eof, void *data)
255 struct net_device *dev = data;
256 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
260 len += snprintf(page + len, count - len,
263 "RX CRC Error(0-500): %lu\n"
264 "RX CRC Error(500-1000): %lu\n"
265 "RX CRC Error(>1000): %lu\n"
266 "RX ICV Error: %lu\n",
269 priv->stats.rxcrcerrmin,
270 priv->stats.rxcrcerrmid,
271 priv->stats.rxcrcerrmax,
279 static int proc_get_stats_tx(char *page, char **start,
280 off_t offset, int count,
281 int *eof, void *data)
283 struct net_device *dev = data;
284 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
287 unsigned long totalOK;
289 totalOK=priv->stats.txnpokint+priv->stats.txhpokint+priv->stats.txlpokint;
290 len += snprintf(page + len, count - len,
294 "TX beacon OK: %lu\n"
295 "TX beacon error: %lu\n",
297 priv->stats.txnperr+priv->stats.txhperr+priv->stats.txlperr,
299 priv->stats.txbeacon,
300 priv->stats.txbeaconerr
307 void rtl8180_proc_module_init(void)
309 DMESG("Initializing proc filesystem");
310 rtl8180_proc=create_proc_entry(RTL8180_MODULE_NAME, S_IFDIR, init_net.proc_net);
313 void rtl8180_proc_module_remove(void)
315 remove_proc_entry(RTL8180_MODULE_NAME, init_net.proc_net);
318 void rtl8180_proc_remove_one(struct net_device *dev)
320 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
322 remove_proc_entry("stats-hw", priv->dir_dev);
323 remove_proc_entry("stats-tx", priv->dir_dev);
324 remove_proc_entry("stats-rx", priv->dir_dev);
325 remove_proc_entry("registers", priv->dir_dev);
326 remove_proc_entry(dev->name, rtl8180_proc);
327 priv->dir_dev = NULL;
331 void rtl8180_proc_init_one(struct net_device *dev)
333 struct proc_dir_entry *e;
334 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
336 priv->dir_dev = rtl8180_proc;
337 if (!priv->dir_dev) {
338 DMESGE("Unable to initialize /proc/net/r8180/%s\n",
343 e = create_proc_read_entry("stats-hw", S_IFREG | S_IRUGO,
344 priv->dir_dev, proc_get_stats_hw, dev);
346 DMESGE("Unable to initialize "
347 "/proc/net/r8180/%s/stats-hw\n",
351 e = create_proc_read_entry("stats-rx", S_IFREG | S_IRUGO,
352 priv->dir_dev, proc_get_stats_rx, dev);
354 DMESGE("Unable to initialize "
355 "/proc/net/r8180/%s/stats-rx\n",
360 e = create_proc_read_entry("stats-tx", S_IFREG | S_IRUGO,
361 priv->dir_dev, proc_get_stats_tx, dev);
363 DMESGE("Unable to initialize "
364 "/proc/net/r8180/%s/stats-tx\n",
368 e = create_proc_read_entry("registers", S_IFREG | S_IRUGO,
369 priv->dir_dev, proc_get_registers, dev);
371 DMESGE("Unable to initialize "
372 "/proc/net/r8180/%s/registers\n",
378 FIXME: check if we can use some standard already-existent
379 data type+functions in kernel
382 short buffer_add(struct buffer **buffer, u32 *buf, dma_addr_t dma,
383 struct buffer **bufferhead)
389 *buffer = kmalloc(sizeof(struct buffer),GFP_KERNEL);
391 if (*buffer == NULL) {
392 DMESGE("Failed to kmalloc head of TX/RX struct");
395 (*buffer)->next=*buffer;
398 if(bufferhead !=NULL)
399 (*bufferhead) = (*buffer);
404 while(tmp->next!=(*buffer)) tmp=tmp->next;
405 if ((tmp->next= kmalloc(sizeof(struct buffer),GFP_KERNEL)) == NULL){
406 DMESGE("Failed to kmalloc TX/RX struct");
411 tmp->next->next=*buffer;
416 void buffer_free(struct net_device *dev,struct buffer **buffer,int len,short
420 struct buffer *tmp,*next;
421 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
422 struct pci_dev *pdev=priv->pdev;
432 pci_free_consistent(pdev,len,
435 pci_unmap_single(pdev, tmp->dma,
436 len,PCI_DMA_FROMDEVICE);
442 while(next != *buffer);
447 void print_buffer(u32 *buffer, int len)
450 u8 *buf =(u8*)buffer;
452 printk("ASCII BUFFER DUMP (len: %x):\n",len);
457 printk("\nBINARY BUFFER DUMP (len: %x):\n",len);
460 printk("%02x",buf[i]);
465 int get_curr_tx_free_desc(struct net_device *dev, int priority)
467 struct r8180_priv *priv = ieee80211_priv(dev);
473 case MANAGE_PRIORITY:
474 head = priv->txmapringhead;
475 tail = priv->txmapringtail;
478 head = priv->txbkpringhead;
479 tail = priv->txbkpringtail;
482 head = priv->txbepringhead;
483 tail = priv->txbepringtail;
486 head = priv->txvipringhead;
487 tail = priv->txvipringtail;
490 head = priv->txvopringhead;
491 tail = priv->txvopringtail;
494 head = priv->txhpringhead;
495 tail = priv->txhpringtail;
502 ret = priv->txringcount - (tail - head)/8;
504 ret = (head - tail)/8;
506 if (ret > priv->txringcount)
512 short check_nic_enought_desc(struct net_device *dev, int priority)
514 struct r8180_priv *priv = ieee80211_priv(dev);
515 struct ieee80211_device *ieee = netdev_priv(dev);
516 int requiredbyte, required;
518 requiredbyte = priv->ieee80211->fts + sizeof(struct ieee80211_header_data);
520 if (ieee->current_network.QoS_Enable)
523 required = requiredbyte / (priv->txbuffsize-4);
525 if (requiredbyte % priv->txbuffsize)
528 /* for now we keep two free descriptor as a safety boundary
529 * between the tail and the head
532 return (required+2 < get_curr_tx_free_desc(dev,priority));
535 void fix_tx_fifo(struct net_device *dev)
537 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
541 for (tmp=priv->txmapring, i=0;
542 i < priv->txringcount;
544 *tmp = *tmp &~ (1<<31);
547 for (tmp=priv->txbkpring, i=0;
548 i < priv->txringcount;
550 *tmp = *tmp &~ (1<<31);
553 for (tmp=priv->txbepring, i=0;
554 i < priv->txringcount;
556 *tmp = *tmp &~ (1<<31);
558 for (tmp=priv->txvipring, i=0;
559 i < priv->txringcount;
561 *tmp = *tmp &~ (1<<31);
564 for (tmp=priv->txvopring, i=0;
565 i < priv->txringcount;
567 *tmp = *tmp &~ (1<<31);
570 for (tmp=priv->txhpring, i=0;
571 i < priv->txringcount;
573 *tmp = *tmp &~ (1<<31);
576 for (tmp=priv->txbeaconring, i=0;
577 i < priv->txbeaconcount;
579 *tmp = *tmp &~ (1<<31);
582 priv->txmapringtail = priv->txmapring;
583 priv->txmapringhead = priv->txmapring;
584 priv->txmapbufstail = priv->txmapbufs;
586 priv->txbkpringtail = priv->txbkpring;
587 priv->txbkpringhead = priv->txbkpring;
588 priv->txbkpbufstail = priv->txbkpbufs;
590 priv->txbepringtail = priv->txbepring;
591 priv->txbepringhead = priv->txbepring;
592 priv->txbepbufstail = priv->txbepbufs;
594 priv->txvipringtail = priv->txvipring;
595 priv->txvipringhead = priv->txvipring;
596 priv->txvipbufstail = priv->txvipbufs;
598 priv->txvopringtail = priv->txvopring;
599 priv->txvopringhead = priv->txvopring;
600 priv->txvopbufstail = priv->txvopbufs;
602 priv->txhpringtail = priv->txhpring;
603 priv->txhpringhead = priv->txhpring;
604 priv->txhpbufstail = priv->txhpbufs;
606 priv->txbeaconringtail = priv->txbeaconring;
607 priv->txbeaconbufstail = priv->txbeaconbufs;
610 ieee80211_reset_queue(priv->ieee80211);
611 priv->ack_tx_to_ieee = 0;
614 void fix_rx_fifo(struct net_device *dev)
616 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
618 struct buffer *rxbuf;
621 rx_desc_size = 8; // 4*8 = 32 bytes
623 for (tmp=priv->rxring, rxbuf=priv->rxbufferhead;
624 (tmp < (priv->rxring)+(priv->rxringcount)*rx_desc_size);
625 tmp+=rx_desc_size,rxbuf=rxbuf->next){
626 *(tmp+2) = rxbuf->dma;
628 *tmp=*tmp | priv->rxbuffersize;
632 priv->rxringtail=priv->rxring;
633 priv->rxbuffer=priv->rxbufferhead;
634 priv->rx_skb_complete=1;
638 unsigned char QUALITY_MAP[] = {
639 0x64, 0x64, 0x64, 0x63, 0x63, 0x62, 0x62, 0x61,
640 0x61, 0x60, 0x60, 0x5f, 0x5f, 0x5e, 0x5d, 0x5c,
641 0x5b, 0x5a, 0x59, 0x57, 0x56, 0x54, 0x52, 0x4f,
642 0x4c, 0x49, 0x45, 0x41, 0x3c, 0x37, 0x31, 0x29,
643 0x24, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
644 0x22, 0x22, 0x21, 0x21, 0x21, 0x21, 0x21, 0x20,
645 0x20, 0x20, 0x20, 0x1f, 0x1f, 0x1e, 0x1e, 0x1e,
646 0x1d, 0x1d, 0x1c, 0x1c, 0x1b, 0x1a, 0x19, 0x19,
647 0x18, 0x17, 0x16, 0x15, 0x14, 0x12, 0x11, 0x0f,
648 0x0e, 0x0c, 0x0a, 0x08, 0x06, 0x04, 0x01, 0x00
651 unsigned char STRENGTH_MAP[] = {
652 0x64, 0x64, 0x63, 0x62, 0x61, 0x60, 0x5f, 0x5e,
653 0x5d, 0x5c, 0x5b, 0x5a, 0x57, 0x54, 0x52, 0x50,
654 0x4e, 0x4c, 0x4a, 0x48, 0x46, 0x44, 0x41, 0x3f,
655 0x3c, 0x3a, 0x37, 0x36, 0x36, 0x1c, 0x1c, 0x1b,
656 0x1b, 0x1a, 0x1a, 0x19, 0x19, 0x18, 0x18, 0x17,
657 0x17, 0x16, 0x16, 0x15, 0x15, 0x14, 0x14, 0x13,
658 0x13, 0x12, 0x12, 0x11, 0x11, 0x10, 0x10, 0x0f,
659 0x0f, 0x0e, 0x0e, 0x0d, 0x0d, 0x0c, 0x0c, 0x0b,
660 0x0b, 0x0a, 0x0a, 0x09, 0x09, 0x08, 0x08, 0x07,
661 0x07, 0x06, 0x06, 0x05, 0x04, 0x03, 0x02, 0x00
664 void rtl8180_RSSI_calc(struct net_device *dev, u8 *rssi, u8 *qual)
666 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
677 _rssi = 0; // avoid gcc complains..
680 temp = QUALITY_MAP[q];
692 switch(priv->rf_chip){
696 if ( !lsb || !(temp2 <= 0x3c) ) {
699 temp2 = 100 * temp2 / 0x3c;
701 *rssi = temp2 & 0xff;
702 _rssi = temp2 & 0xff;
704 case RFCHIPID_INTERSIL:
715 if ( temp2 <= 0x3e0000 ) {
716 if ( temp2 < 0xffef0000 )
730 temp3 = temp3 / 0x6d;
732 _rssi = temp3 & 0xff;
733 *rssi = temp3 & 0xff;
738 if ( ! lsb || !(temp2 <= 0x3c) ){
741 temp2 = (100 * temp2) / 0x3c;
743 *rssi = temp2 & 0xff;
744 _rssi = temp2 & 0xff;
746 case RFCHIPID_PHILIPS:
747 if( orig_qual <= 0x4e ){
748 _rssi = STRENGTH_MAP[orig_qual];
769 *rssi = temp2 & 0xff;
770 _rssi = temp2 & 0xff;
785 void rtl8180_irq_enable(struct net_device *dev)
787 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
789 priv->irq_enabled = 1;
790 write_nic_word(dev,INTA_MASK, priv->irq_mask);
793 void rtl8180_irq_disable(struct net_device *dev)
795 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
797 write_nic_dword(dev,IMR,0);
798 force_pci_posting(dev);
799 priv->irq_enabled = 0;
802 void rtl8180_set_mode(struct net_device *dev,int mode)
806 ecmd=read_nic_byte(dev, EPROM_CMD);
807 ecmd=ecmd &~ EPROM_CMD_OPERATING_MODE_MASK;
808 ecmd=ecmd | (mode<<EPROM_CMD_OPERATING_MODE_SHIFT);
809 ecmd=ecmd &~ (1<<EPROM_CS_SHIFT);
810 ecmd=ecmd &~ (1<<EPROM_CK_SHIFT);
811 write_nic_byte(dev, EPROM_CMD, ecmd);
814 void rtl8180_adapter_start(struct net_device *dev);
815 void rtl8180_beacon_tx_enable(struct net_device *dev);
817 void rtl8180_update_msr(struct net_device *dev)
819 struct r8180_priv *priv = ieee80211_priv(dev);
823 msr = read_nic_byte(dev, MSR);
824 msr &= ~ MSR_LINK_MASK;
826 rxconf=read_nic_dword(dev,RX_CONF);
828 if(priv->ieee80211->state == IEEE80211_LINKED)
830 if(priv->ieee80211->iw_mode == IW_MODE_ADHOC)
831 msr |= (MSR_LINK_ADHOC<<MSR_LINK_SHIFT);
832 else if (priv->ieee80211->iw_mode == IW_MODE_MASTER)
833 msr |= (MSR_LINK_MASTER<<MSR_LINK_SHIFT);
834 else if (priv->ieee80211->iw_mode == IW_MODE_INFRA)
835 msr |= (MSR_LINK_MANAGED<<MSR_LINK_SHIFT);
837 msr |= (MSR_LINK_NONE<<MSR_LINK_SHIFT);
838 rxconf |= (1<<RX_CHECK_BSSID_SHIFT);
841 msr |= (MSR_LINK_NONE<<MSR_LINK_SHIFT);
842 rxconf &= ~(1<<RX_CHECK_BSSID_SHIFT);
845 write_nic_byte(dev, MSR, msr);
846 write_nic_dword(dev, RX_CONF, rxconf);
849 void rtl8180_set_chan(struct net_device *dev,short ch)
851 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
853 if ((ch > 14) || (ch < 1)) {
854 printk("In %s: Invalid chnanel %d\n", __func__, ch);
859 priv->rf_set_chan(dev,priv->chan);
862 void rtl8180_rx_enable(struct net_device *dev)
866 /* for now we accept data, management & ctl frame*/
867 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
869 rxconf=read_nic_dword(dev,RX_CONF);
870 rxconf = rxconf &~ MAC_FILTER_MASK;
871 rxconf = rxconf | (1<<ACCEPT_MNG_FRAME_SHIFT);
872 rxconf = rxconf | (1<<ACCEPT_DATA_FRAME_SHIFT);
873 rxconf = rxconf | (1<<ACCEPT_BCAST_FRAME_SHIFT);
874 rxconf = rxconf | (1<<ACCEPT_MCAST_FRAME_SHIFT);
875 if (dev->flags & IFF_PROMISC)
876 DMESG("NIC in promisc mode");
878 if(priv->ieee80211->iw_mode == IW_MODE_MONITOR || \
879 dev->flags & IFF_PROMISC){
880 rxconf = rxconf | (1<<ACCEPT_ALLMAC_FRAME_SHIFT);
882 rxconf = rxconf | (1<<ACCEPT_NICMAC_FRAME_SHIFT);
885 if(priv->ieee80211->iw_mode == IW_MODE_MONITOR){
886 rxconf = rxconf | (1<<ACCEPT_CTL_FRAME_SHIFT);
887 rxconf = rxconf | (1<<ACCEPT_ICVERR_FRAME_SHIFT);
888 rxconf = rxconf | (1<<ACCEPT_PWR_FRAME_SHIFT);
891 if( priv->crcmon == 1 && priv->ieee80211->iw_mode == IW_MODE_MONITOR)
892 rxconf = rxconf | (1<<ACCEPT_CRCERR_FRAME_SHIFT);
894 rxconf = rxconf & ~RX_FIFO_THRESHOLD_MASK;
895 rxconf = rxconf | (RX_FIFO_THRESHOLD_NONE << RX_FIFO_THRESHOLD_SHIFT);
897 rxconf = rxconf | (1<<RX_AUTORESETPHY_SHIFT);
898 rxconf = rxconf &~ MAX_RX_DMA_MASK;
899 rxconf = rxconf | (MAX_RX_DMA_2048<<MAX_RX_DMA_SHIFT);
901 rxconf = rxconf | RCR_ONLYERLPKT;
903 rxconf = rxconf &~ RCR_CS_MASK;
905 write_nic_dword(dev, RX_CONF, rxconf);
909 cmd=read_nic_byte(dev,CMD);
910 write_nic_byte(dev,CMD,cmd | (1<<CMD_RX_ENABLE_SHIFT));
913 void set_nic_txring(struct net_device *dev)
915 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
917 write_nic_dword(dev, TX_MANAGEPRIORITY_RING_ADDR, priv->txmapringdma);
918 write_nic_dword(dev, TX_BKPRIORITY_RING_ADDR, priv->txbkpringdma);
919 write_nic_dword(dev, TX_BEPRIORITY_RING_ADDR, priv->txbepringdma);
920 write_nic_dword(dev, TX_VIPRIORITY_RING_ADDR, priv->txvipringdma);
921 write_nic_dword(dev, TX_VOPRIORITY_RING_ADDR, priv->txvopringdma);
922 write_nic_dword(dev, TX_HIGHPRIORITY_RING_ADDR, priv->txhpringdma);
923 write_nic_dword(dev, TX_BEACON_RING_ADDR, priv->txbeaconringdma);
926 void rtl8180_conttx_enable(struct net_device *dev)
930 txconf = read_nic_dword(dev,TX_CONF);
931 txconf = txconf &~ TX_LOOPBACK_MASK;
932 txconf = txconf | (TX_LOOPBACK_CONTINUE <<TX_LOOPBACK_SHIFT);
933 write_nic_dword(dev,TX_CONF,txconf);
936 void rtl8180_conttx_disable(struct net_device *dev)
940 txconf = read_nic_dword(dev,TX_CONF);
941 txconf = txconf &~ TX_LOOPBACK_MASK;
942 txconf = txconf | (TX_LOOPBACK_NONE <<TX_LOOPBACK_SHIFT);
943 write_nic_dword(dev,TX_CONF,txconf);
946 void rtl8180_tx_enable(struct net_device *dev)
952 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
954 txconf = read_nic_dword(dev, TX_CONF);
956 byte = read_nic_byte(dev, CW_CONF);
957 byte &= ~(1<<CW_CONF_PERPACKET_CW_SHIFT);
958 byte &= ~(1<<CW_CONF_PERPACKET_RETRY_SHIFT);
959 write_nic_byte(dev, CW_CONF, byte);
961 tx_agc_ctl = read_nic_byte(dev, TX_AGC_CTL);
962 tx_agc_ctl &= ~(1<<TX_AGC_CTL_PERPACKET_GAIN_SHIFT);
963 tx_agc_ctl &= ~(1<<TX_AGC_CTL_PERPACKET_ANTSEL_SHIFT);
964 tx_agc_ctl |= (1<<TX_AGC_CTL_FEEDBACK_ANT);
965 write_nic_byte(dev, TX_AGC_CTL, tx_agc_ctl);
966 write_nic_byte(dev, 0xec, 0x3f); /* Disable early TX */
968 txconf = txconf & ~(1<<TCR_PROBE_NOTIMESTAMP_SHIFT);
970 txconf = txconf &~ TX_LOOPBACK_MASK;
971 txconf = txconf | (TX_LOOPBACK_NONE <<TX_LOOPBACK_SHIFT);
972 txconf = txconf &~ TCR_DPRETRY_MASK;
973 txconf = txconf &~ TCR_RTSRETRY_MASK;
974 txconf = txconf | (priv->retry_data<<TX_DPRETRY_SHIFT);
975 txconf = txconf | (priv->retry_rts<<TX_RTSRETRY_SHIFT);
976 txconf = txconf &~ (1<<TX_NOCRC_SHIFT);
978 if (priv->hw_plcp_len)
979 txconf = txconf & ~TCR_PLCP_LEN;
981 txconf = txconf | TCR_PLCP_LEN;
983 txconf = txconf &~ TCR_MXDMA_MASK;
984 txconf = txconf | (TCR_MXDMA_2048<<TCR_MXDMA_SHIFT);
985 txconf = txconf | TCR_CWMIN;
986 txconf = txconf | TCR_DISCW;
988 txconf = txconf | (1 << TX_NOICV_SHIFT);
990 write_nic_dword(dev,TX_CONF,txconf);
994 cmd=read_nic_byte(dev,CMD);
995 write_nic_byte(dev,CMD,cmd | (1<<CMD_TX_ENABLE_SHIFT));
997 write_nic_dword(dev,TX_CONF,txconf);
1000 void rtl8180_beacon_tx_enable(struct net_device *dev)
1002 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1004 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
1005 priv->dma_poll_stop_mask &= ~(TPPOLLSTOP_BQ);
1006 write_nic_byte(dev,TPPollStop, priv->dma_poll_mask);
1007 rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
1010 void rtl8180_beacon_tx_disable(struct net_device *dev)
1012 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1014 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
1015 priv->dma_poll_stop_mask |= TPPOLLSTOP_BQ;
1016 write_nic_byte(dev,TPPollStop, priv->dma_poll_stop_mask);
1017 rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
1021 void rtl8180_rtx_disable(struct net_device *dev)
1024 struct r8180_priv *priv = ieee80211_priv(dev);
1026 cmd=read_nic_byte(dev,CMD);
1027 write_nic_byte(dev, CMD, cmd &~ \
1028 ((1<<CMD_RX_ENABLE_SHIFT)|(1<<CMD_TX_ENABLE_SHIFT)));
1029 force_pci_posting(dev);
1032 if(!priv->rx_skb_complete)
1033 dev_kfree_skb_any(priv->rx_skb);
1036 short alloc_tx_desc_ring(struct net_device *dev, int bufsize, int count,
1042 dma_addr_t dma_desc, dma_tmp;
1043 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1044 struct pci_dev *pdev = priv->pdev;
1047 if((bufsize & 0xfff) != bufsize) {
1048 DMESGE ("TX buffer allocation too large");
1051 desc = (u32*)pci_alloc_consistent(pdev,
1052 sizeof(u32)*8*count+256, &dma_desc);
1056 if (dma_desc & 0xff)
1058 * descriptor's buffer must be 256 byte aligned
1059 * we shouldn't be here, since we set DMA mask !
1061 WARN(1, "DMA buffer is not aligned\n");
1065 for (i = 0; i < count; i++) {
1066 buf = (void *)pci_alloc_consistent(pdev, bufsize, &dma_tmp);
1071 case TX_MANAGEPRIORITY_RING_ADDR:
1072 if(-1 == buffer_add(&(priv->txmapbufs),buf,dma_tmp,NULL)){
1073 DMESGE("Unable to allocate mem for buffer NP");
1077 case TX_BKPRIORITY_RING_ADDR:
1078 if(-1 == buffer_add(&(priv->txbkpbufs),buf,dma_tmp,NULL)){
1079 DMESGE("Unable to allocate mem for buffer LP");
1083 case TX_BEPRIORITY_RING_ADDR:
1084 if(-1 == buffer_add(&(priv->txbepbufs),buf,dma_tmp,NULL)){
1085 DMESGE("Unable to allocate mem for buffer NP");
1089 case TX_VIPRIORITY_RING_ADDR:
1090 if(-1 == buffer_add(&(priv->txvipbufs),buf,dma_tmp,NULL)){
1091 DMESGE("Unable to allocate mem for buffer LP");
1095 case TX_VOPRIORITY_RING_ADDR:
1096 if(-1 == buffer_add(&(priv->txvopbufs),buf,dma_tmp,NULL)){
1097 DMESGE("Unable to allocate mem for buffer NP");
1101 case TX_HIGHPRIORITY_RING_ADDR:
1102 if(-1 == buffer_add(&(priv->txhpbufs),buf,dma_tmp,NULL)){
1103 DMESGE("Unable to allocate mem for buffer HP");
1107 case TX_BEACON_RING_ADDR:
1108 if(-1 == buffer_add(&(priv->txbeaconbufs),buf,dma_tmp,NULL)){
1109 DMESGE("Unable to allocate mem for buffer BP");
1114 *tmp = *tmp &~ (1<<31); // descriptor empty, owned by the drv
1115 *(tmp+2) = (u32)dma_tmp;
1119 *(tmp+4) = (u32)dma_desc+((i+1)*8*4);
1121 *(tmp+4) = (u32)dma_desc;
1127 case TX_MANAGEPRIORITY_RING_ADDR:
1128 priv->txmapringdma=dma_desc;
1129 priv->txmapring=desc;
1131 case TX_BKPRIORITY_RING_ADDR:
1132 priv->txbkpringdma=dma_desc;
1133 priv->txbkpring=desc;
1135 case TX_BEPRIORITY_RING_ADDR:
1136 priv->txbepringdma=dma_desc;
1137 priv->txbepring=desc;
1139 case TX_VIPRIORITY_RING_ADDR:
1140 priv->txvipringdma=dma_desc;
1141 priv->txvipring=desc;
1143 case TX_VOPRIORITY_RING_ADDR:
1144 priv->txvopringdma=dma_desc;
1145 priv->txvopring=desc;
1147 case TX_HIGHPRIORITY_RING_ADDR:
1148 priv->txhpringdma=dma_desc;
1149 priv->txhpring=desc;
1151 case TX_BEACON_RING_ADDR:
1152 priv->txbeaconringdma=dma_desc;
1153 priv->txbeaconring=desc;
1161 void free_tx_desc_rings(struct net_device *dev)
1163 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1164 struct pci_dev *pdev=priv->pdev;
1165 int count = priv->txringcount;
1167 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1168 priv->txmapring, priv->txmapringdma);
1169 buffer_free(dev,&(priv->txmapbufs),priv->txbuffsize,1);
1171 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1172 priv->txbkpring, priv->txbkpringdma);
1173 buffer_free(dev,&(priv->txbkpbufs),priv->txbuffsize,1);
1175 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1176 priv->txbepring, priv->txbepringdma);
1177 buffer_free(dev,&(priv->txbepbufs),priv->txbuffsize,1);
1179 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1180 priv->txvipring, priv->txvipringdma);
1181 buffer_free(dev,&(priv->txvipbufs),priv->txbuffsize,1);
1183 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1184 priv->txvopring, priv->txvopringdma);
1185 buffer_free(dev,&(priv->txvopbufs),priv->txbuffsize,1);
1187 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1188 priv->txhpring, priv->txhpringdma);
1189 buffer_free(dev,&(priv->txhpbufs),priv->txbuffsize,1);
1191 count = priv->txbeaconcount;
1192 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1193 priv->txbeaconring, priv->txbeaconringdma);
1194 buffer_free(dev,&(priv->txbeaconbufs),priv->txbuffsize,1);
1197 void free_rx_desc_ring(struct net_device *dev)
1199 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1200 struct pci_dev *pdev = priv->pdev;
1201 int count = priv->rxringcount;
1203 pci_free_consistent(pdev, sizeof(u32)*8*count+256,
1204 priv->rxring, priv->rxringdma);
1206 buffer_free(dev,&(priv->rxbuffer),priv->rxbuffersize,0);
1209 short alloc_rx_desc_ring(struct net_device *dev, u16 bufsize, int count)
1214 dma_addr_t dma_desc,dma_tmp;
1215 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1216 struct pci_dev *pdev=priv->pdev;
1220 rx_desc_size = 8; // 4*8 = 32 bytes
1222 if((bufsize & 0xfff) != bufsize){
1223 DMESGE ("RX buffer allocation too large");
1227 desc = (u32*)pci_alloc_consistent(pdev,sizeof(u32)*rx_desc_size*count+256,
1230 if (dma_desc & 0xff)
1232 * descriptor's buffer must be 256 byte aligned
1233 * should never happen since we specify the DMA mask
1235 WARN(1, "DMA buffer is not aligned\n");
1238 priv->rxringdma=dma_desc;
1241 for (i = 0; i < count; i++) {
1242 if ((buf= kmalloc(bufsize * sizeof(u8),GFP_ATOMIC)) == NULL){
1243 DMESGE("Failed to kmalloc RX buffer");
1247 dma_tmp = pci_map_single(pdev,buf,bufsize * sizeof(u8),
1248 PCI_DMA_FROMDEVICE);
1250 if(-1 == buffer_add(&(priv->rxbuffer), buf,dma_tmp,
1251 &(priv->rxbufferhead))){
1252 DMESGE("Unable to allocate mem RX buf");
1255 *tmp = 0; //zero pads the header of the descriptor
1256 *tmp = *tmp |( bufsize&0xfff);
1257 *(tmp+2) = (u32)dma_tmp;
1258 *tmp = *tmp |(1<<31); // descriptor void, owned by the NIC
1260 tmp=tmp+rx_desc_size;
1263 *(tmp-rx_desc_size) = *(tmp-rx_desc_size) | (1<<30); // this is the last descriptor
1269 void set_nic_rxring(struct net_device *dev)
1272 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1274 pgreg=read_nic_byte(dev, PGSELECT);
1275 write_nic_byte(dev, PGSELECT, pgreg &~ (1<<PGSELECT_PG_SHIFT));
1277 write_nic_dword(dev, RXRING_ADDR,priv->rxringdma);
1280 void rtl8180_reset(struct net_device *dev)
1284 rtl8180_irq_disable(dev);
1286 cr=read_nic_byte(dev,CMD);
1288 cr = cr | (1<<CMD_RST_SHIFT);
1289 write_nic_byte(dev,CMD,cr);
1291 force_pci_posting(dev);
1295 if(read_nic_byte(dev,CMD) & (1<<CMD_RST_SHIFT))
1296 DMESGW("Card reset timeout!");
1298 DMESG("Card successfully reset");
1300 rtl8180_set_mode(dev,EPROM_CMD_LOAD);
1301 force_pci_posting(dev);
1305 inline u16 ieeerate2rtlrate(int rate)
1337 static u16 rtl_rate[] = {10,20,55,110,60,90,120,180,240,360,480,540,720};
1339 inline u16 rtl8180_rate2rate(short rate)
1343 return rtl_rate[rate];
1346 inline u8 rtl8180_IsWirelessBMode(u16 rate)
1348 if( ((rate <= 110) && (rate != 60) && (rate != 90)) || (rate == 220) )
1354 u16 N_DBPSOfRate(u16 DataRate);
1356 u16 ComputeTxTime(u16 FrameLength, u16 DataRate, u8 bManagementFrame,
1363 if (rtl8180_IsWirelessBMode(DataRate)) {
1364 if (bManagementFrame || !bShortPreamble || DataRate == 10)
1366 FrameTime = (u16)(144+48+(FrameLength*8/(DataRate/10)));
1368 /* short preamble */
1369 FrameTime = (u16)(72+24+(FrameLength*8/(DataRate/10)));
1371 if ((FrameLength*8 % (DataRate/10)) != 0) /* get the ceilling */
1373 } else { /* 802.11g DSSS-OFDM PLCP length field calculation. */
1374 N_DBPS = N_DBPSOfRate(DataRate);
1375 Ceiling = (16 + 8*FrameLength + 6) / N_DBPS
1376 + (((16 + 8*FrameLength + 6) % N_DBPS) ? 1 : 0);
1377 FrameTime = (u16)(16 + 4 + 4*Ceiling + 6);
1382 u16 N_DBPSOfRate(u16 DataRate)
1420 // For Netgear case, they want good-looking singal strength.
1422 long NetgearSignalStrengthTranslate(long LastSS, long CurrSS)
1426 // Step 1. Scale mapping.
1427 if (CurrSS >= 71 && CurrSS <= 100)
1428 RetSS = 90 + ((CurrSS - 70) / 3);
1429 else if (CurrSS >= 41 && CurrSS <= 70)
1430 RetSS = 78 + ((CurrSS - 40) / 3);
1431 else if (CurrSS >= 31 && CurrSS <= 40)
1432 RetSS = 66 + (CurrSS - 30);
1433 else if (CurrSS >= 21 && CurrSS <= 30)
1434 RetSS = 54 + (CurrSS - 20);
1435 else if (CurrSS >= 5 && CurrSS <= 20)
1436 RetSS = 42 + (((CurrSS - 5) * 2) / 3);
1437 else if (CurrSS == 4)
1439 else if (CurrSS == 3)
1441 else if (CurrSS == 2)
1443 else if (CurrSS == 1)
1448 // Step 2. Smoothing.
1450 RetSS = ((LastSS * 5) + (RetSS)+ 5) / 6;
1457 // Translate 0-100 signal strength index into dBm.
1459 long TranslateToDbm8185(u8 SignalStrengthIndex)
1463 // Translate to dBm (x=0.5y-95).
1464 SignalPower = (long)((SignalStrengthIndex + 1) >> 1);
1472 // Perform signal smoothing for dynamic mechanism.
1473 // This is different with PerformSignalSmoothing8185 in smoothing fomula.
1474 // No dramatic adjustion is apply because dynamic mechanism need some degree
1475 // of correctness. Ported from 8187B.
1477 void PerformUndecoratedSignalSmoothing8185(struct r8180_priv *priv,
1480 // Determin the current packet is CCK rate.
1481 priv->bCurCCKPkt = bCckRate;
1483 if (priv->UndecoratedSmoothedSS >= 0)
1484 priv->UndecoratedSmoothedSS = ( (priv->UndecoratedSmoothedSS * 5) + (priv->SignalStrength * 10) ) / 6;
1486 priv->UndecoratedSmoothedSS = priv->SignalStrength * 10;
1488 priv->UndercorateSmoothedRxPower = ( (priv->UndercorateSmoothedRxPower * 50) + (priv->RxPower* 11)) / 60;
1491 priv->CurCCKRSSI = priv->RSSI;
1493 priv->CurCCKRSSI = 0;
1497 /* This is rough RX isr handling routine*/
1498 void rtl8180_rx(struct net_device *dev)
1500 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1501 struct sk_buff *tmp_skb;
1505 unsigned char quality, signal;
1514 u8 LNA_gain[4]={02, 17, 29, 39};
1516 struct ieee80211_hdr_4addr *hdr;
1518 u8 bHwError = 0,bCRC = 0,bICV = 0;
1519 bool bCckRate = false;
1521 long SignalStrengthIndex = 0;
1522 struct ieee80211_rx_stats stats = {
1526 .freq = IEEE80211_24GHZ_BAND,
1529 stats.nic_type = NIC_8185B;
1532 if ((*(priv->rxringtail)) & (1<<31)) {
1533 /* we have got an RX int, but the descriptor
1534 * we are pointing is empty*/
1536 priv->stats.rxnodata++;
1537 priv->ieee80211->stats.rx_errors++;
1540 tmp = priv->rxringtail;
1542 if(tmp == priv->rxring)
1543 tmp = priv->rxring + (priv->rxringcount - 1)*rx_desc_size;
1545 tmp -= rx_desc_size;
1547 if(! (*tmp & (1<<31)))
1549 }while(tmp != priv->rxring);
1551 if(tmp2) priv->rxringtail = tmp2;
1554 /* while there are filled descriptors */
1555 while(!(*(priv->rxringtail) & (1<<31))){
1556 if(*(priv->rxringtail) & (1<<26))
1557 DMESGW("RX buffer overflow");
1558 if(*(priv->rxringtail) & (1<<12))
1559 priv->stats.rxicverr++;
1561 if(*(priv->rxringtail) & (1<<27)){
1562 priv->stats.rxdmafail++;
1563 //DMESG("EE: RX DMA FAILED at buffer pointed by descriptor %x",(u32)priv->rxringtail);
1567 pci_dma_sync_single_for_cpu(priv->pdev,
1568 priv->rxbuffer->dma,
1569 priv->rxbuffersize * \
1571 PCI_DMA_FROMDEVICE);
1573 first = *(priv->rxringtail) & (1<<29) ? 1:0;
1574 if(first) priv->rx_prevlen=0;
1576 last = *(priv->rxringtail) & (1<<28) ? 1:0;
1578 lastlen=((*priv->rxringtail) &0xfff);
1580 /* if the last descriptor (that should
1581 * tell us the total packet len) tell
1582 * us something less than the descriptors
1583 * len we had until now, then there is some
1585 * workaround to prevent kernel panic
1587 if(lastlen < priv->rx_prevlen)
1590 len=lastlen-priv->rx_prevlen;
1592 if(*(priv->rxringtail) & (1<<13)) {
1593 if ((*(priv->rxringtail) & 0xfff) <500)
1594 priv->stats.rxcrcerrmin++;
1595 else if ((*(priv->rxringtail) & 0x0fff) >1000)
1596 priv->stats.rxcrcerrmax++;
1598 priv->stats.rxcrcerrmid++;
1603 len = priv->rxbuffersize;
1607 padding = ((*(priv->rxringtail+3))&(0x04000000))>>26;
1609 padding = ((*(priv->rxringtail+3))&(0x04000000))>>26;
1617 priv->rx_prevlen+=len;
1619 if(priv->rx_prevlen > MAX_FRAG_THRESHOLD + 100){
1620 /* HW is probably passing several buggy frames
1621 * without FD or LD flag set.
1622 * Throw this garbage away to prevent skb
1625 if(!priv->rx_skb_complete)
1626 dev_kfree_skb_any(priv->rx_skb);
1627 priv->rx_skb_complete = 1;
1630 signal=(unsigned char)(((*(priv->rxringtail+3))& (0x00ff0000))>>16);
1631 signal = (signal & 0xfe) >> 1;
1633 quality=(unsigned char)((*(priv->rxringtail+3)) & (0xff));
1635 stats.mac_time[0] = *(priv->rxringtail+1);
1636 stats.mac_time[1] = *(priv->rxringtail+2);
1637 rxpower =((char)(((*(priv->rxringtail+4))& (0x00ff0000))>>16))/2 - 42;
1638 RSSI = ((u8)(((*(priv->rxringtail+3)) & (0x0000ff00))>> 8)) & (0x7f);
1640 rate=((*(priv->rxringtail)) &
1641 ((1<<23)|(1<<22)|(1<<21)|(1<<20)))>>20;
1643 stats.rate = rtl8180_rate2rate(rate);
1644 Antenna = (((*(priv->rxringtail +3))& (0x00008000)) == 0 )? 0:1 ;
1645 if(!rtl8180_IsWirelessBMode(stats.rate))
1648 RxAGC_dBm = rxpower+1; //bias
1652 RxAGC_dBm = signal;//bit 0 discard
1654 LNA = (u8) (RxAGC_dBm & 0x60 ) >> 5 ; //bit 6~ bit 5
1655 BB = (u8) (RxAGC_dBm & 0x1F); // bit 4 ~ bit 0
1657 RxAGC_dBm = -( LNA_gain[LNA] + (BB *2) ); //Pin_11b=-(LNA_gain+BB_gain) (dBm)
1659 RxAGC_dBm +=4; //bias
1662 if(RxAGC_dBm & 0x80) //absolute value
1663 RXAGC= ~(RxAGC_dBm)+1;
1664 bCckRate = rtl8180_IsWirelessBMode(stats.rate);
1665 // Translate RXAGC into 1-100.
1666 if(!rtl8180_IsWirelessBMode(stats.rate))
1672 RXAGC=(90-RXAGC)*100/65;
1680 RXAGC=(95-RXAGC)*100/65;
1682 priv->SignalStrength = (u8)RXAGC;
1683 priv->RecvSignalPower = RxAGC_dBm;
1684 priv->RxPower = rxpower;
1686 /* SQ translation formula is provided by SD3 DZ. 2006.06.27 */
1688 quality = 1;//0; //0 will cause epc to show signal zero , walk aroud now;
1689 else if(quality < 27)
1692 quality = 127 - quality;
1693 priv->SignalQuality = quality;
1695 stats.signal = (u8)quality;//priv->wstats.qual.level = priv->SignalStrength;
1696 stats.signalstrength = RXAGC;
1697 if(stats.signalstrength > 100)
1698 stats.signalstrength = 100;
1699 stats.signalstrength = (stats.signalstrength * 70)/100 + 30;
1700 // printk("==========================>rx : RXAGC is %d,signalstrength is %d\n",RXAGC,stats.signalstrength);
1701 stats.rssi = priv->wstats.qual.qual = priv->SignalQuality;
1702 stats.noise = priv->wstats.qual.noise = 100 - priv ->wstats.qual.qual;
1703 bHwError = (((*(priv->rxringtail))& (0x00000fff)) == 4080)| (((*(priv->rxringtail))& (0x04000000)) != 0 )
1704 | (((*(priv->rxringtail))& (0x08000000)) != 0 )| (((~(*(priv->rxringtail)))& (0x10000000)) != 0 )| (((~(*(priv->rxringtail)))& (0x20000000)) != 0 );
1705 bCRC = ((*(priv->rxringtail)) & (0x00002000)) >> 13;
1706 bICV = ((*(priv->rxringtail)) & (0x00001000)) >> 12;
1707 hdr = (struct ieee80211_hdr_4addr *)priv->rxbuffer->buf;
1708 fc = le16_to_cpu(hdr->frame_ctl);
1709 type = WLAN_FC_GET_TYPE(fc);
1711 if((IEEE80211_FTYPE_CTL != type) &&
1712 (eqMacAddr(priv->ieee80211->current_network.bssid, (fc & IEEE80211_FCTL_TODS)? hdr->addr1 : (fc & IEEE80211_FCTL_FROMDS )? hdr->addr2 : hdr->addr3))
1713 && (!bHwError) && (!bCRC)&& (!bICV))
1715 /* Perform signal smoothing for dynamic
1716 * mechanism on demand. This is different
1717 * with PerformSignalSmoothing8185 in smoothing
1718 * fomula. No dramatic adjustion is apply
1719 * because dynamic mechanism need some degree
1720 * of correctness. */
1721 PerformUndecoratedSignalSmoothing8185(priv,bCckRate);
1723 // For good-looking singal strength.
1725 SignalStrengthIndex = NetgearSignalStrengthTranslate(
1726 priv->LastSignalStrengthInPercent,
1727 priv->SignalStrength);
1729 priv->LastSignalStrengthInPercent = SignalStrengthIndex;
1730 priv->Stats_SignalStrength = TranslateToDbm8185((u8)SignalStrengthIndex);
1732 // We need more correct power of received packets and the "SignalStrength" of RxStats is beautified,
1733 // so we record the correct power here.
1735 priv->Stats_SignalQuality =(long) (priv->Stats_SignalQuality * 5 + (long)priv->SignalQuality + 5) / 6;
1736 priv->Stats_RecvSignalPower = (long)(priv->Stats_RecvSignalPower * 5 + priv->RecvSignalPower -1) / 6;
1738 // Figure out which antenna that received the lasted packet.
1739 priv->LastRxPktAntenna = Antenna ? 1 : 0; // 0: aux, 1: main.
1740 SwAntennaDiversityRxOk8185(dev, priv->SignalStrength);
1744 if(!priv->rx_skb_complete){
1745 /* seems that HW sometimes fails to reiceve and
1746 doesn't provide the last descriptor */
1747 dev_kfree_skb_any(priv->rx_skb);
1748 priv->stats.rxnolast++;
1750 /* support for prism header has been originally added by Christian */
1751 if(priv->prism_hdr && priv->ieee80211->iw_mode == IW_MODE_MONITOR){
1754 priv->rx_skb = dev_alloc_skb(len+2);
1755 if( !priv->rx_skb) goto drop;
1758 priv->rx_skb_complete=0;
1759 priv->rx_skb->dev=dev;
1761 /* if we are here we should have already RXed
1763 * If we get here and the skb is not allocated then
1764 * we have just throw out garbage (skb not allocated)
1765 * and we are still rxing garbage....
1767 if(!priv->rx_skb_complete){
1769 tmp_skb= dev_alloc_skb(priv->rx_skb->len +len+2);
1771 if(!tmp_skb) goto drop;
1775 memcpy(skb_put(tmp_skb,priv->rx_skb->len),
1779 dev_kfree_skb_any(priv->rx_skb);
1781 priv->rx_skb=tmp_skb;
1785 if(!priv->rx_skb_complete) {
1787 memcpy(skb_put(priv->rx_skb,len),
1788 (((unsigned char *)priv->rxbuffer->buf) + 2),len);
1790 memcpy(skb_put(priv->rx_skb,len),
1791 priv->rxbuffer->buf,len);
1795 if(last && !priv->rx_skb_complete){
1796 if(priv->rx_skb->len > 4)
1797 skb_trim(priv->rx_skb,priv->rx_skb->len-4);
1798 if(!ieee80211_rtl_rx(priv->ieee80211,
1799 priv->rx_skb, &stats))
1800 dev_kfree_skb_any(priv->rx_skb);
1801 priv->rx_skb_complete=1;
1804 pci_dma_sync_single_for_device(priv->pdev,
1805 priv->rxbuffer->dma,
1806 priv->rxbuffersize * \
1808 PCI_DMA_FROMDEVICE);
1810 drop: // this is used when we have not enough mem
1811 /* restore the descriptor */
1812 *(priv->rxringtail+2)=priv->rxbuffer->dma;
1813 *(priv->rxringtail)=*(priv->rxringtail) &~ 0xfff;
1814 *(priv->rxringtail)=
1815 *(priv->rxringtail) | priv->rxbuffersize;
1817 *(priv->rxringtail)=
1818 *(priv->rxringtail) | (1<<31);
1820 priv->rxringtail+=rx_desc_size;
1821 if(priv->rxringtail >=
1822 (priv->rxring)+(priv->rxringcount )*rx_desc_size)
1823 priv->rxringtail=priv->rxring;
1825 priv->rxbuffer=(priv->rxbuffer->next);
1830 void rtl8180_dma_kick(struct net_device *dev, int priority)
1832 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1834 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
1835 write_nic_byte(dev, TX_DMA_POLLING,
1836 (1 << (priority + 1)) | priv->dma_poll_mask);
1837 rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
1839 force_pci_posting(dev);
1842 void rtl8180_data_hard_stop(struct net_device *dev)
1844 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1846 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
1847 priv->dma_poll_stop_mask |= TPPOLLSTOP_AC_VIQ;
1848 write_nic_byte(dev,TPPollStop, priv->dma_poll_stop_mask);
1849 rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
1852 void rtl8180_data_hard_resume(struct net_device *dev)
1854 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1856 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
1857 priv->dma_poll_stop_mask &= ~(TPPOLLSTOP_AC_VIQ);
1858 write_nic_byte(dev,TPPollStop, priv->dma_poll_stop_mask);
1859 rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
1862 /* this function TX data frames when the ieee80211 stack requires this.
1863 * It checks also if we need to stop the ieee tx queue, eventually do it
1865 void rtl8180_hard_data_xmit(struct sk_buff *skb,struct net_device *dev, int
1868 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1870 struct ieee80211_hdr_3addr *h = (struct ieee80211_hdr_3addr *) skb->data;
1871 short morefrag = (h->frame_ctl) & IEEE80211_FCTL_MOREFRAGS;
1872 unsigned long flags;
1875 mode = priv->ieee80211->iw_mode;
1877 rate = ieeerate2rtlrate(rate);
1879 * This function doesn't require lock because we make
1880 * sure it's called with the tx_lock already acquired.
1881 * this come from the kernel's hard_xmit callback (through
1882 * the ieee stack, or from the try_wake_queue (again through
1885 priority = AC2Q(skb->priority);
1886 spin_lock_irqsave(&priv->tx_lock,flags);
1888 if(priv->ieee80211->bHwRadioOff)
1890 spin_unlock_irqrestore(&priv->tx_lock,flags);
1895 if (!check_nic_enought_desc(dev, priority)){
1896 DMESGW("Error: no descriptor left by previous TX (avail %d) ",
1897 get_curr_tx_free_desc(dev, priority));
1898 ieee80211_rtl_stop_queue(priv->ieee80211);
1900 rtl8180_tx(dev, skb->data, skb->len, priority, morefrag,0,rate);
1901 if (!check_nic_enought_desc(dev, priority))
1902 ieee80211_rtl_stop_queue(priv->ieee80211);
1904 spin_unlock_irqrestore(&priv->tx_lock,flags);
1907 /* This is a rough attempt to TX a frame
1908 * This is called by the ieee 80211 stack to TX management frames.
1909 * If the ring is full packet are dropped (for data frame the queue
1910 * is stopped before this can happen). For this reason it is better
1911 * if the descriptors are larger than the largest management frame
1912 * we intend to TX: i'm unsure what the HW does if it will not found
1913 * the last fragment of a frame because it has been dropped...
1914 * Since queues for Management and Data frames are different we
1915 * might use a different lock than tx_lock (for example mgmt_tx_lock)
1917 /* these function may loops if invoked with 0 descriptors or 0 len buffer*/
1918 int rtl8180_hard_start_xmit(struct sk_buff *skb,struct net_device *dev)
1920 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1921 unsigned long flags;
1924 priority = MANAGE_PRIORITY;
1926 spin_lock_irqsave(&priv->tx_lock,flags);
1928 if (priv->ieee80211->bHwRadioOff) {
1929 spin_unlock_irqrestore(&priv->tx_lock,flags);
1930 dev_kfree_skb_any(skb);
1931 return NETDEV_TX_OK;
1934 rtl8180_tx(dev, skb->data, skb->len, priority,
1935 0, 0,ieeerate2rtlrate(priv->ieee80211->basic_rate));
1937 priv->ieee80211->stats.tx_bytes+=skb->len;
1938 priv->ieee80211->stats.tx_packets++;
1939 spin_unlock_irqrestore(&priv->tx_lock,flags);
1941 dev_kfree_skb_any(skb);
1942 return NETDEV_TX_OK;
1945 // longpre 144+48 shortpre 72+24
1946 u16 rtl8180_len2duration(u32 len, short rate,short* ext)
1955 duration = ((len+4)<<4) /0x2;
1956 drift = ((len+4)<<4) % 0x2;
1957 if(drift ==0 ) break;
1962 duration = ((len+4)<<4) /0x4;
1963 drift = ((len+4)<<4) % 0x4;
1964 if(drift ==0 ) break;
1969 duration = ((len+4)<<4) /0xb;
1970 drift = ((len+4)<<4) % 0xb;
1978 duration = ((len+4)<<4) /0x16;
1979 drift = ((len+4)<<4) % 0x16;
1992 void rtl8180_prepare_beacon(struct net_device *dev)
1994 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1995 struct sk_buff *skb;
1997 u16 word = read_nic_word(dev, BcnItv);
1998 word &= ~BcnItv_BcnItv; // clear Bcn_Itv
1999 word |= cpu_to_le16(priv->ieee80211->current_network.beacon_interval);//0x64;
2000 write_nic_word(dev, BcnItv, word);
2002 skb = ieee80211_get_beacon(priv->ieee80211);
2004 rtl8180_tx(dev,skb->data,skb->len,BEACON_PRIORITY,
2005 0,0,ieeerate2rtlrate(priv->ieee80211->basic_rate));
2006 dev_kfree_skb_any(skb);
2010 /* This function do the real dirty work: it enqueues a TX command
2011 * descriptor in the ring buffer, copyes the frame in a TX buffer
2012 * and kicks the NIC to ensure it does the DMA transfer.
2014 short rtl8180_tx(struct net_device *dev, u8* txbuf, int len, int priority,
2015 short morefrag, short descfrag, int rate)
2017 struct r8180_priv *priv = ieee80211_priv(dev);
2018 u32 *tail,*temp_tail;
2027 struct buffer* buflist;
2028 struct ieee80211_hdr_3addr *frag_hdr = (struct ieee80211_hdr_3addr *)txbuf;
2030 u8 bUseShortPreamble = 0;
2035 u16 ThisFrameTime = 0;
2036 u16 TxDescDuration = 0;
2037 u8 ownbit_flag = false;
2040 case MANAGE_PRIORITY:
2041 tail=priv->txmapringtail;
2042 begin=priv->txmapring;
2043 buflist = priv->txmapbufstail;
2044 count = priv->txringcount;
2047 tail=priv->txbkpringtail;
2048 begin=priv->txbkpring;
2049 buflist = priv->txbkpbufstail;
2050 count = priv->txringcount;
2053 tail=priv->txbepringtail;
2054 begin=priv->txbepring;
2055 buflist = priv->txbepbufstail;
2056 count = priv->txringcount;
2059 tail=priv->txvipringtail;
2060 begin=priv->txvipring;
2061 buflist = priv->txvipbufstail;
2062 count = priv->txringcount;
2065 tail=priv->txvopringtail;
2066 begin=priv->txvopring;
2067 buflist = priv->txvopbufstail;
2068 count = priv->txringcount;
2071 tail=priv->txhpringtail;
2072 begin=priv->txhpring;
2073 buflist = priv->txhpbufstail;
2074 count = priv->txringcount;
2076 case BEACON_PRIORITY:
2077 tail=priv->txbeaconringtail;
2078 begin=priv->txbeaconring;
2079 buflist = priv->txbeaconbufstail;
2080 count = priv->txbeaconcount;
2087 memcpy(&dest, frag_hdr->addr1, ETH_ALEN);
2088 if (is_multicast_ether_addr(dest) ||
2089 is_broadcast_ether_addr(dest))
2096 ThisFrameTime = ComputeTxTime(len + sCrcLng, rtl8180_rate2rate(rate), 0, bUseShortPreamble);
2097 TxDescDuration = ThisFrameTime;
2098 } else {// Unicast packet
2101 //YJ,add,080828,for Keep alive
2102 priv->NumTxUnicast++;
2104 /* Figure out ACK rate according to BSS basic rate
2106 AckTime = ComputeTxTime(14, 10,0, 0); // AckCTSLng = 14 use 1M bps send
2108 if ( ((len + sCrcLng) > priv->rts) && priv->rts )
2110 u16 RtsTime, CtsTime;
2115 // Rate and time required for RTS.
2116 RtsTime = ComputeTxTime( sAckCtsLng/8,priv->ieee80211->basic_rate, 0, 0);
2117 // Rate and time required for CTS.
2118 CtsTime = ComputeTxTime(14, 10,0, 0); // AckCTSLng = 14 use 1M bps send
2120 // Figure out time required to transmit this frame.
2121 ThisFrameTime = ComputeTxTime(len + sCrcLng,
2122 rtl8180_rate2rate(rate),
2126 // RTS-CTS-ThisFrame-ACK.
2127 RtsDur = CtsTime + ThisFrameTime + AckTime + 3*aSifsTime;
2129 TxDescDuration = RtsTime + RtsDur;
2131 else {// Normal case.
2136 ThisFrameTime = ComputeTxTime(len + sCrcLng, rtl8180_rate2rate(rate), 0, bUseShortPreamble);
2137 TxDescDuration = ThisFrameTime + aSifsTime + AckTime;
2140 if(!(frag_hdr->frame_ctl & IEEE80211_FCTL_MOREFRAGS)) { //no more fragment
2142 Duration = aSifsTime + AckTime;
2143 } else { // One or more fragments remained.
2145 NextFragTime = ComputeTxTime( len + sCrcLng, //pretend following packet length equal current packet
2146 rtl8180_rate2rate(rate),
2148 bUseShortPreamble );
2150 //ThisFrag-ACk-NextFrag-ACK.
2151 Duration = NextFragTime + 3*aSifsTime + 2*AckTime;
2154 } // End of Unicast packet
2156 frag_hdr->duration_id = Duration;
2158 buflen=priv->txbuffsize;
2165 DMESGE("TX buffer error, cannot TX frames. pri %d.", priority);
2170 if ((*tail & (1 << 31)) && (priority != BEACON_PRIORITY)) {
2171 DMESGW("No more TX desc, returning %x of %x",
2173 priv->stats.txrdu++;
2177 *tail= 0; // zeroes header
2184 /*FIXME: this should be triggered by HW encryption parameters.*/
2185 *tail |= (1<<15); /* no encrypt */
2187 if(remain==len && !descfrag) {
2188 ownbit_flag = false;
2189 *tail = *tail| (1<<29) ; //fist segment of the packet
2190 *tail = *tail |(len);
2195 for(i=0;i<buflen&& remain >0;i++,remain--){
2196 ((u8*)buf)[i]=txbuf[i]; //copy data into descriptor pointed DMAble buffer
2197 if(remain == 4 && i+4 >= buflen) break;
2198 /* ensure the last desc has at least 4 bytes payload */
2202 *(tail+3)=*(tail+3) &~ 0xfff;
2203 *(tail+3)=*(tail+3) | i; // buffer lenght
2204 // Use short preamble or not
2205 if (priv->ieee80211->current_network.capability&WLAN_CAPABILITY_SHORT_PREAMBLE)
2206 if (priv->plcp_preamble_mode==1 && rate!=0) // short mode now, not long!
2207 ;// *tail |= (1<<16); // enable short preamble mode.
2213 if(bRTSEnable) //rts enable
2215 *tail |= ((ieeerate2rtlrate(priv->ieee80211->basic_rate))<<19);//RTS RATE
2216 *tail |= (1<<23);//rts enable
2217 *(tail+1) |=(RtsDur&0xffff);//RTS Duration
2219 *(tail+3) |= ((TxDescDuration&0xffff)<<16); //DURATION
2220 // *(tail+3) |= (0xe6<<16);
2221 *(tail+5) |= (11<<8);//(priv->retry_data<<8); //retry lim ;
2223 *tail = *tail | ((rate&0xf) << 24);
2225 /* hw_plcp_len is not used for rtl8180 chip */
2227 if (!priv->hw_plcp_len) {
2228 duration = rtl8180_len2duration(len, rate, &ext);
2229 *(tail+1) = *(tail+1) | ((duration & 0x7fff)<<16);
2230 if(ext) *(tail+1) = *(tail+1) |(1<<31); //plcp length extension
2233 if(morefrag) *tail = (*tail) | (1<<17); // more fragment
2234 if(!remain) *tail = (*tail) | (1<<28); // last segment of frame
2236 *(tail+5) = *(tail+5)|(2<<27);
2237 *(tail+7) = *(tail+7)|(1<<4);
2242 *tail = *tail | (1<<31); // descriptor ready to be txed
2245 if((tail - begin)/8 == count-1)
2250 buflist=buflist->next;
2255 case MANAGE_PRIORITY:
2256 priv->txmapringtail=tail;
2257 priv->txmapbufstail=buflist;
2260 priv->txbkpringtail=tail;
2261 priv->txbkpbufstail=buflist;
2264 priv->txbepringtail=tail;
2265 priv->txbepbufstail=buflist;
2268 priv->txvipringtail=tail;
2269 priv->txvipbufstail=buflist;
2272 priv->txvopringtail=tail;
2273 priv->txvopbufstail=buflist;
2276 priv->txhpringtail=tail;
2277 priv->txhpbufstail = buflist;
2279 case BEACON_PRIORITY:
2280 /* the HW seems to be happy with the 1st
2281 * descriptor filled and the 2nd empty...
2282 * So always update descriptor 1 and never
2288 *temp_tail = *temp_tail | (1<<31); // descriptor ready to be txed
2289 rtl8180_dma_kick(dev,priority);
2294 void rtl8180_irq_rx_tasklet(struct r8180_priv * priv);
2296 void rtl8180_link_change(struct net_device *dev)
2298 struct r8180_priv *priv = ieee80211_priv(dev);
2299 u16 beacon_interval;
2300 struct ieee80211_network *net = &priv->ieee80211->current_network;
2302 rtl8180_update_msr(dev);
2304 rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
2306 write_nic_dword(dev,BSSID,((u32*)net->bssid)[0]);
2307 write_nic_word(dev,BSSID+4,((u16*)net->bssid)[2]);
2309 beacon_interval = read_nic_dword(dev,BEACON_INTERVAL);
2310 beacon_interval &= ~ BEACON_INTERVAL_MASK;
2311 beacon_interval |= net->beacon_interval;
2312 write_nic_dword(dev, BEACON_INTERVAL, beacon_interval);
2314 rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
2316 rtl8180_set_chan(dev, priv->chan);
2319 void rtl8180_rq_tx_ack(struct net_device *dev){
2321 struct r8180_priv *priv = ieee80211_priv(dev);
2323 write_nic_byte(dev,CONFIG4,read_nic_byte(dev,CONFIG4)|CONFIG4_PWRMGT);
2324 priv->ack_tx_to_ieee = 1;
2327 short rtl8180_is_tx_queue_empty(struct net_device *dev){
2329 struct r8180_priv *priv = ieee80211_priv(dev);
2332 for (d = priv->txmapring;
2333 d < priv->txmapring + priv->txringcount;d+=8)
2334 if(*d & (1<<31)) return 0;
2336 for (d = priv->txbkpring;
2337 d < priv->txbkpring + priv->txringcount;d+=8)
2338 if(*d & (1<<31)) return 0;
2340 for (d = priv->txbepring;
2341 d < priv->txbepring + priv->txringcount;d+=8)
2342 if(*d & (1<<31)) return 0;
2344 for (d = priv->txvipring;
2345 d < priv->txvipring + priv->txringcount;d+=8)
2346 if(*d & (1<<31)) return 0;
2348 for (d = priv->txvopring;
2349 d < priv->txvopring + priv->txringcount;d+=8)
2350 if(*d & (1<<31)) return 0;
2352 for (d = priv->txhpring;
2353 d < priv->txhpring + priv->txringcount;d+=8)
2354 if(*d & (1<<31)) return 0;
2357 /* FIXME FIXME 5msecs is random */
2358 #define HW_WAKE_DELAY 5
2360 void rtl8180_hw_wakeup(struct net_device *dev)
2362 unsigned long flags;
2363 struct r8180_priv *priv = ieee80211_priv(dev);
2365 spin_lock_irqsave(&priv->ps_lock,flags);
2366 write_nic_byte(dev,CONFIG4,read_nic_byte(dev,CONFIG4)&~CONFIG4_PWRMGT);
2367 if (priv->rf_wakeup)
2368 priv->rf_wakeup(dev);
2369 spin_unlock_irqrestore(&priv->ps_lock,flags);
2372 void rtl8180_hw_sleep_down(struct net_device *dev)
2374 unsigned long flags;
2375 struct r8180_priv *priv = ieee80211_priv(dev);
2377 spin_lock_irqsave(&priv->ps_lock,flags);
2379 priv->rf_sleep(dev);
2380 spin_unlock_irqrestore(&priv->ps_lock,flags);
2383 void rtl8180_hw_sleep(struct net_device *dev, u32 th, u32 tl)
2385 struct r8180_priv *priv = ieee80211_priv(dev);
2387 unsigned long flags;
2389 spin_lock_irqsave(&priv->ps_lock,flags);
2391 /* Writing HW register with 0 equals to disable
2392 * the timer, that is not really what we want
2394 tl -= MSECS(4+16+7);
2396 /* If the interval in witch we are requested to sleep is too
2397 * short then give up and remain awake
2399 if(((tl>=rb)&& (tl-rb) <= MSECS(MIN_SLEEP_TIME))
2400 ||((rb>tl)&& (rb-tl) < MSECS(MIN_SLEEP_TIME))) {
2401 spin_unlock_irqrestore(&priv->ps_lock,flags);
2402 printk("too short to sleep\n");
2407 u32 tmp = (tl>rb)?(tl-rb):(rb-tl);
2409 priv->DozePeriodInPast2Sec += jiffies_to_msecs(tmp);
2411 queue_delayed_work(priv->ieee80211->wq, &priv->ieee80211->hw_wakeup_wq, tmp); //as tl may be less than rb
2413 /* if we suspect the TimerInt is gone beyond tl
2414 * while setting it, then give up
2417 if(((tl > rb) && ((tl-rb) > MSECS(MAX_SLEEP_TIME)))||
2418 ((tl < rb) && ((rb-tl) > MSECS(MAX_SLEEP_TIME)))) {
2419 spin_unlock_irqrestore(&priv->ps_lock,flags);
2423 queue_work(priv->ieee80211->wq, (void *)&priv->ieee80211->hw_sleep_wq);
2424 spin_unlock_irqrestore(&priv->ps_lock,flags);
2427 void rtl8180_wmm_param_update(struct work_struct * work)
2429 struct ieee80211_device * ieee = container_of(work, struct ieee80211_device,wmm_param_update_wq);
2430 struct net_device *dev = ieee->dev;
2431 u8 *ac_param = (u8 *)(ieee->current_network.wmm_param);
2432 u8 mode = ieee->current_network.mode;
2438 if(!ieee->current_network.QoS_Enable){
2439 //legacy ac_xx_param update
2440 AcParam.longData = 0;
2441 AcParam.f.AciAifsn.f.AIFSN = 2; // Follow 802.11 DIFS.
2442 AcParam.f.AciAifsn.f.ACM = 0;
2443 AcParam.f.Ecw.f.ECWmin = 3; // Follow 802.11 CWmin.
2444 AcParam.f.Ecw.f.ECWmax = 7; // Follow 802.11 CWmax.
2445 AcParam.f.TXOPLimit = 0;
2446 for(eACI = 0; eACI < AC_MAX; eACI++){
2447 AcParam.f.AciAifsn.f.ACI = (u8)eACI;
2451 pAcParam = (PAC_PARAM)(&AcParam);
2452 // Retrive paramters to udpate.
2453 u1bAIFS = pAcParam->f.AciAifsn.f.AIFSN *(((mode&IEEE_G) == IEEE_G)?9:20) + aSifsTime;
2454 u4bAcParam = ((((u32)(pAcParam->f.TXOPLimit))<<AC_PARAM_TXOP_LIMIT_OFFSET)|
2455 (((u32)(pAcParam->f.Ecw.f.ECWmax))<<AC_PARAM_ECW_MAX_OFFSET)|
2456 (((u32)(pAcParam->f.Ecw.f.ECWmin))<<AC_PARAM_ECW_MIN_OFFSET)|
2457 (((u32)u1bAIFS) << AC_PARAM_AIFS_OFFSET));
2460 write_nic_dword(dev, AC_BK_PARAM, u4bAcParam);
2463 write_nic_dword(dev, AC_BE_PARAM, u4bAcParam);
2466 write_nic_dword(dev, AC_VI_PARAM, u4bAcParam);
2469 write_nic_dword(dev, AC_VO_PARAM, u4bAcParam);
2472 printk(KERN_WARNING "SetHwReg8185():invalid ACI: %d!\n", eACI);
2480 for(i = 0; i < AC_MAX; i++){
2481 //AcParam.longData = 0;
2482 pAcParam = (AC_PARAM * )ac_param;
2488 // Retrive paramters to udpate.
2489 eACI = pAcParam->f.AciAifsn.f.ACI;
2490 //Mode G/A: slotTimeTimer = 9; Mode B: 20
2491 u1bAIFS = pAcParam->f.AciAifsn.f.AIFSN * (((mode&IEEE_G) == IEEE_G)?9:20) + aSifsTime;
2492 u4bAcParam = ( (((u32)(pAcParam->f.TXOPLimit)) << AC_PARAM_TXOP_LIMIT_OFFSET) |
2493 (((u32)(pAcParam->f.Ecw.f.ECWmax)) << AC_PARAM_ECW_MAX_OFFSET) |
2494 (((u32)(pAcParam->f.Ecw.f.ECWmin)) << AC_PARAM_ECW_MIN_OFFSET) |
2495 (((u32)u1bAIFS) << AC_PARAM_AIFS_OFFSET));
2499 write_nic_dword(dev, AC_BK_PARAM, u4bAcParam);
2502 write_nic_dword(dev, AC_BE_PARAM, u4bAcParam);
2505 write_nic_dword(dev, AC_VI_PARAM, u4bAcParam);
2508 write_nic_dword(dev, AC_VO_PARAM, u4bAcParam);
2511 printk(KERN_WARNING "SetHwReg8185(): invalid ACI: %d !\n", eACI);
2515 ac_param += (sizeof(AC_PARAM));
2519 void rtl8180_tx_irq_wq(struct work_struct *work);
2520 void rtl8180_restart_wq(struct work_struct *work);
2521 //void rtl8180_rq_tx_ack(struct work_struct *work);
2522 void rtl8180_watch_dog_wq(struct work_struct *work);
2523 void rtl8180_hw_wakeup_wq(struct work_struct *work);
2524 void rtl8180_hw_sleep_wq(struct work_struct *work);
2525 void rtl8180_sw_antenna_wq(struct work_struct *work);
2526 void rtl8180_watch_dog(struct net_device *dev);
2528 void watch_dog_adaptive(unsigned long data)
2530 struct r8180_priv* priv = ieee80211_priv((struct net_device *)data);
2533 DMESG("<----watch_dog_adaptive():driver is not up!\n");
2537 // Tx High Power Mechanism.
2538 if(CheckHighPower((struct net_device *)data))
2539 queue_work(priv->ieee80211->wq, (void *)&priv->ieee80211->tx_pw_wq);
2541 // Tx Power Tracking on 87SE.
2542 if (CheckTxPwrTracking((struct net_device *)data))
2543 TxPwrTracking87SE((struct net_device *)data);
2545 // Perform DIG immediately.
2546 if(CheckDig((struct net_device *)data) == true)
2547 queue_work(priv->ieee80211->wq, (void *)&priv->ieee80211->hw_dig_wq);
2548 rtl8180_watch_dog((struct net_device *)data);
2550 queue_work(priv->ieee80211->wq, (void *)&priv->ieee80211->GPIOChangeRFWorkItem);
2552 priv->watch_dog_timer.expires = jiffies + MSECS(IEEE80211_WATCH_DOG_TIME);
2553 add_timer(&priv->watch_dog_timer);
2556 static CHANNEL_LIST ChannelPlan[] = {
2557 {{1,2,3,4,5,6,7,8,9,10,11,36,40,44,48,52,56,60,64},19}, //FCC
2558 {{1,2,3,4,5,6,7,8,9,10,11},11}, //IC
2559 {{1,2,3,4,5,6,7,8,9,10,11,12,13,36,40,44,48,52,56,60,64},21}, //ETSI
2560 {{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.
2561 {{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.
2562 {{14,36,40,44,48,52,56,60,64},9}, //MKK
2563 {{1,2,3,4,5,6,7,8,9,10,11,12,13,14, 36,40,44,48,52,56,60,64},22},//MKK1
2564 {{1,2,3,4,5,6,7,8,9,10,11,12,13,36,40,44,48,52,56,60,64},21}, //Israel.
2565 {{1,2,3,4,5,6,7,8,9,10,11,12,13,34,38,42,46},17}, // For 11a , TELEC
2566 {{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
2567 {{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
2570 static void rtl8180_set_channel_map(u8 channel_plan, struct ieee80211_device *ieee)
2575 ieee->MinPassiveChnlNum=MAX_CHANNEL_NUMBER+1;
2576 ieee->IbssStartChnl=0;
2578 switch (channel_plan)
2580 case COUNTRY_CODE_FCC:
2581 case COUNTRY_CODE_IC:
2582 case COUNTRY_CODE_ETSI:
2583 case COUNTRY_CODE_SPAIN:
2584 case COUNTRY_CODE_FRANCE:
2585 case COUNTRY_CODE_MKK:
2586 case COUNTRY_CODE_MKK1:
2587 case COUNTRY_CODE_ISRAEL:
2588 case COUNTRY_CODE_TELEC:
2591 ieee->bGlobalDomain = false;
2592 if (ChannelPlan[channel_plan].Len != 0){
2593 // Clear old channel map
2594 memset(GET_DOT11D_INFO(ieee)->channel_map, 0, sizeof(GET_DOT11D_INFO(ieee)->channel_map));
2595 // Set new channel map
2596 for (i=0;i<ChannelPlan[channel_plan].Len;i++)
2598 if(ChannelPlan[channel_plan].Channel[i] <= 14)
2599 GET_DOT11D_INFO(ieee)->channel_map[ChannelPlan[channel_plan].Channel[i]] = 1;
2604 case COUNTRY_CODE_GLOBAL_DOMAIN:
2606 GET_DOT11D_INFO(ieee)->bEnabled = 0;
2608 ieee->bGlobalDomain = true;
2611 case COUNTRY_CODE_WORLD_WIDE_13_INDEX://lzm add 080826
2613 ieee->MinPassiveChnlNum=12;
2614 ieee->IbssStartChnl= 10;
2620 ieee->bGlobalDomain = false;
2621 memset(GET_DOT11D_INFO(ieee)->channel_map, 0, sizeof(GET_DOT11D_INFO(ieee)->channel_map));
2624 GET_DOT11D_INFO(ieee)->channel_map[i] = 1;
2631 void GPIOChangeRFWorkItemCallBack(struct work_struct *work);
2634 static void rtl8180_statistics_init(struct Stats *pstats)
2636 memset(pstats, 0, sizeof(struct Stats));
2639 static void rtl8180_link_detect_init(plink_detect_t plink_detect)
2641 memset(plink_detect, 0, sizeof(link_detect_t));
2642 plink_detect->SlotNum = DEFAULT_SLOT_NUM;
2646 static void rtl8187se_eeprom_register_read(struct eeprom_93cx6 *eeprom)
2648 struct net_device *dev = eeprom->data;
2649 u8 reg = read_nic_byte(dev, EPROM_CMD);
2651 eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
2652 eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
2653 eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
2654 eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
2657 static void rtl8187se_eeprom_register_write(struct eeprom_93cx6 *eeprom)
2659 struct net_device *dev = eeprom->data;
2662 if (eeprom->reg_data_in)
2663 reg |= RTL818X_EEPROM_CMD_WRITE;
2664 if (eeprom->reg_data_out)
2665 reg |= RTL818X_EEPROM_CMD_READ;
2666 if (eeprom->reg_data_clock)
2667 reg |= RTL818X_EEPROM_CMD_CK;
2668 if (eeprom->reg_chip_select)
2669 reg |= RTL818X_EEPROM_CMD_CS;
2671 write_nic_byte(dev, EPROM_CMD, reg);
2672 read_nic_byte(dev, EPROM_CMD);
2676 short rtl8180_init(struct net_device *dev)
2678 struct r8180_priv *priv = ieee80211_priv(dev);
2684 struct eeprom_93cx6 eeprom;
2688 eeprom.register_read = rtl8187se_eeprom_register_read;
2689 eeprom.register_write = rtl8187se_eeprom_register_write;
2690 eeprom.width = PCI_EEPROM_WIDTH_93C46;
2692 eeprom_93cx6_read(&eeprom, EEPROM_COUNTRY_CODE>>1, &eeprom_val);
2693 priv->channel_plan = eeprom_val & 0xFF;
2694 if(priv->channel_plan > COUNTRY_CODE_GLOBAL_DOMAIN){
2695 printk("rtl8180_init:Error channel plan! Set to default.\n");
2696 priv->channel_plan = 0;
2699 DMESG("Channel plan is %d\n",priv->channel_plan);
2700 rtl8180_set_channel_map(priv->channel_plan, priv->ieee80211);
2702 //FIXME: these constants are placed in a bad pleace.
2703 priv->txbuffsize = 2048;//1024;
2704 priv->txringcount = 32;//32;
2705 priv->rxbuffersize = 2048;//1024;
2706 priv->rxringcount = 64;//32;
2707 priv->txbeaconcount = 2;
2708 priv->rx_skb_complete = 1;
2710 priv->RegThreeWireMode = HW_THREE_WIRE_SI;
2712 priv->RFChangeInProgress = false;
2713 priv->SetRFPowerStateInProgress = false;
2714 priv->RFProgType = 0;
2715 priv->bInHctTest = false;
2717 priv->irq_enabled=0;
2719 rtl8180_statistics_init(&priv->stats);
2720 rtl8180_link_detect_init(&priv->link_detect);
2722 priv->ack_tx_to_ieee = 0;
2723 priv->ieee80211->current_network.beacon_interval = DEFAULT_BEACONINTERVAL;
2724 priv->ieee80211->iw_mode = IW_MODE_INFRA;
2725 priv->ieee80211->softmac_features = IEEE_SOFTMAC_SCAN |
2726 IEEE_SOFTMAC_ASSOCIATE | IEEE_SOFTMAC_PROBERQ |
2727 IEEE_SOFTMAC_PROBERS | IEEE_SOFTMAC_TX_QUEUE;
2728 priv->ieee80211->active_scan = 1;
2729 priv->ieee80211->rate = 110; //11 mbps
2730 priv->ieee80211->modulation = IEEE80211_CCK_MODULATION;
2731 priv->ieee80211->host_encrypt = 1;
2732 priv->ieee80211->host_decrypt = 1;
2733 priv->ieee80211->sta_wake_up = rtl8180_hw_wakeup;
2734 priv->ieee80211->ps_request_tx_ack = rtl8180_rq_tx_ack;
2735 priv->ieee80211->enter_sleep_state = rtl8180_hw_sleep;
2736 priv->ieee80211->ps_is_queue_empty = rtl8180_is_tx_queue_empty;
2738 priv->hw_wep = hwwep;
2741 priv->retry_rts = DEFAULT_RETRY_RTS;
2742 priv->retry_data = DEFAULT_RETRY_DATA;
2743 priv->RFChangeInProgress = false;
2744 priv->SetRFPowerStateInProgress = false;
2745 priv->RFProgType = 0;
2746 priv->bInHctTest = false;
2747 priv->bInactivePs = true;//false;
2748 priv->ieee80211->bInactivePs = priv->bInactivePs;
2749 priv->bSwRfProcessing = false;
2750 priv->eRFPowerState = eRfOff;
2751 priv->RfOffReason = 0;
2752 priv->LedStrategy = SW_LED_MODE0;
2753 priv->TxPollingTimes = 0;//lzm add 080826
2754 priv->bLeisurePs = true;
2755 priv->dot11PowerSaveMode = eActive;
2756 priv->AdMinCheckPeriod = 5;
2757 priv->AdMaxCheckPeriod = 10;
2758 priv->AdMaxRxSsThreshold = 30;//60->30
2759 priv->AdRxSsThreshold = 20;//50->20
2760 priv->AdCheckPeriod = priv->AdMinCheckPeriod;
2761 priv->AdTickCount = 0;
2762 priv->AdRxSignalStrength = -1;
2763 priv->RegSwAntennaDiversityMechanism = 0;
2764 priv->RegDefaultAntenna = 0;
2765 priv->SignalStrength = 0;
2766 priv->AdRxOkCnt = 0;
2767 priv->CurrAntennaIndex = 0;
2768 priv->AdRxSsBeforeSwitched = 0;
2769 init_timer(&priv->SwAntennaDiversityTimer);
2770 priv->SwAntennaDiversityTimer.data = (unsigned long)dev;
2771 priv->SwAntennaDiversityTimer.function = (void *)SwAntennaDiversityTimerCallback;
2772 priv->bDigMechanism = 1;
2773 priv->InitialGain = 6;
2774 priv->bXtalCalibration = false;
2775 priv->XtalCal_Xin = 0;
2776 priv->XtalCal_Xout = 0;
2777 priv->bTxPowerTrack = false;
2778 priv->ThermalMeter = 0;
2779 priv->FalseAlarmRegValue = 0;
2780 priv->RegDigOfdmFaUpTh = 0xc; // Upper threhold of OFDM false alarm, which is used in DIG.
2781 priv->DIG_NumberFallbackVote = 0;
2782 priv->DIG_NumberUpgradeVote = 0;
2783 priv->LastSignalStrengthInPercent = 0;
2784 priv->Stats_SignalStrength = 0;
2785 priv->LastRxPktAntenna = 0;
2786 priv->SignalQuality = 0; // in 0-100 index.
2787 priv->Stats_SignalQuality = 0;
2788 priv->RecvSignalPower = 0; // in dBm.
2789 priv->Stats_RecvSignalPower = 0;
2790 priv->AdMainAntennaRxOkCnt = 0;
2791 priv->AdAuxAntennaRxOkCnt = 0;
2792 priv->bHWAdSwitched = false;
2793 priv->bRegHighPowerMechanism = true;
2794 priv->RegHiPwrUpperTh = 77;
2795 priv->RegHiPwrLowerTh = 75;
2796 priv->RegRSSIHiPwrUpperTh = 70;
2797 priv->RegRSSIHiPwrLowerTh = 20;
2798 priv->bCurCCKPkt = false;
2799 priv->UndecoratedSmoothedSS = -1;
2800 priv->bToUpdateTxPwr = false;
2801 priv->CurCCKRSSI = 0;
2804 priv->NumTxOkTotal = 0;
2805 priv->NumTxUnicast = 0;
2806 priv->keepAliveLevel = DEFAULT_KEEP_ALIVE_LEVEL;
2807 priv->PowerProfile = POWER_PROFILE_AC;
2808 priv->CurrRetryCnt = 0;
2809 priv->LastRetryCnt = 0;
2810 priv->LastTxokCnt = 0;
2811 priv->LastRxokCnt = 0;
2812 priv->LastRetryRate = 0;
2813 priv->bTryuping = 0;
2814 priv->CurrTxRate = 0;
2815 priv->CurrRetryRate = 0;
2816 priv->TryupingCount = 0;
2817 priv->TryupingCountNoData = 0;
2818 priv->TryDownCountLowData = 0;
2819 priv->LastTxOKBytes = 0;
2820 priv->LastFailTxRate = 0;
2821 priv->LastFailTxRateSS = 0;
2822 priv->FailTxRateCount = 0;
2823 priv->LastTxThroughput = 0;
2824 priv->NumTxOkBytesTotal = 0;
2825 priv->ForcedDataRate = 0;
2826 priv->RegBModeGainStage = 1;
2828 priv->promisc = (dev->flags & IFF_PROMISC) ? 1 : 0;
2829 spin_lock_init(&priv->irq_lock);
2830 spin_lock_init(&priv->irq_th_lock);
2831 spin_lock_init(&priv->tx_lock);
2832 spin_lock_init(&priv->ps_lock);
2833 spin_lock_init(&priv->rf_ps_lock);
2834 sema_init(&priv->wx_sem, 1);
2835 sema_init(&priv->rf_state, 1);
2836 INIT_WORK(&priv->reset_wq, (void *)rtl8180_restart_wq);
2837 INIT_WORK(&priv->tx_irq_wq, (void *)rtl8180_tx_irq_wq);
2838 INIT_DELAYED_WORK(&priv->ieee80211->hw_wakeup_wq,
2839 (void *)rtl8180_hw_wakeup_wq);
2840 INIT_DELAYED_WORK(&priv->ieee80211->hw_sleep_wq,
2841 (void *)rtl8180_hw_sleep_wq);
2842 INIT_WORK(&priv->ieee80211->wmm_param_update_wq,
2843 (void *)rtl8180_wmm_param_update);
2844 INIT_DELAYED_WORK(&priv->ieee80211->rate_adapter_wq,
2845 (void *)rtl8180_rate_adapter);
2846 INIT_DELAYED_WORK(&priv->ieee80211->hw_dig_wq,
2847 (void *)rtl8180_hw_dig_wq);
2848 INIT_DELAYED_WORK(&priv->ieee80211->tx_pw_wq,
2849 (void *)rtl8180_tx_pw_wq);
2850 INIT_DELAYED_WORK(&priv->ieee80211->GPIOChangeRFWorkItem,
2851 (void *) GPIOChangeRFWorkItemCallBack);
2852 tasklet_init(&priv->irq_rx_tasklet,
2853 (void(*)(unsigned long)) rtl8180_irq_rx_tasklet,
2854 (unsigned long)priv);
2856 init_timer(&priv->watch_dog_timer);
2857 priv->watch_dog_timer.data = (unsigned long)dev;
2858 priv->watch_dog_timer.function = watch_dog_adaptive;
2860 init_timer(&priv->rateadapter_timer);
2861 priv->rateadapter_timer.data = (unsigned long)dev;
2862 priv->rateadapter_timer.function = timer_rate_adaptive;
2863 priv->RateAdaptivePeriod = RATE_ADAPTIVE_TIMER_PERIOD;
2864 priv->bEnhanceTxPwr = false;
2866 priv->ieee80211->softmac_hard_start_xmit = rtl8180_hard_start_xmit;
2867 priv->ieee80211->set_chan = rtl8180_set_chan;
2868 priv->ieee80211->link_change = rtl8180_link_change;
2869 priv->ieee80211->softmac_data_hard_start_xmit = rtl8180_hard_data_xmit;
2870 priv->ieee80211->data_hard_stop = rtl8180_data_hard_stop;
2871 priv->ieee80211->data_hard_resume = rtl8180_data_hard_resume;
2873 priv->ieee80211->init_wmmparam_flag = 0;
2875 priv->ieee80211->start_send_beacons = rtl8180_start_tx_beacon;
2876 priv->ieee80211->stop_send_beacons = rtl8180_beacon_tx_disable;
2877 priv->ieee80211->fts = DEFAULT_FRAG_THRESHOLD;
2879 priv->MWIEnable = 0;
2881 priv->ShortRetryLimit = 7;
2882 priv->LongRetryLimit = 7;
2883 priv->EarlyRxThreshold = 7;
2885 priv->CSMethod = (0x01 << 29);
2887 priv->TransmitConfig = TCR_DurProcMode_OFFSET |
2888 (7<<TCR_MXDMA_OFFSET) |
2889 (priv->ShortRetryLimit<<TCR_SRL_OFFSET) |
2890 (priv->LongRetryLimit<<TCR_LRL_OFFSET) |
2893 priv->ReceiveConfig = RCR_AMF | RCR_ADF | RCR_ACF |
2894 RCR_AB | RCR_AM | RCR_APM |
2895 (7<<RCR_MXDMA_OFFSET) |
2896 (priv->EarlyRxThreshold<<RCR_FIFO_OFFSET) |
2897 (priv->EarlyRxThreshold == 7 ?
2898 RCR_ONLYERLPKT : 0);
2900 priv->IntrMask = IMR_TMGDOK | IMR_TBDER | IMR_THPDER |
2901 IMR_THPDER | IMR_THPDOK |
2902 IMR_TVODER | IMR_TVODOK |
2903 IMR_TVIDER | IMR_TVIDOK |
2904 IMR_TBEDER | IMR_TBEDOK |
2905 IMR_TBKDER | IMR_TBKDOK |
2910 priv->InitialGain = 6;
2912 DMESG("MAC controller is a RTL8187SE b/g");
2915 priv->ieee80211->modulation |= IEEE80211_OFDM_MODULATION;
2916 priv->ieee80211->short_slot = 1;
2919 priv->enable_gpio0 = 0;
2921 eeprom_93cx6_read(&eeprom, EEPROM_SW_REVD_OFFSET, &eeprom_val);
2922 usValue = eeprom_val;
2923 DMESG("usValue is 0x%x\n",usValue);
2924 //3Read AntennaDiversity
2926 // SW Antenna Diversity.
2927 if ((usValue & EEPROM_SW_AD_MASK) != EEPROM_SW_AD_ENABLE)
2928 priv->EEPROMSwAntennaDiversity = false;
2930 priv->EEPROMSwAntennaDiversity = true;
2932 // Default Antenna to use.
2933 if ((usValue & EEPROM_DEF_ANT_MASK) != EEPROM_DEF_ANT_1)
2934 priv->EEPROMDefaultAntenna1 = false;
2936 priv->EEPROMDefaultAntenna1 = true;
2938 if( priv->RegSwAntennaDiversityMechanism == 0 ) // Auto
2939 /* 0: default from EEPROM. */
2940 priv->bSwAntennaDiverity = priv->EEPROMSwAntennaDiversity;
2942 /* 1:disable antenna diversity, 2: enable antenna diversity. */
2943 priv->bSwAntennaDiverity = ((priv->RegSwAntennaDiversityMechanism == 1)? false : true);
2945 if (priv->RegDefaultAntenna == 0)
2946 /* 0: default from EEPROM. */
2947 priv->bDefaultAntenna1 = priv->EEPROMDefaultAntenna1;
2949 /* 1: main, 2: aux. */
2950 priv->bDefaultAntenna1 = ((priv->RegDefaultAntenna== 2) ? true : false);
2952 /* rtl8185 can calc plcp len in HW.*/
2953 priv->hw_plcp_len = 1;
2955 priv->plcp_preamble_mode = 2;
2956 /*the eeprom type is stored in RCR register bit #6 */
2957 if (RCR_9356SEL & read_nic_dword(dev, RCR))
2958 priv->epromtype=EPROM_93c56;
2960 priv->epromtype=EPROM_93c46;
2962 eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)
2965 for(i=1,j=0; i<14; i+=2,j++){
2966 eeprom_93cx6_read(&eeprom, EPROM_TXPW_CH1_2 + j, &word);
2967 priv->chtxpwr[i]=word & 0xff;
2968 priv->chtxpwr[i+1]=(word & 0xff00)>>8;
2970 for (i = 1, j = 0; i < 14; i += 2, j++) {
2971 eeprom_93cx6_read(&eeprom, EPROM_TXPW_OFDM_CH1_2 + j, &word);
2972 priv->chtxpwr_ofdm[i] = word & 0xff;
2973 priv->chtxpwr_ofdm[i+1] = (word & 0xff00) >> 8;
2976 /* 3Read crystal calibtration and thermal meter indication on 87SE. */
2977 eeprom_93cx6_read(&eeprom, EEPROM_RSV>>1, &tmpu16);
2979 /* Crystal calibration for Xin and Xout resp. */
2980 priv->XtalCal_Xout = tmpu16 & EEPROM_XTAL_CAL_XOUT_MASK;
2981 priv->XtalCal_Xin = (tmpu16 & EEPROM_XTAL_CAL_XIN_MASK) >> 4;
2982 if ((tmpu16 & EEPROM_XTAL_CAL_ENABLE) >> 12)
2983 priv->bXtalCalibration = true;
2985 /* Thermal meter reference indication. */
2986 priv->ThermalMeter = (u8)((tmpu16 & EEPROM_THERMAL_METER_MASK) >> 8);
2987 if ((tmpu16 & EEPROM_THERMAL_METER_ENABLE) >> 13)
2988 priv->bTxPowerTrack = true;
2990 eeprom_93cx6_read(&eeprom, EPROM_TXPW_BASE, &word);
2991 priv->cck_txpwr_base = word & 0xf;
2992 priv->ofdm_txpwr_base = (word>>4) & 0xf;
2994 eeprom_93cx6_read(&eeprom, EPROM_VERSION, &version);
2995 DMESG("EEPROM version %x",version);
2996 priv->rcr_csense = 3;
2998 eeprom_93cx6_read(&eeprom, ENERGY_TRESHOLD, &eeprom_val);
2999 priv->cs_treshold = (eeprom_val & 0xff00) >> 8;
3001 eeprom_93cx6_read(&eeprom, RFCHIPID, &eeprom_val);
3002 priv->rf_chip = 0xff & eeprom_val;
3004 priv->rf_chip = RF_ZEBRA4;
3005 priv->rf_sleep = rtl8225z4_rf_sleep;
3006 priv->rf_wakeup = rtl8225z4_rf_wakeup;
3007 DMESGW("**PLEASE** REPORT SUCCESSFUL/UNSUCCESSFUL TO Realtek!");
3009 priv->rf_close = rtl8225z2_rf_close;
3010 priv->rf_init = rtl8225z2_rf_init;
3011 priv->rf_set_chan = rtl8225z2_rf_set_chan;
3012 priv->rf_set_sens = NULL;
3014 if (0!=alloc_rx_desc_ring(dev, priv->rxbuffersize, priv->rxringcount))
3017 if (0!=alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txringcount,
3018 TX_MANAGEPRIORITY_RING_ADDR))
3021 if (0!=alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txringcount,
3022 TX_BKPRIORITY_RING_ADDR))
3025 if (0!=alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txringcount,
3026 TX_BEPRIORITY_RING_ADDR))
3029 if (0!=alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txringcount,
3030 TX_VIPRIORITY_RING_ADDR))
3033 if (0!=alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txringcount,
3034 TX_VOPRIORITY_RING_ADDR))
3037 if (0!=alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txringcount,
3038 TX_HIGHPRIORITY_RING_ADDR))
3041 if (0!=alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txbeaconcount,
3042 TX_BEACON_RING_ADDR))
3045 if(request_irq(dev->irq, (void *)rtl8180_interrupt, IRQF_SHARED, dev->name, dev)){
3046 DMESGE("Error allocating IRQ %d",dev->irq);
3050 DMESG("IRQ %d",dev->irq);
3056 void rtl8180_no_hw_wep(struct net_device *dev)
3060 void rtl8180_set_hw_wep(struct net_device *dev)
3062 struct r8180_priv *priv = ieee80211_priv(dev);
3067 pgreg=read_nic_byte(dev, PGSELECT);
3068 write_nic_byte(dev, PGSELECT, pgreg &~ (1<<PGSELECT_PG_SHIFT));
3070 key0_word4 = read_nic_dword(dev, KEY0+4+4+4);
3071 key0_word4 &= ~ 0xff;
3072 key0_word4 |= priv->key0[3]& 0xff;
3073 write_nic_dword(dev,KEY0,(priv->key0[0]));
3074 write_nic_dword(dev,KEY0+4,(priv->key0[1]));
3075 write_nic_dword(dev,KEY0+4+4,(priv->key0[2]));
3076 write_nic_dword(dev,KEY0+4+4+4,(key0_word4));
3078 security = read_nic_byte(dev,SECURITY);
3079 security |= (1<<SECURITY_WEP_TX_ENABLE_SHIFT);
3080 security |= (1<<SECURITY_WEP_RX_ENABLE_SHIFT);
3081 security &= ~ SECURITY_ENCRYP_MASK;
3082 security |= (SECURITY_ENCRYP_104<<SECURITY_ENCRYP_SHIFT);
3084 write_nic_byte(dev, SECURITY, security);
3086 DMESG("key %x %x %x %x",read_nic_dword(dev,KEY0+4+4+4),
3087 read_nic_dword(dev,KEY0+4+4),read_nic_dword(dev,KEY0+4),
3088 read_nic_dword(dev,KEY0));
3092 void rtl8185_rf_pins_enable(struct net_device *dev)
3095 // tmp = read_nic_word(dev, RFPinsEnable);
3096 write_nic_word(dev, RFPinsEnable, 0x1fff);// | tmp);
3099 void rtl8185_set_anaparam2(struct net_device *dev, u32 a)
3103 rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
3105 conf3 = read_nic_byte(dev, CONFIG3);
3106 write_nic_byte(dev, CONFIG3, conf3 | (1<<CONFIG3_ANAPARAM_W_SHIFT));
3107 write_nic_dword(dev, ANAPARAM2, a);
3109 conf3 = read_nic_byte(dev, CONFIG3);
3110 write_nic_byte(dev, CONFIG3, conf3 &~(1<<CONFIG3_ANAPARAM_W_SHIFT));
3111 rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
3114 void rtl8180_set_anaparam(struct net_device *dev, u32 a)
3118 rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
3120 conf3 = read_nic_byte(dev, CONFIG3);
3121 write_nic_byte(dev, CONFIG3, conf3 | (1<<CONFIG3_ANAPARAM_W_SHIFT));
3122 write_nic_dword(dev, ANAPARAM, a);
3124 conf3 = read_nic_byte(dev, CONFIG3);
3125 write_nic_byte(dev, CONFIG3, conf3 &~(1<<CONFIG3_ANAPARAM_W_SHIFT));
3126 rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
3129 void rtl8185_tx_antenna(struct net_device *dev, u8 ant)
3131 write_nic_byte(dev, TX_ANTENNA, ant);
3132 force_pci_posting(dev);
3136 void rtl8185_write_phy(struct net_device *dev, u8 adr, u32 data)
3142 phyw= ((data<<8) | adr);
3144 // Note that, we must write 0xff7c after 0x7d-0x7f to write BB register.
3145 write_nic_byte(dev, 0x7f, ((phyw & 0xff000000) >> 24));
3146 write_nic_byte(dev, 0x7e, ((phyw & 0x00ff0000) >> 16));
3147 write_nic_byte(dev, 0x7d, ((phyw & 0x0000ff00) >> 8));
3148 write_nic_byte(dev, 0x7c, ((phyw & 0x000000ff) ));
3150 /* this is ok to fail when we write AGC table. check for AGC table might be
3151 * done by masking with 0x7f instead of 0xff
3153 //if(phyr != (data&0xff)) DMESGW("Phy write timeout %x %x %x", phyr, data,adr);
3156 inline void write_phy_ofdm (struct net_device *dev, u8 adr, u32 data)
3159 rtl8185_write_phy(dev, adr, data);
3162 void write_phy_cck (struct net_device *dev, u8 adr, u32 data)
3165 rtl8185_write_phy(dev, adr, data | 0x10000);
3168 void rtl8185_set_rate(struct net_device *dev)
3172 int basic_rate,min_rr_rate,max_rr_rate;
3174 basic_rate = ieeerate2rtlrate(240);
3175 min_rr_rate = ieeerate2rtlrate(60);
3176 max_rr_rate = ieeerate2rtlrate(240);
3178 write_nic_byte(dev, RESP_RATE,
3179 max_rr_rate<<MAX_RESP_RATE_SHIFT| min_rr_rate<<MIN_RESP_RATE_SHIFT);
3181 word = read_nic_word(dev, BRSR);
3182 word &= ~BRSR_MBR_8185;
3184 for(i=0;i<=basic_rate;i++)
3187 write_nic_word(dev, BRSR, word);
3190 void rtl8180_adapter_start(struct net_device *dev)
3192 struct r8180_priv *priv = ieee80211_priv(dev);
3194 rtl8180_rtx_disable(dev);
3197 /* enable beacon timeout, beacon TX ok and err
3198 * LP tx ok and err, HP TX ok and err, NP TX ok and err,
3199 * RX ok and ERR, and GP timer */
3200 priv->irq_mask = 0x6fcf;
3202 priv->dma_poll_mask = 0;
3204 rtl8180_beacon_tx_disable(dev);
3206 rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
3207 write_nic_dword(dev, MAC0, ((u32*)dev->dev_addr)[0]);
3208 write_nic_word(dev, MAC4, ((u32*)dev->dev_addr)[1] & 0xffff );
3209 rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
3211 rtl8180_update_msr(dev);
3213 /* These might be unnecessary since we do in rx_enable / tx_enable */
3217 rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
3220 The following is very strange. seems to be that 1 means test mode,
3221 but we need to acknolwledges the nic when a packet is ready
3222 although we set it to 0
3226 CONFIG2, read_nic_byte(dev,CONFIG2) &~\
3227 (1<<CONFIG2_DMA_POLLING_MODE_SHIFT));
3228 //^the nic isn't in test mode
3230 CONFIG2, read_nic_byte(dev,CONFIG2)|(1<<4));
3232 rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
3234 write_nic_dword(dev,INT_TIMEOUT,0);
3236 write_nic_byte(dev, WPA_CONFIG, 0);
3238 rtl8180_no_hw_wep(dev);
3240 rtl8185_set_rate(dev);
3241 write_nic_byte(dev, RATE_FALLBACK, 0x81);
3243 write_nic_byte(dev, GP_ENABLE, read_nic_byte(dev, GP_ENABLE) & ~(1<<6));
3245 /*FIXME cfg 3 ClkRun enable - isn't it ReadOnly ? */
3246 rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
3247 write_nic_byte(dev, CONFIG3, read_nic_byte(dev, CONFIG3)
3248 | (1 << CONFIG3_CLKRUN_SHIFT));
3249 rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
3253 if(priv->rf_set_sens != NULL)
3254 priv->rf_set_sens(dev,priv->sens);
3255 rtl8180_irq_enable(dev);
3257 netif_start_queue(dev);
3260 /* this configures registers for beacon tx and enables it via
3261 * rtl8180_beacon_tx_enable(). rtl8180_beacon_tx_disable() might
3262 * be used to stop beacon transmission
3264 void rtl8180_start_tx_beacon(struct net_device *dev)
3268 DMESG("Enabling beacon TX");
3269 rtl8180_prepare_beacon(dev);
3270 rtl8180_irq_disable(dev);
3271 rtl8180_beacon_tx_enable(dev);
3273 word = read_nic_word(dev, AtimWnd) &~ AtimWnd_AtimWnd;
3274 write_nic_word(dev, AtimWnd,word);// word |=
3276 word = read_nic_word(dev, BintrItv);
3277 word &= ~BintrItv_BintrItv;
3278 word |= 1000;/*priv->ieee80211->current_network.beacon_interval *
3279 ((priv->txbeaconcount > 1)?(priv->txbeaconcount-1):1);
3280 // FIXME: check if correct ^^ worked with 0x3e8;
3282 write_nic_word(dev, BintrItv, word);
3284 rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
3286 rtl8185b_irq_enable(dev);
3289 static struct net_device_stats *rtl8180_stats(struct net_device *dev)
3291 struct r8180_priv *priv = ieee80211_priv(dev);
3293 return &priv->ieee80211->stats;
3296 // Change current and default preamble mode.
3299 MgntActSet_802_11_PowerSaveMode(
3300 struct r8180_priv *priv,
3304 // Currently, we do not change power save mode on IBSS mode.
3305 if(priv->ieee80211->iw_mode == IW_MODE_ADHOC)
3308 priv->ieee80211->ps = rtPsMode;
3313 void LeisurePSEnter(struct r8180_priv *priv)
3315 if (priv->bLeisurePs) {
3316 if (priv->ieee80211->ps == IEEE80211_PS_DISABLED)
3317 MgntActSet_802_11_PowerSaveMode(priv, IEEE80211_PS_MBCAST|IEEE80211_PS_UNICAST);//IEEE80211_PS_ENABLE
3321 void LeisurePSLeave(struct r8180_priv *priv)
3323 if (priv->bLeisurePs) {
3324 if (priv->ieee80211->ps != IEEE80211_PS_DISABLED)
3325 MgntActSet_802_11_PowerSaveMode(priv, IEEE80211_PS_DISABLED);
3329 void rtl8180_hw_wakeup_wq (struct work_struct *work)
3331 struct delayed_work *dwork = to_delayed_work(work);
3332 struct ieee80211_device *ieee = container_of(dwork,struct ieee80211_device,hw_wakeup_wq);
3333 struct net_device *dev = ieee->dev;
3335 rtl8180_hw_wakeup(dev);
3338 void rtl8180_hw_sleep_wq (struct work_struct *work)
3340 struct delayed_work *dwork = to_delayed_work(work);
3341 struct ieee80211_device *ieee = container_of(dwork,struct ieee80211_device,hw_sleep_wq);
3342 struct net_device *dev = ieee->dev;
3344 rtl8180_hw_sleep_down(dev);
3347 static void MgntLinkKeepAlive(struct r8180_priv *priv )
3349 if (priv->keepAliveLevel == 0)
3352 if(priv->ieee80211->state == IEEE80211_LINKED)
3358 if ( (priv->keepAliveLevel== 2) ||
3359 (priv->link_detect.LastNumTxUnicast == priv->NumTxUnicast &&
3360 priv->link_detect.LastNumRxUnicast == priv->ieee80211->NumRxUnicast )
3363 priv->link_detect.IdleCount++;
3366 // Send a Keep-Alive packet packet to AP if we had been idle for a while.
3368 if(priv->link_detect.IdleCount >= ((KEEP_ALIVE_INTERVAL / CHECK_FOR_HANG_PERIOD)-1) )
3370 priv->link_detect.IdleCount = 0;
3371 ieee80211_sta_ps_send_null_frame(priv->ieee80211, false);
3376 priv->link_detect.IdleCount = 0;
3378 priv->link_detect.LastNumTxUnicast = priv->NumTxUnicast;
3379 priv->link_detect.LastNumRxUnicast = priv->ieee80211->NumRxUnicast;
3383 static u8 read_acadapter_file(char *filename);
3385 void rtl8180_watch_dog(struct net_device *dev)
3387 struct r8180_priv *priv = ieee80211_priv(dev);
3388 bool bEnterPS = false;
3389 bool bBusyTraffic = false;
3393 if(priv->ieee80211->actscanning == false){
3394 if((priv->ieee80211->iw_mode != IW_MODE_ADHOC) && (priv->ieee80211->state == IEEE80211_NOLINK) && (priv->ieee80211->beinretry == false) && (priv->eRFPowerState == eRfOn)){
3398 //YJ,add,080828,for link state check
3399 if((priv->ieee80211->state == IEEE80211_LINKED) && (priv->ieee80211->iw_mode == IW_MODE_INFRA)){
3400 SlotIndex = (priv->link_detect.SlotIndex++) % priv->link_detect.SlotNum;
3401 priv->link_detect.RxFrameNum[SlotIndex] = priv->ieee80211->NumRxDataInPeriod + priv->ieee80211->NumRxBcnInPeriod;
3402 for( i=0; i<priv->link_detect.SlotNum; i++ )
3403 TotalRxNum+= priv->link_detect.RxFrameNum[i];
3405 if(TotalRxNum == 0){
3406 priv->ieee80211->state = IEEE80211_ASSOCIATING;
3407 queue_work(priv->ieee80211->wq, &priv->ieee80211->associate_procedure_wq);
3411 //YJ,add,080828,for KeepAlive
3412 MgntLinkKeepAlive(priv);
3414 //YJ,add,080828,for LPS
3415 if (priv->PowerProfile == POWER_PROFILE_BATTERY)
3416 priv->bLeisurePs = true;
3417 else if (priv->PowerProfile == POWER_PROFILE_AC) {
3418 LeisurePSLeave(priv);
3419 priv->bLeisurePs= false;
3422 if(priv->ieee80211->state == IEEE80211_LINKED){
3423 priv->link_detect.NumRxOkInPeriod = priv->ieee80211->NumRxDataInPeriod;
3424 if( priv->link_detect.NumRxOkInPeriod> 666 ||
3425 priv->link_detect.NumTxOkInPeriod> 666 ) {
3426 bBusyTraffic = true;
3428 if(((priv->link_detect.NumRxOkInPeriod + priv->link_detect.NumTxOkInPeriod) > 8)
3429 || (priv->link_detect.NumRxOkInPeriod > 2)) {
3435 LeisurePSEnter(priv);
3437 LeisurePSLeave(priv);
3439 LeisurePSLeave(priv);
3440 priv->link_detect.bBusyTraffic = bBusyTraffic;
3441 priv->link_detect.NumRxOkInPeriod = 0;
3442 priv->link_detect.NumTxOkInPeriod = 0;
3443 priv->ieee80211->NumRxDataInPeriod = 0;
3444 priv->ieee80211->NumRxBcnInPeriod = 0;
3447 int _rtl8180_up(struct net_device *dev)
3449 struct r8180_priv *priv = ieee80211_priv(dev);
3453 DMESG("Bringing up iface");
3454 rtl8185b_adapter_start(dev);
3455 rtl8185b_rx_enable(dev);
3456 rtl8185b_tx_enable(dev);
3457 if(priv->bInactivePs){
3458 if(priv->ieee80211->iw_mode == IW_MODE_ADHOC)
3461 timer_rate_adaptive((unsigned long)dev);
3462 watch_dog_adaptive((unsigned long)dev);
3463 if(priv->bSwAntennaDiverity)
3464 SwAntennaDiversityTimerCallback(dev);
3465 ieee80211_softmac_start_protocol(priv->ieee80211);
3469 int rtl8180_open(struct net_device *dev)
3471 struct r8180_priv *priv = ieee80211_priv(dev);
3474 down(&priv->wx_sem);
3475 ret = rtl8180_up(dev);
3480 int rtl8180_up(struct net_device *dev)
3482 struct r8180_priv *priv = ieee80211_priv(dev);
3484 if (priv->up == 1) return -1;
3486 return _rtl8180_up(dev);
3489 int rtl8180_close(struct net_device *dev)
3491 struct r8180_priv *priv = ieee80211_priv(dev);
3494 down(&priv->wx_sem);
3495 ret = rtl8180_down(dev);
3501 int rtl8180_down(struct net_device *dev)
3503 struct r8180_priv *priv = ieee80211_priv(dev);
3510 ieee80211_softmac_stop_protocol(priv->ieee80211);
3512 if (!netif_queue_stopped(dev))
3513 netif_stop_queue(dev);
3514 rtl8180_rtx_disable(dev);
3515 rtl8180_irq_disable(dev);
3516 del_timer_sync(&priv->watch_dog_timer);
3517 del_timer_sync(&priv->rateadapter_timer);
3518 cancel_delayed_work(&priv->ieee80211->rate_adapter_wq);
3519 cancel_delayed_work(&priv->ieee80211->hw_wakeup_wq);
3520 cancel_delayed_work(&priv->ieee80211->hw_sleep_wq);
3521 cancel_delayed_work(&priv->ieee80211->hw_dig_wq);
3522 cancel_delayed_work(&priv->ieee80211->tx_pw_wq);
3523 del_timer_sync(&priv->SwAntennaDiversityTimer);
3524 SetZebraRFPowerState8185(dev,eRfOff);
3525 memset(&(priv->ieee80211->current_network),0,sizeof(struct ieee80211_network));
3526 priv->ieee80211->state = IEEE80211_NOLINK;
3530 void rtl8180_restart_wq(struct work_struct *work)
3532 struct r8180_priv *priv = container_of(work, struct r8180_priv, reset_wq);
3533 struct net_device *dev = priv->dev;
3535 down(&priv->wx_sem);
3537 rtl8180_commit(dev);
3542 void rtl8180_restart(struct net_device *dev)
3544 struct r8180_priv *priv = ieee80211_priv(dev);
3546 schedule_work(&priv->reset_wq);
3549 void rtl8180_commit(struct net_device *dev)
3551 struct r8180_priv *priv = ieee80211_priv(dev);
3556 del_timer_sync(&priv->watch_dog_timer);
3557 del_timer_sync(&priv->rateadapter_timer);
3558 cancel_delayed_work(&priv->ieee80211->rate_adapter_wq);
3559 cancel_delayed_work(&priv->ieee80211->hw_wakeup_wq);
3560 cancel_delayed_work(&priv->ieee80211->hw_sleep_wq);
3561 cancel_delayed_work(&priv->ieee80211->hw_dig_wq);
3562 cancel_delayed_work(&priv->ieee80211->tx_pw_wq);
3563 del_timer_sync(&priv->SwAntennaDiversityTimer);
3564 ieee80211_softmac_stop_protocol(priv->ieee80211);
3565 rtl8180_irq_disable(dev);
3566 rtl8180_rtx_disable(dev);
3570 static void r8180_set_multicast(struct net_device *dev)
3572 struct r8180_priv *priv = ieee80211_priv(dev);
3575 promisc = (dev->flags & IFF_PROMISC) ? 1:0;
3577 if (promisc != priv->promisc)
3578 rtl8180_restart(dev);
3580 priv->promisc = promisc;
3583 int r8180_set_mac_adr(struct net_device *dev, void *mac)
3585 struct r8180_priv *priv = ieee80211_priv(dev);
3586 struct sockaddr *addr = mac;
3588 down(&priv->wx_sem);
3590 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
3592 if(priv->ieee80211->iw_mode == IW_MODE_MASTER)
3593 memcpy(priv->ieee80211->current_network.bssid, dev->dev_addr, ETH_ALEN);
3605 /* based on ipw2200 driver */
3606 int rtl8180_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3608 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
3609 struct iwreq *wrq = (struct iwreq *) rq;
3613 case RTL_IOCTL_WPA_SUPPLICANT:
3614 ret = ieee80211_wpa_supplicant_ioctl(priv->ieee80211, &wrq->u.data);
3623 static const struct net_device_ops rtl8180_netdev_ops = {
3624 .ndo_open = rtl8180_open,
3625 .ndo_stop = rtl8180_close,
3626 .ndo_get_stats = rtl8180_stats,
3627 .ndo_tx_timeout = rtl8180_restart,
3628 .ndo_do_ioctl = rtl8180_ioctl,
3629 .ndo_set_multicast_list = r8180_set_multicast,
3630 .ndo_set_mac_address = r8180_set_mac_adr,
3631 .ndo_validate_addr = eth_validate_addr,
3632 .ndo_change_mtu = eth_change_mtu,
3633 .ndo_start_xmit = ieee80211_rtl_xmit,
3636 static int __devinit rtl8180_pci_probe(struct pci_dev *pdev,
3637 const struct pci_device_id *id)
3639 unsigned long ioaddr = 0;
3640 struct net_device *dev = NULL;
3641 struct r8180_priv *priv= NULL;
3644 unsigned long pmem_start, pmem_len, pmem_flags;
3646 DMESG("Configuring chip resources");
3648 if( pci_enable_device (pdev) ){
3649 DMESG("Failed to enable PCI device");
3653 pci_set_master(pdev);
3654 pci_set_dma_mask(pdev, 0xffffff00ULL);
3655 pci_set_consistent_dma_mask(pdev,0xffffff00ULL);
3656 dev = alloc_ieee80211(sizeof(struct r8180_priv));
3659 priv = ieee80211_priv(dev);
3660 priv->ieee80211 = netdev_priv(dev);
3662 pci_set_drvdata(pdev, dev);
3663 SET_NETDEV_DEV(dev, &pdev->dev);
3665 priv = ieee80211_priv(dev);
3668 pmem_start = pci_resource_start(pdev, 1);
3669 pmem_len = pci_resource_len(pdev, 1);
3670 pmem_flags = pci_resource_flags (pdev, 1);
3672 if (!(pmem_flags & IORESOURCE_MEM)) {
3673 DMESG("region #1 not a MMIO resource, aborting");
3677 if( ! request_mem_region(pmem_start, pmem_len, RTL8180_MODULE_NAME)) {
3678 DMESG("request_mem_region failed!");
3682 ioaddr = (unsigned long)ioremap_nocache( pmem_start, pmem_len);
3683 if( ioaddr == (unsigned long)NULL ){
3684 DMESG("ioremap failed!");
3688 dev->mem_start = ioaddr; // shared mem start
3689 dev->mem_end = ioaddr + pci_resource_len(pdev, 0); // shared mem end
3691 pci_read_config_byte(pdev, 0x05, &unit);
3692 pci_write_config_byte(pdev, 0x05, unit & (~0x04));
3694 dev->irq = pdev->irq;
3697 dev->netdev_ops = &rtl8180_netdev_ops;
3698 dev->wireless_handlers = &r8180_wx_handlers_def;
3700 dev->type=ARPHRD_ETHER;
3701 dev->watchdog_timeo = HZ*3;
3703 if (dev_alloc_name(dev, ifname) < 0){
3704 DMESG("Oops: devname already taken! Trying wlan%%d...\n");
3706 dev_alloc_name(dev, ifname);
3709 if(rtl8180_init(dev)!=0){
3710 DMESG("Initialization failed");
3714 netif_carrier_off(dev);
3716 register_netdev(dev);
3718 rtl8180_proc_init_one(dev);
3720 DMESG("Driver probe completed\n");
3723 if( dev->mem_start != (unsigned long)NULL ){
3724 iounmap( (void *)dev->mem_start );
3725 release_mem_region( pci_resource_start(pdev, 1),
3726 pci_resource_len(pdev, 1) );
3731 free_irq(dev->irq, dev);
3734 free_ieee80211(dev);
3737 pci_disable_device(pdev);
3739 DMESG("wlan driver load failed\n");
3740 pci_set_drvdata(pdev, NULL);
3744 static void __devexit rtl8180_pci_remove(struct pci_dev *pdev)
3746 struct r8180_priv *priv;
3747 struct net_device *dev = pci_get_drvdata(pdev);
3750 unregister_netdev(dev);
3752 priv = ieee80211_priv(dev);
3754 rtl8180_proc_remove_one(dev);
3756 priv->rf_close(dev);
3761 DMESG("Freeing irq %d",dev->irq);
3762 free_irq(dev->irq, dev);
3766 free_rx_desc_ring(dev);
3767 free_tx_desc_rings(dev);
3769 if( dev->mem_start != (unsigned long)NULL ){
3770 iounmap( (void *)dev->mem_start );
3771 release_mem_region( pci_resource_start(pdev, 1),
3772 pci_resource_len(pdev, 1) );
3775 free_ieee80211(dev);
3777 pci_disable_device(pdev);
3779 DMESG("wlan driver removed\n");
3782 /* fun with the built-in ieee80211 stack... */
3783 extern int ieee80211_crypto_init(void);
3784 extern void ieee80211_crypto_deinit(void);
3785 extern int ieee80211_crypto_tkip_init(void);
3786 extern void ieee80211_crypto_tkip_exit(void);
3787 extern int ieee80211_crypto_ccmp_init(void);
3788 extern void ieee80211_crypto_ccmp_exit(void);
3789 extern int ieee80211_crypto_wep_init(void);
3790 extern void ieee80211_crypto_wep_exit(void);
3792 static int __init rtl8180_pci_module_init(void)
3796 ret = ieee80211_crypto_init();
3798 printk(KERN_ERR "ieee80211_crypto_init() failed %d\n", ret);
3801 ret = ieee80211_crypto_tkip_init();
3803 printk(KERN_ERR "ieee80211_crypto_tkip_init() failed %d\n", ret);
3806 ret = ieee80211_crypto_ccmp_init();
3808 printk(KERN_ERR "ieee80211_crypto_ccmp_init() failed %d\n", ret);
3811 ret = ieee80211_crypto_wep_init();
3813 printk(KERN_ERR "ieee80211_crypto_wep_init() failed %d\n", ret);
3817 printk(KERN_INFO "\nLinux kernel driver for RTL8180 / RTL8185 based WLAN cards\n");
3818 printk(KERN_INFO "Copyright (c) 2004-2005, Andrea Merello\n");
3819 DMESG("Initializing module");
3820 DMESG("Wireless extensions version %d", WIRELESS_EXT);
3821 rtl8180_proc_module_init();
3823 if (pci_register_driver(&rtl8180_pci_driver)) {
3824 DMESG("No device found");
3830 static void __exit rtl8180_pci_module_exit(void)
3832 pci_unregister_driver (&rtl8180_pci_driver);
3833 rtl8180_proc_module_remove();
3834 ieee80211_crypto_tkip_exit();
3835 ieee80211_crypto_ccmp_exit();
3836 ieee80211_crypto_wep_exit();
3837 ieee80211_crypto_deinit();
3841 void rtl8180_try_wake_queue(struct net_device *dev, int pri)
3843 unsigned long flags;
3845 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
3847 spin_lock_irqsave(&priv->tx_lock,flags);
3848 enough_desc = check_nic_enought_desc(dev,pri);
3849 spin_unlock_irqrestore(&priv->tx_lock,flags);
3852 ieee80211_rtl_wake_queue(priv->ieee80211);
3855 void rtl8180_tx_isr(struct net_device *dev, int pri,short error)
3857 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
3858 u32 *tail; //tail virtual addr
3859 u32 *head; //head virtual addr
3860 u32 *begin;//start of ring virtual addr
3861 u32 *nicv; //nic pointer virtual addr
3862 u32 nic; //nic pointer physical addr
3863 u32 nicbegin;// start of ring physical addr
3865 /* physical addr are ok on 32 bits since we set DMA mask*/
3869 if (error) priv->stats.txretry++; //tony 20060601
3870 spin_lock_irqsave(&priv->tx_lock,flag);
3872 case MANAGE_PRIORITY:
3873 tail = priv->txmapringtail;
3874 begin = priv->txmapring;
3875 head = priv->txmapringhead;
3876 nic = read_nic_dword(dev,TX_MANAGEPRIORITY_RING_ADDR);
3877 nicbegin = priv->txmapringdma;
3880 tail = priv->txbkpringtail;
3881 begin = priv->txbkpring;
3882 head = priv->txbkpringhead;
3883 nic = read_nic_dword(dev,TX_BKPRIORITY_RING_ADDR);
3884 nicbegin = priv->txbkpringdma;
3887 tail = priv->txbepringtail;
3888 begin = priv->txbepring;
3889 head = priv->txbepringhead;
3890 nic = read_nic_dword(dev,TX_BEPRIORITY_RING_ADDR);
3891 nicbegin = priv->txbepringdma;
3894 tail = priv->txvipringtail;
3895 begin = priv->txvipring;
3896 head = priv->txvipringhead;
3897 nic = read_nic_dword(dev,TX_VIPRIORITY_RING_ADDR);
3898 nicbegin = priv->txvipringdma;
3901 tail = priv->txvopringtail;
3902 begin = priv->txvopring;
3903 head = priv->txvopringhead;
3904 nic = read_nic_dword(dev,TX_VOPRIORITY_RING_ADDR);
3905 nicbegin = priv->txvopringdma;
3908 tail = priv->txhpringtail;
3909 begin = priv->txhpring;
3910 head = priv->txhpringhead;
3911 nic = read_nic_dword(dev,TX_HIGHPRIORITY_RING_ADDR);
3912 nicbegin = priv->txhpringdma;
3916 spin_unlock_irqrestore(&priv->tx_lock,flag);
3920 nicv = (u32*) ((nic - nicbegin) + (u8*)begin);
3921 if((head <= tail && (nicv > tail || nicv < head)) ||
3922 (head > tail && (nicv > tail && nicv < head))){
3923 DMESGW("nic has lost pointer");
3924 spin_unlock_irqrestore(&priv->tx_lock,flag);
3925 rtl8180_restart(dev);
3929 /* we check all the descriptors between the head and the nic,
3930 * but not the currently pointed by the nic (the next to be txed)
3931 * and the previous of the pointed (might be in process ??)
3933 offs = (nic - nicbegin);
3935 hd = (head - begin) /8;
3940 j = offs + (priv->txringcount -1 -hd);
3947 if((*head) & (1<<31))
3949 if(((*head)&(0x10000000)) != 0){
3950 priv->CurrRetryCnt += (u16)((*head) & (0x000000ff));
3952 priv->NumTxOkTotal++;
3956 priv->NumTxOkBytesTotal += (*(head+3)) & (0x00000fff);
3958 *head = *head &~ (1<<31);
3960 if((head - begin)/8 == priv->txringcount-1)
3966 /* the head has been moved to the last certainly TXed
3967 * (or at least processed by the nic) packet.
3968 * The driver take forcefully owning of all these packets
3969 * If the packet previous of the nic pointer has been
3970 * processed this doesn't matter: it will be checked
3971 * here at the next round. Anyway if no more packet are
3972 * TXed no memory leak occour at all.
3976 case MANAGE_PRIORITY:
3977 priv->txmapringhead = head;
3979 if(priv->ack_tx_to_ieee){
3980 if(rtl8180_is_tx_queue_empty(dev)){
3981 priv->ack_tx_to_ieee = 0;
3982 ieee80211_ps_tx_ack(priv->ieee80211,!error);
3987 priv->txbkpringhead = head;
3990 priv->txbepringhead = head;
3993 priv->txvipringhead = head;
3996 priv->txvopringhead = head;
3999 priv->txhpringhead = head;
4003 spin_unlock_irqrestore(&priv->tx_lock,flag);
4006 void rtl8180_tx_irq_wq(struct work_struct *work)
4008 struct delayed_work *dwork = to_delayed_work(work);
4009 struct ieee80211_device * ieee = (struct ieee80211_device*)
4010 container_of(dwork, struct ieee80211_device, watch_dog_wq);
4011 struct net_device *dev = ieee->dev;
4013 rtl8180_tx_isr(dev,MANAGE_PRIORITY,0);
4015 irqreturn_t rtl8180_interrupt(int irq, void *netdev, struct pt_regs *regs)
4017 struct net_device *dev = (struct net_device *) netdev;
4018 struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
4019 unsigned long flags;
4022 /* We should return IRQ_NONE, but for now let me keep this */
4023 if(priv->irq_enabled == 0) return IRQ_HANDLED;
4025 spin_lock_irqsave(&priv->irq_th_lock,flags);
4028 inta = read_nic_dword(dev, ISR);// & priv->IntrMask;
4029 write_nic_dword(dev,ISR,inta); // reset int situation
4031 priv->stats.shints++;
4034 spin_unlock_irqrestore(&priv->irq_th_lock,flags);
4037 most probably we can safely return IRQ_NONE,
4038 but for now is better to avoid problems
4042 if (inta == 0xffff) {
4044 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
4050 if(!netif_running(dev)) {
4051 spin_unlock_irqrestore(&priv->irq_th_lock,flags);
4055 if (inta & ISR_TimeOut)
4056 write_nic_dword(dev, TimerInt, 0);
4058 if (inta & ISR_TBDOK)
4059 priv->stats.txbeacon++;
4061 if (inta & ISR_TBDER)
4062 priv->stats.txbeaconerr++;
4064 if (inta & IMR_TMGDOK)
4065 rtl8180_tx_isr(dev,MANAGE_PRIORITY,0);
4067 if(inta & ISR_THPDER){
4068 priv->stats.txhperr++;
4069 rtl8180_tx_isr(dev,HI_PRIORITY,1);
4070 priv->ieee80211->stats.tx_errors++;
4073 if(inta & ISR_THPDOK){ //High priority tx ok
4074 priv->link_detect.NumTxOkInPeriod++; //YJ,add,080828
4075 priv->stats.txhpokint++;
4076 rtl8180_tx_isr(dev,HI_PRIORITY,0);
4079 if(inta & ISR_RER) {
4080 priv->stats.rxerr++;
4082 if(inta & ISR_TBKDER){ //corresponding to BK_PRIORITY
4083 priv->stats.txbkperr++;
4084 priv->ieee80211->stats.tx_errors++;
4085 rtl8180_tx_isr(dev,BK_PRIORITY,1);
4086 rtl8180_try_wake_queue(dev, BE_PRIORITY);
4089 if(inta & ISR_TBEDER){ //corresponding to BE_PRIORITY
4090 priv->stats.txbeperr++;
4091 priv->ieee80211->stats.tx_errors++;
4092 rtl8180_tx_isr(dev,BE_PRIORITY,1);
4093 rtl8180_try_wake_queue(dev, BE_PRIORITY);
4095 if(inta & ISR_TNPDER){ //corresponding to VO_PRIORITY
4096 priv->stats.txnperr++;
4097 priv->ieee80211->stats.tx_errors++;
4098 rtl8180_tx_isr(dev,NORM_PRIORITY,1);
4099 rtl8180_try_wake_queue(dev, NORM_PRIORITY);
4102 if(inta & ISR_TLPDER){ //corresponding to VI_PRIORITY
4103 priv->stats.txlperr++;
4104 priv->ieee80211->stats.tx_errors++;
4105 rtl8180_tx_isr(dev,LOW_PRIORITY,1);
4106 rtl8180_try_wake_queue(dev, LOW_PRIORITY);
4110 priv->stats.rxint++;
4111 tasklet_schedule(&priv->irq_rx_tasklet);
4114 if(inta & ISR_RQoSOK ){
4115 priv->stats.rxint++;
4116 tasklet_schedule(&priv->irq_rx_tasklet);
4118 if(inta & ISR_BcnInt) {
4119 rtl8180_prepare_beacon(dev);
4123 DMESGW("No RX descriptor available");
4124 priv->stats.rxrdu++;
4125 tasklet_schedule(&priv->irq_rx_tasklet);
4128 if(inta & ISR_RXFOVW){
4129 priv->stats.rxoverflow++;
4130 tasklet_schedule(&priv->irq_rx_tasklet);
4133 if (inta & ISR_TXFOVW)
4134 priv->stats.txoverflow++;
4136 if(inta & ISR_TNPDOK){ //Normal priority tx ok
4137 priv->link_detect.NumTxOkInPeriod++; //YJ,add,080828
4138 priv->stats.txnpokint++;
4139 rtl8180_tx_isr(dev,NORM_PRIORITY,0);
4142 if(inta & ISR_TLPDOK){ //Low priority tx ok
4143 priv->link_detect.NumTxOkInPeriod++; //YJ,add,080828
4144 priv->stats.txlpokint++;
4145 rtl8180_tx_isr(dev,LOW_PRIORITY,0);
4146 rtl8180_try_wake_queue(dev, LOW_PRIORITY);
4149 if(inta & ISR_TBKDOK){ //corresponding to BK_PRIORITY
4150 priv->stats.txbkpokint++;
4151 priv->link_detect.NumTxOkInPeriod++; //YJ,add,080828
4152 rtl8180_tx_isr(dev,BK_PRIORITY,0);
4153 rtl8180_try_wake_queue(dev, BE_PRIORITY);
4156 if(inta & ISR_TBEDOK){ //corresponding to BE_PRIORITY
4157 priv->stats.txbeperr++;
4158 priv->link_detect.NumTxOkInPeriod++; //YJ,add,080828
4159 rtl8180_tx_isr(dev,BE_PRIORITY,0);
4160 rtl8180_try_wake_queue(dev, BE_PRIORITY);
4162 force_pci_posting(dev);
4163 spin_unlock_irqrestore(&priv->irq_th_lock,flags);
4168 void rtl8180_irq_rx_tasklet(struct r8180_priv* priv)
4170 rtl8180_rx(priv->dev);
4173 void GPIOChangeRFWorkItemCallBack(struct work_struct *work)
4175 struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, GPIOChangeRFWorkItem.work);
4176 struct net_device *dev = ieee->dev;
4177 struct r8180_priv *priv = ieee80211_priv(dev);
4180 RT_RF_POWER_STATE eRfPowerStateToSet;
4181 bool bActuallySet=false;
4184 static char *RadioPowerPath = "/etc/acpi/events/RadioPower.sh";
4185 static char *envp[] = {"HOME=/", "TERM=linux", "PATH=/usr/bin:/bin", NULL};
4186 static int readf_count = 0;
4188 if(readf_count % 10 == 0)
4189 priv->PowerProfile = read_acadapter_file("/proc/acpi/ac_adapter/AC0/state");
4191 readf_count = (readf_count+1)%0xffff;
4192 /* We should turn off LED before polling FF51[4]. */
4195 btPSR = read_nic_byte(dev, PSR);
4196 write_nic_byte(dev, PSR, (btPSR & ~BIT3));
4198 /* It need to delay 4us suggested by Jong, 2008-01-16 */
4201 /* HW radio On/Off according to the value of FF51[4](config0) */
4202 btConfig0 = btPSR = read_nic_byte(dev, CONFIG0);
4204 eRfPowerStateToSet = (btConfig0 & BIT4) ? eRfOn : eRfOff;
4206 /* Turn LED back on when radio enabled */
4207 if (eRfPowerStateToSet == eRfOn)
4208 write_nic_byte(dev, PSR, btPSR | BIT3);
4210 if ((priv->ieee80211->bHwRadioOff == true) &&
4211 (eRfPowerStateToSet == eRfOn)) {
4212 priv->ieee80211->bHwRadioOff = false;
4213 bActuallySet = true;
4214 } else if ((priv->ieee80211->bHwRadioOff == false) &&
4215 (eRfPowerStateToSet == eRfOff)) {
4216 priv->ieee80211->bHwRadioOff = true;
4217 bActuallySet = true;
4221 MgntActSet_RF_State(dev, eRfPowerStateToSet, RF_CHANGE_BY_HW);
4223 /* To update the UI status for Power status changed */
4224 if (priv->ieee80211->bHwRadioOff == true)
4228 argv[0] = RadioPowerPath;
4231 call_usermodehelper(RadioPowerPath, argv, envp, 1);
4235 static u8 read_acadapter_file(char *filename)
4240 module_init(rtl8180_pci_module_init);
4241 module_exit(rtl8180_pci_module_exit);