@@ -239,6 +239,12 @@ bool tud_msc_set_sense(uint8_t lun, uint8_t sense_key, uint8_t add_sense_code, u
239
239
return true;
240
240
}
241
241
242
+ static inline void set_default_error_sense (uint8_t lun )
243
+ {
244
+ // default sense is NOT READY, MEDIUM NOT PRESENT
245
+ tud_msc_set_sense (lun , SCSI_SENSE_NOT_READY , 0x3A , 0x00 );
246
+ }
247
+
242
248
//--------------------------------------------------------------------+
243
249
// USBD Driver API
244
250
//--------------------------------------------------------------------+
@@ -406,7 +412,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
406
412
return false;
407
413
}
408
414
409
- TU_LOG (MSC_DEBUG , " SCSI Command: %s\r\n" , tu_lookup_find (& _msc_scsi_cmd_table , p_cbw -> command [0 ]));
415
+ TU_LOG (MSC_DEBUG , " SCSI Command [Lun%u] : %s\r\n" , p_cbw -> lun , tu_lookup_find (& _msc_scsi_cmd_table , p_cbw -> command [0 ]));
410
416
//TU_LOG_MEM(MSC_DEBUG, p_cbw, xferred_bytes, 2);
411
417
412
418
p_csw -> signature = MSC_CSW_SIGNATURE ;
@@ -473,7 +479,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
473
479
if ( resplen < 0 )
474
480
{
475
481
// unsupported command
476
- TU_LOG (MSC_DEBUG , " SCSI unsupported command\r\n" );
482
+ TU_LOG (MSC_DEBUG , " SCSI unsupported or failed command\r\n" );
477
483
fail_scsi_op (rhport , p_msc , MSC_CSW_STATUS_FAILED );
478
484
}
479
485
else if (resplen == 0 )
@@ -508,7 +514,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
508
514
break ;
509
515
510
516
case MSC_STAGE_DATA :
511
- TU_LOG (MSC_DEBUG , " SCSI Data\r\n" );
517
+ TU_LOG (MSC_DEBUG , " SCSI Data [Lun%u] \r\n" , p_cbw -> lun );
512
518
//TU_LOG_MEM(MSC_DEBUG, _mscd_buf, xferred_bytes, 2);
513
519
514
520
if (SCSI_CMD_READ_10 == p_cbw -> command [0 ])
@@ -569,7 +575,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
569
575
// Wait for the Status phase to complete
570
576
if ( (ep_addr == p_msc -> ep_in ) && (xferred_bytes == sizeof (msc_csw_t )) )
571
577
{
572
- TU_LOG (MSC_DEBUG , " SCSI Status = %u\r\n" , p_csw -> status );
578
+ TU_LOG (MSC_DEBUG , " SCSI Status [Lun%u] = %u\r\n" , p_cbw -> lun , p_csw -> status );
573
579
// TU_LOG_MEM(MSC_DEBUG, p_csw, xferred_bytes, 2);
574
580
575
581
// Invoke complete callback if defined
@@ -654,8 +660,8 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
654
660
// Failed status response
655
661
resplen = - 1 ;
656
662
657
- // If sense key is not set by callback, default to Logical Unit Not Ready, Cause Not Reportable
658
- if ( p_msc -> sense_key == 0 ) tud_msc_set_sense (lun , SCSI_SENSE_NOT_READY , 0x04 , 0x00 );
663
+ // set default sense if not set by callback
664
+ if ( p_msc -> sense_key == 0 ) set_default_error_sense (lun );
659
665
}
660
666
break ;
661
667
@@ -670,8 +676,8 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
670
676
// Failed status response
671
677
resplen = - 1 ;
672
678
673
- // If sense key is not set by callback, default to Logical Unit Not Ready, Cause Not Reportable
674
- if ( p_msc -> sense_key == 0 ) tud_msc_set_sense (lun , SCSI_SENSE_NOT_READY , 0x04 , 0x00 );
679
+ // set default sense if not set by callback
680
+ if ( p_msc -> sense_key == 0 ) set_default_error_sense (lun );
675
681
}
676
682
}
677
683
break ;
@@ -691,13 +697,13 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
691
697
{
692
698
resplen = -1 ;
693
699
694
- // If sense key is not set by callback, default to Logical Unit Not Ready, Cause Not Reportable
695
- if ( p_msc -> sense_key == 0 ) tud_msc_set_sense (lun , SCSI_SENSE_NOT_READY , 0x04 , 0x00 );
700
+ // set default sense if not set by callback
701
+ if ( p_msc -> sense_key == 0 ) set_default_error_sense (lun );
696
702
}else
697
703
{
698
704
scsi_read_capacity10_resp_t read_capa10 ;
699
705
700
- read_capa10 .last_lba = tu_htonl (block_count - 1 );
706
+ read_capa10 .last_lba = tu_htonl (block_count - 1 );
701
707
read_capa10 .block_size = tu_htonl (block_size );
702
708
703
709
resplen = sizeof (read_capa10 );
@@ -727,8 +733,8 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
727
733
{
728
734
resplen = -1 ;
729
735
730
- // If sense key is not set by callback, default to Logical Unit Not Ready, Cause Not Reportable
731
- if ( p_msc -> sense_key == 0 ) tud_msc_set_sense (lun , SCSI_SENSE_NOT_READY , 0x04 , 0x00 );
736
+ // set default sense if not set by callback
737
+ if ( p_msc -> sense_key == 0 ) set_default_error_sense (lun );
732
738
}else
733
739
{
734
740
read_fmt_capa .block_num = tu_htonl (block_count );
@@ -765,10 +771,10 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
765
771
{
766
772
scsi_mode_sense6_resp_t mode_resp =
767
773
{
768
- .data_len = 3 ,
769
- .medium_type = 0 ,
770
- .write_protected = false,
771
- .reserved = 0 ,
774
+ .data_len = 3 ,
775
+ .medium_type = 0 ,
776
+ .write_protected = false,
777
+ .reserved = 0 ,
772
778
.block_descriptor_len = 0 // no block descriptor are included
773
779
};
774
780
@@ -789,19 +795,24 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
789
795
{
790
796
scsi_sense_fixed_resp_t sense_rsp =
791
797
{
792
- .response_code = 0x70 ,
798
+ .response_code = 0x70 , // current, fixed format
793
799
.valid = 1
794
800
};
795
801
796
- sense_rsp .add_sense_len = sizeof (scsi_sense_fixed_resp_t ) - 8 ;
797
-
802
+ sense_rsp .add_sense_len = sizeof (scsi_sense_fixed_resp_t ) - 8 ;
798
803
sense_rsp .sense_key = p_msc -> sense_key ;
799
804
sense_rsp .add_sense_code = p_msc -> add_sense_code ;
800
805
sense_rsp .add_sense_qualifier = p_msc -> add_sense_qualifier ;
801
806
802
807
resplen = sizeof (sense_rsp );
803
808
memcpy (buffer , & sense_rsp , resplen );
804
809
810
+ // request sense callback could overwrite the sense data
811
+ if (tud_msc_request_sense_cb )
812
+ {
813
+ resplen = tud_msc_request_sense_cb (lun , buffer , bufsize );
814
+ }
815
+
805
816
// Clear sense data after copy
806
817
tud_msc_set_sense (lun , 0 , 0 , 0 );
807
818
}
@@ -835,8 +846,8 @@ static void proc_read10_cmd(uint8_t rhport, mscd_interface_t* p_msc)
835
846
// negative means error -> endpoint is stalled & status in CSW set to failed
836
847
TU_LOG (MSC_DEBUG , " tud_msc_read10_cb() return -1\r\n" );
837
848
838
- // Sense = Flash not ready for access
839
- tud_msc_set_sense (p_cbw -> lun , SCSI_SENSE_MEDIUM_ERROR , 0x33 , 0x00 );
849
+ // set sense
850
+ set_default_error_sense (p_cbw -> lun );
840
851
841
852
fail_scsi_op (rhport , p_msc , MSC_CSW_STATUS_FAILED );
842
853
}
@@ -900,8 +911,8 @@ static void proc_write10_new_data(uint8_t rhport, mscd_interface_t* p_msc, uint3
900
911
// update actual byte before failed
901
912
p_msc -> xferred_len += xferred_bytes ;
902
913
903
- // Sense = Flash not ready for access
904
- tud_msc_set_sense (p_cbw -> lun , SCSI_SENSE_MEDIUM_ERROR , 0x33 , 0x00 );
914
+ // Set sense
915
+ set_default_error_sense (p_cbw -> lun );
905
916
906
917
fail_scsi_op (rhport , p_msc , MSC_CSW_STATUS_FAILED );
907
918
}else
0 commit comments