| 
									
										
										
										
											2019-03-20 16:11:42 +07:00
										 |  |  | /* 
 | 
					
						
							|  |  |  |  * The MIT License (MIT) | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-05-14 11:48:05 +07:00
										 |  |  |  * Copyright (c) 2019 Ha Thach (tinyusb.org) | 
					
						
							| 
									
										
										
										
											2019-03-20 16:11:42 +07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "tusb_option.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-23 15:25:45 +07:00
										 |  |  | #if (TUSB_OPT_DEVICE_ENABLED && CFG_TUD_MSC)
 | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 14:15:16 +07:00
										 |  |  | #include "common/tusb_common.h"
 | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  | #include "msc_device.h"
 | 
					
						
							| 
									
										
										
										
											2018-03-22 14:15:16 +07:00
										 |  |  | #include "device/usbd_pvt.h"
 | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // MACRO CONSTANT TYPEDEF
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2018-03-22 17:43:13 +07:00
										 |  |  | enum | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   MSC_STAGE_CMD  = 0, | 
					
						
							|  |  |  |   MSC_STAGE_DATA, | 
					
						
							|  |  |  |   MSC_STAGE_STATUS | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-01 20:41:26 +07:00
										 |  |  | typedef struct | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   CFG_TUSB_MEM_ALIGN msc_cbw_t cbw; | 
					
						
							| 
									
										
										
										
											2018-11-16 21:56:39 +07:00
										 |  |  |   CFG_TUSB_MEM_ALIGN msc_csw_t csw; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   uint8_t  itf_num; | 
					
						
							|  |  |  |   uint8_t  ep_in; | 
					
						
							|  |  |  |   uint8_t  ep_out; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Bulk Only Transfer (BOT) Protocol
 | 
					
						
							|  |  |  |   uint8_t  stage; | 
					
						
							|  |  |  |   uint32_t total_len; | 
					
						
							|  |  |  |   uint32_t xferred_len; // numbered of bytes transferred so far in the Data Stage
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Sense Response Data
 | 
					
						
							|  |  |  |   uint8_t sense_key; | 
					
						
							|  |  |  |   uint8_t add_sense_code; | 
					
						
							|  |  |  |   uint8_t add_sense_qualifier; | 
					
						
							|  |  |  | }mscd_interface_t; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-01 20:41:26 +07:00
										 |  |  | CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN static mscd_interface_t _mscd_itf; | 
					
						
							| 
									
										
										
										
											2018-11-22 17:21:07 +07:00
										 |  |  | CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN static uint8_t _mscd_buf[CFG_TUD_MSC_BUFSIZE]; | 
					
						
							| 
									
										
										
										
											2018-04-19 16:10:52 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // INTERNAL OBJECT & FUNCTION DECLARATION
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  | static void proc_read10_cmd(uint8_t rhport, mscd_interface_t* p_msc); | 
					
						
							|  |  |  | static void proc_write10_cmd(uint8_t rhport, mscd_interface_t* p_msc); | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-06 17:36:10 +07:00
										 |  |  | static inline uint32_t rdwr10_get_lba(uint8_t const command[]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   // read10 & write10 has the same format
 | 
					
						
							|  |  |  |   scsi_write10_t* p_rdwr10 = (scsi_write10_t*) command; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // copy first to prevent mis-aligned access
 | 
					
						
							|  |  |  |   uint32_t lba; | 
					
						
							|  |  |  |   memcpy(&lba, &p_rdwr10->lba, 4); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-14 11:46:22 +07:00
										 |  |  |   // lba is in Big Endian format
 | 
					
						
							| 
									
										
										
										
											2019-05-14 12:12:23 +07:00
										 |  |  |   return tu_ntohl(lba); | 
					
						
							| 
									
										
										
										
											2018-06-06 17:36:10 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline uint16_t rdwr10_get_blockcount(uint8_t const command[]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   // read10 & write10 has the same format
 | 
					
						
							|  |  |  |   scsi_write10_t* p_rdwr10 = (scsi_write10_t*) command; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // copy first to prevent mis-aligned access
 | 
					
						
							|  |  |  |   uint16_t block_count; | 
					
						
							|  |  |  |   memcpy(&block_count, &p_rdwr10->block_count, 2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-14 12:12:23 +07:00
										 |  |  |   return tu_ntohs(block_count); | 
					
						
							| 
									
										
										
										
											2018-06-06 17:36:10 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-26 15:59:13 +07:00
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // APPLICATION API
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | bool tud_msc_set_sense(uint8_t lun, uint8_t sense_key, uint8_t add_sense_code, uint8_t add_sense_qualifier) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   (void) lun; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   _mscd_itf.sense_key           = sense_key; | 
					
						
							|  |  |  |   _mscd_itf.add_sense_code      = add_sense_code; | 
					
						
							|  |  |  |   _mscd_itf.add_sense_qualifier = add_sense_qualifier; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // USBD-CLASS API
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2013-11-13 12:40:11 +07:00
										 |  |  | void mscd_init(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-10-23 12:19:32 +07:00
										 |  |  |   tu_memclr(&_mscd_itf, sizeof(mscd_interface_t)); | 
					
						
							| 
									
										
										
										
											2013-11-13 12:40:11 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-13 16:09:26 +07:00
										 |  |  | void mscd_reset(uint8_t rhport) | 
					
						
							| 
									
										
										
										
											2013-11-13 12:40:11 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-11-26 12:25:28 +07:00
										 |  |  |   (void) rhport; | 
					
						
							| 
									
										
										
										
											2018-10-23 12:19:32 +07:00
										 |  |  |   tu_memclr(&_mscd_itf, sizeof(mscd_interface_t)); | 
					
						
							| 
									
										
										
										
											2013-11-13 12:40:11 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-12 13:00:59 +07:00
										 |  |  | bool mscd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_len) | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-06-16 12:30:10 +07:00
										 |  |  |   // only support SCSI's BOT protocol
 | 
					
						
							| 
									
										
										
										
											2018-12-12 13:00:59 +07:00
										 |  |  |   TU_ASSERT(MSC_SUBCLASS_SCSI == itf_desc->bInterfaceSubClass && | 
					
						
							|  |  |  |             MSC_PROTOCOL_BOT  == itf_desc->bInterfaceProtocol); | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-19 16:35:58 +07:00
										 |  |  |   mscd_interface_t * p_msc = &_mscd_itf; | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-01 19:29:56 +07:00
										 |  |  |   // Open endpoint pair
 | 
					
						
							|  |  |  |   TU_ASSERT( usbd_open_edpt_pair(rhport, tu_desc_next(itf_desc), 2, TUSB_XFER_BULK, &p_msc->ep_out, &p_msc->ep_in) ); | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-12 12:01:15 +07:00
										 |  |  |   p_msc->itf_num = itf_desc->bInterfaceNumber; | 
					
						
							| 
									
										
										
										
											2018-06-16 12:30:10 +07:00
										 |  |  |   (*p_len) = sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t); | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-12 13:00:59 +07:00
										 |  |  |   // Prepare for Command Block Wrapper
 | 
					
						
							| 
									
										
										
										
											2019-05-29 16:55:15 +07:00
										 |  |  |   TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_out, (uint8_t*) &p_msc->cbw, sizeof(msc_cbw_t)) ); | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-12 13:00:59 +07:00
										 |  |  |   return true; | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 21:56:39 +07:00
										 |  |  | // Handle class control request
 | 
					
						
							|  |  |  | // return false to stall control endpoint (e.g unsupported request)
 | 
					
						
							|  |  |  | bool mscd_control_request(uint8_t rhport, tusb_control_request_t const * p_request) | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-11-16 21:56:39 +07:00
										 |  |  |   TU_ASSERT(p_request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS); | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 21:56:39 +07:00
										 |  |  |   switch ( p_request->bRequest ) | 
					
						
							| 
									
										
										
										
											2018-03-21 20:08:57 +07:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2018-11-16 21:56:39 +07:00
										 |  |  |     case MSC_REQ_RESET: | 
					
						
							|  |  |  |       // TODO: Actually reset interface.
 | 
					
						
							|  |  |  |       usbd_control_status(rhport, p_request); | 
					
						
							|  |  |  |     break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     case MSC_REQ_GET_MAX_LUN: | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-04-28 00:43:36 +07:00
										 |  |  |       uint8_t maxlun = 1; | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |       if (tud_msc_get_maxlun_cb) maxlun = tud_msc_get_maxlun_cb(); | 
					
						
							| 
									
										
										
										
											2019-04-28 00:43:36 +07:00
										 |  |  |       TU_VERIFY(maxlun); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-26 15:54:42 +07:00
										 |  |  |       // MAX LUN is minus 1 by specs
 | 
					
						
							| 
									
										
										
										
											2019-04-28 00:43:36 +07:00
										 |  |  |       maxlun--; | 
					
						
							| 
									
										
										
										
											2019-04-26 15:54:42 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 21:56:39 +07:00
										 |  |  |       usbd_control_xfer(rhport, p_request, &maxlun, 1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     default: return false; // stall unsupported request
 | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-11-16 21:56:39 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return true; | 
					
						
							| 
									
										
										
										
											2018-10-24 23:55:10 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 21:56:39 +07:00
										 |  |  | // Invoked when class request DATA stage is finished.
 | 
					
						
							|  |  |  | // return false to stall control endpoint (e.g Host send non-sense DATA)
 | 
					
						
							|  |  |  | bool mscd_control_request_complete(uint8_t rhport, tusb_control_request_t const * p_request) | 
					
						
							| 
									
										
										
										
											2018-11-08 13:45:30 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-11-26 12:25:28 +07:00
										 |  |  |   (void) rhport; | 
					
						
							|  |  |  |   (void) p_request; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 21:56:39 +07:00
										 |  |  |   // nothing to do
 | 
					
						
							|  |  |  |   return true; | 
					
						
							| 
									
										
										
										
											2018-11-08 13:45:30 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  | // return response's length (copied to buffer). Negative if it is not an built-in command or indicate Failed status (CSW)
 | 
					
						
							|  |  |  | // In case of a failed status, sense key must be set for reason of failure
 | 
					
						
							|  |  |  | int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_t* buffer, uint32_t bufsize) | 
					
						
							| 
									
										
										
										
											2018-07-25 23:56:06 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-11-26 12:25:28 +07:00
										 |  |  |   (void) bufsize; // TODO refractor later
 | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |   int32_t resplen; | 
					
						
							| 
									
										
										
										
											2018-07-25 23:56:06 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |   switch ( scsi_cmd[0] ) | 
					
						
							| 
									
										
										
										
											2018-07-25 23:56:06 +07:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |     case SCSI_CMD_TEST_UNIT_READY: | 
					
						
							|  |  |  |       resplen = 0; | 
					
						
							|  |  |  |       if ( !tud_msc_test_unit_ready_cb(lun) ) | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         // not ready response with Failed status and sense key = not ready
 | 
					
						
							|  |  |  |         resplen = - 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 16:48:50 +07:00
										 |  |  |         // If sense key is not set by callback, default to Logical Unit Not Ready, Cause Not Reportable
 | 
					
						
							|  |  |  |         if ( _mscd_itf.sense_key == 0 ) tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x04, 0x00); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     case SCSI_CMD_START_STOP_UNIT: | 
					
						
							|  |  |  |       resplen = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (tud_msc_start_stop_cb) | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         scsi_start_stop_unit_t const * start_stop = (scsi_start_stop_unit_t const *) scsi_cmd; | 
					
						
							|  |  |  |         tud_msc_start_stop_cb(lun, start_stop->power_condition, start_stop->start, start_stop->load_eject); | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |       } | 
					
						
							|  |  |  |     break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-25 23:56:06 +07:00
										 |  |  |     case SCSI_CMD_READ_CAPACITY_10: | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-11-22 12:11:06 +07:00
										 |  |  |       uint32_t block_count; | 
					
						
							|  |  |  |       uint32_t block_size; | 
					
						
							|  |  |  |       uint16_t block_size_u16; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |       tud_msc_capacity_cb(lun, &block_count, &block_size_u16); | 
					
						
							| 
									
										
										
										
											2018-11-22 12:11:06 +07:00
										 |  |  |       block_size = (uint32_t) block_size_u16; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-09 23:33:55 +07:00
										 |  |  |       // Invalid block size/count from callback, possibly unit is not ready
 | 
					
						
							|  |  |  |       // stall this request, set sense key to NOT READY
 | 
					
						
							|  |  |  |       if (block_count == 0 || block_size == 0) | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         resplen = -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // If sense key is not set by callback, default to Logical Unit Not Ready, Cause Not Reportable
 | 
					
						
							|  |  |  |         if ( _mscd_itf.sense_key == 0 ) tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x04, 0x00); | 
					
						
							|  |  |  |       }else | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         scsi_read_capacity10_resp_t read_capa10; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-14 12:12:23 +07:00
										 |  |  |         read_capa10.last_lba = tu_htonl(block_count-1); | 
					
						
							|  |  |  |         read_capa10.block_size = tu_htonl(block_size); | 
					
						
							| 
									
										
										
										
											2018-07-25 23:56:06 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-09 23:33:55 +07:00
										 |  |  |         resplen = sizeof(read_capa10); | 
					
						
							|  |  |  |         memcpy(buffer, &read_capa10, resplen); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-07-25 23:56:06 +07:00
										 |  |  |     } | 
					
						
							|  |  |  |     break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     case SCSI_CMD_READ_FORMAT_CAPACITY: | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       scsi_read_format_capacity_data_t read_fmt_capa = | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |           .list_length     = 8, | 
					
						
							| 
									
										
										
										
											2018-10-24 23:55:10 -07:00
										 |  |  |           .block_num       = 0, | 
					
						
							| 
									
										
										
										
											2018-11-22 12:11:06 +07:00
										 |  |  |           .descriptor_type = 2, // formatted media
 | 
					
						
							| 
									
										
										
										
											2018-10-24 23:55:10 -07:00
										 |  |  |           .block_size_u16  = 0 | 
					
						
							| 
									
										
										
										
											2018-07-25 23:56:06 +07:00
										 |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-22 12:11:06 +07:00
										 |  |  |       uint32_t block_count; | 
					
						
							| 
									
										
										
										
											2018-10-24 23:55:10 -07:00
										 |  |  |       uint16_t block_size; | 
					
						
							| 
									
										
										
										
											2018-11-22 12:11:06 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |       tud_msc_capacity_cb(lun, &block_count, &block_size); | 
					
						
							| 
									
										
										
										
											2018-10-24 23:55:10 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-09 23:33:55 +07:00
										 |  |  |       // Invalid block size/count from callback, possibly unit is not ready
 | 
					
						
							|  |  |  |       // stall this request, set sense key to NOT READY
 | 
					
						
							|  |  |  |       if (block_count == 0 || block_size == 0) | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         resplen = -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // If sense key is not set by callback, default to Logical Unit Not Ready, Cause Not Reportable
 | 
					
						
							|  |  |  |         if ( _mscd_itf.sense_key == 0 ) tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x04, 0x00); | 
					
						
							|  |  |  |       }else | 
					
						
							|  |  |  |       { | 
					
						
							| 
									
										
										
										
											2019-05-14 12:12:23 +07:00
										 |  |  |         read_fmt_capa.block_num = tu_htonl(block_count); | 
					
						
							|  |  |  |         read_fmt_capa.block_size_u16 = tu_htons(block_size); | 
					
						
							| 
									
										
										
										
											2019-05-09 23:33:55 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         resplen = sizeof(read_fmt_capa); | 
					
						
							|  |  |  |         memcpy(buffer, &read_fmt_capa, resplen); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-07-25 23:56:06 +07:00
										 |  |  |     } | 
					
						
							|  |  |  |     break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     case SCSI_CMD_INQUIRY: | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-07-26 16:57:30 +07:00
										 |  |  |       scsi_inquiry_resp_t inquiry_rsp = | 
					
						
							| 
									
										
										
										
											2018-07-25 23:56:06 +07:00
										 |  |  |       { | 
					
						
							|  |  |  |           .is_removable         = 1, | 
					
						
							|  |  |  |           .version              = 2, | 
					
						
							|  |  |  |           .response_data_format = 2, | 
					
						
							|  |  |  |       }; | 
					
						
							| 
									
										
										
										
											2019-03-04 11:51:36 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-03 23:46:01 +07:00
										 |  |  |       // vendor_id, product_id, product_rev is space padded string
 | 
					
						
							|  |  |  |       memset(inquiry_rsp.vendor_id  , ' ', sizeof(inquiry_rsp.vendor_id)); | 
					
						
							|  |  |  |       memset(inquiry_rsp.product_id , ' ', sizeof(inquiry_rsp.product_id)); | 
					
						
							| 
									
										
										
										
											2019-03-04 11:51:36 +07:00
										 |  |  |       memset(inquiry_rsp.product_rev, ' ', sizeof(inquiry_rsp.product_rev)); | 
					
						
							| 
									
										
										
										
											2019-05-03 23:46:01 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |       tud_msc_inquiry_cb(lun, inquiry_rsp.vendor_id, inquiry_rsp.product_id, inquiry_rsp.product_rev); | 
					
						
							| 
									
										
										
										
											2018-07-25 23:56:06 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |       resplen = sizeof(inquiry_rsp); | 
					
						
							|  |  |  |       memcpy(buffer, &inquiry_rsp, resplen); | 
					
						
							| 
									
										
										
										
											2018-07-25 23:56:06 +07:00
										 |  |  |     } | 
					
						
							|  |  |  |     break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-26 16:57:30 +07:00
										 |  |  |     case SCSI_CMD_MODE_SENSE_6: | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-10-24 23:55:10 -07:00
										 |  |  |       scsi_mode_sense6_resp_t mode_resp = | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |           .data_len = 3, | 
					
						
							|  |  |  |           .medium_type = 0, | 
					
						
							|  |  |  |           .write_protected = false, | 
					
						
							|  |  |  |           .reserved = 0, | 
					
						
							|  |  |  |           .block_descriptor_len = 0  // no block descriptor are included
 | 
					
						
							| 
									
										
										
										
											2018-07-26 16:57:30 +07:00
										 |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-24 23:55:10 -07:00
										 |  |  |       bool writable = true; | 
					
						
							|  |  |  |       if (tud_msc_is_writable_cb) { | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |           writable = tud_msc_is_writable_cb(lun); | 
					
						
							| 
									
										
										
										
											2018-10-24 23:55:10 -07:00
										 |  |  |       } | 
					
						
							|  |  |  |       mode_resp.write_protected = !writable; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |       resplen = sizeof(mode_resp); | 
					
						
							|  |  |  |       memcpy(buffer, &mode_resp, resplen); | 
					
						
							| 
									
										
										
										
											2018-07-26 16:57:30 +07:00
										 |  |  |     } | 
					
						
							|  |  |  |     break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-25 23:56:06 +07:00
										 |  |  |     case SCSI_CMD_REQUEST_SENSE: | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-07-26 16:57:30 +07:00
										 |  |  |       scsi_sense_fixed_resp_t sense_rsp = | 
					
						
							| 
									
										
										
										
											2018-07-25 23:56:06 +07:00
										 |  |  |       { | 
					
						
							|  |  |  |           .response_code = 0x70, | 
					
						
							|  |  |  |           .valid         = 1 | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-26 16:57:30 +07:00
										 |  |  |       sense_rsp.add_sense_len = sizeof(scsi_sense_fixed_resp_t) - 8; | 
					
						
							| 
									
										
										
										
											2018-07-25 23:56:06 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-26 15:59:13 +07:00
										 |  |  |       sense_rsp.sense_key           = _mscd_itf.sense_key; | 
					
						
							|  |  |  |       sense_rsp.add_sense_code      = _mscd_itf.add_sense_code; | 
					
						
							|  |  |  |       sense_rsp.add_sense_qualifier = _mscd_itf.add_sense_qualifier; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |       resplen = sizeof(sense_rsp); | 
					
						
							|  |  |  |       memcpy(buffer, &sense_rsp, resplen); | 
					
						
							| 
									
										
										
										
											2018-07-26 15:59:13 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // Clear sense data after copy
 | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |       tud_msc_set_sense(lun, 0, 0, 0); | 
					
						
							| 
									
										
										
										
											2018-07-25 23:56:06 +07:00
										 |  |  |     } | 
					
						
							|  |  |  |     break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |     default: resplen = -1; break; | 
					
						
							| 
									
										
										
										
											2018-07-25 23:56:06 +07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |   return resplen; | 
					
						
							| 
									
										
										
										
											2018-07-25 23:56:06 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-12 13:12:06 +07:00
										 |  |  | bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) | 
					
						
							| 
									
										
										
										
											2018-03-22 17:43:13 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-20 20:23:22 +07:00
										 |  |  |   mscd_interface_t* p_msc = &_mscd_itf; | 
					
						
							|  |  |  |   msc_cbw_t const * p_cbw = &p_msc->cbw; | 
					
						
							|  |  |  |   msc_csw_t       * p_csw = &p_msc->csw; | 
					
						
							| 
									
										
										
										
											2013-11-26 13:15:40 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 17:43:13 +07:00
										 |  |  |   switch (p_msc->stage) | 
					
						
							| 
									
										
										
										
											2013-12-01 22:34:59 +07:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2018-03-22 17:43:13 +07:00
										 |  |  |     case MSC_STAGE_CMD: | 
					
						
							| 
									
										
										
										
											2018-04-19 16:10:52 +07:00
										 |  |  |       //------------- new CBW received -------------//
 | 
					
						
							| 
									
										
										
										
											2018-03-22 17:48:10 +07:00
										 |  |  |       // Complete IN while waiting for CMD is usually Status of previous SCSI op, ignore it
 | 
					
						
							| 
									
										
										
										
											2018-12-12 13:12:06 +07:00
										 |  |  |       if(ep_addr != p_msc->ep_out) return true; | 
					
						
							| 
									
										
										
										
											2018-03-22 17:48:10 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-23 15:22:46 +07:00
										 |  |  |       TU_ASSERT( event == XFER_RESULT_SUCCESS && | 
					
						
							| 
									
										
										
										
											2018-12-12 13:12:06 +07:00
										 |  |  |                  xferred_bytes == sizeof(msc_cbw_t) && p_cbw->signature == MSC_CBW_SIGNATURE ); | 
					
						
							| 
									
										
										
										
											2018-03-22 17:43:13 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       p_csw->signature    = MSC_CSW_SIGNATURE; | 
					
						
							|  |  |  |       p_csw->tag          = p_cbw->tag; | 
					
						
							|  |  |  |       p_csw->data_residue = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-18 13:20:31 +07:00
										 |  |  |       /*------------- Parse command and prepare DATA -------------*/ | 
					
						
							| 
									
										
										
										
											2018-10-23 12:33:21 +07:00
										 |  |  |       p_msc->stage = MSC_STAGE_DATA; | 
					
						
							|  |  |  |       p_msc->total_len = p_cbw->total_bytes; | 
					
						
							| 
									
										
										
										
											2018-03-23 11:53:49 +07:00
										 |  |  |       p_msc->xferred_len = 0; | 
					
						
							| 
									
										
										
										
											2018-03-22 17:43:13 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  |       if (SCSI_CMD_READ_10 == p_cbw->command[0]) | 
					
						
							| 
									
										
										
										
											2018-03-22 17:43:13 +07:00
										 |  |  |       { | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  |         proc_read10_cmd(rhport, p_msc); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       else if (SCSI_CMD_WRITE_10 == p_cbw->command[0]) | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         proc_write10_cmd(rhport, p_msc); | 
					
						
							| 
									
										
										
										
											2018-03-22 17:43:13 +07:00
										 |  |  |       } | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |       { | 
					
						
							| 
									
										
										
										
											2018-04-18 13:46:05 +07:00
										 |  |  |         // For other SCSI commands
 | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |         // 1. OUT : queue transfer (invoke app callback after done)
 | 
					
						
							|  |  |  |         // 2. IN & Zero: Process if is built-in, else Invoke app callback. Skip DATA if zero length
 | 
					
						
							| 
									
										
										
										
											2019-05-14 12:54:29 +07:00
										 |  |  |         if ( (p_cbw->total_bytes > 0 ) && !tu_bit_test(p_cbw->dir, 7) ) | 
					
						
							| 
									
										
										
										
											2018-04-18 13:46:05 +07:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |           // queue transfer
 | 
					
						
							| 
									
										
										
										
											2019-05-29 16:55:15 +07:00
										 |  |  |           TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_out, _mscd_buf, p_msc->total_len) ); | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |         }else | 
					
						
							| 
									
										
										
										
											2018-03-23 11:53:49 +07:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |           int32_t resplen; | 
					
						
							| 
									
										
										
										
											2018-07-04 14:39:32 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |           // First process if it is a built-in commands
 | 
					
						
							|  |  |  |           resplen = proc_builtin_scsi(p_cbw->lun, p_cbw->command, _mscd_buf, sizeof(_mscd_buf)); | 
					
						
							| 
									
										
										
										
											2018-07-25 23:56:06 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |           // Not built-in, invoke user callback
 | 
					
						
							|  |  |  |           if ( (resplen < 0) && (p_msc->sense_key == 0) ) | 
					
						
							| 
									
										
										
										
											2018-07-04 14:39:32 +07:00
										 |  |  |           { | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |             resplen = tud_msc_scsi_cb(p_cbw->lun, p_cbw->command, _mscd_buf, p_msc->total_len); | 
					
						
							| 
									
										
										
										
											2018-07-04 14:39:32 +07:00
										 |  |  |           } | 
					
						
							| 
									
										
										
										
											2018-06-24 00:16:22 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |           if ( resplen < 0 ) | 
					
						
							| 
									
										
										
										
											2018-03-22 17:43:13 +07:00
										 |  |  |           { | 
					
						
							| 
									
										
										
										
											2018-10-23 12:33:21 +07:00
										 |  |  |             p_msc->total_len = 0; | 
					
						
							|  |  |  |             p_csw->status = MSC_CSW_STATUS_FAILED; | 
					
						
							|  |  |  |             p_msc->stage = MSC_STAGE_STATUS; | 
					
						
							| 
									
										
										
										
											2018-07-25 16:56:57 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |             // failed but senskey is not set: default to Illegal Request
 | 
					
						
							|  |  |  |             if ( p_msc->sense_key == 0 ) tud_msc_set_sense(p_cbw->lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             /// Stall bulk In if needed
 | 
					
						
							|  |  |  |             if (p_cbw->total_bytes) usbd_edpt_stall(rhport, p_msc->ep_in); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             p_msc->total_len = (uint32_t) resplen; | 
					
						
							|  |  |  |             p_csw->status = MSC_CSW_STATUS_PASSED; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (p_msc->total_len) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |               TU_ASSERT( p_cbw->total_bytes >= p_msc->total_len ); // cannot return more than host expect
 | 
					
						
							| 
									
										
										
										
											2019-05-29 16:55:15 +07:00
										 |  |  |               TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_in, _mscd_buf, p_msc->total_len) ); | 
					
						
							| 
									
										
										
										
											2019-05-06 15:01:38 +07:00
										 |  |  |             }else | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |               p_msc->stage = MSC_STAGE_STATUS; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-03-22 17:43:13 +07:00
										 |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     break; | 
					
						
							| 
									
										
										
										
											2013-11-26 13:15:40 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 17:43:13 +07:00
										 |  |  |     case MSC_STAGE_DATA: | 
					
						
							| 
									
										
										
										
											2018-04-19 16:10:52 +07:00
										 |  |  |       // OUT transfer, invoke callback if needed
 | 
					
						
							| 
									
										
										
										
											2019-05-14 12:54:29 +07:00
										 |  |  |       if ( !tu_bit_test(p_cbw->dir, 7) ) | 
					
						
							| 
									
										
										
										
											2013-11-25 16:42:04 +07:00
										 |  |  |       { | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  |         if ( SCSI_CMD_WRITE_10 != p_cbw->command[0] ) | 
					
						
							| 
									
										
										
										
											2018-04-18 13:46:05 +07:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-10-23 12:33:21 +07:00
										 |  |  |           int32_t cb_result = tud_msc_scsi_cb(p_cbw->lun, p_cbw->command, _mscd_buf, p_msc->total_len); | 
					
						
							| 
									
										
										
										
											2018-07-26 15:59:13 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |           if ( cb_result < 0 ) | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             p_csw->status = MSC_CSW_STATUS_FAILED; | 
					
						
							|  |  |  |             tud_msc_set_sense(p_cbw->lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00); // Sense = Invalid Command Operation
 | 
					
						
							|  |  |  |           }else | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             p_csw->status = MSC_CSW_STATUS_PASSED; | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2018-04-19 16:10:52 +07:00
										 |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							| 
									
										
										
										
											2018-04-18 13:46:05 +07:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-10-23 12:33:21 +07:00
										 |  |  |           uint16_t const block_sz = p_cbw->total_bytes / rdwr10_get_blockcount(p_cbw->command); | 
					
						
							| 
									
										
										
										
											2018-06-19 22:59:54 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |           // Adjust lba with transferred bytes
 | 
					
						
							|  |  |  |           uint32_t const lba = rdwr10_get_lba(p_cbw->command) + (p_msc->xferred_len / block_sz); | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |           // Application can consume smaller bytes
 | 
					
						
							| 
									
										
										
										
											2018-07-25 20:34:56 +07:00
										 |  |  |           int32_t nbytes = tud_msc_write10_cb(p_cbw->lun, lba, p_msc->xferred_len % block_sz, _mscd_buf, xferred_bytes); | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |           if ( nbytes < 0 ) | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             // negative means error -> skip to status phase, status in CSW set to failed
 | 
					
						
							| 
									
										
										
										
											2018-10-23 12:33:21 +07:00
										 |  |  |             p_csw->data_residue = p_cbw->total_bytes - p_msc->xferred_len; | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  |             p_csw->status       = MSC_CSW_STATUS_FAILED; | 
					
						
							| 
									
										
										
										
											2018-07-26 15:59:13 +07:00
										 |  |  |             p_msc->stage        = MSC_STAGE_STATUS; | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-26 15:59:13 +07:00
										 |  |  |             tud_msc_set_sense(p_cbw->lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00); // Sense = Invalid Command Operation
 | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  |             break; | 
					
						
							|  |  |  |           }else | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             // Application consume less than what we got (including zero)
 | 
					
						
							| 
									
										
										
										
											2018-10-24 23:55:10 -07:00
										 |  |  |             if ( nbytes < (int32_t) xferred_bytes ) | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  |             { | 
					
						
							|  |  |  |               if ( nbytes > 0 ) | 
					
						
							|  |  |  |               { | 
					
						
							|  |  |  |                 p_msc->xferred_len += nbytes; | 
					
						
							|  |  |  |                 memmove(_mscd_buf, _mscd_buf+nbytes, xferred_bytes-nbytes); | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-25 22:21:14 +07:00
										 |  |  |               // simulate an transfer complete with adjusted parameters --> this driver callback will fired again
 | 
					
						
							| 
									
										
										
										
											2018-11-23 15:22:46 +07:00
										 |  |  |               dcd_event_xfer_complete(rhport, p_msc->ep_out, xferred_bytes-nbytes, XFER_RESULT_SUCCESS, false); | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-12 13:12:06 +07:00
										 |  |  |               return true; // skip the rest
 | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  |             } | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2018-07-26 15:59:13 +07:00
										 |  |  |               // Application consume all bytes in our buffer. Nothing to do, process with normal flow
 | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  |             } | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2018-04-18 13:46:05 +07:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-03-23 11:53:49 +07:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-04-19 16:10:52 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  |       // Accumulate data so far
 | 
					
						
							| 
									
										
										
										
											2018-04-19 16:10:52 +07:00
										 |  |  |       p_msc->xferred_len += xferred_bytes; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-23 12:33:21 +07:00
										 |  |  |       if ( p_msc->xferred_len >= p_msc->total_len ) | 
					
						
							| 
									
										
										
										
											2018-04-19 16:10:52 +07:00
										 |  |  |       { | 
					
						
							|  |  |  |         // Data Stage is complete
 | 
					
						
							|  |  |  |         p_msc->stage = MSC_STAGE_STATUS; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-04-18 13:46:05 +07:00
										 |  |  |       else | 
					
						
							| 
									
										
										
										
											2018-03-23 11:53:49 +07:00
										 |  |  |       { | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  |         // READ10 & WRITE10 Can be executed with large bulk of data e.g write 8K bytes (several flash write)
 | 
					
						
							|  |  |  |         // We break it into multiple smaller command whose data size is up to CFG_TUD_MSC_BUFSIZE
 | 
					
						
							|  |  |  |         if (SCSI_CMD_READ_10 == p_cbw->command[0]) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           proc_read10_cmd(rhport, p_msc); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (SCSI_CMD_WRITE_10 == p_cbw->command[0]) | 
					
						
							| 
									
										
										
										
											2018-04-18 13:46:05 +07:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  |           proc_write10_cmd(rhport, p_msc); | 
					
						
							| 
									
										
										
										
											2018-04-19 16:10:52 +07:00
										 |  |  |         }else | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           // No other command take more than one transfer yet -> unlikely error
 | 
					
						
							| 
									
										
										
										
											2018-10-24 12:37:43 +07:00
										 |  |  |           TU_BREAKPOINT(); | 
					
						
							| 
									
										
										
										
											2018-04-18 13:46:05 +07:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-11-25 16:42:04 +07:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-03-22 17:43:13 +07:00
										 |  |  |     break; | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-18 13:46:05 +07:00
										 |  |  |     case MSC_STAGE_STATUS: break; // processed immediately after this switch
 | 
					
						
							| 
									
										
										
										
											2018-03-22 17:43:13 +07:00
										 |  |  |     default : break; | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 17:43:13 +07:00
										 |  |  |   if ( p_msc->stage == MSC_STAGE_STATUS ) | 
					
						
							| 
									
										
										
										
											2013-11-26 13:15:40 +07:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2018-07-25 22:21:14 +07:00
										 |  |  |     // Either endpoints is stalled, need to wait until it is cleared by host
 | 
					
						
							| 
									
										
										
										
											2019-03-27 16:09:49 +07:00
										 |  |  |     if ( usbd_edpt_stalled(rhport,  p_msc->ep_in) || usbd_edpt_stalled(rhport,  p_msc->ep_out) ) | 
					
						
							| 
									
										
										
										
											2018-04-18 17:12:58 +07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-07-25 22:21:14 +07:00
										 |  |  |       // simulate an transfer complete with adjusted parameters --> this driver callback will fired again
 | 
					
						
							| 
									
										
										
										
											2018-11-23 15:22:46 +07:00
										 |  |  |       dcd_event_xfer_complete(rhport, p_msc->ep_out, 0, XFER_RESULT_SUCCESS, false); | 
					
						
							| 
									
										
										
										
											2018-06-24 00:16:22 +07:00
										 |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							| 
									
										
										
										
											2018-04-18 17:12:58 +07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-02-22 01:31:13 +07:00
										 |  |  |       // Move to default CMD stage when sending status
 | 
					
						
							|  |  |  |       p_msc->stage = MSC_STAGE_CMD; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Send SCSI Status
 | 
					
						
							| 
									
										
										
										
											2019-05-29 16:55:15 +07:00
										 |  |  |       TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_in , (uint8_t*) &p_msc->csw, sizeof(msc_csw_t)) ); | 
					
						
							| 
									
										
										
										
											2019-02-22 01:31:13 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-25 22:21:14 +07:00
										 |  |  |       // Invoke complete callback if defined
 | 
					
						
							|  |  |  |       if ( SCSI_CMD_READ_10 == p_cbw->command[0]) | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         if ( tud_msc_read10_complete_cb ) tud_msc_read10_complete_cb(p_cbw->lun); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       else if ( SCSI_CMD_WRITE_10 == p_cbw->command[0] ) | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         if ( tud_msc_write10_complete_cb ) tud_msc_write10_complete_cb(p_cbw->lun); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         if ( tud_msc_scsi_complete_cb ) tud_msc_scsi_complete_cb(p_cbw->lun, p_cbw->command); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-04-18 17:12:58 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-22 01:31:13 +07:00
										 |  |  |       // Queue for the next CBW
 | 
					
						
							| 
									
										
										
										
											2019-05-29 16:55:15 +07:00
										 |  |  |       TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_out, (uint8_t*) &p_msc->cbw, sizeof(msc_cbw_t)) ); | 
					
						
							| 
									
										
										
										
											2018-07-25 22:21:14 +07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-11-26 13:15:40 +07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-12 13:12:06 +07:00
										 |  |  |   return true; | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-04 14:39:32 +07:00
										 |  |  | /*------------------------------------------------------------------*/ | 
					
						
							|  |  |  | /* SCSI Command Process
 | 
					
						
							|  |  |  |  *------------------------------------------------------------------*/ | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  | static void proc_read10_cmd(uint8_t rhport, mscd_interface_t* p_msc) | 
					
						
							| 
									
										
										
										
											2014-03-09 15:30:57 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-20 20:23:22 +07:00
										 |  |  |   msc_cbw_t const * p_cbw = &p_msc->cbw; | 
					
						
							|  |  |  |   msc_csw_t       * p_csw = &p_msc->csw; | 
					
						
							| 
									
										
										
										
											2014-03-09 15:30:57 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-23 12:33:21 +07:00
										 |  |  |   uint16_t const block_sz = p_cbw->total_bytes / rdwr10_get_blockcount(p_cbw->command); | 
					
						
							| 
									
										
										
										
											2018-06-19 22:59:54 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Adjust lba with transferred bytes
 | 
					
						
							|  |  |  |   uint32_t const lba = rdwr10_get_lba(p_cbw->command) + (p_msc->xferred_len / block_sz); | 
					
						
							| 
									
										
										
										
											2018-03-23 11:53:49 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  |   // remaining bytes capped at class buffer
 | 
					
						
							| 
									
										
										
										
											2018-10-23 12:33:21 +07:00
										 |  |  |   int32_t nbytes = (int32_t) tu_min32(sizeof(_mscd_buf), p_cbw->total_bytes-p_msc->xferred_len); | 
					
						
							| 
									
										
										
										
											2018-03-22 17:43:13 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  |   // Application can consume smaller bytes
 | 
					
						
							| 
									
										
										
										
											2018-07-25 20:34:56 +07:00
										 |  |  |   nbytes = tud_msc_read10_cb(p_cbw->lun, lba, p_msc->xferred_len % block_sz, _mscd_buf, (uint32_t) nbytes); | 
					
						
							| 
									
										
										
										
											2018-03-22 17:43:13 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  |   if ( nbytes < 0 ) | 
					
						
							| 
									
										
										
										
											2018-03-22 17:43:13 +07:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  |     // negative means error -> pipe is stalled & status in CSW set to failed
 | 
					
						
							| 
									
										
										
										
											2018-10-23 12:33:21 +07:00
										 |  |  |     p_csw->data_residue = p_cbw->total_bytes - p_msc->xferred_len; | 
					
						
							| 
									
										
										
										
											2014-03-09 15:30:57 +07:00
										 |  |  |     p_csw->status       = MSC_CSW_STATUS_FAILED; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-26 15:59:13 +07:00
										 |  |  |     tud_msc_set_sense(p_cbw->lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00); // Sense = Invalid Command Operation
 | 
					
						
							| 
									
										
										
										
											2019-03-27 16:09:49 +07:00
										 |  |  |     usbd_edpt_stall(rhport, p_msc->ep_in); | 
					
						
							| 
									
										
										
										
											2018-04-20 20:23:22 +07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  |   else if ( nbytes == 0 ) | 
					
						
							| 
									
										
										
										
											2018-04-20 20:23:22 +07:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2018-07-25 22:21:14 +07:00
										 |  |  |     // zero means not ready -> simulate an transfer complete so that this driver callback will fired again
 | 
					
						
							| 
									
										
										
										
											2018-11-23 15:22:46 +07:00
										 |  |  |     dcd_event_xfer_complete(rhport, p_msc->ep_in, 0, XFER_RESULT_SUCCESS, false); | 
					
						
							| 
									
										
										
										
											2018-04-20 20:23:22 +07:00
										 |  |  |   } | 
					
						
							|  |  |  |   else | 
					
						
							| 
									
										
										
										
											2014-03-09 15:30:57 +07:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2019-05-29 16:55:15 +07:00
										 |  |  |     TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_in, _mscd_buf, nbytes), ); | 
					
						
							| 
									
										
										
										
											2014-03-09 15:30:57 +07:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  | static void proc_write10_cmd(uint8_t rhport, mscd_interface_t* p_msc) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   msc_cbw_t const * p_cbw = &p_msc->cbw; | 
					
						
							| 
									
										
										
										
											2019-02-20 15:12:36 -08:00
										 |  |  |   bool writable = true; | 
					
						
							|  |  |  |   if (tud_msc_is_writable_cb) { | 
					
						
							|  |  |  |     writable = tud_msc_is_writable_cb(p_cbw->lun); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (!writable) { | 
					
						
							|  |  |  |     msc_csw_t* p_csw = &p_msc->csw; | 
					
						
							|  |  |  |     p_csw->data_residue = p_cbw->total_bytes; | 
					
						
							|  |  |  |     p_csw->status       = MSC_CSW_STATUS_FAILED; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     tud_msc_set_sense(p_cbw->lun, SCSI_SENSE_DATA_PROTECT, 0x27, 0x00); // Sense = Write protected
 | 
					
						
							| 
									
										
										
										
											2019-03-27 16:09:49 +07:00
										 |  |  |     usbd_edpt_stall(rhport, p_msc->ep_out); | 
					
						
							| 
									
										
										
										
											2019-02-20 15:12:36 -08:00
										 |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // remaining bytes capped at class buffer
 | 
					
						
							| 
									
										
										
										
											2018-10-23 12:33:21 +07:00
										 |  |  |   int32_t nbytes = (int32_t) tu_min32(sizeof(_mscd_buf), p_cbw->total_bytes-p_msc->xferred_len); | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Write10 callback will be called later when usb transfer complete
 | 
					
						
							| 
									
										
										
										
											2019-05-29 16:55:15 +07:00
										 |  |  |   TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_out, _mscd_buf, nbytes), ); | 
					
						
							| 
									
										
										
										
											2018-06-07 12:10:07 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-01 12:11:26 +07:00
										 |  |  | #endif
 |