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 -------- ---------- ----------------------------------------------
36 Sample Mar/21/07 Merge RT2870 and RT2860 drivers.
39 #include "rt_config.h"
41 #define FORTY_MHZ_INTOLERANT_INTERVAL (60*1000) // 1 min
43 #ifdef MULTIPLE_CARD_SUPPORT
44 // record whether the card in the card list is used in the card file
45 UINT8 MC_CardUsed[MAX_NUM_OF_MULTIPLE_CARD];
46 // record used card mac address in the card list
47 static UINT8 MC_CardMac[MAX_NUM_OF_MULTIPLE_CARD][6];
48 #endif // MULTIPLE_CARD_SUPPORT //
50 /*---------------------------------------------------------------------*/
51 /* Private Variables Used */
52 /*---------------------------------------------------------------------*/
53 //static RALINK_TIMER_STRUCT PeriodicTimer;
55 char *mac = ""; // default 00:00:00:00:00:00
56 char *hostname = ""; // default CMPC
57 module_param (mac, charp, 0);
58 MODULE_PARM_DESC (mac, "rt28xx: wireless mac addr");
61 /*---------------------------------------------------------------------*/
62 /* Prototypes of Functions Used */
63 /*---------------------------------------------------------------------*/
64 #ifdef DOT11_N_SUPPORT
65 extern BOOLEAN ba_reordering_resource_init(PRTMP_ADAPTER pAd, int num);
66 extern void ba_reordering_resource_release(PRTMP_ADAPTER pAd);
67 #endif // DOT11_N_SUPPORT //
68 extern NDIS_STATUS NICLoadRateSwitchingParams(IN PRTMP_ADAPTER pAd);
70 extern void init_thread_task(PRTMP_ADAPTER pAd);
72 // public function prototype
73 INT __devinit rt28xx_probe(IN void *_dev_p, IN void *_dev_id_p,
74 IN UINT argc, OUT PRTMP_ADAPTER *ppAd);
76 // private function prototype
77 static int rt28xx_init(IN struct net_device *net_dev);
78 INT rt28xx_send_packets(IN struct sk_buff *skb_p, IN struct net_device *net_dev);
80 static void CfgInitHook(PRTMP_ADAPTER pAd);
82 #ifdef CONFIG_STA_SUPPORT
83 extern const struct iw_handler_def rt28xx_iw_handler_def;
84 #endif // CONFIG_STA_SUPPORT //
86 #if WIRELESS_EXT >= 12
87 // This function will be called when query /proc
88 struct iw_statistics *rt28xx_get_wireless_stats(
89 IN struct net_device *net_dev);
92 struct net_device_stats *RT28xx_get_ether_stats(
93 IN struct net_device *net_dev);
96 ========================================================================
101 *net_dev the raxx interface pointer
108 1. if open fail, kernel will not call the close function.
110 (1) Mlme Memory Handler: MlmeHalt()
111 (2) TX & RX: RTMPFreeTxRxRingMemory()
112 (3) BA Reordering: ba_reordering_resource_release()
113 ========================================================================
115 int MainVirtualIF_close(IN struct net_device *net_dev)
117 RTMP_ADAPTER *pAd = net_dev->ml_priv;
119 // Sanity check for pAd
121 return 0; // close ok
123 netif_carrier_off(pAd->net_dev);
124 netif_stop_queue(pAd->net_dev);
127 VIRTUAL_IF_DOWN(pAd);
129 RT_MOD_DEC_USE_COUNT();
131 return 0; // close ok
135 ========================================================================
140 *net_dev the raxx interface pointer
147 1. if open fail, kernel will not call the close function.
149 (1) Mlme Memory Handler: MlmeHalt()
150 (2) TX & RX: RTMPFreeTxRxRingMemory()
151 (3) BA Reordering: ba_reordering_resource_release()
152 ========================================================================
154 int MainVirtualIF_open(IN struct net_device *net_dev)
156 RTMP_ADAPTER *pAd = net_dev->ml_priv;
158 // Sanity check for pAd
160 return 0; // close ok
162 if (VIRTUAL_IF_UP(pAd) != 0)
165 // increase MODULE use count
166 RT_MOD_INC_USE_COUNT();
168 netif_start_queue(net_dev);
169 netif_carrier_on(net_dev);
170 netif_wake_queue(net_dev);
176 ========================================================================
178 Close raxx interface.
181 *net_dev the raxx interface pointer
188 1. if open fail, kernel will not call the close function.
190 (1) Mlme Memory Handler: MlmeHalt()
191 (2) TX & RX: RTMPFreeTxRxRingMemory()
192 (3) BA Reordering: ba_reordering_resource_release()
193 ========================================================================
195 int rt28xx_close(IN PNET_DEV dev)
197 struct net_device * net_dev = (struct net_device *)dev;
198 RTMP_ADAPTER *pAd = net_dev->ml_priv;
199 BOOLEAN Cancelled = FALSE;
203 DBGPRINT(RT_DEBUG_TRACE, ("===> rt28xx_close\n"));
205 // Sanity check for pAd
207 return 0; // close ok
209 #ifdef CONFIG_STA_SUPPORT
210 IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
212 // If dirver doesn't wake up firmware here,
213 // NICLoadFirmware will hang forever when interface is up again.
214 if (OPSTATUS_TEST_FLAG(pAd, fOP_STATUS_DOZE) ||
215 RTMP_SET_PSFLAG(pAd, fRTMP_PS_SET_PCI_CLK_OFF_COMMAND) ||
216 RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_IDLE_RADIO_OFF))
218 AsicForceWakeup(pAd, RTMP_HALT);
221 #ifdef QOS_DLS_SUPPORT
222 // send DLS-TEAR_DOWN message,
223 if (pAd->CommonCfg.bDLSCapable)
227 // tear down local dls table entry
228 for (i=0; i<MAX_NUM_OF_INIT_DLS_ENTRY; i++)
230 if (pAd->StaCfg.DLSEntry[i].Valid && (pAd->StaCfg.DLSEntry[i].Status == DLS_FINISH))
232 RTMPSendDLSTearDownFrame(pAd, pAd->StaCfg.DLSEntry[i].MacAddr);
233 pAd->StaCfg.DLSEntry[i].Status = DLS_NONE;
234 pAd->StaCfg.DLSEntry[i].Valid = FALSE;
238 // tear down peer dls table entry
239 for (i=MAX_NUM_OF_INIT_DLS_ENTRY; i<MAX_NUM_OF_DLS_ENTRY; i++)
241 if (pAd->StaCfg.DLSEntry[i].Valid && (pAd->StaCfg.DLSEntry[i].Status == DLS_FINISH))
243 RTMPSendDLSTearDownFrame(pAd, pAd->StaCfg.DLSEntry[i].MacAddr);
244 pAd->StaCfg.DLSEntry[i].Status = DLS_NONE;
245 pAd->StaCfg.DLSEntry[i].Valid = FALSE;
248 RT28XX_MLME_HANDLER(pAd);
250 #endif // QOS_DLS_SUPPORT //
253 (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)))
255 MLME_DISASSOC_REQ_STRUCT DisReq;
256 MLME_QUEUE_ELEM *MsgElem = (MLME_QUEUE_ELEM *) kmalloc(sizeof(MLME_QUEUE_ELEM), MEM_ALLOC_FLAG);
258 COPY_MAC_ADDR(DisReq.Addr, pAd->CommonCfg.Bssid);
259 DisReq.Reason = REASON_DEAUTH_STA_LEAVING;
261 MsgElem->Machine = ASSOC_STATE_MACHINE;
262 MsgElem->MsgType = MT2_MLME_DISASSOC_REQ;
263 MsgElem->MsgLen = sizeof(MLME_DISASSOC_REQ_STRUCT);
264 NdisMoveMemory(MsgElem->Msg, &DisReq, sizeof(MLME_DISASSOC_REQ_STRUCT));
266 // Prevent to connect AP again in STAMlmePeriodicExec
267 pAd->MlmeAux.AutoReconnectSsidLen= 32;
268 NdisZeroMemory(pAd->MlmeAux.AutoReconnectSsid, pAd->MlmeAux.AutoReconnectSsidLen);
270 pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_OID_DISASSOC;
271 MlmeDisassocReqAction(pAd, MsgElem);
279 RTMPCancelTimer(&pAd->StaCfg.LeapAuthTimer, &Cancelled);
282 RTMPCancelTimer(&pAd->StaCfg.StaQuickResponeForRateUpTimer, &Cancelled);
283 RTMPCancelTimer(&pAd->StaCfg.WpaDisassocAndBlockAssocTimer, &Cancelled);
285 #ifdef WPA_SUPPLICANT_SUPPORT
286 #ifndef NATIVE_WPA_SUPPLICANT_SUPPORT
288 union iwreq_data wrqu;
289 // send wireless event to wpa_supplicant for infroming interface down.
290 memset(&wrqu, 0, sizeof(wrqu));
291 wrqu.data.flags = RT_INTERFACE_DOWN;
292 wireless_send_event(pAd->net_dev, IWEVCUSTOM, &wrqu, NULL);
294 #endif // NATIVE_WPA_SUPPLICANT_SUPPORT //
295 #endif // WPA_SUPPLICANT_SUPPORT //
298 pAd->bPCIclkOff = FALSE;
300 #endif // CONFIG_STA_SUPPORT //
302 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
304 for (i = 0 ; i < NUM_OF_TX_RING; i++)
306 while (pAd->DeQueueRunning[i] == TRUE)
308 printk("Waiting for TxQueue[%d] done..........\n", i);
313 // Stop Mlme state machine
316 // Close kernel threads or tasklets
317 kill_thread_task(pAd);
320 #ifdef CONFIG_STA_SUPPORT
321 IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
325 #endif // CONFIG_STA_SUPPORT //
328 MeasureReqTabExit(pAd);
332 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_ACTIVE))
334 NICDisableInterrupt(pAd);
337 // Disable Rx, register value supposed will remain after reset
341 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE))
343 // Deregister interrupt function
344 RT28XX_IRQ_RELEASE(net_dev)
345 RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE);
349 // Free Ring or USB buffers
350 RTMPFreeTxRxRingMemory(pAd);
352 RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
354 #ifdef DOT11_N_SUPPORT
355 // Free BA reorder resource
356 ba_reordering_resource_release(pAd);
357 #endif // DOT11_N_SUPPORT //
360 RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_START_UP);
362 return 0; // close ok
363 } /* End of rt28xx_close */
365 static int rt28xx_init(IN struct net_device *net_dev)
367 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)net_dev->ml_priv;
374 #ifdef DOT11_N_SUPPORT
375 // Allocate BA Reordering memory
376 ba_reordering_resource_init(pAd, MAX_REORDERING_MPDU_NUM);
377 #endif // DOT11_N_SUPPORT //
379 // Make sure MAC gets ready.
383 RTMP_IO_READ32(pAd, MAC_CSR0, &MacCsr0);
384 pAd->MACVersion = MacCsr0;
386 if ((pAd->MACVersion != 0x00) && (pAd->MACVersion != 0xFFFFFFFF))
390 } while (index++ < 100);
392 DBGPRINT(RT_DEBUG_TRACE, ("MAC_CSR0 [ Ver:Rev=0x%08x]\n", pAd->MACVersion));
395 RT28XXDMADisable(pAd);
398 // Load 8051 firmware
399 Status = NICLoadFirmware(pAd);
400 if (Status != NDIS_STATUS_SUCCESS)
402 DBGPRINT_ERR(("NICLoadFirmware failed, Status[=0x%08x]\n", Status));
406 NICLoadRateSwitchingParams(pAd);
408 // Disable interrupts here which is as soon as possible
409 // This statement should never be true. We might consider to remove it later
410 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_ACTIVE))
412 NICDisableInterrupt(pAd);
415 Status = RTMPAllocTxRxRingMemory(pAd);
416 if (Status != NDIS_STATUS_SUCCESS)
418 DBGPRINT_ERR(("RTMPAllocDMAMemory failed, Status[=0x%08x]\n", Status));
422 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE);
427 Status = MlmeInit(pAd);
428 if (Status != NDIS_STATUS_SUCCESS)
430 DBGPRINT_ERR(("MlmeInit failed, Status[=0x%08x]\n", Status));
434 // Initialize pAd->StaCfg, pAd->ApCfg, pAd->CommonCfg to manufacture default
439 RT28XX_TASK_THREAD_INIT(pAd, Status);
440 if (Status != NDIS_STATUS_SUCCESS)
447 initblockQueueTab(pAd);
448 #endif // BLOCK_NET_IF //
450 #ifdef CONFIG_STA_SUPPORT
451 IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
452 NdisAllocateSpinLock(&pAd->MacTabLock);
453 #endif // CONFIG_STA_SUPPORT //
455 MeasureReqTabInit(pAd);
459 // Init the hardware, we need to init asic before read registry, otherwise mac register will be reset
461 Status = NICInitializeAdapter(pAd, TRUE);
462 if (Status != NDIS_STATUS_SUCCESS)
464 DBGPRINT_ERR(("NICInitializeAdapter failed, Status[=0x%08x]\n", Status));
465 if (Status != NDIS_STATUS_SUCCESS)
469 // Read parameters from Config File
470 Status = RTMPReadParametersHook(pAd);
472 printk("1. Phy Mode = %d\n", pAd->CommonCfg.PhyMode);
473 if (Status != NDIS_STATUS_SUCCESS)
475 DBGPRINT_ERR(("NICReadRegParameters failed, Status[=0x%08x]\n",Status));
481 //Init Ba Capability parameters.
482 #ifdef DOT11_N_SUPPORT
483 pAd->CommonCfg.DesiredHtPhy.MpduDensity = (UCHAR)pAd->CommonCfg.BACapability.field.MpduDensity;
484 pAd->CommonCfg.DesiredHtPhy.AmsduEnable = (USHORT)pAd->CommonCfg.BACapability.field.AmsduEnable;
485 pAd->CommonCfg.DesiredHtPhy.AmsduSize = (USHORT)pAd->CommonCfg.BACapability.field.AmsduSize;
486 pAd->CommonCfg.DesiredHtPhy.MimoPs = (USHORT)pAd->CommonCfg.BACapability.field.MMPSmode;
488 pAd->CommonCfg.HtCapability.HtCapInfo.MimoPs = (USHORT)pAd->CommonCfg.BACapability.field.MMPSmode;
489 pAd->CommonCfg.HtCapability.HtCapInfo.AMsduSize = (USHORT)pAd->CommonCfg.BACapability.field.AmsduSize;
490 pAd->CommonCfg.HtCapability.HtCapParm.MpduDensity = (UCHAR)pAd->CommonCfg.BACapability.field.MpduDensity;
491 #endif // DOT11_N_SUPPORT //
493 printk("2. Phy Mode = %d\n", pAd->CommonCfg.PhyMode);
495 // We should read EEPROM for all cases. rt2860b
496 NICReadEEPROMParameters(pAd, mac);
498 printk("3. Phy Mode = %d\n", pAd->CommonCfg.PhyMode);
500 NICInitAsicFromEEPROM(pAd); //rt2860b
502 // Set PHY to appropriate mode
503 TmpPhy = pAd->CommonCfg.PhyMode;
504 pAd->CommonCfg.PhyMode = 0xff;
505 RTMPSetPhyMode(pAd, TmpPhy);
506 #ifdef DOT11_N_SUPPORT
508 #endif // DOT11_N_SUPPORT //
510 // No valid channels.
511 if (pAd->ChannelListNum == 0)
513 printk("Wrong configuration. No valid channel found. Check \"ContryCode\" and \"ChannelGeography\" setting.\n");
517 #ifdef DOT11_N_SUPPORT
518 printk("MCS Set = %02x %02x %02x %02x %02x\n", pAd->CommonCfg.HtCapability.MCSSet[0],
519 pAd->CommonCfg.HtCapability.MCSSet[1], pAd->CommonCfg.HtCapability.MCSSet[2],
520 pAd->CommonCfg.HtCapability.MCSSet[3], pAd->CommonCfg.HtCapability.MCSSet[4]);
521 #endif // DOT11_N_SUPPORT //
524 VR_IKANOS_FP_Init(pAd->ApCfg.BssidNum, pAd->PermanentAddress);
525 #endif // IKANOS_VX_1X0 //
528 // Initialize RF register to default value
530 AsicSwitchChannel(pAd, pAd->CommonCfg.Channel, FALSE);
531 AsicLockChannel(pAd, pAd->CommonCfg.Channel);
533 // 8051 firmware require the signal during booting time.
534 AsicSendCommandToMcu(pAd, 0x72, 0xFF, 0x00, 0x00);
536 if (pAd && (Status != NDIS_STATUS_SUCCESS))
539 // Undo everything if it failed
541 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE))
543 RTMP_CLEAR_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE);
548 // Microsoft HCT require driver send a disconnect event after driver initialization.
549 OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_MEDIA_STATE_CONNECTED);
550 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_MEDIA_STATE_CHANGE);
551 DBGPRINT(RT_DEBUG_TRACE, ("NDIS_STATUS_MEDIA_DISCONNECT Event B!\n"));
557 DBGPRINT_S(Status, ("<==== RTMPInitialize, Status=%x\n", Status));
566 RTMPFreeTxRxRingMemory(pAd);
569 #ifdef DOT11_N_SUPPORT
570 os_free_mem(pAd, pAd->mpdu_blk_pool.mem); // free BA pool
571 #endif // DOT11_N_SUPPORT //
572 RT28XX_IRQ_RELEASE(net_dev);
574 // shall not set ml_priv to NULL here because the ml_priv didn't been free yet.
575 //net_dev->ml_priv = 0;
578 #endif // INF_AMAZON_SE //
579 printk("!!! %s Initialized fail !!!\n", RT28xx_CHIP_NAME);
581 } /* End of rt28xx_init */
585 ========================================================================
590 *net_dev the raxx interface pointer
597 ========================================================================
599 int rt28xx_open(IN PNET_DEV dev)
601 struct net_device * net_dev = (struct net_device *)dev;
602 PRTMP_ADAPTER pAd = net_dev->ml_priv;
607 // Sanity check for pAd
610 /* if 1st open fail, pAd will be free;
611 So the net_dev->ml_priv will be NULL in 2rd open */
616 pObj = (POS_COOKIE)pAd->OS_Cookie;
618 // reset Adapter flags
619 RTMP_CLEAR_FLAGS(pAd);
621 // Request interrupt service routine for PCI device
622 // register the interrupt routine with the os
623 RT28XX_IRQ_REQUEST(net_dev);
626 // Init BssTab & ChannelInfo tabbles for auto channel select.
630 if (rt28xx_init(net_dev) == FALSE)
633 #ifdef CONFIG_STA_SUPPORT
634 IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
636 NdisZeroMemory(pAd->StaCfg.dev_name, 16);
637 NdisMoveMemory(pAd->StaCfg.dev_name, net_dev->name, strlen(net_dev->name));
639 #endif // CONFIG_STA_SUPPORT //
641 // Set up the Mac address
642 NdisMoveMemory(net_dev->dev_addr, (void *) pAd->CurrentAddress, 6);
644 // Init IRQ parameters
645 RT28XX_IRQ_INIT(pAd);
647 // Various AP function init
649 #ifdef CONFIG_STA_SUPPORT
650 IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
652 #ifdef WPA_SUPPLICANT_SUPPORT
653 #ifndef NATIVE_WPA_SUPPLICANT_SUPPORT
655 union iwreq_data wrqu;
656 // send wireless event to wpa_supplicant for infroming interface down.
657 memset(&wrqu, 0, sizeof(wrqu));
658 wrqu.data.flags = RT_INTERFACE_UP;
659 wireless_send_event(pAd->net_dev, IWEVCUSTOM, &wrqu, NULL);
661 #endif // NATIVE_WPA_SUPPLICANT_SUPPORT //
662 #endif // WPA_SUPPLICANT_SUPPORT //
665 #endif // CONFIG_STA_SUPPORT //
668 RT28XX_IRQ_ENABLE(pAd);
672 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_START_UP);
676 RTMP_IO_READ32(pAd, 0x1300, ®); // clear garbage interrupts
677 printk("0x1300 = %08x\n", reg);
680 #ifdef CONFIG_STA_SUPPORT
681 IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
682 RTMPInitPCIeLinkCtrlValue(pAd);
683 #endif // CONFIG_STA_SUPPORT //
689 } /* End of rt28xx_open */
691 static const struct net_device_ops rt2860_netdev_ops = {
692 .ndo_open = MainVirtualIF_open,
693 .ndo_stop = MainVirtualIF_close,
694 .ndo_do_ioctl = rt28xx_ioctl,
695 .ndo_get_stats = RT28xx_get_ether_stats,
696 .ndo_validate_addr = NULL,
697 .ndo_set_mac_address = eth_mac_addr,
698 .ndo_change_mtu = eth_change_mtu,
700 .ndo_start_xmit = IKANOS_DataFramesTx,
702 .ndo_start_xmit = rt28xx_send_packets,
706 /* Must not be called for mdev and apdev */
707 static NDIS_STATUS rt_ieee80211_if_setup(struct net_device *dev, PRTMP_ADAPTER pAd)
711 CHAR slot_name[IFNAMSIZ];
712 struct net_device *device;
717 #ifdef CONFIG_STA_SUPPORT
718 #if WIRELESS_EXT >= 12
719 if (pAd->OpMode == OPMODE_STA)
721 dev->wireless_handlers = &rt28xx_iw_handler_def;
723 #endif //WIRELESS_EXT >= 12
724 #endif // CONFIG_STA_SUPPORT //
726 #if WIRELESS_EXT < 21
727 dev->get_wireless_stats = rt28xx_get_wireless_stats;
729 dev->priv_flags = INT_MAIN;
730 dev->netdev_ops = &rt2860_netdev_ops;
731 // find available device name
732 for (i = 0; i < 8; i++)
734 #ifdef MULTIPLE_CARD_SUPPORT
735 if (pAd->MC_RowID >= 0)
736 sprintf(slot_name, "ra%02d_%d", pAd->MC_RowID, i);
738 #endif // MULTIPLE_CARD_SUPPORT //
739 sprintf(slot_name, "ra%d", i);
741 device = dev_get_by_name(dev_net(dev), slot_name);
751 DBGPRINT(RT_DEBUG_ERROR, ("No available slot name\n"));
752 Status = NDIS_STATUS_FAILURE;
756 #ifdef MULTIPLE_CARD_SUPPORT
757 if (pAd->MC_RowID >= 0)
758 sprintf(dev->name, "ra%02d_%d", pAd->MC_RowID, i);
760 #endif // MULTIPLE_CARD_SUPPORT //
761 sprintf(dev->name, "ra%d", i);
762 Status = NDIS_STATUS_SUCCESS;
770 #ifdef MULTIPLE_CARD_SUPPORT
772 ========================================================================
774 Get card profile path.
780 TRUE - Find a card profile
781 FALSE - use default profile
784 ========================================================================
786 extern INT RTMPGetKeyParameter(
792 BOOLEAN RTMP_CardInfoRead(
793 IN PRTMP_ADAPTER pAd)
795 #define MC_SELECT_CARDID 0 /* use CARD ID (0 ~ 31) to identify different cards */
796 #define MC_SELECT_MAC 1 /* use CARD MAC to identify different cards */
797 #define MC_SELECT_CARDTYPE 2 /* use CARD type (abgn or bgn) to identify different cards */
799 #define LETTER_CASE_TRANSLATE(txt_p, card_id) \
800 { UINT32 _len; char _char; \
801 for(_len=0; _len<strlen(card_id); _len++) { \
802 _char = *(txt_p + _len); \
803 if (('A' <= _char) && (_char <= 'Z')) \
804 *(txt_p+_len) = 'a'+(_char-'A'); \
808 INT retval, orgfsuid, orgfsgid;
810 CHAR *buffer, *tmpbuf, card_id_buf[30], RFIC_word[30];
811 BOOLEAN flg_match_ok = FALSE;
812 INT32 card_select_method;
813 INT32 card_free_id, card_nouse_id, card_same_mac_id, card_match_id;
814 EEPROM_ANTENNA_STRUC antenna;
815 USHORT addr01, addr23, addr45;
817 UINT32 data, card_index;
822 buffer = kmalloc(MAX_INI_BUFFER_SIZE, MEM_ALLOC_FLAG);
826 tmpbuf = kmalloc(MAX_PARAM_BUFFER_SIZE, MEM_ALLOC_FLAG);
830 return NDIS_STATUS_FAILURE;
833 orgfsuid = current->fsuid;
834 orgfsgid = current->fsgid;
835 current->fsuid = current->fsgid = 0;
840 RTMP_IO_READ32(pAd, E2PROM_CSR, &data);
842 if ((data & 0x30) == 0)
843 pAd->EEPROMAddressNum = 6; // 93C46
844 else if ((data & 0x30) == 0x10)
845 pAd->EEPROMAddressNum = 8; // 93C66
847 pAd->EEPROMAddressNum = 8; // 93C86
849 RT28xx_EEPROM_READ16(pAd, EEPROM_NIC1_OFFSET, antenna.word);
851 if ((antenna.field.RfIcType == RFIC_2850) ||
852 (antenna.field.RfIcType == RFIC_2750))
855 strcpy(RFIC_word, "abgn");
860 strcpy(RFIC_word, "bgn");
864 RT28xx_EEPROM_READ16(pAd, 0x04, addr01);
865 RT28xx_EEPROM_READ16(pAd, 0x06, addr23);
866 RT28xx_EEPROM_READ16(pAd, 0x08, addr45);
868 mac[0] = (UCHAR)(addr01 & 0xff);
869 mac[1] = (UCHAR)(addr01 >> 8);
870 mac[2] = (UCHAR)(addr23 & 0xff);
871 mac[3] = (UCHAR)(addr23 >> 8);
872 mac[4] = (UCHAR)(addr45 & 0xff);
873 mac[5] = (UCHAR)(addr45 >> 8);
875 // open card information file
876 srcf = filp_open(CARD_INFO_PATH, O_RDONLY, 0);
879 /* card information file does not exist */
880 DBGPRINT(RT_DEBUG_TRACE,
881 ("--> Error %ld opening %s\n", -PTR_ERR(srcf), CARD_INFO_PATH));
885 if (srcf->f_op && srcf->f_op->read)
887 /* card information file exists so reading the card information */
888 memset(buffer, 0x00, MAX_INI_BUFFER_SIZE);
889 retval = srcf->f_op->read(srcf, buffer, MAX_INI_BUFFER_SIZE, &srcf->f_pos);
893 DBGPRINT(RT_DEBUG_TRACE,
894 ("--> Read %s error %d\n", CARD_INFO_PATH, -retval));
898 /* get card selection method */
899 memset(tmpbuf, 0x00, MAX_PARAM_BUFFER_SIZE);
900 card_select_method = MC_SELECT_CARDTYPE; // default
902 if (RTMPGetKeyParameter("SELECT", tmpbuf, 256, buffer))
904 if (strcmp(tmpbuf, "CARDID") == 0)
905 card_select_method = MC_SELECT_CARDID;
906 else if (strcmp(tmpbuf, "MAC") == 0)
907 card_select_method = MC_SELECT_MAC;
908 else if (strcmp(tmpbuf, "CARDTYPE") == 0)
909 card_select_method = MC_SELECT_CARDTYPE;
912 DBGPRINT(RT_DEBUG_TRACE,
913 ("MC> Card Selection = %d\n", card_select_method));
918 card_same_mac_id = -1;
921 // search current card information records
923 card_index<MAX_NUM_OF_MULTIPLE_CARD;
926 if ((*(UINT32 *)&MC_CardMac[card_index][0] == 0) &&
927 (*(UINT16 *)&MC_CardMac[card_index][4] == 0))
929 // MAC is all-0 so the entry is available
930 MC_CardUsed[card_index] = 0;
932 if (card_free_id < 0)
933 card_free_id = card_index; // 1st free entry
937 if (memcmp(MC_CardMac[card_index], mac, 6) == 0)
939 // we find the entry with same MAC
940 if (card_same_mac_id < 0)
941 card_same_mac_id = card_index; // 1st same entry
945 // MAC is not all-0 but used flag == 0
946 if ((MC_CardUsed[card_index] == 0) &&
949 card_nouse_id = card_index; // 1st available entry
955 DBGPRINT(RT_DEBUG_TRACE,
956 ("MC> Free = %d, Same = %d, NOUSE = %d\n",
957 card_free_id, card_same_mac_id, card_nouse_id));
959 if ((card_same_mac_id >= 0) &&
960 ((card_select_method == MC_SELECT_CARDID) ||
961 (card_select_method == MC_SELECT_CARDTYPE)))
963 // same MAC entry is found
964 card_match_id = card_same_mac_id;
966 if (card_select_method == MC_SELECT_CARDTYPE)
969 sprintf(card_id_buf, "%02dCARDTYPE%s",
970 card_match_id, RFIC_word);
972 if ((start_ptr=rtstrstruncasecmp(buffer, card_id_buf)) != NULL)
974 // we found the card ID
975 LETTER_CASE_TRANSLATE(start_ptr, card_id_buf);
981 // the card is 1st plug-in, try to find the match card profile
982 switch(card_select_method)
984 case MC_SELECT_CARDID: // CARDID
986 if (card_free_id >= 0)
987 card_match_id = card_free_id;
989 card_match_id = card_nouse_id;
992 case MC_SELECT_MAC: // MAC
993 sprintf(card_id_buf, "MAC%02x:%02x:%02x:%02x:%02x:%02x",
994 mac[0], mac[1], mac[2],
995 mac[3], mac[4], mac[5]);
997 /* try to find the key word in the card file */
998 if ((start_ptr=rtstrstruncasecmp(buffer, card_id_buf)) != NULL)
1000 LETTER_CASE_TRANSLATE(start_ptr, card_id_buf);
1002 /* get the row ID (2 ASCII characters) */
1004 card_id_buf[0] = *(start_ptr);
1005 card_id_buf[1] = *(start_ptr+1);
1006 card_id_buf[2] = 0x00;
1008 card_match_id = simple_strtol(card_id_buf, 0, 10);
1012 case MC_SELECT_CARDTYPE: // CARDTYPE
1016 card_index<MAX_NUM_OF_MULTIPLE_CARD;
1019 sprintf(card_id_buf, "%02dCARDTYPE%s",
1020 card_index, RFIC_word);
1022 if ((start_ptr=rtstrstruncasecmp(buffer,
1023 card_id_buf)) != NULL)
1025 LETTER_CASE_TRANSLATE(start_ptr, card_id_buf);
1027 if (MC_CardUsed[card_index] == 0)
1029 /* current the card profile is not used */
1030 if ((*(UINT32 *)&MC_CardMac[card_index][0] == 0) &&
1031 (*(UINT16 *)&MC_CardMac[card_index][4] == 0))
1033 // find it and no previous card use it
1034 card_match_id = card_index;
1039 // ever a card use it
1040 if (card_nouse_id < 0)
1041 card_nouse_id = card_index;
1047 // if not find a free one, use the available one
1048 if (card_match_id < 0)
1049 card_match_id = card_nouse_id;
1054 if (card_match_id >= 0)
1056 // make up search keyword
1057 switch(card_select_method)
1059 case MC_SELECT_CARDID: // CARDID
1060 sprintf(card_id_buf, "%02dCARDID", card_match_id);
1063 case MC_SELECT_MAC: // MAC
1064 sprintf(card_id_buf,
1065 "%02dmac%02x:%02x:%02x:%02x:%02x:%02x",
1067 mac[0], mac[1], mac[2],
1068 mac[3], mac[4], mac[5]);
1071 case MC_SELECT_CARDTYPE: // CARDTYPE
1073 sprintf(card_id_buf, "%02dcardtype%s",
1074 card_match_id, RFIC_word);
1078 DBGPRINT(RT_DEBUG_TRACE, ("Search Keyword = %s\n", card_id_buf));
1080 // read card file path
1081 if (RTMPGetKeyParameter(card_id_buf, tmpbuf, 256, buffer))
1083 if (strlen(tmpbuf) < sizeof(pAd->MC_FileName))
1085 // backup card information
1086 pAd->MC_RowID = card_match_id; /* base 0 */
1087 MC_CardUsed[card_match_id] = 1;
1088 memcpy(MC_CardMac[card_match_id], mac, sizeof(mac));
1090 // backup card file path
1091 NdisMoveMemory(pAd->MC_FileName, tmpbuf , strlen(tmpbuf));
1092 pAd->MC_FileName[strlen(tmpbuf)] = '\0';
1093 flg_match_ok = TRUE;
1095 DBGPRINT(RT_DEBUG_TRACE,
1096 ("Card Profile Name = %s\n", pAd->MC_FileName));
1100 DBGPRINT(RT_DEBUG_ERROR,
1101 ("Card Profile Name length too large!\n"));
1106 DBGPRINT(RT_DEBUG_ERROR,
1107 ("Can not find search key word in card.dat!\n"));
1110 if ((flg_match_ok != TRUE) &&
1111 (card_match_id < MAX_NUM_OF_MULTIPLE_CARD))
1113 MC_CardUsed[card_match_id] = 0;
1114 memset(MC_CardMac[card_match_id], 0, sizeof(mac));
1116 } // if (card_match_id >= 0)
1121 retval = filp_close(srcf, NULL);
1123 current->fsuid = orgfsuid;
1124 current->fsgid = orgfsgid;
1127 return flg_match_ok;
1129 #endif // MULTIPLE_CARD_SUPPORT //
1133 ========================================================================
1134 Routine Description:
1135 Probe RT28XX chipset.
1138 _dev_p Point to the PCI or USB device
1139 _dev_id_p Point to the PCI or USB device ID
1146 ========================================================================
1148 INT __devinit rt28xx_probe(
1152 OUT PRTMP_ADAPTER *ppAd)
1154 struct net_device *net_dev;
1155 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER) NULL;
1158 struct pci_dev *dev_p = (struct pci_dev *)_dev_p;
1161 #ifdef CONFIG_STA_SUPPORT
1162 DBGPRINT(RT_DEBUG_TRACE, ("STA Driver version-%s\n", STA_DRIVER_VERSION));
1163 #endif // CONFIG_STA_SUPPORT //
1165 net_dev = alloc_etherdev(sizeof(PRTMP_ADAPTER));
1166 if (net_dev == NULL)
1168 printk("alloc_netdev failed\n");
1173 netif_stop_queue(net_dev);
1174 #ifdef NATIVE_WPA_SUPPLICANT_SUPPORT
1175 /* for supporting Network Manager */
1176 /* Set the sysfs physical device reference for the network logical device
1177 * if set prior to registration will cause a symlink during initialization.
1179 SET_NETDEV_DEV(net_dev, &(dev_p->dev));
1180 #endif // NATIVE_WPA_SUPPLICANT_SUPPORT //
1182 // Allocate RTMP_ADAPTER miniport adapter structure
1183 handle = kmalloc(sizeof(struct os_cookie), GFP_KERNEL);
1184 RT28XX_HANDLE_DEV_ASSIGN(handle, dev_p);
1186 status = RTMPAllocAdapterBlock(handle, &pAd);
1187 if (status != NDIS_STATUS_SUCCESS)
1188 goto err_out_free_netdev;
1190 net_dev->ml_priv = (PVOID)pAd;
1191 pAd->net_dev = net_dev; // must be before RT28XXNetDevInit()
1193 RT28XXNetDevInit(_dev_p, net_dev, pAd);
1195 #ifdef CONFIG_STA_SUPPORT
1196 pAd->StaCfg.OriDevType = net_dev->type;
1197 #endif // CONFIG_STA_SUPPORT //
1200 if (RT28XXProbePostConfig(_dev_p, pAd, 0) == FALSE)
1203 #ifdef CONFIG_STA_SUPPORT
1204 pAd->OpMode = OPMODE_STA;
1205 #endif // CONFIG_STA_SUPPORT //
1208 #ifdef MULTIPLE_CARD_SUPPORT
1209 // find its profile path
1210 pAd->MC_RowID = -1; // use default profile path
1211 RTMP_CardInfoRead(pAd);
1213 if (pAd->MC_RowID == -1)
1214 #ifdef CONFIG_STA_SUPPORT
1215 strcpy(pAd->MC_FileName, STA_PROFILE_PATH);
1216 #endif // CONFIG_STA_SUPPORT //
1218 DBGPRINT(RT_DEBUG_TRACE,
1219 ("MC> ROW = %d, PATH = %s\n", pAd->MC_RowID, pAd->MC_FileName));
1220 #endif // MULTIPLE_CARD_SUPPORT //
1223 if (rt_ieee80211_if_setup(net_dev, pAd) != NDIS_STATUS_SUCCESS)
1226 // Register this device
1227 status = register_netdev(net_dev);
1232 RT28XX_DRVDATA_SET(_dev_p);
1235 return 0; // probe ok
1238 /* --------------------------- ERROR HANDLE --------------------------- */
1240 RTMPFreeAdapter(pAd);
1243 err_out_free_netdev:
1244 free_netdev(net_dev);
1247 RT28XX_PUT_DEVICE(dev_p);
1249 return -ENODEV; /* probe fail */
1250 } /* End of rt28xx_probe */
1254 ========================================================================
1255 Routine Description:
1256 The entry point for Linux kernel sent packet to our driver.
1259 sk_buff *skb the pointer refer to a sk_buffer.
1265 This function is the entry point of Tx Path for Os delivery packet to
1266 our driver. You only can put OS-depened & STA/AP common handle procedures
1268 ========================================================================
1270 int rt28xx_packet_xmit(struct sk_buff *skb)
1272 struct net_device *net_dev = skb->dev;
1273 PRTMP_ADAPTER pAd = net_dev->ml_priv;
1275 PNDIS_PACKET pPacket = (PNDIS_PACKET) skb;
1277 #ifdef CONFIG_STA_SUPPORT
1278 IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
1280 // Drop send request since we are in monitor mode
1281 if (MONITOR_ON(pAd))
1283 RELEASE_NDIS_PACKET(pAd, pPacket, NDIS_STATUS_FAILURE);
1287 #endif // CONFIG_STA_SUPPORT //
1289 // EapolStart size is 18
1292 //printk("bad packet size: %d\n", pkt->len);
1293 hex_dump("bad packet", skb->data, skb->len);
1294 RELEASE_NDIS_PACKET(pAd, pPacket, NDIS_STATUS_FAILURE);
1298 RTMP_SET_PACKET_5VT(pPacket, 0);
1299 #ifdef CONFIG_5VT_ENHANCE
1300 if (*(int*)(skb->cb) == BRIDGE_TAG) {
1301 RTMP_SET_PACKET_5VT(pPacket, 1);
1307 #ifdef CONFIG_STA_SUPPORT
1308 IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
1311 STASendPackets((NDIS_HANDLE)pAd, (PPNDIS_PACKET) &pPacket, 1);
1314 #endif // CONFIG_STA_SUPPORT //
1324 ========================================================================
1325 Routine Description:
1326 Send a packet to WLAN.
1329 skb_p points to our adapter
1330 dev_p which WLAN network interface
1333 0: transmit successfully
1334 otherwise: transmit fail
1337 ========================================================================
1339 INT rt28xx_send_packets(
1340 IN struct sk_buff *skb_p,
1341 IN struct net_device *net_dev)
1343 RTMP_ADAPTER *pAd = net_dev->ml_priv;
1344 if (!(net_dev->flags & IFF_UP))
1346 RELEASE_NDIS_PACKET(pAd, (PNDIS_PACKET)skb_p, NDIS_STATUS_FAILURE);
1350 NdisZeroMemory((PUCHAR)&skb_p->cb[CB_OFF], 15);
1351 RTMP_SET_PACKET_NET_DEVICE_MBSSID(skb_p, MAIN_MBSSID);
1353 return rt28xx_packet_xmit(skb_p);
1355 } /* End of MBSS_VirtualIF_PacketSend */
1360 void CfgInitHook(PRTMP_ADAPTER pAd)
1362 pAd->bBroadComHT = TRUE;
1363 } /* End of CfgInitHook */
1366 #if WIRELESS_EXT >= 12
1367 // This function will be called when query /proc
1368 struct iw_statistics *rt28xx_get_wireless_stats(
1369 IN struct net_device *net_dev)
1371 PRTMP_ADAPTER pAd = net_dev->ml_priv;
1374 DBGPRINT(RT_DEBUG_TRACE, ("rt28xx_get_wireless_stats --->\n"));
1376 pAd->iw_stats.status = 0; // Status - device dependent for now
1379 pAd->iw_stats.qual.qual = ((pAd->Mlme.ChannelQuality * 12)/10 + 10);
1380 if(pAd->iw_stats.qual.qual > 100)
1381 pAd->iw_stats.qual.qual = 100;
1383 #ifdef CONFIG_STA_SUPPORT
1384 if (pAd->OpMode == OPMODE_STA)
1385 pAd->iw_stats.qual.level = RTMPMaxRssi(pAd, pAd->StaCfg.RssiSample.LastRssi0, pAd->StaCfg.RssiSample.LastRssi1, pAd->StaCfg.RssiSample.LastRssi2);
1386 #endif // CONFIG_STA_SUPPORT //
1388 pAd->iw_stats.qual.noise = pAd->BbpWriteLatch[66]; // noise level (dBm)
1390 pAd->iw_stats.qual.noise += 256 - 143;
1391 pAd->iw_stats.qual.updated = 1; // Flags to know if updated
1393 pAd->iw_stats.qual.updated |= IW_QUAL_DBM; // Level + Noise are dBm
1394 #endif // IW_QUAL_DBM //
1396 pAd->iw_stats.discard.nwid = 0; // Rx : Wrong nwid/essid
1397 pAd->iw_stats.miss.beacon = 0; // Missed beacons/superframe
1399 DBGPRINT(RT_DEBUG_TRACE, ("<--- rt28xx_get_wireless_stats\n"));
1400 return &pAd->iw_stats;
1401 } /* End of rt28xx_get_wireless_stats */
1402 #endif // WIRELESS_EXT //
1406 void tbtt_tasklet(unsigned long data)
1408 #define MAX_TX_IN_TBTT (16)
1413 IN struct net_device *net_dev,
1414 IN OUT struct ifreq *rq,
1417 VIRTUAL_ADAPTER *pVirtualAd = NULL;
1418 RTMP_ADAPTER *pAd = NULL;
1421 if (net_dev->priv_flags == INT_MAIN)
1423 pAd = net_dev->ml_priv;
1427 pVirtualAd = net_dev->ml_priv;
1428 pAd = pVirtualAd->RtmpDev->ml_priv;
1433 /* if 1st open fail, pAd will be free;
1434 So the net_dev->ml_priv will be NULL in 2rd open */
1439 #ifdef CONFIG_STA_SUPPORT
1440 IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
1442 ret = rt28xx_sta_ioctl(net_dev, rq, cmd);
1444 #endif // CONFIG_STA_SUPPORT //
1450 ========================================================================
1452 Routine Description:
1453 return ethernet statistics counter
1456 net_dev Pointer to net_device
1463 ========================================================================
1465 struct net_device_stats *RT28xx_get_ether_stats(
1466 IN struct net_device *net_dev)
1468 RTMP_ADAPTER *pAd = NULL;
1471 pAd = net_dev->ml_priv;
1476 pAd->stats.rx_packets = pAd->WlanCounters.ReceivedFragmentCount.QuadPart;
1477 pAd->stats.tx_packets = pAd->WlanCounters.TransmittedFragmentCount.QuadPart;
1479 pAd->stats.rx_bytes = pAd->RalinkCounters.ReceivedByteCount;
1480 pAd->stats.tx_bytes = pAd->RalinkCounters.TransmittedByteCount;
1482 pAd->stats.rx_errors = pAd->Counters8023.RxErrors;
1483 pAd->stats.tx_errors = pAd->Counters8023.TxErrors;
1485 pAd->stats.rx_dropped = 0;
1486 pAd->stats.tx_dropped = 0;
1488 pAd->stats.multicast = pAd->WlanCounters.MulticastReceivedFrameCount.QuadPart; // multicast packets received
1489 pAd->stats.collisions = pAd->Counters8023.OneCollision + pAd->Counters8023.MoreCollisions; // Collision packets
1491 pAd->stats.rx_length_errors = 0;
1492 pAd->stats.rx_over_errors = pAd->Counters8023.RxNoBuffer; // receiver ring buff overflow
1493 pAd->stats.rx_crc_errors = 0;//pAd->WlanCounters.FCSErrorCount; // recved pkt with crc error
1494 pAd->stats.rx_frame_errors = pAd->Counters8023.RcvAlignmentErrors; // recv'd frame alignment error
1495 pAd->stats.rx_fifo_errors = pAd->Counters8023.RxNoBuffer; // recv'r fifo overrun
1496 pAd->stats.rx_missed_errors = 0; // receiver missed packet
1498 // detailed tx_errors
1499 pAd->stats.tx_aborted_errors = 0;
1500 pAd->stats.tx_carrier_errors = 0;
1501 pAd->stats.tx_fifo_errors = 0;
1502 pAd->stats.tx_heartbeat_errors = 0;
1503 pAd->stats.tx_window_errors = 0;
1506 pAd->stats.rx_compressed = 0;
1507 pAd->stats.tx_compressed = 0;