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 IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
481 bReschedule = STARxDoneInterruptHandle(pAd, 0);
483 RTMP_INT_LOCK(&pAd->irq_lock, flags);
485 * double check to avoid rotting packet
487 if (pAd->int_pending & INT_RX || bReschedule)
489 tasklet_hi_schedule(&pObj->rx_done_task);
490 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
494 /* enable RxINT again */
495 rt2860_int_enable(pAd, INT_RX);
496 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
500 void fifo_statistic_full_tasklet(unsigned long data)
503 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
506 // Do nothing if the driver is starting halt state.
507 // This might happen when timer already been fired before cancel timer with mlmehalt
508 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
511 pObj = (POS_COOKIE) pAd->OS_Cookie;
513 pAd->int_pending &= ~(FifoStaFullInt);
514 NICUpdateFifoStaCounters(pAd);
516 RTMP_INT_LOCK(&pAd->irq_lock, flags);
518 * double check to avoid rotting packet
520 if (pAd->int_pending & FifoStaFullInt)
522 tasklet_hi_schedule(&pObj->fifo_statistic_full_task);
523 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
527 /* enable RxINT again */
529 rt2860_int_enable(pAd, FifoStaFullInt);
530 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
534 static void hcca_dma_done_tasklet(unsigned long data)
537 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
538 INT_SOURCE_CSR_STRUC IntSource;
541 // Do nothing if the driver is starting halt state.
542 // This might happen when timer already been fired before cancel timer with mlmehalt
543 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
546 pObj = (POS_COOKIE) pAd->OS_Cookie;
550 IntSource.field.HccaDmaDone = 1;
551 pAd->int_pending &= ~INT_HCCA_DLY;
553 RTMPHandleTxRingDmaDoneInterrupt(pAd, IntSource);
555 RTMP_INT_LOCK(&pAd->irq_lock, flags);
557 * double check to avoid lose of interrupts
559 if (pAd->int_pending & INT_HCCA_DLY)
561 tasklet_hi_schedule(&pObj->hcca_dma_done_task);
562 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
566 /* enable TxDataInt again */
567 rt2860_int_enable(pAd, INT_HCCA_DLY);
568 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
571 static void ac3_dma_done_tasklet(unsigned long data)
574 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
575 INT_SOURCE_CSR_STRUC IntSource;
577 BOOLEAN bReschedule = 0;
579 // Do nothing if the driver is starting halt state.
580 // This might happen when timer already been fired before cancel timer with mlmehalt
581 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
584 pObj = (POS_COOKIE) pAd->OS_Cookie;
587 IntSource.field.Ac3DmaDone = 1;
588 pAd->int_pending &= ~INT_AC3_DLY;
590 bReschedule = RTMPHandleTxRingDmaDoneInterrupt(pAd, IntSource);
592 RTMP_INT_LOCK(&pAd->irq_lock, flags);
594 * double check to avoid lose of interrupts
596 if ((pAd->int_pending & INT_AC3_DLY) || bReschedule)
598 tasklet_hi_schedule(&pObj->ac3_dma_done_task);
599 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
603 /* enable TxDataInt again */
604 rt2860_int_enable(pAd, INT_AC3_DLY);
605 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
608 static void ac2_dma_done_tasklet(unsigned long data)
611 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
612 INT_SOURCE_CSR_STRUC IntSource;
614 BOOLEAN bReschedule = 0;
616 // Do nothing if the driver is starting halt state.
617 // This might happen when timer already been fired before cancel timer with mlmehalt
618 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
621 pObj = (POS_COOKIE) pAd->OS_Cookie;
624 IntSource.field.Ac2DmaDone = 1;
625 pAd->int_pending &= ~INT_AC2_DLY;
627 bReschedule = RTMPHandleTxRingDmaDoneInterrupt(pAd, IntSource);
629 RTMP_INT_LOCK(&pAd->irq_lock, flags);
632 * double check to avoid lose of interrupts
634 if ((pAd->int_pending & INT_AC2_DLY) || bReschedule)
636 tasklet_hi_schedule(&pObj->ac2_dma_done_task);
637 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
641 /* enable TxDataInt again */
642 rt2860_int_enable(pAd, INT_AC2_DLY);
643 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
646 static void ac1_dma_done_tasklet(unsigned long data)
649 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
650 INT_SOURCE_CSR_STRUC IntSource;
652 BOOLEAN bReschedule = 0;
654 // Do nothing if the driver is starting halt state.
655 // This might happen when timer already been fired before cancel timer with mlmehalt
656 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
659 pObj = (POS_COOKIE) pAd->OS_Cookie;
662 IntSource.field.Ac1DmaDone = 1;
663 pAd->int_pending &= ~INT_AC1_DLY;
665 bReschedule = RTMPHandleTxRingDmaDoneInterrupt(pAd, IntSource);
667 RTMP_INT_LOCK(&pAd->irq_lock, flags);
669 * double check to avoid lose of interrupts
671 if ((pAd->int_pending & INT_AC1_DLY) || bReschedule)
673 tasklet_hi_schedule(&pObj->ac1_dma_done_task);
674 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
678 /* enable TxDataInt again */
679 rt2860_int_enable(pAd, INT_AC1_DLY);
680 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
683 static void ac0_dma_done_tasklet(unsigned long data)
686 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
687 INT_SOURCE_CSR_STRUC IntSource;
689 BOOLEAN bReschedule = 0;
691 // Do nothing if the driver is starting halt state.
692 // This might happen when timer already been fired before cancel timer with mlmehalt
693 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
696 pObj = (POS_COOKIE) pAd->OS_Cookie;
699 IntSource.field.Ac0DmaDone = 1;
700 pAd->int_pending &= ~INT_AC0_DLY;
702 bReschedule = RTMPHandleTxRingDmaDoneInterrupt(pAd, IntSource);
704 RTMP_INT_LOCK(&pAd->irq_lock, flags);
706 * double check to avoid lose of interrupts
708 if ((pAd->int_pending & INT_AC0_DLY) || bReschedule)
710 tasklet_hi_schedule(&pObj->ac0_dma_done_task);
711 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
715 /* enable TxDataInt again */
716 rt2860_int_enable(pAd, INT_AC0_DLY);
717 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
724 rt2860_interrupt(int irq, void *dev_instance)
726 struct net_device *net_dev = (struct net_device *) dev_instance;
727 PRTMP_ADAPTER pAd = net_dev->ml_priv;
728 INT_SOURCE_CSR_STRUC IntSource;
732 pObj = (POS_COOKIE) pAd->OS_Cookie;
735 /* Note 03312008: we can not return here before
736 RTMP_IO_READ32(pAd, INT_SOURCE_CSR, &IntSource.word);
737 RTMP_IO_WRITE32(pAd, INT_SOURCE_CSR, IntSource.word);
738 Or kernel will panic after ifconfig ra0 down sometimes */
742 // Inital the Interrupt source.
744 IntSource.word = 0x00000000L;
745 // McuIntSource.word = 0x00000000L;
748 // Get the interrupt sources & saved to local variable
750 //RTMP_IO_READ32(pAd, where, &McuIntSource.word);
751 //RTMP_IO_WRITE32(pAd, , McuIntSource.word);
754 // Flag fOP_STATUS_DOZE On, means ASIC put to sleep, elase means ASICK WakeUp
755 // And at the same time, clock maybe turned off that say there is no DMA service.
756 // when ASIC get to sleep.
757 // To prevent system hang on power saving.
758 // We need to check it before handle the INT_SOURCE_CSR, ASIC must be wake up.
760 // RT2661 => when ASIC is sleeping, MAC register cannot be read and written.
761 // RT2860 => when ASIC is sleeping, MAC register can be read and written.
763 bOldValue = pAd->bPCIclkOff;
764 pAd->bPCIclkOff = FALSE;
766 RTMP_IO_READ32(pAd, INT_SOURCE_CSR, &IntSource.word);
767 RTMP_IO_WRITE32(pAd, INT_SOURCE_CSR, IntSource.word); // write 1 to clear
769 pAd->bPCIclkOff = bOldValue;
771 // Do nothing if Reset in progress
772 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS) ||
773 RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS))
779 // Handle interrupt, walk through all bits
780 // Should start from highest priority interrupt
781 // The priority can be adjust by altering processing if statement
784 // If required spinlock, each interrupt service routine has to acquire
785 // and release itself.
788 // Do nothing if NIC doesn't exist
789 if (IntSource.word == 0xffffffff)
791 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST | fRTMP_ADAPTER_HALT_IN_PROGRESS);
792 printk("snowpin - IntSource.word == 0xffffffff\n");
796 if (IntSource.word & TxCoherent)
798 DBGPRINT(RT_DEBUG_ERROR, (">>>TxCoherent<<<\n"));
799 RTMPHandleRxCoherentInterrupt(pAd);
802 if (IntSource.word & RxCoherent)
804 DBGPRINT(RT_DEBUG_ERROR, (">>>RxCoherent<<<\n"));
805 RTMPHandleRxCoherentInterrupt(pAd);
808 if (IntSource.word & FifoStaFullInt)
811 if ((pAd->int_disable_mask & FifoStaFullInt) == 0)
813 /* mask FifoStaFullInt */
814 rt2860_int_disable(pAd, FifoStaFullInt);
815 tasklet_hi_schedule(&pObj->fifo_statistic_full_task);
817 pAd->int_pending |= FifoStaFullInt;
819 NICUpdateFifoStaCounters(pAd);
823 if (IntSource.word & INT_MGMT_DLY)
825 if ((pAd->int_disable_mask & INT_MGMT_DLY) ==0 )
827 rt2860_int_disable(pAd, INT_MGMT_DLY);
828 tasklet_hi_schedule(&pObj->mgmt_dma_done_task);
830 pAd->int_pending |= INT_MGMT_DLY ;
833 if (IntSource.word & INT_RX)
835 if ((pAd->int_disable_mask & INT_RX) == 0)
838 rt2860_int_disable(pAd, INT_RX);
839 tasklet_hi_schedule(&pObj->rx_done_task);
841 pAd->int_pending |= INT_RX;
844 if (IntSource.word & INT_HCCA_DLY)
847 if ((pAd->int_disable_mask & INT_HCCA_DLY) == 0)
850 rt2860_int_disable(pAd, INT_HCCA_DLY);
851 tasklet_hi_schedule(&pObj->hcca_dma_done_task);
853 pAd->int_pending |= INT_HCCA_DLY;
856 if (IntSource.word & INT_AC3_DLY)
859 if ((pAd->int_disable_mask & INT_AC3_DLY) == 0)
862 rt2860_int_disable(pAd, INT_AC3_DLY);
863 tasklet_hi_schedule(&pObj->ac3_dma_done_task);
865 pAd->int_pending |= INT_AC3_DLY;
868 if (IntSource.word & INT_AC2_DLY)
871 if ((pAd->int_disable_mask & INT_AC2_DLY) == 0)
874 rt2860_int_disable(pAd, INT_AC2_DLY);
875 tasklet_hi_schedule(&pObj->ac2_dma_done_task);
877 pAd->int_pending |= INT_AC2_DLY;
880 if (IntSource.word & INT_AC1_DLY)
883 pAd->int_pending |= INT_AC1_DLY;
885 if ((pAd->int_disable_mask & INT_AC1_DLY) == 0)
888 rt2860_int_disable(pAd, INT_AC1_DLY);
889 tasklet_hi_schedule(&pObj->ac1_dma_done_task);
894 if (IntSource.word & INT_AC0_DLY)
896 pAd->int_pending |= INT_AC0_DLY;
898 if ((pAd->int_disable_mask & INT_AC0_DLY) == 0)
901 rt2860_int_disable(pAd, INT_AC0_DLY);
902 tasklet_hi_schedule(&pObj->ac0_dma_done_task);
907 if (IntSource.word & PreTBTTInt)
909 RTMPHandlePreTBTTInterrupt(pAd);
912 if (IntSource.word & TBTTInt)
914 RTMPHandleTBTTInterrupt(pAd);
917 IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
919 if (IntSource.word & AutoWakeupInt)
920 RTMPHandleTwakeupInterrupt(pAd);
927 ========================================================================
929 Check the chipset vendor/product ID.
932 _dev_p Point to the PCI or USB device
939 ========================================================================
941 BOOLEAN RT28XXChipsetCheck(
950 ========================================================================
952 Init net device structure.
955 _dev_p Point to the PCI or USB device
956 *net_dev Point to the net device
957 *pAd the raxx interface data pointer
964 ========================================================================
966 BOOLEAN RT28XXNetDevInit(
968 IN struct net_device *net_dev,
969 IN RTMP_ADAPTER *pAd)
971 struct pci_dev *pci_dev = (struct pci_dev *)_dev_p;
972 const CHAR *print_name;
976 print_name = pci_dev ? pci_name(pci_dev) : "rt2860";
978 net_dev->base_addr = 0;
981 if (pci_request_regions(pci_dev, print_name))
982 goto err_out_free_netdev;
984 // interrupt IRQ number
985 net_dev->irq = pci_dev->irq;
987 // map physical address to virtual address for accessing register
988 csr_addr = (unsigned long) ioremap(pci_resource_start(pci_dev, 0),
989 pci_resource_len(pci_dev, 0));
993 DBGPRINT(RT_DEBUG_ERROR,
994 ("ioremap failed for device %s, region 0x%lX @ 0x%lX\n",
995 print_name, (ULONG)pci_resource_len(pci_dev, 0),
996 (ULONG)pci_resource_start(pci_dev, 0)));
997 goto err_out_free_res;
1000 // Save CSR virtual address and irq to device structure
1001 net_dev->base_addr = csr_addr;
1002 pAd->CSRBaseAddress = (PUCHAR)net_dev->base_addr;
1005 pci_set_master(pci_dev);
1007 net_dev->priv_flags = INT_MAIN;
1009 DBGPRINT(RT_DEBUG_TRACE, ("%s: at 0x%lx, VA 0x%lx, IRQ %d. \n",
1010 net_dev->name, (ULONG)pci_resource_start(pci_dev, 0),
1011 (ULONG)csr_addr, pci_dev->irq));
1015 /* --------------------------- ERROR HANDLE --------------------------- */
1017 pci_release_regions(pci_dev);
1018 err_out_free_netdev:
1019 /* free netdev in caller, not here */
1025 ========================================================================
1026 Routine Description:
1027 Init net device structure.
1030 _dev_p Point to the PCI or USB device
1031 *pAd the raxx interface data pointer
1038 ========================================================================
1040 BOOLEAN RT28XXProbePostConfig(
1042 IN RTMP_ADAPTER *pAd,
1051 ========================================================================
1052 Routine Description:
1056 *pAd the raxx interface data pointer
1062 ========================================================================
1064 VOID RT28XXDMADisable(
1065 IN RTMP_ADAPTER *pAd)
1067 WPDMA_GLO_CFG_STRUC GloCfg;
1070 RTMP_IO_READ32(pAd, WPDMA_GLO_CFG, &GloCfg.word);
1071 GloCfg.word &= 0xff0;
1072 GloCfg.field.EnTXWriteBackDDONE =1;
1073 RTMP_IO_WRITE32(pAd, WPDMA_GLO_CFG, GloCfg.word);
1078 ========================================================================
1079 Routine Description:
1083 *pAd the raxx interface data pointer
1089 ========================================================================
1091 VOID RT28XXDMAEnable(
1092 IN RTMP_ADAPTER *pAd)
1094 WPDMA_GLO_CFG_STRUC GloCfg;
1097 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, 0x4);
1100 RTMP_IO_READ32(pAd, WPDMA_GLO_CFG, &GloCfg.word);
1101 if ((GloCfg.field.TxDMABusy == 0) && (GloCfg.field.RxDMABusy == 0))
1104 DBGPRINT(RT_DEBUG_TRACE, ("==> DMABusy\n"));
1105 RTMPusecDelay(1000);
1111 GloCfg.field.EnTXWriteBackDDONE = 1;
1112 GloCfg.field.WPDMABurstSIZE = 2;
1113 GloCfg.field.EnableRxDMA = 1;
1114 GloCfg.field.EnableTxDMA = 1;
1116 DBGPRINT(RT_DEBUG_TRACE, ("<== WRITE DMA offset 0x208 = 0x%x\n", GloCfg.word));
1117 RTMP_IO_WRITE32(pAd, WPDMA_GLO_CFG, GloCfg.word);
1122 ========================================================================
1123 Routine Description:
1124 Write Beacon buffer to Asic.
1127 *pAd the raxx interface data pointer
1133 ========================================================================
1135 VOID RT28xx_UpdateBeaconToAsic(
1136 IN RTMP_ADAPTER *pAd,
1141 ULONG CapInfoPos = 0;
1142 UCHAR *ptr, *ptr_update, *ptr_capinfo;
1144 BOOLEAN bBcnReq = FALSE;
1148 DBGPRINT(RT_DEBUG_ERROR, ("%s() : No valid Interface be found.\n", __func__));
1152 if (bBcnReq == FALSE)
1154 /* when the ra interface is down, do not send its beacon frame */
1155 /* clear all zero */
1156 for(i=0; i<TXWI_SIZE; i+=4)
1157 RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[bcn_idx] + i, 0x00);
1161 ptr = (PUCHAR)&pAd->BeaconTxWI;
1163 for (i=0; i<TXWI_SIZE; i+=4) // 16-byte TXWI field
1165 UINT32 longptr = *ptr + (*(ptr+1)<<8) + (*(ptr+2)<<16) + (*(ptr+3)<<24);
1166 RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[bcn_idx] + i, longptr);
1170 // Update CapabilityInfo in Beacon
1171 for (i = CapInfoPos; i < (CapInfoPos+2); i++)
1173 RTMP_IO_WRITE8(pAd, pAd->BeaconOffset[bcn_idx] + TXWI_SIZE + i, *ptr_capinfo);
1177 if (FrameLen > UpdatePos)
1179 for (i= UpdatePos; i< (FrameLen); i++)
1181 RTMP_IO_WRITE8(pAd, pAd->BeaconOffset[bcn_idx] + TXWI_SIZE + i, *ptr_update);
1190 VOID RTMPInitPCIeLinkCtrlValue(
1191 IN PRTMP_ADAPTER pAd)
1195 VOID RTMPFindHostPCIDev(
1196 IN PRTMP_ADAPTER pAd)
1201 ========================================================================
1203 Routine Description:
1206 Level = RESTORE_HALT : Restore PCI host and Ralink PCIe Link Control field to its default value.
1207 Level = Other Value : Restore from dot11 power save or radio off status. And force PCI host Link Control fields to 0x1
1209 ========================================================================
1211 VOID RTMPPCIeLinkCtrlValueRestore(
1212 IN PRTMP_ADAPTER pAd,
1218 ========================================================================
1220 Routine Description:
1223 Max : limit Host PCI and Ralink PCIe device's LINK CONTROL field's value.
1224 Because now frequently set our device to mode 1 or mode 3 will cause problem.
1226 ========================================================================
1228 VOID RTMPPCIeLinkCtrlSetting(
1229 IN PRTMP_ADAPTER pAd,
1234 VOID rt2860_stop(struct net_device *net_dev)
1236 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)NULL;
1237 if (net_dev == NULL)
1239 DBGPRINT(RT_DEBUG_ERROR, ("net_dev == NULL!\n"));
1242 pAd = net_dev->ml_priv;
1247 netif_carrier_off(net_dev);
1248 netif_stop_queue(net_dev);
1250 // mark device as removed from system and therefore no longer available
1251 netif_device_detach(net_dev);
1254 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
1255 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_RADIO_OFF);
1257 // take down the device
1258 rt28xx_close((PNET_DEV)net_dev);
1259 RT_MOD_DEC_USE_COUNT();
1265 * invaild or writeback cache
1266 * and convert virtual address to physical address
1268 dma_addr_t linux_pci_map_single(void *handle, void *ptr, size_t size, int sd_idx, int direction)
1274 ------ Porting Information ------
1276 mgmt packets => sd_idx = 0
1277 SwIdx: pAd->MgmtRing.TxCpuIdx
1278 pTxD : pAd->MgmtRing.Cell[SwIdx].AllocVa;
1280 data packets => sd_idx = 1
1281 TxIdx : pAd->TxRing[pTxBlk->QueIdx].TxCpuIdx
1282 QueIdx: pTxBlk->QueIdx
1283 pTxD : pAd->TxRing[pTxBlk->QueIdx].Cell[TxIdx].AllocVa;
1289 pAd = (PRTMP_ADAPTER)handle;
1290 pObj = (POS_COOKIE)pAd->OS_Cookie;
1295 pTxBlk = (PTX_BLK)ptr;
1296 return pci_map_single(pObj->pci_dev, pTxBlk->pSrcBufData, pTxBlk->SrcBufLen, direction);
1300 return pci_map_single(pObj->pci_dev, ptr, size, direction);
1305 void linux_pci_unmap_single(void *handle, dma_addr_t dma_addr, size_t size, int direction)
1310 pAd=(PRTMP_ADAPTER)handle;
1311 pObj = (POS_COOKIE)pAd->OS_Cookie;
1313 pci_unmap_single(pObj->pci_dev, dma_addr, size, direction);