⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbdetection.c

📁 card 的检测读取等功能操作,主要用于多媒体,数码的各种卡的操作.
💻 C
📖 第 1 页 / 共 3 页
字号:
								}
							}
							UsbUpdateDeviceAddr(pHubBlock->moDevice.byDeviceAddr);
						}
						else
						{
							if(AVAILABLE == pHubBlock->maPortStatus[cPortCount])
							{																
								pHubBlock->maPortStatus[cPortCount] = NOT_AVAILABLE;
								//remove device
								USB_RemoveDevice(pHubBlock->maPortDeviceInfo[cPortCount].mwPortDeviceIndex, 
													pHubBlock->maPortDeviceInfo[cPortCount].mwPortDeviceType);
								if(USB_DEVICE_TYPE_HUB == pHubBlock->maPortDeviceInfo[cPortCount].mwPortDeviceType)
								{
									DEVICE_LIST_POINTER oDevicePointer;
									
									USB_GetHubDeviceBlockPointer(scUSBCurrentWorkingDevice, &oDevicePointer);
									memcpy(&pHubBlock->moPointer, &oDevicePointer, sizeof(DEVICE_LIST_POINTER));
								}
								
							}
						}
	       			}

					USB_SetHubDeviceBlock(scUSBCurrentWorkingDevice, pHubBlock);
					cIndex = pHubBlock->moPointer.mcIndexofNextDevice;
					if(USB_GetHubDeviceBlock(pHubBlock->moPointer.mcIndexofNextDevice, (PHUB_DEVICE_BLOCK)spBlockInSRAM))
					{
						pHubBlock = (PHUB_DEVICE_BLOCK)spBlockInSRAM;
					}
					else
					{
						break;
					}
		       	}while(pHubBlock != NULL && cHubCount);
			}
#endif

			//check mass storage
#ifdef D_SUPPORT_USB_HUB
			if(USB_GetHeadMassStorageBlock((PMASS_STORAGE_DEVICE_BLOCK)spBlockInSRAM, &cIndex) == TRUE)
			{
#endif			
				UINT8* pUSBInternalMem = aUsbInternalMem;
				UINT8 cSegmentCount;
				
				for(cSegmentCount=0; cSegmentCount<USB_SCBW_STRUCT; cSegmentCount++)
					pUSBInternalMem = pUSBInternalMem + USB_SP_MAPPING_SIZE[cSegmentCount];
				psCbw = (PSCBW)pUSBInternalMem;
				
				psCsw = (PSCSW)(pUSBInternalMem + USB_SP_MAPPING_SIZE[USB_SCBW_STRUCT]);
				memset(psCbw, 0, sizeof(SCBW));
				memset(psCsw, 0, sizeof(SCSW));

			     	pMassStorageBlock = (PMASS_STORAGE_DEVICE_BLOCK)spBlockInSRAM;
			       do
		       	{
		       		#ifdef D_SUPPORT_USB_HUB
		       		scUSBCurrentWorkingDevice = cIndex;
					#else
		       		scUSBCurrentWorkingDevice = 0;
					#endif
		       		UsbUpdateDeviceAddr(pMassStorageBlock->moDevice.byDeviceAddr);
					hBulkOut = &pMassStorageBlock->moBulkOutPipe;
					hBulkIn = &pMassStorageBlock->moBulkInPipe;
					//check if parent still exist
#ifdef D_SUPPORT_USB_HUB
					if(pMassStorageBlock->moPointer.mcIndexofParent != USB_LIST_NULL_INDEX  || gbRootUSBAttached == FALSE)
					{
#endif
						if(gbRootUSBAttached == FALSE
							#ifdef D_SUPPORT_USB_HUB
							|| USB_IsUsbDeviceParentStillExist(pMassStorageBlock->moPointer.mcIndexofParent) == FALSE 
							#endif
							)
						{
							DEVICE_LIST_POINTER oDevicePointer;

							USB_RemoveDevice(scUSBCurrentWorkingDevice, USB_DEVICE_TYPE_MASS_STORAGE);
							#ifdef D_SUPPORT_USB_HUB
							USB_GetMassStorageBlockPointer(scUSBCurrentWorkingDevice, &oDevicePointer);
							#endif
							memcpy(&pMassStorageBlock->moPointer, &oDevicePointer, sizeof(DEVICE_LIST_POINTER));
							if(Exception_catch(EXCEPTION_USB_TIMEOUT_ERROR))
							{
								//HUB_PORT_DEVICE_INFO newDeviceInfo;

								dbg_printf(("UsbDetection(): The USB Device has to be re-enumeration[1].\n"));
								USB_RemoveAllDevice(USB_DEVICE_TYPE_MASS_STORAGE);
								UsbHostClose();
								UsbTerminate();
								UsbPreAttachInit(FALSE);
								
								UsbMem_Init(); // initalized usb internal use heap

								UsbHostOpen( USB_NUMBER_OF_DEVICES ); // it also enable the USB attach interrupt
								#ifdef D_SUPPORT_USB_HUB
								USB_InitHubDeviceList();
								USB_InitMassStorageList();
								#endif
								//USB_NewDevice(NULL, &newDeviceInfo);
								gbRootUSBAttached = FALSE;
								gbTimeoutError = TRUE;
								return;
							}
							#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
							continue;	
						}
#ifdef D_SUPPORT_USB_HUB
					}
#endif
				
		       		pDevice = &pMassStorageBlock->moDevice;
					//pDefaultPipe = (HPIPE)pMassStorageBlock->moDevice.psPipes[0];
		       		for(cUnitCount=0; cUnitCount<=pMassStorageBlock->mcMaxLogicalUnit; cUnitCount++)
	       			{
						if( (pMassStorageBlock->maMassStorageUnitStatus[cUnitCount] == NOT_AVAILABLE)
							&& (PST_STOP != gcs.pstate)
							#ifdef D_AV_RECEIVER
							&&(MODE_RECEIVER != BrowseHandler_GetCurrMode())
							#endif
							)
						{
							continue;
						}

						uiRes = USB_MassStorageTestUnitStatus(pDevice, cUnitCount);
						if(UFI_COMMAND_STATUS_SUCCESS == uiRes)
						{
							if(pMassStorageBlock->maMassStorageUnitStatus[cUnitCount] == NOT_AVAILABLE)
							{
								SUFIREADCAPDATA oCapacityData;
								SUFIREQSENSEDATA oReqSenseData;
								UINT uiRes;
								UINT32 dwValue;
								UINT16 	wType;


								uiRes = MassStorageUFICommand((PBYTE)&oReqSenseData, UFI_REQUEST_SENSE);
								uiRes = MassStorageUFICommand((PBYTE)&oCapacityData, UFI_READ_CAPACITY);
								if(UFI_COMMAND_STATUS_SUCCESS == uiRes)
								{
									if(oCapacityData.uwBlockLengthMidLsb != 0x02
										|| oCapacityData.uwBlockLengthLsb != 0x00)
									{

										continue;
									}
								}
								else
								{
									uiRes = MassStorageUFICommand((PBYTE)&oReqSenseData, UFI_REQUEST_SENSE);
									if(UFI_COMMAND_STATUS_SUCCESS == uiRes)
									{
										tr_printf(("Sensekey: %02X, ASC: %02X, ASCQ: %02X\n",
											oReqSenseData.uwSenseKey,
											oReqSenseData.uwAdditionalSenseCode,
											oReqSenseData.wAdditionalSenseQualifier));	
									}
									uiRes = MassStorageUFICommand((PBYTE)&oCapacityData, UFI_READ_CAPACITY);
									if(UFI_COMMAND_STATUS_SUCCESS == uiRes)
									{
										if(oCapacityData.uwBlockLengthMidLsb != 0x02
											|| oCapacityData.uwBlockLengthLsb != 0x00)
										{

											continue;
										}
									}
								}
								
								wType = (WORD)CARD_TYPE_USB |(((WORD)cUnitCount ) << 8);
								dwValue = (((DWORD)scUSBCurrentWorkingDevice)<< 16) + wType;
								ie_send_ex(IE_CORE_CARD_INSERTED, (void *)dwValue);
								pMassStorageBlock->maMassStorageUnitStatus[cUnitCount] = AVAILABLE;
								dbg_printf(("UsbDetection(): Mass Storage Inserted! DeviceBlockIndex: %02X, Unit: %02X\n",
												scUSBCurrentWorkingDevice, cUnitCount));

							}
						}
						else if(UFI_COMMAND_STATUS_FAIL == uiRes)
						{
							if(pMassStorageBlock->maMassStorageUnitStatus[cUnitCount] == AVAILABLE)
							{
								UINT32 dwValue;
								UINT16 	wType;

								wType = (WORD)CARD_TYPE_USB|(((WORD)cUnitCount ) << 8);
								dwValue = (((DWORD)scUSBCurrentWorkingDevice)<< 16) + wType;
								ie_send_ex(IE_CORE_CARD_REMOVED, (void *)dwValue);
								pMassStorageBlock->maMassStorageUnitStatus[cUnitCount] = NOT_AVAILABLE;
								dbg_printf(("UsbDetection(): Mass Storage Removed! DeviceBlockIndex: %02X, Unit: %02X\n",
												scUSBCurrentWorkingDevice, cUnitCount));
							}
						}
						else
						{
							UINT8 cUnitCount=0;
							UINT32 dwValue;
							UINT16 wType;

							for(cUnitCount=0; cUnitCount<=pMassStorageBlock->mcMaxLogicalUnit; cUnitCount++)
							{
								if(pMassStorageBlock->maMassStorageUnitStatus[cUnitCount] == AVAILABLE)
									{
										wType = (WORD)CARD_TYPE_USB|(((WORD)cUnitCount ) << 8);
										dwValue = (((DWORD)scUSBCurrentWorkingDevice)<< 16) + wType;
										ie_send_ex(IE_CORE_CARD_REMOVED, (void *)dwValue);
										USB_RemoveDevice(scUSBCurrentWorkingDevice, USB_DEVICE_TYPE_MASS_STORAGE);
										pMassStorageBlock->maMassStorageUnitStatus[cUnitCount] = NOT_AVAILABLE;
									}
	
							}

							dbg_printf(("USB_Detection(): Mass Storage Device Unit Detection Error\n"));
							if(Exception_catch(EXCEPTION_USB_TIMEOUT_ERROR))
							{
								dbg_printf(("USB_Detection(): The USB Device has to be re-enumeration[2].\n"));

								USB_RemoveAllDevice(USB_DEVICE_TYPE_MASS_STORAGE);
								UsbHostClose();
								UsbTerminate();
								
								UsbPreAttachInit(FALSE);

								UsbMem_Init(); // initalized usb internal use heap

								UsbHostOpen( USB_NUMBER_OF_DEVICES ); // it also enable the USB attach interrupt
								#ifdef D_SUPPORT_USB_HUB
								USB_InitHubDeviceList();
								USB_InitMassStorageList();
								#endif
								gbRootUSBAttached = FALSE;
								gbTimeoutError = TRUE;
								return;
							}
						}
	       			}

					#ifdef D_SUPPORT_USB_HUB
					USB_SetMassStorageBlock(scUSBCurrentWorkingDevice, pMassStorageBlock);
					cIndex = pMassStorageBlock->moPointer.mcIndexofNextDevice;
					if(USB_GetMassStorageBlock(pMassStorageBlock->moPointer.mcIndexofNextDevice, (PMASS_STORAGE_DEVICE_BLOCK)spBlockInSRAM))
					{
						pMassStorageBlock = (PMASS_STORAGE_DEVICE_BLOCK)spBlockInSRAM;
					}
					else
					{
						break;
					}
					#endif
					#ifndef D_SUPPORT_USB_HUB
					break;
					#endif
		       	}while(pMassStorageBlock != NULL);			
#ifdef D_SUPPORT_USB_HUB				   
			}
			if(USB_IsUsbMassStorageListEmpty()==TRUE 
				&& USB_IsUsbHubListEmpty()==TRUE

				)
				gbRootUSBAttached = FALSE;
#endif			
		}
		else
		{
			#ifdef D_SUPPORT_USB_HUB
			HUB_PORT_DEVICE_INFO oNewDeviceInfo;
			#endif

			if(bIsUnsupportedDevice)
				return;
			#ifdef D_SUPPORT_USB_HUB
			if(USB_NewDevice(NULL, &oNewDeviceInfo))
			#else
			if(USB_NewDevice(NULL))
			#endif
			{
				bIsUnsupportedDevice = FALSE;
				cUsbEnumerationRetryCounter = 0;
				
				gbRootUSBAttached = TRUE;
				//scCountSkipDetection = 10;
			}
			else
			{
				dbg_printf(("UsbDetection(): Root Device Open Fail\n"));
				if(Exception_catch(EXCEPTION_USB_TIMEOUT_ERROR))
					gbTimeoutError = TRUE;
				
				UsbHostClose();
				UsbTerminate();
				
				UsbPreAttachInit(FALSE);

				UsbMem_Init(); // initalized usb internal use heap

				UsbHostOpen( USB_NUMBER_OF_DEVICES ); // it also enable the USB attach interrupt
				#ifdef D_SUPPORT_USB_HUB
				USB_InitHubDeviceList();
				USB_InitMassStorageList();
				#endif

				gbRootUSBAttached = FALSE;
				cUsbEnumerationRetryCounter++;

				if(cUsbEnumerationRetryCounter >= USB_ENUMERATION_RETRY_TIMES)
				{
					bIsUnsupportedDevice = TRUE;
					#ifdef D_GINGER_I86
					ie_send(IE_CORE_UNSUPPORT_USB_DEVICE_INSERTED);
					#endif
					#ifdef D_GINGER_I96
					ie_send(IE_UI_UNSUPPORT_USB_DEVICE_INSERTED);
					#endif					
				}

			}
				
			
		}
	}
	else if(UsbGetAttachIntStatReg() == FALSE)
	{
		bIsUnsupportedDevice = FALSE;
		cUsbEnumerationRetryCounter = 0;
		if(gbRootUSBAttached)
		{
		#ifndef D_SUPPORT_USB_HUB
		DEVICE_LIST_POINTER oDevicePointer;

		USB_RemoveDevice(scUSBCurrentWorkingDevice, USB_DEVICE_TYPE_MASS_STORAGE);
		#ifdef D_SUPPORT_USB_HUB
		USB_GetMassStorageBlockPointer(scUSBCurrentWorkingDevice, &oDevicePointer);
		#endif
		memcpy(&pMassStorageBlock->moPointer, &oDevicePointer, sizeof(DEVICE_LIST_POINTER));
		if(Exception_catch(EXCEPTION_USB_TIMEOUT_ERROR))
			{
			//HUB_PORT_DEVICE_INFO newDeviceInfo;

			dbg_printf(("UsbDetection(): The USB Device has to be re-enumeration[1].\n"));
			USB_RemoveAllDevice(USB_DEVICE_TYPE_MASS_STORAGE);
			UsbHostClose();
			UsbTerminate();
			UsbPreAttachInit(FALSE);
							
			UsbMem_Init(); // initalized usb internal use heap

			UsbHostOpen( USB_NUMBER_OF_DEVICES ); // it also enable the USB attach interrupt
			#ifdef D_SUPPORT_USB_HUB
			USB_InitHubDeviceList();
			USB_InitMassStorageList();
			#endif
			gbRootUSBAttached = FALSE;
			gbTimeoutError = TRUE;
			return;
			}
		#endif
		gbRootUSBAttached = FALSE;
		}
	}
}


/*--------------------------------------------------------
function: USB_MassStorageTestUnitStatus
description:	get lun number and send UFI command to test every unit's status.
input:	pDeviceHandler: device structure;
		cLun: current logical unit number.
return : uiRes: UFI command state
--------------------------------------------------------*/	
static UINT  USB_MassStorageTestUnitStatus(HDEVICE pDeviceHandler, UINT8 cLun)
{
	UINT8*	pDataBuffer;
	UINT8	cMaxLun;
	UINT 	uiRes = UFI_COMMAND_STATUS_FAIL;

	cMaxLun = UsbHostGetMaxLun(pDeviceHandler);
	if(cLun > cMaxLun)
	{
		dbg_printf(("USB_MassStorageTestUnitStatus():  wrong device lun.\n"));
		return uiRes;
	}
	pDataBuffer = MEM_Allocate(DEFAULT_HEAP, 100);
	if(NULL == pDataBuffer)
	{
		dbg_printf(("USB_MassStorageTestUnitStatus(): Can not allocate memory\n"));
		return uiRes;
	}
	((PSUSBDEVICE)pDeviceHandler)->ucCurrLun = cLun;
	uiRes = USB_MassStorageWaitForUnitReady(pDeviceHandler, (PBYTE)pDataBuffer);
	
	SAFELY_FREE_PTR(pDataBuffer);
	return uiRes;
}

/*--------------------------------------------------------
function: USB_MassStorageWaitForUnitReady
description:	send UFI command to test every unit's status.
input:	pDeviceHandler: device structure;
		pDataBuffer: data buffer for UFI command.
return : uiRes: UFI command state
--------------------------------------------------------*/	
#pragma argsused
static UINT USB_MassStorageWaitForUnitReady(HDEVICE pDeviceHandler, UINT8* pDataBuffer)
{
   	UINT uiRes = UFI_COMMAND_STATUS_FAIL;
		
		uiRes = MassStorageUFICommand( NULL, UFI_TEST_UNIT_READY);
		if(UFI_COMMAND_STATUS_ERROR == uiRes)
			{
			return uiRes;
			}
		else if(UFI_COMMAND_STATUS_FAIL == uiRes)
			{
			uiRes = MassStorageUFICommand( NULL, UFI_TEST_UNIT_READY);
			return uiRes;
			}
		else
			return uiRes;
}

#ifdef D_USB_REMOVE_PATCH
void USB_RemoveAll(void)
{
	// remove all usbs only once 
	if (b_UsbAllRemoved)
		return;
		
	tr_printf(("\nremove all usbs!"));
	USB_RemoveAllDevice(USB_DEVICE_TYPE_MASS_STORAGE);	
	UsbTerminate();
	gbRootUSBAttached = FALSE;
	b_UsbAllRemoved = TRUE;

}
#endif

#endif //I86_USB_SUPPORT

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -