105 lines
3.4 KiB
C
Raw Normal View History

/*
* The MIT License (MIT)
*
* Copyright (c) 2018, hathach (tinyusb.org)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* This file is part of the TinyUSB stack.
2018-09-07 17:21:06 +07:00
*/
2018-03-23 14:23:39 +07:00
2018-11-22 11:36:50 +07:00
#include "bsp/board.h"
#include "tusb.h"
2018-03-23 14:23:39 +07:00
2018-04-12 13:23:52 +07:00
#if CFG_TUD_MSC
2018-03-23 14:23:39 +07:00
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+
//--------------------------------------------------------------------+
// tinyusb callbacks
//--------------------------------------------------------------------+
2018-07-26 22:44:11 +07:00
// Callback invoked when received an SCSI command not in built-in list below
// - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE
// - READ10 and WRITE10 has their own callbacks
2018-07-25 20:34:56 +07:00
int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize)
2018-03-23 14:23:39 +07:00
{
// read10 & write10 has their own callback and MUST not be handled here
2018-04-18 18:05:08 +07:00
2018-07-27 17:14:49 +07:00
void const* response = NULL;
uint16_t resplen = 0;
2018-05-14 13:40:20 +07:00
// most scsi handled is input
bool in_xfer = true;
2018-04-18 18:05:08 +07:00
2018-03-23 14:23:39 +07:00
switch (scsi_cmd[0])
{
case SCSI_CMD_TEST_UNIT_READY:
2018-07-25 22:35:02 +07:00
// Command that host uses to check our readiness before sending other commands
2018-07-27 17:14:49 +07:00
resplen = 0;
2018-03-23 14:23:39 +07:00
break;
case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
2018-07-25 22:35:02 +07:00
// Host is about to read/write etc ... better not to disconnect disk
2018-07-27 17:14:49 +07:00
resplen = 0;
2018-07-25 22:35:02 +07:00
break;
2018-07-26 15:59:35 +07:00
case SCSI_CMD_START_STOP_UNIT:
2018-07-26 22:44:11 +07:00
// Host try to eject/safe remove/poweroff us. We could safely disconnect with disk storage, or go into lower power
2018-07-27 17:14:49 +07:00
/* scsi_start_stop_unit_t const * start_stop = (scsi_start_stop_unit_t const *) scsi_cmd;
// Start bit = 0 : low power mode, if load_eject = 1 : unmount disk storage as well
// Start bit = 1 : Ready mode, if load_eject = 1 : mount disk storage
start_stop->start;
start_stop->load_eject;
*/
resplen = 0;
2018-07-26 15:59:35 +07:00
break;
2018-03-23 14:23:39 +07:00
default:
// Set Sense = Invalid Command Operation
tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
// negative means error -> tinyusb could stall and/or response with failed status
resplen = -1;
break;
2018-04-18 18:05:08 +07:00
}
2018-07-27 17:14:49 +07:00
// return resplen must not larger than bufsize
if ( resplen > bufsize ) resplen = bufsize;
2018-04-18 18:05:08 +07:00
if ( response && (resplen > 0) )
2018-05-14 13:40:20 +07:00
{
if(in_xfer)
{
2018-07-27 17:14:49 +07:00
memcpy(buffer, response, resplen);
2018-05-14 13:40:20 +07:00
}else
{
// SCSI output
}
2018-03-23 14:23:39 +07:00
}
2018-07-27 17:14:49 +07:00
return resplen;
2018-03-23 14:23:39 +07:00
}
#endif