📄 usbdetection.c
字号:
pNewMassStorageDevice->mcMaxLogicalUnit = cMaxLun;
pNewMassStorageDevice->mcCurrentUnit = 0;
pNewMassStorageDevice->mwDeviceType = USB_DEVICE_TYPE_MASS_STORAGE;
if(pBlockBufInSRAM != NULL)
pNewMassStorageDevice->moPointer.mcIndexofParent = scUSBCurrentWorkingDevice;
else
pNewMassStorageDevice->moPointer.mcIndexofParent = USB_LIST_NULL_INDEX;
for(cUnitCount=0; cUnitCount<=pNewMassStorageDevice->mcMaxLogicalUnit; cUnitCount++)
{
pNewMassStorageDevice->maMassStorageUnitStatus[cUnitCount] = NOT_AVAILABLE;
}
#ifdef D_SUPPORT_USB_HUB
if(USB_AddMassStorageBlock(pNewMassStorageDevice, &cNewIndex))
{
pNewDeviceInfo->mwPortDeviceIndex = cNewIndex;
pNewDeviceInfo->mwPortDeviceType = USB_DEVICE_TYPE_MASS_STORAGE;
pNewDeviceInfo->mwPortNumber = 0;//((PSUSBPORT)hPort)->uiPortNum; //hPort is NULL
}
else
{
dbg_printf(("Add New Mass Storage Device Block Fail\n"));
if(pBlockBufInSRAM != NULL)
{
UsbMem_Restore();
memcpy(aUsbInternalMem, pBackupDeviceBlock, USB_INTERNAL_USAGE_HEAP_SIZE);
}
MEM_Free(DEFAULT_HEAP, pDescriptorBuffer);
MEM_Free(DEFAULT_HEAP, pBackupDeviceBlock);
return FALSE;
}
#endif
}
else
{
dbg_printf(("USB_NewDevice(): Not supported USB device\n"));
if(pBlockBufInSRAM != NULL)
{
UsbMem_Restore();
memcpy(aUsbInternalMem, pBackupDeviceBlock, USB_INTERNAL_USAGE_HEAP_SIZE);
}
MEM_Free(DEFAULT_HEAP, pDescriptorBuffer);
MEM_Free(DEFAULT_HEAP, pBackupDeviceBlock);
return FALSE;
}
if(pBlockBufInSRAM != NULL)
{
UsbMem_Restore();
memcpy(aUsbInternalMem, pBackupDeviceBlock, USB_INTERNAL_USAGE_HEAP_SIZE);
}
MEM_Free(DEFAULT_HEAP, pDescriptorBuffer);
MEM_Free(DEFAULT_HEAP, pBackupDeviceBlock);
return TRUE;
}
/*--------------------------------------------------------
function: USB_RemoveAllDevice
description: Remove all device and free mem space, send event to sys.
input: oDeviceType: Removed device type.
return : Null
--------------------------------------------------------*/
STATIC VOID USB_RemoveAllDevice(USB_DEVICE_TYPE oDeviceType)
{
UINT8* pBackupDeviceBlock;
pBackupDeviceBlock = MEM_Allocate(DEFAULT_HEAP, USB_INTERNAL_USAGE_HEAP_SIZE);
if(NULL == pBackupDeviceBlock)
{
dbg_printf(("USB_NewDevice(): Can not allocate memory for BackupDeviceBlock\n"));
return;
}
dbg_printf(("USB_RemoveAllDevice() Calling...\n"));
#ifdef D_SUPPORT_USB_HUB
if(USB_DEVICE_TYPE_HUB == oDeviceType)
{
;// TODO:
}
else
#endif
if(USB_DEVICE_TYPE_MASS_STORAGE == oDeviceType)
{
UINT8 cUnitCount;
UINT8 cIndex=0;
UINT32 dwValue;
UINT16 wType;
PMASS_STORAGE_DEVICE_BLOCK pMassStorageBlock;
UsbMem_Backup();
memcpy(pBackupDeviceBlock, spBlockInSRAM, USB_INTERNAL_USAGE_HEAP_SIZE);
#ifdef D_SUPPORT_USB_HUB
if(USB_GetHeadMassStorageBlock((PMASS_STORAGE_DEVICE_BLOCK)spBlockInSRAM, &cIndex) == TRUE)
#endif
{
pMassStorageBlock = (PMASS_STORAGE_DEVICE_BLOCK)spBlockInSRAM;
do
{
for(cUnitCount=0; cUnitCount<=pMassStorageBlock->mcMaxLogicalUnit; cUnitCount++)
{
if(AVAILABLE == pMassStorageBlock->maMassStorageUnitStatus[cUnitCount])
{
wType = (WORD)CARD_TYPE_USB |(((WORD)cUnitCount ) << 8);
dwValue = (((DWORD)cIndex)<< 16) + (DWORD)wType;
ie_send_ex(IE_CORE_CARD_REMOVED, (void *)dwValue);
dbg_printf(("USB_RemoveAllDevice(): Mass Storage Remove! DeviceBlockIndex: %02X, Unit: %02X\n",
cIndex, cUnitCount));
#ifdef D_SUPPORT_USB_HUB
break;
#endif
}
}
#ifdef D_SUPPORT_USB_HUB
cIndex = pMassStorageBlock->moPointer.mcIndexofNextDevice;
if(USB_GetMassStorageBlock(pMassStorageBlock->moPointer.mcIndexofNextDevice, (PMASS_STORAGE_DEVICE_BLOCK)spBlockInSRAM))
{
pMassStorageBlock = (PMASS_STORAGE_DEVICE_BLOCK)spBlockInSRAM;
}
else
{
break;
}
#endif
pMassStorageBlock = NULL;
}while(pMassStorageBlock != NULL);
}
memcpy(spBlockInSRAM, pBackupDeviceBlock, USB_INTERNAL_USAGE_HEAP_SIZE);
UsbMem_Restore();
}
MEM_Free(DEFAULT_HEAP, pBackupDeviceBlock);
}
/*--------------------------------------------------------
function: USB_RemoveDevice
description: Remove one device and free mem space, send event to sys.
input: cIndex: the position of removed device;
oDeviceType: removed device type.
return : Null
--------------------------------------------------------*/
STATIC VOID USB_RemoveDevice(UINT8 cIndex, USB_DEVICE_TYPE oDeviceType)
{
UINT8* pBackupDeviceBlock;
pBackupDeviceBlock = MEM_Allocate(DEFAULT_HEAP, USB_INTERNAL_USAGE_HEAP_SIZE);
if(NULL == pBackupDeviceBlock)
{
dbg_printf(("USB_NewDevice(): Can not allocate memory for BackupDeviceBlock\n"));
return;
}
dbg_printf(("USB_RemoveDevice() Calling...\n"));
#ifdef D_SUPPORT_USB_HUB
if(USB_DEVICE_TYPE_HUB == oDeviceType)
{
UsbMem_Backup();
memcpy(pBackupDeviceBlock, spBlockInSRAM, USB_INTERNAL_USAGE_HEAP_SIZE);
if(USB_GetHubDeviceBlock(cIndex, (PHUB_DEVICE_BLOCK)spBlockInSRAM))
{
UsbHostDeviceClose(&((PHUB_DEVICE_BLOCK)spBlockInSRAM)->moDevice);
UsbHostPortDisconnect(NULL);
USB_DeleteHubDeviceBlock(cIndex);
}
else
{
dbg_printf(("USB_RemoveDevice(): No Such Hub Device or Get Device Block Fail\n"));
}
memcpy(spBlockInSRAM, pBackupDeviceBlock, USB_INTERNAL_USAGE_HEAP_SIZE);
UsbMem_Restore();
}
else
#endif
if(USB_DEVICE_TYPE_MASS_STORAGE == oDeviceType)
{
UINT8 cUnitCount;
UsbMem_Backup();
memcpy(pBackupDeviceBlock, spBlockInSRAM, USB_INTERNAL_USAGE_HEAP_SIZE);
#ifdef D_SUPPORT_USB_HUB
if(USB_GetMassStorageBlock(cIndex, (PMASS_STORAGE_DEVICE_BLOCK)spBlockInSRAM))
{
#endif
for(cUnitCount = 0; cUnitCount <=((PMASS_STORAGE_DEVICE_BLOCK)spBlockInSRAM)->mcMaxLogicalUnit; cUnitCount++)
{
if(((PMASS_STORAGE_DEVICE_BLOCK)spBlockInSRAM)->maMassStorageUnitStatus[cUnitCount] == AVAILABLE)
{
UINT32 dwValue;
UINT16 wType;
wType = (WORD)CARD_TYPE_USB |(((WORD)cUnitCount ) << 8);
dwValue = (((DWORD)cIndex)<< 16) + (DWORD)wType;
ie_send_ex(IE_CORE_CARD_REMOVED, (void *)dwValue);
dbg_printf(("USB_RemoveDevice(): Mass Storage Remove! DeviceBlockIndex: %02X, Unit: %02X\n",
cIndex, cUnitCount));
}
}
UsbHostDeviceClose(&((PMASS_STORAGE_DEVICE_BLOCK)spBlockInSRAM)->moDevice);
UsbHostPortDisconnect(NULL);
#ifdef D_SUPPORT_USB_HUB
USB_DeleteMassStorageBlock(cIndex);
}
else
#endif
{
dbg_printf(("No Such Mass Storage Device or Get Device Block Fail\n"));
}
memcpy(spBlockInSRAM, pBackupDeviceBlock, USB_INTERNAL_USAGE_HEAP_SIZE);
UsbMem_Restore();
}
MEM_Free(DEFAULT_HEAP, pBackupDeviceBlock);
}
/*--------------------------------------------------------
function: USB_Detection
description: main usb detection function, including hub and mass storage device.
input: void.
return : Null
--------------------------------------------------------*/
VOID USB_Detection(void)
{
#ifdef D_SUPPORT_USB_HUB
PHUB_DEVICE_BLOCK pHubBlock;
BOOL bReturnValue;
INT32 lNumBytesRead;
UINT8 cReturnData;
HPIPE pDefaultPipe;
UINT8 cPortCount;
#endif
PMASS_STORAGE_DEVICE_BLOCK pMassStorageBlock;
UINT8 cUnitCount;
HDEVICE pDevice;
UINT uiRes;
STATIC UINT8 scCountSkipDetection = 0;
if((gbIsUSBDriver == FALSE) && IS_PLAYING_CARD && gcs.pstate!= PST_STOP)
{
return;
}
if(gbTimeoutError)
{
gbTimeoutError = FALSE;
return;
}
if(scCountSkipDetection != 0)
{
scCountSkipDetection--;
dbg_printf(("USB_Detection(): Detection will be skip\n"));
return;
}
#ifdef D_GINGER_I96
FcuTerminate();
UsbInit(FALSE);
#endif
#ifdef D_USB_REMOVE_PATCH
if (b_UsbAllRemoved)
{
// if all usbs removed already, reset
b_UsbAllRemoved = FALSE;
}
#endif
if(gbIsUSBDriver == FALSE)
{
#ifdef D_GINGER_I86
FcuTerminate();
UsbInit(FALSE);
#endif
gbIsUSBDriver = TRUE;
scCountSkipDetection = 3;
return;
}
if(UsbGetAttachIntStatReg() == TRUE
#ifdef D_SUPPORT_USB_HUB
|| !USB_IsUsbHubListEmpty() ||
!USB_IsUsbMassStorageListEmpty()
#endif
)
{
if(UsbGetAttachIntStatReg() == TRUE)
UsbResetDetachOnTheFlyDetection();
else
gbRootUSBAttached = FALSE;
if(gbRootUSBAttached==TRUE
#ifdef D_SUPPORT_USB_HUB
||!USB_IsUsbHubListEmpty() ||
!USB_IsUsbMassStorageListEmpty()
#endif
)
{
#ifdef D_SUPPORT_USB_HUB
UINT8 cIndex;
UINT8 cHubCount;
//if the root device is not exist anymore, just empty the hub device list
if(gbRootUSBAttached == FALSE)
{
USB_InitHubDeviceList();
}
//check hub
else if(USB_GetHeadHubDeviceBlock((PHUB_DEVICE_BLOCK)spBlockInSRAM, &cIndex) == TRUE)
{
pHubBlock = (PHUB_DEVICE_BLOCK)spBlockInSRAM;
cHubCount = USB_GetHubCount();
do
{
cHubCount--;
scUSBCurrentWorkingDevice = cIndex;
UsbUpdateDeviceAddr(pHubBlock->moDevice.byDeviceAddr);
//check if parent still exist
if(pHubBlock->moPointer.mcIndexofParent != USB_LIST_NULL_INDEX || gbRootUSBAttached == FALSE)
{
if(USB_IsUsbDeviceParentStillExist(pHubBlock->moPointer.mcIndexofParent) == FALSE || gbRootUSBAttached == FALSE)
{
DEVICE_LIST_POINTER oDevicePointer;
//remove hub devcie
USB_RemoveDevice(scUSBCurrentWorkingDevice, USB_DEVICE_TYPE_HUB);
USB_GetHubDeviceBlockPointer(scUSBCurrentWorkingDevice, &oDevicePointer);
memcpy(&pHubBlock->moPointer, &oDevicePointer, sizeof(DEVICE_LIST_POINTER));
dbg_printf(("UsbDetection(): Hub Removed! HubBlockIndex: %02X \n",
scUSBCurrentWorkingDevice));
cIndex = pHubBlock->moPointer.mcIndexofNextDevice;
if(USB_GetHubDeviceBlock(pHubBlock->moPointer.mcIndexofNextDevice, (PHUB_DEVICE_BLOCK)spBlockInSRAM))
{
pHubBlock = (PHUB_DEVICE_BLOCK)spBlockInSRAM;
}
else
{
break;
}
continue;
}
}
pDevice = &pHubBlock->moDevice;
pDefaultPipe = UsbHostGetDefaultPipe(pDevice);
for(cPortCount=0; cPortCount<pHubBlock->mcMaxChild; cPortCount++)
{
if( (pHubBlock->maPortStatus[cPortCount] == NOT_AVAILABLE)
&& (PST_STOP != gcs.pstate))
{
continue;
}
//get port status change bit
bReturnValue = UsbHostHubGetStatus(pDefaultPipe,
cPortCount + 1,
(PBYTE)&cReturnData,
(PLONG)&lNumBytesRead,
USB_WAIT_FOREVER);
if(bReturnValue == FALSE)
{
DEVICE_LIST_POINTER oDevicePointer;
//Exception_catch(EXCEPTION_USB_TIMEOUT_ERROR);
//remove hub devcie
USB_RemoveDevice(scUSBCurrentWorkingDevice, USB_DEVICE_TYPE_HUB);
USB_GetHubDeviceBlockPointer(scUSBCurrentWorkingDevice, &oDevicePointer);
memcpy(&pHubBlock->moPointer, &oDevicePointer, sizeof(DEVICE_LIST_POINTER));
dbg_printf(("UsbDetection(): Hub Removed! HubBlockIndex: %02X \n",
scUSBCurrentWorkingDevice));
break;
}
if(cReturnData & HUB_STS_PORT_PRESENT)
{
if(NOT_AVAILABLE == pHubBlock->maPortStatus[cPortCount])
{
HUB_PORT_DEVICE_INFO newDeviceInfo;
bReturnValue = UsbHostHubSetFeature( pDefaultPipe,
cPortCount + 1,
USB_HUB_FS_PORT_RESET,
USB_WAIT_FOREVER );
if(bReturnValue == FALSE)
{
Exception_catch(EXCEPTION_USB_TIMEOUT_ERROR);
continue;
}
bReturnValue = UsbHostHubClearFeature( pDefaultPipe,
USB_HUB_FS_C_PORT_RESET,
0,
cPortCount + 1,
USB_WAIT_FOREVER );
if(bReturnValue == FALSE)
{
Exception_catch(EXCEPTION_USB_TIMEOUT_ERROR);
continue;
}
bReturnValue = UsbHostHubSetFeature( pDefaultPipe,
cPortCount + 1,
USB_HUB_FS_PORT_RESET,
USB_WAIT_FOREVER );
if(bReturnValue == FALSE)
{
Exception_catch(EXCEPTION_USB_TIMEOUT_ERROR);
continue;
}
bReturnValue = UsbHostHubClearFeature( pDefaultPipe,
USB_HUB_FS_C_PORT_RESET,
0,
cPortCount + 1,
USB_WAIT_FOREVER );
if(bReturnValue == FALSE)
{
Exception_catch(EXCEPTION_USB_TIMEOUT_ERROR);
continue;
}
//new device
if(USB_NewDevice(pHubBlock, &newDeviceInfo))
{
DEVICE_LIST_POINTER oDevicePointer;
pHubBlock->maPortDeviceInfo[cPortCount].mwPortDeviceIndex = newDeviceInfo.mwPortDeviceIndex;
pHubBlock->maPortDeviceInfo[cPortCount].mwPortDeviceType = newDeviceInfo.mwPortDeviceType;
pHubBlock->maPortDeviceInfo[cPortCount].mwPortNumber = newDeviceInfo.mwPortNumber;
pHubBlock->maPortStatus[cPortCount] = AVAILABLE;
if(USB_DEVICE_TYPE_HUB == newDeviceInfo.mwPortDeviceType)
{
USB_GetHubDeviceBlockPointer(scUSBCurrentWorkingDevice, &oDevicePointer);
memcpy(&pHubBlock->moPointer, &oDevicePointer, sizeof(DEVICE_LIST_POINTER));
}
//bIsAnyNewDevice = TRUE;
bIsUnsupportedDevice = FALSE;
cUsbEnumerationRetryCounter = 0;
break;
}
else
{
Exception_catch(EXCEPTION_USB_TIMEOUT_ERROR);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -