Skip to content

Commit 17576a6

Browse files
authored
Merge pull request hathach#2156 from hathach/usbh-xfer-user-callback
Usbh xfer user callback
2 parents 6c7c9f2 + 16ad918 commit 17576a6

File tree

5 files changed

+122
-122
lines changed

5 files changed

+122
-122
lines changed

src/class/msc/msc_device.c

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@
3434

3535
#include "msc_device.h"
3636

37+
// Level where CFG_TUSB_DEBUG must be at least for this driver is logged
38+
#ifndef CFG_TUD_MSC_LOG_LEVEL
39+
#define CFG_TUD_MSC_LOG_LEVEL 2
40+
#endif
41+
42+
#define TU_LOG_DRV(...) TU_LOG(CFG_TUD_MSC_LOG_LEVEL, __VA_ARGS__)
43+
3744
//--------------------------------------------------------------------+
3845
// MACRO CONSTANT TYPEDEF
3946
//--------------------------------------------------------------------+
40-
41-
// Can be selectively disabled to reduce logging when troubleshooting other driver
42-
#define MSC_DEBUG 2
43-
4447
enum
4548
{
4649
MSC_STAGE_CMD = 0,
@@ -164,7 +167,7 @@ uint8_t rdwr10_validate_cmd(msc_cbw_t const* cbw)
164167
{
165168
if ( block_count )
166169
{
167-
TU_LOG(MSC_DEBUG, " SCSI case 2 (Hn < Di) or case 3 (Hn < Do) \r\n");
170+
TU_LOG_DRV(" SCSI case 2 (Hn < Di) or case 3 (Hn < Do) \r\n");
168171
status = MSC_CSW_STATUS_PHASE_ERROR;
169172
}else
170173
{
@@ -174,22 +177,22 @@ uint8_t rdwr10_validate_cmd(msc_cbw_t const* cbw)
174177
{
175178
if ( SCSI_CMD_READ_10 == cbw->command[0] && !is_data_in(cbw->dir) )
176179
{
177-
TU_LOG(MSC_DEBUG, " SCSI case 10 (Ho <> Di)\r\n");
180+
TU_LOG_DRV(" SCSI case 10 (Ho <> Di)\r\n");
178181
status = MSC_CSW_STATUS_PHASE_ERROR;
179182
}
180183
else if ( SCSI_CMD_WRITE_10 == cbw->command[0] && is_data_in(cbw->dir) )
181184
{
182-
TU_LOG(MSC_DEBUG, " SCSI case 8 (Hi <> Do)\r\n");
185+
TU_LOG_DRV(" SCSI case 8 (Hi <> Do)\r\n");
183186
status = MSC_CSW_STATUS_PHASE_ERROR;
184187
}
185188
else if ( 0 == block_count )
186189
{
187-
TU_LOG(MSC_DEBUG, " SCSI case 4 Hi > Dn (READ10) or case 9 Ho > Dn (WRITE10) \r\n");
190+
TU_LOG_DRV(" SCSI case 4 Hi > Dn (READ10) or case 9 Ho > Dn (WRITE10) \r\n");
188191
status = MSC_CSW_STATUS_FAILED;
189192
}
190193
else if ( cbw->total_bytes / block_count == 0 )
191194
{
192-
TU_LOG(MSC_DEBUG, " Computed block size = 0. SCSI case 7 Hi < Di (READ10) or case 13 Ho < Do (WRIT10)\r\n");
195+
TU_LOG_DRV(" Computed block size = 0. SCSI case 7 Hi < Di (READ10) or case 13 Ho < Do (WRIT10)\r\n");
193196
status = MSC_CSW_STATUS_PHASE_ERROR;
194197
}
195198
}
@@ -352,7 +355,7 @@ bool mscd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t
352355
switch ( request->bRequest )
353356
{
354357
case MSC_REQ_RESET:
355-
TU_LOG(MSC_DEBUG, " MSC BOT Reset\r\n");
358+
TU_LOG_DRV(" MSC BOT Reset\r\n");
356359
TU_VERIFY(request->wValue == 0 && request->wLength == 0);
357360

358361
// driver state reset
@@ -363,7 +366,7 @@ bool mscd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t
363366

364367
case MSC_REQ_GET_MAX_LUN:
365368
{
366-
TU_LOG(MSC_DEBUG, " MSC Get Max Lun\r\n");
369+
TU_LOG_DRV(" MSC Get Max Lun\r\n");
367370
TU_VERIFY(request->wValue == 0 && request->wLength == 1);
368371

369372
uint8_t maxlun = 1;
@@ -400,7 +403,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
400403

401404
if ( !(xferred_bytes == sizeof(msc_cbw_t) && p_cbw->signature == MSC_CBW_SIGNATURE) )
402405
{
403-
TU_LOG(MSC_DEBUG, " SCSI CBW is not valid\r\n");
406+
TU_LOG_DRV(" SCSI CBW is not valid\r\n");
404407

405408
// BOT 6.6.1 If CBW is not valid stall both endpoints until reset recovery
406409
p_msc->stage = MSC_STAGE_NEED_RESET;
@@ -412,7 +415,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
412415
return false;
413416
}
414417

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]));
418+
TU_LOG_DRV(" SCSI Command [Lun%u]: %s\r\n", p_cbw->lun, tu_lookup_find(&_msc_scsi_cmd_table, p_cbw->command[0]));
416419
//TU_LOG_MEM(MSC_DEBUG, p_cbw, xferred_bytes, 2);
417420

418421
p_csw->signature = MSC_CSW_SIGNATURE;
@@ -457,7 +460,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
457460
{
458461
if (p_cbw->total_bytes > sizeof(_mscd_buf))
459462
{
460-
TU_LOG(MSC_DEBUG, " SCSI reject non READ10/WRITE10 with large data\r\n");
463+
TU_LOG_DRV(" SCSI reject non READ10/WRITE10 with large data\r\n");
461464
fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
462465
}else
463466
{
@@ -479,7 +482,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
479482
if ( resplen < 0 )
480483
{
481484
// unsupported command
482-
TU_LOG(MSC_DEBUG, " SCSI unsupported or failed command\r\n");
485+
TU_LOG_DRV(" SCSI unsupported or failed command\r\n");
483486
fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
484487
}
485488
else if (resplen == 0)
@@ -514,7 +517,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
514517
break;
515518

516519
case MSC_STAGE_DATA:
517-
TU_LOG(MSC_DEBUG, " SCSI Data [Lun%u]\r\n", p_cbw->lun);
520+
TU_LOG_DRV(" SCSI Data [Lun%u]\r\n", p_cbw->lun);
518521
//TU_LOG_MEM(MSC_DEBUG, _mscd_buf, xferred_bytes, 2);
519522

520523
if (SCSI_CMD_READ_10 == p_cbw->command[0])
@@ -546,7 +549,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
546549
if ( cb_result < 0 )
547550
{
548551
// unsupported command
549-
TU_LOG(MSC_DEBUG, " SCSI unsupported command\r\n");
552+
TU_LOG_DRV(" SCSI unsupported command\r\n");
550553
fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
551554
}else
552555
{
@@ -575,7 +578,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
575578
// Wait for the Status phase to complete
576579
if( (ep_addr == p_msc->ep_in) && (xferred_bytes == sizeof(msc_csw_t)) )
577580
{
578-
TU_LOG(MSC_DEBUG, " SCSI Status [Lun%u] = %u\r\n", p_cbw->lun, p_csw->status);
581+
TU_LOG_DRV(" SCSI Status [Lun%u] = %u\r\n", p_cbw->lun, p_csw->status);
579582
// TU_LOG_MEM(MSC_DEBUG, p_csw, xferred_bytes, 2);
580583

581584
// Invoke complete callback if defined
@@ -845,7 +848,7 @@ static void proc_read10_cmd(uint8_t rhport, mscd_interface_t* p_msc)
845848
if ( nbytes < 0 )
846849
{
847850
// negative means error -> endpoint is stalled & status in CSW set to failed
848-
TU_LOG(MSC_DEBUG, " tud_msc_read10_cb() return -1\r\n");
851+
TU_LOG_DRV(" tud_msc_read10_cb() return -1\r\n");
849852

850853
// set sense
851854
set_sense_medium_not_present(p_cbw->lun);
@@ -907,7 +910,7 @@ static void proc_write10_new_data(uint8_t rhport, mscd_interface_t* p_msc, uint3
907910
if ( nbytes < 0 )
908911
{
909912
// negative means error -> failed this scsi op
910-
TU_LOG(MSC_DEBUG, " tud_msc_write10_cb() return -1\r\n");
913+
TU_LOG_DRV(" tud_msc_write10_cb() return -1\r\n");
911914

912915
// update actual byte before failed
913916
p_msc->xferred_len += xferred_bytes;

0 commit comments

Comments
 (0)