2 *************************************************************************
4 * 5F., No.36, Taiyuan St., Jhubei City,
8 * (c) Copyright 2002-2007, Ralink Technology, Inc.
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., *
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 *************************************************************************
31 Create and register network interface.
35 -------- ---------- ----------------------------------------------
38 #include "rt_config.h"
40 extern INT __devinit rt28xx_probe(IN void *_dev_p, IN void *_dev_id_p,
41 IN UINT argc, OUT PRTMP_ADAPTER *ppAd);
43 static void rx_done_tasklet(unsigned long data);
44 static void mgmt_dma_done_tasklet(unsigned long data);
45 static void ac0_dma_done_tasklet(unsigned long data);
46 static void ac1_dma_done_tasklet(unsigned long data);
47 static void ac2_dma_done_tasklet(unsigned long data);
48 static void ac3_dma_done_tasklet(unsigned long data);
49 static void hcca_dma_done_tasklet(unsigned long data);
50 static void fifo_statistic_full_tasklet(unsigned long data);
53 /*---------------------------------------------------------------------*/
54 /* Symbol & Macro Definitions */
55 /*---------------------------------------------------------------------*/
56 #define RT2860_INT_RX_DLY (1<<0) // bit 0
57 #define RT2860_INT_TX_DLY (1<<1) // bit 1
58 #define RT2860_INT_RX_DONE (1<<2) // bit 2
59 #define RT2860_INT_AC0_DMA_DONE (1<<3) // bit 3
60 #define RT2860_INT_AC1_DMA_DONE (1<<4) // bit 4
61 #define RT2860_INT_AC2_DMA_DONE (1<<5) // bit 5
62 #define RT2860_INT_AC3_DMA_DONE (1<<6) // bit 6
63 #define RT2860_INT_HCCA_DMA_DONE (1<<7) // bit 7
64 #define RT2860_INT_MGMT_DONE (1<<8) // bit 8
66 #define INT_RX RT2860_INT_RX_DONE
68 #define INT_AC0_DLY (RT2860_INT_AC0_DMA_DONE) //| RT2860_INT_TX_DLY)
69 #define INT_AC1_DLY (RT2860_INT_AC1_DMA_DONE) //| RT2860_INT_TX_DLY)
70 #define INT_AC2_DLY (RT2860_INT_AC2_DMA_DONE) //| RT2860_INT_TX_DLY)
71 #define INT_AC3_DLY (RT2860_INT_AC3_DMA_DONE) //| RT2860_INT_TX_DLY)
72 #define INT_HCCA_DLY (RT2860_INT_HCCA_DMA_DONE) //| RT2860_INT_TX_DLY)
73 #define INT_MGMT_DLY RT2860_INT_MGMT_DONE
75 /*---------------------------------------------------------------------*/
76 /* Prototypes of Functions Used */
77 /*---------------------------------------------------------------------*/
78 /* function declarations */
79 static INT __devinit rt2860_init_one (struct pci_dev *pci_dev, const struct pci_device_id *ent);
80 static VOID __devexit rt2860_remove_one(struct pci_dev *pci_dev);
81 static INT __devinit rt2860_probe(struct pci_dev *pci_dev, const struct pci_device_id *ent);
82 void init_thread_task(PRTMP_ADAPTER pAd);
83 static void __exit rt2860_cleanup_module(void);
84 static int __init rt2860_init_module(void);
87 static int rt2860_suspend(struct pci_dev *pci_dev, pm_message_t state);
88 static int rt2860_resume(struct pci_dev *pci_dev);
89 #endif // CONFIG_PM //
93 // Ralink PCI device table, include all supported chipsets
95 static struct pci_device_id rt2860_pci_tbl[] __devinitdata =
97 {PCI_DEVICE(NIC_PCI_VENDOR_ID, NIC2860_PCI_DEVICE_ID)}, //RT28602.4G
98 {PCI_DEVICE(NIC_PCI_VENDOR_ID, NIC2860_PCIe_DEVICE_ID)},
99 {PCI_DEVICE(NIC_PCI_VENDOR_ID, NIC2760_PCI_DEVICE_ID)},
100 {PCI_DEVICE(NIC_PCI_VENDOR_ID, NIC2790_PCIe_DEVICE_ID)},
101 {PCI_DEVICE(VEN_AWT_PCI_VENDOR_ID, VEN_AWT_PCIe_DEVICE_ID)},
102 {0,} // terminate list
105 MODULE_DEVICE_TABLE(pci, rt2860_pci_tbl);
106 MODULE_LICENSE("GPL");
107 #ifdef MODULE_VERSION
108 MODULE_VERSION(STA_DRIVER_VERSION);
112 // Our PCI driver structure
114 static struct pci_driver rt2860_driver =
117 id_table: rt2860_pci_tbl,
118 probe: rt2860_init_one,
119 remove: __devexit_p(rt2860_remove_one),
122 suspend: rt2860_suspend,
123 resume: rt2860_resume,
130 VOID RT2860RejectPendingPackets(
131 IN PRTMP_ADAPTER pAd)
134 // clear TxSw packets
137 static int rt2860_suspend(
138 struct pci_dev *pci_dev,
141 struct net_device *net_dev = pci_get_drvdata(pci_dev);
142 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)NULL;
146 DBGPRINT(RT_DEBUG_TRACE, ("===> rt2860_suspend()\n"));
150 DBGPRINT(RT_DEBUG_ERROR, ("net_dev == NULL!\n"));
154 pAd = net_dev->ml_priv;
156 /* we can not use IFF_UP because ra0 down but ra1 up */
157 /* and 1 suspend/resume function for 1 module, not for each interface */
158 /* so Linux will call suspend/resume function once */
159 if (VIRTUAL_IF_NUM(pAd) > 0)
161 // avoid users do suspend after interface is down
164 netif_carrier_off(net_dev);
165 netif_stop_queue(net_dev);
167 // mark device as removed from system and therefore no longer available
168 netif_device_detach(net_dev);
171 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
172 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_RADIO_OFF);
174 // take down the device
175 rt28xx_close((PNET_DEV)net_dev);
177 RT_MOD_DEC_USE_COUNT();
181 // reference to http://vovo2000.com/type-lab/linux/kernel-api/linux-kernel-api.html
182 // enable device to generate PME# when suspended
183 // pci_choose_state(): Choose the power state of a PCI device to be suspended
184 retval = pci_enable_wake(pci_dev, pci_choose_state(pci_dev, state), 1);
185 // save the PCI configuration space of a device before suspending
186 pci_save_state(pci_dev);
187 // disable PCI device after use
188 pci_disable_device(pci_dev);
190 retval = pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
192 DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2860_suspend()\n"));
196 static int rt2860_resume(
197 struct pci_dev *pci_dev)
199 struct net_device *net_dev = pci_get_drvdata(pci_dev);
200 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)NULL;
204 // set the power state of a PCI device
205 // PCI has 4 power states, DO (normal) ~ D3(less power)
206 // in include/linux/pci.h, you can find that
207 // #define PCI_D0 ((pci_power_t __force) 0)
208 // #define PCI_D1 ((pci_power_t __force) 1)
209 // #define PCI_D2 ((pci_power_t __force) 2)
210 // #define PCI_D3hot ((pci_power_t __force) 3)
211 // #define PCI_D3cold ((pci_power_t __force) 4)
212 // #define PCI_UNKNOWN ((pci_power_t __force) 5)
213 // #define PCI_POWER_ERROR ((pci_power_t __force) -1)
214 retval = pci_set_power_state(pci_dev, PCI_D0);
216 // restore the saved state of a PCI device
217 pci_restore_state(pci_dev);
219 // initialize device before it's used by a driver
220 if (pci_enable_device(pci_dev))
222 printk("pci enable fail!\n");
226 DBGPRINT(RT_DEBUG_TRACE, ("===> rt2860_resume()\n"));
230 DBGPRINT(RT_DEBUG_ERROR, ("net_dev == NULL!\n"));
233 pAd = net_dev->ml_priv;
237 /* we can not use IFF_UP because ra0 down but ra1 up */
238 /* and 1 suspend/resume function for 1 module, not for each interface */
239 /* so Linux will call suspend/resume function once */
240 if (VIRTUAL_IF_NUM(pAd) > 0)
242 // mark device as attached from system and restart if needed
243 netif_device_attach(net_dev);
245 if (rt28xx_open((PNET_DEV)net_dev) != 0)
248 DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2860_resume()\n"));
252 // increase MODULE use count
253 RT_MOD_INC_USE_COUNT();
255 RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
256 RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_RADIO_OFF);
258 netif_start_queue(net_dev);
259 netif_carrier_on(net_dev);
260 netif_wake_queue(net_dev);
264 DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2860_resume()\n"));
267 #endif // CONFIG_PM //
270 static INT __init rt2860_init_module(VOID)
272 return pci_register_driver(&rt2860_driver);
277 // Driver module unload function
279 static VOID __exit rt2860_cleanup_module(VOID)
281 pci_unregister_driver(&rt2860_driver);
284 module_init(rt2860_init_module);
285 module_exit(rt2860_cleanup_module);
288 static INT __devinit rt2860_init_one (
289 IN struct pci_dev *pci_dev,
290 IN const struct pci_device_id *ent)
294 DBGPRINT(RT_DEBUG_TRACE, ("===> rt2860_init_one\n"));
296 // wake up and enable device
297 if (pci_enable_device (pci_dev))
303 rc = rt2860_probe(pci_dev, ent);
306 DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2860_init_one\n"));
311 static VOID __devexit rt2860_remove_one(
312 IN struct pci_dev *pci_dev)
314 struct net_device *net_dev = pci_get_drvdata(pci_dev);
315 RTMP_ADAPTER *pAd = net_dev->ml_priv;
317 DBGPRINT(RT_DEBUG_TRACE, ("===> rt2860_remove_one\n"));
321 // Unregister network device
322 unregister_netdev(net_dev);
324 // Unmap CSR base address
325 iounmap((char *)(net_dev->base_addr));
327 RTMPFreeAdapter(pAd);
329 // release memory region
330 release_mem_region(pci_resource_start(pci_dev, 0), pci_resource_len(pci_dev, 0));
334 // Unregister network device
335 unregister_netdev(net_dev);
337 // Unmap CSR base address
338 iounmap((char *)(net_dev->base_addr));
340 // release memory region
341 release_mem_region(pci_resource_start(pci_dev, 0), pci_resource_len(pci_dev, 0));
344 // Free pre-allocated net_device memory
345 free_netdev(net_dev);
349 // PCI device probe & initialization function
351 static INT __devinit rt2860_probe(
352 IN struct pci_dev *pci_dev,
353 IN const struct pci_device_id *ent)
358 rv = (INT)rt28xx_probe((void *)pci_dev, (void *)ent, 0, &pAd);
359 OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_ADVANCE_POWER_SAVE_PCIE_DEVICE);
364 void init_thread_task(IN PRTMP_ADAPTER pAd)
368 pObj = (POS_COOKIE) pAd->OS_Cookie;
370 tasklet_init(&pObj->rx_done_task, rx_done_tasklet, (unsigned long)pAd);
371 tasklet_init(&pObj->mgmt_dma_done_task, mgmt_dma_done_tasklet, (unsigned long)pAd);
372 tasklet_init(&pObj->ac0_dma_done_task, ac0_dma_done_tasklet, (unsigned long)pAd);
373 tasklet_init(&pObj->ac1_dma_done_task, ac1_dma_done_tasklet, (unsigned long)pAd);
374 tasklet_init(&pObj->ac2_dma_done_task, ac2_dma_done_tasklet, (unsigned long)pAd);
375 tasklet_init(&pObj->ac3_dma_done_task, ac3_dma_done_tasklet, (unsigned long)pAd);
376 tasklet_init(&pObj->hcca_dma_done_task, hcca_dma_done_tasklet, (unsigned long)pAd);
377 tasklet_init(&pObj->tbtt_task, tbtt_tasklet, (unsigned long)pAd);
378 tasklet_init(&pObj->fifo_statistic_full_task, fifo_statistic_full_tasklet, (unsigned long)pAd);
381 void kill_thread_task(IN PRTMP_ADAPTER pAd)
385 pObj = (POS_COOKIE) pAd->OS_Cookie;
387 tasklet_kill(&pObj->rx_done_task);
388 tasklet_kill(&pObj->mgmt_dma_done_task);
389 tasklet_kill(&pObj->ac0_dma_done_task);
390 tasklet_kill(&pObj->ac1_dma_done_task);
391 tasklet_kill(&pObj->ac2_dma_done_task);
392 tasklet_kill(&pObj->ac3_dma_done_task);
393 tasklet_kill(&pObj->hcca_dma_done_task);
394 tasklet_kill(&pObj->tbtt_task);
395 tasklet_kill(&pObj->fifo_statistic_full_task);
399 static void rt2860_int_enable(PRTMP_ADAPTER pAd, unsigned int mode)
403 pAd->int_disable_mask &= ~(mode);
404 regValue = pAd->int_enable_reg & ~(pAd->int_disable_mask);
405 RTMP_IO_WRITE32(pAd, INT_MASK_CSR, regValue); // 1:enable
408 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_ACTIVE);
412 static void rt2860_int_disable(PRTMP_ADAPTER pAd, unsigned int mode)
416 pAd->int_disable_mask |= mode;
417 regValue = pAd->int_enable_reg & ~(pAd->int_disable_mask);
418 RTMP_IO_WRITE32(pAd, INT_MASK_CSR, regValue); // 0: disable
422 RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_ACTIVE);
426 static void mgmt_dma_done_tasklet(unsigned long data)
429 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
430 INT_SOURCE_CSR_STRUC IntSource;
433 // Do nothing if the driver is starting halt state.
434 // This might happen when timer already been fired before cancel timer with mlmehalt
435 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
438 pObj = (POS_COOKIE) pAd->OS_Cookie;
441 IntSource.field.MgmtDmaDone = 1;
442 pAd->int_pending &= ~INT_MGMT_DLY;
444 RTMPHandleMgmtRingDmaDoneInterrupt(pAd);
446 // if you use RTMP_SEM_LOCK, sometimes kernel will hang up, no any
448 RTMP_INT_LOCK(&pAd->irq_lock, flags);
450 * double check to avoid lose of interrupts
452 if (pAd->int_pending & INT_MGMT_DLY)
454 tasklet_hi_schedule(&pObj->mgmt_dma_done_task);
455 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
459 /* enable TxDataInt again */
460 rt2860_int_enable(pAd, INT_MGMT_DLY);
461 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
464 static void rx_done_tasklet(unsigned long data)
467 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
468 BOOLEAN bReschedule = 0;
471 // Do nothing if the driver is starting halt state.
472 // This might happen when timer already been fired before cancel timer with mlmehalt
473 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
476 pObj = (POS_COOKIE) pAd->OS_Cookie;
478 pAd->int_pending &= ~(INT_RX);
480 bReschedule = STARxDoneInterruptHandle(pAd, 0);
482 RTMP_INT_LOCK(&pAd->irq_lock, flags);
484 * double check to avoid rotting packet
486 if (pAd->int_pending & INT_RX || bReschedule)
488 tasklet_hi_schedule(&pObj->rx_done_task);
489 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
493 /* enable RxINT again */
494 rt2860_int_enable(pAd, INT_RX);
495 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
499 void fifo_statistic_full_tasklet(unsigned long data)
502 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
505 // Do nothing if the driver is starting halt state.
506 // This might happen when timer already been fired before cancel timer with mlmehalt
507 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
510 pObj = (POS_COOKIE) pAd->OS_Cookie;
512 pAd->int_pending &= ~(FifoStaFullInt);
513 NICUpdateFifoStaCounters(pAd);
515 RTMP_INT_LOCK(&pAd->irq_lock, flags);
517 * double check to avoid rotting packet
519 if (pAd->int_pending & FifoStaFullInt)
521 tasklet_hi_schedule(&pObj->fifo_statistic_full_task);
522 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
526 /* enable RxINT again */
528 rt2860_int_enable(pAd, FifoStaFullInt);
529 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
533 static void hcca_dma_done_tasklet(unsigned long data)
536 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
537 INT_SOURCE_CSR_STRUC IntSource;
540 // Do nothing if the driver is starting halt state.
541 // This might happen when timer already been fired before cancel timer with mlmehalt
542 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
545 pObj = (POS_COOKIE) pAd->OS_Cookie;
549 IntSource.field.HccaDmaDone = 1;
550 pAd->int_pending &= ~INT_HCCA_DLY;
552 RTMPHandleTxRingDmaDoneInterrupt(pAd, IntSource);
554 RTMP_INT_LOCK(&pAd->irq_lock, flags);
556 * double check to avoid lose of interrupts
558 if (pAd->int_pending & INT_HCCA_DLY)
560 tasklet_hi_schedule(&pObj->hcca_dma_done_task);
561 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
565 /* enable TxDataInt again */
566 rt2860_int_enable(pAd, INT_HCCA_DLY);
567 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
570 static void ac3_dma_done_tasklet(unsigned long data)
573 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
574 INT_SOURCE_CSR_STRUC IntSource;
576 BOOLEAN bReschedule = 0;
578 // Do nothing if the driver is starting halt state.
579 // This might happen when timer already been fired before cancel timer with mlmehalt
580 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
583 pObj = (POS_COOKIE) pAd->OS_Cookie;
586 IntSource.field.Ac3DmaDone = 1;
587 pAd->int_pending &= ~INT_AC3_DLY;
589 bReschedule = RTMPHandleTxRingDmaDoneInterrupt(pAd, IntSource);
591 RTMP_INT_LOCK(&pAd->irq_lock, flags);
593 * double check to avoid lose of interrupts
595 if ((pAd->int_pending & INT_AC3_DLY) || bReschedule)
597 tasklet_hi_schedule(&pObj->ac3_dma_done_task);
598 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
602 /* enable TxDataInt again */
603 rt2860_int_enable(pAd, INT_AC3_DLY);
604 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
607 static void ac2_dma_done_tasklet(unsigned long data)
610 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
611 INT_SOURCE_CSR_STRUC IntSource;
613 BOOLEAN bReschedule = 0;
615 // Do nothing if the driver is starting halt state.
616 // This might happen when timer already been fired before cancel timer with mlmehalt
617 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
620 pObj = (POS_COOKIE) pAd->OS_Cookie;
623 IntSource.field.Ac2DmaDone = 1;
624 pAd->int_pending &= ~INT_AC2_DLY;
626 bReschedule = RTMPHandleTxRingDmaDoneInterrupt(pAd, IntSource);
628 RTMP_INT_LOCK(&pAd->irq_lock, flags);
631 * double check to avoid lose of interrupts
633 if ((pAd->int_pending & INT_AC2_DLY) || bReschedule)
635 tasklet_hi_schedule(&pObj->ac2_dma_done_task);
636 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
640 /* enable TxDataInt again */
641 rt2860_int_enable(pAd, INT_AC2_DLY);
642 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
645 static void ac1_dma_done_tasklet(unsigned long data)
648 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
649 INT_SOURCE_CSR_STRUC IntSource;
651 BOOLEAN bReschedule = 0;
653 // Do nothing if the driver is starting halt state.
654 // This might happen when timer already been fired before cancel timer with mlmehalt
655 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
658 pObj = (POS_COOKIE) pAd->OS_Cookie;
661 IntSource.field.Ac1DmaDone = 1;
662 pAd->int_pending &= ~INT_AC1_DLY;
664 bReschedule = RTMPHandleTxRingDmaDoneInterrupt(pAd, IntSource);
666 RTMP_INT_LOCK(&pAd->irq_lock, flags);
668 * double check to avoid lose of interrupts
670 if ((pAd->int_pending & INT_AC1_DLY) || bReschedule)
672 tasklet_hi_schedule(&pObj->ac1_dma_done_task);
673 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
677 /* enable TxDataInt again */
678 rt2860_int_enable(pAd, INT_AC1_DLY);
679 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
682 static void ac0_dma_done_tasklet(unsigned long data)
685 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
686 INT_SOURCE_CSR_STRUC IntSource;
688 BOOLEAN bReschedule = 0;
690 // Do nothing if the driver is starting halt state.
691 // This might happen when timer already been fired before cancel timer with mlmehalt
692 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
695 pObj = (POS_COOKIE) pAd->OS_Cookie;
698 IntSource.field.Ac0DmaDone = 1;
699 pAd->int_pending &= ~INT_AC0_DLY;
701 bReschedule = RTMPHandleTxRingDmaDoneInterrupt(pAd, IntSource);
703 RTMP_INT_LOCK(&pAd->irq_lock, flags);
705 * double check to avoid lose of interrupts
707 if ((pAd->int_pending & INT_AC0_DLY) || bReschedule)
709 tasklet_hi_schedule(&pObj->ac0_dma_done_task);
710 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
714 /* enable TxDataInt again */
715 rt2860_int_enable(pAd, INT_AC0_DLY);
716 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
723 rt2860_interrupt(int irq, void *dev_instance)
725 struct net_device *net_dev = (struct net_device *) dev_instance;
726 PRTMP_ADAPTER pAd = net_dev->ml_priv;
727 INT_SOURCE_CSR_STRUC IntSource;
731 pObj = (POS_COOKIE) pAd->OS_Cookie;
734 /* Note 03312008: we can not return here before
735 RTMP_IO_READ32(pAd, INT_SOURCE_CSR, &IntSource.word);
736 RTMP_IO_WRITE32(pAd, INT_SOURCE_CSR, IntSource.word);
737 Or kernel will panic after ifconfig ra0 down sometimes */
741 // Inital the Interrupt source.
743 IntSource.word = 0x00000000L;
744 // McuIntSource.word = 0x00000000L;
747 // Get the interrupt sources & saved to local variable
749 //RTMP_IO_READ32(pAd, where, &McuIntSource.word);
750 //RTMP_IO_WRITE32(pAd, , McuIntSource.word);
753 // Flag fOP_STATUS_DOZE On, means ASIC put to sleep, elase means ASICK WakeUp
754 // And at the same time, clock maybe turned off that say there is no DMA service.
755 // when ASIC get to sleep.
756 // To prevent system hang on power saving.
757 // We need to check it before handle the INT_SOURCE_CSR, ASIC must be wake up.
759 // RT2661 => when ASIC is sleeping, MAC register cannot be read and written.
760 // RT2860 => when ASIC is sleeping, MAC register can be read and written.
762 bOldValue = pAd->bPCIclkOff;
763 pAd->bPCIclkOff = FALSE;
765 RTMP_IO_READ32(pAd, INT_SOURCE_CSR, &IntSource.word);
766 RTMP_IO_WRITE32(pAd, INT_SOURCE_CSR, IntSource.word); // write 1 to clear
768 pAd->bPCIclkOff = bOldValue;
770 // Do nothing if Reset in progress
771 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS) ||
772 RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS))
778 // Handle interrupt, walk through all bits
779 // Should start from highest priority interrupt
780 // The priority can be adjust by altering processing if statement
783 // If required spinlock, each interrupt service routine has to acquire
784 // and release itself.
787 // Do nothing if NIC doesn't exist
788 if (IntSource.word == 0xffffffff)
790 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST | fRTMP_ADAPTER_HALT_IN_PROGRESS);
791 printk("snowpin - IntSource.word == 0xffffffff\n");
795 if (IntSource.word & TxCoherent)
797 DBGPRINT(RT_DEBUG_ERROR, (">>>TxCoherent<<<\n"));
798 RTMPHandleRxCoherentInterrupt(pAd);
801 if (IntSource.word & RxCoherent)
803 DBGPRINT(RT_DEBUG_ERROR, (">>>RxCoherent<<<\n"));
804 RTMPHandleRxCoherentInterrupt(pAd);
807 if (IntSource.word & FifoStaFullInt)
810 if ((pAd->int_disable_mask & FifoStaFullInt) == 0)
812 /* mask FifoStaFullInt */
813 rt2860_int_disable(pAd, FifoStaFullInt);
814 tasklet_hi_schedule(&pObj->fifo_statistic_full_task);
816 pAd->int_pending |= FifoStaFullInt;
818 NICUpdateFifoStaCounters(pAd);
822 if (IntSource.word & INT_MGMT_DLY)
824 if ((pAd->int_disable_mask & INT_MGMT_DLY) ==0 )
826 rt2860_int_disable(pAd, INT_MGMT_DLY);
827 tasklet_hi_schedule(&pObj->mgmt_dma_done_task);
829 pAd->int_pending |= INT_MGMT_DLY ;
832 if (IntSource.word & INT_RX)
834 if ((pAd->int_disable_mask & INT_RX) == 0)
837 rt2860_int_disable(pAd, INT_RX);
838 tasklet_hi_schedule(&pObj->rx_done_task);
840 pAd->int_pending |= INT_RX;
843 if (IntSource.word & INT_HCCA_DLY)
846 if ((pAd->int_disable_mask & INT_HCCA_DLY) == 0)
849 rt2860_int_disable(pAd, INT_HCCA_DLY);
850 tasklet_hi_schedule(&pObj->hcca_dma_done_task);
852 pAd->int_pending |= INT_HCCA_DLY;
855 if (IntSource.word & INT_AC3_DLY)
858 if ((pAd->int_disable_mask & INT_AC3_DLY) == 0)
861 rt2860_int_disable(pAd, INT_AC3_DLY);
862 tasklet_hi_schedule(&pObj->ac3_dma_done_task);
864 pAd->int_pending |= INT_AC3_DLY;
867 if (IntSource.word & INT_AC2_DLY)
870 if ((pAd->int_disable_mask & INT_AC2_DLY) == 0)
873 rt2860_int_disable(pAd, INT_AC2_DLY);
874 tasklet_hi_schedule(&pObj->ac2_dma_done_task);
876 pAd->int_pending |= INT_AC2_DLY;
879 if (IntSource.word & INT_AC1_DLY)
882 pAd->int_pending |= INT_AC1_DLY;
884 if ((pAd->int_disable_mask & INT_AC1_DLY) == 0)
887 rt2860_int_disable(pAd, INT_AC1_DLY);
888 tasklet_hi_schedule(&pObj->ac1_dma_done_task);
893 if (IntSource.word & INT_AC0_DLY)
895 pAd->int_pending |= INT_AC0_DLY;
897 if ((pAd->int_disable_mask & INT_AC0_DLY) == 0)
900 rt2860_int_disable(pAd, INT_AC0_DLY);
901 tasklet_hi_schedule(&pObj->ac0_dma_done_task);
906 if (IntSource.word & PreTBTTInt)
908 RTMPHandlePreTBTTInterrupt(pAd);
911 if (IntSource.word & TBTTInt)
913 RTMPHandleTBTTInterrupt(pAd);
916 if (IntSource.word & AutoWakeupInt)
917 RTMPHandleTwakeupInterrupt(pAd);
923 ========================================================================
925 Check the chipset vendor/product ID.
928 _dev_p Point to the PCI or USB device
935 ========================================================================
937 BOOLEAN RT28XXChipsetCheck(
946 ========================================================================
948 Init net device structure.
951 _dev_p Point to the PCI or USB device
952 *net_dev Point to the net device
953 *pAd the raxx interface data pointer
960 ========================================================================
962 BOOLEAN RT28XXNetDevInit(
964 IN struct net_device *net_dev,
965 IN RTMP_ADAPTER *pAd)
967 struct pci_dev *pci_dev = (struct pci_dev *)_dev_p;
968 const CHAR *print_name;
972 print_name = pci_dev ? pci_name(pci_dev) : "rt2860";
974 net_dev->base_addr = 0;
977 if (pci_request_regions(pci_dev, print_name))
978 goto err_out_free_netdev;
980 // interrupt IRQ number
981 net_dev->irq = pci_dev->irq;
983 // map physical address to virtual address for accessing register
984 csr_addr = (unsigned long) ioremap(pci_resource_start(pci_dev, 0),
985 pci_resource_len(pci_dev, 0));
989 DBGPRINT(RT_DEBUG_ERROR,
990 ("ioremap failed for device %s, region 0x%lX @ 0x%lX\n",
991 print_name, (ULONG)pci_resource_len(pci_dev, 0),
992 (ULONG)pci_resource_start(pci_dev, 0)));
993 goto err_out_free_res;
996 // Save CSR virtual address and irq to device structure
997 net_dev->base_addr = csr_addr;
998 pAd->CSRBaseAddress = (PUCHAR)net_dev->base_addr;
1001 pci_set_master(pci_dev);
1003 net_dev->priv_flags = INT_MAIN;
1005 DBGPRINT(RT_DEBUG_TRACE, ("%s: at 0x%lx, VA 0x%lx, IRQ %d. \n",
1006 net_dev->name, (ULONG)pci_resource_start(pci_dev, 0),
1007 (ULONG)csr_addr, pci_dev->irq));
1011 /* --------------------------- ERROR HANDLE --------------------------- */
1013 pci_release_regions(pci_dev);
1014 err_out_free_netdev:
1015 /* free netdev in caller, not here */
1021 ========================================================================
1022 Routine Description:
1023 Init net device structure.
1026 _dev_p Point to the PCI or USB device
1027 *pAd the raxx interface data pointer
1034 ========================================================================
1036 BOOLEAN RT28XXProbePostConfig(
1038 IN RTMP_ADAPTER *pAd,
1047 ========================================================================
1048 Routine Description:
1052 *pAd the raxx interface data pointer
1058 ========================================================================
1060 VOID RT28XXDMADisable(
1061 IN RTMP_ADAPTER *pAd)
1063 WPDMA_GLO_CFG_STRUC GloCfg;
1066 RTMP_IO_READ32(pAd, WPDMA_GLO_CFG, &GloCfg.word);
1067 GloCfg.word &= 0xff0;
1068 GloCfg.field.EnTXWriteBackDDONE =1;
1069 RTMP_IO_WRITE32(pAd, WPDMA_GLO_CFG, GloCfg.word);
1074 ========================================================================
1075 Routine Description:
1079 *pAd the raxx interface data pointer
1085 ========================================================================
1087 VOID RT28XXDMAEnable(
1088 IN RTMP_ADAPTER *pAd)
1090 WPDMA_GLO_CFG_STRUC GloCfg;
1093 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, 0x4);
1096 RTMP_IO_READ32(pAd, WPDMA_GLO_CFG, &GloCfg.word);
1097 if ((GloCfg.field.TxDMABusy == 0) && (GloCfg.field.RxDMABusy == 0))
1100 DBGPRINT(RT_DEBUG_TRACE, ("==> DMABusy\n"));
1101 RTMPusecDelay(1000);
1107 GloCfg.field.EnTXWriteBackDDONE = 1;
1108 GloCfg.field.WPDMABurstSIZE = 2;
1109 GloCfg.field.EnableRxDMA = 1;
1110 GloCfg.field.EnableTxDMA = 1;
1112 DBGPRINT(RT_DEBUG_TRACE, ("<== WRITE DMA offset 0x208 = 0x%x\n", GloCfg.word));
1113 RTMP_IO_WRITE32(pAd, WPDMA_GLO_CFG, GloCfg.word);
1118 ========================================================================
1119 Routine Description:
1120 Write Beacon buffer to Asic.
1123 *pAd the raxx interface data pointer
1129 ========================================================================
1131 VOID RT28xx_UpdateBeaconToAsic(
1132 IN RTMP_ADAPTER *pAd,
1137 ULONG CapInfoPos = 0;
1138 UCHAR *ptr, *ptr_update, *ptr_capinfo;
1140 BOOLEAN bBcnReq = FALSE;
1144 DBGPRINT(RT_DEBUG_ERROR, ("%s() : No valid Interface be found.\n", __func__));
1148 if (bBcnReq == FALSE)
1150 /* when the ra interface is down, do not send its beacon frame */
1151 /* clear all zero */
1152 for(i=0; i<TXWI_SIZE; i+=4)
1153 RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[bcn_idx] + i, 0x00);
1157 ptr = (PUCHAR)&pAd->BeaconTxWI;
1159 for (i=0; i<TXWI_SIZE; i+=4) // 16-byte TXWI field
1161 UINT32 longptr = *ptr + (*(ptr+1)<<8) + (*(ptr+2)<<16) + (*(ptr+3)<<24);
1162 RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[bcn_idx] + i, longptr);
1166 // Update CapabilityInfo in Beacon
1167 for (i = CapInfoPos; i < (CapInfoPos+2); i++)
1169 RTMP_IO_WRITE8(pAd, pAd->BeaconOffset[bcn_idx] + TXWI_SIZE + i, *ptr_capinfo);
1173 if (FrameLen > UpdatePos)
1175 for (i= UpdatePos; i< (FrameLen); i++)
1177 RTMP_IO_WRITE8(pAd, pAd->BeaconOffset[bcn_idx] + TXWI_SIZE + i, *ptr_update);
1186 VOID RTMPInitPCIeLinkCtrlValue(
1187 IN PRTMP_ADAPTER pAd)
1191 VOID RTMPFindHostPCIDev(
1192 IN PRTMP_ADAPTER pAd)
1197 ========================================================================
1199 Routine Description:
1202 Level = RESTORE_HALT : Restore PCI host and Ralink PCIe Link Control field to its default value.
1203 Level = Other Value : Restore from dot11 power save or radio off status. And force PCI host Link Control fields to 0x1
1205 ========================================================================
1207 VOID RTMPPCIeLinkCtrlValueRestore(
1208 IN PRTMP_ADAPTER pAd,
1214 ========================================================================
1216 Routine Description:
1219 Max : limit Host PCI and Ralink PCIe device's LINK CONTROL field's value.
1220 Because now frequently set our device to mode 1 or mode 3 will cause problem.
1222 ========================================================================
1224 VOID RTMPPCIeLinkCtrlSetting(
1225 IN PRTMP_ADAPTER pAd,
1230 VOID rt2860_stop(struct net_device *net_dev)
1232 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)NULL;
1233 if (net_dev == NULL)
1235 DBGPRINT(RT_DEBUG_ERROR, ("net_dev == NULL!\n"));
1238 pAd = net_dev->ml_priv;
1243 netif_carrier_off(net_dev);
1244 netif_stop_queue(net_dev);
1246 // mark device as removed from system and therefore no longer available
1247 netif_device_detach(net_dev);
1250 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
1251 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_RADIO_OFF);
1253 // take down the device
1254 rt28xx_close((PNET_DEV)net_dev);
1255 RT_MOD_DEC_USE_COUNT();
1261 * invaild or writeback cache
1262 * and convert virtual address to physical address
1264 dma_addr_t linux_pci_map_single(void *handle, void *ptr, size_t size, int sd_idx, int direction)
1270 ------ Porting Information ------
1272 mgmt packets => sd_idx = 0
1273 SwIdx: pAd->MgmtRing.TxCpuIdx
1274 pTxD : pAd->MgmtRing.Cell[SwIdx].AllocVa;
1276 data packets => sd_idx = 1
1277 TxIdx : pAd->TxRing[pTxBlk->QueIdx].TxCpuIdx
1278 QueIdx: pTxBlk->QueIdx
1279 pTxD : pAd->TxRing[pTxBlk->QueIdx].Cell[TxIdx].AllocVa;
1285 pAd = (PRTMP_ADAPTER)handle;
1286 pObj = (POS_COOKIE)pAd->OS_Cookie;
1291 pTxBlk = (PTX_BLK)ptr;
1292 return pci_map_single(pObj->pci_dev, pTxBlk->pSrcBufData, pTxBlk->SrcBufLen, direction);
1296 return pci_map_single(pObj->pci_dev, ptr, size, direction);
1301 void linux_pci_unmap_single(void *handle, dma_addr_t dma_addr, size_t size, int direction)
1306 pAd=(PRTMP_ADAPTER)handle;
1307 pObj = (POS_COOKIE)pAd->OS_Cookie;
1309 pci_unmap_single(pObj->pci_dev, dma_addr, size, direction);