Merge tag 'trace-3.15' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux...
[pandora-kernel.git] / drivers / staging / rtl8187se / r8180_core.c
1 /*
2  * This is part of rtl818x pci OpenSource driver - v 0.1
3  * Copyright (C) Andrea Merello 2004-2005  <andrea.merello@gmail.com>
4  * Released under the terms of GPL (General Public License)
5  *
6  * Parts of this driver are based on the GPL part of the official
7  * Realtek driver.
8  *
9  * Parts of this driver are based on the rtl8180 driver skeleton
10  * from Patric Schenke & Andres Salomon.
11  *
12  * Parts of this driver are based on the Intel Pro Wireless 2100 GPL driver.
13  *
14  * Parts of BB/RF code are derived from David Young rtl8180 netbsd driver.
15  *
16  * RSSI calc function from 'The Deuce'
17  *
18  * Some ideas borrowed from the 8139too.c driver included in linux kernel.
19  *
20  * We (I?) want to thanks the Authors of those projecs and also the
21  * Ndiswrapper's project Authors.
22  *
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.
25  *
26  * Power management interface routines.
27  * Written by Mariusz Matuszek.
28  */
29
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
32 #undef RX_DONT_PASS_UL
33 #undef DUMMY_RX
34
35 #include <linux/slab.h>
36 #include <linux/syscalls.h>
37 #include <linux/eeprom_93cx6.h>
38 #include <linux/interrupt.h>
39 #include <linux/proc_fs.h>
40 #include <linux/seq_file.h>
41
42 #include "r8180_hw.h"
43 #include "r8180.h"
44 #include "r8180_rtl8225.h" /* RTL8225 Radio frontend */
45 #include "r8180_93cx6.h"   /* Card EEPROM */
46 #include "r8180_wx.h"
47 #include "r8180_dm.h"
48
49 #include "ieee80211/dot11d.h"
50
51 static struct pci_device_id rtl8180_pci_id_tbl[] = {
52         {
53                 .vendor = PCI_VENDOR_ID_REALTEK,
54                 .device = 0x8199,
55                 .subvendor = PCI_ANY_ID,
56                 .subdevice = PCI_ANY_ID,
57                 .driver_data = 0,
58         },
59         {
60                 .vendor = 0,
61                 .device = 0,
62                 .subvendor = 0,
63                 .subdevice = 0,
64                 .driver_data = 0,
65         }
66 };
67
68 static char ifname[IFNAMSIZ] = "wlan%d";
69 static int hwwep;
70
71 MODULE_LICENSE("GPL");
72 MODULE_DEVICE_TABLE(pci, rtl8180_pci_id_tbl);
73 MODULE_AUTHOR("Andrea Merello <andrea.merello@gmail.com>");
74 MODULE_DESCRIPTION("Linux driver for Realtek RTL8187SE WiFi cards");
75
76 module_param_string(ifname, ifname, sizeof(ifname), S_IRUGO|S_IWUSR);
77 module_param(hwwep, int, S_IRUGO|S_IWUSR);
78
79 MODULE_PARM_DESC(hwwep, " Try to use hardware WEP support. Still broken and not available on all cards");
80
81 static int rtl8180_pci_probe(struct pci_dev *pdev,
82                              const struct pci_device_id *id);
83
84 static void rtl8180_pci_remove(struct pci_dev *pdev);
85
86 static void rtl8180_shutdown(struct pci_dev *pdev)
87 {
88         struct net_device *dev = pci_get_drvdata(pdev);
89         if (dev->netdev_ops->ndo_stop)
90                 dev->netdev_ops->ndo_stop(dev);
91         pci_disable_device(pdev);
92 }
93
94 static int rtl8180_suspend(struct pci_dev *pdev, pm_message_t state)
95 {
96         struct net_device *dev = pci_get_drvdata(pdev);
97
98         if (!netif_running(dev))
99                 goto out_pci_suspend;
100
101         if (dev->netdev_ops->ndo_stop)
102                 dev->netdev_ops->ndo_stop(dev);
103
104         netif_device_detach(dev);
105
106 out_pci_suspend:
107         pci_save_state(pdev);
108         pci_disable_device(pdev);
109         pci_set_power_state(pdev, pci_choose_state(pdev, state));
110         return 0;
111 }
112
113 static int rtl8180_resume(struct pci_dev *pdev)
114 {
115         struct net_device *dev = pci_get_drvdata(pdev);
116         int err;
117         u32 val;
118
119         pci_set_power_state(pdev, PCI_D0);
120
121         err = pci_enable_device(pdev);
122         if (err) {
123                 dev_err(&pdev->dev, "pci_enable_device failed on resume\n");
124
125                 return err;
126         }
127
128         pci_restore_state(pdev);
129
130         /*
131          * Suspend/Resume resets the PCI configuration space, so we have to
132          * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
133          * from interfering with C3 CPU state. pci_restore_state won't help
134          * here since it only restores the first 64 bytes pci config header.
135          */
136         pci_read_config_dword(pdev, 0x40, &val);
137         if ((val & 0x0000ff00) != 0)
138                 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
139
140         if (!netif_running(dev))
141                 goto out;
142
143         if (dev->netdev_ops->ndo_open)
144                 dev->netdev_ops->ndo_open(dev);
145
146         netif_device_attach(dev);
147 out:
148         return 0;
149 }
150
151 static struct pci_driver rtl8180_pci_driver = {
152         .name           = RTL8180_MODULE_NAME,
153         .id_table       = rtl8180_pci_id_tbl,
154         .probe          = rtl8180_pci_probe,
155         .remove         = rtl8180_pci_remove,
156         .suspend        = rtl8180_suspend,
157         .resume         = rtl8180_resume,
158         .shutdown       = rtl8180_shutdown,
159 };
160
161 u8 read_nic_byte(struct net_device *dev, int x)
162 {
163         return 0xff&readb((u8 __iomem *)dev->mem_start + x);
164 }
165
166 u32 read_nic_dword(struct net_device *dev, int x)
167 {
168         return readl((u8 __iomem *)dev->mem_start + x);
169 }
170
171 u16 read_nic_word(struct net_device *dev, int x)
172 {
173         return readw((u8 __iomem *)dev->mem_start + x);
174 }
175
176 void write_nic_byte(struct net_device *dev, int x, u8 y)
177 {
178         writeb(y, (u8 __iomem *)dev->mem_start + x);
179         udelay(20);
180 }
181
182 void write_nic_dword(struct net_device *dev, int x, u32 y)
183 {
184         writel(y, (u8 __iomem *)dev->mem_start + x);
185         udelay(20);
186 }
187
188 void write_nic_word(struct net_device *dev, int x, u16 y)
189 {
190         writew(y, (u8 __iomem *)dev->mem_start + x);
191         udelay(20);
192 }
193
194 inline void force_pci_posting(struct net_device *dev)
195 {
196         read_nic_byte(dev, EPROM_CMD);
197         mb();
198 }
199
200 static irqreturn_t rtl8180_interrupt(int irq, void *netdev);
201 void set_nic_rxring(struct net_device *dev);
202 void set_nic_txring(struct net_device *dev);
203 static struct net_device_stats *rtl8180_stats(struct net_device *dev);
204 void rtl8180_commit(struct net_device *dev);
205 void rtl8180_start_tx_beacon(struct net_device *dev);
206
207 static struct proc_dir_entry *rtl8180_proc;
208
209 static int proc_get_registers(struct seq_file *m, void *v)
210 {
211         struct net_device *dev = m->private;
212         int i, n, max = 0xff;
213
214         /* This dump the current register page */
215         for (n = 0; n <= max;) {
216                 seq_printf(m, "\nD:  %2x > ", n);
217
218                 for (i = 0; i < 16 && n <= max; i++, n++)
219                         seq_printf(m, "%2x ", read_nic_byte(dev, n));
220         }
221         seq_putc(m, '\n');
222         return 0;
223 }
224
225 int get_curr_tx_free_desc(struct net_device *dev, int priority);
226
227 static int proc_get_stats_hw(struct seq_file *m, void *v)
228 {
229         return 0;
230 }
231
232 static int proc_get_stats_rx(struct seq_file *m, void *v)
233 {
234         struct net_device *dev = m->private;
235         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
236
237         seq_printf(m,
238                 "RX OK: %lu\n"
239                 "RX Retry: %lu\n"
240                 "RX CRC Error(0-500): %lu\n"
241                 "RX CRC Error(500-1000): %lu\n"
242                 "RX CRC Error(>1000): %lu\n"
243                 "RX ICV Error: %lu\n",
244                 priv->stats.rxint,
245                 priv->stats.rxerr,
246                 priv->stats.rxcrcerrmin,
247                 priv->stats.rxcrcerrmid,
248                 priv->stats.rxcrcerrmax,
249                 priv->stats.rxicverr
250                 );
251
252         return 0;
253 }
254
255 static int proc_get_stats_tx(struct seq_file *m, void *v)
256 {
257         struct net_device *dev = m->private;
258         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
259         unsigned long totalOK;
260
261         totalOK = priv->stats.txnpokint + priv->stats.txhpokint +
262                 priv->stats.txlpokint;
263
264         seq_printf(m,
265                 "TX OK: %lu\n"
266                 "TX Error: %lu\n"
267                 "TX Retry: %lu\n"
268                 "TX beacon OK: %lu\n"
269                 "TX beacon error: %lu\n",
270                 totalOK,
271                 priv->stats.txnperr+priv->stats.txhperr+priv->stats.txlperr,
272                 priv->stats.txretry,
273                 priv->stats.txbeacon,
274                 priv->stats.txbeaconerr
275         );
276
277         return 0;
278 }
279
280 static void rtl8180_proc_module_init(void)
281 {
282         DMESG("Initializing proc filesystem");
283         rtl8180_proc = proc_mkdir(RTL8180_MODULE_NAME, init_net.proc_net);
284 }
285
286 static void rtl8180_proc_module_remove(void)
287 {
288         remove_proc_entry(RTL8180_MODULE_NAME, init_net.proc_net);
289 }
290
291 static void rtl8180_proc_remove_one(struct net_device *dev)
292 {
293         remove_proc_subtree(dev->name, rtl8180_proc);
294 }
295
296 /*
297  * seq_file wrappers for procfile show routines.
298  */
299 static int rtl8180_proc_open(struct inode *inode, struct file *file)
300 {
301         struct net_device *dev = proc_get_parent_data(inode);
302         int (*show)(struct seq_file *, void *) = PDE_DATA(inode);
303
304         return single_open(file, show, dev);
305 }
306
307 static const struct file_operations rtl8180_proc_fops = {
308         .open           = rtl8180_proc_open,
309         .read           = seq_read,
310         .llseek         = seq_lseek,
311         .release        = single_release,
312 };
313
314 /*
315  * Table of proc files we need to create.
316  */
317 struct rtl8180_proc_file {
318         char name[12];
319         int (*show)(struct seq_file *, void *);
320 };
321
322 static const struct rtl8180_proc_file rtl8180_proc_files[] = {
323         { "stats-hw",   &proc_get_stats_hw },
324         { "stats-rx",   &proc_get_stats_rx },
325         { "stats-tx",   &proc_get_stats_tx },
326         { "registers",  &proc_get_registers },
327         { "" }
328 };
329
330 static void rtl8180_proc_init_one(struct net_device *dev)
331 {
332         const struct rtl8180_proc_file *f;
333         struct proc_dir_entry *dir;
334
335         dir = proc_mkdir_data(dev->name, 0, rtl8180_proc, dev);
336         if (!dir) {
337                 DMESGE("Unable to initialize /proc/net/r8180/%s\n", dev->name);
338                 return;
339         }
340
341         for (f = rtl8180_proc_files; f->name[0]; f++) {
342                 if (!proc_create_data(f->name, S_IFREG | S_IRUGO, dir,
343                                       &rtl8180_proc_fops, f->show)) {
344                         DMESGE("Unable to initialize /proc/net/r8180/%s/%s\n",
345                                dev->name, f->name);
346                         return;
347                 }
348         }
349 }
350
351 /*
352  * FIXME: check if we can use some standard already-existent
353  * data type+functions in kernel.
354  */
355
356 static short buffer_add(struct buffer **buffer, u32 *buf, dma_addr_t dma,
357                         struct buffer **bufferhead)
358 {
359         struct buffer *tmp;
360
361         if (!*buffer) {
362
363                 *buffer = kmalloc(sizeof(struct buffer), GFP_KERNEL);
364
365                 if (*buffer == NULL) {
366                         DMESGE("Failed to kmalloc head of TX/RX struct");
367                         return -1;
368                 }
369                 (*buffer)->next = *buffer;
370                 (*buffer)->buf = buf;
371                 (*buffer)->dma = dma;
372                 if (bufferhead != NULL)
373                         (*bufferhead) = (*buffer);
374                 return 0;
375         }
376         tmp = *buffer;
377
378         while (tmp->next != (*buffer))
379                 tmp = tmp->next;
380         tmp->next = kmalloc(sizeof(struct buffer), GFP_KERNEL);
381         if (tmp->next == NULL) {
382                 DMESGE("Failed to kmalloc TX/RX struct");
383                 return -1;
384         }
385         tmp->next->buf = buf;
386         tmp->next->dma = dma;
387         tmp->next->next = *buffer;
388
389         return 0;
390 }
391
392 static void buffer_free(struct net_device *dev, struct buffer **buffer, int len,
393                  short consistent)
394 {
395
396         struct buffer *tmp, *next;
397         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
398         struct pci_dev *pdev = priv->pdev;
399
400         if (!*buffer)
401                 return;
402
403         tmp = *buffer;
404
405         do {
406                 next = tmp->next;
407                 if (consistent) {
408                         pci_free_consistent(pdev, len,
409                                     tmp->buf, tmp->dma);
410                 } else {
411                         pci_unmap_single(pdev, tmp->dma,
412                         len, PCI_DMA_FROMDEVICE);
413                         kfree(tmp->buf);
414                 }
415                 kfree(tmp);
416                 tmp = next;
417         } while (next != *buffer);
418
419         *buffer = NULL;
420 }
421
422 int get_curr_tx_free_desc(struct net_device *dev, int priority)
423 {
424         struct r8180_priv *priv = ieee80211_priv(dev);
425         u32 *tail;
426         u32 *head;
427         int ret;
428
429         switch (priority) {
430         case MANAGE_PRIORITY:
431                 head = priv->txmapringhead;
432                 tail = priv->txmapringtail;
433                 break;
434         case BK_PRIORITY:
435                 head = priv->txbkpringhead;
436                 tail = priv->txbkpringtail;
437                 break;
438         case BE_PRIORITY:
439                 head = priv->txbepringhead;
440                 tail = priv->txbepringtail;
441                 break;
442         case VI_PRIORITY:
443                 head = priv->txvipringhead;
444                 tail = priv->txvipringtail;
445                 break;
446         case VO_PRIORITY:
447                 head = priv->txvopringhead;
448                 tail = priv->txvopringtail;
449                 break;
450         case HI_PRIORITY:
451                 head = priv->txhpringhead;
452                 tail = priv->txhpringtail;
453                 break;
454         default:
455                 return -1;
456         }
457
458         if (head <= tail)
459                 ret = priv->txringcount - (tail - head)/8;
460         else
461                 ret = (head - tail)/8;
462
463         if (ret > priv->txringcount)
464                 DMESG("BUG");
465
466         return ret;
467 }
468
469 static short check_nic_enought_desc(struct net_device *dev, int priority)
470 {
471         struct r8180_priv *priv = ieee80211_priv(dev);
472         struct ieee80211_device *ieee = netdev_priv(dev);
473         int requiredbyte;
474         int required;
475
476         requiredbyte = priv->ieee80211->fts +
477                 sizeof(struct ieee80211_header_data);
478
479         if (ieee->current_network.QoS_Enable)
480                 requiredbyte += 2;
481
482         required = requiredbyte / (priv->txbuffsize-4);
483
484         if (requiredbyte % priv->txbuffsize)
485                 required++;
486
487         /* for now we keep two free descriptor as a safety boundary
488          * between the tail and the head
489          */
490
491         return required + 2 < get_curr_tx_free_desc(dev, priority);
492 }
493
494 void fix_tx_fifo(struct net_device *dev)
495 {
496         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
497         u32 *tmp;
498         int i;
499
500         for (tmp = priv->txmapring, i = 0;
501              i < priv->txringcount;
502              tmp += 8, i++) {
503                 *tmp = *tmp & ~(1<<31);
504         }
505
506         for (tmp = priv->txbkpring, i = 0;
507              i < priv->txringcount;
508              tmp += 8, i++) {
509                 *tmp = *tmp & ~(1<<31);
510         }
511
512         for (tmp = priv->txbepring, i = 0;
513              i < priv->txringcount;
514              tmp += 8, i++) {
515                 *tmp = *tmp & ~(1<<31);
516         }
517         for (tmp = priv->txvipring, i = 0;
518              i < priv->txringcount;
519              tmp += 8, i++) {
520                 *tmp = *tmp & ~(1<<31);
521         }
522
523         for (tmp = priv->txvopring, i = 0;
524              i < priv->txringcount;
525              tmp += 8, i++) {
526                 *tmp = *tmp & ~(1<<31);
527         }
528
529         for (tmp = priv->txhpring, i = 0;
530              i < priv->txringcount;
531              tmp += 8, i++) {
532                 *tmp = *tmp & ~(1<<31);
533         }
534
535         for (tmp = priv->txbeaconring, i = 0;
536              i < priv->txbeaconcount;
537              tmp += 8, i++) {
538                 *tmp = *tmp & ~(1<<31);
539         }
540
541         priv->txmapringtail = priv->txmapring;
542         priv->txmapringhead = priv->txmapring;
543         priv->txmapbufstail = priv->txmapbufs;
544
545         priv->txbkpringtail = priv->txbkpring;
546         priv->txbkpringhead = priv->txbkpring;
547         priv->txbkpbufstail = priv->txbkpbufs;
548
549         priv->txbepringtail = priv->txbepring;
550         priv->txbepringhead = priv->txbepring;
551         priv->txbepbufstail = priv->txbepbufs;
552
553         priv->txvipringtail = priv->txvipring;
554         priv->txvipringhead = priv->txvipring;
555         priv->txvipbufstail = priv->txvipbufs;
556
557         priv->txvopringtail = priv->txvopring;
558         priv->txvopringhead = priv->txvopring;
559         priv->txvopbufstail = priv->txvopbufs;
560
561         priv->txhpringtail = priv->txhpring;
562         priv->txhpringhead = priv->txhpring;
563         priv->txhpbufstail = priv->txhpbufs;
564
565         priv->txbeaconringtail = priv->txbeaconring;
566         priv->txbeaconbufstail = priv->txbeaconbufs;
567         set_nic_txring(dev);
568
569         ieee80211_reset_queue(priv->ieee80211);
570         priv->ack_tx_to_ieee = 0;
571 }
572
573 void fix_rx_fifo(struct net_device *dev)
574 {
575         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
576         u32 *tmp;
577         struct buffer *rxbuf;
578         u8 rx_desc_size;
579
580         rx_desc_size = 8; /* 4*8 = 32 bytes */
581
582         for (tmp = priv->rxring, rxbuf = priv->rxbufferhead;
583              (tmp < (priv->rxring)+(priv->rxringcount)*rx_desc_size);
584              tmp += rx_desc_size, rxbuf = rxbuf->next) {
585                 *(tmp+2) = rxbuf->dma;
586                 *tmp = *tmp & ~0xfff;
587                 *tmp = *tmp | priv->rxbuffersize;
588                 *tmp |= (1<<31);
589         }
590
591         priv->rxringtail = priv->rxring;
592         priv->rxbuffer = priv->rxbufferhead;
593         priv->rx_skb_complete = 1;
594         set_nic_rxring(dev);
595 }
596
597 static void rtl8180_irq_disable(struct net_device *dev)
598 {
599         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
600
601         write_nic_dword(dev, IMR, 0);
602         force_pci_posting(dev);
603         priv->irq_enabled = 0;
604 }
605
606 void rtl8180_set_mode(struct net_device *dev, int mode)
607 {
608         u8 ecmd;
609
610         ecmd = read_nic_byte(dev, EPROM_CMD);
611         ecmd = ecmd & ~EPROM_CMD_OPERATING_MODE_MASK;
612         ecmd = ecmd | (mode<<EPROM_CMD_OPERATING_MODE_SHIFT);
613         ecmd = ecmd & ~(1<<EPROM_CS_SHIFT);
614         ecmd = ecmd & ~(1<<EPROM_CK_SHIFT);
615         write_nic_byte(dev, EPROM_CMD, ecmd);
616 }
617
618 void rtl8180_beacon_tx_enable(struct net_device *dev);
619
620 void rtl8180_update_msr(struct net_device *dev)
621 {
622         struct r8180_priv *priv = ieee80211_priv(dev);
623         u8 msr;
624         u32 rxconf;
625
626         msr  = read_nic_byte(dev, MSR);
627         msr &= ~MSR_LINK_MASK;
628
629         rxconf = read_nic_dword(dev, RX_CONF);
630
631         if (priv->ieee80211->state == IEEE80211_LINKED) {
632                 if (priv->ieee80211->iw_mode == IW_MODE_ADHOC)
633                         msr |= (MSR_LINK_ADHOC<<MSR_LINK_SHIFT);
634                 else if (priv->ieee80211->iw_mode == IW_MODE_MASTER)
635                         msr |= (MSR_LINK_MASTER<<MSR_LINK_SHIFT);
636                 else if (priv->ieee80211->iw_mode == IW_MODE_INFRA)
637                         msr |= (MSR_LINK_MANAGED<<MSR_LINK_SHIFT);
638                 else
639                         msr |= (MSR_LINK_NONE<<MSR_LINK_SHIFT);
640                 rxconf |= (1<<RX_CHECK_BSSID_SHIFT);
641
642         } else {
643                 msr |= (MSR_LINK_NONE<<MSR_LINK_SHIFT);
644                 rxconf &= ~(1<<RX_CHECK_BSSID_SHIFT);
645         }
646
647         write_nic_byte(dev, MSR, msr);
648         write_nic_dword(dev, RX_CONF, rxconf);
649 }
650
651 void rtl8180_set_chan(struct net_device *dev, short ch)
652 {
653         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
654
655         if ((ch > 14) || (ch < 1)) {
656                 netdev_err(dev, "In %s: Invalid channel %d\n", __func__, ch);
657                 return;
658         }
659
660         priv->chan = ch;
661         priv->rf_set_chan(dev, priv->chan);
662 }
663
664 void set_nic_txring(struct net_device *dev)
665 {
666         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
667
668         write_nic_dword(dev, TX_MANAGEPRIORITY_RING_ADDR, priv->txmapringdma);
669         write_nic_dword(dev, TX_BKPRIORITY_RING_ADDR, priv->txbkpringdma);
670         write_nic_dword(dev, TX_BEPRIORITY_RING_ADDR, priv->txbepringdma);
671         write_nic_dword(dev, TX_VIPRIORITY_RING_ADDR, priv->txvipringdma);
672         write_nic_dword(dev, TX_VOPRIORITY_RING_ADDR, priv->txvopringdma);
673         write_nic_dword(dev, TX_HIGHPRIORITY_RING_ADDR, priv->txhpringdma);
674         write_nic_dword(dev, TX_BEACON_RING_ADDR, priv->txbeaconringdma);
675 }
676
677 void rtl8180_beacon_tx_enable(struct net_device *dev)
678 {
679         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
680
681         rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
682         priv->dma_poll_stop_mask &= ~(TPPOLLSTOP_BQ);
683         write_nic_byte(dev, TPPollStop, priv->dma_poll_mask);
684         rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
685 }
686
687 void rtl8180_beacon_tx_disable(struct net_device *dev)
688 {
689         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
690
691         rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
692         priv->dma_poll_stop_mask |= TPPOLLSTOP_BQ;
693         write_nic_byte(dev, TPPollStop, priv->dma_poll_stop_mask);
694         rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
695
696 }
697
698 void rtl8180_rtx_disable(struct net_device *dev)
699 {
700         u8 cmd;
701         struct r8180_priv *priv = ieee80211_priv(dev);
702
703         cmd = read_nic_byte(dev, CMD);
704         write_nic_byte(dev, CMD, cmd &
705                        ~((1<<CMD_RX_ENABLE_SHIFT)|(1<<CMD_TX_ENABLE_SHIFT)));
706         force_pci_posting(dev);
707         mdelay(10);
708
709         if (!priv->rx_skb_complete)
710                 dev_kfree_skb_any(priv->rx_skb);
711 }
712
713 static short alloc_tx_desc_ring(struct net_device *dev, int bufsize, int count,
714                                 int addr)
715 {
716         int i;
717         u32 *desc;
718         u32 *tmp;
719         dma_addr_t dma_desc, dma_tmp;
720         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
721         struct pci_dev *pdev = priv->pdev;
722         void *buf;
723
724         if ((bufsize & 0xfff) != bufsize) {
725                 DMESGE("TX buffer allocation too large");
726                 return 0;
727         }
728         desc = (u32 *)pci_alloc_consistent(pdev,
729                                           sizeof(u32)*8*count+256, &dma_desc);
730         if (desc == NULL)
731                 return -1;
732
733         if (dma_desc & 0xff)
734                 /*
735                  * descriptor's buffer must be 256 byte aligned
736                  * we shouldn't be here, since we set DMA mask !
737                  */
738                 WARN(1, "DMA buffer is not aligned\n");
739
740         tmp = desc;
741
742         for (i = 0; i < count; i++) {
743                 buf = (void *)pci_alloc_consistent(pdev, bufsize, &dma_tmp);
744                 if (buf == NULL)
745                         return -ENOMEM;
746
747                 switch (addr) {
748                 case TX_MANAGEPRIORITY_RING_ADDR:
749                         if (-1 == buffer_add(&priv->txmapbufs,
750                                 buf, dma_tmp, NULL)) {
751                                 DMESGE("Unable to allocate mem for buffer NP");
752                                 return -ENOMEM;
753                         }
754                         break;
755                 case TX_BKPRIORITY_RING_ADDR:
756                         if (-1 == buffer_add(&priv->txbkpbufs,
757                                 buf, dma_tmp, NULL)) {
758                                 DMESGE("Unable to allocate mem for buffer LP");
759                                 return -ENOMEM;
760                         }
761                         break;
762                 case TX_BEPRIORITY_RING_ADDR:
763                         if (-1 == buffer_add(&priv->txbepbufs,
764                                 buf, dma_tmp, NULL)) {
765                                 DMESGE("Unable to allocate mem for buffer NP");
766                                 return -ENOMEM;
767                         }
768                         break;
769                 case TX_VIPRIORITY_RING_ADDR:
770                         if (-1 == buffer_add(&priv->txvipbufs,
771                                 buf, dma_tmp, NULL)) {
772                                 DMESGE("Unable to allocate mem for buffer LP");
773                                 return -ENOMEM;
774                         }
775                         break;
776                 case TX_VOPRIORITY_RING_ADDR:
777                         if (-1 == buffer_add(&priv->txvopbufs,
778                                 buf, dma_tmp, NULL)) {
779                                 DMESGE("Unable to allocate mem for buffer NP");
780                                 return -ENOMEM;
781                         }
782                         break;
783                 case TX_HIGHPRIORITY_RING_ADDR:
784                         if (-1 == buffer_add(&priv->txhpbufs,
785                                 buf, dma_tmp, NULL)) {
786                                 DMESGE("Unable to allocate mem for buffer HP");
787                                 return -ENOMEM;
788                         }
789                         break;
790                 case TX_BEACON_RING_ADDR:
791                         if (-1 == buffer_add(&priv->txbeaconbufs,
792                                 buf, dma_tmp, NULL)) {
793                                 DMESGE("Unable to allocate mem for buffer BP");
794                                 return -ENOMEM;
795                         }
796                         break;
797                 }
798                 *tmp = *tmp & ~(1<<31); /* descriptor empty, owned by the drv */
799                 *(tmp+2) = (u32)dma_tmp;
800                 *(tmp+3) = bufsize;
801
802                 if (i+1 < count)
803                         *(tmp+4) = (u32)dma_desc+((i+1)*8*4);
804                 else
805                         *(tmp+4) = (u32)dma_desc;
806
807                 tmp = tmp+8;
808         }
809
810         switch (addr) {
811         case TX_MANAGEPRIORITY_RING_ADDR:
812                 priv->txmapringdma = dma_desc;
813                 priv->txmapring = desc;
814                 break;
815         case TX_BKPRIORITY_RING_ADDR:
816                 priv->txbkpringdma = dma_desc;
817                 priv->txbkpring = desc;
818                 break;
819         case TX_BEPRIORITY_RING_ADDR:
820                 priv->txbepringdma = dma_desc;
821                 priv->txbepring = desc;
822                 break;
823         case TX_VIPRIORITY_RING_ADDR:
824                 priv->txvipringdma = dma_desc;
825                 priv->txvipring = desc;
826                 break;
827         case TX_VOPRIORITY_RING_ADDR:
828                 priv->txvopringdma = dma_desc;
829                 priv->txvopring = desc;
830                 break;
831         case TX_HIGHPRIORITY_RING_ADDR:
832                 priv->txhpringdma = dma_desc;
833                 priv->txhpring = desc;
834                 break;
835         case TX_BEACON_RING_ADDR:
836                 priv->txbeaconringdma = dma_desc;
837                 priv->txbeaconring = desc;
838                 break;
839
840         }
841
842         return 0;
843 }
844
845 static void free_tx_desc_rings(struct net_device *dev)
846 {
847         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
848         struct pci_dev *pdev = priv->pdev;
849         int count = priv->txringcount;
850
851         pci_free_consistent(pdev, sizeof(u32)*8*count+256,
852                             priv->txmapring, priv->txmapringdma);
853         buffer_free(dev, &(priv->txmapbufs), priv->txbuffsize, 1);
854
855         pci_free_consistent(pdev, sizeof(u32)*8*count+256,
856                             priv->txbkpring, priv->txbkpringdma);
857         buffer_free(dev, &(priv->txbkpbufs), priv->txbuffsize, 1);
858
859         pci_free_consistent(pdev, sizeof(u32)*8*count+256,
860                             priv->txbepring, priv->txbepringdma);
861         buffer_free(dev, &(priv->txbepbufs), priv->txbuffsize, 1);
862
863         pci_free_consistent(pdev, sizeof(u32)*8*count+256,
864                             priv->txvipring, priv->txvipringdma);
865         buffer_free(dev, &(priv->txvipbufs), priv->txbuffsize, 1);
866
867         pci_free_consistent(pdev, sizeof(u32)*8*count+256,
868                             priv->txvopring, priv->txvopringdma);
869         buffer_free(dev, &(priv->txvopbufs), priv->txbuffsize, 1);
870
871         pci_free_consistent(pdev, sizeof(u32)*8*count+256,
872                             priv->txhpring, priv->txhpringdma);
873         buffer_free(dev, &(priv->txhpbufs), priv->txbuffsize, 1);
874
875         count = priv->txbeaconcount;
876         pci_free_consistent(pdev, sizeof(u32)*8*count+256,
877                             priv->txbeaconring, priv->txbeaconringdma);
878         buffer_free(dev, &(priv->txbeaconbufs), priv->txbuffsize, 1);
879 }
880
881 static void free_rx_desc_ring(struct net_device *dev)
882 {
883         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
884         struct pci_dev *pdev = priv->pdev;
885         int count = priv->rxringcount;
886
887         pci_free_consistent(pdev, sizeof(u32)*8*count+256,
888                             priv->rxring, priv->rxringdma);
889
890         buffer_free(dev, &(priv->rxbuffer), priv->rxbuffersize, 0);
891 }
892
893 static short alloc_rx_desc_ring(struct net_device *dev, u16 bufsize, int count)
894 {
895         int i;
896         u32 *desc;
897         u32 *tmp;
898         dma_addr_t dma_desc, dma_tmp;
899         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
900         struct pci_dev *pdev = priv->pdev;
901         void *buf;
902         u8 rx_desc_size;
903
904         rx_desc_size = 8; /* 4*8 = 32 bytes */
905
906         if ((bufsize & 0xfff) != bufsize) {
907                 DMESGE("RX buffer allocation too large");
908                 return -1;
909         }
910
911         desc = (u32 *)pci_alloc_consistent(pdev,
912                 sizeof(u32) * rx_desc_size * count + 256, &dma_desc);
913
914         if (dma_desc & 0xff)
915                 /*
916                  * descriptor's buffer must be 256 byte aligned
917                  * should never happen since we specify the DMA mask
918                  */
919                 WARN(1, "DMA buffer is not aligned\n");
920
921         priv->rxring = desc;
922         priv->rxringdma = dma_desc;
923         tmp = desc;
924
925         for (i = 0; i < count; i++) {
926                 buf = kmalloc(bufsize * sizeof(u8), GFP_ATOMIC);
927                 if (buf == NULL) {
928                         DMESGE("Failed to kmalloc RX buffer");
929                         return -1;
930                 }
931
932                 dma_tmp = pci_map_single(pdev, buf, bufsize * sizeof(u8),
933                                          PCI_DMA_FROMDEVICE);
934                 if (pci_dma_mapping_error(pdev, dma_tmp))
935                         return -1;
936                 if (-1 == buffer_add(&(priv->rxbuffer), buf, dma_tmp,
937                            &(priv->rxbufferhead))) {
938                         DMESGE("Unable to allocate mem RX buf");
939                         return -1;
940                 }
941                 *tmp = 0; /* zero pads the header of the descriptor */
942                 *tmp = *tmp | (bufsize&0xfff);
943                 *(tmp+2) = (u32)dma_tmp;
944                 *tmp = *tmp | (1<<31); /* descriptor void, owned by the NIC */
945
946                 tmp = tmp+rx_desc_size;
947         }
948
949         /* this is the last descriptor */
950         *(tmp - rx_desc_size) = *(tmp - rx_desc_size) | (1 << 30);
951
952         return 0;
953 }
954
955
956 void set_nic_rxring(struct net_device *dev)
957 {
958         u8 pgreg;
959         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
960
961         pgreg = read_nic_byte(dev, PGSELECT);
962         write_nic_byte(dev, PGSELECT, pgreg & ~(1<<PGSELECT_PG_SHIFT));
963
964         write_nic_dword(dev, RXRING_ADDR, priv->rxringdma);
965 }
966
967 void rtl8180_reset(struct net_device *dev)
968 {
969         u8 cr;
970
971         rtl8180_irq_disable(dev);
972
973         cr = read_nic_byte(dev, CMD);
974         cr = cr & 2;
975         cr = cr | (1<<CMD_RST_SHIFT);
976         write_nic_byte(dev, CMD, cr);
977
978         force_pci_posting(dev);
979
980         mdelay(200);
981
982         if (read_nic_byte(dev, CMD) & (1<<CMD_RST_SHIFT))
983                 DMESGW("Card reset timeout!");
984         else
985                 DMESG("Card successfully reset");
986
987         rtl8180_set_mode(dev, EPROM_CMD_LOAD);
988         force_pci_posting(dev);
989         mdelay(200);
990 }
991
992 inline u16 ieeerate2rtlrate(int rate)
993 {
994         switch (rate) {
995         case 10:
996                 return 0;
997         case 20:
998                 return 1;
999         case 55:
1000                 return 2;
1001         case 110:
1002                 return 3;
1003         case 60:
1004                 return 4;
1005         case 90:
1006                 return 5;
1007         case 120:
1008                 return 6;
1009         case 180:
1010                 return 7;
1011         case 240:
1012                 return 8;
1013         case 360:
1014                 return 9;
1015         case 480:
1016                 return 10;
1017         case 540:
1018                 return 11;
1019         default:
1020                 return 3;
1021         }
1022 }
1023
1024 static u16 rtl_rate[] = {10, 20, 55, 110, 60,
1025         90, 120, 180, 240, 360, 480, 540, 720};
1026
1027 inline u16 rtl8180_rate2rate(short rate)
1028 {
1029         if (rate > 12)
1030                 return 10;
1031         return rtl_rate[rate];
1032 }
1033
1034 inline u8 rtl8180_IsWirelessBMode(u16 rate)
1035 {
1036         if (((rate <= 110) && (rate != 60) && (rate != 90)) || (rate == 220))
1037                 return 1;
1038         else
1039                 return 0;
1040 }
1041
1042 u16 N_DBPSOfRate(u16 DataRate);
1043
1044 static u16 ComputeTxTime(u16 FrameLength, u16 DataRate, u8 bManagementFrame,
1045                   u8 bShortPreamble)
1046 {
1047         u16     FrameTime;
1048         u16     N_DBPS;
1049         u16     Ceiling;
1050
1051         if (rtl8180_IsWirelessBMode(DataRate)) {
1052                 if (bManagementFrame || !bShortPreamble || DataRate == 10)
1053                         /* long preamble */
1054                         FrameTime = (u16)(144+48+(FrameLength*8/(DataRate/10)));
1055                 else
1056                         /* short preamble */
1057                         FrameTime = (u16)(72+24+(FrameLength*8/(DataRate/10)));
1058
1059                 if ((FrameLength*8 % (DataRate/10)) != 0) /* get the ceilling */
1060                         FrameTime++;
1061         } else {        /* 802.11g DSSS-OFDM PLCP length field calculation. */
1062                 N_DBPS = N_DBPSOfRate(DataRate);
1063                 Ceiling = (16 + 8*FrameLength + 6) / N_DBPS
1064                                 + (((16 + 8*FrameLength + 6) % N_DBPS) ? 1 : 0);
1065                 FrameTime = (u16)(16 + 4 + 4*Ceiling + 6);
1066         }
1067         return FrameTime;
1068 }
1069
1070 u16 N_DBPSOfRate(u16 DataRate)
1071 {
1072          u16 N_DBPS = 24;
1073
1074         switch (DataRate) {
1075         case 60:
1076                 N_DBPS = 24;
1077                 break;
1078         case 90:
1079                 N_DBPS = 36;
1080                 break;
1081         case 120:
1082                 N_DBPS = 48;
1083                 break;
1084         case 180:
1085                 N_DBPS = 72;
1086                 break;
1087         case 240:
1088                 N_DBPS = 96;
1089                 break;
1090         case 360:
1091                 N_DBPS = 144;
1092                 break;
1093         case 480:
1094                 N_DBPS = 192;
1095                 break;
1096         case 540:
1097                 N_DBPS = 216;
1098                 break;
1099         default:
1100                 break;
1101         }
1102
1103         return N_DBPS;
1104 }
1105
1106 /*
1107  * For Netgear case, they want good-looking signal strength.
1108  */
1109 static long NetgearSignalStrengthTranslate(long LastSS, long CurrSS)
1110 {
1111         long RetSS;
1112
1113         /* Step 1. Scale mapping. */
1114         if (CurrSS >= 71 && CurrSS <= 100)
1115                 RetSS = 90 + ((CurrSS - 70) / 3);
1116         else if (CurrSS >= 41 && CurrSS <= 70)
1117                 RetSS = 78 + ((CurrSS - 40) / 3);
1118         else if (CurrSS >= 31 && CurrSS <= 40)
1119                 RetSS = 66 + (CurrSS - 30);
1120         else if (CurrSS >= 21 && CurrSS <= 30)
1121                 RetSS = 54 + (CurrSS - 20);
1122         else if (CurrSS >= 5 && CurrSS <= 20)
1123                 RetSS = 42 + (((CurrSS - 5) * 2) / 3);
1124         else if (CurrSS == 4)
1125                 RetSS = 36;
1126         else if (CurrSS == 3)
1127                 RetSS = 27;
1128         else if (CurrSS == 2)
1129                 RetSS = 18;
1130         else if (CurrSS == 1)
1131                 RetSS = 9;
1132         else
1133                 RetSS = CurrSS;
1134
1135         /* Step 2. Smoothing. */
1136         if (LastSS > 0)
1137                 RetSS = ((LastSS * 5) + (RetSS) + 5) / 6;
1138
1139         return RetSS;
1140 }
1141
1142 /*
1143  * Translate 0-100 signal strength index into dBm.
1144  */
1145 static long TranslateToDbm8185(u8 SignalStrengthIndex)
1146 {
1147         long SignalPower;
1148
1149         /* Translate to dBm (x=0.5y-95). */
1150         SignalPower = (long)((SignalStrengthIndex + 1) >> 1);
1151         SignalPower -= 95;
1152
1153         return SignalPower;
1154 }
1155
1156 /*
1157  * Perform signal smoothing for dynamic mechanism.
1158  * This is different with PerformSignalSmoothing8185 in smoothing formula.
1159  * No dramatic adjustment is applied because dynamic mechanism need some
1160  * degree of correctness. Ported from 8187B.
1161  */
1162 static void PerformUndecoratedSignalSmoothing8185(struct r8180_priv *priv,
1163                                                   bool bCckRate)
1164 {
1165         long smoothedSS;
1166         long smoothedRx;
1167
1168         /* Determine the current packet is CCK rate. */
1169         priv->bCurCCKPkt = bCckRate;
1170
1171         smoothedSS = priv->SignalStrength * 10;
1172
1173         if (priv->UndecoratedSmoothedSS >= 0)
1174                 smoothedSS = ((priv->UndecoratedSmoothedSS * 5) +
1175                                 smoothedSS) / 6;
1176
1177         priv->UndecoratedSmoothedSS = smoothedSS;
1178
1179         smoothedRx = ((priv->UndecoratedSmoothedRxPower * 50) +
1180                         (priv->RxPower * 11)) / 60;
1181
1182         priv->UndecoratedSmoothedRxPower = smoothedRx;
1183
1184         if (bCckRate)
1185                 priv->CurCCKRSSI = priv->RSSI;
1186         else
1187                 priv->CurCCKRSSI = 0;
1188 }
1189
1190
1191 /*
1192  * This is rough RX isr handling routine
1193  */
1194 static void rtl8180_rx(struct net_device *dev)
1195 {
1196         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1197         struct sk_buff *tmp_skb;
1198         short first, last;
1199         u32 len;
1200         int lastlen;
1201         unsigned char quality, signal;
1202         u8 rate;
1203         u32 *tmp, *tmp2;
1204         u8 rx_desc_size;
1205         u8 padding;
1206         char rxpower = 0;
1207         u32 RXAGC = 0;
1208         long RxAGC_dBm = 0;
1209         u8      LNA = 0, BB = 0;
1210         u8      LNA_gain[4] = {02, 17, 29, 39};
1211         u8  Antenna = 0;
1212         struct ieee80211_hdr_4addr *hdr;
1213         u16 fc, type;
1214         u8 bHwError = 0, bCRC = 0, bICV = 0;
1215         bool    bCckRate = false;
1216         u8     RSSI = 0;
1217         long    SignalStrengthIndex = 0;
1218         struct ieee80211_rx_stats stats = {
1219                 .signal = 0,
1220                 .noise = -98,
1221                 .rate = 0,
1222                 .freq = IEEE80211_24GHZ_BAND,
1223         };
1224
1225         stats.nic_type = NIC_8185B;
1226         rx_desc_size = 8;
1227
1228         if ((*(priv->rxringtail)) & (1<<31)) {
1229                 /* we have got an RX int, but the descriptor. we are pointing
1230                  * is empty.
1231                  */
1232
1233                 priv->stats.rxnodata++;
1234                 priv->ieee80211->stats.rx_errors++;
1235
1236                 tmp2 = NULL;
1237                 tmp = priv->rxringtail;
1238                 do {
1239                         if (tmp == priv->rxring)
1240                                 tmp  = priv->rxring + (priv->rxringcount - 1) *
1241                                         rx_desc_size;
1242                         else
1243                                 tmp -= rx_desc_size;
1244
1245                         if (!(*tmp & (1<<31)))
1246                                 tmp2 = tmp;
1247                 } while (tmp != priv->rxring);
1248
1249                 if (tmp2)
1250                         priv->rxringtail = tmp2;
1251         }
1252
1253         /* while there are filled descriptors */
1254         while (!(*(priv->rxringtail) & (1<<31))) {
1255                 if (*(priv->rxringtail) & (1<<26))
1256                         DMESGW("RX buffer overflow");
1257                 if (*(priv->rxringtail) & (1<<12))
1258                         priv->stats.rxicverr++;
1259
1260                 if (*(priv->rxringtail) & (1<<27)) {
1261                         priv->stats.rxdmafail++;
1262                         goto drop;
1263                 }
1264
1265                 pci_dma_sync_single_for_cpu(priv->pdev,
1266                                     priv->rxbuffer->dma,
1267                                     priv->rxbuffersize * sizeof(u8),
1268                                     PCI_DMA_FROMDEVICE);
1269
1270                 first = *(priv->rxringtail) & (1<<29) ? 1 : 0;
1271                 if (first)
1272                         priv->rx_prevlen = 0;
1273
1274                 last = *(priv->rxringtail) & (1<<28) ? 1 : 0;
1275                 if (last) {
1276                         lastlen = ((*priv->rxringtail) & 0xfff);
1277
1278                         /* if the last descriptor (that should tell us the total
1279                          * packet len) tell us something less than the
1280                          * descriptors len we had until now, then there is some
1281                          * problem..
1282                          * workaround to prevent kernel panic
1283                          */
1284                         if (lastlen < priv->rx_prevlen)
1285                                 len = 0;
1286                         else
1287                                 len = lastlen-priv->rx_prevlen;
1288
1289                         if (*(priv->rxringtail) & (1<<13)) {
1290                                 if ((*(priv->rxringtail) & 0xfff) < 500)
1291                                         priv->stats.rxcrcerrmin++;
1292                                 else if ((*(priv->rxringtail) & 0x0fff) > 1000)
1293                                         priv->stats.rxcrcerrmax++;
1294                                 else
1295                                         priv->stats.rxcrcerrmid++;
1296
1297                         }
1298
1299                 } else {
1300                         len = priv->rxbuffersize;
1301                 }
1302
1303                 if (first && last) {
1304                         padding = ((*(priv->rxringtail+3))&(0x04000000))>>26;
1305                 } else if (first) {
1306                         padding = ((*(priv->rxringtail+3))&(0x04000000))>>26;
1307                         if (padding)
1308                                 len -= 2;
1309                 } else {
1310                         padding = 0;
1311                 }
1312                 padding = 0;
1313                 priv->rx_prevlen += len;
1314
1315                 if (priv->rx_prevlen > MAX_FRAG_THRESHOLD + 100) {
1316                         /* HW is probably passing several buggy frames without
1317                          * FD or LD flag set.
1318                          * Throw this garbage away to prevent skb memory
1319                          * exhausting
1320                          */
1321                         if (!priv->rx_skb_complete)
1322                                 dev_kfree_skb_any(priv->rx_skb);
1323                         priv->rx_skb_complete = 1;
1324                 }
1325
1326                 signal = (unsigned char)((*(priv->rxringtail + 3) &
1327                         0x00ff0000) >> 16);
1328                 signal = (signal & 0xfe) >> 1;
1329
1330                 quality = (unsigned char)((*(priv->rxringtail+3)) & (0xff));
1331
1332                 stats.mac_time[0] = *(priv->rxringtail+1);
1333                 stats.mac_time[1] = *(priv->rxringtail+2);
1334
1335                 rxpower = ((char)((*(priv->rxringtail + 4) &
1336                         0x00ff0000) >> 16)) / 2 - 42;
1337
1338                 RSSI = ((u8)((*(priv->rxringtail + 3) &
1339                         0x0000ff00) >> 8)) & 0x7f;
1340
1341                 rate = ((*(priv->rxringtail)) &
1342                         ((1<<23)|(1<<22)|(1<<21)|(1<<20)))>>20;
1343
1344                 stats.rate = rtl8180_rate2rate(rate);
1345                 Antenna = (*(priv->rxringtail + 3) & 0x00008000) == 0 ? 0 : 1;
1346                 if (!rtl8180_IsWirelessBMode(stats.rate)) { /* OFDM rate. */
1347                         RxAGC_dBm = rxpower+1;  /* bias */
1348                 } else { /* CCK rate. */
1349                         RxAGC_dBm = signal; /* bit 0 discard */
1350
1351                         LNA = (u8) (RxAGC_dBm & 0x60) >> 5; /* bit 6~ bit 5 */
1352                         BB  = (u8) (RxAGC_dBm & 0x1F); /* bit 4 ~ bit 0 */
1353
1354                         /* Pin_11b=-(LNA_gain+BB_gain) (dBm) */
1355                         RxAGC_dBm = -(LNA_gain[LNA] + (BB * 2));
1356
1357                         RxAGC_dBm += 4; /* bias */
1358                 }
1359
1360                 if (RxAGC_dBm & 0x80) /* absolute value */
1361                         RXAGC = ~(RxAGC_dBm)+1;
1362                 bCckRate = rtl8180_IsWirelessBMode(stats.rate);
1363                 /* Translate RXAGC into 1-100. */
1364                 if (!rtl8180_IsWirelessBMode(stats.rate)) { /* OFDM rate. */
1365                         if (RXAGC > 90)
1366                                 RXAGC = 90;
1367                         else if (RXAGC < 25)
1368                                 RXAGC = 25;
1369                         RXAGC = (90-RXAGC)*100/65;
1370                 } else { /* CCK rate. */
1371                         if (RXAGC > 95)
1372                                 RXAGC = 95;
1373                         else if (RXAGC < 30)
1374                                 RXAGC = 30;
1375                         RXAGC = (95-RXAGC)*100/65;
1376                 }
1377                 priv->SignalStrength = (u8)RXAGC;
1378                 priv->RecvSignalPower = RxAGC_dBm;
1379                 priv->RxPower = rxpower;
1380                 priv->RSSI = RSSI;
1381                 /* SQ translation formula is provided by SD3 DZ. 2006.06.27 */
1382                 if (quality >= 127)
1383                         /* 0 causes epc to show signal zero, walk around now */
1384                         quality = 1;
1385                 else if (quality < 27)
1386                         quality = 100;
1387                 else
1388                         quality = 127 - quality;
1389                 priv->SignalQuality = quality;
1390
1391                 stats.signal = (u8) quality;
1392
1393                 stats.signalstrength = RXAGC;
1394                 if (stats.signalstrength > 100)
1395                         stats.signalstrength = 100;
1396                 stats.signalstrength = (stats.signalstrength * 70) / 100 + 30;
1397                 stats.rssi = priv->wstats.qual.qual = priv->SignalQuality;
1398                 stats.noise = priv->wstats.qual.noise =
1399                         100 - priv->wstats.qual.qual;
1400                 bHwError = (((*(priv->rxringtail)) & (0x00000fff)) == 4080) |
1401                            (((*(priv->rxringtail)) & (0x04000000)) != 0) |
1402                            (((*(priv->rxringtail)) & (0x08000000)) != 0) |
1403                            (((~(*(priv->rxringtail))) & (0x10000000)) != 0) |
1404                            (((~(*(priv->rxringtail))) & (0x20000000)) != 0);
1405                 bCRC = ((*(priv->rxringtail)) & (0x00002000)) >> 13;
1406                 bICV = ((*(priv->rxringtail)) & (0x00001000)) >> 12;
1407                 hdr = (struct ieee80211_hdr_4addr *)priv->rxbuffer->buf;
1408                     fc = le16_to_cpu(hdr->frame_ctl);
1409                 type = WLAN_FC_GET_TYPE(fc);
1410
1411                 if (IEEE80211_FTYPE_CTL != type &&
1412                     !bHwError && !bCRC && !bICV &&
1413                     eqMacAddr(priv->ieee80211->current_network.bssid,
1414                         fc & IEEE80211_FCTL_TODS ? hdr->addr1 :
1415                         fc & IEEE80211_FCTL_FROMDS ? hdr->addr2 :
1416                         hdr->addr3)) {
1417
1418                         /* Perform signal smoothing for dynamic
1419                          * mechanism on demand. This is different
1420                          * with PerformSignalSmoothing8185 in smoothing
1421                          * fomula. No dramatic adjustion is apply
1422                          * because dynamic mechanism need some degree
1423                          * of correctness. */
1424                         PerformUndecoratedSignalSmoothing8185(priv, bCckRate);
1425
1426                         /* For good-looking singal strength. */
1427                         SignalStrengthIndex = NetgearSignalStrengthTranslate(
1428                                 priv->LastSignalStrengthInPercent,
1429                                 priv->SignalStrength);
1430
1431                         priv->LastSignalStrengthInPercent = SignalStrengthIndex;
1432                         priv->Stats_SignalStrength =
1433                                 TranslateToDbm8185((u8)SignalStrengthIndex);
1434
1435                         /*
1436                          * We need more correct power of received packets and
1437                          * the "SignalStrength" of RxStats is beautified, so we
1438                          * record the correct power here.
1439                          */
1440
1441                         priv->Stats_SignalQuality = (long)(
1442                                 priv->Stats_SignalQuality * 5 +
1443                                 (long)priv->SignalQuality + 5) / 6;
1444
1445                         priv->Stats_RecvSignalPower = (long)(
1446                                 priv->Stats_RecvSignalPower * 5 +
1447                                 priv->RecvSignalPower - 1) / 6;
1448
1449                         /*
1450                          * Figure out which antenna received the last packet.
1451                          * 0: aux, 1: main
1452                          */
1453                         priv->LastRxPktAntenna = Antenna ? 1 : 0;
1454                         SwAntennaDiversityRxOk8185(dev, priv->SignalStrength);
1455                 }
1456
1457                 if (first) {
1458                         if (!priv->rx_skb_complete) {
1459                                 /* seems that HW sometimes fails to receive and
1460                                  * doesn't provide the last descriptor.
1461                                  */
1462                                 dev_kfree_skb_any(priv->rx_skb);
1463                                 priv->stats.rxnolast++;
1464                         }
1465                         priv->rx_skb = dev_alloc_skb(len+2);
1466                         if (!priv->rx_skb)
1467                                 goto drop;
1468
1469                         priv->rx_skb_complete = 0;
1470                         priv->rx_skb->dev = dev;
1471                 } else {
1472                         /* if we are here we should have already RXed the first
1473                          * frame.
1474                          * If we get here and the skb is not allocated then
1475                          * we have just throw out garbage (skb not allocated)
1476                          * and we are still rxing garbage....
1477                          */
1478                         if (!priv->rx_skb_complete) {
1479
1480                                 tmp_skb = dev_alloc_skb(
1481                                         priv->rx_skb->len + len + 2);
1482
1483                                 if (!tmp_skb)
1484                                         goto drop;
1485
1486                                 tmp_skb->dev = dev;
1487
1488                                 memcpy(skb_put(tmp_skb, priv->rx_skb->len),
1489                                         priv->rx_skb->data,
1490                                         priv->rx_skb->len);
1491
1492                                 dev_kfree_skb_any(priv->rx_skb);
1493
1494                                 priv->rx_skb = tmp_skb;
1495                         }
1496                 }
1497
1498                 if (!priv->rx_skb_complete) {
1499                         memcpy(skb_put(priv->rx_skb, len), ((unsigned char *)
1500                                 priv->rxbuffer->buf) + (padding ? 2 : 0), len);
1501                 }
1502
1503                 if (last && !priv->rx_skb_complete) {
1504                         if (priv->rx_skb->len > 4)
1505                                 skb_trim(priv->rx_skb, priv->rx_skb->len-4);
1506                         if (!ieee80211_rtl_rx(priv->ieee80211,
1507                                          priv->rx_skb, &stats))
1508                                 dev_kfree_skb_any(priv->rx_skb);
1509                         priv->rx_skb_complete = 1;
1510                 }
1511
1512                 pci_dma_sync_single_for_device(priv->pdev,
1513                                     priv->rxbuffer->dma,
1514                                     priv->rxbuffersize * sizeof(u8),
1515                                     PCI_DMA_FROMDEVICE);
1516
1517 drop: /* this is used when we have not enough mem */
1518                 /* restore the descriptor */
1519                 *(priv->rxringtail+2) = priv->rxbuffer->dma;
1520                 *(priv->rxringtail) = *(priv->rxringtail) & ~0xfff;
1521                 *(priv->rxringtail) =
1522                         *(priv->rxringtail) | priv->rxbuffersize;
1523
1524                 *(priv->rxringtail) =
1525                         *(priv->rxringtail) | (1<<31);
1526
1527                 priv->rxringtail += rx_desc_size;
1528                 if (priv->rxringtail >=
1529                    (priv->rxring)+(priv->rxringcount)*rx_desc_size)
1530                         priv->rxringtail = priv->rxring;
1531
1532                 priv->rxbuffer = (priv->rxbuffer->next);
1533         }
1534 }
1535
1536
1537 static void rtl8180_dma_kick(struct net_device *dev, int priority)
1538 {
1539         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1540
1541         rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
1542         write_nic_byte(dev, TX_DMA_POLLING,
1543                         (1 << (priority + 1)) | priv->dma_poll_mask);
1544         rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
1545
1546         force_pci_posting(dev);
1547 }
1548
1549 static void rtl8180_data_hard_stop(struct net_device *dev)
1550 {
1551         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1552
1553         rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
1554         priv->dma_poll_stop_mask |= TPPOLLSTOP_AC_VIQ;
1555         write_nic_byte(dev, TPPollStop, priv->dma_poll_stop_mask);
1556         rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
1557 }
1558
1559 static void rtl8180_data_hard_resume(struct net_device *dev)
1560 {
1561         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1562
1563         rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
1564         priv->dma_poll_stop_mask &= ~(TPPOLLSTOP_AC_VIQ);
1565         write_nic_byte(dev, TPPollStop, priv->dma_poll_stop_mask);
1566         rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
1567 }
1568
1569 /*
1570  * This function TX data frames when the ieee80211 stack requires this.
1571  * It checks also if we need to stop the ieee tx queue, eventually do it
1572  */
1573 static void rtl8180_hard_data_xmit(struct sk_buff *skb, struct net_device *dev,
1574                                    int rate)
1575 {
1576         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1577         int mode;
1578         struct ieee80211_hdr_3addr *h = (struct ieee80211_hdr_3addr *)skb->data;
1579         bool morefrag = le16_to_cpu(h->frame_control) & IEEE80211_FCTL_MOREFRAGS;
1580         unsigned long flags;
1581         int priority;
1582
1583         mode = priv->ieee80211->iw_mode;
1584
1585         rate = ieeerate2rtlrate(rate);
1586         /*
1587          * This function doesn't require lock because we make sure it's called
1588          * with the tx_lock already acquired.
1589          * This come from the kernel's hard_xmit callback (through the ieee
1590          * stack, or from the try_wake_queue (again through the ieee stack.
1591          */
1592         priority = AC2Q(skb->priority);
1593         spin_lock_irqsave(&priv->tx_lock, flags);
1594
1595         if (priv->ieee80211->bHwRadioOff) {
1596                 spin_unlock_irqrestore(&priv->tx_lock, flags);
1597
1598                 return;
1599         }
1600
1601         if (!check_nic_enought_desc(dev, priority)) {
1602                 DMESGW("Error: no descriptor left by previous TX (avail %d) ",
1603                         get_curr_tx_free_desc(dev, priority));
1604                 ieee80211_rtl_stop_queue(priv->ieee80211);
1605         }
1606         rtl8180_tx(dev, skb->data, skb->len, priority, morefrag, 0, rate);
1607         if (!check_nic_enought_desc(dev, priority))
1608                 ieee80211_rtl_stop_queue(priv->ieee80211);
1609
1610         spin_unlock_irqrestore(&priv->tx_lock, flags);
1611 }
1612
1613 /*
1614  * This is a rough attempt to TX a frame
1615  * This is called by the ieee 80211 stack to TX management frames.
1616  * If the ring is full packets are dropped (for data frame the queue
1617  * is stopped before this can happen). For this reason it is better
1618  * if the descriptors are larger than the largest management frame
1619  * we intend to TX: i'm unsure what the HW does if it will not find
1620  * the last fragment of a frame because it has been dropped...
1621  * Since queues for Management and Data frames are different we
1622  * might use a different lock than tx_lock (for example mgmt_tx_lock)
1623  */
1624 /* these function may loop if invoked with 0 descriptors or 0 len buffer */
1625 static int rtl8180_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1626 {
1627         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1628         unsigned long flags;
1629         int priority;
1630
1631         priority = MANAGE_PRIORITY;
1632
1633         spin_lock_irqsave(&priv->tx_lock, flags);
1634
1635         if (priv->ieee80211->bHwRadioOff) {
1636                 spin_unlock_irqrestore(&priv->tx_lock, flags);
1637                 dev_kfree_skb_any(skb);
1638                 return NETDEV_TX_OK;
1639         }
1640
1641         rtl8180_tx(dev, skb->data, skb->len, priority,
1642                 0, 0, ieeerate2rtlrate(priv->ieee80211->basic_rate));
1643
1644         priv->ieee80211->stats.tx_bytes += skb->len;
1645         priv->ieee80211->stats.tx_packets++;
1646         spin_unlock_irqrestore(&priv->tx_lock, flags);
1647
1648         dev_kfree_skb_any(skb);
1649         return NETDEV_TX_OK;
1650 }
1651
1652 static void rtl8180_prepare_beacon(struct net_device *dev)
1653 {
1654         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
1655         struct sk_buff *skb;
1656
1657         u16 word  = read_nic_word(dev, BcnItv);
1658         word &= ~BcnItv_BcnItv; /* clear Bcn_Itv */
1659
1660         /* word |= 0x64; */
1661         word |= cpu_to_le16(priv->ieee80211->current_network.beacon_interval);
1662
1663         write_nic_word(dev, BcnItv, word);
1664
1665         skb = ieee80211_get_beacon(priv->ieee80211);
1666         if (skb) {
1667                 rtl8180_tx(dev, skb->data, skb->len, BEACON_PRIORITY,
1668                         0, 0, ieeerate2rtlrate(priv->ieee80211->basic_rate));
1669                 dev_kfree_skb_any(skb);
1670         }
1671 }
1672
1673 /*
1674  * This function do the real dirty work: it enqueues a TX command descriptor in
1675  * the ring buffer, copyes the frame in a TX buffer and kicks the NIC to ensure
1676  * it does the DMA transfer.
1677  */
1678 short rtl8180_tx(struct net_device *dev, u8 *txbuf, int len, int priority,
1679                  bool morefrag, short descfrag, int rate)
1680 {
1681         struct r8180_priv *priv = ieee80211_priv(dev);
1682         u32 *tail, *temp_tail;
1683         u32 *begin;
1684         u32 *buf;
1685         int i;
1686         int remain;
1687         int buflen;
1688         int count;
1689         struct buffer *buflist;
1690         struct ieee80211_hdr_3addr *frag_hdr =
1691                 (struct ieee80211_hdr_3addr *)txbuf;
1692         u8 dest[ETH_ALEN];
1693         u8 bUseShortPreamble = 0;
1694         u8 bCTSEnable = 0;
1695         u8 bRTSEnable = 0;
1696         u16 Duration = 0;
1697         u16 RtsDur = 0;
1698         u16 ThisFrameTime = 0;
1699         u16 TxDescDuration = 0;
1700         bool ownbit_flag = false;
1701
1702         switch (priority) {
1703         case MANAGE_PRIORITY:
1704                 tail = priv->txmapringtail;
1705                 begin = priv->txmapring;
1706                 buflist = priv->txmapbufstail;
1707                 count = priv->txringcount;
1708                 break;
1709         case BK_PRIORITY:
1710                 tail = priv->txbkpringtail;
1711                 begin = priv->txbkpring;
1712                 buflist = priv->txbkpbufstail;
1713                 count = priv->txringcount;
1714                 break;
1715         case BE_PRIORITY:
1716                 tail = priv->txbepringtail;
1717                 begin = priv->txbepring;
1718                 buflist = priv->txbepbufstail;
1719                 count = priv->txringcount;
1720                 break;
1721         case VI_PRIORITY:
1722                 tail = priv->txvipringtail;
1723                 begin = priv->txvipring;
1724                 buflist = priv->txvipbufstail;
1725                 count = priv->txringcount;
1726                 break;
1727         case VO_PRIORITY:
1728                 tail = priv->txvopringtail;
1729                 begin = priv->txvopring;
1730                 buflist = priv->txvopbufstail;
1731                 count = priv->txringcount;
1732                 break;
1733         case HI_PRIORITY:
1734                 tail = priv->txhpringtail;
1735                 begin = priv->txhpring;
1736                 buflist = priv->txhpbufstail;
1737                 count = priv->txringcount;
1738                 break;
1739         case BEACON_PRIORITY:
1740                 tail = priv->txbeaconringtail;
1741                 begin = priv->txbeaconring;
1742                 buflist = priv->txbeaconbufstail;
1743                 count = priv->txbeaconcount;
1744                 break;
1745         default:
1746                 return -1;
1747                 break;
1748         }
1749
1750         memcpy(&dest, frag_hdr->addr1, ETH_ALEN);
1751         if (is_multicast_ether_addr(dest)) {
1752                 Duration = 0;
1753                 RtsDur = 0;
1754                 bRTSEnable = 0;
1755                 bCTSEnable = 0;
1756
1757                 ThisFrameTime = ComputeTxTime(len + sCrcLng,
1758                         rtl8180_rate2rate(rate), 0, bUseShortPreamble);
1759                 TxDescDuration = ThisFrameTime;
1760         } else { /* Unicast packet */
1761                 u16 AckTime;
1762
1763                 /* for Keep alive */
1764                 priv->NumTxUnicast++;
1765
1766                 /* Figure out ACK rate according to BSS basic rate
1767                  * and Tx rate.
1768                  * AckCTSLng = 14 use 1M bps send
1769                  */
1770                 AckTime = ComputeTxTime(14, 10, 0, 0);
1771
1772                 if (((len + sCrcLng) > priv->rts) && priv->rts) { /* RTS/CTS. */
1773                         u16 RtsTime, CtsTime;
1774                         bRTSEnable = 1;
1775                         bCTSEnable = 0;
1776
1777                         /* Rate and time required for RTS. */
1778                         RtsTime = ComputeTxTime(sAckCtsLng / 8,
1779                                 priv->ieee80211->basic_rate, 0, 0);
1780
1781                         /* Rate and time required for CTS.
1782                          * AckCTSLng = 14 use 1M bps send
1783                          */
1784                         CtsTime = ComputeTxTime(14, 10, 0, 0);
1785
1786                         /* Figure out time required to transmit this frame. */
1787                         ThisFrameTime = ComputeTxTime(len + sCrcLng,
1788                                 rtl8180_rate2rate(rate), 0,
1789                                 bUseShortPreamble);
1790
1791                         /* RTS-CTS-ThisFrame-ACK. */
1792                         RtsDur = CtsTime + ThisFrameTime +
1793                                 AckTime + 3 * aSifsTime;
1794
1795                         TxDescDuration = RtsTime + RtsDur;
1796                 } else { /* Normal case. */
1797                         bCTSEnable = 0;
1798                         bRTSEnable = 0;
1799                         RtsDur = 0;
1800
1801                         ThisFrameTime = ComputeTxTime(len + sCrcLng,
1802                                 rtl8180_rate2rate(rate), 0, bUseShortPreamble);
1803                         TxDescDuration = ThisFrameTime + aSifsTime + AckTime;
1804                 }
1805
1806                 if (!(le16_to_cpu(frag_hdr->frame_control) & IEEE80211_FCTL_MOREFRAGS)) {
1807                         /* ThisFrame-ACK. */
1808                         Duration = aSifsTime + AckTime;
1809                 } else { /* One or more fragments remained. */
1810                         u16 NextFragTime;
1811
1812                         /* pretend following packet length = current packet */
1813                         NextFragTime = ComputeTxTime(len + sCrcLng,
1814                                 rtl8180_rate2rate(rate), 0, bUseShortPreamble);
1815
1816                         /* ThisFrag-ACk-NextFrag-ACK. */
1817                         Duration = NextFragTime + 3 * aSifsTime + 2 * AckTime;
1818                 }
1819
1820         } /* End of Unicast packet */
1821
1822         frag_hdr->duration_id = Duration;
1823
1824         buflen = priv->txbuffsize;
1825         remain = len;
1826         temp_tail = tail;
1827
1828         while (remain != 0) {
1829                 mb();
1830                 if (!buflist) {
1831                         DMESGE("TX buffer error, cannot TX frames. pri %d.",
1832                                 priority);
1833                         return -1;
1834                 }
1835                 buf = buflist->buf;
1836
1837                 if ((*tail & (1 << 31)) && (priority != BEACON_PRIORITY)) {
1838                         DMESGW("No more TX desc, returning %x of %x",
1839                                remain, len);
1840                         priv->stats.txrdu++;
1841                         return remain;
1842                 }
1843
1844                 *tail = 0; /* zeroes header */
1845                 *(tail+1) = 0;
1846                 *(tail+3) = 0;
1847                 *(tail+5) = 0;
1848                 *(tail+6) = 0;
1849                 *(tail+7) = 0;
1850
1851                 /* FIXME: should be triggered by HW encryption parameters.*/
1852                 *tail |= (1<<15); /* no encrypt */
1853
1854                 if (remain == len && !descfrag) {
1855                         ownbit_flag = false;
1856                         *tail = *tail | (1 << 29); /* first segment of packet */
1857                         *tail = *tail | (len);
1858                 } else {
1859                         ownbit_flag = true;
1860                 }
1861
1862                 for (i = 0; i < buflen && remain > 0; i++, remain--) {
1863                         /* copy data into descriptor pointed DMAble buffer */
1864                         ((u8 *)buf)[i] = txbuf[i];
1865
1866                         if (remain == 4 && i+4 >= buflen)
1867                                 break;
1868                         /* ensure the last desc has at least 4 bytes payload */
1869                 }
1870                 txbuf = txbuf + i;
1871                 *(tail+3) = *(tail+3) & ~0xfff;
1872                 *(tail+3) = *(tail+3) | i; /* buffer length */
1873
1874                 if (bCTSEnable)
1875                         *tail |= (1<<18);
1876
1877                 if (bRTSEnable) { /* rts enable */
1878                         /* RTS RATE */
1879                         *tail |= (ieeerate2rtlrate(
1880                                 priv->ieee80211->basic_rate) << 19);
1881
1882                         *tail |= (1<<23); /* rts enable */
1883                         *(tail+1) |= (RtsDur&0xffff); /* RTS Duration */
1884                 }
1885                 *(tail+3) |= ((TxDescDuration&0xffff)<<16); /* DURATION */
1886
1887                 *(tail + 5) |= (11 << 8); /* retry lim; */
1888
1889                 *tail = *tail | ((rate&0xf) << 24);
1890
1891                 if (morefrag)
1892                         *tail = (*tail) | (1<<17); /* more fragment */
1893                 if (!remain)
1894                         *tail = (*tail) | (1<<28); /* last segment of frame */
1895
1896                 *(tail+5) = *(tail+5)|(2<<27);
1897                 *(tail+7) = *(tail+7)|(1<<4);
1898
1899                 wmb();
1900                 if (ownbit_flag)
1901                         /* descriptor ready to be txed */
1902                         *tail |= (1 << 31);
1903
1904                 if ((tail - begin)/8 == count-1)
1905                         tail = begin;
1906                 else
1907                         tail = tail+8;
1908
1909                 buflist = buflist->next;
1910
1911                 mb();
1912
1913                 switch (priority) {
1914                 case MANAGE_PRIORITY:
1915                         priv->txmapringtail = tail;
1916                         priv->txmapbufstail = buflist;
1917                         break;
1918                 case BK_PRIORITY:
1919                         priv->txbkpringtail = tail;
1920                         priv->txbkpbufstail = buflist;
1921                         break;
1922                 case BE_PRIORITY:
1923                         priv->txbepringtail = tail;
1924                         priv->txbepbufstail = buflist;
1925                         break;
1926                 case VI_PRIORITY:
1927                         priv->txvipringtail = tail;
1928                         priv->txvipbufstail = buflist;
1929                         break;
1930                 case VO_PRIORITY:
1931                         priv->txvopringtail = tail;
1932                         priv->txvopbufstail = buflist;
1933                         break;
1934                 case HI_PRIORITY:
1935                         priv->txhpringtail = tail;
1936                         priv->txhpbufstail = buflist;
1937                         break;
1938                 case BEACON_PRIORITY:
1939                         /*
1940                          * The HW seems to be happy with the 1st
1941                          * descriptor filled and the 2nd empty...
1942                          * So always update descriptor 1 and never
1943                          * touch 2nd
1944                          */
1945                         break;
1946                 }
1947         }
1948         *temp_tail = *temp_tail | (1<<31); /* descriptor ready to be txed */
1949         rtl8180_dma_kick(dev, priority);
1950
1951         return 0;
1952 }
1953
1954 void rtl8180_irq_rx_tasklet(struct r8180_priv *priv);
1955
1956 static void rtl8180_link_change(struct net_device *dev)
1957 {
1958         struct r8180_priv *priv = ieee80211_priv(dev);
1959         u16 beacon_interval;
1960         struct ieee80211_network *net = &priv->ieee80211->current_network;
1961
1962         rtl8180_update_msr(dev);
1963
1964         rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
1965
1966         write_nic_dword(dev, BSSID, ((u32 *)net->bssid)[0]);
1967         write_nic_word(dev, BSSID+4, ((u16 *)net->bssid)[2]);
1968
1969         beacon_interval  = read_nic_word(dev, BEACON_INTERVAL);
1970         beacon_interval &= ~BEACON_INTERVAL_MASK;
1971         beacon_interval |= net->beacon_interval;
1972         write_nic_word(dev, BEACON_INTERVAL, beacon_interval);
1973
1974         rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
1975
1976         rtl8180_set_chan(dev, priv->chan);
1977 }
1978
1979 static void rtl8180_rq_tx_ack(struct net_device *dev)
1980 {
1981
1982         struct r8180_priv *priv = ieee80211_priv(dev);
1983
1984         write_nic_byte(dev, CONFIG4,
1985                 read_nic_byte(dev, CONFIG4) | CONFIG4_PWRMGT);
1986         priv->ack_tx_to_ieee = 1;
1987 }
1988
1989 static short rtl8180_is_tx_queue_empty(struct net_device *dev)
1990 {
1991
1992         struct r8180_priv *priv = ieee80211_priv(dev);
1993         u32 *d;
1994
1995         for (d = priv->txmapring;
1996                 d < priv->txmapring + priv->txringcount; d += 8)
1997                         if (*d & (1<<31))
1998                                 return 0;
1999
2000         for (d = priv->txbkpring;
2001                 d < priv->txbkpring + priv->txringcount; d += 8)
2002                         if (*d & (1<<31))
2003                                 return 0;
2004
2005         for (d = priv->txbepring;
2006                 d < priv->txbepring + priv->txringcount; d += 8)
2007                         if (*d & (1<<31))
2008                                 return 0;
2009
2010         for (d = priv->txvipring;
2011                 d < priv->txvipring + priv->txringcount; d += 8)
2012                         if (*d & (1<<31))
2013                                 return 0;
2014
2015         for (d = priv->txvopring;
2016                 d < priv->txvopring + priv->txringcount; d += 8)
2017                         if (*d & (1<<31))
2018                                 return 0;
2019
2020         for (d = priv->txhpring;
2021                 d < priv->txhpring + priv->txringcount; d += 8)
2022                         if (*d & (1<<31))
2023                                 return 0;
2024         return 1;
2025 }
2026
2027 static void rtl8180_hw_wakeup(struct net_device *dev)
2028 {
2029         unsigned long flags;
2030         struct r8180_priv *priv = ieee80211_priv(dev);
2031
2032         spin_lock_irqsave(&priv->ps_lock, flags);
2033         write_nic_byte(dev, CONFIG4,
2034                 read_nic_byte(dev, CONFIG4) & ~CONFIG4_PWRMGT);
2035         if (priv->rf_wakeup)
2036                 priv->rf_wakeup(dev);
2037         spin_unlock_irqrestore(&priv->ps_lock, flags);
2038 }
2039
2040 static void rtl8180_hw_sleep_down(struct net_device *dev)
2041 {
2042         unsigned long flags;
2043         struct r8180_priv *priv = ieee80211_priv(dev);
2044
2045         spin_lock_irqsave(&priv->ps_lock, flags);
2046         if (priv->rf_sleep)
2047                 priv->rf_sleep(dev);
2048         spin_unlock_irqrestore(&priv->ps_lock, flags);
2049 }
2050
2051 static void rtl8180_hw_sleep(struct net_device *dev, u32 th, u32 tl)
2052 {
2053         struct r8180_priv *priv = ieee80211_priv(dev);
2054         u32 rb = jiffies;
2055         unsigned long flags;
2056
2057         spin_lock_irqsave(&priv->ps_lock, flags);
2058
2059         /*
2060          * Writing HW register with 0 equals to disable
2061          * the timer, that is not really what we want
2062          */
2063         tl -= MSECS(4+16+7);
2064
2065         /*
2066          * If the interval in which we are requested to sleep is too
2067          * short then give up and remain awake
2068          */
2069         if (((tl >= rb) && (tl-rb) <= MSECS(MIN_SLEEP_TIME))
2070                 || ((rb > tl) && (rb-tl) < MSECS(MIN_SLEEP_TIME))) {
2071                 spin_unlock_irqrestore(&priv->ps_lock, flags);
2072                 netdev_warn(dev, "too short to sleep\n");
2073                 return;
2074         }
2075
2076         {
2077                 u32 tmp = (tl > rb) ? (tl-rb) : (rb-tl);
2078
2079                 priv->DozePeriodInPast2Sec += jiffies_to_msecs(tmp);
2080                 /* as tl may be less than rb */
2081                 queue_delayed_work(priv->ieee80211->wq,
2082                         &priv->ieee80211->hw_wakeup_wq, tmp);
2083         }
2084         /*
2085          * If we suspect the TimerInt is gone beyond tl
2086          * while setting it, then give up
2087          */
2088
2089         if (((tl > rb) && ((tl-rb) > MSECS(MAX_SLEEP_TIME))) ||
2090                 ((tl < rb) && ((rb-tl) > MSECS(MAX_SLEEP_TIME)))) {
2091                 spin_unlock_irqrestore(&priv->ps_lock, flags);
2092                 return;
2093         }
2094
2095         queue_work(priv->ieee80211->wq, (void *)&priv->ieee80211->hw_sleep_wq);
2096         spin_unlock_irqrestore(&priv->ps_lock, flags);
2097 }
2098
2099 static void rtl8180_wmm_single_param_update(struct net_device *dev,
2100         u8 mode, AC_CODING eACI, PAC_PARAM param)
2101 {
2102         u8 u1bAIFS;
2103         u32 u4bAcParam;
2104
2105         /* Retrieve parameters to update. */
2106         /* Mode G/A: slotTimeTimer = 9; Mode B: 20 */
2107         u1bAIFS = param->f.AciAifsn.f.AIFSN * ((mode & IEEE_G) == IEEE_G ?
2108                 9 : 20) + aSifsTime;
2109         u4bAcParam = (((u32)param->f.TXOPLimit << AC_PARAM_TXOP_LIMIT_OFFSET) |
2110                 ((u32)param->f.Ecw.f.ECWmax << AC_PARAM_ECW_MAX_OFFSET) |
2111                 ((u32)param->f.Ecw.f.ECWmin << AC_PARAM_ECW_MIN_OFFSET) |
2112                 ((u32)u1bAIFS << AC_PARAM_AIFS_OFFSET));
2113
2114         switch (eACI) {
2115         case AC1_BK:
2116                 write_nic_dword(dev, AC_BK_PARAM, u4bAcParam);
2117                 return;
2118         case AC0_BE:
2119                 write_nic_dword(dev, AC_BE_PARAM, u4bAcParam);
2120                 return;
2121         case AC2_VI:
2122                 write_nic_dword(dev, AC_VI_PARAM, u4bAcParam);
2123                 return;
2124         case AC3_VO:
2125                 write_nic_dword(dev, AC_VO_PARAM, u4bAcParam);
2126                 return;
2127         default:
2128                 pr_warn("SetHwReg8185(): invalid ACI: %d!\n", eACI);
2129                 return;
2130         }
2131 }
2132
2133 static void rtl8180_wmm_param_update(struct work_struct *work)
2134 {
2135         struct ieee80211_device *ieee = container_of(work,
2136                 struct ieee80211_device, wmm_param_update_wq);
2137         struct net_device *dev = ieee->dev;
2138         u8 *ac_param = (u8 *)(ieee->current_network.wmm_param);
2139         u8 mode = ieee->current_network.mode;
2140         AC_CODING eACI;
2141         AC_PARAM AcParam;
2142
2143         if (!ieee->current_network.QoS_Enable) {
2144                 /* legacy ac_xx_param update */
2145                 AcParam.longData = 0;
2146                 AcParam.f.AciAifsn.f.AIFSN = 2; /* Follow 802.11 DIFS. */
2147                 AcParam.f.AciAifsn.f.ACM = 0;
2148                 AcParam.f.Ecw.f.ECWmin = 3; /* Follow 802.11 CWmin. */
2149                 AcParam.f.Ecw.f.ECWmax = 7; /* Follow 802.11 CWmax. */
2150                 AcParam.f.TXOPLimit = 0;
2151
2152                 for (eACI = 0; eACI < AC_MAX; eACI++) {
2153                         AcParam.f.AciAifsn.f.ACI = (u8)eACI;
2154
2155                         rtl8180_wmm_single_param_update(dev, mode, eACI,
2156                                 (PAC_PARAM)&AcParam);
2157                 }
2158                 return;
2159         }
2160
2161         for (eACI = 0; eACI < AC_MAX; eACI++) {
2162                 rtl8180_wmm_single_param_update(dev, mode,
2163                         ((PAC_PARAM)ac_param)->f.AciAifsn.f.ACI,
2164                         (PAC_PARAM)ac_param);
2165
2166                 ac_param += sizeof(AC_PARAM);
2167         }
2168 }
2169
2170 void rtl8180_restart_wq(struct work_struct *work);
2171 void rtl8180_watch_dog_wq(struct work_struct *work);
2172 void rtl8180_hw_wakeup_wq(struct work_struct *work);
2173 void rtl8180_hw_sleep_wq(struct work_struct *work);
2174 void rtl8180_sw_antenna_wq(struct work_struct *work);
2175 void rtl8180_watch_dog(struct net_device *dev);
2176
2177 static void watch_dog_adaptive(unsigned long data)
2178 {
2179         struct r8180_priv *priv = ieee80211_priv((struct net_device *)data);
2180
2181         if (!priv->up) {
2182                 DMESG("<----watch_dog_adaptive():driver is not up!\n");
2183                 return;
2184         }
2185
2186         /* Tx High Power Mechanism. */
2187         if (CheckHighPower((struct net_device *)data))
2188                 queue_work(priv->ieee80211->wq,
2189                         (void *)&priv->ieee80211->tx_pw_wq);
2190
2191         /* Tx Power Tracking on 87SE. */
2192         if (CheckTxPwrTracking((struct net_device *)data))
2193                 TxPwrTracking87SE((struct net_device *)data);
2194
2195         /* Perform DIG immediately. */
2196         if (CheckDig((struct net_device *)data))
2197                 queue_work(priv->ieee80211->wq,
2198                         (void *)&priv->ieee80211->hw_dig_wq);
2199
2200         rtl8180_watch_dog((struct net_device *)data);
2201
2202         queue_work(priv->ieee80211->wq,
2203                 (void *)&priv->ieee80211->GPIOChangeRFWorkItem);
2204
2205         priv->watch_dog_timer.expires = jiffies +
2206                 MSECS(IEEE80211_WATCH_DOG_TIME);
2207
2208         add_timer(&priv->watch_dog_timer);
2209 }
2210
2211 static struct rtl8187se_channel_list channel_plan_list[] = {
2212         /* FCC */
2213         {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 36, 40,
2214                 44, 48, 52, 56, 60, 64}, 19},
2215
2216         /* IC */
2217         {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 11},
2218
2219         /* ETSI */
2220         {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 36, 40,
2221                 44, 48, 52, 56, 60, 64}, 21},
2222
2223         /* Spain. Change to ETSI. */
2224         {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 36, 40,
2225                 44, 48, 52, 56, 60, 64}, 21},
2226
2227         /* France. Change to ETSI. */
2228         {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 36, 40,
2229                 44, 48, 52, 56, 60, 64}, 21},
2230
2231         /* MKK */
2232         {{14, 36, 40, 44, 48, 52, 56, 60, 64}, 9},
2233
2234         /* MKK1 */
2235         {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 36,
2236                 40, 44, 48, 52, 56, 60, 64}, 22},
2237
2238         /* Israel. */
2239         {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 36, 40,
2240                 44, 48, 52, 56, 60, 64}, 21},
2241
2242         /* For 11a , TELEC */
2243         {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 34, 38, 42, 46}, 17},
2244
2245         /* For Global Domain. 1-11 active, 12-14 passive. */
2246         {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, 14},
2247
2248         /* world wide 13: ch1~ch11 active, ch12~13 passive */
2249         {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, 13}
2250 };
2251
2252 static void rtl8180_set_channel_map(u8 channel_plan,
2253                                     struct ieee80211_device *ieee)
2254 {
2255         int i;
2256
2257         ieee->MinPassiveChnlNum = MAX_CHANNEL_NUMBER+1;
2258         ieee->IbssStartChnl = 0;
2259
2260         switch (channel_plan) {
2261         case COUNTRY_CODE_FCC:
2262         case COUNTRY_CODE_IC:
2263         case COUNTRY_CODE_ETSI:
2264         case COUNTRY_CODE_SPAIN:
2265         case COUNTRY_CODE_FRANCE:
2266         case COUNTRY_CODE_MKK:
2267         case COUNTRY_CODE_MKK1:
2268         case COUNTRY_CODE_ISRAEL:
2269         case COUNTRY_CODE_TELEC:
2270                 {
2271                         Dot11d_Init(ieee);
2272                         ieee->bGlobalDomain = false;
2273                         if (channel_plan_list[channel_plan].len != 0) {
2274                                 /* Clear old channel map */
2275                                 memset(GET_DOT11D_INFO(ieee)->channel_map, 0, sizeof(GET_DOT11D_INFO(ieee)->channel_map));
2276                                 /* Set new channel map */
2277                                 for (i = 0; i < channel_plan_list[channel_plan].len; i++) {
2278                                         if (channel_plan_list[channel_plan].channel[i] <= 14)
2279                                                 GET_DOT11D_INFO(ieee)->channel_map[channel_plan_list[channel_plan].channel[i]] = 1;
2280                                 }
2281                         }
2282                         break;
2283                 }
2284         case COUNTRY_CODE_GLOBAL_DOMAIN:
2285                 {
2286                         GET_DOT11D_INFO(ieee)->bEnabled = false;
2287                         Dot11d_Reset(ieee);
2288                         ieee->bGlobalDomain = true;
2289                         break;
2290                 }
2291         case COUNTRY_CODE_WORLD_WIDE_13_INDEX:
2292                 {
2293                         ieee->MinPassiveChnlNum = 12;
2294                         ieee->IbssStartChnl = 10;
2295                         break;
2296                 }
2297         default:
2298                 {
2299                         Dot11d_Init(ieee);
2300                         ieee->bGlobalDomain = false;
2301                         memset(GET_DOT11D_INFO(ieee)->channel_map, 0, sizeof(GET_DOT11D_INFO(ieee)->channel_map));
2302                         for (i = 1; i <= 14; i++)
2303                                 GET_DOT11D_INFO(ieee)->channel_map[i] = 1;
2304                         break;
2305                 }
2306         }
2307 }
2308
2309 void GPIOChangeRFWorkItemCallBack(struct work_struct *work);
2310
2311 static void rtl8180_statistics_init(struct stats *pstats)
2312 {
2313         memset(pstats, 0, sizeof(struct stats));
2314 }
2315
2316 static void rtl8180_link_detect_init(struct link_detect_t *plink_detect)
2317 {
2318         memset(plink_detect, 0, sizeof(struct link_detect_t));
2319         plink_detect->slot_num = DEFAULT_SLOT_NUM;
2320 }
2321
2322 static void rtl8187se_eeprom_register_read(struct eeprom_93cx6 *eeprom)
2323 {
2324         struct net_device *dev = eeprom->data;
2325         u8 reg = read_nic_byte(dev, EPROM_CMD);
2326
2327         eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
2328         eeprom->reg_data_out = reg & RTL818X_EEPROM_CMD_READ;
2329         eeprom->reg_data_clock = reg & RTL818X_EEPROM_CMD_CK;
2330         eeprom->reg_chip_select = reg & RTL818X_EEPROM_CMD_CS;
2331 }
2332
2333 static void rtl8187se_eeprom_register_write(struct eeprom_93cx6 *eeprom)
2334 {
2335         struct net_device *dev = eeprom->data;
2336         u8 reg = 2 << 6;
2337
2338         if (eeprom->reg_data_in)
2339                 reg |= RTL818X_EEPROM_CMD_WRITE;
2340         if (eeprom->reg_data_out)
2341                 reg |= RTL818X_EEPROM_CMD_READ;
2342         if (eeprom->reg_data_clock)
2343                 reg |= RTL818X_EEPROM_CMD_CK;
2344         if (eeprom->reg_chip_select)
2345                 reg |= RTL818X_EEPROM_CMD_CS;
2346
2347         write_nic_byte(dev, EPROM_CMD, reg);
2348         read_nic_byte(dev, EPROM_CMD);
2349         udelay(10);
2350 }
2351
2352 static short rtl8180_init(struct net_device *dev)
2353 {
2354         struct r8180_priv *priv = ieee80211_priv(dev);
2355         u16 word;
2356         u16 usValue;
2357         u16 tmpu16;
2358         int i, j;
2359         struct eeprom_93cx6 eeprom;
2360         u16 eeprom_val;
2361
2362         eeprom.data = dev;
2363         eeprom.register_read = rtl8187se_eeprom_register_read;
2364         eeprom.register_write = rtl8187se_eeprom_register_write;
2365         eeprom.width = PCI_EEPROM_WIDTH_93C46;
2366
2367         eeprom_93cx6_read(&eeprom, EEPROM_COUNTRY_CODE>>1, &eeprom_val);
2368         priv->channel_plan = eeprom_val & 0xFF;
2369         if (priv->channel_plan > COUNTRY_CODE_GLOBAL_DOMAIN) {
2370                 netdev_err(dev, "rtl8180_init: Invalid channel plan! Set to default.\n");
2371                 priv->channel_plan = 0;
2372         }
2373
2374         DMESG("Channel plan is %d\n", priv->channel_plan);
2375         rtl8180_set_channel_map(priv->channel_plan, priv->ieee80211);
2376
2377         /* FIXME: these constants are placed in a bad pleace. */
2378         priv->txbuffsize = 2048;        /* 1024; */
2379         priv->txringcount = 32;         /* 32; */
2380         priv->rxbuffersize = 2048;      /* 1024; */
2381         priv->rxringcount = 64;         /* 32; */
2382         priv->txbeaconcount = 2;
2383         priv->rx_skb_complete = 1;
2384
2385         priv->RFChangeInProgress = false;
2386         priv->SetRFPowerStateInProgress = false;
2387         priv->RFProgType = 0;
2388
2389         priv->irq_enabled = 0;
2390
2391         rtl8180_statistics_init(&priv->stats);
2392         rtl8180_link_detect_init(&priv->link_detect);
2393
2394         priv->ack_tx_to_ieee = 0;
2395         priv->ieee80211->current_network.beacon_interval =
2396                 DEFAULT_BEACONINTERVAL;
2397         priv->ieee80211->iw_mode = IW_MODE_INFRA;
2398         priv->ieee80211->softmac_features  = IEEE_SOFTMAC_SCAN |
2399                 IEEE_SOFTMAC_ASSOCIATE | IEEE_SOFTMAC_PROBERQ |
2400                 IEEE_SOFTMAC_PROBERS | IEEE_SOFTMAC_TX_QUEUE;
2401         priv->ieee80211->active_scan = 1;
2402         priv->ieee80211->rate = 110; /* 11 mbps */
2403         priv->ieee80211->modulation = IEEE80211_CCK_MODULATION;
2404         priv->ieee80211->host_encrypt = 1;
2405         priv->ieee80211->host_decrypt = 1;
2406         priv->ieee80211->sta_wake_up = rtl8180_hw_wakeup;
2407         priv->ieee80211->ps_request_tx_ack = rtl8180_rq_tx_ack;
2408         priv->ieee80211->enter_sleep_state = rtl8180_hw_sleep;
2409         priv->ieee80211->ps_is_queue_empty = rtl8180_is_tx_queue_empty;
2410
2411         priv->hw_wep = hwwep;
2412         priv->dev = dev;
2413         priv->retry_rts = DEFAULT_RETRY_RTS;
2414         priv->retry_data = DEFAULT_RETRY_DATA;
2415         priv->RFChangeInProgress = false;
2416         priv->SetRFPowerStateInProgress = false;
2417         priv->RFProgType = 0;
2418         priv->bInactivePs = true; /* false; */
2419         priv->ieee80211->bInactivePs = priv->bInactivePs;
2420         priv->bSwRfProcessing = false;
2421         priv->eRFPowerState = RF_OFF;
2422         priv->RfOffReason = 0;
2423         priv->led_strategy = SW_LED_MODE0;
2424         priv->TxPollingTimes = 0;
2425         priv->bLeisurePs = true;
2426         priv->dot11PowerSaveMode = ACTIVE;
2427         priv->AdMinCheckPeriod = 5;
2428         priv->AdMaxCheckPeriod = 10;
2429         priv->AdMaxRxSsThreshold = 30;  /* 60->30 */
2430         priv->AdRxSsThreshold = 20;     /* 50->20 */
2431         priv->AdCheckPeriod = priv->AdMinCheckPeriod;
2432         priv->AdTickCount = 0;
2433         priv->AdRxSignalStrength = -1;
2434         priv->RegSwAntennaDiversityMechanism = 0;
2435         priv->RegDefaultAntenna = 0;
2436         priv->SignalStrength = 0;
2437         priv->AdRxOkCnt = 0;
2438         priv->CurrAntennaIndex = 0;
2439         priv->AdRxSsBeforeSwitched = 0;
2440         init_timer(&priv->SwAntennaDiversityTimer);
2441         priv->SwAntennaDiversityTimer.data = (unsigned long)dev;
2442         priv->SwAntennaDiversityTimer.function =
2443                 (void *)SwAntennaDiversityTimerCallback;
2444         priv->bDigMechanism = true;
2445         priv->InitialGain = 6;
2446         priv->bXtalCalibration = false;
2447         priv->XtalCal_Xin = 0;
2448         priv->XtalCal_Xout = 0;
2449         priv->bTxPowerTrack = false;
2450         priv->ThermalMeter = 0;
2451         priv->FalseAlarmRegValue = 0;
2452         priv->RegDigOfdmFaUpTh = 0xc; /* Upper threshold of OFDM false alarm,
2453                                         which is used in DIG. */
2454         priv->DIG_NumberFallbackVote = 0;
2455         priv->DIG_NumberUpgradeVote = 0;
2456         priv->LastSignalStrengthInPercent = 0;
2457         priv->Stats_SignalStrength = 0;
2458         priv->LastRxPktAntenna = 0;
2459         priv->SignalQuality = 0; /* in 0-100 index. */
2460         priv->Stats_SignalQuality = 0;
2461         priv->RecvSignalPower = 0; /* in dBm. */
2462         priv->Stats_RecvSignalPower = 0;
2463         priv->AdMainAntennaRxOkCnt = 0;
2464         priv->AdAuxAntennaRxOkCnt = 0;
2465         priv->bHWAdSwitched = false;
2466         priv->bRegHighPowerMechanism = true;
2467         priv->RegHiPwrUpperTh = 77;
2468         priv->RegHiPwrLowerTh = 75;
2469         priv->RegRSSIHiPwrUpperTh = 70;
2470         priv->RegRSSIHiPwrLowerTh = 20;
2471         priv->bCurCCKPkt = false;
2472         priv->UndecoratedSmoothedSS = -1;
2473         priv->bToUpdateTxPwr = false;
2474         priv->CurCCKRSSI = 0;
2475         priv->RxPower = 0;
2476         priv->RSSI = 0;
2477         priv->NumTxOkTotal = 0;
2478         priv->NumTxUnicast = 0;
2479         priv->keepAliveLevel = DEFAULT_KEEP_ALIVE_LEVEL;
2480         priv->CurrRetryCnt = 0;
2481         priv->LastRetryCnt = 0;
2482         priv->LastTxokCnt = 0;
2483         priv->LastRxokCnt = 0;
2484         priv->LastRetryRate = 0;
2485         priv->bTryuping = 0;
2486         priv->CurrTxRate = 0;
2487         priv->CurrRetryRate = 0;
2488         priv->TryupingCount = 0;
2489         priv->TryupingCountNoData = 0;
2490         priv->TryDownCountLowData = 0;
2491         priv->LastTxOKBytes = 0;
2492         priv->LastFailTxRate = 0;
2493         priv->LastFailTxRateSS = 0;
2494         priv->FailTxRateCount = 0;
2495         priv->LastTxThroughput = 0;
2496         priv->NumTxOkBytesTotal = 0;
2497         priv->ForcedDataRate = 0;
2498         priv->RegBModeGainStage = 1;
2499
2500         priv->promisc = (dev->flags & IFF_PROMISC) ? 1 : 0;
2501         spin_lock_init(&priv->irq_th_lock);
2502         spin_lock_init(&priv->tx_lock);
2503         spin_lock_init(&priv->ps_lock);
2504         spin_lock_init(&priv->rf_ps_lock);
2505         sema_init(&priv->wx_sem, 1);
2506         INIT_WORK(&priv->reset_wq, (void *)rtl8180_restart_wq);
2507         INIT_DELAYED_WORK(&priv->ieee80211->hw_wakeup_wq,
2508                           (void *)rtl8180_hw_wakeup_wq);
2509         INIT_DELAYED_WORK(&priv->ieee80211->hw_sleep_wq,
2510                           (void *)rtl8180_hw_sleep_wq);
2511         INIT_WORK(&priv->ieee80211->wmm_param_update_wq,
2512                   (void *)rtl8180_wmm_param_update);
2513         INIT_DELAYED_WORK(&priv->ieee80211->rate_adapter_wq,
2514                           (void *)rtl8180_rate_adapter);
2515         INIT_DELAYED_WORK(&priv->ieee80211->hw_dig_wq,
2516                          (void *)rtl8180_hw_dig_wq);
2517         INIT_DELAYED_WORK(&priv->ieee80211->tx_pw_wq,
2518                          (void *)rtl8180_tx_pw_wq);
2519         INIT_DELAYED_WORK(&priv->ieee80211->GPIOChangeRFWorkItem,
2520                          (void *) GPIOChangeRFWorkItemCallBack);
2521         tasklet_init(&priv->irq_rx_tasklet,
2522                      (void(*)(unsigned long)) rtl8180_irq_rx_tasklet,
2523                      (unsigned long)priv);
2524
2525         init_timer(&priv->watch_dog_timer);
2526         priv->watch_dog_timer.data = (unsigned long)dev;
2527         priv->watch_dog_timer.function = watch_dog_adaptive;
2528
2529         init_timer(&priv->rateadapter_timer);
2530         priv->rateadapter_timer.data = (unsigned long)dev;
2531         priv->rateadapter_timer.function = timer_rate_adaptive;
2532         priv->RateAdaptivePeriod = RATE_ADAPTIVE_TIMER_PERIOD;
2533         priv->bEnhanceTxPwr = false;
2534
2535         priv->ieee80211->softmac_hard_start_xmit = rtl8180_hard_start_xmit;
2536         priv->ieee80211->set_chan = rtl8180_set_chan;
2537         priv->ieee80211->link_change = rtl8180_link_change;
2538         priv->ieee80211->softmac_data_hard_start_xmit = rtl8180_hard_data_xmit;
2539         priv->ieee80211->data_hard_stop = rtl8180_data_hard_stop;
2540         priv->ieee80211->data_hard_resume = rtl8180_data_hard_resume;
2541
2542         priv->ieee80211->init_wmmparam_flag = 0;
2543
2544         priv->ieee80211->start_send_beacons = rtl8180_start_tx_beacon;
2545         priv->ieee80211->stop_send_beacons = rtl8180_beacon_tx_disable;
2546         priv->ieee80211->fts = DEFAULT_FRAG_THRESHOLD;
2547
2548         priv->ShortRetryLimit = 7;
2549         priv->LongRetryLimit = 7;
2550         priv->EarlyRxThreshold = 7;
2551
2552         priv->TransmitConfig =  (1<<TCR_DurProcMode_OFFSET) |
2553                                 (7<<TCR_MXDMA_OFFSET) |
2554                                 (priv->ShortRetryLimit<<TCR_SRL_OFFSET) |
2555                                 (priv->LongRetryLimit<<TCR_LRL_OFFSET);
2556
2557         priv->ReceiveConfig =   RCR_AMF | RCR_ADF | RCR_ACF |
2558                                 RCR_AB | RCR_AM | RCR_APM |
2559                                 (7<<RCR_MXDMA_OFFSET) |
2560                                 (priv->EarlyRxThreshold<<RCR_FIFO_OFFSET) |
2561                                 (priv->EarlyRxThreshold == 7 ?
2562                                          RCR_ONLYERLPKT : 0);
2563
2564         priv->IntrMask          = IMR_TMGDOK | IMR_TBDER |
2565                                   IMR_THPDER | IMR_THPDOK |
2566                                   IMR_TVODER | IMR_TVODOK |
2567                                   IMR_TVIDER | IMR_TVIDOK |
2568                                   IMR_TBEDER | IMR_TBEDOK |
2569                                   IMR_TBKDER | IMR_TBKDOK |
2570                                   IMR_RDU |
2571                                   IMR_RER | IMR_ROK |
2572                                   IMR_RQoSOK;
2573
2574         priv->InitialGain = 6;
2575
2576         DMESG("MAC controller is a RTL8187SE b/g");
2577
2578         priv->ieee80211->modulation |= IEEE80211_OFDM_MODULATION;
2579         priv->ieee80211->short_slot = 1;
2580
2581         eeprom_93cx6_read(&eeprom, EEPROM_SW_REVD_OFFSET, &usValue);
2582         DMESG("usValue is %#hx\n", usValue);
2583         /* 3Read AntennaDiversity */
2584
2585         /* SW Antenna Diversity. */
2586         priv->EEPROMSwAntennaDiversity = (usValue & EEPROM_SW_AD_MASK) ==
2587                 EEPROM_SW_AD_ENABLE;
2588
2589         /* Default Antenna to use. */
2590         priv->EEPROMDefaultAntenna1 = (usValue & EEPROM_DEF_ANT_MASK) ==
2591                 EEPROM_DEF_ANT_1;
2592
2593         if (priv->RegSwAntennaDiversityMechanism == 0) /* Auto */
2594                 /* 0: default from EEPROM. */
2595                 priv->bSwAntennaDiverity = priv->EEPROMSwAntennaDiversity;
2596         else
2597                 /* 1:disable antenna diversity, 2: enable antenna diversity. */
2598                 priv->bSwAntennaDiverity =
2599                         priv->RegSwAntennaDiversityMechanism == 2;
2600
2601         if (priv->RegDefaultAntenna == 0)
2602                 /* 0: default from EEPROM. */
2603                 priv->bDefaultAntenna1 = priv->EEPROMDefaultAntenna1;
2604         else
2605                 /* 1: main, 2: aux. */
2606                 priv->bDefaultAntenna1 = priv->RegDefaultAntenna == 2;
2607
2608         priv->plcp_preamble_mode = 2;
2609         /* the eeprom type is stored in RCR register bit #6 */
2610         if (RCR_9356SEL & read_nic_dword(dev, RCR))
2611                 priv->epromtype = EPROM_93c56;
2612         else
2613                 priv->epromtype = EPROM_93c46;
2614
2615         eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)
2616                                dev->dev_addr, 3);
2617
2618         for (i = 1, j = 0; i < 14; i += 2, j++) {
2619                 eeprom_93cx6_read(&eeprom, EPROM_TXPW_CH1_2 + j, &word);
2620                 priv->chtxpwr[i] = word & 0xff;
2621                 priv->chtxpwr[i+1] = (word & 0xff00)>>8;
2622         }
2623         for (i = 1, j = 0; i < 14; i += 2, j++) {
2624                 eeprom_93cx6_read(&eeprom, EPROM_TXPW_OFDM_CH1_2 + j, &word);
2625                 priv->chtxpwr_ofdm[i] = word & 0xff;
2626                 priv->chtxpwr_ofdm[i+1] = (word & 0xff00) >> 8;
2627         }
2628
2629         /* 3Read crystal calibration and thermal meter indication on 87SE. */
2630         eeprom_93cx6_read(&eeprom, EEPROM_RSV>>1, &tmpu16);
2631
2632         /* Crystal calibration for Xin and Xout resp. */
2633         priv->XtalCal_Xout = tmpu16 & EEPROM_XTAL_CAL_XOUT_MASK;
2634         priv->XtalCal_Xin = (tmpu16 & EEPROM_XTAL_CAL_XIN_MASK) >> 4;
2635         if ((tmpu16 & EEPROM_XTAL_CAL_ENABLE) >> 12)
2636                 priv->bXtalCalibration = true;
2637
2638         /* Thermal meter reference indication. */
2639         priv->ThermalMeter =  (u8)((tmpu16 & EEPROM_THERMAL_METER_MASK) >> 8);
2640         if ((tmpu16 & EEPROM_THERMAL_METER_ENABLE) >> 13)
2641                 priv->bTxPowerTrack = true;
2642
2643         priv->rf_sleep = rtl8225z4_rf_sleep;
2644         priv->rf_wakeup = rtl8225z4_rf_wakeup;
2645         DMESGW("**PLEASE** REPORT SUCCESSFUL/UNSUCCESSFUL TO Realtek!");
2646
2647         priv->rf_close = rtl8225z2_rf_close;
2648         priv->rf_init = rtl8225z2_rf_init;
2649         priv->rf_set_chan = rtl8225z2_rf_set_chan;
2650         priv->rf_set_sens = NULL;
2651
2652         if (0 != alloc_rx_desc_ring(dev, priv->rxbuffersize, priv->rxringcount))
2653                 return -ENOMEM;
2654
2655         if (0 != alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txringcount,
2656                                   TX_MANAGEPRIORITY_RING_ADDR))
2657                 return -ENOMEM;
2658
2659         if (0 != alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txringcount,
2660                                  TX_BKPRIORITY_RING_ADDR))
2661                 return -ENOMEM;
2662
2663         if (0 != alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txringcount,
2664                                  TX_BEPRIORITY_RING_ADDR))
2665                 return -ENOMEM;
2666
2667         if (0 != alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txringcount,
2668                                   TX_VIPRIORITY_RING_ADDR))
2669                 return -ENOMEM;
2670
2671         if (0 != alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txringcount,
2672                                   TX_VOPRIORITY_RING_ADDR))
2673                 return -ENOMEM;
2674
2675         if (0 != alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txringcount,
2676                                   TX_HIGHPRIORITY_RING_ADDR))
2677                 return -ENOMEM;
2678
2679         if (0 != alloc_tx_desc_ring(dev, priv->txbuffsize, priv->txbeaconcount,
2680                                   TX_BEACON_RING_ADDR))
2681                 return -ENOMEM;
2682
2683         if (request_irq(dev->irq, rtl8180_interrupt,
2684                 IRQF_SHARED, dev->name, dev)) {
2685                 DMESGE("Error allocating IRQ %d", dev->irq);
2686                 return -1;
2687         } else {
2688                 priv->irq = dev->irq;
2689                 DMESG("IRQ %d", dev->irq);
2690         }
2691
2692         return 0;
2693 }
2694
2695 void rtl8180_no_hw_wep(struct net_device *dev)
2696 {
2697 }
2698
2699 void rtl8180_set_hw_wep(struct net_device *dev)
2700 {
2701         struct r8180_priv *priv = ieee80211_priv(dev);
2702         u8 pgreg;
2703         u8 security;
2704         u32 key0_word4;
2705
2706         pgreg = read_nic_byte(dev, PGSELECT);
2707         write_nic_byte(dev, PGSELECT, pgreg & ~(1<<PGSELECT_PG_SHIFT));
2708
2709         key0_word4 = read_nic_dword(dev, KEY0+4+4+4);
2710         key0_word4 &= ~0xff;
2711         key0_word4 |= priv->key0[3] & 0xff;
2712         write_nic_dword(dev, KEY0, (priv->key0[0]));
2713         write_nic_dword(dev, KEY0+4, (priv->key0[1]));
2714         write_nic_dword(dev, KEY0+4+4, (priv->key0[2]));
2715         write_nic_dword(dev, KEY0+4+4+4, (key0_word4));
2716
2717         security  = read_nic_byte(dev, SECURITY);
2718         security |= (1<<SECURITY_WEP_TX_ENABLE_SHIFT);
2719         security |= (1<<SECURITY_WEP_RX_ENABLE_SHIFT);
2720         security &= ~SECURITY_ENCRYP_MASK;
2721         security |= (SECURITY_ENCRYP_104<<SECURITY_ENCRYP_SHIFT);
2722
2723         write_nic_byte(dev, SECURITY, security);
2724
2725         DMESG("key %x %x %x %x", read_nic_dword(dev, KEY0+4+4+4),
2726               read_nic_dword(dev, KEY0+4+4), read_nic_dword(dev, KEY0+4),
2727               read_nic_dword(dev, KEY0));
2728 }
2729
2730
2731 void rtl8185_rf_pins_enable(struct net_device *dev)
2732 {
2733         write_nic_word(dev, RFPinsEnable, 0x1fff); /* | tmp); */
2734 }
2735
2736 void rtl8185_set_anaparam2(struct net_device *dev, u32 a)
2737 {
2738         u8 conf3;
2739
2740         rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
2741
2742         conf3 = read_nic_byte(dev, CONFIG3);
2743         write_nic_byte(dev, CONFIG3, conf3 | (1<<CONFIG3_ANAPARAM_W_SHIFT));
2744         write_nic_dword(dev, ANAPARAM2, a);
2745
2746         conf3 = read_nic_byte(dev, CONFIG3);
2747         write_nic_byte(dev, CONFIG3, conf3 & ~(1<<CONFIG3_ANAPARAM_W_SHIFT));
2748         rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
2749 }
2750
2751 void rtl8180_set_anaparam(struct net_device *dev, u32 a)
2752 {
2753         u8 conf3;
2754
2755         rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
2756
2757         conf3 = read_nic_byte(dev, CONFIG3);
2758         write_nic_byte(dev, CONFIG3, conf3 | (1<<CONFIG3_ANAPARAM_W_SHIFT));
2759         write_nic_dword(dev, ANAPARAM, a);
2760
2761         conf3 = read_nic_byte(dev, CONFIG3);
2762         write_nic_byte(dev, CONFIG3, conf3 & ~(1<<CONFIG3_ANAPARAM_W_SHIFT));
2763         rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
2764 }
2765
2766 void rtl8185_tx_antenna(struct net_device *dev, u8 ant)
2767 {
2768         write_nic_byte(dev, TX_ANTENNA, ant);
2769         force_pci_posting(dev);
2770         mdelay(1);
2771 }
2772
2773 static void rtl8185_write_phy(struct net_device *dev, u8 adr, u32 data)
2774 {
2775         u32 phyw;
2776
2777         adr |= 0x80;
2778
2779         phyw = ((data<<8) | adr);
2780
2781         /* Note: we must write 0xff7c after 0x7d-0x7f to write BB register. */
2782         write_nic_byte(dev, 0x7f, ((phyw & 0xff000000) >> 24));
2783         write_nic_byte(dev, 0x7e, ((phyw & 0x00ff0000) >> 16));
2784         write_nic_byte(dev, 0x7d, ((phyw & 0x0000ff00) >> 8));
2785         write_nic_byte(dev, 0x7c, ((phyw & 0x000000ff)));
2786 }
2787
2788 inline void write_phy_ofdm(struct net_device *dev, u8 adr, u32 data)
2789 {
2790         data = data & 0xff;
2791         rtl8185_write_phy(dev, adr, data);
2792 }
2793
2794 void write_phy_cck(struct net_device *dev, u8 adr, u32 data)
2795 {
2796         data = data & 0xff;
2797         rtl8185_write_phy(dev, adr, data | 0x10000);
2798 }
2799
2800 /*
2801  * This configures registers for beacon tx and enables it via
2802  * rtl8180_beacon_tx_enable(). rtl8180_beacon_tx_disable() might
2803  * be used to stop beacon transmission
2804  */
2805 void rtl8180_start_tx_beacon(struct net_device *dev)
2806 {
2807         u16 word;
2808
2809         DMESG("Enabling beacon TX");
2810         rtl8180_prepare_beacon(dev);
2811         rtl8180_irq_disable(dev);
2812         rtl8180_beacon_tx_enable(dev);
2813
2814         word = read_nic_word(dev, AtimWnd) & ~AtimWnd_AtimWnd;
2815         write_nic_word(dev, AtimWnd, word); /* word |= */
2816
2817         word  = read_nic_word(dev, BintrItv);
2818         word &= ~BintrItv_BintrItv;
2819         word |= 1000; /* priv->ieee80211->current_network.beacon_interval *
2820                        * ((priv->txbeaconcount > 1)?(priv->txbeaconcount-1):1);
2821                        * FIXME: check if correct ^^ worked with 0x3e8;
2822                        */
2823         write_nic_word(dev, BintrItv, word);
2824
2825         rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
2826
2827         rtl8185b_irq_enable(dev);
2828 }
2829
2830 static struct net_device_stats *rtl8180_stats(struct net_device *dev)
2831 {
2832         struct r8180_priv *priv = ieee80211_priv(dev);
2833
2834         return &priv->ieee80211->stats;
2835 }
2836
2837 /*
2838  * Change current and default preamble mode.
2839  */
2840 static bool MgntActSet_802_11_PowerSaveMode(struct r8180_priv *priv,
2841                                      enum rt_ps_mode rtPsMode)
2842 {
2843         /* Currently, we do not change power save mode on IBSS mode. */
2844         if (priv->ieee80211->iw_mode == IW_MODE_ADHOC)
2845                 return false;
2846
2847         priv->ieee80211->ps = rtPsMode;
2848
2849         return true;
2850 }
2851
2852 static void LeisurePSEnter(struct r8180_priv *priv)
2853 {
2854         if (priv->bLeisurePs)
2855                 if (priv->ieee80211->ps == IEEE80211_PS_DISABLED)
2856                         /* IEEE80211_PS_ENABLE */
2857                         MgntActSet_802_11_PowerSaveMode(priv,
2858                                 IEEE80211_PS_MBCAST | IEEE80211_PS_UNICAST);
2859 }
2860
2861 static void LeisurePSLeave(struct r8180_priv *priv)
2862 {
2863         if (priv->bLeisurePs)
2864                 if (priv->ieee80211->ps != IEEE80211_PS_DISABLED)
2865                         MgntActSet_802_11_PowerSaveMode(
2866                                 priv, IEEE80211_PS_DISABLED);
2867 }
2868
2869 void rtl8180_hw_wakeup_wq(struct work_struct *work)
2870 {
2871         struct delayed_work *dwork = to_delayed_work(work);
2872         struct ieee80211_device *ieee = container_of(
2873                 dwork, struct ieee80211_device, hw_wakeup_wq);
2874         struct net_device *dev = ieee->dev;
2875
2876         rtl8180_hw_wakeup(dev);
2877 }
2878
2879 void rtl8180_hw_sleep_wq(struct work_struct *work)
2880 {
2881         struct delayed_work *dwork = to_delayed_work(work);
2882         struct ieee80211_device *ieee = container_of(
2883                 dwork, struct ieee80211_device, hw_sleep_wq);
2884         struct net_device *dev = ieee->dev;
2885
2886         rtl8180_hw_sleep_down(dev);
2887 }
2888
2889 static void MgntLinkKeepAlive(struct r8180_priv *priv)
2890 {
2891         if (priv->keepAliveLevel == 0)
2892                 return;
2893
2894         if (priv->ieee80211->state == IEEE80211_LINKED) {
2895                 /*
2896                  * Keep-Alive.
2897                  */
2898
2899                 if ((priv->keepAliveLevel == 2) ||
2900                         (priv->link_detect.last_num_tx_unicast ==
2901                                 priv->NumTxUnicast &&
2902                         priv->link_detect.last_num_rx_unicast ==
2903                                 priv->ieee80211->NumRxUnicast)
2904                         ) {
2905                         priv->link_detect.idle_count++;
2906
2907                         /*
2908                          * Send a Keep-Alive packet packet to AP if we had
2909                          * been idle for a while.
2910                          */
2911                         if (priv->link_detect.idle_count >=
2912                                 KEEP_ALIVE_INTERVAL /
2913                                 CHECK_FOR_HANG_PERIOD - 1) {
2914                                 priv->link_detect.idle_count = 0;
2915                                 ieee80211_sta_ps_send_null_frame(
2916                                         priv->ieee80211, false);
2917                         }
2918                 } else {
2919                         priv->link_detect.idle_count = 0;
2920                 }
2921                 priv->link_detect.last_num_tx_unicast = priv->NumTxUnicast;
2922                 priv->link_detect.last_num_rx_unicast =
2923                         priv->ieee80211->NumRxUnicast;
2924         }
2925 }
2926
2927 void rtl8180_watch_dog(struct net_device *dev)
2928 {
2929         struct r8180_priv *priv = ieee80211_priv(dev);
2930         bool bEnterPS = false;
2931         bool bBusyTraffic = false;
2932         u32 TotalRxNum = 0;
2933         u16 SlotIndex = 0;
2934         u16 i = 0;
2935         if (priv->ieee80211->actscanning == false) {
2936                 if ((priv->ieee80211->iw_mode != IW_MODE_ADHOC) &&
2937                     (priv->ieee80211->state == IEEE80211_NOLINK) &&
2938                     (priv->ieee80211->beinretry == false) &&
2939                     (priv->eRFPowerState == RF_ON))
2940                         IPSEnter(dev);
2941         }
2942         if ((priv->ieee80211->state == IEEE80211_LINKED) &&
2943                 (priv->ieee80211->iw_mode == IW_MODE_INFRA)) {
2944                 SlotIndex = (priv->link_detect.slot_index++) %
2945                         priv->link_detect.slot_num;
2946
2947                 priv->link_detect.rx_frame_num[SlotIndex] =
2948                         priv->ieee80211->NumRxDataInPeriod +
2949                         priv->ieee80211->NumRxBcnInPeriod;
2950
2951                 for (i = 0; i < priv->link_detect.slot_num; i++)
2952                         TotalRxNum += priv->link_detect.rx_frame_num[i];
2953
2954                 if (TotalRxNum == 0) {
2955                         priv->ieee80211->state = IEEE80211_ASSOCIATING;
2956                         queue_work(priv->ieee80211->wq,
2957                                 &priv->ieee80211->associate_procedure_wq);
2958                 }
2959         }
2960
2961         MgntLinkKeepAlive(priv);
2962
2963         LeisurePSLeave(priv);
2964
2965         if (priv->ieee80211->state == IEEE80211_LINKED) {
2966                 priv->link_detect.num_rx_ok_in_period =
2967                         priv->ieee80211->NumRxDataInPeriod;
2968                 if (priv->link_detect.num_rx_ok_in_period > 666 ||
2969                         priv->link_detect.num_tx_ok_in_period > 666) {
2970                         bBusyTraffic = true;
2971                 }
2972                 if ((priv->link_detect.num_rx_ok_in_period +
2973                         priv->link_detect.num_tx_ok_in_period > 8)
2974                         || (priv->link_detect.num_rx_ok_in_period > 2)) {
2975                         bEnterPS = false;
2976                 } else
2977                         bEnterPS = true;
2978
2979                 if (bEnterPS)
2980                         LeisurePSEnter(priv);
2981                 else
2982                         LeisurePSLeave(priv);
2983         } else
2984                 LeisurePSLeave(priv);
2985         priv->link_detect.b_busy_traffic = bBusyTraffic;
2986         priv->link_detect.num_rx_ok_in_period = 0;
2987         priv->link_detect.num_tx_ok_in_period = 0;
2988         priv->ieee80211->NumRxDataInPeriod = 0;
2989         priv->ieee80211->NumRxBcnInPeriod = 0;
2990 }
2991
2992 static int _rtl8180_up(struct net_device *dev)
2993 {
2994         struct r8180_priv *priv = ieee80211_priv(dev);
2995
2996         priv->up = 1;
2997
2998         DMESG("Bringing up iface");
2999         rtl8185b_adapter_start(dev);
3000         rtl8185b_rx_enable(dev);
3001         rtl8185b_tx_enable(dev);
3002         if (priv->bInactivePs) {
3003                 if (priv->ieee80211->iw_mode == IW_MODE_ADHOC)
3004                         IPSLeave(dev);
3005         }
3006         timer_rate_adaptive((unsigned long)dev);
3007         watch_dog_adaptive((unsigned long)dev);
3008         if (priv->bSwAntennaDiverity)
3009                         SwAntennaDiversityTimerCallback(dev);
3010         ieee80211_softmac_start_protocol(priv->ieee80211);
3011         return 0;
3012 }
3013
3014 static int rtl8180_open(struct net_device *dev)
3015 {
3016         struct r8180_priv *priv = ieee80211_priv(dev);
3017         int ret;
3018
3019         down(&priv->wx_sem);
3020         ret = rtl8180_up(dev);
3021         up(&priv->wx_sem);
3022         return ret;
3023 }
3024
3025 int rtl8180_up(struct net_device *dev)
3026 {
3027         struct r8180_priv *priv = ieee80211_priv(dev);
3028
3029         if (priv->up == 1)
3030                 return -1;
3031
3032         return _rtl8180_up(dev);
3033 }
3034
3035 static int rtl8180_close(struct net_device *dev)
3036 {
3037         struct r8180_priv *priv = ieee80211_priv(dev);
3038         int ret;
3039
3040         down(&priv->wx_sem);
3041         ret = rtl8180_down(dev);
3042         up(&priv->wx_sem);
3043
3044         return ret;
3045 }
3046
3047 int rtl8180_down(struct net_device *dev)
3048 {
3049         struct r8180_priv *priv = ieee80211_priv(dev);
3050
3051         if (priv->up == 0)
3052                 return -1;
3053
3054         priv->up = 0;
3055
3056         ieee80211_softmac_stop_protocol(priv->ieee80211);
3057         /* FIXME */
3058         if (!netif_queue_stopped(dev))
3059                 netif_stop_queue(dev);
3060         rtl8180_rtx_disable(dev);
3061         rtl8180_irq_disable(dev);
3062         del_timer_sync(&priv->watch_dog_timer);
3063         del_timer_sync(&priv->rateadapter_timer);
3064         cancel_delayed_work(&priv->ieee80211->rate_adapter_wq);
3065         cancel_delayed_work(&priv->ieee80211->hw_wakeup_wq);
3066         cancel_delayed_work(&priv->ieee80211->hw_sleep_wq);
3067         cancel_delayed_work(&priv->ieee80211->hw_dig_wq);
3068         cancel_delayed_work(&priv->ieee80211->tx_pw_wq);
3069         del_timer_sync(&priv->SwAntennaDiversityTimer);
3070         SetZebraRFPowerState8185(dev, RF_OFF);
3071         memset(&priv->ieee80211->current_network,
3072                 0, sizeof(struct ieee80211_network));
3073         priv->ieee80211->state = IEEE80211_NOLINK;
3074         return 0;
3075 }
3076
3077 void rtl8180_restart_wq(struct work_struct *work)
3078 {
3079         struct r8180_priv *priv = container_of(
3080                 work, struct r8180_priv, reset_wq);
3081         struct net_device *dev = priv->dev;
3082
3083         down(&priv->wx_sem);
3084
3085         rtl8180_commit(dev);
3086
3087         up(&priv->wx_sem);
3088 }
3089
3090 static void rtl8180_restart(struct net_device *dev)
3091 {
3092         struct r8180_priv *priv = ieee80211_priv(dev);
3093
3094         schedule_work(&priv->reset_wq);
3095 }
3096
3097 void rtl8180_commit(struct net_device *dev)
3098 {
3099         struct r8180_priv *priv = ieee80211_priv(dev);
3100
3101         if (priv->up == 0)
3102                 return;
3103
3104         del_timer_sync(&priv->watch_dog_timer);
3105         del_timer_sync(&priv->rateadapter_timer);
3106         cancel_delayed_work(&priv->ieee80211->rate_adapter_wq);
3107         cancel_delayed_work(&priv->ieee80211->hw_wakeup_wq);
3108         cancel_delayed_work(&priv->ieee80211->hw_sleep_wq);
3109         cancel_delayed_work(&priv->ieee80211->hw_dig_wq);
3110         cancel_delayed_work(&priv->ieee80211->tx_pw_wq);
3111         del_timer_sync(&priv->SwAntennaDiversityTimer);
3112         ieee80211_softmac_stop_protocol(priv->ieee80211);
3113         rtl8180_irq_disable(dev);
3114         rtl8180_rtx_disable(dev);
3115         _rtl8180_up(dev);
3116 }
3117
3118 static void r8180_set_multicast(struct net_device *dev)
3119 {
3120         struct r8180_priv *priv = ieee80211_priv(dev);
3121         short promisc;
3122
3123         promisc = (dev->flags & IFF_PROMISC) ? 1 : 0;
3124
3125         if (promisc != priv->promisc)
3126                 rtl8180_restart(dev);
3127
3128         priv->promisc = promisc;
3129 }
3130
3131 static int r8180_set_mac_adr(struct net_device *dev, void *mac)
3132 {
3133         struct r8180_priv *priv = ieee80211_priv(dev);
3134         struct sockaddr *addr = mac;
3135
3136         down(&priv->wx_sem);
3137
3138         memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
3139
3140         if (priv->ieee80211->iw_mode == IW_MODE_MASTER)
3141                 memcpy(priv->ieee80211->current_network.bssid,
3142                         dev->dev_addr, ETH_ALEN);
3143
3144         if (priv->up) {
3145                 rtl8180_down(dev);
3146                 rtl8180_up(dev);
3147         }
3148
3149         up(&priv->wx_sem);
3150
3151         return 0;
3152 }
3153
3154 /* based on ipw2200 driver */
3155 static int rtl8180_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3156 {
3157         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
3158         struct iwreq *wrq = (struct iwreq *) rq;
3159         int ret = -1;
3160
3161         switch (cmd) {
3162         case RTL_IOCTL_WPA_SUPPLICANT:
3163                 ret = ieee80211_wpa_supplicant_ioctl(
3164                         priv->ieee80211, &wrq->u.data);
3165                 return ret;
3166         default:
3167                 return -EOPNOTSUPP;
3168         }
3169
3170         return -EOPNOTSUPP;
3171 }
3172
3173 static const struct net_device_ops rtl8180_netdev_ops = {
3174         .ndo_open               = rtl8180_open,
3175         .ndo_stop               = rtl8180_close,
3176         .ndo_get_stats          = rtl8180_stats,
3177         .ndo_tx_timeout         = rtl8180_restart,
3178         .ndo_do_ioctl           = rtl8180_ioctl,
3179         .ndo_set_rx_mode        = r8180_set_multicast,
3180         .ndo_set_mac_address    = r8180_set_mac_adr,
3181         .ndo_validate_addr      = eth_validate_addr,
3182         .ndo_change_mtu         = eth_change_mtu,
3183         .ndo_start_xmit         = ieee80211_rtl_xmit,
3184 };
3185
3186 static int rtl8180_pci_probe(struct pci_dev *pdev,
3187                              const struct pci_device_id *id)
3188 {
3189         unsigned long ioaddr = 0;
3190         struct net_device *dev = NULL;
3191         struct r8180_priv *priv = NULL;
3192         u8 unit = 0;
3193         int ret = -ENODEV;
3194
3195         unsigned long pmem_start, pmem_len, pmem_flags;
3196
3197         DMESG("Configuring chip resources");
3198
3199         if (pci_enable_device(pdev)) {
3200                 DMESG("Failed to enable PCI device");
3201                 return -EIO;
3202         }
3203
3204         pci_set_master(pdev);
3205         pci_set_dma_mask(pdev, 0xffffff00ULL);
3206         pci_set_consistent_dma_mask(pdev, 0xffffff00ULL);
3207         dev = alloc_ieee80211(sizeof(struct r8180_priv));
3208         if (!dev) {
3209                 ret = -ENOMEM;
3210                 goto fail_free;
3211         }
3212         priv = ieee80211_priv(dev);
3213         priv->ieee80211 = netdev_priv(dev);
3214
3215         pci_set_drvdata(pdev, dev);
3216         SET_NETDEV_DEV(dev, &pdev->dev);
3217
3218         priv = ieee80211_priv(dev);
3219         priv->pdev = pdev;
3220
3221         pmem_start = pci_resource_start(pdev, 1);
3222         pmem_len = pci_resource_len(pdev, 1);
3223         pmem_flags = pci_resource_flags(pdev, 1);
3224
3225         if (!(pmem_flags & IORESOURCE_MEM)) {
3226                 DMESG("region #1 not a MMIO resource, aborting");
3227                 goto fail;
3228         }
3229
3230         if (!request_mem_region(pmem_start, pmem_len, RTL8180_MODULE_NAME)) {
3231                 DMESG("request_mem_region failed!");
3232                 goto fail;
3233         }
3234
3235         ioaddr = (unsigned long)ioremap_nocache(pmem_start, pmem_len);
3236         if (ioaddr == (unsigned long)NULL) {
3237                 DMESG("ioremap failed!");
3238                 goto fail1;
3239         }
3240
3241         dev->mem_start = ioaddr; /* shared mem start */
3242         dev->mem_end = ioaddr + pci_resource_len(pdev, 0); /* shared mem end */
3243
3244         pci_read_config_byte(pdev, 0x05, &unit);
3245         pci_write_config_byte(pdev, 0x05, unit & (~0x04));
3246
3247         dev->irq = pdev->irq;
3248         priv->irq = 0;
3249
3250         dev->netdev_ops = &rtl8180_netdev_ops;
3251         dev->wireless_handlers = &r8180_wx_handlers_def;
3252
3253         dev->type = ARPHRD_ETHER;
3254         dev->watchdog_timeo = HZ*3;
3255
3256         if (dev_alloc_name(dev, ifname) < 0) {
3257                 DMESG("Oops: devname already taken! Trying wlan%%d...\n");
3258                 strcpy(ifname, "wlan%d");
3259                 dev_alloc_name(dev, ifname);
3260         }
3261
3262         if (rtl8180_init(dev) != 0) {
3263                 DMESG("Initialization failed");
3264                 goto fail1;
3265         }
3266
3267         netif_carrier_off(dev);
3268
3269         if (register_netdev(dev))
3270                 goto fail1;
3271
3272         rtl8180_proc_init_one(dev);
3273
3274         DMESG("Driver probe completed\n");
3275         return 0;
3276 fail1:
3277         if (dev->mem_start != (unsigned long)NULL) {
3278                 iounmap((void __iomem *)dev->mem_start);
3279                 release_mem_region(pci_resource_start(pdev, 1),
3280                                    pci_resource_len(pdev, 1));
3281         }
3282 fail:
3283         if (dev) {
3284                 if (priv->irq) {
3285                         free_irq(dev->irq, dev);
3286                         dev->irq = 0;
3287                 }
3288                 free_ieee80211(dev);
3289         }
3290
3291 fail_free:
3292         pci_disable_device(pdev);
3293
3294         DMESG("wlan driver load failed\n");
3295         return ret;
3296 }
3297
3298 static void rtl8180_pci_remove(struct pci_dev *pdev)
3299 {
3300         struct r8180_priv *priv;
3301         struct net_device *dev = pci_get_drvdata(pdev);
3302
3303         if (dev) {
3304                 unregister_netdev(dev);
3305
3306                 priv = ieee80211_priv(dev);
3307
3308                 rtl8180_proc_remove_one(dev);
3309                 rtl8180_down(dev);
3310                 priv->rf_close(dev);
3311                 rtl8180_reset(dev);
3312                 mdelay(10);
3313
3314                 if (priv->irq) {
3315                         DMESG("Freeing irq %d", dev->irq);
3316                         free_irq(dev->irq, dev);
3317                         priv->irq = 0;
3318                 }
3319
3320                 free_rx_desc_ring(dev);
3321                 free_tx_desc_rings(dev);
3322
3323                 if (dev->mem_start != (unsigned long)NULL) {
3324                         iounmap((void __iomem *)dev->mem_start);
3325                         release_mem_region(pci_resource_start(pdev, 1),
3326                                            pci_resource_len(pdev, 1));
3327                 }
3328
3329                 free_ieee80211(dev);
3330         }
3331         pci_disable_device(pdev);
3332
3333         DMESG("wlan driver removed\n");
3334 }
3335
3336 static int __init rtl8180_pci_module_init(void)
3337 {
3338         int ret;
3339
3340         ret = ieee80211_crypto_init();
3341         if (ret) {
3342                 pr_err("ieee80211_crypto_init() failed %d\n", ret);
3343                 return ret;
3344         }
3345         ret = ieee80211_crypto_tkip_init();
3346         if (ret) {
3347                 pr_err("ieee80211_crypto_tkip_init() failed %d\n", ret);
3348                 return ret;
3349         }
3350         ret = ieee80211_crypto_ccmp_init();
3351         if (ret) {
3352                 pr_err("ieee80211_crypto_ccmp_init() failed %d\n", ret);
3353                 return ret;
3354         }
3355         ret = ieee80211_crypto_wep_init();
3356         if (ret) {
3357                 pr_err("ieee80211_crypto_wep_init() failed %d\n", ret);
3358                 return ret;
3359         }
3360
3361         pr_info("\nLinux kernel driver for RTL8180 / RTL8185 based WLAN cards\n");
3362         pr_info("Copyright (c) 2004-2005, Andrea Merello\n");
3363         DMESG("Initializing module");
3364         DMESG("Wireless extensions version %d", WIRELESS_EXT);
3365         rtl8180_proc_module_init();
3366
3367         if (pci_register_driver(&rtl8180_pci_driver)) {
3368                 DMESG("No device found");
3369                 return -ENODEV;
3370         }
3371         return 0;
3372 }
3373
3374 static void __exit rtl8180_pci_module_exit(void)
3375 {
3376         pci_unregister_driver(&rtl8180_pci_driver);
3377         rtl8180_proc_module_remove();
3378         ieee80211_crypto_tkip_exit();
3379         ieee80211_crypto_ccmp_exit();
3380         ieee80211_crypto_wep_exit();
3381         ieee80211_crypto_deinit();
3382         DMESG("Exiting");
3383 }
3384
3385 static void rtl8180_try_wake_queue(struct net_device *dev, int pri)
3386 {
3387         unsigned long flags;
3388         short enough_desc;
3389         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
3390
3391         spin_lock_irqsave(&priv->tx_lock, flags);
3392         enough_desc = check_nic_enought_desc(dev, pri);
3393         spin_unlock_irqrestore(&priv->tx_lock, flags);
3394
3395         if (enough_desc)
3396                 ieee80211_rtl_wake_queue(priv->ieee80211);
3397 }
3398
3399 static void rtl8180_tx_isr(struct net_device *dev, int pri, short error)
3400 {
3401         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
3402         u32 *tail; /* tail virtual addr */
3403         u32 *head; /* head virtual addr */
3404         u32 *begin; /* start of ring virtual addr */
3405         u32 *nicv; /* nic pointer virtual addr */
3406         u32 nic; /* nic pointer physical addr */
3407         u32 nicbegin; /* start of ring physical addr */
3408         unsigned long flag;
3409         /* physical addr are ok on 32 bits since we set DMA mask */
3410         int offs;
3411         int j, i;
3412         int hd;
3413         if (error)
3414                 priv->stats.txretry++;
3415         spin_lock_irqsave(&priv->tx_lock, flag);
3416         switch (pri) {
3417         case MANAGE_PRIORITY:
3418                 tail = priv->txmapringtail;
3419                 begin = priv->txmapring;
3420                 head = priv->txmapringhead;
3421                 nic = read_nic_dword(dev, TX_MANAGEPRIORITY_RING_ADDR);
3422                 nicbegin = priv->txmapringdma;
3423                 break;
3424         case BK_PRIORITY:
3425                 tail = priv->txbkpringtail;
3426                 begin = priv->txbkpring;
3427                 head = priv->txbkpringhead;
3428                 nic = read_nic_dword(dev, TX_BKPRIORITY_RING_ADDR);
3429                 nicbegin = priv->txbkpringdma;
3430                 break;
3431         case BE_PRIORITY:
3432                 tail = priv->txbepringtail;
3433                 begin = priv->txbepring;
3434                 head = priv->txbepringhead;
3435                 nic = read_nic_dword(dev, TX_BEPRIORITY_RING_ADDR);
3436                 nicbegin = priv->txbepringdma;
3437                 break;
3438         case VI_PRIORITY:
3439                 tail = priv->txvipringtail;
3440                 begin = priv->txvipring;
3441                 head = priv->txvipringhead;
3442                 nic = read_nic_dword(dev, TX_VIPRIORITY_RING_ADDR);
3443                 nicbegin = priv->txvipringdma;
3444                 break;
3445         case VO_PRIORITY:
3446                 tail = priv->txvopringtail;
3447                 begin = priv->txvopring;
3448                 head = priv->txvopringhead;
3449                 nic = read_nic_dword(dev, TX_VOPRIORITY_RING_ADDR);
3450                 nicbegin = priv->txvopringdma;
3451                 break;
3452         case HI_PRIORITY:
3453                 tail = priv->txhpringtail;
3454                 begin = priv->txhpring;
3455                 head = priv->txhpringhead;
3456                 nic = read_nic_dword(dev, TX_HIGHPRIORITY_RING_ADDR);
3457                 nicbegin = priv->txhpringdma;
3458                 break;
3459
3460         default:
3461                 spin_unlock_irqrestore(&priv->tx_lock, flag);
3462                 return;
3463         }
3464
3465         nicv = (u32 *)((nic - nicbegin) + (u8 *)begin);
3466         if ((head <= tail && (nicv > tail || nicv < head)) ||
3467                 (head > tail && (nicv > tail && nicv < head))) {
3468                         DMESGW("nic has lost pointer");
3469                         spin_unlock_irqrestore(&priv->tx_lock, flag);
3470                         rtl8180_restart(dev);
3471                         return;
3472                 }
3473
3474         /*
3475          * We check all the descriptors between the head and the nic,
3476          * but not the currently pointed by the nic (the next to be txed)
3477          * and the previous of the pointed (might be in process ??)
3478          */
3479         offs = (nic - nicbegin);
3480         offs = offs / 8 / 4;
3481         hd = (head - begin) / 8;
3482
3483         if (offs >= hd)
3484                 j = offs - hd;
3485         else
3486                 j = offs + (priv->txringcount-1-hd);
3487
3488         j -= 2;
3489         if (j < 0)
3490                 j = 0;
3491
3492         for (i = 0; i < j; i++) {
3493                 if ((*head) & (1<<31))
3494                         break;
3495                 if (((*head)&(0x10000000)) != 0) {
3496                         priv->CurrRetryCnt += (u16)((*head) & (0x000000ff));
3497                         if (!error)
3498                                 priv->NumTxOkTotal++;
3499                 }
3500
3501                 if (!error)
3502                         priv->NumTxOkBytesTotal += (*(head+3)) & (0x00000fff);
3503
3504                 *head = *head & ~(1<<31);
3505
3506                 if ((head - begin)/8 == priv->txringcount-1)
3507                         head = begin;
3508                 else
3509                         head += 8;
3510         }
3511
3512         /*
3513          * The head has been moved to the last certainly TXed
3514          * (or at least processed by the nic) packet.
3515          * The driver take forcefully owning of all these packets
3516          * If the packet previous of the nic pointer has been
3517          * processed this doesn't matter: it will be checked
3518          * here at the next round. Anyway if no more packet are
3519          * TXed no memory leak occur at all.
3520          */
3521
3522         switch (pri) {
3523         case MANAGE_PRIORITY:
3524                 priv->txmapringhead = head;
3525
3526                 if (priv->ack_tx_to_ieee) {
3527                         if (rtl8180_is_tx_queue_empty(dev)) {
3528                                 priv->ack_tx_to_ieee = 0;
3529                                 ieee80211_ps_tx_ack(priv->ieee80211, !error);
3530                         }
3531                 }
3532                 break;
3533         case BK_PRIORITY:
3534                 priv->txbkpringhead = head;
3535                 break;
3536         case BE_PRIORITY:
3537                 priv->txbepringhead = head;
3538                 break;
3539         case VI_PRIORITY:
3540                 priv->txvipringhead = head;
3541                 break;
3542         case VO_PRIORITY:
3543                 priv->txvopringhead = head;
3544                 break;
3545         case HI_PRIORITY:
3546                 priv->txhpringhead = head;
3547                 break;
3548         }
3549
3550         spin_unlock_irqrestore(&priv->tx_lock, flag);
3551 }
3552
3553 static irqreturn_t rtl8180_interrupt(int irq, void *netdev)
3554 {
3555         struct net_device *dev = (struct net_device *) netdev;
3556         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
3557         unsigned long flags;
3558         u32 inta;
3559
3560         /* We should return IRQ_NONE, but for now let me keep this */
3561         if (priv->irq_enabled == 0)
3562                 return IRQ_HANDLED;
3563
3564         spin_lock_irqsave(&priv->irq_th_lock, flags);
3565
3566         /* ISR: 4bytes */
3567         inta = read_nic_dword(dev, ISR);
3568         write_nic_dword(dev, ISR, inta); /* reset int situation */
3569
3570         priv->stats.shints++;
3571
3572         if (!inta) {
3573                 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
3574                 return IRQ_HANDLED;
3575         /*
3576          * most probably we can safely return IRQ_NONE,
3577          * but for now is better to avoid problems
3578          */
3579         }
3580
3581         if (inta == 0xffff) {
3582                 /* HW disappeared */
3583                 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
3584                 return IRQ_HANDLED;
3585         }
3586
3587         priv->stats.ints++;
3588
3589         if (!netif_running(dev)) {
3590                 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
3591                 return IRQ_HANDLED;
3592         }
3593
3594         if (inta & ISR_TimeOut)
3595                 write_nic_dword(dev, TimerInt, 0);
3596
3597         if (inta & ISR_TBDOK)
3598                 priv->stats.txbeacon++;
3599
3600         if (inta & ISR_TBDER)
3601                 priv->stats.txbeaconerr++;
3602
3603         if (inta & IMR_TMGDOK)
3604                 rtl8180_tx_isr(dev, MANAGE_PRIORITY, 0);
3605
3606         if (inta & ISR_THPDER) {
3607                 priv->stats.txhperr++;
3608                 rtl8180_tx_isr(dev, HI_PRIORITY, 1);
3609                 priv->ieee80211->stats.tx_errors++;
3610         }
3611
3612         if (inta & ISR_THPDOK) { /* High priority tx ok */
3613                 priv->link_detect.num_tx_ok_in_period++;
3614                 priv->stats.txhpokint++;
3615                 rtl8180_tx_isr(dev, HI_PRIORITY, 0);
3616         }
3617
3618         if (inta & ISR_RER)
3619                 priv->stats.rxerr++;
3620
3621         if (inta & ISR_TBKDER) { /* corresponding to BK_PRIORITY */
3622                 priv->stats.txbkperr++;
3623                 priv->ieee80211->stats.tx_errors++;
3624                 rtl8180_tx_isr(dev, BK_PRIORITY, 1);
3625                 rtl8180_try_wake_queue(dev, BK_PRIORITY);
3626         }
3627
3628         if (inta & ISR_TBEDER) { /* corresponding to BE_PRIORITY */
3629                 priv->stats.txbeperr++;
3630                 priv->ieee80211->stats.tx_errors++;
3631                 rtl8180_tx_isr(dev, BE_PRIORITY, 1);
3632                 rtl8180_try_wake_queue(dev, BE_PRIORITY);
3633         }
3634         if (inta & ISR_TNPDER) { /* corresponding to VO_PRIORITY */
3635                 priv->stats.txnperr++;
3636                 priv->ieee80211->stats.tx_errors++;
3637                 rtl8180_tx_isr(dev, NORM_PRIORITY, 1);
3638                 rtl8180_try_wake_queue(dev, NORM_PRIORITY);
3639         }
3640
3641         if (inta & ISR_TLPDER) { /* corresponding to VI_PRIORITY */
3642                 priv->stats.txlperr++;
3643                 priv->ieee80211->stats.tx_errors++;
3644                 rtl8180_tx_isr(dev, LOW_PRIORITY, 1);
3645                 rtl8180_try_wake_queue(dev, LOW_PRIORITY);
3646         }
3647
3648         if (inta & ISR_ROK) {
3649                 priv->stats.rxint++;
3650                 tasklet_schedule(&priv->irq_rx_tasklet);
3651         }
3652
3653         if (inta & ISR_RQoSOK) {
3654                 priv->stats.rxint++;
3655                 tasklet_schedule(&priv->irq_rx_tasklet);
3656         }
3657
3658         if (inta & ISR_BcnInt)
3659                 rtl8180_prepare_beacon(dev);
3660
3661         if (inta & ISR_RDU) {
3662                 DMESGW("No RX descriptor available");
3663                 priv->stats.rxrdu++;
3664                 tasklet_schedule(&priv->irq_rx_tasklet);
3665         }
3666
3667         if (inta & ISR_RXFOVW) {
3668                 priv->stats.rxoverflow++;
3669                 tasklet_schedule(&priv->irq_rx_tasklet);
3670         }
3671
3672         if (inta & ISR_TXFOVW)
3673                 priv->stats.txoverflow++;
3674
3675         if (inta & ISR_TNPDOK) { /* Normal priority tx ok */
3676                 priv->link_detect.num_tx_ok_in_period++;
3677                 priv->stats.txnpokint++;
3678                 rtl8180_tx_isr(dev, NORM_PRIORITY, 0);
3679                 rtl8180_try_wake_queue(dev, NORM_PRIORITY);
3680         }
3681
3682         if (inta & ISR_TLPDOK) { /* Low priority tx ok */
3683                 priv->link_detect.num_tx_ok_in_period++;
3684                 priv->stats.txlpokint++;
3685                 rtl8180_tx_isr(dev, LOW_PRIORITY, 0);
3686                 rtl8180_try_wake_queue(dev, LOW_PRIORITY);
3687         }
3688
3689         if (inta & ISR_TBKDOK) { /* corresponding to BK_PRIORITY */
3690                 priv->stats.txbkpokint++;
3691                 priv->link_detect.num_tx_ok_in_period++;
3692                 rtl8180_tx_isr(dev, BK_PRIORITY, 0);
3693                 rtl8180_try_wake_queue(dev, BE_PRIORITY);
3694         }
3695
3696         if (inta & ISR_TBEDOK) { /* corresponding to BE_PRIORITY */
3697                 priv->stats.txbeperr++;
3698                 priv->link_detect.num_tx_ok_in_period++;
3699                 rtl8180_tx_isr(dev, BE_PRIORITY, 0);
3700                 rtl8180_try_wake_queue(dev, BE_PRIORITY);
3701         }
3702         force_pci_posting(dev);
3703         spin_unlock_irqrestore(&priv->irq_th_lock, flags);
3704
3705         return IRQ_HANDLED;
3706 }
3707
3708 void rtl8180_irq_rx_tasklet(struct r8180_priv *priv)
3709 {
3710         rtl8180_rx(priv->dev);
3711 }
3712
3713 void GPIOChangeRFWorkItemCallBack(struct work_struct *work)
3714 {
3715         struct ieee80211_device *ieee = container_of(
3716                 work, struct ieee80211_device, GPIOChangeRFWorkItem.work);
3717         struct net_device *dev = ieee->dev;
3718         struct r8180_priv *priv = ieee80211_priv(dev);
3719         u8 btPSR;
3720         u8 btConfig0;
3721         enum rt_rf_power_state eRfPowerStateToSet;
3722         bool bActuallySet = false;
3723
3724         char *argv[3];
3725         static char *RadioPowerPath = "/etc/acpi/events/RadioPower.sh";
3726         static char *envp[] = {"HOME=/", "TERM=linux",
3727                 "PATH=/usr/bin:/bin", NULL};
3728         static int readf_count;
3729
3730         readf_count = (readf_count+1)%0xffff;
3731         /* We should turn off LED before polling FF51[4]. */
3732
3733         /* Turn off LED. */
3734         btPSR = read_nic_byte(dev, PSR);
3735         write_nic_byte(dev, PSR, (btPSR & ~BIT3));
3736
3737         /* It need to delay 4us suggested */
3738         udelay(4);
3739
3740         /* HW radio On/Off according to the value of FF51[4](config0) */
3741         btConfig0 = btPSR = read_nic_byte(dev, CONFIG0);
3742
3743         eRfPowerStateToSet = (btConfig0 & BIT4) ?  RF_ON : RF_OFF;
3744
3745         /* Turn LED back on when radio enabled */
3746         if (eRfPowerStateToSet == RF_ON)
3747                 write_nic_byte(dev, PSR, btPSR | BIT3);
3748
3749         if ((priv->ieee80211->bHwRadioOff == true) &&
3750            (eRfPowerStateToSet == RF_ON)) {
3751                 priv->ieee80211->bHwRadioOff = false;
3752                 bActuallySet = true;
3753         } else if ((priv->ieee80211->bHwRadioOff == false) &&
3754                   (eRfPowerStateToSet == RF_OFF)) {
3755                 priv->ieee80211->bHwRadioOff = true;
3756                 bActuallySet = true;
3757         }
3758
3759         if (bActuallySet) {
3760                 MgntActSet_RF_State(dev, eRfPowerStateToSet, RF_CHANGE_BY_HW);
3761
3762                 /* To update the UI status for Power status changed */
3763                 if (priv->ieee80211->bHwRadioOff == true)
3764                         argv[1] = "RFOFF";
3765                 else
3766                         argv[1] = "RFON";
3767                 argv[0] = RadioPowerPath;
3768                 argv[2] = NULL;
3769
3770                 call_usermodehelper(RadioPowerPath, argv, envp, UMH_WAIT_PROC);
3771         }
3772 }
3773
3774 module_init(rtl8180_pci_module_init);
3775 module_exit(rtl8180_pci_module_exit);