📄 rtusb_main.c
字号:
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;
NdisZeroMemory(pAdapter->PortCfg.SharedKey[KeyIdx].Key, MAX_LEN_OF_KEY);
NdisMoveMemory(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;
NdisZeroMemory(pAdapter->PortCfg.SharedKey[KeyIdx].Key, MAX_LEN_OF_KEY);
NdisMoveMemory(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;
PNDIS_802_11_KEY pkey = (PNDIS_802_11_KEY)pData;
DBGPRINT(RT_DEBUG_INFO,"pKey->KeyMaterial =\n ");
for (i = 0; i < 16; i++)
{
DBGPRINT_RAW(RT_DEBUG_INFO,"%02x:", pkey->KeyMaterial[i]);
}
DBGPRINT(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) &&
#if 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;
printk("RT25usb Driver version %x.%x.%x\n", DRV_MAJORVERSION,DRV_MINORVERSION, DRV_SUBVERSION);
init_MUTEX(&(pAdapter->usbdev_semaphore));
// init mediastate to disconnected
pAdapter->MediaState = NdisMediaStateDisconnected;
pAdapter->rx_bh.func = RTUSBRxPacket;
PortCfgInit(pAdapter);
// 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
mlme_kill = 0;
RTUSBCmd_kill =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);
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 (pAdapter->MLMEThr_pid >= 0)
{
mlme_kill = 1;
RTUSBUp(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;
RTUSBUp(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);
}
RTUSBHalt( pAdapter, FALSE);
DBGPRINT(RT_DEBUG_TEMP,"<--usb_rt2570_close\n");
return 0;
}
int MlmeThread(void * Context)
{
PRT2570ADAPTER pAdapter = (PRT2570ADAPTER)Context;
daemonize();
current->flags |= PF_NOFREEZE;
/* signal that we've started the thread */
complete(&(pAdapter->notify));
#if 1
while (1)
{
//if(down_interruptible(&pAdapter->mlme_semaphore))
//break;
/* lock the device pointers */
down(&(pAdapter->mlme_semaphore));
if (mlme_kill)
break;
/* lock the device pointers , need to check if required*/
down(&(pAdapter->usbdev_semaphore));
MlmeHandler(pAdapter);
/* unlock the device pointers */
up(&(pAdapter->usbdev_semaphore));
}
#else
// I tried this way for thread handling
while(1)
{
timeout = next_tick;
do {
timeout = interruptible_sleep_on_timeout (&pAdapter->MLMEThr_wait, timeout);
/* make swsusp happy with our thread */
if (current->flags & PF_FREEZE)
refrigerator(PF_FREEZE);
DBGPRINT(RT_DEBUG_TRACE, "current->flags = 0x%x\n",current->flags );
} while (!signal_pending (current) && (timeout > 0));
if (signal_pending (current)) {
flush_signals(current);
}
if (mlme_kill)
break;
}
#endif
/* 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");
}
int RTUSBCmdThread(void * Context)
{
PRT2570ADAPTER pAdapter = (PRT2570ADAPTER)Context;
daemonize();
current->flags |= PF_NOFREEZE;
/* signal that we've started the thread */
complete(&(pAdapter->notify));
while (1)
{
//if(down_interruptible(&pAdapter->mlme_semaphore))
//break;
/* lock the device pointers */
down(&(pAdapter->RTUSBCmd_semaphore));
if (RTUSBCmd_kill)
break;
/* lock the device pointers , need to check if required*/
down(&(pAdapter->usbdev_semaphore));
CMDHandler(pAdapter);
/* unlock the device pointers */
up(&(pAdapter->usbdev_semaphore));
}
/* 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, "<---RTUSBCmdThread\n");
}
static void *usb_rtusb_probe(struct usb_device *dev, UINT interface,
const struct usb_device_id *id_table)
{
PRT2570ADAPTER pAdapter = (PRT2570ADAPTER)NULL;
int i;
struct net_device *netdev;
int res = -ENOMEM;
for (i = 0; i < rtusb_usb_id_len; i++)
{
if (dev->descriptor.idVendor == rtusb_usb_id[i].idVendor &&
dev->descriptor.idProduct == rtusb_usb_id[i].idProduct)
{
printk("idVendor = 0x%x, idProduct = 0x%x \n",dev->descriptor.idVendor, dev->descriptor.idProduct);
break;
}
}
if (i == rtusb_usb_id_len) {
printk("Device Descriptor not matching\n");
return NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -