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 {PCI_DEVICE(EDIMAX_PCI_VENDOR_ID, 0x7708)},
103 {PCI_DEVICE(EDIMAX_PCI_VENDOR_ID, 0x7728)},
104 {PCI_DEVICE(EDIMAX_PCI_VENDOR_ID, 0x7758)},
105 {PCI_DEVICE(EDIMAX_PCI_VENDOR_ID, 0x7727)},
106 {PCI_DEVICE(EDIMAX_PCI_VENDOR_ID, 0x7738)},
107 {PCI_DEVICE(EDIMAX_PCI_VENDOR_ID, 0x7748)},
108 {PCI_DEVICE(EDIMAX_PCI_VENDOR_ID, 0x7768)},
109 {0,} // terminate list
112 MODULE_DEVICE_TABLE(pci, rt2860_pci_tbl);
113 MODULE_LICENSE("GPL");
114 #ifdef MODULE_VERSION
115 MODULE_VERSION(STA_DRIVER_VERSION);
119 // Our PCI driver structure
121 static struct pci_driver rt2860_driver =
124 id_table: rt2860_pci_tbl,
125 probe: rt2860_init_one,
126 remove: __devexit_p(rt2860_remove_one),
129 suspend: rt2860_suspend,
130 resume: rt2860_resume,
137 VOID RT2860RejectPendingPackets(
138 IN PRTMP_ADAPTER pAd)
141 // clear TxSw packets
144 static int rt2860_suspend(
145 struct pci_dev *pci_dev,
148 struct net_device *net_dev = pci_get_drvdata(pci_dev);
149 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)NULL;
153 DBGPRINT(RT_DEBUG_TRACE, ("===> rt2860_suspend()\n"));
157 DBGPRINT(RT_DEBUG_ERROR, ("net_dev == NULL!\n"));
161 pAd = net_dev->ml_priv;
163 /* we can not use IFF_UP because ra0 down but ra1 up */
164 /* and 1 suspend/resume function for 1 module, not for each interface */
165 /* so Linux will call suspend/resume function once */
166 if (VIRTUAL_IF_NUM(pAd) > 0)
168 // avoid users do suspend after interface is down
171 netif_carrier_off(net_dev);
172 netif_stop_queue(net_dev);
174 // mark device as removed from system and therefore no longer available
175 netif_device_detach(net_dev);
178 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
179 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_RADIO_OFF);
181 // take down the device
182 rt28xx_close((PNET_DEV)net_dev);
184 RT_MOD_DEC_USE_COUNT();
188 // reference to http://vovo2000.com/type-lab/linux/kernel-api/linux-kernel-api.html
189 // enable device to generate PME# when suspended
190 // pci_choose_state(): Choose the power state of a PCI device to be suspended
191 retval = pci_enable_wake(pci_dev, pci_choose_state(pci_dev, state), 1);
192 // save the PCI configuration space of a device before suspending
193 pci_save_state(pci_dev);
194 // disable PCI device after use
195 pci_disable_device(pci_dev);
197 retval = pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
199 DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2860_suspend()\n"));
203 static int rt2860_resume(
204 struct pci_dev *pci_dev)
206 struct net_device *net_dev = pci_get_drvdata(pci_dev);
207 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)NULL;
211 // set the power state of a PCI device
212 // PCI has 4 power states, DO (normal) ~ D3(less power)
213 // in include/linux/pci.h, you can find that
214 // #define PCI_D0 ((pci_power_t __force) 0)
215 // #define PCI_D1 ((pci_power_t __force) 1)
216 // #define PCI_D2 ((pci_power_t __force) 2)
217 // #define PCI_D3hot ((pci_power_t __force) 3)
218 // #define PCI_D3cold ((pci_power_t __force) 4)
219 // #define PCI_UNKNOWN ((pci_power_t __force) 5)
220 // #define PCI_POWER_ERROR ((pci_power_t __force) -1)
221 retval = pci_set_power_state(pci_dev, PCI_D0);
223 // restore the saved state of a PCI device
224 pci_restore_state(pci_dev);
226 // initialize device before it's used by a driver
227 if (pci_enable_device(pci_dev))
229 printk("pci enable fail!\n");
233 DBGPRINT(RT_DEBUG_TRACE, ("===> rt2860_resume()\n"));
237 DBGPRINT(RT_DEBUG_ERROR, ("net_dev == NULL!\n"));
240 pAd = net_dev->ml_priv;
244 /* we can not use IFF_UP because ra0 down but ra1 up */
245 /* and 1 suspend/resume function for 1 module, not for each interface */
246 /* so Linux will call suspend/resume function once */
247 if (VIRTUAL_IF_NUM(pAd) > 0)
249 // mark device as attached from system and restart if needed
250 netif_device_attach(net_dev);
252 if (rt28xx_open((PNET_DEV)net_dev) != 0)
255 DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2860_resume()\n"));
259 // increase MODULE use count
260 RT_MOD_INC_USE_COUNT();
262 RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
263 RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_RADIO_OFF);
265 netif_start_queue(net_dev);
266 netif_carrier_on(net_dev);
267 netif_wake_queue(net_dev);
271 DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2860_resume()\n"));
274 #endif // CONFIG_PM //
277 static INT __init rt2860_init_module(VOID)
279 return pci_register_driver(&rt2860_driver);
284 // Driver module unload function
286 static VOID __exit rt2860_cleanup_module(VOID)
288 pci_unregister_driver(&rt2860_driver);
291 module_init(rt2860_init_module);
292 module_exit(rt2860_cleanup_module);
295 static INT __devinit rt2860_init_one (
296 IN struct pci_dev *pci_dev,
297 IN const struct pci_device_id *ent)
301 DBGPRINT(RT_DEBUG_TRACE, ("===> rt2860_init_one\n"));
303 // wake up and enable device
304 if (pci_enable_device (pci_dev))
310 rc = rt2860_probe(pci_dev, ent);
313 DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2860_init_one\n"));
318 static VOID __devexit rt2860_remove_one(
319 IN struct pci_dev *pci_dev)
321 struct net_device *net_dev = pci_get_drvdata(pci_dev);
322 RTMP_ADAPTER *pAd = net_dev->ml_priv;
324 DBGPRINT(RT_DEBUG_TRACE, ("===> rt2860_remove_one\n"));
328 // Unregister network device
329 unregister_netdev(net_dev);
331 // Unmap CSR base address
332 iounmap((char *)(net_dev->base_addr));
334 RTMPFreeAdapter(pAd);
336 // release memory region
337 release_mem_region(pci_resource_start(pci_dev, 0), pci_resource_len(pci_dev, 0));
341 // Unregister network device
342 unregister_netdev(net_dev);
344 // Unmap CSR base address
345 iounmap((char *)(net_dev->base_addr));
347 // release memory region
348 release_mem_region(pci_resource_start(pci_dev, 0), pci_resource_len(pci_dev, 0));
351 // Free pre-allocated net_device memory
352 free_netdev(net_dev);
356 // PCI device probe & initialization function
358 static INT __devinit rt2860_probe(
359 IN struct pci_dev *pci_dev,
360 IN const struct pci_device_id *ent)
365 rv = (INT)rt28xx_probe((void *)pci_dev, (void *)ent, 0, &pAd);
366 OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_ADVANCE_POWER_SAVE_PCIE_DEVICE);
371 void init_thread_task(IN PRTMP_ADAPTER pAd)
375 pObj = (POS_COOKIE) pAd->OS_Cookie;
377 tasklet_init(&pObj->rx_done_task, rx_done_tasklet, (unsigned long)pAd);
378 tasklet_init(&pObj->mgmt_dma_done_task, mgmt_dma_done_tasklet, (unsigned long)pAd);
379 tasklet_init(&pObj->ac0_dma_done_task, ac0_dma_done_tasklet, (unsigned long)pAd);
380 tasklet_init(&pObj->ac1_dma_done_task, ac1_dma_done_tasklet, (unsigned long)pAd);
381 tasklet_init(&pObj->ac2_dma_done_task, ac2_dma_done_tasklet, (unsigned long)pAd);
382 tasklet_init(&pObj->ac3_dma_done_task, ac3_dma_done_tasklet, (unsigned long)pAd);
383 tasklet_init(&pObj->hcca_dma_done_task, hcca_dma_done_tasklet, (unsigned long)pAd);
384 tasklet_init(&pObj->tbtt_task, tbtt_tasklet, (unsigned long)pAd);
385 tasklet_init(&pObj->fifo_statistic_full_task, fifo_statistic_full_tasklet, (unsigned long)pAd);
388 void kill_thread_task(IN PRTMP_ADAPTER pAd)
392 pObj = (POS_COOKIE) pAd->OS_Cookie;
394 tasklet_kill(&pObj->rx_done_task);
395 tasklet_kill(&pObj->mgmt_dma_done_task);
396 tasklet_kill(&pObj->ac0_dma_done_task);
397 tasklet_kill(&pObj->ac1_dma_done_task);
398 tasklet_kill(&pObj->ac2_dma_done_task);
399 tasklet_kill(&pObj->ac3_dma_done_task);
400 tasklet_kill(&pObj->hcca_dma_done_task);
401 tasklet_kill(&pObj->tbtt_task);
402 tasklet_kill(&pObj->fifo_statistic_full_task);
406 static void rt2860_int_enable(PRTMP_ADAPTER pAd, unsigned int mode)
410 pAd->int_disable_mask &= ~(mode);
411 regValue = pAd->int_enable_reg & ~(pAd->int_disable_mask);
412 RTMP_IO_WRITE32(pAd, INT_MASK_CSR, regValue); // 1:enable
415 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_ACTIVE);
419 static void rt2860_int_disable(PRTMP_ADAPTER pAd, unsigned int mode)
423 pAd->int_disable_mask |= mode;
424 regValue = pAd->int_enable_reg & ~(pAd->int_disable_mask);
425 RTMP_IO_WRITE32(pAd, INT_MASK_CSR, regValue); // 0: disable
429 RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_ACTIVE);
433 static void mgmt_dma_done_tasklet(unsigned long data)
436 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
437 INT_SOURCE_CSR_STRUC IntSource;
440 // Do nothing if the driver is starting halt state.
441 // This might happen when timer already been fired before cancel timer with mlmehalt
442 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
445 pObj = (POS_COOKIE) pAd->OS_Cookie;
448 IntSource.field.MgmtDmaDone = 1;
449 pAd->int_pending &= ~INT_MGMT_DLY;
451 RTMPHandleMgmtRingDmaDoneInterrupt(pAd);
453 // if you use RTMP_SEM_LOCK, sometimes kernel will hang up, no any
455 RTMP_INT_LOCK(&pAd->irq_lock, flags);
457 * double check to avoid lose of interrupts
459 if (pAd->int_pending & INT_MGMT_DLY)
461 tasklet_hi_schedule(&pObj->mgmt_dma_done_task);
462 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
466 /* enable TxDataInt again */
467 rt2860_int_enable(pAd, INT_MGMT_DLY);
468 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
471 static void rx_done_tasklet(unsigned long data)
474 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
475 BOOLEAN bReschedule = 0;
478 // Do nothing if the driver is starting halt state.
479 // This might happen when timer already been fired before cancel timer with mlmehalt
480 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
483 pObj = (POS_COOKIE) pAd->OS_Cookie;
485 pAd->int_pending &= ~(INT_RX);
487 bReschedule = STARxDoneInterruptHandle(pAd, 0);
489 RTMP_INT_LOCK(&pAd->irq_lock, flags);
491 * double check to avoid rotting packet
493 if (pAd->int_pending & INT_RX || bReschedule)
495 tasklet_hi_schedule(&pObj->rx_done_task);
496 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
500 /* enable RxINT again */
501 rt2860_int_enable(pAd, INT_RX);
502 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
506 void fifo_statistic_full_tasklet(unsigned long data)
509 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
512 // Do nothing if the driver is starting halt state.
513 // This might happen when timer already been fired before cancel timer with mlmehalt
514 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
517 pObj = (POS_COOKIE) pAd->OS_Cookie;
519 pAd->int_pending &= ~(FifoStaFullInt);
520 NICUpdateFifoStaCounters(pAd);
522 RTMP_INT_LOCK(&pAd->irq_lock, flags);
524 * double check to avoid rotting packet
526 if (pAd->int_pending & FifoStaFullInt)
528 tasklet_hi_schedule(&pObj->fifo_statistic_full_task);
529 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
533 /* enable RxINT again */
535 rt2860_int_enable(pAd, FifoStaFullInt);
536 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
540 static void hcca_dma_done_tasklet(unsigned long data)
543 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
544 INT_SOURCE_CSR_STRUC IntSource;
547 // Do nothing if the driver is starting halt state.
548 // This might happen when timer already been fired before cancel timer with mlmehalt
549 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
552 pObj = (POS_COOKIE) pAd->OS_Cookie;
556 IntSource.field.HccaDmaDone = 1;
557 pAd->int_pending &= ~INT_HCCA_DLY;
559 RTMPHandleTxRingDmaDoneInterrupt(pAd, IntSource);
561 RTMP_INT_LOCK(&pAd->irq_lock, flags);
563 * double check to avoid lose of interrupts
565 if (pAd->int_pending & INT_HCCA_DLY)
567 tasklet_hi_schedule(&pObj->hcca_dma_done_task);
568 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
572 /* enable TxDataInt again */
573 rt2860_int_enable(pAd, INT_HCCA_DLY);
574 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
577 static void ac3_dma_done_tasklet(unsigned long data)
580 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
581 INT_SOURCE_CSR_STRUC IntSource;
583 BOOLEAN bReschedule = 0;
585 // Do nothing if the driver is starting halt state.
586 // This might happen when timer already been fired before cancel timer with mlmehalt
587 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
590 pObj = (POS_COOKIE) pAd->OS_Cookie;
593 IntSource.field.Ac3DmaDone = 1;
594 pAd->int_pending &= ~INT_AC3_DLY;
596 bReschedule = RTMPHandleTxRingDmaDoneInterrupt(pAd, IntSource);
598 RTMP_INT_LOCK(&pAd->irq_lock, flags);
600 * double check to avoid lose of interrupts
602 if ((pAd->int_pending & INT_AC3_DLY) || bReschedule)
604 tasklet_hi_schedule(&pObj->ac3_dma_done_task);
605 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
609 /* enable TxDataInt again */
610 rt2860_int_enable(pAd, INT_AC3_DLY);
611 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
614 static void ac2_dma_done_tasklet(unsigned long data)
617 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
618 INT_SOURCE_CSR_STRUC IntSource;
620 BOOLEAN bReschedule = 0;
622 // Do nothing if the driver is starting halt state.
623 // This might happen when timer already been fired before cancel timer with mlmehalt
624 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
627 pObj = (POS_COOKIE) pAd->OS_Cookie;
630 IntSource.field.Ac2DmaDone = 1;
631 pAd->int_pending &= ~INT_AC2_DLY;
633 bReschedule = RTMPHandleTxRingDmaDoneInterrupt(pAd, IntSource);
635 RTMP_INT_LOCK(&pAd->irq_lock, flags);
638 * double check to avoid lose of interrupts
640 if ((pAd->int_pending & INT_AC2_DLY) || bReschedule)
642 tasklet_hi_schedule(&pObj->ac2_dma_done_task);
643 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
647 /* enable TxDataInt again */
648 rt2860_int_enable(pAd, INT_AC2_DLY);
649 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
652 static void ac1_dma_done_tasklet(unsigned long data)
655 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
656 INT_SOURCE_CSR_STRUC IntSource;
658 BOOLEAN bReschedule = 0;
660 // Do nothing if the driver is starting halt state.
661 // This might happen when timer already been fired before cancel timer with mlmehalt
662 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
665 pObj = (POS_COOKIE) pAd->OS_Cookie;
668 IntSource.field.Ac1DmaDone = 1;
669 pAd->int_pending &= ~INT_AC1_DLY;
671 bReschedule = RTMPHandleTxRingDmaDoneInterrupt(pAd, IntSource);
673 RTMP_INT_LOCK(&pAd->irq_lock, flags);
675 * double check to avoid lose of interrupts
677 if ((pAd->int_pending & INT_AC1_DLY) || bReschedule)
679 tasklet_hi_schedule(&pObj->ac1_dma_done_task);
680 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
684 /* enable TxDataInt again */
685 rt2860_int_enable(pAd, INT_AC1_DLY);
686 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
689 static void ac0_dma_done_tasklet(unsigned long data)
692 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) data;
693 INT_SOURCE_CSR_STRUC IntSource;
695 BOOLEAN bReschedule = 0;
697 // Do nothing if the driver is starting halt state.
698 // This might happen when timer already been fired before cancel timer with mlmehalt
699 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST))
702 pObj = (POS_COOKIE) pAd->OS_Cookie;
705 IntSource.field.Ac0DmaDone = 1;
706 pAd->int_pending &= ~INT_AC0_DLY;
708 bReschedule = RTMPHandleTxRingDmaDoneInterrupt(pAd, IntSource);
710 RTMP_INT_LOCK(&pAd->irq_lock, flags);
712 * double check to avoid lose of interrupts
714 if ((pAd->int_pending & INT_AC0_DLY) || bReschedule)
716 tasklet_hi_schedule(&pObj->ac0_dma_done_task);
717 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
721 /* enable TxDataInt again */
722 rt2860_int_enable(pAd, INT_AC0_DLY);
723 RTMP_INT_UNLOCK(&pAd->irq_lock, flags);
730 rt2860_interrupt(int irq, void *dev_instance)
732 struct net_device *net_dev = (struct net_device *) dev_instance;
733 PRTMP_ADAPTER pAd = net_dev->ml_priv;
734 INT_SOURCE_CSR_STRUC IntSource;
738 pObj = (POS_COOKIE) pAd->OS_Cookie;
741 /* Note 03312008: we can not return here before
742 RTMP_IO_READ32(pAd, INT_SOURCE_CSR, &IntSource.word);
743 RTMP_IO_WRITE32(pAd, INT_SOURCE_CSR, IntSource.word);
744 Or kernel will panic after ifconfig ra0 down sometimes */
748 // Inital the Interrupt source.
750 IntSource.word = 0x00000000L;
751 // McuIntSource.word = 0x00000000L;
754 // Get the interrupt sources & saved to local variable
756 //RTMP_IO_READ32(pAd, where, &McuIntSource.word);
757 //RTMP_IO_WRITE32(pAd, , McuIntSource.word);
760 // Flag fOP_STATUS_DOZE On, means ASIC put to sleep, elase means ASICK WakeUp
761 // And at the same time, clock maybe turned off that say there is no DMA service.
762 // when ASIC get to sleep.
763 // To prevent system hang on power saving.
764 // We need to check it before handle the INT_SOURCE_CSR, ASIC must be wake up.
766 // RT2661 => when ASIC is sleeping, MAC register cannot be read and written.
767 // RT2860 => when ASIC is sleeping, MAC register can be read and written.
769 bOldValue = pAd->bPCIclkOff;
770 pAd->bPCIclkOff = FALSE;
772 RTMP_IO_READ32(pAd, INT_SOURCE_CSR, &IntSource.word);
773 RTMP_IO_WRITE32(pAd, INT_SOURCE_CSR, IntSource.word); // write 1 to clear
775 pAd->bPCIclkOff = bOldValue;
777 // Do nothing if Reset in progress
778 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS) ||
779 RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS))
785 // Handle interrupt, walk through all bits
786 // Should start from highest priority interrupt
787 // The priority can be adjust by altering processing if statement
790 // If required spinlock, each interrupt service routine has to acquire
791 // and release itself.
794 // Do nothing if NIC doesn't exist
795 if (IntSource.word == 0xffffffff)
797 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST | fRTMP_ADAPTER_HALT_IN_PROGRESS);
798 printk("snowpin - IntSource.word == 0xffffffff\n");
802 if (IntSource.word & TxCoherent)
804 DBGPRINT(RT_DEBUG_ERROR, (">>>TxCoherent<<<\n"));
805 RTMPHandleRxCoherentInterrupt(pAd);
808 if (IntSource.word & RxCoherent)
810 DBGPRINT(RT_DEBUG_ERROR, (">>>RxCoherent<<<\n"));
811 RTMPHandleRxCoherentInterrupt(pAd);
814 if (IntSource.word & FifoStaFullInt)
817 if ((pAd->int_disable_mask & FifoStaFullInt) == 0)
819 /* mask FifoStaFullInt */
820 rt2860_int_disable(pAd, FifoStaFullInt);
821 tasklet_hi_schedule(&pObj->fifo_statistic_full_task);
823 pAd->int_pending |= FifoStaFullInt;
825 NICUpdateFifoStaCounters(pAd);
829 if (IntSource.word & INT_MGMT_DLY)
831 if ((pAd->int_disable_mask & INT_MGMT_DLY) ==0 )
833 rt2860_int_disable(pAd, INT_MGMT_DLY);
834 tasklet_hi_schedule(&pObj->mgmt_dma_done_task);
836 pAd->int_pending |= INT_MGMT_DLY ;
839 if (IntSource.word & INT_RX)
841 if ((pAd->int_disable_mask & INT_RX) == 0)
844 rt2860_int_disable(pAd, INT_RX);
845 tasklet_hi_schedule(&pObj->rx_done_task);
847 pAd->int_pending |= INT_RX;
850 if (IntSource.word & INT_HCCA_DLY)
853 if ((pAd->int_disable_mask & INT_HCCA_DLY) == 0)
856 rt2860_int_disable(pAd, INT_HCCA_DLY);
857 tasklet_hi_schedule(&pObj->hcca_dma_done_task);
859 pAd->int_pending |= INT_HCCA_DLY;
862 if (IntSource.word & INT_AC3_DLY)
865 if ((pAd->int_disable_mask & INT_AC3_DLY) == 0)
868 rt2860_int_disable(pAd, INT_AC3_DLY);
869 tasklet_hi_schedule(&pObj->ac3_dma_done_task);
871 pAd->int_pending |= INT_AC3_DLY;
874 if (IntSource.word & INT_AC2_DLY)
877 if ((pAd->int_disable_mask & INT_AC2_DLY) == 0)
880 rt2860_int_disable(pAd, INT_AC2_DLY);
881 tasklet_hi_schedule(&pObj->ac2_dma_done_task);
883 pAd->int_pending |= INT_AC2_DLY;
886 if (IntSource.word & INT_AC1_DLY)
889 pAd->int_pending |= INT_AC1_DLY;
891 if ((pAd->int_disable_mask & INT_AC1_DLY) == 0)
894 rt2860_int_disable(pAd, INT_AC1_DLY);
895 tasklet_hi_schedule(&pObj->ac1_dma_done_task);
900 if (IntSource.word & INT_AC0_DLY)
902 pAd->int_pending |= INT_AC0_DLY;
904 if ((pAd->int_disable_mask & INT_AC0_DLY) == 0)
907 rt2860_int_disable(pAd, INT_AC0_DLY);
908 tasklet_hi_schedule(&pObj->ac0_dma_done_task);
913 if (IntSource.word & PreTBTTInt)
915 RTMPHandlePreTBTTInterrupt(pAd);
918 if (IntSource.word & TBTTInt)
920 RTMPHandleTBTTInterrupt(pAd);
923 if (IntSource.word & AutoWakeupInt)
924 RTMPHandleTwakeupInterrupt(pAd);
930 ========================================================================
932 Check the chipset vendor/product ID.
935 _dev_p Point to the PCI or USB device
942 ========================================================================
944 BOOLEAN RT28XXChipsetCheck(
953 ========================================================================
955 Init net device structure.
958 _dev_p Point to the PCI or USB device
959 *net_dev Point to the net device
960 *pAd the raxx interface data pointer
967 ========================================================================
969 BOOLEAN RT28XXNetDevInit(
971 IN struct net_device *net_dev,
972 IN RTMP_ADAPTER *pAd)
974 struct pci_dev *pci_dev = (struct pci_dev *)_dev_p;
975 const CHAR *print_name;
979 print_name = pci_dev ? pci_name(pci_dev) : "rt2860";
981 net_dev->base_addr = 0;
984 if (pci_request_regions(pci_dev, print_name))
985 goto err_out_free_netdev;
987 // interrupt IRQ number
988 net_dev->irq = pci_dev->irq;
990 // map physical address to virtual address for accessing register
991 csr_addr = (unsigned long) ioremap(pci_resource_start(pci_dev, 0),
992 pci_resource_len(pci_dev, 0));
996 DBGPRINT(RT_DEBUG_ERROR,
997 ("ioremap failed for device %s, region 0x%lX @ 0x%lX\n",
998 print_name, (ULONG)pci_resource_len(pci_dev, 0),
999 (ULONG)pci_resource_start(pci_dev, 0)));
1000 goto err_out_free_res;
1003 // Save CSR virtual address and irq to device structure
1004 net_dev->base_addr = csr_addr;
1005 pAd->CSRBaseAddress = (PUCHAR)net_dev->base_addr;
1008 pci_set_master(pci_dev);
1010 net_dev->priv_flags = INT_MAIN;
1012 DBGPRINT(RT_DEBUG_TRACE, ("%s: at 0x%lx, VA 0x%lx, IRQ %d. \n",
1013 net_dev->name, (ULONG)pci_resource_start(pci_dev, 0),
1014 (ULONG)csr_addr, pci_dev->irq));
1018 /* --------------------------- ERROR HANDLE --------------------------- */
1020 pci_release_regions(pci_dev);
1021 err_out_free_netdev:
1022 /* free netdev in caller, not here */
1028 ========================================================================
1029 Routine Description:
1030 Init net device structure.
1033 _dev_p Point to the PCI or USB device
1034 *pAd the raxx interface data pointer
1041 ========================================================================
1043 BOOLEAN RT28XXProbePostConfig(
1045 IN RTMP_ADAPTER *pAd,
1054 ========================================================================
1055 Routine Description:
1059 *pAd the raxx interface data pointer
1065 ========================================================================
1067 VOID RT28XXDMADisable(
1068 IN RTMP_ADAPTER *pAd)
1070 WPDMA_GLO_CFG_STRUC GloCfg;
1073 RTMP_IO_READ32(pAd, WPDMA_GLO_CFG, &GloCfg.word);
1074 GloCfg.word &= 0xff0;
1075 GloCfg.field.EnTXWriteBackDDONE =1;
1076 RTMP_IO_WRITE32(pAd, WPDMA_GLO_CFG, GloCfg.word);
1081 ========================================================================
1082 Routine Description:
1086 *pAd the raxx interface data pointer
1092 ========================================================================
1094 VOID RT28XXDMAEnable(
1095 IN RTMP_ADAPTER *pAd)
1097 WPDMA_GLO_CFG_STRUC GloCfg;
1100 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, 0x4);
1103 RTMP_IO_READ32(pAd, WPDMA_GLO_CFG, &GloCfg.word);
1104 if ((GloCfg.field.TxDMABusy == 0) && (GloCfg.field.RxDMABusy == 0))
1107 DBGPRINT(RT_DEBUG_TRACE, ("==> DMABusy\n"));
1108 RTMPusecDelay(1000);
1114 GloCfg.field.EnTXWriteBackDDONE = 1;
1115 GloCfg.field.WPDMABurstSIZE = 2;
1116 GloCfg.field.EnableRxDMA = 1;
1117 GloCfg.field.EnableTxDMA = 1;
1119 DBGPRINT(RT_DEBUG_TRACE, ("<== WRITE DMA offset 0x208 = 0x%x\n", GloCfg.word));
1120 RTMP_IO_WRITE32(pAd, WPDMA_GLO_CFG, GloCfg.word);
1125 ========================================================================
1126 Routine Description:
1127 Write Beacon buffer to Asic.
1130 *pAd the raxx interface data pointer
1136 ========================================================================
1138 VOID RT28xx_UpdateBeaconToAsic(
1139 IN RTMP_ADAPTER *pAd,
1144 ULONG CapInfoPos = 0;
1145 UCHAR *ptr, *ptr_update, *ptr_capinfo;
1147 BOOLEAN bBcnReq = FALSE;
1151 DBGPRINT(RT_DEBUG_ERROR, ("%s() : No valid Interface be found.\n", __func__));
1155 if (bBcnReq == FALSE)
1157 /* when the ra interface is down, do not send its beacon frame */
1158 /* clear all zero */
1159 for(i=0; i<TXWI_SIZE; i+=4)
1160 RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[bcn_idx] + i, 0x00);
1164 ptr = (PUCHAR)&pAd->BeaconTxWI;
1166 for (i=0; i<TXWI_SIZE; i+=4) // 16-byte TXWI field
1168 UINT32 longptr = *ptr + (*(ptr+1)<<8) + (*(ptr+2)<<16) + (*(ptr+3)<<24);
1169 RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[bcn_idx] + i, longptr);
1173 // Update CapabilityInfo in Beacon
1174 for (i = CapInfoPos; i < (CapInfoPos+2); i++)
1176 RTMP_IO_WRITE8(pAd, pAd->BeaconOffset[bcn_idx] + TXWI_SIZE + i, *ptr_capinfo);
1180 if (FrameLen > UpdatePos)
1182 for (i= UpdatePos; i< (FrameLen); i++)
1184 RTMP_IO_WRITE8(pAd, pAd->BeaconOffset[bcn_idx] + TXWI_SIZE + i, *ptr_update);
1193 VOID RTMPInitPCIeLinkCtrlValue(
1194 IN PRTMP_ADAPTER pAd)
1198 VOID RTMPFindHostPCIDev(
1199 IN PRTMP_ADAPTER pAd)
1204 ========================================================================
1206 Routine Description:
1209 Level = RESTORE_HALT : Restore PCI host and Ralink PCIe Link Control field to its default value.
1210 Level = Other Value : Restore from dot11 power save or radio off status. And force PCI host Link Control fields to 0x1
1212 ========================================================================
1214 VOID RTMPPCIeLinkCtrlValueRestore(
1215 IN PRTMP_ADAPTER pAd,
1221 ========================================================================
1223 Routine Description:
1226 Max : limit Host PCI and Ralink PCIe device's LINK CONTROL field's value.
1227 Because now frequently set our device to mode 1 or mode 3 will cause problem.
1229 ========================================================================
1231 VOID RTMPPCIeLinkCtrlSetting(
1232 IN PRTMP_ADAPTER pAd,
1237 VOID rt2860_stop(struct net_device *net_dev)
1239 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)NULL;
1240 if (net_dev == NULL)
1242 DBGPRINT(RT_DEBUG_ERROR, ("net_dev == NULL!\n"));
1245 pAd = net_dev->ml_priv;
1250 netif_carrier_off(net_dev);
1251 netif_stop_queue(net_dev);
1253 // mark device as removed from system and therefore no longer available
1254 netif_device_detach(net_dev);
1257 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
1258 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_RADIO_OFF);
1260 // take down the device
1261 rt28xx_close((PNET_DEV)net_dev);
1262 RT_MOD_DEC_USE_COUNT();
1268 * invaild or writeback cache
1269 * and convert virtual address to physical address
1271 dma_addr_t linux_pci_map_single(void *handle, void *ptr, size_t size, int sd_idx, int direction)
1277 ------ Porting Information ------
1279 mgmt packets => sd_idx = 0
1280 SwIdx: pAd->MgmtRing.TxCpuIdx
1281 pTxD : pAd->MgmtRing.Cell[SwIdx].AllocVa;
1283 data packets => sd_idx = 1
1284 TxIdx : pAd->TxRing[pTxBlk->QueIdx].TxCpuIdx
1285 QueIdx: pTxBlk->QueIdx
1286 pTxD : pAd->TxRing[pTxBlk->QueIdx].Cell[TxIdx].AllocVa;
1292 pAd = (PRTMP_ADAPTER)handle;
1293 pObj = (POS_COOKIE)pAd->OS_Cookie;
1298 pTxBlk = (PTX_BLK)ptr;
1299 return pci_map_single(pObj->pci_dev, pTxBlk->pSrcBufData, pTxBlk->SrcBufLen, direction);
1303 return pci_map_single(pObj->pci_dev, ptr, size, direction);
1308 void linux_pci_unmap_single(void *handle, dma_addr_t dma_addr, size_t size, int direction)
1313 pAd=(PRTMP_ADAPTER)handle;
1314 pObj = (POS_COOKIE)pAd->OS_Cookie;
1316 pci_unmap_single(pObj->pci_dev, dma_addr, size, direction);