📄 usb.c
字号:
if (read_error_counter == C_USB_READ_ERROR_COUNT) { read_error_counter = 0; /* task state handling */ usb_fsm_background.state = FSM_ERROR; //TBD - could be general FSM states usb_fsm_background.error_reason = err; //MUSB_MsdCheckMedium(NULL); /* medium could be removed */ //dangerous } else { if (pUsbDevice_tmp) { read_error_counter++; last_read_params = pUsbDevice_tmp->MsdDevice.read_params; /* TBD - chech medium can harm the reading; to procss USB_read return value? */ USB_Read(pUsbMsdVolume, last_read_params.dwStartBlockLo, last_read_params.dwStartBlockHi, last_read_params.wBlockCount, last_read_params.pReadBuffer); } else read_error_counter = C_USB_READ_ERROR_COUNT; //TBD in the case of error } break;#endif#if ((0!=IPOD_PASSTHROUGH) || (0!=IPOD_AP)) //[LL]new case E_USB_DOWNSTREAM: /* pass-through downstream error */ DBG_PRINTF("Downstream error (0x%04x)\r\n", err); /* events handling */ local_usb_downstream_status.DataTag = local_usb_downstream_cmd.DataTag; local_usb_downstream_status.bStatus = err; break;#endif /*IPOD_PASSTHROUGH || IPOD_AP*/ case E_USB_INIT_FATAL: /* task initailization failed */ DBG_PRINTF("FATAL:USB initialization failed (0x%04x)\r\n", err); /* events handling */ event_set_out(USB_STATUS_EVENT); /* try again */ USB_StartEnumTimer(C_USB_INIT_RECOVERY_TIME); //TBD - spread info? break; case E_USB_MOUNT_FATAL: /* mount fatal error */ DBG_PRINTF("FATAL:USB device mounting failed (0x%04x)\r\n", err); if(gUsbOn == TRUE) { /* events handling */ event_set_out(USB_STATUS_EVENT); USB_ReinitializeUsb(); } break; case E_USB_REMOVE_MEDIUM_FATAL: DBG_PRINTF("FATAL:Not working medium removed (0x%04x)\r\n", err); //TBD - and? /* events handling */ event_set_out(USB_STATUS_EVENT); break; default: //TBD if (err | E_USB_FATAL) { DBG_PRINTF("FATAL error "); } else { DBG_PRINTF("Unknown error "); } DBG_PRINTF("(0x%04x)\r\n", err); /* events handling */ event_set_out(USB_STATUS_EVENT); } return S_USB_FSM_END; } do { /* start to mount the MSD volumes */ if(M_IS_SET(gUsbDevice.flag_mount)) { M_CLEAR_FLAG(gUsbDevice.flag_mount); MUSB_MsdMountVolume(NULL, S_USB_MOUNT_ALL); } /* to let the parent know about the change */ if(M_IS_SET(gUsbDevice.flag_change)) { M_CLEAR_FLAG(gUsbDevice.flag_change);#ifdef _USB_DEBUG DBG_PRINTF("usb: Device status changed\r\n");#endif /*_USB_DEBUG*/ /* in the case of device is disconnected */#ifdef _B32_BACK_COMPATIBILITY if( (gUsbDevice.bVolCount == 0) || (!(gUsbDevice.bMediumPresence)) )#else /*_B32_BACK_COMPATIBILITY*/ if(gUsbDevice.bVolCount == 0)#endif /*_B32_BACK_COMPATIBILITY*/ { USB_StopEnumTimer(); //need_to_reinit = TRUE; /* PATCH - to ensure that LS device will be recognized */ gUsbDevice.bReady = FALSE; /* task state handling */ if(usb_fsm_xfer.state == FSM_RUN) { usb_fsm_xfer.state = FSM_ERROR; usb_fsm_xfer.error_reason = E_USB_FORCED_UNPLUG; }#if (1 == HAVE_WMDRM) if(usb_fsm_mtp.state == FSM_RUN) { usb_fsm_mtp.state = FSM_ERROR; usb_fsm_mtp.error_reason = E_USB_FORCED_UNPLUG; }#endif#ifdef _B32_BACK_COMPATIBILITY#if ((0!=IPOD_PASSTHROUGH) || (0!=IPOD_AP)) //[LL]new /* events handling */ if( (gUsbDevice.LastDevInfo.vid == C_USB_IPOD_VID) && (gUsbDevice.LastDevInfo.pid & C_USB_IPOD_PID_MASK) ) { out_event = USB_DISCONNECT_IPOD; DBG_PRINTF("usb: Ipod REMOVED\r\n"); } else#endif /*IPOD_PASSTHROUGH || IPOD_AP*/#endif /*_B32_BACK_COMPATIBILITY*/ { out_event = USB_DISCONNECT;//#ifdef _USB_DEBUG DBG_PRINTF("usb: Device DISCONNECTED\r\n");//#endif /*_USB_DEBUG*/ } usb_fsm_background.state = FSM_DONE; /* PATCH - to ensure that LS device will be recognized */ if (gUsbDevice.bProcessing) { /* device must be disconnected! */ DBG_PRINTF("usb: *W - state handling: Some processing is/was pending\r\n"); }#ifdef _B32_BACK_COMPATIBILITY if(gUsbDevice.bVolCount == 0)#endif /*_B32_BACK_COMPATIBILITY*/ /*!!! USBtask is going to be reinitialized in order to pass LS device usb certification tests */ if(gUsbOn == TRUE) { USB_ReinitializeUsb(); } } else {#ifdef _B32_BACK_COMPATIBILITY#if ((0!=IPOD_PASSTHROUGH) || (0!=IPOD_AP)) //[LL]new if( (gUsbDevice.LastDevInfo.vid == C_USB_IPOD_VID) && (gUsbDevice.LastDevInfo.pid & C_USB_IPOD_PID_MASK) ) { pUsbIpodVolume = (tUsbIpodVolume*)USB_GetVolume(S_USB_TYPE_IPOD, S_USB_GET_FIRST, &zero); if(pUsbIpodVolume) { pout_event->Info.block_size = 0; pout_event->Info.wVendorId = pUsbIpodVolume->DeviceInfo.wVendorId; pout_event->Info.wProductId = pUsbIpodVolume->DeviceInfo.wProductId; pout_event->Info.bcdDevice = pUsbIpodVolume->DeviceInfo.bcdDevice; out_event = USB_MOUNT_IPOD; DBG_PRINTF("usb: iPod MOUNTED\r\n"); } } else#endif /*IPOD_PASSTHROUGH || IPOD_AP*/ { pUsbMsdVolume = (tUsbMsdVolume*)USB_GetVolume(S_USB_TYPE_MSD, S_USB_GET_FIRST, &zero); if(pUsbMsdVolume) { pout_event->Info.block_size = pUsbMsdVolume->DeviceInfo.dwBlockSize; pout_event->Info.wVendorId = pUsbMsdVolume->DeviceInfo.wVendorId; pout_event->Info.wProductId = pUsbMsdVolume->DeviceInfo.wProductId; pout_event->Info.bcdDevice = pUsbMsdVolume->DeviceInfo.bcdDevice; out_event = USB_MOUNT; DBG_PRINTF("usb: MSD Volume MOUNTED\r\n"); }#if (1 == HAVE_WMDRM) else { zero = 0; pUsbMtpVolume = (tUsbMtpVolume*)USB_GetVolume(S_USB_TYPE_MTP, S_USB_GET_FIRST, &zero); if(pUsbMtpVolume) { pout_event->Info.block_size = pUsbMtpVolume->MtpDeviceInfo.dwBlockSize; pout_event->Info.wVendorId = pUsbMtpVolume->MtpDeviceInfo.wVendorId; pout_event->Info.wProductId = pUsbMtpVolume->MtpDeviceInfo.wProductId; pout_event->Info.bcdDevice = pUsbMtpVolume->MtpDeviceInfo.bcdDevice; out_event = USB_MOUNT; DBG_PRINTF("usb: MTP Volume MOUNTED\r\n"); } }#endif }#else /*_B32_BACK_COMPATIBILITY*/ gUsbDevice.bReady = TRUE; /* if 'change' != 'disconection' then volumes are present */ out_event = USB_CHANGE;#endif /*_B32_BACK_COMPATIBILITY*/ } //return is_awaited; break; } /* to let the parent know usb device is physically disconnected - only if no volume is active ! */ if(M_IS_SET(gUsbDevice.flag_disconnect)) { M_CLEAR_FLAG(gUsbDevice.flag_disconnect); //USB_StopEnumTimer(); if(!(gUsbDevice.bVolCount)) { USB_StopEnumTimer(); DBG_PRINTF("usb: Device DISCONNECTED\r\n"); /* events handling */ usb_fsm_background.state = FSM_DONE; out_event = USB_DISCONNECT; } break; } /* to let the parent know usb device is physically connected */ if(M_IS_SET(gUsbDevice.flag_connect)) { M_CLEAR_FLAG(gUsbDevice.flag_connect);//#ifdef _USB_DEBUG DBG_PRINTF("usb: Device CONNECTED\r\n");//#endif /*_USB_DEBUG*/ USB_StartEnumTimer(C_USB_ENUM_TIM_EXPIRED); /* events handling */ out_event = USB_CONNECT; //return is_awaited; break; } /* to let the parent know if HUB is not supported */ if(M_IS_SET(gUsbDevice.flag_hub_unsupported)) { M_CLEAR_FLAG(gUsbDevice.flag_hub_unsupported); DBG_PRINTF("usb: *W - Hub not supported\r\n"); /*_USB_HUB*/ if (APP_HUB_ON == FALSE) { USB_StopEnumTimer(); /* for hubs we need reinitialize for sure! */ } /* events handling */ out_event = USB_UNSUPPORTED_HUB; //return is_awaited; break; } if(M_IS_SET(gUsbDevice.flag_unsupported)) { M_CLEAR_FLAG(gUsbDevice.flag_unsupported); USB_StopEnumTimer();#ifdef _B32_BACK_COMPATIBILITY#if ((0!=IPOD_PASSTHROUGH) || (0!=IPOD_AP)) //[LL]new /* events handling */ if( (gUsbDevice.LastDevInfo.vid == C_USB_IPOD_VID) && (gUsbDevice.LastDevInfo.pid & C_USB_IPOD_PID_MASK) ) { out_event = USB_UNSUPPORTED_IPOD; DBG_PRINTF("usb: *W - Ipod not supported\r\n"); } else#endif /*IPOD_PASSTHROUGH || IPOD_AP*/#endif /*_B32_BACK_COMPATIBILITY*/ { out_event = USB_UNSUPPORTED; DBG_PRINTF("usb: *W - Device not supported\r\n"); } //return is_awaited; break; } } while(0); if(out_event) { pout_event->USB_state = out_event; /* copy to event_out */ event_set_out(USB_STATUS_EVENT);// /* task state handling */// usb_fsm_background.state = FSM_DONE; /* (be aware of the task switching) */ return is_awaited; //TBD - ?? } while (count<C_USB_VOLUMES_COUNT) { tUsbVolume* pUsbVolume = gUsbDevice.apUsbVolumes[count]; if(pUsbVolume) { switch (pUsbVolume->GenVolume.Type) { case S_USB_TYPE_MSD: pUsbMsdVolume = &(pUsbVolume->MsdVolume); pLastReadParams = &(pUsbMsdVolume->ReadParams); if(M_IS_SET(pUsbMsdVolume->flag_data_in)) { /* if USB transfer finished */ M_CLEAR_FLAG(pUsbMsdVolume->flag_data_in); if(pLastReadParams->ReadErrorCounter == 0) { /* task state handling */ usb_fsm_background.state = FSM_DONE; } else { /* if strange number of blocks have been received (=? error) */ DBG_PRINTF("usb: *E - Read error, attempt no.%d\r\n", pLastReadParams->ReadErrorCounter); if(pLastReadParams->ReadErrorCounter >= C_USB_READ_ERROR_COUNT) { /* don't try again - > error */ pLastReadParams->ReadErrorCounter = 0; /* task state handling */ usb_fsm_background.state = FSM_ERROR; //TBD - could be general FSM states usb_fsm_background.error_reason = E_USB_READ; is_awaited = S_USB_FSM_END; //MUSB_MsdCheckMedium(pUsbMsdDevice); /* medium could be removed */ //dangerous } else { /* try C_USB_READ_ERROR_COUNT times */ USB_Read(pUsbMsdVolume, pLastReadParams->dwStartBlockLo, pLastReadParams->dwStartBlockHi, pLastReadParams->wBlockCount, pLastReadParams->pReadBuffer); //TBD - result? } } return is_awaited; } break;#if ((0!=IPOD_PASSTHROUGH) || (0!=IPOD_AP)) //[LL]new case S_USB_TYPE_IPOD: pUsbIpodVolume = &(pUsbVolume->IpodVolume); /* higher priority than OUT */ if(M_IS_SET(pUsbIpodVolume->flag_data_in)) { /* input data processed */ M_CLEAR_FLAG(pUsbIpodVolume->flag_data_in); USB_PassthroughUpstreamTransition(&(pUsbIpodVolume->Mode.UsbPassthrough)); //TBD returns something? return is_awaited; } if(M_IS_SET(pUsbIpodVolume->flag_data_out)) { /* output data processed */ M_CLEAR_FLAG(pUsbIpodVolume->flag_data_out); /* events handling */ local_usb_downstream_status.DataTag = local_usb_downstream_cmd.DataTag; local_usb_downstream_status.bStatus = USB_OK;#ifdef _USB_DEBUG DBG_PRINTF("U:Dst\r\n");#endif usb_downstream_status(&local_usb_downstream_status); return is_awaited; } break;#endif /*IPOD_PASSTHROUGH || IPOD_AP*/#if (1 == HAVE_WMDRM) case S_USB_TYPE_MTP: pUsbMtpVolume = &(pUsbVolume->MtpVolume); /* higher priority than OUT */ if(M_IS_SET(pUsbMtpVolume->flag_data_ready)) { /* input data processed */ M_CLEAR_FLAG(pUsbMtpVolume->flag_data_ready); /* task state handling */ usb_fsm_background.state = FSM_DONE; return is_awaited; }#endif default: DBG_PRINTF("usb: *W - state_handling: Unknown usb volume type!\r\n"); } } if(out_event) break; count++; if(count == C_USB_VOLUMES_COUNT) { count = 0; break; } } if(out_event) { pout_event->USB_state = out_event; /* copy to event_out */ event_set_out(USB_STATUS_EVENT);// /* task state handling */// usb_fsm_background.state = FSM_DONE; /* (be aware of the task switching) */ return is_awaited; //TBD - ?? } return S_USB_FSM_END;}/*********************** Usb task interface functions ********
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -