2008-10-25 00:42:28 +02:00
|
|
|
/****************************************************************************
|
2012-01-26 15:24:15 +01:00
|
|
|
* drivers/usbdev/usbmsc_scsi.c
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
2020-04-01 18:13:52 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
2020-04-01 18:13:52 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
2020-04-01 18:13:52 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-04 07:10:42 +01:00
|
|
|
/* Mass storage class device. Bulk-only with SCSI subclass. */
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* References:
|
2008-10-25 00:42:28 +02:00
|
|
|
* "Universal Serial Bus Mass Storage Class, Specification Overview,"
|
|
|
|
* Revision 1.2, USB Implementer's Forum, June 23, 2003.
|
|
|
|
*
|
|
|
|
* "Universal Serial Bus Mass Storage Class, Bulk-Only Transport,"
|
|
|
|
* Revision 1.0, USB Implementer's Forum, September 31, 1999.
|
|
|
|
*
|
|
|
|
* "SCSI Primary Commands - 3 (SPC-3)," American National Standard
|
|
|
|
* for Information Technology, May 4, 2005
|
|
|
|
*
|
|
|
|
* "SCSI Primary Commands - 4 (SPC-4)," American National Standard
|
|
|
|
* for Information Technology, July 19, 2008
|
|
|
|
*
|
|
|
|
* "SCSI Block Commands -2 (SBC-2)," American National Standard
|
|
|
|
* for Information Technology, November 13, 2004
|
2020-04-01 18:13:52 +02:00
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2010-12-04 18:35:19 +01:00
|
|
|
#include <unistd.h>
|
2014-03-25 21:19:27 +01:00
|
|
|
#include <stdlib.h>
|
2008-10-25 00:42:28 +02:00
|
|
|
#include <string.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2008-10-25 00:42:28 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <queue.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
#include <nuttx/irq.h>
|
2014-03-25 21:19:27 +01:00
|
|
|
#include <nuttx/kthread.h>
|
2008-10-25 00:42:28 +02:00
|
|
|
#include <nuttx/arch.h>
|
2019-11-30 00:37:39 +01:00
|
|
|
#include <nuttx/signal.h>
|
2008-10-25 00:42:28 +02:00
|
|
|
#include <nuttx/scsi.h>
|
2011-01-12 03:12:41 +01:00
|
|
|
#include <nuttx/usb/storage.h>
|
2010-12-14 04:33:39 +01:00
|
|
|
#include <nuttx/usb/usbdev.h>
|
|
|
|
#include <nuttx/usb/usbdev_trace.h>
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2012-01-26 15:24:15 +01:00
|
|
|
#include "usbmsc.h"
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2009-12-15 15:25:14 +01:00
|
|
|
* Pre-processor Definitions
|
2008-10-25 00:42:28 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2010-04-04 18:21:13 +02:00
|
|
|
/* Configuration ************************************************************/
|
|
|
|
|
2014-03-20 22:20:50 +01:00
|
|
|
/* Race condition workaround found by David Hewson. This race condition:
|
|
|
|
*
|
2010-04-04 18:21:13 +02:00
|
|
|
* "seems to relate to stalling the endpoint when a short response is
|
2014-03-20 22:20:50 +01:00
|
|
|
* generated which causes a residue to exist when the CSW would be returned.
|
|
|
|
* I think there's two issues here. The first being if the transfer which
|
|
|
|
* had just been queued before the stall had not completed then it wouldn't
|
|
|
|
* then complete once the endpoint was stalled? The second is that the
|
|
|
|
* subsequent transfer for the CSW would be dropped on the floor (by the
|
|
|
|
* epsubmit() function) if the end point was still stalled as the control
|
|
|
|
* transfer to resume it hadn't occurred."
|
|
|
|
*
|
|
|
|
* If queuing of stall requests is supported by DCD then this workaround is
|
|
|
|
* not required. In this case, (1) the stall is not sent until all write
|
|
|
|
* requests preceding the stall request are sent, (2) the stall is sent,
|
|
|
|
* and then after the stall is cleared, (3) all write requests queued after
|
|
|
|
* the stall are sent.
|
2010-04-04 18:21:13 +02:00
|
|
|
*/
|
|
|
|
|
2014-03-20 22:20:50 +01:00
|
|
|
#ifndef CONFIG_ARCH_USBDEV_STALLQUEUE
|
|
|
|
# define USBMSC_STALL_RACEWAR 1
|
|
|
|
#endif
|
2010-04-04 18:21:13 +02:00
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Debug ********************************************************************/
|
|
|
|
|
2016-06-11 19:50:18 +02:00
|
|
|
#if defined(CONFIG_DEBUG_INFO) && defined (CONFIG_DEBUG_USB)
|
2012-01-25 21:17:59 +01:00
|
|
|
static void usbmsc_dumpdata(const char *msg, const uint8_t *buf,
|
|
|
|
int buflen);
|
2008-10-25 00:42:28 +02:00
|
|
|
#else
|
2012-01-25 21:17:59 +01:00
|
|
|
# define usbmsc_dumpdata(msg, buf, len)
|
2008-10-25 00:42:28 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Utility Support Functions ************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static uint16_t usbmsc_getbe16(uint8_t *buf);
|
|
|
|
static uint32_t usbmsc_getbe32(uint8_t *buf);
|
|
|
|
static void usbmsc_putbe16(uint8_t * buf, uint16_t val);
|
|
|
|
static void usbmsc_putbe24(uint8_t *buf, uint32_t val);
|
|
|
|
static void usbmsc_putbe32(uint8_t *buf, uint32_t val);
|
2008-10-25 00:42:28 +02:00
|
|
|
#if 0 /* not used */
|
2012-01-25 21:17:59 +01:00
|
|
|
static uint16_t usbmsc_getle16(uint8_t *buf);
|
2008-10-25 00:42:28 +02:00
|
|
|
#endif
|
2012-01-25 21:17:59 +01:00
|
|
|
static uint32_t usbmsc_getle32(uint8_t *buf);
|
2008-10-25 00:42:28 +02:00
|
|
|
#if 0 /* not used */
|
2012-01-25 21:17:59 +01:00
|
|
|
static void usbmsc_putle16(uint8_t * buf, uint16_t val);
|
2008-10-25 00:42:28 +02:00
|
|
|
#endif
|
2012-01-25 21:17:59 +01:00
|
|
|
static void usbmsc_putle32(uint8_t *buf, uint32_t val);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* SCSI Command Processing **************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdtestunitready(FAR struct usbmsc_dev_s *priv);
|
|
|
|
static inline int usbmsc_cmdrequestsense(FAR struct usbmsc_dev_s *priv,
|
2009-12-15 15:25:14 +01:00
|
|
|
FAR uint8_t *buf);
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdread6(FAR struct usbmsc_dev_s *priv);
|
|
|
|
static inline int usbmsc_cmdwrite6(FAR struct usbmsc_dev_s *priv);
|
|
|
|
static inline int usbmsc_cmdinquiry(FAR struct usbmsc_dev_s *priv,
|
2009-12-15 15:25:14 +01:00
|
|
|
FAR uint8_t *buf);
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdmodeselect6(FAR struct usbmsc_dev_s *priv);
|
|
|
|
static int usbmsc_modepage(FAR struct usbmsc_dev_s *priv,
|
2009-12-15 15:25:14 +01:00
|
|
|
FAR uint8_t *buf, uint8_t pcpgcode, int *mdlen);
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdmodesense6(FAR struct usbmsc_dev_s *priv,
|
2009-12-15 15:25:14 +01:00
|
|
|
FAR uint8_t *buf);
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdstartstopunit(FAR struct usbmsc_dev_s *priv);
|
2020-04-01 18:13:52 +02:00
|
|
|
static inline int usbmsc_cmdpreventmediumremoval(
|
|
|
|
FAR struct usbmsc_dev_s *priv);
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdreadformatcapacity(FAR struct usbmsc_dev_s *priv,
|
2009-12-15 15:25:14 +01:00
|
|
|
FAR uint8_t *buf);
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdreadcapacity10(FAR struct usbmsc_dev_s *priv,
|
2009-12-15 15:25:14 +01:00
|
|
|
FAR uint8_t *buf);
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdread10(FAR struct usbmsc_dev_s *priv);
|
|
|
|
static inline int usbmsc_cmdwrite10(FAR struct usbmsc_dev_s *priv);
|
|
|
|
static inline int usbmsc_cmdverify10(FAR struct usbmsc_dev_s *priv);
|
2020-04-01 18:13:52 +02:00
|
|
|
static inline int usbmsc_cmdsynchronizecache10(
|
|
|
|
FAR struct usbmsc_dev_s *priv);
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdmodeselect10(FAR struct usbmsc_dev_s *priv);
|
|
|
|
static inline int usbmsc_cmdmodesense10(FAR struct usbmsc_dev_s *priv,
|
2009-12-15 15:25:14 +01:00
|
|
|
FAR uint8_t *buf);
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdread12(FAR struct usbmsc_dev_s *priv);
|
|
|
|
static inline int usbmsc_cmdwrite12(FAR struct usbmsc_dev_s *priv);
|
|
|
|
static inline int usbmsc_setupcmd(FAR struct usbmsc_dev_s *priv,
|
2009-12-15 15:25:14 +01:00
|
|
|
uint8_t cdblen, uint8_t flags);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* SCSI Worker Thread *******************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static int usbmsc_idlestate(FAR struct usbmsc_dev_s *priv);
|
|
|
|
static int usbmsc_cmdparsestate(FAR struct usbmsc_dev_s *priv);
|
|
|
|
static int usbmsc_cmdreadstate(FAR struct usbmsc_dev_s *priv);
|
|
|
|
static int usbmsc_cmdwritestate(FAR struct usbmsc_dev_s *priv);
|
|
|
|
static int usbmsc_cmdfinishstate(FAR struct usbmsc_dev_s *priv);
|
|
|
|
static int usbmsc_cmdstatusstate(FAR struct usbmsc_dev_s *priv);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-01-18 02:36:37 +01:00
|
|
|
static const char *g_productrevision = "0101";
|
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Debug
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_dumpdata
|
2008-10-25 00:42:28 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2016-06-11 19:50:18 +02:00
|
|
|
#if defined(CONFIG_DEBUG_INFO) && defined (CONFIG_DEBUG_USB)
|
2012-01-25 21:17:59 +01:00
|
|
|
static void usbmsc_dumpdata(const char *msg, const uint8_t *buf, int buflen)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2016-06-20 16:57:08 +02:00
|
|
|
syslog(LOG_DEBUG, "%s:", msg);
|
2008-10-25 00:42:28 +02:00
|
|
|
for (i = 0; i < buflen; i++)
|
|
|
|
{
|
2016-06-20 16:57:08 +02:00
|
|
|
syslog(LOG_DEBUG, " %02x", buf[i]);
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
2014-10-08 18:18:58 +02:00
|
|
|
|
2016-06-20 16:57:08 +02:00
|
|
|
syslog(LOG_DEBUG, "\n");
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Utility Support Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_getbe16
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get a 16-bit big-endian value reference by the byte pointer
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static uint16_t usbmsc_getbe16(uint8_t *buf)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2009-12-15 15:25:14 +01:00
|
|
|
return ((uint16_t)buf[0] << 8) | ((uint16_t)buf[1]);
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_getbe32
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get a 32-bit big-endian value reference by the byte pointer
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static uint32_t usbmsc_getbe32(uint8_t *buf)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2009-12-15 15:25:14 +01:00
|
|
|
return ((uint32_t)buf[0] << 24) | ((uint32_t)buf[1] << 16) |
|
|
|
|
((uint32_t)buf[2] << 8) | ((uint32_t)buf[3]);
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_putbe16
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Store a 16-bit value in big-endian order to the location specified by
|
|
|
|
* a byte pointer
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static void usbmsc_putbe16(uint8_t * buf, uint16_t val)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
|
|
|
buf[0] = val >> 8;
|
|
|
|
buf[1] = val;
|
|
|
|
}
|
|
|
|
|
2008-10-28 01:31:22 +01:00
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_putbe24
|
2008-10-28 01:31:22 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Store a 32-bit value in big-endian order to the location specified by
|
|
|
|
* a byte pointer
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static void usbmsc_putbe24(uint8_t *buf, uint32_t val)
|
2008-10-28 01:31:22 +01:00
|
|
|
{
|
|
|
|
buf[0] = val >> 16;
|
|
|
|
buf[1] = val >> 8;
|
|
|
|
buf[2] = val;
|
|
|
|
}
|
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_putbe32
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Store a 32-bit value in big-endian order to the location specified by
|
|
|
|
* a byte pointer
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static void usbmsc_putbe32(uint8_t *buf, uint32_t val)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
|
|
|
buf[0] = val >> 24;
|
|
|
|
buf[1] = val >> 16;
|
|
|
|
buf[2] = val >> 8;
|
|
|
|
buf[3] = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_getle16
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get a 16-bit little-endian value reference by the byte pointer
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if 0 /* not used */
|
2012-01-25 21:17:59 +01:00
|
|
|
static uint16_t usbmsc_getle16(uint8_t *buf)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2009-12-15 15:25:14 +01:00
|
|
|
return ((uint16_t)buf[1] << 8) | ((uint16_t)buf[0]);
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_getle32
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get a 32-bit little-endian value reference by the byte pointer
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static uint32_t usbmsc_getle32(uint8_t *buf)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2009-12-15 15:25:14 +01:00
|
|
|
return ((uint32_t)buf[3] << 24) | ((uint32_t)buf[2] << 16) |
|
|
|
|
((uint32_t)buf[1] << 8) | ((uint32_t)buf[0]);
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_putle16
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Store a 16-bit value in little-endian order to the location specified by
|
|
|
|
* a byte pointer
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if 0 /* not used */
|
2012-01-25 21:17:59 +01:00
|
|
|
static void usbmsc_putle16(uint8_t * buf, uint16_t val)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
|
|
|
buf[0] = val;
|
|
|
|
buf[1] = val >> 8;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_putle32
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Store a 32-bit value in little-endian order to the location specified by
|
|
|
|
* a byte pointer
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static void usbmsc_putle32(uint8_t *buf, uint32_t val)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
|
|
|
buf[0] = val;
|
|
|
|
buf[1] = val >> 8;
|
|
|
|
buf[2] = val >> 16;
|
|
|
|
buf[3] = val >> 24;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* SCSI Worker Thread
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-03-25 21:19:27 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbmsc_scsi_wait
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Wait for a SCSI worker thread event.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
static int usbmsc_scsi_wait(FAR struct usbmsc_dev_s *priv)
|
2014-03-25 21:19:27 +01:00
|
|
|
{
|
2014-11-25 20:15:09 +01:00
|
|
|
irqstate_t flags;
|
2020-04-01 18:13:52 +02:00
|
|
|
int ret;
|
|
|
|
int ret2;
|
2014-03-25 21:19:27 +01:00
|
|
|
|
|
|
|
/* We must hold the SCSI lock to call this function */
|
|
|
|
|
|
|
|
DEBUGASSERT(priv->thlock.semcount < 1);
|
|
|
|
|
|
|
|
/* A flag is used to prevent driving up the semaphore count. This function
|
|
|
|
* is called (primarily) from the SCSI work thread so we must disable
|
|
|
|
* interrupts momentarily to assure that test of the flag and the wait fo
|
|
|
|
* the semaphore count are atomic. Interrupts will, of course, be re-
|
|
|
|
* enabled while we wait for the event.
|
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2014-03-25 21:19:27 +01:00
|
|
|
priv->thwaiting = true;
|
|
|
|
|
|
|
|
/* Relinquish our lock on the SCSI state data */
|
|
|
|
|
|
|
|
usbmsc_scsi_unlock(priv);
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* Now wait for a SCSI event to be signaled */
|
2014-03-25 21:19:27 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2020-04-01 18:13:52 +02:00
|
|
|
ret = nxsem_wait_uninterruptible(&priv->thwaitsem);
|
2014-03-25 21:19:27 +01:00
|
|
|
}
|
2020-04-01 18:13:52 +02:00
|
|
|
while (priv->thwaiting && ret >= 0);
|
2014-03-25 21:19:27 +01:00
|
|
|
|
|
|
|
/* Re-acquire our lock on the SCSI state data */
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
ret2 = usbmsc_scsi_lock(priv);
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2020-04-01 18:13:52 +02:00
|
|
|
return ret >= 0 ? ret2 : ret;
|
2014-03-25 21:19:27 +01:00
|
|
|
}
|
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_cmdtestunitready
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle the SCSI_CMD_TESTUNITREADY command
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdtestunitready(FAR struct usbmsc_dev_s *priv)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
priv->u.alloclen = 0;
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_setupcmd(priv, 6, USBMSC_FLAGS_DIRNONE);
|
2008-10-25 00:42:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_cmdrequestsense
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle the SCSI_CMD_REQUESTSENSE command
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdrequestsense(FAR struct usbmsc_dev_s *priv,
|
|
|
|
FAR uint8_t *buf)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2020-01-10 16:06:36 +01:00
|
|
|
FAR struct scsicmd_requestsense_s *request =
|
|
|
|
(FAR struct scsicmd_requestsense_s *)priv->cdb;
|
|
|
|
FAR struct scsiresp_fixedsensedata_s *response =
|
|
|
|
(FAR struct scsiresp_fixedsensedata_s *)buf;
|
2012-01-25 21:17:59 +01:00
|
|
|
FAR struct usbmsc_lun_s *lun;
|
2009-12-15 15:25:14 +01:00
|
|
|
uint32_t sd;
|
|
|
|
uint32_t sdinfo;
|
|
|
|
uint8_t cdblen;
|
|
|
|
int ret;
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Extract the host allocation length */
|
|
|
|
|
|
|
|
priv->u.alloclen = request->alloclen;
|
|
|
|
|
|
|
|
/* Get the expected length of the command (with hack for MS-Windows 12-byte
|
|
|
|
* REQUEST SENSE command.
|
|
|
|
*/
|
|
|
|
|
|
|
|
cdblen = SCSICMD_REQUESTSENSE_SIZEOF;
|
|
|
|
if (cdblen != priv->cdblen)
|
|
|
|
{
|
|
|
|
/* Try MS-Windows REQUEST SENSE with cbw->cdblen == 12 */
|
|
|
|
|
|
|
|
cdblen = SCSICMD_REQUESTSENSE_MSSIZEOF;
|
|
|
|
}
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_setupcmd(priv, cdblen,
|
2020-04-01 18:13:52 +02:00
|
|
|
USBMSC_FLAGS_DIRDEVICE2HOST |
|
|
|
|
USBMSC_FLAGS_LUNNOTNEEDED |
|
|
|
|
USBMSC_FLAGS_UACOKAY |
|
|
|
|
USBMSC_FLAGS_RETAINSENSEDATA);
|
2008-10-25 00:42:28 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
lun = priv->lun;
|
|
|
|
if (!lun)
|
|
|
|
{
|
|
|
|
sd = SCSI_KCQIR_INVALIDLUN;
|
|
|
|
sdinfo = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Get the saved sense data from the LUN structure */
|
|
|
|
|
|
|
|
sd = lun->sd;
|
|
|
|
sdinfo = lun->sdinfo;
|
|
|
|
|
|
|
|
/* Discard the sense data */
|
|
|
|
|
|
|
|
lun->sd = SCSI_KCQ_NOSENSE;
|
|
|
|
lun->sdinfo = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the fixed sense data response */
|
|
|
|
|
|
|
|
memset(response, 0, SCSIRESP_FIXEDSENSEDATA_SIZEOF);
|
|
|
|
|
2020-01-10 16:06:36 +01:00
|
|
|
response->code = SCSIRESP_SENSEDATA_RESPVALID |
|
|
|
|
SCSIRESP_SENSEDATA_CURRENTFIXED;
|
2009-12-15 15:25:14 +01:00
|
|
|
response->flags = (uint8_t)(sd >> 16);
|
2012-01-25 21:17:59 +01:00
|
|
|
usbmsc_putbe32(response->info, sdinfo);
|
2008-10-25 00:42:28 +02:00
|
|
|
response->len = SCSIRESP_FIXEDSENSEDATA_SIZEOF - 7;
|
2009-12-15 15:25:14 +01:00
|
|
|
response->code2 = (uint8_t)(sd >> 8);
|
|
|
|
response->qual2 = (uint8_t)sd;
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
priv->nreqbytes = SCSIRESP_FIXEDSENSEDATA_SIZEOF;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_cmdread6
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle the SCSI_CMD_READ6 command
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdread6(FAR struct usbmsc_dev_s *priv)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2020-04-01 18:13:52 +02:00
|
|
|
FAR struct scsicmd_read6_s *read6 =
|
|
|
|
(FAR struct scsicmd_read6_s *)priv->cdb;
|
2014-01-09 05:49:20 +01:00
|
|
|
FAR struct usbmsc_lun_s *lun;
|
2008-10-25 00:42:28 +02:00
|
|
|
int ret;
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
priv->u.xfrlen = (uint16_t)read6->xfrlen;
|
2008-10-25 00:42:28 +02:00
|
|
|
if (priv->u.xfrlen == 0)
|
|
|
|
{
|
|
|
|
priv->u.xfrlen = 256;
|
|
|
|
}
|
|
|
|
|
2014-03-25 21:19:27 +01:00
|
|
|
ret = usbmsc_setupcmd(priv, SCSICMD_READ6_SIZEOF,
|
|
|
|
USBMSC_FLAGS_DIRDEVICE2HOST | USBMSC_FLAGS_BLOCKXFR);
|
2008-10-25 00:42:28 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2014-01-09 05:49:20 +01:00
|
|
|
lun = priv->lun;
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Get the Logical Block Address (LBA) from cdb[] as the starting
|
|
|
|
* sector
|
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
priv->sector =
|
|
|
|
(uint32_t)(read6->mslba & SCSICMD_READ6_MSLBAMASK) << 16 |
|
|
|
|
(uint32_t)usbmsc_getbe16(read6->lslba);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Verify that a block driver has been bound to the LUN */
|
|
|
|
|
|
|
|
if (!lun->inode)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_READ6MEDIANOTPRESENT), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQNR_MEDIANOTPRESENT;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that sector lies in the range supported by the block driver */
|
|
|
|
|
|
|
|
else if (priv->sector >= lun->nsectors)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_READ6LBARANGE), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQIR_LBAOUTOFRANGE;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Looks like we are good to go */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2014-03-25 21:19:27 +01:00
|
|
|
usbtrace(TRACE_CLASSSTATE(USBMSC_CLASSSTATE_CMDPARSECMDREAD6),
|
|
|
|
priv->cdb[0]);
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->thstate = USBMSC_STATE_CMDREAD;
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-03-25 21:19:27 +01:00
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_cmdwrite6
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle the SCSI_CMD_WRITE6 command
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdwrite6(FAR struct usbmsc_dev_s *priv)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2020-04-01 18:13:52 +02:00
|
|
|
FAR struct scsicmd_write6_s *write6 =
|
|
|
|
(FAR struct scsicmd_write6_s *)priv->cdb;
|
2014-01-09 05:49:20 +01:00
|
|
|
FAR struct usbmsc_lun_s *lun;
|
2008-10-25 00:42:28 +02:00
|
|
|
int ret;
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
priv->u.xfrlen = (uint16_t)write6->xfrlen;
|
2008-10-25 00:42:28 +02:00
|
|
|
if (priv->u.xfrlen == 0)
|
|
|
|
{
|
|
|
|
priv->u.xfrlen = 256;
|
|
|
|
}
|
|
|
|
|
2014-03-25 21:19:27 +01:00
|
|
|
ret = usbmsc_setupcmd(priv, SCSICMD_WRITE6_SIZEOF,
|
|
|
|
USBMSC_FLAGS_DIRHOST2DEVICE | USBMSC_FLAGS_BLOCKXFR);
|
2008-10-25 00:42:28 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2014-01-09 05:49:20 +01:00
|
|
|
lun = priv->lun;
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Get the Logical Block Address (LBA) from cdb[] as the starting
|
|
|
|
* sector
|
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
priv->sector =
|
|
|
|
(uint32_t)(write6->mslba & SCSICMD_WRITE6_MSLBAMASK) << 16 |
|
|
|
|
(uint32_t)usbmsc_getbe16(write6->lslba);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Verify that a block driver has been bound to the LUN */
|
|
|
|
|
|
|
|
if (!lun->inode)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_WRITE6MEDIANOTPRESENT), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQNR_MEDIANOTPRESENT;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for attempts to write to a read-only device */
|
|
|
|
|
|
|
|
else if (lun->readonly)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_WRITE6READONLY), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQWP_COMMANDNOTALLOWED;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that sector lies in the range supported by the block driver */
|
|
|
|
|
|
|
|
else if (priv->sector >= lun->nsectors)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_WRITE6LBARANGE), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQIR_LBAOUTOFRANGE;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Looks like we are good to go */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2020-01-10 16:06:36 +01:00
|
|
|
usbtrace(TRACE_CLASSSTATE(USBMSC_CLASSSTATE_CMDPARSECMDWRITE6),
|
|
|
|
priv->cdb[0]);
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->thstate = USBMSC_STATE_CMDWRITE;
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-03-25 21:19:27 +01:00
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_cmdinquiry
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle SCSI_CMD_INQUIRY command
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdinquiry(FAR struct usbmsc_dev_s *priv,
|
|
|
|
FAR uint8_t *buf)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2020-04-01 18:13:52 +02:00
|
|
|
FAR struct scscicmd_inquiry_s *inquiry =
|
|
|
|
(FAR struct scscicmd_inquiry_s *)priv->cdb;
|
|
|
|
FAR struct scsiresp_inquiry_s *response =
|
|
|
|
(FAR struct scsiresp_inquiry_s *)buf;
|
2008-10-25 00:42:28 +02:00
|
|
|
int len;
|
|
|
|
int ret;
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->u.alloclen = usbmsc_getbe16(inquiry->alloclen);
|
|
|
|
ret = usbmsc_setupcmd(priv, SCSICMD_INQUIRY_SIZEOF,
|
2020-04-01 18:13:52 +02:00
|
|
|
USBMSC_FLAGS_DIRDEVICE2HOST |
|
|
|
|
USBMSC_FLAGS_LUNNOTNEEDED |
|
2014-03-25 21:19:27 +01:00
|
|
|
USBMSC_FLAGS_UACOKAY);
|
2008-10-25 00:42:28 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
if (!priv->lun)
|
|
|
|
{
|
2020-01-10 16:06:36 +01:00
|
|
|
response->qualtype = SCSIRESP_INQUIRYPQ_NOTCAPABLE |
|
|
|
|
SCSIRESP_INQUIRYPD_UNKNOWN;
|
|
|
|
}
|
2021-06-13 16:58:22 +02:00
|
|
|
#ifndef CONFIG_USBMSC_NOT_STALL_BULKEP
|
2008-10-25 00:42:28 +02:00
|
|
|
else if ((inquiry->flags != 0) || (inquiry->pagecode != 0))
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_INQUIRYFLAGS), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
priv->lun->sd = SCSI_KCQIR_INVALIDFIELDINCBA;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
2021-06-13 16:58:22 +02:00
|
|
|
#endif
|
2008-10-25 00:42:28 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
memset(response, 0, SCSIRESP_INQUIRY_SIZEOF);
|
2014-03-20 21:25:56 +01:00
|
|
|
priv->nreqbytes = SCSIRESP_INQUIRY_SIZEOF;
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
#ifdef CONFIG_USBMSC_REMOVABLE
|
2008-10-25 00:42:28 +02:00
|
|
|
response->flags1 = SCSIRESP_INQUIRYFLAGS1_RMB;
|
|
|
|
#endif
|
|
|
|
response->version = 2; /* SCSI-2 */
|
|
|
|
response->flags2 = 2; /* SCSI-2 INQUIRY response data format */
|
2014-04-13 22:32:20 +02:00
|
|
|
response->len = SCSIRESP_INQUIRY_SIZEOF - 5;
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Strings */
|
|
|
|
|
2020-01-10 16:06:36 +01:00
|
|
|
memset(response->vendorid, ' ', 8 + 16 + 4);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2012-01-25 20:27:20 +01:00
|
|
|
len = strlen(g_mscvendorstr);
|
2013-02-28 18:34:57 +01:00
|
|
|
if (len > 8)
|
|
|
|
{
|
|
|
|
len = 8;
|
|
|
|
}
|
|
|
|
|
2012-01-25 20:27:20 +01:00
|
|
|
memcpy(response->vendorid, g_mscvendorstr, len);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2012-01-25 20:27:20 +01:00
|
|
|
len = strlen(g_mscproductstr);
|
2013-02-28 18:34:57 +01:00
|
|
|
if (len > 16)
|
|
|
|
{
|
|
|
|
len = 16;
|
|
|
|
}
|
|
|
|
|
2012-01-25 20:27:20 +01:00
|
|
|
memcpy(response->productid, g_mscproductstr, len);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2022-01-18 02:36:37 +01:00
|
|
|
len = strlen(g_productrevision);
|
2013-02-28 18:34:57 +01:00
|
|
|
if (len > 4)
|
|
|
|
{
|
|
|
|
len = 4;
|
|
|
|
}
|
|
|
|
|
2022-01-18 02:36:37 +01:00
|
|
|
memcpy(response->revision, g_productrevision, len);
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_cmdmodeselect6
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle SCSI_CMD_MODESELECT6 command
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdmodeselect6(FAR struct usbmsc_dev_s *priv)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2020-01-10 16:06:36 +01:00
|
|
|
FAR struct scsicmd_modeselect6_s *modeselect =
|
|
|
|
(FAR struct scsicmd_modeselect6_s *)priv->cdb;
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
priv->u.alloclen = modeselect->plen;
|
2020-01-02 17:49:34 +01:00
|
|
|
usbmsc_setupcmd(priv, SCSICMD_MODESELECT6_SIZEOF,
|
|
|
|
USBMSC_FLAGS_DIRHOST2DEVICE);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Not supported */
|
|
|
|
|
|
|
|
priv->lun->sd = SCSI_KCQIR_INVALIDCOMMAND;
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_modepage
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2012-01-25 21:17:59 +01:00
|
|
|
* Common logic for usbmsc_cmdmodesense6() and usbmsc_cmdmodesense10()
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static int usbmsc_modepage(FAR struct usbmsc_dev_s *priv, FAR uint8_t *buf,
|
|
|
|
uint8_t pcpgcode, int *mdlen)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2020-01-10 16:06:36 +01:00
|
|
|
FAR struct scsiresp_cachingmodepage_s *cmp =
|
|
|
|
(FAR struct scsiresp_cachingmodepage_s *)buf;
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Saving parms not supported */
|
|
|
|
|
|
|
|
if ((pcpgcode & SCSICMD_MODESENSE_PCMASK) == SCSICMD_MODESENSE_PCSAVED)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_PCSAVED), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
priv->lun->sd = SCSI_KCQIR_SAVINGPARMSNOTSUPPORTED;
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only the caching mode page is supported: */
|
|
|
|
|
2020-01-10 16:06:36 +01:00
|
|
|
if ((pcpgcode & SCSICMD_MODESENSE_PGCODEMASK) ==
|
|
|
|
SCSIRESP_MODESENSE_PGCCODE_CACHING ||
|
|
|
|
(pcpgcode & SCSICMD_MODESENSE_PGCODEMASK) ==
|
|
|
|
SCSIRESP_MODESENSE_PGCCODE_RETURNALL)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
|
|
|
memset(cmp, 0, 12);
|
|
|
|
cmp->pgcode = SCSIRESP_MODESENSE_PGCCODE_CACHING;
|
|
|
|
cmp->len = 10; /* n-2 */
|
|
|
|
|
|
|
|
/* None of the fields are changeable */
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
if (((pcpgcode & SCSICMD_MODESENSE_PCMASK) !=
|
|
|
|
SCSICMD_MODESENSE_PCCHANGEABLE))
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
|
|
|
cmp->flags1 = SCSIRESP_CACHINGMODEPG_WCE; /* Write cache enable */
|
2020-04-01 18:13:52 +02:00
|
|
|
|
|
|
|
/* Disable prefetch transfer length = 0xffffffff */
|
|
|
|
|
|
|
|
cmp->dpflen[0] = 0xff;
|
2008-10-25 00:42:28 +02:00
|
|
|
cmp->dpflen[1] = 0xff;
|
2020-04-01 18:13:52 +02:00
|
|
|
|
|
|
|
/* Maximum pre-fetch = 0xffffffff */
|
|
|
|
|
|
|
|
cmp->maxpf[0] = 0xff;
|
2008-10-25 00:42:28 +02:00
|
|
|
cmp->maxpf[1] = 0xff;
|
2020-04-01 18:13:52 +02:00
|
|
|
|
|
|
|
/* Maximum pref-fetch ceiling = 0xffffffff */
|
|
|
|
|
|
|
|
cmp->maxpfc[0] = 0xff;
|
2008-10-25 00:42:28 +02:00
|
|
|
cmp->maxpfc[1] = 0xff;
|
|
|
|
}
|
|
|
|
|
2020-01-10 16:06:36 +01:00
|
|
|
/* Return the mode data length */
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
*mdlen = 12; /* Only the first 12-bytes of caching mode page sent */
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_MODEPAGEFLAGS), pcpgcode);
|
2008-10-25 00:42:28 +02:00
|
|
|
priv->lun->sd = SCSI_KCQIR_INVALIDFIELDINCBA;
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_cmdmodesense6
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle SCSI_CMD_MODESENSE6 command
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static int inline usbmsc_cmdmodesense6(FAR struct usbmsc_dev_s *priv,
|
|
|
|
FAR uint8_t *buf)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2020-01-10 16:06:36 +01:00
|
|
|
FAR struct scsicmd_modesense6_s *modesense =
|
|
|
|
(FAR struct scsicmd_modesense6_s *)priv->cdb;
|
|
|
|
FAR struct scsiresp_modeparameterhdr6_s *mph =
|
|
|
|
(FAR struct scsiresp_modeparameterhdr6_s *)buf;
|
2021-06-13 16:58:22 +02:00
|
|
|
#ifndef CONFIG_USBMSC_NOT_STALL_BULKEP
|
2008-10-25 00:42:28 +02:00
|
|
|
int mdlen;
|
2021-06-13 16:58:22 +02:00
|
|
|
#endif
|
2008-10-25 00:42:28 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
priv->u.alloclen = modesense->alloclen;
|
2014-03-25 21:19:27 +01:00
|
|
|
ret = usbmsc_setupcmd(priv, SCSICMD_MODESENSE6_SIZEOF,
|
|
|
|
USBMSC_FLAGS_DIRDEVICE2HOST);
|
2008-10-25 00:42:28 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2021-06-13 16:58:22 +02:00
|
|
|
#ifdef CONFIG_USBMSC_NOT_STALL_BULKEP
|
|
|
|
priv->residue = priv->cbwlen = priv->nreqbytes =
|
|
|
|
SCSIRESP_MODEPARAMETERHDR6_SIZEOF;
|
|
|
|
#endif
|
|
|
|
|
2020-01-10 16:06:36 +01:00
|
|
|
if ((modesense->flags & ~SCSICMD_MODESENSE6_DBD) != 0 ||
|
|
|
|
modesense->subpgcode != 0)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_MODESENSE6FLAGS), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
priv->lun->sd = SCSI_KCQIR_INVALIDFIELDINCBA;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The response consists of:
|
|
|
|
*
|
|
|
|
* (1) A MODESENSE6-specific mode parameter header,
|
|
|
|
* (2) A variable length list of block descriptors, and
|
2008-10-28 18:20:09 +01:00
|
|
|
* (3) A variable length list of mode page formats
|
2008-10-25 00:42:28 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
mph->type = 0; /* Medium type */
|
2020-04-01 18:13:52 +02:00
|
|
|
mph->param =
|
|
|
|
(priv->lun->readonly ? SCSIRESP_MODEPARMHDR_DAPARM_WP : 0x00);
|
2008-10-25 00:42:28 +02:00
|
|
|
mph->bdlen = 0; /* Block descriptor length */
|
|
|
|
|
2021-06-13 16:58:22 +02:00
|
|
|
#ifndef CONFIG_USBMSC_NOT_STALL_BULKEP
|
2008-10-25 00:42:28 +02:00
|
|
|
/* There are no block descriptors, only the following mode page: */
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
ret = usbmsc_modepage(priv,
|
|
|
|
&buf[SCSIRESP_MODEPARAMETERHDR6_SIZEOF],
|
2020-01-10 16:06:36 +01:00
|
|
|
modesense->pcpgcode, &mdlen);
|
2008-10-25 00:42:28 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Store the mode data length and return the total message
|
|
|
|
* size
|
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
mph->mdlen =
|
|
|
|
mdlen + SCSIRESP_MODEPARAMETERHDR6_SIZEOF - 1;
|
|
|
|
priv->nreqbytes =
|
|
|
|
mdlen + SCSIRESP_MODEPARAMETERHDR6_SIZEOF;
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
2021-06-13 16:58:22 +02:00
|
|
|
#endif
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-03-25 21:19:27 +01:00
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_cmdstartstopunit
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle SCSI_CMD_STARTSTOPUNIT command
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdstartstopunit(FAR struct usbmsc_dev_s *priv)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
priv->u.alloclen = 0;
|
2014-03-25 21:19:27 +01:00
|
|
|
ret = usbmsc_setupcmd(priv, SCSICMD_STARTSTOPUNIT_SIZEOF,
|
|
|
|
USBMSC_FLAGS_DIRNONE);
|
2008-10-25 00:42:28 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
#ifndef CONFIG_USBMSC_REMOVABLE
|
2013-09-06 21:07:29 +02:00
|
|
|
FAR struct usbmsc_lun_s *lun = priv->lun;
|
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
/* This command is not valid if the media is not removable */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_NOTREMOVABLE), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQIR_INVALIDCOMMAND;
|
|
|
|
ret = -EINVAL;
|
|
|
|
#endif
|
|
|
|
}
|
2014-03-25 21:19:27 +01:00
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_cmdpreventmediumremoval
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle SCSI_CMD_PREVENTMEDIAREMOVAL command
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
static inline int
|
|
|
|
usbmsc_cmdpreventmediumremoval(FAR struct usbmsc_dev_s *priv)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
#ifdef CONFIG_USBMSC_REMOVABLE
|
2020-01-10 16:06:36 +01:00
|
|
|
FAR struct scsicmd_preventmediumremoval_s *pmr =
|
|
|
|
(FAR struct scsicmd_preventmediumremoval_s *)priv->cdb;
|
2008-10-25 00:42:28 +02:00
|
|
|
#endif
|
2014-01-09 05:49:20 +01:00
|
|
|
FAR struct usbmsc_lun_s *lun;
|
2008-10-25 00:42:28 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
priv->u.alloclen = 0;
|
2014-03-25 21:19:27 +01:00
|
|
|
ret = usbmsc_setupcmd(priv, SCSICMD_PREVENTMEDIUMREMOVAL_SIZEOF,
|
|
|
|
USBMSC_FLAGS_DIRNONE);
|
2008-10-25 00:42:28 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2014-01-09 05:49:20 +01:00
|
|
|
lun = priv->lun;
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
#ifndef CONFIG_USBMSC_REMOVABLE
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQIR_INVALIDCOMMAND;
|
|
|
|
ret = -EINVAL;
|
|
|
|
#else
|
|
|
|
if ((pmr->prevent & ~SCSICMD_PREVENTMEDIUMREMOVAL_TRANSPORT) != 0)
|
|
|
|
{
|
2020-04-01 18:13:52 +02:00
|
|
|
usbtrace(
|
|
|
|
TRACE_CLSERROR(USBMSC_TRACEERR_PREVENTMEDIUMREMOVALPREVENT),
|
|
|
|
0);
|
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQIR_INVALIDFIELDINCBA;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
lun->locked = pmr->prevent & SCSICMD_PREVENTMEDIUMREMOVAL_TRANSPORT;
|
|
|
|
#endif
|
|
|
|
}
|
2014-03-25 21:19:27 +01:00
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-10-28 01:31:22 +01:00
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_cmdreadformatcapacity
|
2008-10-28 01:31:22 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle SCSI_CMD_READFORMATCAPACITIES command (MMC)
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdreadformatcapacity(FAR struct usbmsc_dev_s *priv,
|
|
|
|
FAR uint8_t *buf)
|
2008-10-28 01:31:22 +01:00
|
|
|
{
|
2020-01-10 16:06:36 +01:00
|
|
|
FAR struct scsicmd_readformatcapcacities_s *rfc =
|
|
|
|
(FAR struct scsicmd_readformatcapcacities_s *)priv->cdb;
|
2008-10-28 01:31:22 +01:00
|
|
|
FAR struct scsiresp_readformatcapacities_s *hdr;
|
2014-01-09 05:49:20 +01:00
|
|
|
FAR struct usbmsc_lun_s *lun;
|
2008-10-28 01:31:22 +01:00
|
|
|
int ret;
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->u.alloclen = usbmsc_getbe16(rfc->alloclen);
|
2014-03-25 21:19:27 +01:00
|
|
|
ret = usbmsc_setupcmd(priv, SCSICMD_READFORMATCAPACITIES_SIZEOF,
|
|
|
|
USBMSC_FLAGS_DIRDEVICE2HOST);
|
2008-10-28 01:31:22 +01:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2014-01-09 05:49:20 +01:00
|
|
|
lun = priv->lun;
|
|
|
|
|
2008-10-28 01:31:22 +01:00
|
|
|
hdr = (FAR struct scsiresp_readformatcapacities_s *)buf;
|
|
|
|
memset(hdr, 0, SCSIRESP_READFORMATCAPACITIES_SIZEOF);
|
|
|
|
hdr->listlen = SCSIRESP_CURRCAPACITYDESC_SIZEOF;
|
|
|
|
|
|
|
|
/* Only the Current/Maximum Capacity Descriptor follows the header */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
usbmsc_putbe32(hdr->nblocks, lun->nsectors);
|
2008-10-28 01:31:22 +01:00
|
|
|
hdr->type = SCIRESP_RDFMTCAPACITIES_FORMATED;
|
2012-01-25 21:17:59 +01:00
|
|
|
usbmsc_putbe24(hdr->blocklen, lun->sectorsize);
|
2008-10-28 01:31:22 +01:00
|
|
|
priv->nreqbytes = SCSIRESP_READFORMATCAPACITIES_SIZEOF;
|
|
|
|
}
|
2014-03-25 21:19:27 +01:00
|
|
|
|
2008-10-28 01:31:22 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_cmdreadcapacity10
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle SCSI_CMD_READCAPACITY10 command
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static int inline usbmsc_cmdreadcapacity10(FAR struct usbmsc_dev_s *priv,
|
|
|
|
FAR uint8_t *buf)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2020-01-10 16:06:36 +01:00
|
|
|
FAR struct scsicmd_readcapacity10_s *rcc =
|
|
|
|
(FAR struct scsicmd_readcapacity10_s *)priv->cdb;
|
|
|
|
FAR struct scsiresp_readcapacity10_s *rcr =
|
|
|
|
(FAR struct scsiresp_readcapacity10_s *)buf;
|
2014-01-09 05:49:20 +01:00
|
|
|
FAR struct usbmsc_lun_s *lun;
|
2009-12-15 15:25:14 +01:00
|
|
|
uint32_t lba;
|
2008-10-25 00:42:28 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
priv->u.alloclen = SCSIRESP_READCAPACITY10_SIZEOF; /* Fake the allocation length */
|
2014-03-25 21:19:27 +01:00
|
|
|
ret = usbmsc_setupcmd(priv, SCSICMD_READCAPACITY10_SIZEOF,
|
|
|
|
USBMSC_FLAGS_DIRDEVICE2HOST);
|
2008-10-25 00:42:28 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2014-01-09 05:49:20 +01:00
|
|
|
lun = priv->lun;
|
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
/* Check the PMI and LBA fields */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
lba = usbmsc_getbe32(rcc->lba);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
if (rcc->pmi > 1 || (rcc->pmi == 0 && lba != 0))
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_READCAPACITYFLAGS), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQIR_INVALIDFIELDINCBA;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbmsc_putbe32(rcr->lba, lun->nsectors - 1);
|
|
|
|
usbmsc_putbe32(&buf[4], lun->sectorsize);
|
2008-10-25 00:42:28 +02:00
|
|
|
priv->nreqbytes = SCSIRESP_READCAPACITY10_SIZEOF;
|
|
|
|
}
|
|
|
|
}
|
2014-03-25 21:19:27 +01:00
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_cmdread10
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle SCSI_CMD_READ10 command
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdread10(FAR struct usbmsc_dev_s *priv)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
struct scsicmd_read10_s *read10 = (struct scsicmd_read10_s *)priv->cdb;
|
2014-01-09 05:49:20 +01:00
|
|
|
FAR struct usbmsc_lun_s *lun;
|
2008-10-25 00:42:28 +02:00
|
|
|
int ret;
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->u.xfrlen = usbmsc_getbe16(read10->xfrlen);
|
2014-03-25 21:19:27 +01:00
|
|
|
ret = usbmsc_setupcmd(priv, SCSICMD_READ10_SIZEOF,
|
|
|
|
USBMSC_FLAGS_DIRDEVICE2HOST | USBMSC_FLAGS_BLOCKXFR);
|
2008-10-25 00:42:28 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2014-01-09 05:49:20 +01:00
|
|
|
lun = priv->lun;
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Get the Logical Block Address (LBA) from cdb[] as the starting
|
|
|
|
* sector
|
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->sector = usbmsc_getbe32(read10->lba);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Verify that we can support this read command */
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
if ((read10->flags & ~(SCSICMD_READ10FLAGS_DPO |
|
|
|
|
SCSICMD_READ10FLAGS_FUA)) != 0)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_READ10FLAGS), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQIR_INVALIDFIELDINCBA;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that a block driver has been bound to the LUN */
|
|
|
|
|
|
|
|
else if (!lun->inode)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_READ10MEDIANOTPRESENT), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQNR_MEDIANOTPRESENT;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that LBA lies in the range supported by the block driver */
|
|
|
|
|
|
|
|
else if (priv->sector >= lun->nsectors)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_READ10LBARANGE), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQIR_LBAOUTOFRANGE;
|
|
|
|
ret = -EINVAL;
|
2020-01-10 16:06:36 +01:00
|
|
|
}
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Looks like we are good to go */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2020-01-10 16:06:36 +01:00
|
|
|
usbtrace(TRACE_CLASSSTATE(USBMSC_CLASSSTATE_CMDPARSECMDREAD10),
|
|
|
|
priv->cdb[0]);
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->thstate = USBMSC_STATE_CMDREAD;
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_cmdwrite10
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle SCSI_CMD_WRITE10 command
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdwrite10(FAR struct usbmsc_dev_s *priv)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
|
|
|
struct scsicmd_write10_s *write10 = (struct scsicmd_write10_s *)priv->cdb;
|
2014-01-09 05:49:20 +01:00
|
|
|
FAR struct usbmsc_lun_s *lun;
|
2008-10-25 00:42:28 +02:00
|
|
|
int ret;
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->u.xfrlen = usbmsc_getbe16(write10->xfrlen);
|
2014-03-25 21:19:27 +01:00
|
|
|
ret = usbmsc_setupcmd(priv, SCSICMD_WRITE10_SIZEOF,
|
|
|
|
USBMSC_FLAGS_DIRHOST2DEVICE | USBMSC_FLAGS_BLOCKXFR);
|
2008-10-25 00:42:28 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2014-01-09 05:49:20 +01:00
|
|
|
lun = priv->lun;
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Get the Logical Block Address (LBA) from cdb[] as the starting
|
|
|
|
* sector
|
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->sector = usbmsc_getbe32(write10->lba);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Verify that we can support this write command */
|
|
|
|
|
2020-01-10 16:06:36 +01:00
|
|
|
if ((write10->flags & ~(SCSICMD_WRITE10FLAGS_DPO |
|
|
|
|
SCSICMD_WRITE10FLAGS_FUA)) != 0)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_WRITE10FLAGS), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQIR_INVALIDFIELDINCBA;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that a block driver has been bound to the LUN */
|
|
|
|
|
|
|
|
else if (!lun->inode)
|
|
|
|
{
|
2020-04-01 18:13:52 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_WRITE10MEDIANOTPRESENT),
|
|
|
|
0);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQNR_MEDIANOTPRESENT;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for attempts to write to a read-only device */
|
|
|
|
|
|
|
|
else if (lun->readonly)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_WRITE10READONLY), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQWP_COMMANDNOTALLOWED;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-01-10 16:06:36 +01:00
|
|
|
/* Verify that LBA lies in the range supported by the block driver */
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2020-01-10 16:06:36 +01:00
|
|
|
else if (priv->sector >= lun->nsectors)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_WRITE10LBARANGE), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQIR_LBAOUTOFRANGE;
|
|
|
|
ret = -EINVAL;
|
2020-01-10 16:06:36 +01:00
|
|
|
}
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Looks like we are good to go */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2020-01-10 16:06:36 +01:00
|
|
|
usbtrace(TRACE_CLASSSTATE(USBMSC_CLASSSTATE_CMDPARSECMDWRITE10),
|
|
|
|
priv->cdb[0]);
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->thstate = USBMSC_STATE_CMDWRITE;
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-03-25 21:19:27 +01:00
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_cmdverify10
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle SCSI_CMD_VERIFY10 command
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdverify10(FAR struct usbmsc_dev_s *priv)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2020-04-01 18:13:52 +02:00
|
|
|
FAR struct scsicmd_verify10_s *verf =
|
|
|
|
(FAR struct scsicmd_verify10_s *)priv->cdb;
|
2014-01-09 05:49:20 +01:00
|
|
|
FAR struct usbmsc_lun_s *lun;
|
2009-12-15 15:25:14 +01:00
|
|
|
uint32_t lba;
|
|
|
|
uint16_t blocks;
|
2008-10-25 00:42:28 +02:00
|
|
|
size_t sector;
|
|
|
|
ssize_t nread;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
priv->u.alloclen = 0;
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_setupcmd(priv, SCSICMD_VERIFY10_SIZEOF, USBMSC_FLAGS_DIRNONE);
|
2008-10-25 00:42:28 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2014-01-09 05:49:20 +01:00
|
|
|
lun = priv->lun;
|
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
/* Verify the starting and ending LBA */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
lba = usbmsc_getbe32(verf->lba);
|
|
|
|
blocks = usbmsc_getbe16(verf->len);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
if ((verf->flags & ~SCSICMD_VERIFY10_DPO) != 0 || verf->groupno != 0)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_VERIFY10FLAGS), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQIR_INVALIDFIELDINCBA;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
else if (blocks == 0)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_VERIFY10NOBLOCKS), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
ret = -EIO; /* No reply */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that a block driver has been bound to the LUN */
|
|
|
|
|
|
|
|
else if (!lun->inode)
|
|
|
|
{
|
2020-04-01 18:13:52 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_VERIFY10MEDIANOTPRESENT),
|
|
|
|
0);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQNR_MEDIANOTPRESENT;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that LBA lies in the range supported by the block driver */
|
|
|
|
|
|
|
|
else if (lba >= lun->nsectors || lba + blocks > lun->nsectors)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_VERIFY10LBARANGE), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQIR_LBAOUTOFRANGE;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Try to read the requested blocks */
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
for (i = 0, sector = lba + lun->startsector;
|
|
|
|
i < blocks;
|
|
|
|
i++, sector++)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
nread = USBMSC_DRVR_READ(lun, priv->iobuffer, sector, 1);
|
2008-10-25 00:42:28 +02:00
|
|
|
if (nread < 0)
|
|
|
|
{
|
2020-04-01 18:13:52 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_VERIFY10READFAIL),
|
|
|
|
i);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQME_UNRRE1;
|
|
|
|
lun->sdinfo = sector;
|
|
|
|
ret = -EIO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-25 21:19:27 +01:00
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_cmdsynchronizecache10
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle SCSI_CMD_SYNCHCACHE10 command
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdsynchronizecache10(FAR struct usbmsc_dev_s *priv)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
priv->u.alloclen = 0;
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Verify that we have the LUN structure and the block driver has been
|
|
|
|
* bound
|
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
if (!priv->lun->inode)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_SYNCCACHEMEDIANOTPRESENT), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
priv->lun->sd = SCSI_KCQNR_MEDIANOTPRESENT;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-25 21:19:27 +01:00
|
|
|
ret = usbmsc_setupcmd(priv, SCSICMD_SYNCHRONIZECACHE10_SIZEOF,
|
|
|
|
USBMSC_FLAGS_DIRNONE);
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
2014-03-25 21:19:27 +01:00
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_cmdmodeselect10
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle SCSI_CMD_MODESELECT10 command
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdmodeselect10(FAR struct usbmsc_dev_s *priv)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2020-01-10 16:06:36 +01:00
|
|
|
FAR struct scsicmd_modeselect10_s *modeselect =
|
|
|
|
(FAR struct scsicmd_modeselect10_s *)priv->cdb;
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->u.alloclen = usbmsc_getbe16(modeselect->parmlen);
|
2020-01-02 17:49:34 +01:00
|
|
|
usbmsc_setupcmd(priv, SCSICMD_MODESELECT10_SIZEOF,
|
|
|
|
USBMSC_FLAGS_DIRHOST2DEVICE);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Not supported */
|
|
|
|
|
|
|
|
priv->lun->sd = SCSI_KCQIR_INVALIDCOMMAND;
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_cmdmodesense10
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle SCSI_CMD_MODESENSE10 command
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static int inline usbmsc_cmdmodesense10(FAR struct usbmsc_dev_s *priv,
|
|
|
|
FAR uint8_t *buf)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2020-01-10 16:06:36 +01:00
|
|
|
FAR struct scsicmd_modesense10_s *modesense =
|
|
|
|
(FAR struct scsicmd_modesense10_s *)priv->cdb;
|
|
|
|
FAR struct scsiresp_modeparameterhdr10_s *mph =
|
|
|
|
(FAR struct scsiresp_modeparameterhdr10_s *)buf;
|
2008-10-25 00:42:28 +02:00
|
|
|
int mdlen;
|
|
|
|
int ret;
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->u.alloclen = usbmsc_getbe16(modesense->alloclen);
|
2014-03-25 21:19:27 +01:00
|
|
|
ret = usbmsc_setupcmd(priv, SCSICMD_MODESENSE10_SIZEOF,
|
|
|
|
USBMSC_FLAGS_DIRDEVICE2HOST);
|
2008-10-25 00:42:28 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2014-03-25 21:19:27 +01:00
|
|
|
if ((modesense->flags & ~SCSICMD_MODESENSE10_DBD) != 0 ||
|
|
|
|
modesense->subpgcode != 0)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_MODESENSE10FLAGS), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
priv->lun->sd = SCSI_KCQIR_INVALIDFIELDINCBA;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The response consists of:
|
|
|
|
*
|
|
|
|
* (1) A MODESENSE6-specific mode parameter header,
|
|
|
|
* (2) A variable length list of block descriptors, and
|
|
|
|
* (3) A variable lengtth list of mode page formats
|
|
|
|
*/
|
|
|
|
|
2010-12-18 01:31:38 +01:00
|
|
|
memset(mph, 0, SCSIRESP_MODEPARAMETERHDR10_SIZEOF);
|
2020-04-01 18:13:52 +02:00
|
|
|
mph->param =
|
|
|
|
(priv->lun->readonly ? SCSIRESP_MODEPARMHDR_DAPARM_WP : 0x00);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* There are no block descriptors, only the following mode page: */
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
ret = usbmsc_modepage(priv,
|
|
|
|
&buf[SCSIRESP_MODEPARAMETERHDR10_SIZEOF],
|
2020-01-10 16:06:36 +01:00
|
|
|
modesense->pcpgcode, &mdlen);
|
2008-10-25 00:42:28 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Store the mode data length and return the total message
|
|
|
|
* size
|
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
usbmsc_putbe16(mph->mdlen, mdlen - 2);
|
2010-12-18 01:31:38 +01:00
|
|
|
priv->nreqbytes = mdlen + SCSIRESP_MODEPARAMETERHDR10_SIZEOF;
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-25 21:19:27 +01:00
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_cmdread12
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle SCSI_CMD_READ12 command
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdread12(FAR struct usbmsc_dev_s *priv)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
struct scsicmd_read12_s *read12 = (struct scsicmd_read12_s *)priv->cdb;
|
2014-01-09 05:49:20 +01:00
|
|
|
FAR struct usbmsc_lun_s *lun;
|
2008-10-25 00:42:28 +02:00
|
|
|
int ret;
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->u.xfrlen = usbmsc_getbe32(read12->xfrlen);
|
2014-03-25 21:19:27 +01:00
|
|
|
ret = usbmsc_setupcmd(priv, SCSICMD_READ12_SIZEOF,
|
|
|
|
USBMSC_FLAGS_DIRDEVICE2HOST | USBMSC_FLAGS_BLOCKXFR);
|
2008-10-25 00:42:28 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2014-01-09 05:49:20 +01:00
|
|
|
lun = priv->lun;
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Get the Logical Block Address (LBA) from cdb[] as the starting
|
|
|
|
* sector
|
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->sector = usbmsc_getbe32(read12->lba);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Verify that we can support this read command */
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
if ((read12->flags & ~(SCSICMD_READ12FLAGS_DPO |
|
|
|
|
SCSICMD_READ12FLAGS_FUA)) != 0)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_READ12FLAGS), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQIR_INVALIDFIELDINCBA;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that a block driver has been bound to the LUN */
|
|
|
|
|
|
|
|
else if (!lun->inode)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_READ12MEDIANOTPRESENT), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQNR_MEDIANOTPRESENT;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that LBA lies in the range supported by the block driver */
|
|
|
|
|
|
|
|
else if (priv->sector >= lun->nsectors)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_READ12LBARANGE), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQIR_LBAOUTOFRANGE;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Looks like we are good to go */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2020-01-10 16:06:36 +01:00
|
|
|
usbtrace(TRACE_CLASSSTATE(USBMSC_CLASSSTATE_CMDPARSECMDREAD12),
|
|
|
|
priv->cdb[0]);
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->thstate = USBMSC_STATE_CMDREAD;
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-03-25 21:19:27 +01:00
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_cmdwrite12
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle SCSI_CMD_WRITE12 command
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static inline int usbmsc_cmdwrite12(FAR struct usbmsc_dev_s *priv)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
|
|
|
struct scsicmd_write12_s *write12 = (struct scsicmd_write12_s *)priv->cdb;
|
2014-01-09 05:49:20 +01:00
|
|
|
FAR struct usbmsc_lun_s *lun;
|
2008-10-25 00:42:28 +02:00
|
|
|
int ret;
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->u.xfrlen = usbmsc_getbe32(write12->xfrlen);
|
2014-03-25 21:19:27 +01:00
|
|
|
ret = usbmsc_setupcmd(priv, SCSICMD_WRITE12_SIZEOF,
|
|
|
|
USBMSC_FLAGS_DIRHOST2DEVICE | USBMSC_FLAGS_BLOCKXFR);
|
2008-10-25 00:42:28 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2014-01-09 05:49:20 +01:00
|
|
|
lun = priv->lun;
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Get the Logical Block Address (LBA) from cdb[] as the starting
|
|
|
|
* sector
|
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->sector = usbmsc_getbe32(write12->lba);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Verify that we can support this write command */
|
|
|
|
|
2020-01-10 16:06:36 +01:00
|
|
|
if ((write12->flags & ~(SCSICMD_WRITE12FLAGS_DPO |
|
|
|
|
SCSICMD_WRITE12FLAGS_FUA)) != 0)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_WRITE12FLAGS), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQIR_INVALIDFIELDINCBA;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that a block driver has been bound to the LUN */
|
|
|
|
|
|
|
|
else if (!lun->inode)
|
|
|
|
{
|
2020-04-01 18:13:52 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_WRITE12MEDIANOTPRESENT),
|
|
|
|
0);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQNR_MEDIANOTPRESENT;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for attempts to write to a read-only device */
|
|
|
|
|
|
|
|
else if (lun->readonly)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_WRITE12READONLY), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQWP_COMMANDNOTALLOWED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that LBA lies in the range supported by the block driver */
|
|
|
|
|
|
|
|
else if (priv->sector >= lun->nsectors)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_WRITE12LBARANGE), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQIR_LBAOUTOFRANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Looks like we are good to go */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2020-01-10 16:06:36 +01:00
|
|
|
usbtrace(TRACE_CLASSSTATE(USBMSC_CLASSSTATE_CMDPARSECMDWRITE12),
|
|
|
|
priv->cdb[0]);
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->thstate = USBMSC_STATE_CMDWRITE;
|
2008-10-25 00:42:28 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_setupcmd
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Called after each SCSI command is identified in order to perform setup
|
|
|
|
* and verification operations that are common to all SCSI commands. This
|
|
|
|
* function performs the following common setup operations:
|
|
|
|
*
|
2014-03-25 21:19:27 +01:00
|
|
|
* 1. Determine the direction of the response
|
|
|
|
* 2. Verify lengths
|
|
|
|
* 3. Setup and verify the LUN
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Includes special logic for INQUIRY and REQUESTSENSE commands
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-03-25 21:19:27 +01:00
|
|
|
static int inline usbmsc_setupcmd(FAR struct usbmsc_dev_s *priv,
|
|
|
|
uint8_t cdblen, uint8_t flags)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
FAR struct usbmsc_lun_s *lun = NULL;
|
2009-12-15 15:25:14 +01:00
|
|
|
uint32_t datlen;
|
2015-03-12 14:55:53 +01:00
|
|
|
uint8_t dir;
|
2014-03-25 21:19:27 +01:00
|
|
|
int ret = OK;
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Verify the LUN and set up the current LUN reference in the
|
|
|
|
* device structure
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->cbwlun < priv->nluns)
|
|
|
|
{
|
|
|
|
/* LUN number is valid in a valid range, but the LUN is not necessarily
|
|
|
|
* bound to a block driver. That will be checked as necessary in each
|
|
|
|
* individual command.
|
|
|
|
*/
|
|
|
|
|
|
|
|
lun = &priv->luntab[priv->cbwlun];
|
|
|
|
priv->lun = lun;
|
|
|
|
}
|
|
|
|
|
2008-10-28 01:41:50 +01:00
|
|
|
/* Only a few commands may specify unsupported LUNs */
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
else if ((flags & USBMSC_FLAGS_LUNNOTNEEDED) == 0)
|
2008-10-28 01:41:50 +01:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_CMDBADLUN), priv->cbwlun);
|
2008-10-28 01:41:50 +01:00
|
|
|
ret = -EINVAL;
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract the direction and data transfer length */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
dir = flags & USBMSC_FLAGS_DIRMASK; /* Expected data direction */
|
2008-10-25 00:42:28 +02:00
|
|
|
datlen = 0;
|
2012-01-25 21:17:59 +01:00
|
|
|
if ((flags & USBMSC_FLAGS_BLOCKXFR) == 0)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
|
|
|
/* Non-block transfer. Data length: Host allocation to receive data
|
|
|
|
* (only for device-to-host transfers. At present, alloclen is set
|
|
|
|
* to zero for all host-to-device, non-block transfers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
datlen = priv->u.alloclen;
|
|
|
|
}
|
|
|
|
else if (lun)
|
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Block transfer: Calculate the total size of all sectors to be
|
|
|
|
* transferred
|
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
datlen = priv->u.alloclen * lun->sectorsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the data direction. This was set up at the end of the
|
|
|
|
* IDLE state when the CBW was parsed, but if there is no data,
|
|
|
|
* then the direction is none.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (datlen == 0)
|
|
|
|
{
|
|
|
|
/* No data.. then direction is none */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
dir = USBMSC_FLAGS_DIRNONE;
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Compare the expected data length in the command to the data length
|
|
|
|
* in the CBW.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (priv->cbwlen < datlen)
|
|
|
|
{
|
|
|
|
/* Clip to the length in the CBW and declare a phase error */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_PHASEERROR1), priv->cdb[0]);
|
2020-01-10 16:06:36 +01:00
|
|
|
if ((flags & USBMSC_FLAGS_BLOCKXFR) == 0)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
|
|
|
priv->u.alloclen = priv->cbwlen;
|
|
|
|
}
|
2015-03-12 14:55:53 +01:00
|
|
|
else if (lun)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
|
|
|
priv->u.xfrlen = priv->cbwlen / lun->sectorsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->phaseerror = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the residue */
|
|
|
|
|
|
|
|
priv->residue = priv->cbwlen;
|
|
|
|
|
|
|
|
/* Conflicting data directions is a phase error */
|
|
|
|
|
|
|
|
if (priv->cbwdir != dir && datlen > 0)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_PHASEERROR2), priv->cdb[0]);
|
2008-10-25 00:42:28 +02:00
|
|
|
priv->phaseerror = 1;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compare the length of data in the cdb[] with the expected length
|
2014-03-25 21:19:27 +01:00
|
|
|
* of the command. These sizes should match exactly.
|
2008-10-25 00:42:28 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (cdblen != priv->cdblen)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_PHASEERROR3), priv->cdb[0]);
|
2008-10-25 00:42:28 +02:00
|
|
|
priv->phaseerror = 1;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
|
2014-03-25 21:19:27 +01:00
|
|
|
/* Was a valid LUN provided? */
|
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
if (lun)
|
|
|
|
{
|
|
|
|
/* Retain the sense data for the REQUEST SENSE command */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
if ((flags & USBMSC_FLAGS_RETAINSENSEDATA) == 0)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
|
|
|
/* Discard the sense data */
|
|
|
|
|
|
|
|
lun->sd = SCSI_KCQ_NOSENSE;
|
|
|
|
lun->sdinfo = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If a unit attention condition exists, then only a restricted set of
|
|
|
|
* commands is permitted.
|
|
|
|
*/
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
if (lun->uad != SCSI_KCQ_NOSENSE &&
|
|
|
|
(flags & USBMSC_FLAGS_UACOKAY) != 0)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
|
|
|
/* Command not permitted */
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_CMDUNEVIOLATION),
|
|
|
|
priv->cbwlun);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = lun->uad;
|
|
|
|
lun->uad = SCSI_KCQ_NOSENSE;
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The final, 1-byte field of every SCSI command is the Control field which
|
|
|
|
* must be zero
|
|
|
|
*/
|
|
|
|
|
2020-01-10 16:06:36 +01:00
|
|
|
if (priv->cdb[cdblen - 1] != 0)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_SCSICMDCONTROL), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
if (lun)
|
|
|
|
{
|
|
|
|
lun->sd = SCSI_KCQIR_INVALIDFIELDINCBA;
|
|
|
|
}
|
2014-03-25 21:19:27 +01:00
|
|
|
|
|
|
|
ret = -EINVAL;
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
2014-03-25 21:19:27 +01:00
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_idlestate
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2012-01-25 21:17:59 +01:00
|
|
|
* Called from the worker thread in the USBMSC_STATE_IDLE state. Checks
|
2008-10-25 00:42:28 +02:00
|
|
|
* for the receipt of a bulk CBW.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2020-04-01 18:13:52 +02:00
|
|
|
* If no new, valid CBW is available, this function returns a negated
|
|
|
|
* errno value. Otherwise, when a new CBW is successfully parsed, this
|
|
|
|
* function sets priv->thstate to USBMSC_STATE_CMDPARSE and returns OK.
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static int usbmsc_idlestate(FAR struct usbmsc_dev_s *priv)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
FAR struct usbmsc_req_s *privreq;
|
2008-10-25 00:42:28 +02:00
|
|
|
FAR struct usbdev_req_s *req;
|
2012-01-25 21:17:59 +01:00
|
|
|
FAR struct usbmsc_cbw_s *cbw;
|
2008-10-25 00:42:28 +02:00
|
|
|
irqstate_t flags;
|
|
|
|
int ret = -EINVAL;
|
|
|
|
|
|
|
|
/* Take a request from the rdreqlist */
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2012-01-25 21:17:59 +01:00
|
|
|
privreq = (FAR struct usbmsc_req_s *)sq_remfirst(&priv->rdreqlist);
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Has anything been received? If not, just return an error.
|
|
|
|
* This will cause us to remain in the IDLE state. When a USB request is
|
2012-01-25 21:17:59 +01:00
|
|
|
* received, the worker thread will be awakened in the USBMSC_STATE_IDLE
|
2008-10-25 00:42:28 +02:00
|
|
|
* and we will be called again.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!privreq)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_IDLERDREQLISTEMPTY), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
req = privreq->req;
|
2012-01-25 21:17:59 +01:00
|
|
|
cbw = (FAR struct usbmsc_cbw_s *)req->buf;
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Handle the CBW */
|
|
|
|
|
2020-01-10 16:06:36 +01:00
|
|
|
usbmsc_dumpdata("SCSCI CBW", (FAR uint8_t *)cbw, USBMSC_CBW_SIZEOF -
|
|
|
|
USBMSC_MAXCDBLEN);
|
2012-01-25 21:17:59 +01:00
|
|
|
usbmsc_dumpdata(" CDB", cbw->cdb, MIN(cbw->cdblen, USBMSC_MAXCDBLEN));
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Check for properly formatted CBW? */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
if (req->xfrd != USBMSC_CBW_SIZEOF ||
|
2008-10-25 00:42:28 +02:00
|
|
|
cbw->signature[0] != 'U' ||
|
|
|
|
cbw->signature[1] != 'S' ||
|
|
|
|
cbw->signature[2] != 'B' ||
|
|
|
|
cbw->signature[3] != 'C')
|
|
|
|
{
|
2020-04-01 18:13:52 +02:00
|
|
|
/* CBS BAD: Stall the bulk endpoints. If the CBW is bad we must stall
|
|
|
|
* the bulk IN endpoint and either (1) stall the bulk OUT endpoint, or
|
2008-10-25 00:42:28 +02:00
|
|
|
* (2) discard data from the endpoint.
|
|
|
|
*/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_INVALIDCBWSIGNATURE), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
EP_STALL(priv->epbulkout);
|
|
|
|
EP_STALL(priv->epbulkin);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is the CBW meaningful? */
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
else if (cbw->lun >= priv->nluns ||
|
|
|
|
(cbw->flags & ~USBMSC_CBWFLAG_IN) != 0 ||
|
|
|
|
cbw->cdblen < 6 ||
|
|
|
|
cbw->cdblen > USBMSC_MAXCDBLEN)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2020-04-01 18:13:52 +02:00
|
|
|
/* CBS BAD: Stall the bulk endpoints. If the CBW is bad we must stall
|
|
|
|
* the bulk IN endpoint and either (1) stall the bulk OUT endpoint, or
|
2008-10-25 00:42:28 +02:00
|
|
|
* (2) discard data from the endpoint.
|
|
|
|
*/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_INVALIDCBWCONTENT), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
EP_STALL(priv->epbulkout);
|
|
|
|
EP_STALL(priv->epbulkin);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the information from the CBW */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Extract the CDB and copy the whole CBD[] for later use */
|
|
|
|
|
|
|
|
priv->cdblen = cbw->cdblen;
|
|
|
|
memcpy(priv->cdb, cbw->cdb, priv->cdblen);
|
|
|
|
|
|
|
|
/* Extract the data direction */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
if ((cbw->flags & USBMSC_CBWFLAG_IN) != 0)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
|
|
|
/* IN: Device-to-host */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->cbwdir = USBMSC_FLAGS_DIRDEVICE2HOST;
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* OUT: Host-to-device */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->cbwdir = USBMSC_FLAGS_DIRHOST2DEVICE;
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the max size of the data response */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->cbwlen = usbmsc_getle32(cbw->datlen);
|
2008-10-25 00:42:28 +02:00
|
|
|
if (priv->cbwlen == 0)
|
|
|
|
{
|
|
|
|
/* No length? Then no direction either */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->cbwdir = USBMSC_FLAGS_DIRNONE;
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract and save the LUN index and TAG value */
|
|
|
|
|
|
|
|
priv->cbwlun = cbw->lun;
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->cbwtag = usbmsc_getle32(cbw->tag);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Return the read request to the bulk out endpoint for re-filling */
|
|
|
|
|
|
|
|
req = privreq->req;
|
2020-01-10 16:06:36 +01:00
|
|
|
req->len = priv->epbulkout->maxpacket;
|
2009-06-17 01:23:31 +02:00
|
|
|
req->priv = privreq;
|
2012-01-25 21:17:59 +01:00
|
|
|
req->callback = usbmsc_rdcomplete;
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Change to the CMDPARSE state and return success */
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
usbtrace(TRACE_CLASSSTATE(USBMSC_CLASSSTATE_IDLECMDPARSE),
|
|
|
|
priv->cdb[0]);
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->thstate = USBMSC_STATE_CMDPARSE;
|
2008-10-25 00:42:28 +02:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
2012-03-19 18:56:15 +01:00
|
|
|
/* In any event, return the request to be refilled */
|
|
|
|
|
|
|
|
if (EP_SUBMIT(priv->epbulkout, req) != OK)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_IDLERDSUBMIT), (uint16_t)-ret);
|
|
|
|
}
|
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_cmdparsestate
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2012-01-25 21:17:59 +01:00
|
|
|
* Called from the worker thread in the USBMSC_STATE_CMDPARSE state.
|
|
|
|
* This state is entered when usbmsc_idlestate obtains a valid CBW
|
2008-10-25 00:42:28 +02:00
|
|
|
* containing SCSI commands. This function processes those SCSI commands.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2008-10-25 00:42:28 +02:00
|
|
|
* If no write request is available or certain other errors occur, this
|
2012-01-25 21:17:59 +01:00
|
|
|
* function returns a negated errno and stays in the USBMSC_STATE_CMDPARSE
|
2008-10-25 00:42:28 +02:00
|
|
|
* state. Otherwise, when the new CBW is successfully process, this
|
2012-01-25 21:17:59 +01:00
|
|
|
* function sets priv->thstate to one of USBMSC_STATE_CMDREAD,
|
|
|
|
* USBMSC_STATE_CMDWRITE, or USBMSC_STATE_CMDFINISH and returns OK.
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static int usbmsc_cmdparsestate(FAR struct usbmsc_dev_s *priv)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
FAR struct usbmsc_req_s *privreq;
|
2009-12-15 15:25:14 +01:00
|
|
|
FAR uint8_t *buf;
|
2020-04-01 18:13:52 +02:00
|
|
|
int ret;
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
usbmsc_dumpdata("SCSCI CDB", priv->cdb, priv->cdblen);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Check if there is a request in the wrreqlist that we will be able to
|
|
|
|
* use for data or status.
|
|
|
|
*/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
privreq = (FAR struct usbmsc_req_s *)sq_peek(&priv->wrreqlist);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* If there no request structures available, then just return an error.
|
|
|
|
* This will cause us to remain in the CMDPARSE state. When a request is
|
2020-04-01 18:13:52 +02:00
|
|
|
* returned, the worker thread will be awakened in the
|
|
|
|
* USBMSC_STATE_CMDPARSE and we will be called again.
|
2008-10-25 00:42:28 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (!privreq)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_CMDPARSEWRREQLISTEMPTY), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2012-03-16 21:59:21 +01:00
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
DEBUGASSERT(privreq->req && privreq->req->buf);
|
2009-06-12 02:29:07 +02:00
|
|
|
buf = privreq->req->buf;
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Assume that no errors will be encountered */
|
|
|
|
|
2009-06-12 02:29:07 +02:00
|
|
|
priv->phaseerror = 0;
|
|
|
|
priv->shortpacket = 0;
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* No data is buffered */
|
|
|
|
|
|
|
|
priv->nsectbytes = 0;
|
|
|
|
priv->nreqbytes = 0;
|
|
|
|
|
|
|
|
/* Get exclusive access to the block driver */
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
ret = usbmsc_scsi_lock(priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = -EINVAL;
|
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
switch (priv->cdb[0])
|
|
|
|
{
|
2020-04-01 18:13:52 +02:00
|
|
|
case SCSI_CMD_TESTUNITREADY: /* 0x00 Mandatory */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_cmdtestunitready(priv);
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* case SCSI_CMD_REZEROUNIT: 0x01 Obsolete
|
|
|
|
* 0x02 Vendor-specific
|
2020-01-10 16:06:36 +01:00
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
case SCSI_CMD_REQUESTSENSE: /* 0x03 Mandatory */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_cmdrequestsense(priv, buf);
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* case SCSI_CMD_FORMAT_UNIT: 0x04 Mandatory, but not impl.
|
|
|
|
* 0x05 Vendor specific
|
|
|
|
* 0x06 Vendor specific
|
|
|
|
* case SCSI_CMD_REASSIGNBLOCKS: 0x07 Optional
|
2020-01-10 16:06:36 +01:00
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
case SCSI_CMD_READ6: /* 0x08 Mandatory */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_cmdread6(priv);
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* 0x09 Vendor specific */
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
case SCSI_CMD_WRITE6: /* 0x0a Optional */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_cmdwrite6(priv);
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* case SCSI_CMD_SEEK6: 0x0b Obsolete
|
|
|
|
* 0x0c-0x10 Vendor specific
|
|
|
|
* case SCSI_CMD_SPACE6: 0x11 Vendor specific
|
2020-01-10 16:06:36 +01:00
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
case SCSI_CMD_INQUIRY: /* 0x12 Mandatory */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_cmdinquiry(priv, buf);
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* 0x13-0x14 Vendor specific */
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
case SCSI_CMD_MODESELECT6: /* 0x15 Optional */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_cmdmodeselect6(priv);
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* case SCSI_CMD_RESERVE6: 0x16 Obsolete
|
|
|
|
* case SCSI_CMD_RELEASE6: 0x17 Obsolete
|
|
|
|
* case SCSI_CMD_COPY: 0x18 Obsolete
|
|
|
|
* 0x19 Vendor specific
|
2020-01-10 16:06:36 +01:00
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
case SCSI_CMD_MODESENSE6: /* 0x1a Optional */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_cmdmodesense6(priv, buf);
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
case SCSI_CMD_STARTSTOPUNIT: /* 0x1b Optional */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_cmdstartstopunit(priv);
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* case SCSI_CMD_RECEIVEDIAGNOSTICRESULTS: 0x1c Optional
|
|
|
|
* case SCSI_CMD_SENDDIAGNOSTIC: 0x1d Mandatory, but not impl.
|
2020-01-10 16:06:36 +01:00
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
case SCSI_CMD_PREVENTMEDIAREMOVAL: /* 0x1e Optional */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_cmdpreventmediumremoval(priv);
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* 0x20-22 Vendor specific */
|
2020-01-10 16:06:36 +01:00
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
case SCSI_CMD_READFORMATCAPACITIES: /* 0x23 Vend-spec (def. MMC spec) */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_cmdreadformatcapacity(priv, buf);
|
2008-10-28 01:31:22 +01:00
|
|
|
break;
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* 0x24 Vendor specific */
|
2020-01-10 16:06:36 +01:00
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
case SCSI_CMD_READCAPACITY10: /* 0x25 Mandatory */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_cmdreadcapacity10(priv, buf);
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* 0x26-27 Vendor specific */
|
2020-01-10 16:06:36 +01:00
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
case SCSI_CMD_READ10: /* 0x28 Mandatory */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_cmdread10(priv);
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* 0x29 Vendor specific */
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
case SCSI_CMD_WRITE10: /* 0x2a Optional */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_cmdwrite10(priv);
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* case SCSI_CMD_SEEK10: 0x2b Obsolete
|
|
|
|
* 0x2c-2d Vendor specific
|
|
|
|
* case SCSI_CMD_WRITEANDVERIFY: 0x2e Optional
|
2020-01-10 16:06:36 +01:00
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
case SCSI_CMD_VERIFY10: /* 0x2f Opt, excpt Windows */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_cmdverify10(priv);
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* case SCSI_CMD_SEARCHDATAHIGH: 0x30 Obsolete
|
|
|
|
* case SCSI_CMD_SEARCHDATAEQUAL: 0x31 Obsolete
|
|
|
|
* case SCSI_CMD_SEARCHDATALOW: 0x32 Obsolete
|
|
|
|
* case SCSI_CMD_SETLIMITS10: 0x33 Obsolete
|
|
|
|
* case SCSI_CMD_PREFETCH10: 0x34 Optional
|
2020-01-10 16:06:36 +01:00
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
case SCSI_CMD_SYNCHCACHE10: /* 0x35 Optional */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_cmdsynchronizecache10(priv);
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* case SCSI_CMD_LOCKCACHE: 0x36 Obsolete
|
|
|
|
* case SCSI_CMD_READDEFECTDATA10: 0x37 Optional
|
|
|
|
* case SCSI_CMD_COMPARE: 0x39 Obsolete
|
|
|
|
* case SCSI_CMD_COPYANDVERIFY: 0x3a Obsolete
|
|
|
|
* case SCSI_CMD_WRITEBUFFER: 0x3b Optional
|
|
|
|
* case SCSI_CMD_READBUFFER: 0x3c Optional
|
|
|
|
* case SCSI_CMD_READLONG10: 0x3e Optional
|
|
|
|
* case SCSI_CMD_WRITELONG10: 0x3f Optional
|
|
|
|
* case SCSI_CMD_CHANGEDEFINITION: 0x40 Obsolete
|
|
|
|
* case SCSI_CMD_WRITESAME10: 0x41 Optional
|
|
|
|
* case SCSI_CMD_LOGSELECT: 0x4c Optional
|
|
|
|
* case SCSI_CMD_LOGSENSE: 0x4d Optional
|
|
|
|
* case SCSI_CMD_XDWRITE10: 0x50 Optional
|
|
|
|
* case SCSI_CMD_XPWRITE10: 0x51 Optional
|
|
|
|
* case SCSI_CMD_XDREAD10: 0x52 Optional
|
2020-01-10 16:06:36 +01:00
|
|
|
*/
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
case SCSI_CMD_MODESELECT10: /* 0x55 Optional */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_cmdmodeselect10(priv);
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* case SCSI_CMD_RESERVE10: 0x56 Obsolete
|
|
|
|
* case SCSI_CMD_RELEASE10: 0x57 Obsolete
|
2020-01-10 16:06:36 +01:00
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
case SCSI_CMD_MODESENSE10: /* 0x5a Optional */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_cmdmodesense10(priv, buf);
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* case SCSI_CMD_PERSISTENTRESERVEIN: 0x5e Optional
|
|
|
|
* case SCSI_CMD_PERSISTENTRESERVEOUT: 0x5f Optional
|
|
|
|
* case SCSI_CMD_32: 0x7f Optional
|
|
|
|
* case SCSI_CMD_XDWRITEEXTENDED: 0x80 Obsolete
|
|
|
|
* case SCSI_CMD_REBUILD: 0x81 Obsolete
|
|
|
|
* case SCSI_CMD_REGENERATE: 0x82 Obsolete
|
|
|
|
* case SCSI_CMD_EXTENDEDCOPY: 0x83 Optional
|
|
|
|
* case SCSI_CMD_COPYRESULTS: 0x84 Optional
|
|
|
|
* case SCSI_CMD_ACCESSCONTROLIN: 0x86 Optional
|
|
|
|
* case SCSI_CMD_ACCESSCONTROLOUT: 0x87 Optional
|
|
|
|
* case SCSI_CMD_READ16: 0x88 Optional
|
|
|
|
* case SCSI_CMD_WRITE16: 0x8a Optional
|
|
|
|
* case SCSI_CMD_READATTRIBUTE: 0x8c Optional
|
|
|
|
* case SCSI_CMD_WRITEATTRIBUTE: 0x8d Optional
|
|
|
|
* case SCSI_CMD_WRITEANDVERIFY16: 0x8e Optional
|
|
|
|
* case SCSI_CMD_SYNCHCACHE16: 0x91 Optional
|
|
|
|
* case SCSI_CMD_LOCKUNLOCKACACHE: 0x92 Optional
|
|
|
|
* case SCSI_CMD_WRITESAME16: 0x93 Optional
|
|
|
|
* case SCSI_CMD_READCAPACITY16: 0x9e Optional
|
|
|
|
* case SCSI_CMD_READLONG16: 0x9e Optional
|
|
|
|
* case SCSI_CMD_WRITELONG16 0x9f Optional
|
|
|
|
* case SCSI_CMD_REPORTLUNS: 0xa0 Mandatory, but no-impl
|
|
|
|
* case SCSI_CMD_MAINTENANCEIN: 0xa3 Optional (SCCS==0)
|
|
|
|
* case SCSI_CMD_MAINTENANCEOUT: 0xa4 Optional (SCCS==0)
|
|
|
|
* case SCSI_CMD_MOVEMEDIUM: 0xa5 ?
|
|
|
|
* case SCSI_CMD_MOVEMEDIUMATTACHED: 0xa7 Optional (MCHNGR==0)
|
2020-01-10 16:06:36 +01:00
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
case SCSI_CMD_READ12: /* 0xa8 Optional */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_cmdread12(priv);
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
case SCSI_CMD_WRITE12: /* 0xaa Optional */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_cmdwrite12(priv);
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* case SCSI_CMD_READMEDIASERIALNUMBER: 0xab Optional
|
|
|
|
* case SCSI_CMD_WRITEANDVERIFY12: 0xae Optional
|
|
|
|
* case SCSI_CMD_VERIFY12: 0xaf Optional
|
|
|
|
* case SCSI_CMD_SETLIMITS12 0xb3 Obsolete
|
|
|
|
* case SCSI_CMD_READELEMENTSTATUS: 0xb4 Optional (MCHNGR==0)
|
|
|
|
* case SCSI_CMD_READDEFECTDATA12: 0xb7 Optional
|
|
|
|
* case SCSI_CMD_REDUNDANCYGROUPIN: 0xba Optional
|
|
|
|
* case SCSI_CMD_REDUNDANCYGROUPOUT: 0xbb Optional
|
|
|
|
* case SCSI_CMD_SPAREIN: 0xbc Optional (SCCS==0)
|
|
|
|
* case SCSI_CMD_SPAREOUT: 0xbd Optional (SCCS==0)
|
|
|
|
* case SCSI_CMD_VOLUMESETIN: 0xbe Optional (SCCS==0)
|
|
|
|
* case SCSI_CMD_VOLUMESETOUT: 0xbe Optional (SCCS==0)
|
|
|
|
* 0xc0-0xff Vendor specific
|
2020-01-10 16:06:36 +01:00
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
priv->u.alloclen = 0;
|
2020-01-10 16:06:36 +01:00
|
|
|
priv->residue = priv->cbwlen;
|
|
|
|
priv->lun->sd = SCSI_KCQIR_INVALIDCOMMAND;
|
|
|
|
ret = -EINVAL;
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-03-25 21:19:27 +01:00
|
|
|
|
|
|
|
usbmsc_scsi_unlock(priv);
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2010-04-04 18:21:13 +02:00
|
|
|
/* Is a response required? (Not for read6/10/12 and write6/10/12). */
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
if (priv->thstate == USBMSC_STATE_CMDPARSE)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2020-04-01 18:13:52 +02:00
|
|
|
/* All commands come through this path (EXCEPT read6/10/12 and
|
|
|
|
* write6/10/12). For all other commands, the following setup is
|
|
|
|
* expected for the response based on data direction:
|
2010-04-04 18:21:13 +02:00
|
|
|
*
|
|
|
|
* For direction NONE:
|
|
|
|
* 1. priv->u.alloclen == 0
|
|
|
|
* 2. priv->nreqbytes == 0
|
|
|
|
*
|
|
|
|
* For direction = device-to-host:
|
|
|
|
* 1. priv->u.alloclen == allocation length; space set aside by the
|
|
|
|
* host to receive the device data. The size of the response
|
|
|
|
* cannot exceed this value.
|
|
|
|
* 2. response data is in the request currently at the head of
|
2020-04-01 18:13:52 +02:00
|
|
|
* the priv->wrreqlist queue. priv->nreqbytes is set to the
|
|
|
|
* length of data in the response.
|
2010-04-04 18:21:13 +02:00
|
|
|
*
|
|
|
|
* For direction host-to-device
|
2020-04-01 18:13:52 +02:00
|
|
|
* At present, there are no supported commands that should have
|
|
|
|
* host-to-device transfers (except write6/10/12 and that command
|
|
|
|
* logic does not take this path. The 'residue' is left at the full
|
|
|
|
* host-to-device data size.
|
2010-04-04 18:21:13 +02:00
|
|
|
*
|
|
|
|
* For all:
|
|
|
|
* ret set to <0 if an error occurred in parsing the commands.
|
2008-10-25 00:42:28 +02:00
|
|
|
*/
|
|
|
|
|
2010-04-04 18:21:13 +02:00
|
|
|
/* For from device-to-hose operations, the residue is the expected size
|
|
|
|
* (the initial value of 'residue') minus the amount actually returned
|
|
|
|
* in the response:
|
|
|
|
*/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
if (priv->cbwdir == USBMSC_FLAGS_DIRDEVICE2HOST)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2010-04-04 18:21:13 +02:00
|
|
|
/* The number of bytes in the response cannot exceed the host
|
|
|
|
* 'allocation length' in the command.
|
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2010-04-04 18:21:13 +02:00
|
|
|
if (priv->nreqbytes > priv->u.alloclen)
|
|
|
|
{
|
|
|
|
priv->nreqbytes = priv->u.alloclen;
|
|
|
|
}
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2010-04-04 18:21:13 +02:00
|
|
|
/* The residue is then the number of bytes that were not sent */
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2010-04-04 18:21:13 +02:00
|
|
|
priv->residue -= priv->nreqbytes;
|
|
|
|
}
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2010-04-04 18:21:13 +02:00
|
|
|
/* On return, we need the following:
|
|
|
|
*
|
|
|
|
* 1. Setup for CMDFINISH state if appropriate
|
2020-04-01 18:13:52 +02:00
|
|
|
* 2. priv->thstate set to either CMDPARSE if no buffer was
|
|
|
|
* available or CMDFINISH to send the response
|
2010-04-04 18:21:13 +02:00
|
|
|
* 3. Return OK to continue; <0 to wait for the next event
|
|
|
|
*/
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
usbtrace(TRACE_CLASSSTATE(USBMSC_CLASSSTATE_CMDPARSECMDFINISH),
|
|
|
|
priv->cdb[0]);
|
2014-03-20 21:25:56 +01:00
|
|
|
priv->thstate = USBMSC_STATE_CMDFINISH;
|
2010-04-04 18:21:13 +02:00
|
|
|
ret = OK;
|
|
|
|
}
|
2014-03-20 21:25:56 +01:00
|
|
|
|
2010-04-04 18:21:13 +02:00
|
|
|
return ret;
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_cmdreadstate
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2012-01-25 21:17:59 +01:00
|
|
|
* Called from the worker thread in the USBMSC_STATE_CMDREAD state.
|
|
|
|
* The USBMSC_STATE_CMDREAD state is entered when usbmsc_cmdparsestate
|
2008-10-25 00:42:28 +02:00
|
|
|
* obtains a valid SCSI read command. This state is really a continuation
|
2012-01-25 21:17:59 +01:00
|
|
|
* of the USBMSC_STATE_CMDPARSE state that handles extended SCSI read
|
2008-10-25 00:42:28 +02:00
|
|
|
* command handling.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2020-04-01 18:13:52 +02:00
|
|
|
* If no USBDEV write request is available or certain other errors occur,
|
|
|
|
* this function returns a negated errno and stays in the
|
|
|
|
* USBMSC_STATE_CMDREAD state. Otherwise, when the new SCSI read command
|
|
|
|
* is fully processed, this function sets priv->thstate to
|
|
|
|
* USBMSC_STATE_CMDFINISH and returns OK.
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* State variables:
|
|
|
|
* xfrlen - holds the number of sectors read to be read.
|
|
|
|
* sector - holds the sector number of the next sector to be read
|
|
|
|
* nsectbytes - holds the number of bytes buffered for the current sector
|
|
|
|
* nreqbytes - holds the number of bytes currently buffered in the request
|
|
|
|
* at the head of the wrreqlist.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static int usbmsc_cmdreadstate(FAR struct usbmsc_dev_s *priv)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
FAR struct usbmsc_lun_s *lun = priv->lun;
|
|
|
|
FAR struct usbmsc_req_s *privreq;
|
2008-10-25 00:42:28 +02:00
|
|
|
FAR struct usbdev_req_s *req;
|
|
|
|
irqstate_t flags;
|
|
|
|
ssize_t nread;
|
2009-12-15 15:25:14 +01:00
|
|
|
uint8_t *src;
|
|
|
|
uint8_t *dest;
|
2008-10-25 00:42:28 +02:00
|
|
|
int nbytes;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Loop transferring data until either (1) all of the data has been
|
2020-04-01 18:13:52 +02:00
|
|
|
* transferred, or (2) we have used up all of the write requests that we
|
|
|
|
* have available.
|
2008-10-25 00:42:28 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
while (priv->u.xfrlen > 0 || priv->nsectbytes > 0)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLASSSTATE(USBMSC_CLASSSTATE_CMDREAD), priv->u.xfrlen);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Is the I/O buffer empty? */
|
|
|
|
|
|
|
|
if (priv->nsectbytes <= 0)
|
|
|
|
{
|
|
|
|
/* Yes.. read the next sector */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
nread = USBMSC_DRVR_READ(lun, priv->iobuffer, priv->sector, 1);
|
2008-10-25 00:42:28 +02:00
|
|
|
if (nread < 0)
|
|
|
|
{
|
2020-04-01 18:13:52 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_CMDREADREADFAIL),
|
|
|
|
-nread);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQME_UNRRE1;
|
|
|
|
lun->sdinfo = priv->sector;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->nsectbytes = lun->sectorsize;
|
|
|
|
priv->u.xfrlen--;
|
|
|
|
priv->sector++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if there is a request in the wrreqlist that we will be able to
|
|
|
|
* use for data transfer.
|
|
|
|
*/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
privreq = (FAR struct usbmsc_req_s *)sq_peek(&priv->wrreqlist);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* If there no request structures available, then just return an
|
|
|
|
* error. This will cause us to remain in the CMDREAD state. When a
|
|
|
|
* request is returned, the worker thread will be awakened in the
|
|
|
|
* USBMSC_STATE_CMDREAD and we will be called again.
|
2008-10-25 00:42:28 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (!privreq)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_CMDREADWRRQEMPTY), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
priv->nreqbytes = 0;
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2014-03-20 21:25:56 +01:00
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
req = privreq->req;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* Transfer all of the data that will (1) fit into the request buffer,
|
|
|
|
* OR (2) all of the data available in the sector buffer.
|
2008-10-25 00:42:28 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
src = &priv->iobuffer[lun->sectorsize - priv->nsectbytes];
|
2009-06-12 02:29:07 +02:00
|
|
|
dest = &req->buf[priv->nreqbytes];
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
nbytes = MIN(priv->epbulkin->maxpacket - priv->nreqbytes,
|
|
|
|
priv->nsectbytes);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Copy the data from the sector buffer to the USB request and update
|
|
|
|
* counts
|
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
memcpy(dest, src, nbytes);
|
|
|
|
priv->nreqbytes += nbytes;
|
|
|
|
priv->nsectbytes -= nbytes;
|
|
|
|
|
2020-01-10 16:06:36 +01:00
|
|
|
/* If (1) the request buffer is full OR (2) this is the final request
|
|
|
|
* full of data,
|
2008-10-25 00:42:28 +02:00
|
|
|
* then submit the request
|
|
|
|
*/
|
|
|
|
|
2020-01-10 16:06:36 +01:00
|
|
|
if (priv->nreqbytes >= priv->epbulkin->maxpacket ||
|
2008-10-25 00:42:28 +02:00
|
|
|
(priv->u.xfrlen <= 0 && priv->nsectbytes <= 0))
|
|
|
|
{
|
2020-01-10 16:06:36 +01:00
|
|
|
/* Remove the request that we just filled from wrreqlist (we've
|
|
|
|
* already checked that is it not NULL
|
2008-10-25 00:42:28 +02:00
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2012-01-25 21:17:59 +01:00
|
|
|
privreq = (FAR struct usbmsc_req_s *)sq_remfirst(&priv->wrreqlist);
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* And submit the request to the bulk IN endpoint */
|
|
|
|
|
|
|
|
req->len = priv->nreqbytes;
|
2009-06-17 01:23:31 +02:00
|
|
|
req->priv = privreq;
|
2012-01-25 21:17:59 +01:00
|
|
|
req->callback = usbmsc_wrcomplete;
|
2008-10-25 16:40:37 +02:00
|
|
|
req->flags = 0;
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2008-10-25 16:40:37 +02:00
|
|
|
ret = EP_SUBMIT(priv->epbulkin, req);
|
2008-10-25 00:42:28 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2020-04-01 18:13:52 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_CMDREADSUBMIT),
|
|
|
|
(uint16_t)-ret);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQME_UNRRE1;
|
|
|
|
lun->sdinfo = priv->sector;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* Assume success... residue should probably really be decremented
|
|
|
|
* in wrcomplete when we know that the transfer completed
|
|
|
|
* successfully.
|
2008-10-25 00:42:28 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
priv->residue -= priv->nreqbytes;
|
|
|
|
priv->nreqbytes = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
usbtrace(TRACE_CLASSSTATE(USBMSC_CLASSSTATE_CMDREADCMDFINISH),
|
|
|
|
priv->u.xfrlen);
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->thstate = USBMSC_STATE_CMDFINISH;
|
2008-10-25 00:42:28 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_cmdwritestate
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2012-01-25 21:17:59 +01:00
|
|
|
* Called from the worker thread in the USBMSC_STATE_CMDWRITE state.
|
|
|
|
* The USBMSC_STATE_CMDWRITE state is entered when usbmsc_cmdparsestate
|
2008-10-25 00:42:28 +02:00
|
|
|
* obtains a valid SCSI write command. This state is really a continuation
|
2012-01-25 21:17:59 +01:00
|
|
|
* of the USBMSC_STATE_CMDPARSE state that handles extended SCSI write
|
2008-10-25 00:42:28 +02:00
|
|
|
* command handling.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2020-04-01 18:13:52 +02:00
|
|
|
* If no USBDEV write request is available or certain other errors occur,
|
|
|
|
* this function returns a negated errno and stays in the
|
|
|
|
* USBMSC_STATE_CMDWRITE state. Otherwise, when the new SCSI write
|
|
|
|
* command is fully processed, this function sets priv->thstate to
|
|
|
|
* USBMSC_STATE_CMDFINISH and returns OK.
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* State variables:
|
|
|
|
* xfrlen - holds the number of sectors read to be written.
|
|
|
|
* sector - holds the sector number of the next sector to write
|
|
|
|
* nsectbytes - holds the number of bytes buffered for the current sector
|
|
|
|
* nreqbytes - holds the number of untransferred bytes currently in the
|
|
|
|
* request at the head of the rdreqlist.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static int usbmsc_cmdwritestate(FAR struct usbmsc_dev_s *priv)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
FAR struct usbmsc_lun_s *lun = priv->lun;
|
|
|
|
FAR struct usbmsc_req_s *privreq;
|
2008-10-25 00:42:28 +02:00
|
|
|
FAR struct usbdev_req_s *req;
|
|
|
|
ssize_t nwritten;
|
2009-12-15 15:25:14 +01:00
|
|
|
uint16_t xfrd;
|
|
|
|
uint8_t *src;
|
|
|
|
uint8_t *dest;
|
2008-10-25 00:42:28 +02:00
|
|
|
int nbytes;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Loop transferring data until either (1) all of the data has been
|
|
|
|
* transferred, or (2) we have written all of the data in the available
|
|
|
|
* read requests.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (priv->u.xfrlen > 0)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLASSSTATE(USBMSC_CLASSSTATE_CMDWRITE), priv->u.xfrlen);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Check if there is a request in the rdreqlist containing additional
|
|
|
|
* data to be written.
|
|
|
|
*/
|
|
|
|
|
2016-07-24 05:10:41 +02:00
|
|
|
irqstate_t flags = enter_critical_section();
|
2012-01-25 21:17:59 +01:00
|
|
|
privreq = (FAR struct usbmsc_req_s *)sq_remfirst(&priv->rdreqlist);
|
2016-07-24 05:10:41 +02:00
|
|
|
leave_critical_section(flags);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* If there no request data available, then just return an error.
|
2020-04-01 18:13:52 +02:00
|
|
|
* This will cause us to remain in the CMDWRITE state. When a filled
|
|
|
|
* request is received, the worker thread will be awakened in the
|
|
|
|
* USBMSC_STATE_CMDWRITE and we will be called again.
|
2008-10-25 00:42:28 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (!privreq)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_CMDWRITERDRQEMPTY), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
priv->nreqbytes = 0;
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
req = privreq->req;
|
|
|
|
xfrd = req->xfrd;
|
|
|
|
priv->nreqbytes = xfrd;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* Now loop until all of the data in the read request has been
|
|
|
|
* transferred to the block driver OR all of the request data has been
|
|
|
|
* transferred.
|
2008-10-25 00:42:28 +02:00
|
|
|
*/
|
|
|
|
|
2012-03-18 21:39:18 +01:00
|
|
|
while (priv->nreqbytes > 0 && priv->u.xfrlen > 0)
|
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Copy the data received in the read request into the sector I/O
|
|
|
|
* buffer
|
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2012-03-18 21:39:18 +01:00
|
|
|
src = &req->buf[xfrd - priv->nreqbytes];
|
|
|
|
dest = &priv->iobuffer[priv->nsectbytes];
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2022-07-12 07:44:55 +02:00
|
|
|
#ifdef CONFIG_USBMSC_WRMULTIPLE
|
|
|
|
/* nbytes may end up being zero, after which the loop no longer
|
|
|
|
* proceeds but will be stuck forever. Make sure nbytes isn't
|
|
|
|
* zero.
|
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2022-07-12 07:44:55 +02:00
|
|
|
if (lun->sectorsize > priv->nsectbytes)
|
|
|
|
{
|
|
|
|
nbytes = MIN(lun->sectorsize - priv->nsectbytes,
|
|
|
|
priv->nreqbytes);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nbytes = priv->nreqbytes;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
nbytes = MIN(lun->sectorsize - priv->nsectbytes, priv->nreqbytes);
|
|
|
|
#endif
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Copy the data from the sector buffer to the USB request and
|
|
|
|
* update counts
|
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2012-03-18 21:39:18 +01:00
|
|
|
memcpy(dest, src, nbytes);
|
|
|
|
priv->nsectbytes += nbytes;
|
|
|
|
priv->nreqbytes -= nbytes;
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2022-07-12 07:44:55 +02:00
|
|
|
#ifdef CONFIG_USBMSC_WRMULTIPLE
|
|
|
|
uint32_t nrbufs = MIN(priv->u.xfrlen, CONFIG_USBMSC_NWRREQS);
|
|
|
|
|
2012-03-18 21:39:18 +01:00
|
|
|
/* Is the I/O buffer full? */
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2022-07-12 07:44:55 +02:00
|
|
|
if ((priv->nsectbytes >= lun->sectorsize * priv->u.xfrlen) ||
|
|
|
|
(priv->nsectbytes >= lun->sectorsize * CONFIG_USBMSC_NWRREQS))
|
|
|
|
{
|
|
|
|
/* Yes.. Write next sectors */
|
|
|
|
|
|
|
|
nwritten = USBMSC_DRVR_WRITE(lun, priv->iobuffer,
|
|
|
|
priv->sector, nrbufs);
|
|
|
|
if (nwritten < 0)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_CMDWRITEWRITEFAIL),
|
|
|
|
-nwritten);
|
|
|
|
lun->sd = SCSI_KCQME_WRITEFAULTAUTOREALLOCFAILED;
|
|
|
|
lun->sdinfo = priv->sector;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->nsectbytes = 0;
|
|
|
|
priv->residue -= lun->sectorsize * nrbufs;
|
|
|
|
priv->u.xfrlen -= nrbufs;
|
|
|
|
priv->sector += nrbufs;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if ((priv->nsectbytes >= lun->sectorsize))
|
2012-03-18 21:39:18 +01:00
|
|
|
{
|
2008-10-25 00:42:28 +02:00
|
|
|
/* Yes.. Write the next sector */
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
nwritten = USBMSC_DRVR_WRITE(lun, priv->iobuffer,
|
|
|
|
priv->sector, 1);
|
2008-10-25 00:42:28 +02:00
|
|
|
if (nwritten < 0)
|
|
|
|
{
|
2020-01-10 16:06:36 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_CMDWRITEWRITEFAIL),
|
|
|
|
-nwritten);
|
2008-10-25 00:42:28 +02:00
|
|
|
lun->sd = SCSI_KCQME_WRITEFAULTAUTOREALLOCFAILED;
|
|
|
|
lun->sdinfo = priv->sector;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2012-03-18 21:39:18 +01:00
|
|
|
priv->nsectbytes = 0;
|
|
|
|
priv->residue -= lun->sectorsize;
|
|
|
|
priv->u.xfrlen--;
|
|
|
|
priv->sector++;
|
|
|
|
}
|
2022-07-12 07:44:55 +02:00
|
|
|
#endif
|
2012-03-18 21:39:18 +01:00
|
|
|
}
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* In either case, we are finished with this read request and can
|
|
|
|
* return it to the endpoint. Then we will go back to the top of the
|
|
|
|
* top and attempt to get the next read request.
|
2012-03-18 21:39:18 +01:00
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2020-01-10 16:06:36 +01:00
|
|
|
req->len = priv->epbulkout->maxpacket;
|
2009-06-17 01:23:31 +02:00
|
|
|
req->priv = privreq;
|
2012-01-25 21:17:59 +01:00
|
|
|
req->callback = usbmsc_rdcomplete;
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
ret = EP_SUBMIT(priv->epbulkout, req);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2020-04-01 18:13:52 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_CMDWRITERDSUBMIT),
|
|
|
|
(uint16_t)-ret);
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Did the host decide to stop early? */
|
|
|
|
|
2020-01-10 16:06:36 +01:00
|
|
|
if (xfrd != priv->epbulkout->maxpacket)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
|
|
|
priv->shortpacket = 1;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
2020-04-01 18:13:52 +02:00
|
|
|
usbtrace(TRACE_CLASSSTATE(USBMSC_CLASSSTATE_CMDWRITECMDFINISH),
|
|
|
|
priv->u.xfrlen);
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->thstate = USBMSC_STATE_CMDFINISH;
|
2008-10-25 00:42:28 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_cmdfinishstate
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2012-01-25 21:17:59 +01:00
|
|
|
* Called from the worker thread in the USBMSC_STATE_CMDFINISH state.
|
|
|
|
* The USBMSC_STATE_CMDFINISH state is entered when processing of a
|
2008-10-25 00:42:28 +02:00
|
|
|
* command has finished but before status has been returned.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2020-04-01 18:13:52 +02:00
|
|
|
* If no USBDEV write request is available or certain other errors occur,
|
|
|
|
* this function returns a negated errno and stays in the
|
|
|
|
* USBMSC_STATE_CMDFINISH state. Otherwise, when the command is fully
|
|
|
|
* processed, this function sets priv->thstate to USBMSC_STATE_CMDSTATUS
|
|
|
|
* and returns OK.
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static int usbmsc_cmdfinishstate(FAR struct usbmsc_dev_s *priv)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
FAR struct usbmsc_req_s *privreq;
|
2008-10-25 00:42:28 +02:00
|
|
|
irqstate_t flags;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Check if there is a request in the wrreqlist that we will be able to
|
|
|
|
* use for data transfer.
|
|
|
|
*/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
privreq = (FAR struct usbmsc_req_s *)sq_peek(&priv->wrreqlist);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* If there no request structures available, then just return an error.
|
|
|
|
* This will cause us to remain in the CMDREAD state. When a request is
|
2012-01-25 21:17:59 +01:00
|
|
|
* returned, the worker thread will be awakened in the USBMSC_STATE_CMDREAD
|
2008-10-25 00:42:28 +02:00
|
|
|
* and we will be called again.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!privreq)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_CMDFINISHRQEMPTY), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finish the final stages of the reply */
|
|
|
|
|
|
|
|
switch (priv->cbwdir)
|
|
|
|
{
|
|
|
|
/* Device-to-host: All but the last data buffer has been sent */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
case USBMSC_FLAGS_DIRDEVICE2HOST:
|
2008-10-25 00:42:28 +02:00
|
|
|
if (priv->cbwlen > 0)
|
|
|
|
{
|
2008-10-28 18:20:09 +01:00
|
|
|
/* On most commands (the exception is outgoing, write commands),
|
2014-03-20 21:25:56 +01:00
|
|
|
* the data has not yet been sent.
|
2008-10-25 00:42:28 +02:00
|
|
|
*/
|
|
|
|
|
2008-10-28 18:20:09 +01:00
|
|
|
if (priv->nreqbytes > 0)
|
|
|
|
{
|
|
|
|
struct usbdev_req_s *req;
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2008-10-28 18:20:09 +01:00
|
|
|
/* Take a request from the wrreqlist (we've already checked
|
|
|
|
* that is it not NULL)
|
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2020-04-01 18:13:52 +02:00
|
|
|
privreq = (FAR struct usbmsc_req_s *)
|
|
|
|
sq_remfirst(&priv->wrreqlist);
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2008-10-25 16:40:37 +02:00
|
|
|
|
2008-10-28 18:20:09 +01:00
|
|
|
/* Send the write request */
|
|
|
|
|
|
|
|
req = privreq->req;
|
|
|
|
req->len = priv->nreqbytes;
|
2012-01-25 21:17:59 +01:00
|
|
|
req->callback = usbmsc_wrcomplete;
|
2009-06-17 01:23:31 +02:00
|
|
|
req->priv = privreq;
|
2008-10-28 18:20:09 +01:00
|
|
|
req->flags = USBDEV_REQFLAGS_NULLPKT;
|
|
|
|
|
|
|
|
ret = EP_SUBMIT(priv->epbulkin, privreq->req);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-10 16:06:36 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_CMDFINISHSUBMIT),
|
|
|
|
(uint16_t)-ret);
|
2008-10-28 18:20:09 +01:00
|
|
|
}
|
|
|
|
}
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Stall the BULK In endpoint if there is a residue */
|
|
|
|
|
|
|
|
if (priv->residue > 0)
|
|
|
|
{
|
2021-06-13 16:58:22 +02:00
|
|
|
#ifndef CONFIG_USBMSC_NOT_STALL_BULKEP
|
2020-01-10 16:06:36 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_CMDFINISHRESIDUE),
|
|
|
|
(uint16_t)priv->residue);
|
2010-04-04 18:21:13 +02:00
|
|
|
|
2014-03-20 22:20:50 +01:00
|
|
|
#ifdef USBMSC_STALL_RACEWAR
|
2010-04-04 18:21:13 +02:00
|
|
|
/* (See description of the workaround at the top of the file).
|
2020-04-01 18:13:52 +02:00
|
|
|
* First, wait for the transfer to complete, then stall the
|
|
|
|
* endpoint
|
2010-04-04 18:21:13 +02:00
|
|
|
*/
|
|
|
|
|
2019-11-30 00:37:39 +01:00
|
|
|
nxsig_usleep (100000);
|
2020-01-02 17:49:34 +01:00
|
|
|
EP_STALL(priv->epbulkin);
|
2010-04-04 18:21:13 +02:00
|
|
|
|
2012-03-16 21:59:21 +01:00
|
|
|
/* now wait for stall to go away .... */
|
2010-04-04 18:21:13 +02:00
|
|
|
|
2019-11-30 00:37:39 +01:00
|
|
|
nxsig_usleep (100000);
|
2010-04-04 18:21:13 +02:00
|
|
|
#else
|
2020-01-02 17:49:34 +01:00
|
|
|
EP_STALL(priv->epbulkin);
|
2021-06-13 16:58:22 +02:00
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
priv->residue = 0;
|
2010-04-04 18:21:13 +02:00
|
|
|
#endif
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Host-to-device: We have processed all we want of the host data. */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
case USBMSC_FLAGS_DIRHOST2DEVICE:
|
2008-10-25 00:42:28 +02:00
|
|
|
if (priv->residue > 0)
|
|
|
|
{
|
|
|
|
/* Did the host stop sending unexpectedly early? */
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2008-10-25 00:42:28 +02:00
|
|
|
if (priv->shortpacket)
|
|
|
|
{
|
2020-01-10 16:06:36 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_CMDFINISHSHORTPKT),
|
|
|
|
(uint16_t)priv->residue);
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Unprocessed incoming data: STALL and cancel requests. */
|
|
|
|
|
2014-04-13 22:32:20 +02:00
|
|
|
else
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2020-01-10 16:06:36 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_CMDFINSHSUBMIT),
|
|
|
|
(uint16_t)priv->residue);
|
2008-10-25 00:42:28 +02:00
|
|
|
EP_STALL(priv->epbulkout);
|
2020-01-10 16:06:36 +01:00
|
|
|
}
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->theventset |= USBMSC_EVENT_ABORTBULKOUT;
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_CMDFINSHDIR), priv->cbwdir);
|
|
|
|
case USBMSC_FLAGS_DIRNONE: /* Nothing to send */
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return to the IDLE state */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLASSSTATE(USBMSC_CLASSSTATE_CMDFINISHCMDSTATUS), 0);
|
|
|
|
priv->thstate = USBMSC_STATE_CMDSTATUS;
|
2008-10-25 00:42:28 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: usbmsc_cmdstatusstate
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2012-01-25 21:17:59 +01:00
|
|
|
* Called from the worker thread in the USBMSC_STATE_CMDSTATUS state.
|
2008-10-25 00:42:28 +02:00
|
|
|
* That state is after a CBW has been fully processed. This function sends
|
|
|
|
* the concluding CSW.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2008-10-25 00:42:28 +02:00
|
|
|
* If no write request is available or certain other errors occur, this
|
2012-01-25 21:17:59 +01:00
|
|
|
* function returns a negated errno and stays in the USBMSC_STATE_CMDSTATUS
|
2008-10-25 00:42:28 +02:00
|
|
|
* state. Otherwise, when the SCSI statis is successfully returned, this
|
2012-01-25 21:17:59 +01:00
|
|
|
* function sets priv->thstate to USBMSC_STATE_IDLE and returns OK.
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
static int usbmsc_cmdstatusstate(FAR struct usbmsc_dev_s *priv)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
FAR struct usbmsc_lun_s *lun = priv->lun;
|
|
|
|
FAR struct usbmsc_req_s *privreq;
|
2008-10-25 00:42:28 +02:00
|
|
|
FAR struct usbdev_req_s *req;
|
2012-01-25 21:17:59 +01:00
|
|
|
FAR struct usbmsc_csw_s *csw;
|
2008-10-25 00:42:28 +02:00
|
|
|
irqstate_t flags;
|
2009-12-15 15:25:14 +01:00
|
|
|
uint32_t sd;
|
2012-01-25 21:17:59 +01:00
|
|
|
uint8_t status = USBMSC_CSWSTATUS_PASS;
|
2008-10-25 00:42:28 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Take a request from the wrreqlist */
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2012-01-25 21:17:59 +01:00
|
|
|
privreq = (FAR struct usbmsc_req_s *)sq_remfirst(&priv->wrreqlist);
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* If there no request structures available, then just return an error.
|
2020-04-01 18:13:52 +02:00
|
|
|
* This will cause us to remain in the CMDSTATUS status. When a request
|
|
|
|
* is returned, the worker thread will be awakened in the
|
|
|
|
* USBMSC_STATE_CMDSTATUS and we will be called again.
|
2008-10-25 00:42:28 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (!privreq)
|
|
|
|
{
|
2016-07-24 05:10:20 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_CMDSTATUSWRREQLISTEMPTY), 0);
|
2008-10-25 00:42:28 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
req = privreq->req;
|
2015-10-10 18:41:00 +02:00
|
|
|
csw = (FAR struct usbmsc_csw_s *)req->buf;
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Extract the sense data from the LUN structure */
|
|
|
|
|
|
|
|
if (lun)
|
|
|
|
{
|
|
|
|
sd = lun->sd;
|
|
|
|
}
|
2014-04-13 22:32:20 +02:00
|
|
|
else
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
|
|
|
sd = SCSI_KCQIR_INVALIDLUN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine the CSW status: PASS, PHASEERROR, or FAIL */
|
|
|
|
|
|
|
|
if (priv->phaseerror)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_SNDPHERROR), 0);
|
|
|
|
status = USBMSC_CSWSTATUS_PHASEERROR;
|
2008-10-25 00:42:28 +02:00
|
|
|
sd = SCSI_KCQIR_INVALIDCOMMAND;
|
|
|
|
}
|
|
|
|
else if (sd != SCSI_KCQ_NOSENSE)
|
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_SNDCSWFAIL), 0);
|
|
|
|
status = USBMSC_CSWSTATUS_FAIL;
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Format and submit the CSW */
|
|
|
|
|
|
|
|
csw->signature[0] = 'U';
|
|
|
|
csw->signature[1] = 'S';
|
|
|
|
csw->signature[2] = 'B';
|
|
|
|
csw->signature[3] = 'S';
|
2012-01-25 21:17:59 +01:00
|
|
|
usbmsc_putle32(csw->tag, priv->cbwtag);
|
|
|
|
usbmsc_putle32(csw->residue, priv->residue);
|
2008-10-25 00:42:28 +02:00
|
|
|
csw->status = status;
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
usbmsc_dumpdata("SCSCI CSW", (FAR uint8_t *)csw, USBMSC_CSW_SIZEOF);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
req->len = USBMSC_CSW_SIZEOF;
|
|
|
|
req->callback = usbmsc_wrcomplete;
|
2009-06-17 01:23:31 +02:00
|
|
|
req->priv = privreq;
|
2008-10-25 16:40:37 +02:00
|
|
|
req->flags = USBDEV_REQFLAGS_NULLPKT;
|
|
|
|
|
|
|
|
ret = EP_SUBMIT(priv->epbulkin, req);
|
2008-10-25 00:42:28 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-04-01 18:13:52 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_SNDSTATUSSUBMIT),
|
|
|
|
(uint16_t)-ret);
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2020-01-02 17:49:34 +01:00
|
|
|
sq_addlast((FAR sq_entry_t *)privreq, &priv->wrreqlist);
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return to the IDLE state */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
usbtrace(TRACE_CLASSSTATE(USBMSC_CLASSSTATE_CMDSTATUSIDLE), 0);
|
|
|
|
priv->thstate = USBMSC_STATE_IDLE;
|
2008-10-25 00:42:28 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-03-25 21:19:27 +01:00
|
|
|
* Name: usbmsc_scsi_main
|
2008-10-25 00:42:28 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is the main function of the USB storage worker thread. It loops
|
|
|
|
* until USB-related events occur, then processes those events accordingly
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-03-25 21:19:27 +01:00
|
|
|
int usbmsc_scsi_main(int argc, char *argv[])
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2014-03-25 21:19:27 +01:00
|
|
|
FAR struct usbmsc_dev_s *priv;
|
2008-10-25 00:42:28 +02:00
|
|
|
irqstate_t flags;
|
2009-12-15 15:25:14 +01:00
|
|
|
uint16_t eventset;
|
2008-10-25 00:42:28 +02:00
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Started\n");
|
2014-03-25 21:19:27 +01:00
|
|
|
|
|
|
|
/* Get the SCSI state data handed off from the initialization logic */
|
|
|
|
|
|
|
|
priv = g_usbmsc_handoff;
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
g_usbmsc_handoff = NULL;
|
|
|
|
usbmsc_synch_signal(priv);
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
/* Get exclusive access to SCSI state data */
|
|
|
|
|
|
|
|
ret = usbmsc_scsi_lock(priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This thread is started before the USB storage class is fully
|
|
|
|
* initialized. Wait here until we are told to begin. Start in the
|
|
|
|
* NOTINITIALIZED state
|
2008-10-25 00:42:28 +02:00
|
|
|
*/
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
uinfo("Waiting to be signaled\n");
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->thstate = USBMSC_STATE_STARTED;
|
2016-07-24 05:11:00 +02:00
|
|
|
while ((priv->theventset & USBMSC_EVENT_READY) == 0 &&
|
|
|
|
(priv->theventset & USBMSC_EVENT_TERMINATEREQUEST) == 0)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2020-04-01 18:13:52 +02:00
|
|
|
ret = usbmsc_scsi_wait(priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* The thread has been canceled */
|
|
|
|
|
|
|
|
usbmsc_scsi_unlock(priv);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Running\n");
|
2014-03-25 21:19:27 +01:00
|
|
|
|
2008-10-25 00:42:28 +02:00
|
|
|
/* Transition to the INITIALIZED/IDLE state */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->thstate = USBMSC_STATE_IDLE;
|
2008-10-25 00:42:28 +02:00
|
|
|
eventset = priv->theventset;
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->theventset = USBMSC_EVENT_NOEVENTS;
|
2014-03-25 21:19:27 +01:00
|
|
|
usbmsc_scsi_unlock(priv);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Then loop until we are asked to terminate */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
while ((eventset & USBMSC_EVENT_TERMINATEREQUEST) == 0)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
|
|
|
/* Wait for some interesting event. Note that we must both take the
|
|
|
|
* lock (to eliminate race conditions with other threads) and disable
|
2020-04-01 18:13:52 +02:00
|
|
|
* interrupts (to eliminate race conditions with USB interrupt
|
|
|
|
* handling.
|
2008-10-25 00:42:28 +02:00
|
|
|
*/
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
ret = usbmsc_scsi_lock(priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2012-01-25 21:17:59 +01:00
|
|
|
if (priv->theventset == USBMSC_EVENT_NOEVENTS)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2020-04-01 18:13:52 +02:00
|
|
|
ret = usbmsc_scsi_wait(priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* The thread has been canceled */
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
usbmsc_scsi_unlock(priv);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Sample any events before re-enabling interrupts. Any events that
|
|
|
|
* occur after re-enabling interrupts will have to be handled in the
|
|
|
|
* next time through the loop.
|
|
|
|
*/
|
|
|
|
|
|
|
|
eventset = priv->theventset;
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->theventset = USBMSC_EVENT_NOEVENTS;
|
2014-03-25 21:19:27 +01:00
|
|
|
usbmsc_scsi_unlock(priv);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2008-10-27 17:41:12 +01:00
|
|
|
/* Were we awakened by some event that requires immediate action?
|
|
|
|
*
|
2020-04-01 18:13:52 +02:00
|
|
|
* - The USBMSC_EVENT_DISCONNECT is signaled from the disconnect
|
|
|
|
* method after all transfers have been stopped, when the host is
|
|
|
|
* disconnected.
|
2008-10-27 17:41:12 +01:00
|
|
|
*
|
2020-04-01 18:13:52 +02:00
|
|
|
* - The CUSBMSC_EVENT_RESET is signaled when the bulk-storage
|
|
|
|
* specific USBMSC_REQ_MSRESET EP0 setup received. We must stop the
|
|
|
|
* current operation and reinitialize state.
|
2008-10-27 17:41:12 +01:00
|
|
|
*
|
2012-01-25 21:17:59 +01:00
|
|
|
* - The USBMSC_EVENT_CFGCHANGE is signaled when the EP0 setup logic
|
2008-10-27 17:41:12 +01:00
|
|
|
* receives a valid USB_REQ_SETCONFIGURATION request
|
|
|
|
*
|
2012-01-25 21:17:59 +01:00
|
|
|
* - The USBMSC_EVENT_IFCHANGE is signaled when the EP0 setup logic
|
2008-10-27 17:41:12 +01:00
|
|
|
* receives a valid USB_REQ_SETINTERFACE request
|
|
|
|
*
|
2020-04-01 18:13:52 +02:00
|
|
|
* - The USBMSC_EVENT_ABORTBULKOUT event is signaled by the CMDFINISH
|
2008-10-27 17:41:12 +01:00
|
|
|
* logic when there is a residue after processing a host-to-device
|
|
|
|
* transfer. We need to discard all incoming request.
|
|
|
|
*
|
|
|
|
* All other events are just wakeup calls and are intended only
|
|
|
|
* drive the state machine.
|
2008-10-25 00:42:28 +02:00
|
|
|
*/
|
|
|
|
|
2020-01-10 16:06:36 +01:00
|
|
|
if ((eventset & (USBMSC_EVENT_DISCONNECT | USBMSC_EVENT_RESET |
|
|
|
|
USBMSC_EVENT_CFGCHANGE | USBMSC_EVENT_IFCHANGE |
|
|
|
|
USBMSC_EVENT_ABORTBULKOUT)) != 0)
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
2009-12-01 16:28:33 +01:00
|
|
|
/* These events require that a new configuration be established */
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2014-03-25 21:19:27 +01:00
|
|
|
if ((eventset & (USBMSC_EVENT_CFGCHANGE)) != 0)
|
2009-12-01 16:28:33 +01:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbmsc_setconfig(priv, priv->thvalue);
|
2009-12-01 16:28:33 +01:00
|
|
|
}
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* These events required that we send a deferred EP0 setup
|
|
|
|
* response
|
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2020-01-10 16:06:36 +01:00
|
|
|
if ((eventset & (USBMSC_EVENT_RESET | USBMSC_EVENT_CFGCHANGE |
|
|
|
|
USBMSC_EVENT_IFCHANGE)) != 0)
|
2008-10-27 17:41:12 +01:00
|
|
|
{
|
2012-01-25 21:17:59 +01:00
|
|
|
usbmsc_deferredresponse(priv, false);
|
2008-10-27 17:41:12 +01:00
|
|
|
}
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* For all of these events... terminate any transactions in
|
|
|
|
* progress
|
|
|
|
*/
|
2008-10-25 00:42:28 +02:00
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->thstate = USBMSC_STATE_IDLE;
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
2013-09-24 23:06:17 +02:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2008-10-25 00:42:28 +02:00
|
|
|
|
|
|
|
/* Loop processing each SCSI command state. Each state handling
|
|
|
|
* function will do the following:
|
|
|
|
*
|
2020-04-01 18:13:52 +02:00
|
|
|
* - If it must block for an event, it will return a negated errno
|
|
|
|
* value
|
2008-10-25 00:42:28 +02:00
|
|
|
* - If it completes the processing for that state, it will (1) set
|
|
|
|
* the next appropriate state value and (2) return OK.
|
|
|
|
*
|
|
|
|
* So the following will loop until we must block for an event in
|
|
|
|
* a particular state. When we are awakened by an event (above) we
|
|
|
|
* will resume processing in the same state.
|
|
|
|
*/
|
|
|
|
|
2008-10-27 17:41:12 +01:00
|
|
|
do
|
2008-10-25 00:42:28 +02:00
|
|
|
{
|
|
|
|
switch (priv->thstate)
|
|
|
|
{
|
2020-04-01 18:13:52 +02:00
|
|
|
case USBMSC_STATE_IDLE: /* Started and waiting for commands */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_idlestate(priv);
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
case USBMSC_STATE_CMDPARSE: /* Parsing the received a command */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_cmdparsestate(priv);
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
case USBMSC_STATE_CMDREAD: /* Continuing to process a SCSI read command */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_cmdreadstate(priv);
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
case USBMSC_STATE_CMDWRITE: /* Continuing to process a SCSI write command */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_cmdwritestate(priv);
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
case USBMSC_STATE_CMDFINISH: /* Finish command processing */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_cmdfinishstate(priv);
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
case USBMSC_STATE_CMDSTATUS: /* Processing the status phase of a command */
|
2012-01-25 21:17:59 +01:00
|
|
|
ret = usbmsc_cmdstatusstate(priv);
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
case USBMSC_STATE_NOTSTARTED: /* Thread has not yet been started */
|
|
|
|
case USBMSC_STATE_STARTED: /* Started, but is not yet initialized */
|
|
|
|
case USBMSC_STATE_TERMINATED: /* Thread has exited */
|
2008-10-25 00:42:28 +02:00
|
|
|
default:
|
2020-04-01 18:13:52 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_INVALIDSTATE),
|
|
|
|
priv->thstate);
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->thstate = USBMSC_STATE_IDLE;
|
2008-10-27 17:41:12 +01:00
|
|
|
ret = OK;
|
2008-10-25 00:42:28 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-10-27 17:41:12 +01:00
|
|
|
while (ret == OK);
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Transition to the TERMINATED state and exit */
|
|
|
|
|
2012-01-25 21:17:59 +01:00
|
|
|
priv->thstate = USBMSC_STATE_TERMINATED;
|
2014-03-25 21:19:27 +01:00
|
|
|
usbmsc_synch_signal(priv);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbmsc_scsi_signal
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Signal the SCSI worker thread that SCSI events need service.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void usbmsc_scsi_signal(FAR struct usbmsc_dev_s *priv)
|
|
|
|
{
|
2014-11-25 20:15:09 +01:00
|
|
|
irqstate_t flags;
|
2014-03-25 21:19:27 +01:00
|
|
|
|
|
|
|
/* A flag is used to prevent driving up the semaphore count. This function
|
|
|
|
* is called (primarily) from interrupt level logic so we must disable
|
|
|
|
* interrupts momentarily to assure that test of the flag and the increment
|
|
|
|
* of the semaphore count are atomic.
|
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2014-03-25 21:19:27 +01:00
|
|
|
if (priv->thwaiting)
|
|
|
|
{
|
|
|
|
priv->thwaiting = false;
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&priv->thwaitsem);
|
2014-03-25 21:19:27 +01:00
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2014-03-25 21:19:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbmsc_scsi_lock
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get exclusive access to SCSI state data.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
int usbmsc_scsi_lock(FAR struct usbmsc_dev_s *priv)
|
2014-03-25 21:19:27 +01:00
|
|
|
{
|
2020-04-01 18:13:52 +02:00
|
|
|
return nxsem_wait_uninterruptible(&priv->thlock);
|
2008-10-25 00:42:28 +02:00
|
|
|
}
|