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"
41 #ifdef MULTIPLE_CARD_SUPPORT
42 // record whether the card in the card list is used in the card file
43 extern UINT8 MC_CardUsed[];
44 #endif // MULTIPLE_CARD_SUPPORT //
47 extern INT __devinit rt28xx_probe(IN void *_dev_p, IN void *_dev_id_p,
48 IN UINT argc, OUT PRTMP_ADAPTER *ppAd);
50 static void rx_done_tasklet(unsigned long data);
51 static void mgmt_dma_done_tasklet(unsigned long data);
52 static void ac0_dma_done_tasklet(unsigned long data);
53 static void ac1_dma_done_tasklet(unsigned long data);
54 static void ac2_dma_done_tasklet(unsigned long data);
55 static void ac3_dma_done_tasklet(unsigned long data);
56 static void hcca_dma_done_tasklet(unsigned long data);
57 static void fifo_statistic_full_tasklet(unsigned long data);
60 /*---------------------------------------------------------------------*/
61 /* Symbol & Macro Definitions */
62 /*---------------------------------------------------------------------*/
63 #define RT2860_INT_RX_DLY (1<<0) // bit 0
64 #define RT2860_INT_TX_DLY (1<<1) // bit 1
65 #define RT2860_INT_RX_DONE (1<<2) // bit 2
66 #define RT2860_INT_AC0_DMA_DONE (1<<3) // bit 3
67 #define RT2860_INT_AC1_DMA_DONE (1<<4) // bit 4
68 #define RT2860_INT_AC2_DMA_DONE (1<<5) // bit 5
69 #define RT2860_INT_AC3_DMA_DONE (1<<6) // bit 6
70 #define RT2860_INT_HCCA_DMA_DONE (1<<7) // bit 7
71 #define RT2860_INT_MGMT_DONE (1<<8) // bit 8
73 #define INT_RX RT2860_INT_RX_DONE
75 #define INT_AC0_DLY (RT2860_INT_AC0_DMA_DONE) //| RT2860_INT_TX_DLY)
76 #define INT_AC1_DLY (RT2860_INT_AC1_DMA_DONE) //| RT2860_INT_TX_DLY)
77 #define INT_AC2_DLY (RT2860_INT_AC2_DMA_DONE) //| RT2860_INT_TX_DLY)
78 #define INT_AC3_DLY (RT2860_INT_AC3_DMA_DONE) //| RT2860_INT_TX_DLY)
79 #define INT_HCCA_DLY (RT2860_INT_HCCA_DMA_DONE) //| RT2860_INT_TX_DLY)
80 #define INT_MGMT_DLY RT2860_INT_MGMT_DONE
82 /*---------------------------------------------------------------------*/
83 /* Prototypes of Functions Used */
84 /*---------------------------------------------------------------------*/
85 /* function declarations */
86 static INT __devinit rt2860_init_one (struct pci_dev *pci_dev, const struct pci_device_id *ent);
87 static VOID __devexit rt2860_remove_one(struct pci_dev *pci_dev);
88 static INT __devinit rt2860_probe(struct pci_dev *pci_dev, const struct pci_device_id *ent);
89 void init_thread_task(PRTMP_ADAPTER pAd);
90 static void __exit rt2860_cleanup_module(void);
91 static int __init rt2860_init_module(void);
94 static int rt2860_suspend(struct pci_dev *pci_dev, pm_message_t state);
95 static int rt2860_resume(struct pci_dev *pci_dev);
96 #endif // CONFIG_PM //
100 // Ralink PCI device table, include all supported chipsets
102 static struct pci_device_id rt2860_pci_tbl[] __devinitdata =
104 {PCI_DEVICE(NIC_PCI_VENDOR_ID, NIC2860_PCI_DEVICE_ID)}, //RT28602.4G
105 {PCI_DEVICE(NIC_PCI_VENDOR_ID, NIC2860_PCIe_DEVICE_ID)},
106 {PCI_DEVICE(NIC_PCI_VENDOR_ID, NIC2760_PCI_DEVICE_ID)},
107 {PCI_DEVICE(NIC_PCI_VENDOR_ID, NIC2790_PCIe_DEVICE_ID)},
108 {PCI_DEVICE(VEN_AWT_PCI_VENDOR_ID, VEN_AWT_PCIe_DEVICE_ID)},
109 {0,} // terminate list
112 MODULE_DEVICE_TABLE(pci, rt2860_pci_tbl);
113 #ifdef CONFIG_STA_SUPPORT
114 MODULE_LICENSE("GPL");
115 #ifdef MODULE_VERSION
116 MODULE_VERSION(STA_DRIVER_VERSION);
118 #endif // CONFIG_STA_SUPPORT //
122 // Our PCI driver structure
124 static struct pci_driver rt2860_driver =
127 id_table: rt2860_pci_tbl,
128 probe: rt2860_init_one,
129 remove: __devexit_p(rt2860_remove_one),
132 suspend: rt2860_suspend,
133 resume: rt2860_resume,
140 VOID RT2860RejectPendingPackets(
141 IN PRTMP_ADAPTER pAd)
144 // clear TxSw packets
147 static int rt2860_suspend(
148 struct pci_dev *pci_dev,
151 struct net_device *net_dev = pci_get_drvdata(pci_dev);
152 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)NULL;
156 DBGPRINT(RT_DEBUG_TRACE, ("===> rt2860_suspend()\n"));
160 DBGPRINT(RT_DEBUG_ERROR, ("net_dev == NULL!\n"));
164 pAd = net_dev->ml_priv;
166 /* we can not use IFF_UP because ra0 down but ra1 up */
167 /* and 1 suspend/resume function for 1 module, not for each interface */
168 /* so Linux will call suspend/resume function once */
169 if (VIRTUAL_IF_NUM(pAd) > 0)
171 // avoid users do suspend after interface is down
174 netif_carrier_off(net_dev);
175 netif_stop_queue(net_dev);
177 // mark device as removed from system and therefore no longer available
178 netif_device_detach(net_dev);
181 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
182 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_RADIO_OFF);
184 // take down the device
185 rt28xx_close((PNET_DEV)net_dev);
187 RT_MOD_DEC_USE_COUNT();
191 // reference to http://vovo2000.com/type-lab/linux/kernel-api/linux-kernel-api.html
192 // enable device to generate PME# when suspended
193 // pci_choose_state(): Choose the power state of a PCI device to be suspended
194 retval = pci_enable_wake(pci_dev, pci_choose_state(pci_dev, state), 1);
195 // save the PCI configuration space of a device before suspending
196 pci_save_state(pci_dev);
197 // disable PCI device after use
198 pci_disable_device(pci_dev);
200 retval = pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
202 DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2860_suspend()\n"));
206 static int rt2860_resume(
207 struct pci_dev *pci_dev)
209 struct net_device *net_dev = pci_get_drvdata(pci_dev);
210 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)NULL;
214 // set the power state of a PCI device
215 // PCI has 4 power states, DO (normal) ~ D3(less power)
216 // in include/linux/pci.h, you can find that
217 // #define PCI_D0 ((pci_power_t __force) 0)
218 // #define PCI_D1 ((pci_power_t __force) 1)
219 // #define PCI_D2 ((pci_power_t __force) 2)
220 // #define PCI_D3hot ((pci_power_t __force) 3)
221 // #define PCI_D3cold ((pci_power_t __force) 4)
222 // #define PCI_UNKNOWN ((pci_power_t __force) 5)
223 // #define PCI_POWER_ERROR ((pci_power_t __force) -1)
224 retval = pci_set_power_state(pci_dev, PCI_D0);
226 // restore the saved state of a PCI device
227 pci_restore_state(pci_dev);
229 // initialize device before it's used by a driver
230 if (pci_enable_device(pci_dev))
232 printk("pci enable fail!\n");
236 DBGPRINT(RT_DEBUG_TRACE, ("===> rt2860_resume()\n"));
240 DBGPRINT(RT_DEBUG_ERROR, ("net_dev == NULL!\n"));
243 pAd = net_dev->ml_priv;
247 /* we can not use IFF_UP because ra0 down but ra1 up */
248 /* and 1 suspend/resume function for 1 module, not for each interface */
249 /* so Linux will call suspend/resume function once */
250 if (VIRTUAL_IF_NUM(pAd) > 0)
252 // mark device as attached from system and restart if needed
253 netif_device_attach(net_dev);
255 if (rt28xx_open((PNET_DEV)net_dev) != 0)
258 DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2860_resume()\n"));
262 // increase MODULE use count
263 RT_MOD_INC_USE_COUNT();
265 RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
266 RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_RADIO_OFF);
268 netif_start_queue(net_dev);
269 netif_carrier_on(net_dev);
270 netif_wake_queue(net_dev);
274 DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2860_resume()\n"));
277 #endif // CONFIG_PM //
280 static INT __init rt2860_init_module(VOID)
282 return pci_register_driver(&rt2860_driver);
287 // Driver module unload function
289 static VOID __exit rt2860_cleanup_module(VOID)
291 pci_unregister_driver(&rt2860_driver);
294 module_init(rt2860_init_module);
295 module_exit(rt2860_cleanup_module);
298 static INT __devinit rt2860_init_one (
299 IN struct pci_dev *pci_dev,
300 IN const struct pci_device_id *ent)
304 DBGPRINT(RT_DEBUG_TRACE, ("===> rt2860_init_one\n"));
306 // wake up and enable device
307 if (pci_enable_device (pci_dev))
313 rc = rt2860_probe(pci_dev, ent);
316 DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2860_init_one\n"));
321 static VOID __devexit rt2860_remove_one(
322 IN struct pci_dev *pci_dev)
324 struct net_device *net_dev = pci_get_drvdata(pci_dev);
325 RTMP_ADAPTER *pAd = net_dev->ml_priv;
327 DBGPRINT(RT_DEBUG_TRACE, ("===> rt2860_remove_one\n"));
331 #ifdef MULTIPLE_CARD_SUPPORT
332 if ((pAd->MC_RowID >= 0) && (pAd->MC_RowID <= MAX_NUM_OF_MULTIPLE_CARD))
333 MC_CardUsed[pAd->MC_RowID] = 0; // not clear MAC address
334 #endif // MULTIPLE_CARD_SUPPORT //
339 // Unregister network device
340 unregister_netdev(net_dev);
342 // Unmap CSR base address
343 iounmap((char *)(net_dev->base_addr));
345 RTMPFreeAdapter(pAd);
347 // release memory region
348 release_mem_region(pci_resource_start(pci_dev, 0), pci_resource_len(pci_dev, 0));
352 // Unregister network device
353 unregister_netdev(net_dev);
355 // Unmap CSR base address
356 iounmap((char *)(net_dev->base_addr));
358 // release memory region
359 release_mem_region(pci_resource_start(pci_dev, 0), pci_resource_len(pci_dev, 0));
362 // Free pre-allocated net_device memory
363 free_netdev(net_dev);
367 // PCI device probe & initialization function
369 static INT __devinit rt2860_probe(
370 IN struct pci_dev *pci_dev,
371 IN const struct pci_device_id *ent)
376 rv = (INT)rt28xx_probe((void *)pci_dev, (void *)ent, 0, &pAd);
377 OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_ADVANCE_POWER_SAVE_PCIE_DEVICE);
382 void init_thread_task(IN PRTMP_ADAPTER pAd)
386 pObj = (POS_COOKIE) pAd->OS_Cookie;
388 tasklet_init(&pObj->rx_done_task, rx_done_tasklet, (unsigned long)pAd);
389 tasklet_init(&pObj->mgmt_dma_done_task, mgmt_dma_done_tasklet, (unsigned long)pAd);
390 tasklet_init(&pObj->ac0_dma_done_task, ac0_dma_done_tasklet, (unsigned long)pAd);
391 tasklet_init(&pObj->ac1_dma_done_task, ac1_dma_done_tasklet, (unsigned long)pAd);
392 tasklet_init(&pObj->ac2_dma_done_task, ac2_dma_done_tasklet, (unsigned long)pAd);
393 tasklet_init(&pObj->ac3_dma_done_task, ac3_dma_done_tasklet, (unsigned long)pAd);
394 tasklet_init(&pObj->hcca_dma_done_task, hcca_dma_done_tasklet, (unsigned long)pAd);
395 tasklet_init(&pObj->tbtt_task, tbtt_tasklet, (unsigned long)pAd);
396 tasklet_init(&pObj->fifo_statistic_full_task, fifo_statistic_full_tasklet, (unsigned long)pAd);
399 void kill_thread_task(IN PRTMP_ADAPTER pAd)
403 pObj = (POS_COOKIE) pAd->OS_Cookie;
405 tasklet_kill(&pObj->rx_done_task);
406 tasklet_kill(&pObj->mgmt_dma_done_task);
407 tasklet_kill(&pObj->ac0_dma_done_task);
408 tasklet_kill(&pObj->ac1_dma_done_task);
409 tasklet_kill(&pObj->ac2_dma_done_task);
410 tasklet_kill(&pObj->ac3_dma_done_task);
411 tasklet_kill(&pObj->hcca_dma_done_task);
412 tasklet_kill(&pObj->tbtt_task);
413 tasklet_kill(&pObj->fifo_statistic_full_task);
417 static void rt2860_int_enable(PRTMP_ADAPTER pAd, unsigned int mode)
421 pAd->int_disable_mask &= ~(mode);
422 regValue = pAd->int_enable_reg & ~(pAd->int_disable_mask);
423 RTMP_IO_WRITE32(pAd, INT_MASK_CSR, regValue); // 1:enable
426 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_ACTIVE);
430 static void rt2860_int_disable(PRTMP_ADAPTER pAd, unsigned int mode)
434 pAd->int_disable_mask |= mode;
435 regValue = pAd->int_enable_reg & ~(pAd->int_disable_mask);
436 RTMP_IO_WRITE32(pAd, INT_MASK_CSR, regValue); // 0: disable
440 RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_ACTIVE);
444 static void mgmt_dma_done_tasklet(unsigned long data)
447 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
448 INT_SOURCE_CSR_STRUC IntSource;
451 // Do nothing if the driver is starting halt state.
452 // This might happen when timer already been fired before cancel timer with mlmehalt
453 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
456 pObj = (POS_COOKIE) pAd->OS_Cookie;
459 IntSource.field.MgmtDmaDone = 1;
460 pAd->int_pending &= ~INT_MGMT_DLY;
462 RTMPHandleMgmtRingDmaDoneInterrupt(pAd);
464 // if you use RTMP_SEM_LOCK, sometimes kernel will hang up, no any
466 RTMP_INT_LOCK(&pAd->irq_lock, flags);
468 * double check to avoid lose of interrupts
470 if (pAd->int_pending & INT_MGMT_DLY)
472 tasklet_hi_schedule(&pObj->mgmt_dma_done_task);
473 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
477 /* enable TxDataInt again */
478 rt2860_int_enable(pAd, INT_MGMT_DLY);
479 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
482 static void rx_done_tasklet(unsigned long data)
485 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
486 BOOLEAN bReschedule = 0;
489 // Do nothing if the driver is starting halt state.
490 // This might happen when timer already been fired before cancel timer with mlmehalt
491 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
494 pObj = (POS_COOKIE) pAd->OS_Cookie;
496 pAd->int_pending &= ~(INT_RX);
497 #ifdef CONFIG_STA_SUPPORT
498 IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
499 bReschedule = STARxDoneInterruptHandle(pAd, 0);
500 #endif // CONFIG_STA_SUPPORT //
502 RTMP_INT_LOCK(&pAd->irq_lock, flags);
504 * double check to avoid rotting packet
506 if (pAd->int_pending & INT_RX || bReschedule)
508 tasklet_hi_schedule(&pObj->rx_done_task);
509 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
513 /* enable RxINT again */
514 rt2860_int_enable(pAd, INT_RX);
515 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
519 void fifo_statistic_full_tasklet(unsigned long data)
522 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
525 // Do nothing if the driver is starting halt state.
526 // This might happen when timer already been fired before cancel timer with mlmehalt
527 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
530 pObj = (POS_COOKIE) pAd->OS_Cookie;
532 pAd->int_pending &= ~(FifoStaFullInt);
533 NICUpdateFifoStaCounters(pAd);
535 RTMP_INT_LOCK(&pAd->irq_lock, flags);
537 * double check to avoid rotting packet
539 if (pAd->int_pending & FifoStaFullInt)
541 tasklet_hi_schedule(&pObj->fifo_statistic_full_task);
542 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
546 /* enable RxINT again */
548 rt2860_int_enable(pAd, FifoStaFullInt);
549 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
553 static void hcca_dma_done_tasklet(unsigned long data)
556 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
557 INT_SOURCE_CSR_STRUC IntSource;
560 // Do nothing if the driver is starting halt state.
561 // This might happen when timer already been fired before cancel timer with mlmehalt
562 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
565 pObj = (POS_COOKIE) pAd->OS_Cookie;
569 IntSource.field.HccaDmaDone = 1;
570 pAd->int_pending &= ~INT_HCCA_DLY;
572 RTMPHandleTxRingDmaDoneInterrupt(pAd, IntSource);
574 RTMP_INT_LOCK(&pAd->irq_lock, flags);
576 * double check to avoid lose of interrupts
578 if (pAd->int_pending & INT_HCCA_DLY)
580 tasklet_hi_schedule(&pObj->hcca_dma_done_task);
581 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
585 /* enable TxDataInt again */
586 rt2860_int_enable(pAd, INT_HCCA_DLY);
587 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
590 static void ac3_dma_done_tasklet(unsigned long data)
593 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
594 INT_SOURCE_CSR_STRUC IntSource;
596 BOOLEAN bReschedule = 0;
598 // Do nothing if the driver is starting halt state.
599 // This might happen when timer already been fired before cancel timer with mlmehalt
600 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
603 pObj = (POS_COOKIE) pAd->OS_Cookie;
606 IntSource.field.Ac3DmaDone = 1;
607 pAd->int_pending &= ~INT_AC3_DLY;
609 bReschedule = RTMPHandleTxRingDmaDoneInterrupt(pAd, IntSource);
611 RTMP_INT_LOCK(&pAd->irq_lock, flags);
613 * double check to avoid lose of interrupts
615 if ((pAd->int_pending & INT_AC3_DLY) || bReschedule)
617 tasklet_hi_schedule(&pObj->ac3_dma_done_task);
618 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
622 /* enable TxDataInt again */
623 rt2860_int_enable(pAd, INT_AC3_DLY);
624 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
627 static void ac2_dma_done_tasklet(unsigned long data)
630 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
631 INT_SOURCE_CSR_STRUC IntSource;
633 BOOLEAN bReschedule = 0;
635 // Do nothing if the driver is starting halt state.
636 // This might happen when timer already been fired before cancel timer with mlmehalt
637 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
640 pObj = (POS_COOKIE) pAd->OS_Cookie;
643 IntSource.field.Ac2DmaDone = 1;
644 pAd->int_pending &= ~INT_AC2_DLY;
646 bReschedule = RTMPHandleTxRingDmaDoneInterrupt(pAd, IntSource);
648 RTMP_INT_LOCK(&pAd->irq_lock, flags);
651 * double check to avoid lose of interrupts
653 if ((pAd->int_pending & INT_AC2_DLY) || bReschedule)
655 tasklet_hi_schedule(&pObj->ac2_dma_done_task);
656 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
660 /* enable TxDataInt again */
661 rt2860_int_enable(pAd, INT_AC2_DLY);
662 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
665 static void ac1_dma_done_tasklet(unsigned long data)
668 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
669 INT_SOURCE_CSR_STRUC IntSource;
671 BOOLEAN bReschedule = 0;
673 // Do nothing if the driver is starting halt state.
674 // This might happen when timer already been fired before cancel timer with mlmehalt
675 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
678 pObj = (POS_COOKIE) pAd->OS_Cookie;
681 IntSource.field.Ac1DmaDone = 1;
682 pAd->int_pending &= ~INT_AC1_DLY;
684 bReschedule = RTMPHandleTxRingDmaDoneInterrupt(pAd, IntSource);
686 RTMP_INT_LOCK(&pAd->irq_lock, flags);
688 * double check to avoid lose of interrupts
690 if ((pAd->int_pending & INT_AC1_DLY) || bReschedule)
692 tasklet_hi_schedule(&pObj->ac1_dma_done_task);
693 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
697 /* enable TxDataInt again */
698 rt2860_int_enable(pAd, INT_AC1_DLY);
699 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
702 static void ac0_dma_done_tasklet(unsigned long data)
705 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
706 INT_SOURCE_CSR_STRUC IntSource;
708 BOOLEAN bReschedule = 0;
710 // Do nothing if the driver is starting halt state.
711 // This might happen when timer already been fired before cancel timer with mlmehalt
712 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
715 pObj = (POS_COOKIE) pAd->OS_Cookie;
718 IntSource.field.Ac0DmaDone = 1;
719 pAd->int_pending &= ~INT_AC0_DLY;
721 bReschedule = RTMPHandleTxRingDmaDoneInterrupt(pAd, IntSource);
723 RTMP_INT_LOCK(&pAd->irq_lock, flags);
725 * double check to avoid lose of interrupts
727 if ((pAd->int_pending & INT_AC0_DLY) || bReschedule)
729 tasklet_hi_schedule(&pObj->ac0_dma_done_task);
730 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
734 /* enable TxDataInt again */
735 rt2860_int_enable(pAd, INT_AC0_DLY);
736 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
743 rt2860_interrupt(int irq, void *dev_instance)
745 struct net_device *net_dev = (struct net_device *) dev_instance;
746 PRTMP_ADAPTER pAd = net_dev->ml_priv;
747 INT_SOURCE_CSR_STRUC IntSource;
751 pObj = (POS_COOKIE) pAd->OS_Cookie;
754 /* Note 03312008: we can not return here before
755 RTMP_IO_READ32(pAd, INT_SOURCE_CSR, &IntSource.word);
756 RTMP_IO_WRITE32(pAd, INT_SOURCE_CSR, IntSource.word);
757 Or kernel will panic after ifconfig ra0 down sometimes */
761 // Inital the Interrupt source.
763 IntSource.word = 0x00000000L;
764 // McuIntSource.word = 0x00000000L;
767 // Get the interrupt sources & saved to local variable
769 //RTMP_IO_READ32(pAd, where, &McuIntSource.word);
770 //RTMP_IO_WRITE32(pAd, , McuIntSource.word);
773 // Flag fOP_STATUS_DOZE On, means ASIC put to sleep, elase means ASICK WakeUp
774 // And at the same time, clock maybe turned off that say there is no DMA service.
775 // when ASIC get to sleep.
776 // To prevent system hang on power saving.
777 // We need to check it before handle the INT_SOURCE_CSR, ASIC must be wake up.
779 // RT2661 => when ASIC is sleeping, MAC register cannot be read and written.
780 // RT2860 => when ASIC is sleeping, MAC register can be read and written.
782 bOldValue = pAd->bPCIclkOff;
783 pAd->bPCIclkOff = FALSE;
785 RTMP_IO_READ32(pAd, INT_SOURCE_CSR, &IntSource.word);
786 RTMP_IO_WRITE32(pAd, INT_SOURCE_CSR, IntSource.word); // write 1 to clear
788 pAd->bPCIclkOff = bOldValue;
790 // Do nothing if Reset in progress
791 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS) ||
792 RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS))
798 // Handle interrupt, walk through all bits
799 // Should start from highest priority interrupt
800 // The priority can be adjust by altering processing if statement
803 // If required spinlock, each interrupt service routine has to acquire
804 // and release itself.
807 // Do nothing if NIC doesn't exist
808 if (IntSource.word == 0xffffffff)
810 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST | fRTMP_ADAPTER_HALT_IN_PROGRESS);
811 printk("snowpin - IntSource.word == 0xffffffff\n");
815 if (IntSource.word & TxCoherent)
817 DBGPRINT(RT_DEBUG_ERROR, (">>>TxCoherent<<<\n"));
818 RTMPHandleRxCoherentInterrupt(pAd);
821 if (IntSource.word & RxCoherent)
823 DBGPRINT(RT_DEBUG_ERROR, (">>>RxCoherent<<<\n"));
824 RTMPHandleRxCoherentInterrupt(pAd);
827 if (IntSource.word & FifoStaFullInt)
830 if ((pAd->int_disable_mask & FifoStaFullInt) == 0)
832 /* mask FifoStaFullInt */
833 rt2860_int_disable(pAd, FifoStaFullInt);
834 tasklet_hi_schedule(&pObj->fifo_statistic_full_task);
836 pAd->int_pending |= FifoStaFullInt;
838 NICUpdateFifoStaCounters(pAd);
842 if (IntSource.word & INT_MGMT_DLY)
844 if ((pAd->int_disable_mask & INT_MGMT_DLY) ==0 )
846 rt2860_int_disable(pAd, INT_MGMT_DLY);
847 tasklet_hi_schedule(&pObj->mgmt_dma_done_task);
849 pAd->int_pending |= INT_MGMT_DLY ;
852 if (IntSource.word & INT_RX)
854 if ((pAd->int_disable_mask & INT_RX) == 0)
857 rt2860_int_disable(pAd, INT_RX);
858 tasklet_hi_schedule(&pObj->rx_done_task);
860 pAd->int_pending |= INT_RX;
863 if (IntSource.word & INT_HCCA_DLY)
866 if ((pAd->int_disable_mask & INT_HCCA_DLY) == 0)
869 rt2860_int_disable(pAd, INT_HCCA_DLY);
870 tasklet_hi_schedule(&pObj->hcca_dma_done_task);
872 pAd->int_pending |= INT_HCCA_DLY;
875 if (IntSource.word & INT_AC3_DLY)
878 if ((pAd->int_disable_mask & INT_AC3_DLY) == 0)
881 rt2860_int_disable(pAd, INT_AC3_DLY);
882 tasklet_hi_schedule(&pObj->ac3_dma_done_task);
884 pAd->int_pending |= INT_AC3_DLY;
887 if (IntSource.word & INT_AC2_DLY)
890 if ((pAd->int_disable_mask & INT_AC2_DLY) == 0)
893 rt2860_int_disable(pAd, INT_AC2_DLY);
894 tasklet_hi_schedule(&pObj->ac2_dma_done_task);
896 pAd->int_pending |= INT_AC2_DLY;
899 if (IntSource.word & INT_AC1_DLY)
902 pAd->int_pending |= INT_AC1_DLY;
904 if ((pAd->int_disable_mask & INT_AC1_DLY) == 0)
907 rt2860_int_disable(pAd, INT_AC1_DLY);
908 tasklet_hi_schedule(&pObj->ac1_dma_done_task);
913 if (IntSource.word & INT_AC0_DLY)
915 pAd->int_pending |= INT_AC0_DLY;
917 if ((pAd->int_disable_mask & INT_AC0_DLY) == 0)
920 rt2860_int_disable(pAd, INT_AC0_DLY);
921 tasklet_hi_schedule(&pObj->ac0_dma_done_task);
926 if (IntSource.word & PreTBTTInt)
928 RTMPHandlePreTBTTInterrupt(pAd);
931 if (IntSource.word & TBTTInt)
933 RTMPHandleTBTTInterrupt(pAd);
938 #ifdef CONFIG_STA_SUPPORT
939 IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
941 if (IntSource.word & AutoWakeupInt)
942 RTMPHandleTwakeupInterrupt(pAd);
944 #endif // CONFIG_STA_SUPPORT //
950 ========================================================================
952 Check the chipset vendor/product ID.
955 _dev_p Point to the PCI or USB device
962 ========================================================================
964 BOOLEAN RT28XXChipsetCheck(
973 ========================================================================
975 Init net device structure.
978 _dev_p Point to the PCI or USB device
979 *net_dev Point to the net device
980 *pAd the raxx interface data pointer
987 ========================================================================
989 BOOLEAN RT28XXNetDevInit(
991 IN struct net_device *net_dev,
992 IN RTMP_ADAPTER *pAd)
994 struct pci_dev *pci_dev = (struct pci_dev *)_dev_p;
995 const CHAR *print_name;
999 print_name = pci_dev ? pci_name(pci_dev) : "rt2860";
1001 net_dev->base_addr = 0;
1004 if (pci_request_regions(pci_dev, print_name))
1005 goto err_out_free_netdev;
1007 // interrupt IRQ number
1008 net_dev->irq = pci_dev->irq;
1010 // map physical address to virtual address for accessing register
1011 csr_addr = (unsigned long) ioremap(pci_resource_start(pci_dev, 0),
1012 pci_resource_len(pci_dev, 0));
1016 DBGPRINT(RT_DEBUG_ERROR,
1017 ("ioremap failed for device %s, region 0x%lX @ 0x%lX\n",
1018 print_name, (ULONG)pci_resource_len(pci_dev, 0),
1019 (ULONG)pci_resource_start(pci_dev, 0)));
1020 goto err_out_free_res;
1023 // Save CSR virtual address and irq to device structure
1024 net_dev->base_addr = csr_addr;
1025 pAd->CSRBaseAddress = (PUCHAR)net_dev->base_addr;
1028 pci_set_master(pci_dev);
1030 net_dev->priv_flags = INT_MAIN;
1032 DBGPRINT(RT_DEBUG_TRACE, ("%s: at 0x%lx, VA 0x%lx, IRQ %d. \n",
1033 net_dev->name, (ULONG)pci_resource_start(pci_dev, 0),
1034 (ULONG)csr_addr, pci_dev->irq));
1038 /* --------------------------- ERROR HANDLE --------------------------- */
1040 pci_release_regions(pci_dev);
1041 err_out_free_netdev:
1042 /* free netdev in caller, not here */
1048 ========================================================================
1049 Routine Description:
1050 Init net device structure.
1053 _dev_p Point to the PCI or USB device
1054 *pAd the raxx interface data pointer
1061 ========================================================================
1063 BOOLEAN RT28XXProbePostConfig(
1065 IN RTMP_ADAPTER *pAd,
1074 ========================================================================
1075 Routine Description:
1079 *pAd the raxx interface data pointer
1085 ========================================================================
1087 VOID RT28XXDMADisable(
1088 IN RTMP_ADAPTER *pAd)
1090 WPDMA_GLO_CFG_STRUC GloCfg;
1093 RTMP_IO_READ32(pAd, WPDMA_GLO_CFG, &GloCfg.word);
1094 GloCfg.word &= 0xff0;
1095 GloCfg.field.EnTXWriteBackDDONE =1;
1096 RTMP_IO_WRITE32(pAd, WPDMA_GLO_CFG, GloCfg.word);
1101 ========================================================================
1102 Routine Description:
1106 *pAd the raxx interface data pointer
1112 ========================================================================
1114 VOID RT28XXDMAEnable(
1115 IN RTMP_ADAPTER *pAd)
1117 WPDMA_GLO_CFG_STRUC GloCfg;
1120 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, 0x4);
1123 RTMP_IO_READ32(pAd, WPDMA_GLO_CFG, &GloCfg.word);
1124 if ((GloCfg.field.TxDMABusy == 0) && (GloCfg.field.RxDMABusy == 0))
1127 DBGPRINT(RT_DEBUG_TRACE, ("==> DMABusy\n"));
1128 RTMPusecDelay(1000);
1134 GloCfg.field.EnTXWriteBackDDONE = 1;
1135 GloCfg.field.WPDMABurstSIZE = 2;
1136 GloCfg.field.EnableRxDMA = 1;
1137 GloCfg.field.EnableTxDMA = 1;
1139 DBGPRINT(RT_DEBUG_TRACE, ("<== WRITE DMA offset 0x208 = 0x%x\n", GloCfg.word));
1140 RTMP_IO_WRITE32(pAd, WPDMA_GLO_CFG, GloCfg.word);
1145 ========================================================================
1146 Routine Description:
1147 Write Beacon buffer to Asic.
1150 *pAd the raxx interface data pointer
1156 ========================================================================
1158 VOID RT28xx_UpdateBeaconToAsic(
1159 IN RTMP_ADAPTER *pAd,
1164 ULONG CapInfoPos = 0;
1165 UCHAR *ptr, *ptr_update, *ptr_capinfo;
1167 BOOLEAN bBcnReq = FALSE;
1171 DBGPRINT(RT_DEBUG_ERROR, ("%s() : No valid Interface be found.\n", __func__));
1175 if (bBcnReq == FALSE)
1177 /* when the ra interface is down, do not send its beacon frame */
1178 /* clear all zero */
1179 for(i=0; i<TXWI_SIZE; i+=4)
1180 RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[bcn_idx] + i, 0x00);
1184 ptr = (PUCHAR)&pAd->BeaconTxWI;
1185 #ifdef RT_BIG_ENDIAN
1186 RTMPWIEndianChange(ptr, TYPE_TXWI);
1188 for (i=0; i<TXWI_SIZE; i+=4) // 16-byte TXWI field
1190 UINT32 longptr = *ptr + (*(ptr+1)<<8) + (*(ptr+2)<<16) + (*(ptr+3)<<24);
1191 RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[bcn_idx] + i, longptr);
1195 // Update CapabilityInfo in Beacon
1196 for (i = CapInfoPos; i < (CapInfoPos+2); i++)
1198 RTMP_IO_WRITE8(pAd, pAd->BeaconOffset[bcn_idx] + TXWI_SIZE + i, *ptr_capinfo);
1202 if (FrameLen > UpdatePos)
1204 for (i= UpdatePos; i< (FrameLen); i++)
1206 RTMP_IO_WRITE8(pAd, pAd->BeaconOffset[bcn_idx] + TXWI_SIZE + i, *ptr_update);
1215 #ifdef CONFIG_STA_SUPPORT
1216 VOID RTMPInitPCIeLinkCtrlValue(
1217 IN PRTMP_ADAPTER pAd)
1221 VOID RTMPFindHostPCIDev(
1222 IN PRTMP_ADAPTER pAd)
1227 ========================================================================
1229 Routine Description:
1232 Level = RESTORE_HALT : Restore PCI host and Ralink PCIe Link Control field to its default value.
1233 Level = Other Value : Restore from dot11 power save or radio off status. And force PCI host Link Control fields to 0x1
1235 ========================================================================
1237 VOID RTMPPCIeLinkCtrlValueRestore(
1238 IN PRTMP_ADAPTER pAd,
1244 ========================================================================
1246 Routine Description:
1249 Max : limit Host PCI and Ralink PCIe device's LINK CONTROL field's value.
1250 Because now frequently set our device to mode 1 or mode 3 will cause problem.
1252 ========================================================================
1254 VOID RTMPPCIeLinkCtrlSetting(
1255 IN PRTMP_ADAPTER pAd,
1259 #endif // CONFIG_STA_SUPPORT //
1261 VOID rt2860_stop(struct net_device *net_dev)
1263 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)NULL;
1264 if (net_dev == NULL)
1266 DBGPRINT(RT_DEBUG_ERROR, ("net_dev == NULL!\n"));
1269 pAd = net_dev->ml_priv;
1274 netif_carrier_off(net_dev);
1275 netif_stop_queue(net_dev);
1277 // mark device as removed from system and therefore no longer available
1278 netif_device_detach(net_dev);
1281 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
1282 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_RADIO_OFF);
1284 // take down the device
1285 rt28xx_close((PNET_DEV)net_dev);
1286 RT_MOD_DEC_USE_COUNT();
1292 * invaild or writeback cache
1293 * and convert virtual address to physical address
1295 dma_addr_t linux_pci_map_single(void *handle, void *ptr, size_t size, int sd_idx, int direction)
1301 ------ Porting Information ------
1303 mgmt packets => sd_idx = 0
1304 SwIdx: pAd->MgmtRing.TxCpuIdx
1305 pTxD : pAd->MgmtRing.Cell[SwIdx].AllocVa;
1307 data packets => sd_idx = 1
1308 TxIdx : pAd->TxRing[pTxBlk->QueIdx].TxCpuIdx
1309 QueIdx: pTxBlk->QueIdx
1310 pTxD : pAd->TxRing[pTxBlk->QueIdx].Cell[TxIdx].AllocVa;
1316 pAd = (PRTMP_ADAPTER)handle;
1317 pObj = (POS_COOKIE)pAd->OS_Cookie;
1322 pTxBlk = (PTX_BLK)ptr;
1323 return pci_map_single(pObj->pci_dev, pTxBlk->pSrcBufData, pTxBlk->SrcBufLen, direction);
1327 return pci_map_single(pObj->pci_dev, ptr, size, direction);
1332 void linux_pci_unmap_single(void *handle, dma_addr_t dma_addr, size_t size, int direction)
1337 pAd=(PRTMP_ADAPTER)handle;
1338 pObj = (POS_COOKIE)pAd->OS_Cookie;
1340 pci_unmap_single(pObj->pci_dev, dma_addr, size, direction);