📄 rtusb_main.c
字号:
NDIS_802_11_NETWORK_TYPE NetType = *(PNDIS_802_11_NETWORK_TYPE)(cmdqelmt->buffer); if (NetType == Ndis802_11DS) RTMPSetPhyMode(pAdapter, PHY_11B); else if (NetType == Ndis802_11OFDM24) RTMPSetPhyMode(pAdapter, PHY_11BG_MIXED); else if (NetType == Ndis802_11OFDM5) RTMPSetPhyMode(pAdapter, PHY_11A); DBGPRINT(RT_DEBUG_ERROR, "Set::OID_802_11_NETWORK_TYPE_IN_USE (=%d)\n",NetType); } break; case RT_OID_802_11_PHY_MODE: { ULONG phymode = *(ULONG *)(cmdqelmt->buffer); RTMPSetPhyMode(pAdapter, phymode); DBGPRINT(RT_DEBUG_ERROR, "Set::RT_OID_802_11_SET_PHY_MODE (=%d)\n", phymode); } break; case OID_802_11_WEP_STATUS: { USHORT Value; NDIS_802_11_WEP_STATUS WepStatus = *(PNDIS_802_11_WEP_STATUS)pData; if (pAdapter->PortCfg.WepStatus != WepStatus) { // Config has changed pAdapter->bConfigChanged = TRUE; pAdapter->PortCfg.WepStatus = WepStatus; pAdapter->PortCfg.OrigWepStatus = WepStatus; pAdapter->PortCfg.PairCipher = WepStatus; pAdapter->PortCfg.GroupCipher = WepStatus; } if ((WepStatus == Ndis802_11Encryption1Enabled) && (pAdapter->PortCfg.SharedKey[pAdapter->PortCfg.DefaultKeyId].KeyLen != 0)) { if (pAdapter->PortCfg.SharedKey[pAdapter->PortCfg.DefaultKeyId].KeyLen <= 5) pAdapter->PortCfg.CipherAlg = CIPHER_WEP64; else pAdapter->PortCfg.CipherAlg = CIPHER_WEP128; RTUSBReadMACRegister(pAdapter, TXRX_CSR0, &Value); Value &= 0xfe00; Value |= ((LENGTH_802_11 << 3) | (pAdapter->PortCfg.CipherAlg)); RTUSBWriteMACRegister(pAdapter, TXRX_CSR0, Value); } else if (WepStatus == Ndis802_11Encryption2Enabled) { pAdapter->PortCfg.CipherAlg = CIPHER_TKIP; RTUSBReadMACRegister(pAdapter, TXRX_CSR0, &Value); Value &= 0xfe00; Value |= ((LENGTH_802_11 << 3) | (pAdapter->PortCfg.CipherAlg)); RTUSBWriteMACRegister(pAdapter, TXRX_CSR0, Value); } else if (WepStatus == Ndis802_11Encryption3Enabled) { pAdapter->PortCfg.CipherAlg = CIPHER_AES; RTUSBReadMACRegister(pAdapter, TXRX_CSR0, &Value); Value &= 0xfe00; Value |= ((LENGTH_802_11 << 3) | (pAdapter->PortCfg.CipherAlg)); RTUSBWriteMACRegister(pAdapter, TXRX_CSR0, Value); } else if (WepStatus == Ndis802_11EncryptionDisabled) { pAdapter->PortCfg.CipherAlg = CIPHER_NONE; RTUSBReadMACRegister(pAdapter, TXRX_CSR0, &Value); Value &= 0xfe00; RTUSBWriteMACRegister(pAdapter, TXRX_CSR0, Value); } DBGPRINT(RT_DEBUG_TRACE, "OID_802_11_WEP_STATUS::TXRX_CSR0 (value=0x%x)\n", Value); } break; case OID_802_11_ADD_WEP: { ULONG KeyIdx; TXRX_CSR0_STRUC TxRxCsr0; PNDIS_802_11_WEP pWepKey = (PNDIS_802_11_WEP)pData; KeyIdx = pWepKey->KeyIndex & 0x0fffffff; if (KeyIdx <= 4) { pAdapter->PortCfg.SharedKey[KeyIdx].KeyLen = (UCHAR)pWepKey->KeyLength; memset(pAdapter->PortCfg.SharedKey[KeyIdx].Key, 0, MAX_LEN_OF_KEY); memcpy(pAdapter->PortCfg.SharedKey[KeyIdx].Key, &pWepKey->KeyMaterial, pWepKey->KeyLength); RTUSBMultiWriteMAC(pAdapter, (USHORT)(SEC_CSR0 + KeyIdx * 0x10), pAdapter->PortCfg.SharedKey[KeyIdx].Key, 16);//steven:always write 16 bytes. if (pWepKey->KeyIndex & 0x80000000) { // Default key for tx (shared key) pAdapter->PortCfg.DefaultKeyId = (UCHAR)KeyIdx; if (pWepKey->KeyLength <= 5) pAdapter->PortCfg.CipherAlg = CIPHER_WEP64; else pAdapter->PortCfg.CipherAlg = CIPHER_WEP128; RTUSBReadMACRegister(pAdapter, TXRX_CSR0, &TxRxCsr0.value); TxRxCsr0.value &= 0xfe00; TxRxCsr0.field.IVOffset = LENGTH_802_11; TxRxCsr0.field.Algorithm = pAdapter->PortCfg.CipherAlg; TxRxCsr0.field.KeyID |= (0x01 << KeyIdx); DBGPRINT(RT_DEBUG_TRACE, "Set::TXRX_CSR0 (value=0x%x)\n", TxRxCsr0.value); RTUSBWriteMACRegister(pAdapter, TXRX_CSR0, TxRxCsr0.value); } } DBGPRINT(RT_DEBUG_TRACE, "Set::OID_802_11_ADD_WEP (id=%d, Len=%d-byte)\n", KeyIdx, pWepKey->KeyLength); } break; case OID_802_11_ADD_KEY_WEP: { PNDIS_802_11_KEY pKey = (PNDIS_802_11_KEY)pData; TXRX_CSR0_STRUC TxRxCsr0; ULONG KeyIdx = pKey->KeyIndex & 0x0fffffff; // it is a shared key if (KeyIdx <= 4) { pAdapter->PortCfg.SharedKey[KeyIdx].KeyLen = (UCHAR)pKey->KeyLength; memset(pAdapter->PortCfg.SharedKey[KeyIdx].Key, 0, MAX_LEN_OF_KEY); memcpy(pAdapter->PortCfg.SharedKey[KeyIdx].Key, &pKey->KeyMaterial, pKey->KeyLength); RTUSBMultiWriteMAC(pAdapter, (USHORT)(SEC_CSR0 + KeyIdx * 0x10), pAdapter->PortCfg.SharedKey[KeyIdx].Key, 16); if (pKey->KeyIndex & 0x80000000) { // Default key for tx (shared key) pAdapter->PortCfg.DefaultKeyId = (UCHAR)KeyIdx; if (pKey->KeyLength <= 5) pAdapter->PortCfg.CipherAlg = CIPHER_WEP64; else pAdapter->PortCfg.CipherAlg = CIPHER_WEP128; RTUSBReadMACRegister(pAdapter, TXRX_CSR0, &TxRxCsr0.value); TxRxCsr0.value &= 0xfe00; TxRxCsr0.field.IVOffset = LENGTH_802_11; TxRxCsr0.field.Algorithm = pAdapter->PortCfg.CipherAlg; TxRxCsr0.field.KeyID |= (0x01 << KeyIdx); RTUSBWriteMACRegister(pAdapter, TXRX_CSR0, TxRxCsr0.value); DBGPRINT(RT_DEBUG_TRACE, "Set::OID_802_11_ADD_WEP TXRX_CSR0=(0x%08x)\n",TxRxCsr0.value); } } } break; case OID_802_11_ADD_KEY: DBGPRINT(RT_DEBUG_TRACE, "Set::OID_802_11_ADD_KEY \n"); { int i = 0; DBGPRINT(RT_DEBUG_INFO,"pKey->KeyMaterial = "); for (i = 0; i < 16; i++) { DBGPRINT_RAW(RT_DEBUG_INFO,"%02x:", ((PNDIS_802_11_KEY)pData)->KeyMaterial[i]); } DBGPRINT_RAW(RT_DEBUG_INFO,"\n"); } NdisStatus = RTMPWPAAddKeyProc(pAdapter, pData); RTUSBBulkReceive(pAdapter); break; case OID_802_11_REMOVE_KEY: NdisStatus = RTMPWPARemoveKeyProc(pAdapter, pData); break; case RT_OID_REMOVE_ALLKEYS: RTMPWPARemoveAllKeys(pAdapter); break;#if 0 case RT_OID_802_11_QUERY_HARDWARE_REGISTER: NdisStatus = RTUSBQueryHardWareRegister(pAdapter, pData); break; case RT_OID_802_11_SET_HARDWARE_REGISTER: NdisStatus = RTMPSetHardWareRegister(pAdapter, pData); break;#endif default: break; } if (cmdqelmt->CmdFromNdis == TRUE) { if ((cmdqelmt->command != OID_802_11_BSSID_LIST_SCAN) && (cmdqelmt->command != RT_OID_802_11_BSSID) && (cmdqelmt->command != OID_802_11_SSID) && (cmdqelmt->command != OID_802_11_DISASSOCIATE)) { } if ((cmdqelmt->command != RT_OID_SINGLE_READ_MAC) && (cmdqelmt->command != RT_OID_MULTI_READ_MAC) && (cmdqelmt->command != RT_OID_VENDOR_READ_BBP) &&#ifdef DBG (cmdqelmt->command != RT_OID_802_11_QUERY_HARDWARE_REGISTER) &&#endif (cmdqelmt->command != RT_OID_USB_VENDOR_EEPROM_READ)) { if (cmdqelmt->buffer != NULL) kfree(cmdqelmt->buffer); } kfree((PCmdQElmt)cmdqelmt); } else cmdqelmt->InUse = FALSE; }}static int usb_rtusb_open(struct net_device *net_dev){ PRT2570ADAPTER pAdapter = (PRT2570ADAPTER) net_dev->priv; NDIS_STATUS Status = NDIS_STATUS_SUCCESS; NDIS_802_11_WEP WepKey; printk("RT25usb Driver version %x.%x.%x\n", DRV_MAJORVERSION,DRV_MINORVERSION, DRV_SUBVERSION);#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0) if (!try_module_get(THIS_MODULE)) { DBGPRINT(RT_DEBUG_ERROR, "%s: cannot reserve module\n", __FUNCTION__); return -1; }#else MOD_INC_USE_COUNT;#endif init_MUTEX(&(pAdapter->usbdev_semaphore)); // init mediastate to disconnected pAdapter->MediaState = NdisMediaStateDisconnected; pAdapter->rx_bh.func = RTUSBRxPacket; // Init RTMP_ADAPTER CmdQElements Status = RT2570InitAdapterBlock(pAdapter); if (Status != NDIS_STATUS_SUCCESS) { return Status; } //NdisAllocateSpinLock // Init send data structures and related parameters // Status = NICInitTransmit(pAdapter); if (Status != NDIS_STATUS_SUCCESS) { return Status; } // // Init receive data structures and related parameters // Status = NICInitRecv(pAdapter); if (Status != NDIS_STATUS_SUCCESS) { goto out; } RT2570InitializeAsic(pAdapter); // RTMPReadParametersFromFile(pAdapter); // Init the hardware and set up everything // NICReadEEPROMParameters(pAdapter); NICInitAsicFromEEPROM(pAdapter); RTUSBWriteHWMACAddress(pAdapter); // initialize MLME Status = MlmeInit(pAdapter); if(Status != NDIS_STATUS_SUCCESS) { goto out; } // mlmethread flag restart#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) mlme_kill = 0; RTUSBCmd_kill =0;#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) */ CreateThreads(net_dev); // at every open handler, copy mac address. memcpy(pAdapter->net->dev_addr, pAdapter->CurrentAddress, pAdapter->net->addr_len); // Clear Reset Flag before starting receiving/transmitting RTMP_CLEAR_FLAG(pAdapter, fRTMP_ADAPTER_RESET_IN_PROGRESS); if (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RADIO_OFF)) { RTUSBBulkReceive(pAdapter); RTUSBWriteMACRegister(pAdapter, TXRX_CSR2, 0x7e); } // start as if the link is up //netif_device_attach (pAdapter->net); // Start net interface tx /rx netif_start_queue(net_dev); netif_carrier_on(net_dev); netif_wake_queue(net_dev); if (pAdapter->PortCfg.WepStatus != Ndis802_11WEPDisabled) { memcpy(WepKey.KeyMaterial, pAdapter->PortCfg.SharedKey[pAdapter->PortCfg.DefaultKeyId].Key, pAdapter->PortCfg.SharedKey[pAdapter->PortCfg.DefaultKeyId].KeyLen); WepKey.KeyIndex = 0x80000000 + pAdapter->PortCfg.DefaultKeyId; WepKey.KeyLength = pAdapter->PortCfg.SharedKey[pAdapter->PortCfg.DefaultKeyId].KeyLen; RTUSBEnqueueCmdFromNdis(pAdapter, OID_802_11_ADD_WEP, TRUE, &WepKey, sizeof(WepKey)); } return 0;out: ReleaseAdapter(pAdapter, FALSE); return 0;}#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)static int usb_rtusb_close(struct net_device *net){ PRT2570ADAPTER pAdapter = (PRT2570ADAPTER) net->priv; int ret; DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup); DECLARE_WAITQUEUE (wait, current); int i = 0; printk("enter usb_rtusb_close( )\n"); DBGPRINT(RT_DEBUG_TEMP,"-->usb_rt2570_close \n"); netif_carrier_off(pAdapter->net); netif_stop_queue(pAdapter->net); DBGPRINT(RT_DEBUG_TEMP,"Ensure there are no more active urbs \n"); // ensure there are no more active urbs. add_wait_queue (&unlink_wakeup, &wait); pAdapter->wait = &unlink_wakeup; // maybe wait for deletions to finish. while ((i < 10) && atomic_read(&pAdapter->PendingRx) > 0) { //msleep(UNLINK_TIMEOUT_MS); i++; DBGPRINT ( RT_DEBUG_TEMP,"waited for %d urb to complete\n", atomic_read(&pAdapter->PendingRx)); } pAdapter->wait = NULL; remove_wait_queue (&unlink_wakeup, &wait); #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) if (pAdapter->MLMEThr_pid >= 0) { mlme_kill = 1; RTUSBCMDUp(pAdapter, (&(pAdapter->mlme_semaphore))); wmb(); // need to check ret = kill_proc (pAdapter->MLMEThr_pid, SIGTERM, 1); if (ret) { printk (KERN_ERR "%s: unable to signal thread\n", pAdapter->net->name); return ret; } wait_for_completion (&pAdapter->notify); } if (pAdapter->RTUSBCmdThr_pid>= 0) { RTUSBCmd_kill = 1; RTUSBCMDUp(pAdapter, (&(pAdapter->RTUSBCmd_semaphore))); wmb(); // need to check ret = kill_proc (pAdapter->RTUSBCmdThr_pid, SIGTERM, 1); if (ret) { printk (KERN_ERR "%s: unable to signal thread\n", pAdapter->net->name); return ret; } wait_for_completion (&pAdapter->notify); }#else if (pAdapter->MLMEThr_active) { ret = kthread_stop(pAdapter->MLMEThr); if (ret) { printk (KERN_ERR "%s: unable to stop thread\n", pAdapter->net->name); return ret; } } if (pAdapter->RTUSBCmdThr_active) { ret = kthread_stop(pAdapter->RTUSBCmdThr); if (ret) { printk (KERN_ERR "%s: unable to stop thread\n", pAdapter->net->name); return ret; } }#endif RTUSBHalt( pAdapter, FALSE); DBGPRINT(RT_DEBUG_TEMP,"<--usb_rt2570_close\n"); MOD_DEC_USE_COUNT; return 0;}int MlmeThread(void * Context){ PRT2570ADAPTER pAdapter = (PRT2570ADAPTER)Context;#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0) pAdapter->MLMEThr_active = 1;#else daemonize(); current->flags |= PF_NOFREEZE; /* signal that we've started the thread */ complete(&(pAdapter->notify));#endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0) */ while (1) {#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0) if (kthread_should_stop()) break;#endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0) */ /* lock the device pointers */ down(&(pAdapter->mlme_semaphore));#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) if (mlme_kill) break;#endif /* lock the device pointers , need to check if required*/ down(&(pAdapter->usbdev_semaphore)); MlmeHandler(pAdapter); /* unlock the device pointers */ up(&(pAdapter->usbdev_semaphore)); }#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) /* notify the exit routine that we're actually exiting now * * complete()/wait_for_completion() is similar to up()/down(), * except that complete() is safe in the case where the structure * is getting deleted in a parallel mode of execution (i.e. just * after the down() -- that's necessary for the thread-shutdown * case. * * complete_and_exit() goes even further than this -- it is safe in * the case that the thread of the caller is going away (not just * the structure) -- this is necessary for the module-remove case. * This is important in preemption kernels, which transfer the flow * of execution immediately upon a complete(). */ complete_and_exit (&pAdapter->notify, 0); DBGPRINT(RT_DEBUG_TRACE, "<---MlmeThread\n");#else DBGPRINT(RT_DEBUG_TRACE, "MlmeThread end\n"); pAdapter->MLMEThr_active = 0;#endif return 0;}int RTUSBCmdThread(void * Context){ PRT2570ADAPTER pAdapter = (PRT2570ADAPTER)Context;#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) daemonize(); current->flags |= PF_NOFREEZE; /* signal that we've started the thread */ complete(&(pAdapter->notify));#else pAdapter->RTUSBCmdThr_active = 1;#endif while (1) {#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0) if (kthread_should_stop()) break;#endif /* lock the device pointers */ down(&(pAdapter->RTUSBCmd_semaphore));#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) if (RTUSBCmd_kill) break;#endif /* lock the device pointers , need to check if required*/ down(&(pAdapter->usbdev_semaphore)); CMDHandler(pAdapter); /* unlock the device pointers */ up(&(pAdapter->usbdev_semaphore)); }#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) complete_and_exit (&pAdapter->notify, 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -