1:
9:
10:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
#include "kernel.h"
24:
#include "uhs_configtop.h"
25:
26:
#include "usb_msc_coreinclude.h"
27:
28:
#include "usb_msc_api.h"
29:
#include "usb_msc_internal_api.h"
30:
#include "usb_msc_bot.h"
31:
#include "usb_msc_scsierror.h"
32:
#include "usb_msc_scsictrl.h"
33:
#include "usb_msc_param.h"
34:
#include "usb_msc_log.h"
35:
36:
37:
38:
39:
#ifdef USBH_STACK_DEBUG_BUILD
40:
static const char g_filename[] = __FILE__;
41:
#endif
42:
43:
#ifdef USBH_STACK_DEBUG_BUILD
44:
#define API_REPORT_SUMMARY(err) Usbh_Stor_Log_ReportError( g_filename, __LINE__, (err), (uhs_ubit8_t*)"1", 0 )
45:
#define API_REPORT_WARNING_SUMMARY(warning) Usbh_Stor_Log_ReportWarning( g_filename, __LINE__, (warning), (uhs_ubit8_t*)"2", 0 )
46:
#define API_REPORT_DETAIL(err, mesg, len) Usbh_Stor_Log_ReportError( g_filename, __LINE__, (err), (mesg), (len) )
47:
#define API_REPORT_MILESTONE(num) Usbh_Stor_Log_ReportMilestone((num))
48:
#else
49:
#define API_REPORT_SUMMARY(err) Usbh_Stor_Log_ReportDummy(err)
50:
#define API_REPORT_WARNING_SUMMARY(warning) Usbh_Stor_Log_ReportDummy(warning)
51:
#define API_REPORT_DETAIL(err, mesg, len) Usbh_Stor_Log_ReportDummy(err)
52:
#define API_REPORT_MILESTONE(num) Usbh_Stor_Log_ReportMilestone((num))
53:
#endif
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
typedef struct {
64: uhs_sbit32_t s32Context;
65:
PUSER_DRV pUserDrv;
66:
PSTOR_UNIT pStorUnit;
67: } DRIVE_TABLE;
68:
69:
70:
71:
72:
static int Usbh_Stor_Api_AttachStorUnit(
STOR_UNIT *pNewStorUnit);
73:
static int Usbh_Stor_Api_DetachStorUnit(
STOR_UNIT *pDelStorUnit);
74:
static uhs_boolean_t Usbh_Stor_Api_IsStorUnitObject(
PSTOR_UNIT pStorUnit);
75:
static PSTOR_UNIT Usbh_Stor_Api_GetAssignStorUnit(
PUSER_DRV pUserDrv);
76:
static uhs_ubit32_t Usbh_Stor_ApiLog_ConvertUsbBotStatToUsbStorStat(USB_BOT_STATUS nBotStat);
77:
static uhs_sbit32_t Usbh_Stor_Api_ConvertUsbScsiStatToUsbStorStat(USB_SCSI_STATUS nScsiStat);
78:
static uhs_ubit32_t Usbh_Stor_ApiLog_ConvertUsbScsiStatToUsbStorStat(USB_SCSI_STATUS nScsiStat);
79:
static uhs_sbit32_t Usbh_Stor_SetLastError(
STOR_UNIT *pUnit, uhs_sbit32_t s32Error);
80:
static PSTOR_UNIT Usbh_Stor_GetStorUnit(uhs_sbit32_t s32Context);
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
static STOR_CLASS_DRIVER *pStorClassDriver = NULL;
98:
static DRIVE_TABLE stDriveTable[MAX_DRIVE];
99:
static uhs_ubit8_t u8MntTblPointer = 0;
100:
101:
102:
int g_mscapp_attach = 0;
103:
int g_mscapp_detach = 0;
104:
105:
106:
107:
108:
109:
124: uhs_status_t
Usbh_Stor_Init(
app_callback_info* stCallback)
125: {
126: uhs_ubit8_t i;
127: USB_BOT_STATUS nBotRet;
128: USB_SCSI_STATUS nScsiRet;
129:
130: API_REPORT_MILESTONE( USBSTOR_CHKPOINTS_API_INITIALIZE );
131:
132:
133:
if( stCallback->clsdrv_type != USB_CLASS_MASSSTOR ) {
134: API_REPORT_SUMMARY( USBSTOR_LOG_SYS_SYSTEM_ERROR );
135:
return UDI_STAT_DATA_ERROR;
136: }
137:
138:
139:
for( i = 0; i < (uhs_ubit8_t) numof(stDriveTable); i++ ) {
140: stDriveTable[i].s32Context = (CONTEXT_BASE + i);
141: stDriveTable[i].pUserDrv = NULL;
142: stDriveTable[i].pStorUnit = NULL;
143: }
144:
145:
146: pStorClassDriver = Usbh_Stor_CreateStorClassDriver( stCallback->attach_callback,
147: stCallback->detach_callback,
148: Usbh_Stor_Api_AttachStorUnit,
149: Usbh_Stor_Api_DetachStorUnit,
150: Usbh_Stor_ScsiCtrl_AttachStorUnit,
151: Usbh_Stor_ScsiCtrl_DetachStorUnit );
152:
if( pStorClassDriver == NULL ) {
153: API_REPORT_SUMMARY( USBSTOR_LOG_SYS_SYSTEM_ERROR );
154:
return UDI_STAT_DATA_ERROR;
155: }
156:
157:
158:
if( (nBotRet = Usbh_Stor_Bot_Initialize(pStorClassDriver)) != USB_BOT_NORMAL ) {
159:
160: Usbh_Stor_DeleteStorClassDriver( pStorClassDriver );
161: pStorClassDriver = NULL;
162:
163: API_REPORT_SUMMARY( Usbh_Stor_ApiLog_ConvertUsbBotStatToUsbStorStat(nBotRet) );
164:
return UDI_STAT_DATA_ERROR;
165: }
166:
167:
168:
if( (nScsiRet = Usbh_Stor_ScsiCtrl_Initialize()) != USB_SCSI_NORMAL ) {
169:
170: Usbh_Stor_Bot_Finalize( pStorClassDriver );
171: Usbh_Stor_DeleteStorClassDriver( pStorClassDriver );
172: pStorClassDriver = NULL;
173:
174: API_REPORT_SUMMARY( Usbh_Stor_ApiLog_ConvertUsbScsiStatToUsbStorStat(nScsiRet) );
175:
return UDI_STAT_DATA_ERROR;
176: }
177:
178:
return UDI_OK;
179: }
180:
181:
191: uhs_status_t
Usbh_Stor_Exit(
void* stCallback)
192: {
193:
194: ((
void)stCallback);
195:
196: API_REPORT_MILESTONE( USBSTOR_CHKPOINTS_API_FINALIZE );
197:
198:
199:
if( pStorClassDriver == NULL ) {
200: API_REPORT_SUMMARY( USBSTOR_LOG_SYS_SYSTEM_ERROR );
201:
return UDI_STAT_DATA_ERROR;
202: }
203:
204:
205: Usbh_Stor_ScsiCtrl_Finalize();
206:
207:
208: Usbh_Stor_Bot_Finalize( pStorClassDriver );
209:
210:
211: Usbh_Stor_DeleteStorClassDriver( pStorClassDriver );
212: pStorClassDriver = NULL;
213:
214:
return UDI_OK;
215: }
216:
217:
227: uhs_status_t
Usbh_Stor_ReregisterCallback(
app_callback_info* stCallback)
228: {
229: API_REPORT_MILESTONE( USBSTOR_CHKPOINTS_API_REREGISTCB );
230:
231:
232:
if( pStorClassDriver == NULL ) {
233: API_REPORT_SUMMARY( USBSTOR_LOG_SYS_SYSTEM_ERROR );
234:
return UDI_STAT_DATA_ERROR;
235: }
236:
237:
238: pStorClassDriver->pUserAttachCallback = stCallback->attach_callback;
239: pStorClassDriver->pUserDetachCallback = stCallback->detach_callback;
240:
241:
return UDI_OK;
242: }
243:
244:
259: uhs_sbit32_t
Usbh_Stor_Mount(
PSTOR_UNIT pUnit,
PUSER_DRV pDrv)
260: {
261: uhs_ubit8_t i;
262:
263: API_REPORT_MILESTONE( USBSTOR_CHKPOINTS_API_MOUNT );
264:
265:
266:
if( pStorClassDriver == NULL ) {
267: API_REPORT_SUMMARY( USBSTOR_LOG_SYS_SYSTEM_ERROR );
268:
return USBSTOR_SYS_SYSTEM_ERROR;
269: }
270:
271:
272: pUnit = Usbh_Stor_GetStorUnit((uhs_sbit32_t)pUnit);
273:
274:
275:
if( (pUnit == NULL) || (pDrv == NULL) ||
276: (Usbh_Stor_Api_IsStorUnitObject(pUnit) == FALSE) ) {
277: API_REPORT_SUMMARY( USBSTOR_LOG_API_PARAM_ERROR );
278:
return Usbh_Stor_SetLastError( NULL,
USBSTOR_API_PARAM_ERROR );
279: }
280:
281:
282:
if( pUnit->nStorStat != STOR_STAT_READY ) {
283:
284: API_REPORT_SUMMARY( USBSTOR_LOG_API_PARAM_ERROR );
285:
return Usbh_Stor_SetLastError( pUnit,
USBSTOR_API_PARAM_ERROR );
286: }
287:
288:
289:
for( i = 0; i < (uhs_ubit8_t) numof(stDriveTable); i++ ) {
290:
if( stDriveTable[i].pStorUnit == pUnit ) {
291:
292:
if( stDriveTable[i].pUserDrv == NULL ) {
293: stDriveTable[i].pUserDrv = pDrv;
294:
return USBSTOR_SUCCESS;
295: }
else {
296:
297: API_REPORT_SUMMARY( USBSTOR_LOG_API_MOUNTED_UNIT );
298:
return Usbh_Stor_SetLastError( pUnit,
USBSTOR_API_MOUNTED_UNIT );
299: }
300: }
301: }
302:
303:
304: API_REPORT_SUMMARY( USBSTOR_LOG_API_PARAM_ERROR );
305:
return Usbh_Stor_SetLastError( NULL,
USBSTOR_API_PARAM_ERROR );
306: }
307:
308:
319: uhs_sbit32_t
Usbh_Stor_Unmount(
PUSER_DRV pDrv)
320: {
321: uhs_ubit8_t i;
322:
323: API_REPORT_MILESTONE( USBSTOR_CHKPOINTS_API_UNMOUNT );
324:
325:
326:
if( pStorClassDriver == NULL ) {
327: API_REPORT_SUMMARY( USBSTOR_LOG_SYS_SYSTEM_ERROR );
328:
return USBSTOR_SYS_SYSTEM_ERROR;
329: }
330:
331:
332:
if( pDrv == NULL ) {
333: API_REPORT_SUMMARY( USBSTOR_LOG_API_PARAM_ERROR );
334:
return Usbh_Stor_SetLastError( NULL,
USBSTOR_API_PARAM_ERROR );
335: }
336:
337:
338:
for( i = 0; i < (uhs_ubit8_t) numof(stDriveTable); i++ ) {
339:
if( stDriveTable[i].pUserDrv == pDrv ) {
340:
341: stDriveTable[i].pUserDrv = NULL;
342:
return USBSTOR_SUCCESS;
343: }
344: }
345:
346:
347: API_REPORT_SUMMARY( USBSTOR_LOG_API_UNMOUNT_DRIVE );
348:
return Usbh_Stor_SetLastError( NULL,
USBSTOR_API_UNMOUNT_DRIVE );
349: }
350:
351:
361:
PSTOR_UNIT Usbh_Stor_GetUnmountUnit(
void)
362: {
363: uhs_ubit8_t i;
364:
365: API_REPORT_MILESTONE( USBSTOR_CHKPOINTS_API_GETUNMOUNTUNIT );
366:
367:
368:
if( pStorClassDriver == NULL ) {
369: API_REPORT_SUMMARY( USBSTOR_LOG_SYS_SYSTEM_ERROR );
370:
return NULL;
371: }
372:
373: u8MntTblPointer = 0;
374:
375:
376:
while( u8MntTblPointer < (uhs_ubit8_t) numof(stDriveTable) ) {
377: i = u8MntTblPointer;
378: u8MntTblPointer++;
379:
if( (stDriveTable[i].pStorUnit != NULL) &&
380: (stDriveTable[i].pUserDrv == NULL) ) {
381:
382:
return (
PSTOR_UNIT)stDriveTable[i].s32Context;
383: }
384: }
385:
386: Usbh_Stor_SetLastError( NULL,
USBSTOR_API_NOT_FOUND_UNIT );
387:
return NULL;
388: }
389:
390:
408: uhs_sbit32_t
Usbh_Stor_GetUnitInfo(
PSTOR_UNIT pUnit,
STOR_UNIT_INFO *pInfo)
409: {
410: uhs_ubit8_t i;
411:
412: API_REPORT_MILESTONE( USBSTOR_CHKPOINTS_API_GETUNITINFO );
413:
414:
415:
if( pStorClassDriver == NULL ) {
416: API_REPORT_SUMMARY( USBSTOR_LOG_SYS_SYSTEM_ERROR );
417:
return USBSTOR_SYS_SYSTEM_ERROR;
418: }
419:
420:
421: pUnit = Usbh_Stor_GetStorUnit((uhs_sbit32_t)pUnit);
422:
423:
424:
if( (pUnit == NULL) || (pInfo == NULL) ||
425: (Usbh_Stor_Api_IsStorUnitObject(pUnit) == FALSE) ) {
426: API_REPORT_SUMMARY( USBSTOR_LOG_API_PARAM_ERROR );
427:
return Usbh_Stor_SetLastError( NULL,
USBSTOR_API_PARAM_ERROR );
428: }
429:
430:
431: pInfo->u16BlockSize = pUnit->u16BlockSize;
432: pInfo->u32LastBlockAddress = pUnit->u32LastBlockAddress;
433: pInfo->bRemovable = pUnit->bRemovable;
434:
for( i = 0; i < (uhs_ubit8_t)
sizeof(pInfo->u8VendorName); i++) {
435:
436: pInfo->u8VendorName[i] = pUnit->u8VendorName[i];
437: }
438:
for( i = 0; i < (uhs_ubit8_t)
sizeof(pInfo->u8ProductName); i++) {
439:
440: pInfo->u8ProductName[i] = pUnit->u8ProductName[i];
441: }
442:
return USBSTOR_SUCCESS;
443: }
444:
445:
455: STOR_UNIT_STATUS
Usbh_Stor_GetUnitStatus(
PSTOR_UNIT pUnit)
456: {
457: API_REPORT_MILESTONE( USBSTOR_CHKPOINTS_API_GETUNITSTATUS );
458:
459:
460:
if( pStorClassDriver == NULL ) {
461: API_REPORT_SUMMARY( USBSTOR_LOG_SYS_SYSTEM_ERROR );
462:
return STOR_NOT_USE;
463: }
464:
465:
466: pUnit = Usbh_Stor_GetStorUnit((uhs_sbit32_t)pUnit);
467:
468:
469:
if( (pUnit == NULL) ||
470: (Usbh_Stor_Api_IsStorUnitObject(pUnit) == FALSE) ) {
471: API_REPORT_SUMMARY( USBSTOR_LOG_API_PARAM_ERROR );
472: Usbh_Stor_SetLastError( NULL,
USBSTOR_API_PARAM_ERROR );
473:
return STOR_NOT_USE;
474: }
475:
476:
477:
if( Usbh_Stor_ScsiCtrl_AttachStorUnit(pUnit) == FALSE ) {
478: Usbh_Stor_SetLastError( pUnit,
USBSTOR_API_UNIT_NOT_USE );
479:
return STOR_NOT_USE;
480: }
481:
482:
483:
switch( pUnit->nStorStat ) {
484:
case STOR_STAT_READY:
485:
486:
return STOR_READY;
487:
case STOR_STAT_NOT_READY:
488:
489: Usbh_Stor_SetLastError( pUnit,
USBSTOR_API_UNIT_NOT_READY );
490:
return STOR_NOT_READY;
491:
default:
492:
493: Usbh_Stor_SetLastError( pUnit,
USBSTOR_API_UNIT_NOT_USE );
494:
return STOR_NOT_USE;
495: }
496: }
497:
498:
528: uhs_sbit32_t
Usbh_Stor_ReadSector(
PUSER_DRV pDrv, uhs_ubit32_t u32LBA, uhs_ubit16_t u16BlockCount, uhs_ubit8_t *pDataBuffer)
529: {
530:
STOR_UNIT *pStorUnit;
531: USB_SCSI_STATUS nScsiStat;
532: uhs_sbit16_t s16Status;
533:
534:
#if STOR_RW_MILESTONE_ERA == TRUE
535: API_REPORT_MILESTONE( USBSTOR_CHKPOINTS_API_READSECTOR );
536:
#endif
537:
538:
539:
if( pStorClassDriver == NULL ) {
540: API_REPORT_SUMMARY( USBSTOR_LOG_SYS_SYSTEM_ERROR );
541:
return USBSTOR_SYS_SYSTEM_ERROR;
542: }
543:
544:
545:
if( pDrv == NULL ) {
546: API_REPORT_SUMMARY( USBSTOR_LOG_API_PARAM_ERROR );
547:
return Usbh_Stor_SetLastError( NULL,
USBSTOR_API_PARAM_ERROR );
548: }
549:
if( ((pStorUnit = Usbh_Stor_Api_GetAssignStorUnit(pDrv)) == NULL) ||
550: (u16BlockCount == 0) ||
551: (pDataBuffer == NULL) ) {
552: API_REPORT_SUMMARY( USBSTOR_LOG_API_PARAM_ERROR );
553:
return Usbh_Stor_SetLastError( NULL,
USBSTOR_API_PARAM_ERROR );
554: }
555:
if( pStorUnit->u32LastBlockAddress < u32LBA ) {
556: API_REPORT_SUMMARY( USBSTOR_LOG_API_PARAM_ERROR );
557:
return Usbh_Stor_SetLastError( NULL,
USBSTOR_API_PARAM_ERROR );
558: }
559:
560:
561: nScsiStat = Usbh_Stor_ScsiCtrl_ReadSector( pStorUnit, u32LBA, u16BlockCount, pDataBuffer );
562: s16Status = Usbh_Stor_Api_ConvertUsbScsiStatToUsbStorStat( nScsiStat );
563:
if( s16Status !=
USBSTOR_SUCCESS ) {
564: API_REPORT_SUMMARY( Usbh_Stor_ApiLog_ConvertUsbScsiStatToUsbStorStat( nScsiStat ) );
565: Usbh_Stor_SetLastError( pStorUnit, s16Status );
566: }
567:
return s16Status;
568: }
569:
570:
600: uhs_sbit32_t
Usbh_Stor_WriteSector(
PUSER_DRV pDrv, uhs_ubit32_t u32LBA, uhs_ubit16_t u16BlockCount, uhs_ubit8_t *pDataBuffer)
601: {
602:
STOR_UNIT *pStorUnit;
603: USB_SCSI_STATUS nScsiStat;
604: uhs_sbit16_t s16Status;
605:
606:
#if STOR_RW_MILESTONE_ERA == TRUE
607: API_REPORT_MILESTONE( USBSTOR_CHKPOINTS_API_WRITESECTOR );
608:
#endif
609:
610:
611:
if( pStorClassDriver == NULL ) {
612: API_REPORT_SUMMARY( USBSTOR_LOG_SYS_SYSTEM_ERROR );
613:
return USBSTOR_SYS_SYSTEM_ERROR;
614: }
615:
616:
617:
if( pDrv == NULL ) {
618:
return Usbh_Stor_SetLastError( NULL,
USBSTOR_API_PARAM_ERROR );
619: }
620:
if( ((pStorUnit = Usbh_Stor_Api_GetAssignStorUnit(pDrv)) == NULL) ||
621: (u16BlockCount == 0) ||
622: (pDataBuffer == NULL) ) {
623:
return Usbh_Stor_SetLastError( NULL,
USBSTOR_API_PARAM_ERROR );
624: }
625:
if( pStorUnit->u32LastBlockAddress < u32LBA ) {
626: API_REPORT_SUMMARY( USBSTOR_LOG_API_PARAM_ERROR );
627:
return Usbh_Stor_SetLastError( NULL,
USBSTOR_API_PARAM_ERROR );
628: }
629:
630:
631: nScsiStat = Usbh_Stor_ScsiCtrl_WriteSector( pStorUnit, u32LBA, u16BlockCount, pDataBuffer );
632: s16Status = Usbh_Stor_Api_ConvertUsbScsiStatToUsbStorStat( nScsiStat );
633:
if( s16Status !=
USBSTOR_SUCCESS ) {
634: API_REPORT_SUMMARY( Usbh_Stor_ApiLog_ConvertUsbScsiStatToUsbStorStat( nScsiStat ) );
635: Usbh_Stor_SetLastError( pStorUnit, s16Status );
636: }
637:
return s16Status;
638: }
639:
640:
651: uhs_sbit32_t
Usbh_Stor_GetLastError(
void *pObj)
652: {
653: uhs_sbit32_t s32Error;
654:
655:
656:
if( pStorClassDriver == NULL ) {
657: API_REPORT_SUMMARY( USBSTOR_LOG_SYS_SYSTEM_ERROR );
658:
return USBSTOR_SYS_SYSTEM_ERROR;
659: }
660:
661:
662:
if( pObj == NULL ) {
663: s32Error = pStorClassDriver->s32LastError;
664: pStorClassDriver->s32LastError =
USBSTOR_SUCCESS;
665:
return s32Error;
666: }
else {
667: uhs_ubit8_t i;
668:
for( i = 0; i < (uhs_ubit8_t) numof(stDriveTable); i++ ) {
669:
if( ((
void*) stDriveTable[i].s32Context == pObj) ||
670: ((
void*) stDriveTable[i].pUserDrv == pObj) ) {
671:
672: s32Error = stDriveTable[i].pStorUnit->s32LastError;
673: stDriveTable[i].pStorUnit->s32LastError = 0;
674:
return s32Error;
675: }
676: }
677:
678: API_REPORT_SUMMARY( USBSTOR_LOG_API_PARAM_ERROR );
679:
return 0;
680: }
681: }
682:
683:
684:
692:
static uhs_sbit32_t Usbh_Stor_SetLastError(
STOR_UNIT *pUnit, uhs_sbit32_t s32Error)
693: {
694:
695:
if( pStorClassDriver == NULL ) {
696: API_REPORT_SUMMARY( USBSTOR_LOG_SYS_SYSTEM_ERROR );
697:
return( s32Error );
698: }
699:
700:
if( Usbh_Stor_Api_IsStorUnitObject(pUnit) == FALSE ) {
701: pStorClassDriver->s32LastError = s32Error;
702: }
else {
703:
if( pUnit != NULL){
704: pUnit->s32LastError = s32Error;
705: }
706: }
707:
return( s32Error );
708: }
709:
710:
711:
718:
static int Usbh_Stor_Api_AttachStorUnit(
STOR_UNIT *pNewStorUnit)
719: {
720: uhs_sbit32_t s32Context = 0;
721: uhs_ubit8_t i;
722:
int iId;
723:
724:
725:
if( pStorClassDriver == NULL ) {
726:
return USBH_CBRTN_NG;
727: }
728:
729:
if( pNewStorUnit == NULL ) {
730:
731: iId = CBID_STOR_DEVATTACH_NOT_USE;
732: Usbh_Stor_SetLastError( NULL,
USBSTOR_API_UNIT_NOT_USE );
733: }
else {
734:
735:
for( i = 0; i < (uhs_ubit8_t) numof(stDriveTable); i++ ) {
736:
if( stDriveTable[i].pStorUnit == NULL ) {
737:
738: stDriveTable[i].pStorUnit = pNewStorUnit;
739: u8MntTblPointer = 0;
740:
741: s32Context = stDriveTable[i].s32Context;
742:
break;
743: }
744: }
745:
746:
747:
switch( pNewStorUnit->nStorStat ) {
748:
case STOR_STAT_READY:
749: iId = CBID_STOR_DEVATTACH;
750:
break;
751:
case STOR_STAT_NOT_READY:
752: iId = CBID_STOR_DEVATTACH_NOT_READY;
753: Usbh_Stor_SetLastError( NULL,
USBSTOR_API_UNIT_NOT_READY );
754:
break;
755:
default:
756: iId = CBID_STOR_DEVATTACH_NOT_USE;
757: Usbh_Stor_SetLastError( NULL,
USBSTOR_API_UNIT_NOT_USE );
758:
break;
759: }
760: }
761:
762:
763:
if( pStorClassDriver->pUserAttachCallback != NULL ) {
764:
765: g_mscapp_attach = 1;
766: pStorClassDriver->pUserAttachCallback(
MOD_ID_CLASSDRV_STOR, iId, (
PSTOR_UNIT)s32Context );
767:
768: g_mscapp_attach = 0;
769: }
770:
771:
return USBH_CBRTN_OK;
772: }
773:
774:
781:
static int Usbh_Stor_Api_DetachStorUnit(
STOR_UNIT *pDelStorUnit)
782: {
783:
void* pContext = NULL;
784: uhs_ubit8_t i;
785:
786:
787:
if( pStorClassDriver == NULL ) {
788:
return USBH_CBRTN_NG;
789: }
790:
791:
792:
for( i = 0; i < (uhs_ubit8_t) numof(stDriveTable); i++ ) {
793:
if( stDriveTable[i].pStorUnit == pDelStorUnit ) {
794:
795: pContext = (
void *)stDriveTable[i].s32Context;
796:
797: stDriveTable[i].pStorUnit = NULL;
798:
break;
799: }
800: }
801:
802:
803:
if( pStorClassDriver->pUserDetachCallback != NULL ) {
804:
805: g_mscapp_detach = 1;
806: pStorClassDriver->pUserDetachCallback(
MOD_ID_CLASSDRV_STOR,
807: CBID_STOR_DEVDETACH,
808: pContext );
809:
810: g_mscapp_detach = 0;
811: }
812:
813:
return USBH_CBRTN_OK;
814: }
815:
816:
823:
static uhs_boolean_t Usbh_Stor_Api_IsStorUnitObject(
PSTOR_UNIT pStorUnit)
824: {
825: uhs_ubit8_t i;
826:
827:
if( pStorUnit == NULL ) {
828:
return FALSE;
829: }
830:
831:
for( i = 0; i < (uhs_ubit8_t) numof(stDriveTable); i++ ) {
832:
if( stDriveTable[i].pStorUnit == pStorUnit ) {
833:
834:
return TRUE;
835: }
836: }
837:
838:
839:
return FALSE;
840: }
841:
842:
849:
static PSTOR_UNIT Usbh_Stor_Api_GetAssignStorUnit(
PUSER_DRV pUserDrv)
850: {
851: uhs_ubit8_t i;
852:
853:
for( i = 0; i < (uhs_ubit8_t) numof(stDriveTable); i++ ) {
854:
if( stDriveTable[i].pUserDrv == pUserDrv ) {
855:
856:
return stDriveTable[i].pStorUnit;
857: }
858: }
859:
860:
861: API_REPORT_SUMMARY( USBSTOR_LOG_API_ILLIGAL_DRIVE );
862:
return NULL;
863: }
864:
865:
872:
static uhs_ubit32_t Usbh_Stor_ApiLog_ConvertUsbBotStatToUsbStorStat(USB_BOT_STATUS nBotStat)
873: {
874:
typedef struct {
875: USB_BOT_STATUS nBotStat;
876: uhs_ubit32_t u32StorStat;
877: } CONV_TABLE;
878:
static const CONV_TABLE stConvTbl[] = {
879: { USB_BOT_NORMAL,
USBSTOR_SUCCESS },
880: { USB_BOT_USB_HALT_PIPE, USBSTOR_LOG_MEDIUM_TRANSFER_ERROR },
881: { USB_BOT_USB_FAILED_BULK_MSG, USBSTOR_LOG_MEDIUM_TRANSFER_ERROR },
882: { USB_BOT_FAILED_STDREQ_MSG, USBSTOR_LOG_MEDIUM_TRANSFER_ERROR },
883: { USB_BOT_FAILED_CLSREQ_MSG, USBSTOR_LOG_MEDIUM_TRANSFER_ERROR },
884: { USB_BOT_FAILED_BULK_MSG, USBSTOR_LOG_MEDIUM_TRANSFER_ERROR },
885: { USB_BOT_FAILED_CBW, USBSTOR_LOG_MEDIUM_TRANSFER_ERROR },
886: { USB_BOT_FAILED_DATA, USBSTOR_LOG_MEDIUM_TRANSFER_ERROR },
887: { USB_BOT_FAILED_CSW, USBSTOR_LOG_MEDIUM_TRANSFER_ERROR },
888: { USB_BOT_ILLIGAL_CSW, USBSTOR_LOG_MEDIUM_TRANSFER_ERROR },
889: { USB_BOT_FAILED_EXEC_CB, USBSTOR_LOG_MEDIUM_TRANSFER_ERROR },
890: { USB_BOT_PHASE_ERROR, USBSTOR_LOG_MEDIUM_TRANSFER_ERROR },
891: { USB_BOT_INTERNAL_FAILED_CORE, USBSTOR_LOG_INTERNAL_FAILED_CORE },
892: { USB_BOT_INTERNAL_FAILED_MSC, USBSTOR_LOG_INTERNAL_FAILED_MSC },
893: { USB_BOT_SYS_SYSTEM_ERROR, USBSTOR_LOG_SYS_SYSTEM_ERROR },
894: { USB_BOT_SYS_OUT_OF_RESOURCE, USBSTOR_LOG_SYS_OUT_OF_RESOURCE },
895: { USB_BOT_NOT_SUPPORT_MULTI_LUN,USBSTOR_LOG_SYS_OUT_OF_RESOURCE },
896: };
897: uhs_ubit8_t i;
898:
899:
900:
for( i = 0; i < (uhs_ubit8_t) numof(stConvTbl); i++ ) {
901:
if( stConvTbl[i].nBotStat == nBotStat ) {
902:
return stConvTbl[i].u32StorStat;
903: }
904: }
905:
906:
return USBSTOR_LOG_INTERNAL_FAILED_MSC;
907: }
908:
909:
916:
static uhs_sbit32_t Usbh_Stor_Api_ConvertUsbScsiStatToUsbStorStat(USB_SCSI_STATUS nScsiStat)
917: {
918:
typedef struct {
919: USB_SCSI_STATUS nScsiStat;
920: uhs_sbit32_t s32StorStat;
921: } CONV_TABLE;
922:
static const CONV_TABLE stConvTbl[] = {
923: { USB_SCSI_NORMAL,
USBSTOR_SUCCESS },
924: { USB_SCSI_MEDIUM_NOT_PRESENT,
USBSTOR_MEDIUM_NOT_PRESENT },
925: { USB_SCSI_WRITE_PROTECT,
USBSTOR_MEDIUM_WRITE_PROTECT },
926: { USB_SCSI_FAILED_WRITE,
USBSTOR_MEDIUM_FAILED_WRITE },
927: { USB_SCSI_FAILED_READ,
USBSTOR_MEDIUM_FAILED_READ },
928: { USB_SCSI_COMMAND_NOT_SUPPORT,
USBSTOR_MEDIUM_COMMAND_NOT_SUPPORT },
929: { USB_SCSI_PARAMETER_NOT_SUPPORT,
USBSTOR_MEDIUM_PARAMETER_NOT_SUPPORT },
930: { USB_SCSI_PARAMETER_ERROR,
USBSTOR_MEDIUM_PARAMETER_ERROR },
931: { USB_SCSI_COMMAND_ERROR,
USBSTOR_MEDIUM_COMMAND_ERROR },
932: { USB_SCSI_MEDIUM_MAY_HAVE_CHANGED,
USBSTOR_MEDIUM_MAY_HAVE_CHANGED },
933: { USB_SCSI_UNKNOWN_SENSE_ERROR,
USBSTOR_MEDIUM_UNKNOWN_SENSE_ERROR },
934: { USB_SCSI_TRANSFER_ERROR,
USBSTOR_MEDIUM_TRANSFER_ERROR },
935: { USB_SCSI_INTERNAL_FAILED_CORE,
USBSTOR_INTERNAL_FAILED_CORE },
936: { USB_SCSI_INTERNAL_FAILED_MSC,
USBSTOR_INTERNAL_FAILED_MSC },
937: { USB_SCSI_SYS_SYSTEM_ERROR,
USBSTOR_SYS_SYSTEM_ERROR },
938: { USB_SCSI_SYS_OUT_OF_RESOURCE,
USBSTOR_SYS_OUT_OF_RESOURCE }
939: };
940: uhs_ubit8_t i;
941:
942:
943:
for( i = 0; i < (uhs_ubit8_t) numof(stConvTbl); i++ ) {
944:
if( stConvTbl[i].nScsiStat == nScsiStat ) {
945:
return stConvTbl[i].s32StorStat;
946: }
947: }
948:
949:
return USBSTOR_INTERNAL_FAILED_MSC;
950: }
951:
952:
959:
static uhs_ubit32_t Usbh_Stor_ApiLog_ConvertUsbScsiStatToUsbStorStat(USB_SCSI_STATUS nScsiStat)
960: {
961:
typedef struct {
962: USB_SCSI_STATUS nScsiStat;
963: uhs_ubit32_t u32StorStat;
964: } CONV_TABLE;
965:
static const CONV_TABLE stConvTbl[] = {
966: { USB_SCSI_NORMAL,
USBSTOR_SUCCESS },
967: { USB_SCSI_MEDIUM_NOT_PRESENT, USBSTOR_LOG_MEDIUM_NOT_PRESENT },
968: { USB_SCSI_WRITE_PROTECT, USBSTOR_LOG_MEDIUM_WRITE_PROTECT },
969: { USB_SCSI_FAILED_WRITE, USBSTOR_LOG_MEDIUM_FAILED_WRITE },
970: { USB_SCSI_FAILED_READ, USBSTOR_LOG_MEDIUM_FAILED_READ },
971: { USB_SCSI_COMMAND_NOT_SUPPORT, USBSTOR_LOG_MEDIUM_COMMAND_NOT_SUPPORT },
972: { USB_SCSI_PARAMETER_NOT_SUPPORT, USBSTOR_LOG_MEDIUM_PARAMETER_NOT_SUPPORT },
973: { USB_SCSI_PARAMETER_ERROR, USBSTOR_LOG_MEDIUM_PARAMETER_ERROR },
974: { USB_SCSI_COMMAND_ERROR, USBSTOR_LOG_MEDIUM_COMMAND_ERROR },
975: { USB_SCSI_MEDIUM_MAY_HAVE_CHANGED, USBSTOR_LOG_MEDIUM_MAY_HAVE_CHANGED },
976: { USB_SCSI_UNKNOWN_SENSE_ERROR, USBSTOR_LOG_MEDIUM_UNKNOWN_SENSE_ERROR },
977: { USB_SCSI_TRANSFER_ERROR, USBSTOR_LOG_MEDIUM_TRANSFER_ERROR },
978: { USB_SCSI_INTERNAL_FAILED_CORE, USBSTOR_LOG_INTERNAL_FAILED_CORE },
979: { USB_SCSI_INTERNAL_FAILED_MSC, USBSTOR_LOG_INTERNAL_FAILED_MSC },
980: { USB_SCSI_SYS_SYSTEM_ERROR, USBSTOR_LOG_SYS_SYSTEM_ERROR },
981: { USB_SCSI_SYS_OUT_OF_RESOURCE, USBSTOR_LOG_SYS_OUT_OF_RESOURCE }
982: };
983: uhs_ubit8_t i;
984:
985:
986:
for( i = 0; i < (uhs_ubit8_t) numof(stConvTbl); i++ ) {
987:
if( stConvTbl[i].nScsiStat == nScsiStat ) {
988:
return stConvTbl[i].u32StorStat;
989: }
990: }
991:
992:
return USBSTOR_LOG_INTERNAL_FAILED_MSC;
993: }
994:
995:
1006: uhs_status_t
Usbh_Stor_GetVersion(uhs_ubit8_t* pu8MajorVersion,
1007: uhs_ubit8_t* pu8MinorVersion)
1008: {
1009:
1010:
1011:
1012:
if( (pu8MajorVersion == NULL) ||
1013: (pu8MinorVersion == NULL) ) {
1014: API_REPORT_SUMMARY( USBSTOR_LOG_API_PARAM_ERROR );
1015:
return Usbh_Stor_SetLastError( NULL,
USBSTOR_API_PARAM_ERROR );
1016: }
1017:
1018:
1019:
1020:
1021:
if( pStorClassDriver == NULL ) {
1022: API_REPORT_SUMMARY( USBSTOR_LOG_SYS_SYSTEM_ERROR );
1023:
return(
USBSTOR_SYS_SYSTEM_ERROR );
1024: }
1025:
1026:
1027: *pu8MajorVersion =
STOR_MAJOR_VERSION;
1028:
1029: *pu8MinorVersion =
STOR_MINOR_VERSION;
1030:
1031:
return(
USBSTOR_SUCCESS );
1032: }
1033:
1034:
1035:
1036:
1044: uhs_status_t Usbh_Stor_Api_StartStop(
PSTOR_UNIT pUnit,
1045:
PSTOR_STARTSTOP_UNIT_INFO pstData )
1046: {
1047: USB_SCSI_STATUS nScsiStat;
1048: uhs_sbit16_t s16Status;
1049:
1050: API_REPORT_MILESTONE( USBSTOR_CHKPOINTS_API_STARTSTOP );
1051:
1052:
1053:
if( pStorClassDriver == NULL ) {
1054: API_REPORT_SUMMARY( USBSTOR_LOG_SYS_SYSTEM_ERROR );
1055:
return STOR_NOT_USE;
1056: }
1057:
1058:
1059: pUnit = Usbh_Stor_GetStorUnit((uhs_sbit32_t)pUnit);
1060:
1061:
1062:
if( (pUnit == NULL) ||
1063: (Usbh_Stor_Api_IsStorUnitObject(pUnit) == FALSE) ) {
1064: API_REPORT_SUMMARY( USBSTOR_LOG_API_PARAM_ERROR );
1065: Usbh_Stor_SetLastError( NULL,
USBSTOR_API_PARAM_ERROR );
1066:
return STOR_NOT_USE;
1067: }
1068:
1069:
1070:
if( pstData == NULL ){
1071: API_REPORT_SUMMARY( USBSTOR_LOG_API_PARAM_ERROR );
1072:
return Usbh_Stor_SetLastError( NULL,
USBSTOR_API_PARAM_ERROR );
1073: }
1074:
if( (pstData->Immed != STOR_IMMED_CLR) &&
1075: (pstData->Immed != STOR_IMMED_SET) ){
1076: API_REPORT_SUMMARY( USBSTOR_LOG_API_PARAM_ERROR );
1077:
return Usbh_Stor_SetLastError( NULL,
USBSTOR_API_PARAM_ERROR );
1078: }
1079:
if( (pstData->LoEj != STOR_LOEJ_CLR) &&
1080: (pstData->LoEj != STOR_LOEJ_SET) ){
1081: API_REPORT_SUMMARY( USBSTOR_LOG_API_PARAM_ERROR );
1082:
return Usbh_Stor_SetLastError( NULL,
USBSTOR_API_PARAM_ERROR );
1083: }
1084:
if( (pstData->Start != STOR_START_CLR) &&
1085: (pstData->Start != STOR_START_SET) ){
1086: API_REPORT_SUMMARY( USBSTOR_LOG_API_PARAM_ERROR );
1087:
return Usbh_Stor_SetLastError( NULL,
USBSTOR_API_PARAM_ERROR );
1088: }
1089:
#if 0
1090: nScsiStat = Usbh_Stor_ScsiCmd_StartStopUnit(pUnit,(uhs_ubit8_t)pstData->Immed,(uhs_ubit8_t)pstData->LoEj,(uhs_ubit8_t)pstData->Start);
1091:
#else
1092: nScsiStat = Usbh_Stor_ScsiCtrl_StartStopUnit(pUnit,(uhs_ubit8_t)pstData->Immed,(uhs_ubit8_t)pstData->LoEj,(uhs_ubit8_t)pstData->Start);
1093:
#endif
1094: s16Status = Usbh_Stor_Api_ConvertUsbScsiStatToUsbStorStat( nScsiStat );
1095:
if( s16Status !=
USBSTOR_SUCCESS ) {
1096: API_REPORT_SUMMARY( Usbh_Stor_ApiLog_ConvertUsbScsiStatToUsbStorStat( nScsiStat ) );
1097: Usbh_Stor_SetLastError( pUnit, s16Status );
1098: }
1099:
return s16Status;
1100: }
1101:
1102:
1110:
int Usbh_Stor_GetAttachDevNum(
void)
1111: {
1112:
int iRet = 0;
1113: uhs_ubit8_t i;
1114:
1115:
1116:
for(i = 0; i < (uhs_ubit8_t) numof(stDriveTable); i++){
1117:
1118:
if(stDriveTable[i].pStorUnit != NULL){
1119: iRet++;
1120: }
1121: }
1122:
return iRet;
1123: }
1124:
1125:
1132:
static PSTOR_UNIT Usbh_Stor_GetStorUnit(uhs_sbit32_t s32Context)
1133: {
1134: uhs_ubit8_t i;
1135:
1136:
1137:
if(s32Context == 0) {
1138:
return NULL;
1139: }
1140:
1141:
1142:
for(i = 0; i < (uhs_ubit8_t) numof(stDriveTable); i++){
1143:
1144:
if(stDriveTable[i].s32Context == s32Context){
1145:
return stDriveTable[i].pStorUnit;
1146: }
1147: }
1148:
return NULL;
1149: }