Initial public release of the 2024A software.
This commit is contained in:
parent
7b9ad3edfd
commit
303e9e1dad
361 changed files with 60083 additions and 2 deletions
110
managed_components/espressif__usb_host_msc/src/diskio_usb.c
Normal file
110
managed_components/espressif__usb_host_msc/src/diskio_usb.c
Normal file
|
@ -0,0 +1,110 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "diskio_impl.h"
|
||||
#include "ffconf.h"
|
||||
#include "ff.h"
|
||||
#include "esp_log.h"
|
||||
#include "diskio_usb.h"
|
||||
#include "msc_scsi_bot.h"
|
||||
#include "msc_common.h"
|
||||
#include "usb/usb_types_stack.h"
|
||||
|
||||
static usb_disk_t *s_disks[FF_VOLUMES] = { NULL };
|
||||
|
||||
static const char *TAG = "diskio_usb";
|
||||
|
||||
static DSTATUS usb_disk_initialize (BYTE pdrv)
|
||||
{
|
||||
return RES_OK;
|
||||
}
|
||||
|
||||
static DSTATUS usb_disk_status (BYTE pdrv)
|
||||
{
|
||||
return RES_OK;
|
||||
}
|
||||
|
||||
static DRESULT usb_disk_read (BYTE pdrv, BYTE *buff, DWORD sector, UINT count)
|
||||
{
|
||||
assert(pdrv < FF_VOLUMES);
|
||||
assert(s_disks[pdrv]);
|
||||
|
||||
usb_disk_t *disk = s_disks[pdrv];
|
||||
size_t sector_size = disk->block_size;
|
||||
msc_device_t *dev = __containerof(disk, msc_device_t, disk);
|
||||
|
||||
esp_err_t err = scsi_cmd_read10(dev, buff, sector, count, sector_size);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "scsi_cmd_read10 failed (%d)", err);
|
||||
return RES_ERROR;
|
||||
}
|
||||
|
||||
return RES_OK;
|
||||
}
|
||||
|
||||
static DRESULT usb_disk_write (BYTE pdrv, const BYTE *buff, DWORD sector, UINT count)
|
||||
{
|
||||
assert(pdrv < FF_VOLUMES);
|
||||
assert(s_disks[pdrv]);
|
||||
|
||||
usb_disk_t *disk = s_disks[pdrv];
|
||||
size_t sector_size = disk->block_size;
|
||||
msc_device_t *dev = __containerof(disk, msc_device_t, disk);
|
||||
|
||||
esp_err_t err = scsi_cmd_write10(dev, buff, sector, count, sector_size);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "scsi_cmd_write10 failed (%d)", err);
|
||||
return RES_ERROR;
|
||||
}
|
||||
return RES_OK;
|
||||
}
|
||||
|
||||
static DRESULT usb_disk_ioctl (BYTE pdrv, BYTE cmd, void *buff)
|
||||
{
|
||||
assert(pdrv < FF_VOLUMES);
|
||||
assert(s_disks[pdrv]);
|
||||
|
||||
usb_disk_t *disk = s_disks[pdrv];
|
||||
|
||||
switch (cmd) {
|
||||
case CTRL_SYNC:
|
||||
return RES_OK;
|
||||
case GET_SECTOR_COUNT:
|
||||
*((DWORD *) buff) = disk->block_count;
|
||||
return RES_OK;
|
||||
case GET_SECTOR_SIZE:
|
||||
*((WORD *) buff) = disk->block_size;
|
||||
return RES_OK;
|
||||
case GET_BLOCK_SIZE:
|
||||
return RES_ERROR;
|
||||
}
|
||||
return RES_ERROR;
|
||||
}
|
||||
|
||||
void ff_diskio_register_msc(BYTE pdrv, usb_disk_t *disk)
|
||||
{
|
||||
assert(pdrv < FF_VOLUMES);
|
||||
|
||||
static const ff_diskio_impl_t usb_disk_impl = {
|
||||
.init = &usb_disk_initialize,
|
||||
.status = &usb_disk_status,
|
||||
.read = &usb_disk_read,
|
||||
.write = &usb_disk_write,
|
||||
.ioctl = &usb_disk_ioctl
|
||||
};
|
||||
s_disks[pdrv] = disk;
|
||||
ff_diskio_register(pdrv, &usb_disk_impl);
|
||||
}
|
||||
|
||||
BYTE ff_diskio_get_pdrv_disk(const usb_disk_t *disk)
|
||||
{
|
||||
for (int i = 0; i < FF_VOLUMES; i++) {
|
||||
if (disk == s_disks[i]) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return 0xff;
|
||||
}
|
698
managed_components/espressif__usb_host_msc/src/msc_host.c
Normal file
698
managed_components/espressif__usb_host_msc/src/msc_host.c
Normal file
|
@ -0,0 +1,698 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/queue.h>
|
||||
#include <sys/param.h>
|
||||
#include "esp_log.h"
|
||||
#include "esp_heap_caps.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "usb/usb_host.h"
|
||||
#include "diskio_usb.h"
|
||||
#include "msc_common.h"
|
||||
#include "usb/msc_host.h"
|
||||
#include "msc_scsi_bot.h"
|
||||
#include "usb/usb_types_ch9.h"
|
||||
#include "usb/usb_helpers.h"
|
||||
#include "soc/soc_memory_layout.h"
|
||||
|
||||
// MSC driver spin lock
|
||||
static portMUX_TYPE msc_lock = portMUX_INITIALIZER_UNLOCKED;
|
||||
#define MSC_ENTER_CRITICAL() portENTER_CRITICAL(&msc_lock)
|
||||
#define MSC_EXIT_CRITICAL() portEXIT_CRITICAL(&msc_lock)
|
||||
|
||||
#define MSC_GOTO_ON_FALSE_CRITICAL(exp, err) \
|
||||
do { \
|
||||
if(!(exp)) { \
|
||||
MSC_EXIT_CRITICAL(); \
|
||||
ret = err; \
|
||||
goto fail; \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
#define MSC_RETURN_ON_FALSE_CRITICAL(exp, err) \
|
||||
do { \
|
||||
if(!(exp)) { \
|
||||
MSC_EXIT_CRITICAL(); \
|
||||
return err; \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
// MSC Control requests
|
||||
#define USB_MASS_REQ_INIT_RESET(ctrl_req_ptr, intf_num) ({ \
|
||||
(ctrl_req_ptr)->bmRequestType = USB_BM_REQUEST_TYPE_DIR_OUT | \
|
||||
USB_BM_REQUEST_TYPE_TYPE_CLASS | \
|
||||
USB_BM_REQUEST_TYPE_RECIP_INTERFACE; \
|
||||
(ctrl_req_ptr)->bRequest = 0xFF; \
|
||||
(ctrl_req_ptr)->wValue = 0; \
|
||||
(ctrl_req_ptr)->wIndex = (intf_num); \
|
||||
(ctrl_req_ptr)->wLength = 0; \
|
||||
})
|
||||
|
||||
#define USB_MASS_REQ_INIT_GET_MAX_LUN(ctrl_req_ptr, intf_num) ({ \
|
||||
(ctrl_req_ptr)->bmRequestType = USB_BM_REQUEST_TYPE_DIR_IN | \
|
||||
USB_BM_REQUEST_TYPE_TYPE_CLASS | \
|
||||
USB_BM_REQUEST_TYPE_RECIP_INTERFACE; \
|
||||
(ctrl_req_ptr)->bRequest = 0xFE; \
|
||||
(ctrl_req_ptr)->wValue = 0; \
|
||||
(ctrl_req_ptr)->wIndex = (intf_num); \
|
||||
(ctrl_req_ptr)->wLength = 1; \
|
||||
})
|
||||
|
||||
#define FEATURE_SELECTOR_ENDPOINT 0
|
||||
#define USB_SETUP_PACKET_INIT_CLEAR_FEATURE_EP(ctrl_req_ptr, ep_num) ({ \
|
||||
(ctrl_req_ptr)->bmRequestType = USB_BM_REQUEST_TYPE_DIR_OUT | \
|
||||
USB_BM_REQUEST_TYPE_TYPE_STANDARD | \
|
||||
USB_BM_REQUEST_TYPE_RECIP_ENDPOINT; \
|
||||
(ctrl_req_ptr)->bRequest = USB_B_REQUEST_CLEAR_FEATURE; \
|
||||
(ctrl_req_ptr)->wValue = FEATURE_SELECTOR_ENDPOINT; \
|
||||
(ctrl_req_ptr)->wIndex = (ep_num); \
|
||||
(ctrl_req_ptr)->wLength = 0; \
|
||||
})
|
||||
|
||||
#define DEFAULT_XFER_SIZE (64) // Transfer size used for all transfers apart from SCSI read/write
|
||||
#define WAIT_FOR_READY_TIMEOUT_MS 5000
|
||||
#define SCSI_COMMAND_SET 0x06
|
||||
#define BULK_ONLY_TRANSFER 0x50
|
||||
#define MSC_NO_SENSE 0x00
|
||||
#define MSC_NOT_READY 0x02
|
||||
#define MSC_UNIT_ATTENTION 0x06
|
||||
|
||||
static const char *TAG = "USB_MSC";
|
||||
typedef struct {
|
||||
usb_host_client_handle_t client_handle;
|
||||
msc_host_event_cb_t user_cb;
|
||||
void *user_arg;
|
||||
SemaphoreHandle_t all_events_handled;
|
||||
volatile bool end_client_event_handling;
|
||||
bool event_handling_started;
|
||||
STAILQ_HEAD(devices, msc_host_device) devices_tailq;
|
||||
} msc_driver_t;
|
||||
|
||||
static msc_driver_t *s_msc_driver;
|
||||
|
||||
|
||||
static const usb_standard_desc_t *next_interface_desc(const usb_standard_desc_t *desc, size_t len, size_t *offset)
|
||||
{
|
||||
return usb_parse_next_descriptor_of_type(desc, len, USB_W_VALUE_DT_INTERFACE, (int *)offset);
|
||||
}
|
||||
|
||||
static const usb_standard_desc_t *next_endpoint_desc(const usb_standard_desc_t *desc, size_t len, size_t *offset)
|
||||
{
|
||||
return usb_parse_next_descriptor_of_type(desc, len, USB_B_DESCRIPTOR_TYPE_ENDPOINT, (int *)offset);
|
||||
}
|
||||
|
||||
static inline bool is_in_endpoint(uint8_t endpoint)
|
||||
{
|
||||
return endpoint & USB_B_ENDPOINT_ADDRESS_EP_DIR_MASK ? true : false;
|
||||
}
|
||||
|
||||
static const usb_intf_desc_t *find_msc_interface(const usb_config_desc_t *config_desc, size_t *offset)
|
||||
{
|
||||
size_t total_length = config_desc->wTotalLength;
|
||||
const usb_standard_desc_t *next_desc = (const usb_standard_desc_t *)config_desc;
|
||||
|
||||
next_desc = next_interface_desc(next_desc, total_length, offset);
|
||||
|
||||
while ( next_desc ) {
|
||||
|
||||
const usb_intf_desc_t *ifc_desc = (const usb_intf_desc_t *)next_desc;
|
||||
|
||||
if ( ifc_desc->bInterfaceClass == USB_CLASS_MASS_STORAGE &&
|
||||
ifc_desc->bInterfaceSubClass == SCSI_COMMAND_SET &&
|
||||
ifc_desc->bInterfaceProtocol == BULK_ONLY_TRANSFER ) {
|
||||
return ifc_desc;
|
||||
}
|
||||
|
||||
next_desc = next_interface_desc(next_desc, total_length, offset);
|
||||
};
|
||||
return NULL;
|
||||
}
|
||||
|
||||
esp_err_t clear_feature(msc_device_t *device, uint8_t endpoint)
|
||||
{
|
||||
usb_device_handle_t dev = device->handle;
|
||||
usb_transfer_t *xfer = device->xfer;
|
||||
|
||||
MSC_RETURN_ON_ERROR( usb_host_endpoint_halt(dev, endpoint) );
|
||||
|
||||
esp_err_t err = usb_host_endpoint_flush(dev, endpoint);
|
||||
if (ESP_OK != err ) {
|
||||
// The endpoint cannot be flushed if it does not have STALL condition
|
||||
// Return without ESP_LOGE
|
||||
return err;
|
||||
}
|
||||
MSC_RETURN_ON_ERROR( usb_host_endpoint_clear(dev, endpoint) );
|
||||
|
||||
USB_SETUP_PACKET_INIT_CLEAR_FEATURE_EP((usb_setup_packet_t *)xfer->data_buffer, endpoint);
|
||||
MSC_RETURN_ON_ERROR( msc_control_transfer(device, USB_SETUP_PACKET_SIZE) );
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Bulk-Only Mass Storage Reset
|
||||
*
|
||||
* This class-specific request shall ready the device for the next CBW from the host.
|
||||
* The device shall preserve the value of its bulk data toggle bits and endpoint STALL conditions despite the Bulk-Only Mass Storage Reset.
|
||||
*
|
||||
* @see USB Mass Storage Class – Bulk Only Transport, Chapter 3.1
|
||||
*
|
||||
* @param[in] dev MSC device handle
|
||||
* @return esp_err_t
|
||||
*/
|
||||
static esp_err_t msc_mass_reset(msc_host_device_handle_t dev)
|
||||
{
|
||||
msc_device_t *device = (msc_device_t *)dev;
|
||||
usb_transfer_t *xfer = device->xfer;
|
||||
|
||||
USB_MASS_REQ_INIT_RESET((usb_setup_packet_t *)xfer->data_buffer, device->config.iface_num);
|
||||
MSC_RETURN_ON_ERROR( msc_control_transfer(device, USB_SETUP_PACKET_SIZE) );
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief MSC get maximum Logical Unit Number
|
||||
*
|
||||
* If the device implements 3 LUNs, the returned value is 2. (LUN0, LUN1, LUN2).
|
||||
*
|
||||
* This driver does not support multiple LUNs yet.
|
||||
*
|
||||
* @see USB Mass Storage Class – Bulk Only Transport, Chapter 3.2
|
||||
*
|
||||
* @param[in] dev MSC device handle
|
||||
* @param[out] lun Maximum Logical Unit Number
|
||||
* @return esp_err_t
|
||||
*/
|
||||
__attribute__((unused)) static esp_err_t msc_get_max_lun(msc_host_device_handle_t dev, uint8_t *lun)
|
||||
{
|
||||
msc_device_t *device = (msc_device_t *)dev;
|
||||
usb_transfer_t *xfer = device->xfer;
|
||||
|
||||
USB_MASS_REQ_INIT_GET_MAX_LUN((usb_setup_packet_t *)xfer->data_buffer, device->config.iface_num);
|
||||
MSC_RETURN_ON_ERROR( msc_control_transfer(device, USB_SETUP_PACKET_SIZE + 1) );
|
||||
|
||||
*lun = xfer->data_buffer[USB_SETUP_PACKET_SIZE];
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Extracts configuration from configuration descriptor.
|
||||
*
|
||||
* @note Passes interface and endpoint descriptors to obtain:
|
||||
|
||||
* - interface number, IN endpoint, OUT endpoint, max. packet size
|
||||
*
|
||||
* @param[in] cfg_desc Configuration descriptor
|
||||
* @param[out] cfg Obtained configuration
|
||||
* @return esp_err_t
|
||||
*/
|
||||
static esp_err_t extract_config_from_descriptor(const usb_config_desc_t *cfg_desc, msc_config_t *cfg)
|
||||
{
|
||||
size_t offset = 0;
|
||||
size_t total_len = cfg_desc->wTotalLength;
|
||||
const usb_intf_desc_t *ifc_desc = find_msc_interface(cfg_desc, &offset);
|
||||
assert(ifc_desc);
|
||||
const usb_standard_desc_t *next_desc = (const usb_standard_desc_t *)ifc_desc;
|
||||
const usb_ep_desc_t *ep_desc = NULL;
|
||||
|
||||
cfg->iface_num = ifc_desc->bInterfaceNumber;
|
||||
|
||||
next_desc = next_endpoint_desc(next_desc, total_len, &offset);
|
||||
MSC_RETURN_ON_FALSE(next_desc, ESP_ERR_NOT_SUPPORTED);
|
||||
ep_desc = (const usb_ep_desc_t *)next_desc;
|
||||
|
||||
if (is_in_endpoint(ep_desc->bEndpointAddress)) {
|
||||
cfg->bulk_in_ep = ep_desc->bEndpointAddress;
|
||||
cfg->bulk_in_mps = ep_desc->wMaxPacketSize;
|
||||
} else {
|
||||
cfg->bulk_out_ep = ep_desc->bEndpointAddress;
|
||||
}
|
||||
|
||||
next_desc = next_endpoint_desc(next_desc, total_len, &offset);
|
||||
MSC_RETURN_ON_FALSE(next_desc, ESP_ERR_NOT_SUPPORTED);
|
||||
ep_desc = (const usb_ep_desc_t *)next_desc;
|
||||
|
||||
if (is_in_endpoint(ep_desc->bEndpointAddress)) {
|
||||
cfg->bulk_in_ep = ep_desc->bEndpointAddress;
|
||||
cfg->bulk_in_mps = ep_desc->wMaxPacketSize;
|
||||
} else {
|
||||
cfg->bulk_out_ep = ep_desc->bEndpointAddress;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t msc_deinit_device(msc_device_t *dev, bool install_failed)
|
||||
{
|
||||
MSC_ENTER_CRITICAL();
|
||||
MSC_RETURN_ON_FALSE_CRITICAL( dev, ESP_ERR_INVALID_STATE );
|
||||
STAILQ_REMOVE(&s_msc_driver->devices_tailq, dev, msc_host_device, tailq_entry);
|
||||
MSC_EXIT_CRITICAL();
|
||||
|
||||
if (dev->transfer_done) {
|
||||
vSemaphoreDelete(dev->transfer_done);
|
||||
}
|
||||
if (install_failed) {
|
||||
// Error code is unchecked, as it's unknown at what point installation failed.
|
||||
usb_host_interface_release(s_msc_driver->client_handle, dev->handle, dev->config.iface_num);
|
||||
usb_host_device_close(s_msc_driver->client_handle, dev->handle);
|
||||
usb_host_transfer_free(dev->xfer);
|
||||
} else {
|
||||
MSC_RETURN_ON_ERROR( usb_host_interface_release(s_msc_driver->client_handle, dev->handle, dev->config.iface_num) );
|
||||
MSC_RETURN_ON_ERROR( usb_host_device_close(s_msc_driver->client_handle, dev->handle) );
|
||||
MSC_RETURN_ON_ERROR( usb_host_transfer_free(dev->xfer) );
|
||||
}
|
||||
|
||||
free(dev);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// Some MSC devices requires to change its internal state from non-ready to ready
|
||||
static esp_err_t msc_wait_for_ready_state(msc_device_t *dev, size_t timeout_ms)
|
||||
{
|
||||
esp_err_t err;
|
||||
scsi_sense_data_t sense;
|
||||
uint32_t trials = MAX(1, timeout_ms / 100);
|
||||
|
||||
do {
|
||||
err = scsi_cmd_unit_ready(dev);
|
||||
if (err == ESP_OK) {
|
||||
return ESP_OK;
|
||||
} else {
|
||||
// Some MSC devices report 'NOT READY TO READY TRANSITION - MEDIA CHANGED', which isn't cleared until a REQUEST SENSE is performed.
|
||||
MSC_RETURN_ON_ERROR( scsi_cmd_sense(dev, &sense) );
|
||||
if (sense.key != MSC_NOT_READY &&
|
||||
sense.key != MSC_UNIT_ATTENTION &&
|
||||
sense.key != MSC_NO_SENSE) {
|
||||
return ESP_ERR_MSC_INTERNAL;
|
||||
}
|
||||
}
|
||||
vTaskDelay( pdMS_TO_TICKS(100) );
|
||||
} while (trials-- && err);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static bool is_mass_storage_device(uint8_t dev_addr)
|
||||
{
|
||||
size_t dummy = 0;
|
||||
bool is_msc_device = false;
|
||||
usb_device_handle_t device;
|
||||
const usb_config_desc_t *config_desc;
|
||||
|
||||
if ( usb_host_device_open(s_msc_driver->client_handle, dev_addr, &device) == ESP_OK) {
|
||||
if ( usb_host_get_active_config_descriptor(device, &config_desc) == ESP_OK ) {
|
||||
if ( find_msc_interface(config_desc, &dummy) ) {
|
||||
is_msc_device = true;
|
||||
} else {
|
||||
ESP_LOGD(TAG, "Connected USB device is not MSC");
|
||||
}
|
||||
}
|
||||
usb_host_device_close(s_msc_driver->client_handle, device);
|
||||
}
|
||||
|
||||
return is_msc_device;
|
||||
}
|
||||
|
||||
esp_err_t msc_host_handle_events(uint32_t timeout)
|
||||
{
|
||||
MSC_RETURN_ON_FALSE(s_msc_driver != NULL, ESP_ERR_INVALID_STATE);
|
||||
|
||||
ESP_LOGV(TAG, "USB MSC handling");
|
||||
s_msc_driver->event_handling_started = true;
|
||||
esp_err_t ret = usb_host_client_handle_events(s_msc_driver->client_handle, timeout);
|
||||
if (s_msc_driver->end_client_event_handling) {
|
||||
xSemaphoreGive(s_msc_driver->all_events_handled);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USB Client Event handler
|
||||
*
|
||||
* Handle all USB client events such as USB transfers and connections/disconnections
|
||||
*
|
||||
* @param[in] arg Argument, not used
|
||||
*/
|
||||
static void event_handler_task(void *arg)
|
||||
{
|
||||
ESP_LOGD(TAG, "USB MSC handling start");
|
||||
while (msc_host_handle_events(portMAX_DELAY) == ESP_OK) {
|
||||
}
|
||||
ESP_LOGD(TAG, "USB MSC handling stop");
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
static msc_device_t *find_msc_device(usb_device_handle_t device_handle)
|
||||
{
|
||||
msc_device_t *iter;
|
||||
msc_device_t *device_found = NULL;
|
||||
|
||||
MSC_ENTER_CRITICAL();
|
||||
STAILQ_FOREACH(iter, &s_msc_driver->devices_tailq, tailq_entry) {
|
||||
if (device_handle == iter->handle) {
|
||||
device_found = iter;
|
||||
break;
|
||||
}
|
||||
}
|
||||
MSC_EXIT_CRITICAL();
|
||||
|
||||
return device_found;
|
||||
}
|
||||
|
||||
static void client_event_cb(const usb_host_client_event_msg_t *event, void *arg)
|
||||
{
|
||||
if (event->event == USB_HOST_CLIENT_EVENT_NEW_DEV) {
|
||||
if (is_mass_storage_device(event->new_dev.address)) {
|
||||
const msc_host_event_t msc_event = {
|
||||
.event = MSC_DEVICE_CONNECTED,
|
||||
.device.address = event->new_dev.address,
|
||||
};
|
||||
s_msc_driver->user_cb(&msc_event, s_msc_driver->user_arg);
|
||||
}
|
||||
} else if (event->event == USB_HOST_CLIENT_EVENT_DEV_GONE) {
|
||||
msc_device_t *msc_device = find_msc_device(event->dev_gone.dev_hdl);
|
||||
if (msc_device) {
|
||||
const msc_host_event_t msc_event = {
|
||||
.event = MSC_DEVICE_DISCONNECTED,
|
||||
.device.handle = msc_device,
|
||||
};
|
||||
s_msc_driver->user_cb(&msc_event, s_msc_driver->user_arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t msc_host_install(const msc_host_driver_config_t *config)
|
||||
{
|
||||
esp_err_t ret;
|
||||
|
||||
MSC_RETURN_ON_INVALID_ARG(config);
|
||||
MSC_RETURN_ON_INVALID_ARG(config->callback);
|
||||
if ( config->create_backround_task ) {
|
||||
MSC_RETURN_ON_FALSE(config->stack_size != 0, ESP_ERR_INVALID_ARG);
|
||||
MSC_RETURN_ON_FALSE(config->task_priority != 0, ESP_ERR_INVALID_ARG);
|
||||
}
|
||||
MSC_RETURN_ON_FALSE(!s_msc_driver, ESP_ERR_INVALID_STATE);
|
||||
|
||||
msc_driver_t *driver = calloc(1, sizeof(msc_driver_t));
|
||||
MSC_RETURN_ON_FALSE(driver, ESP_ERR_NO_MEM);
|
||||
driver->user_cb = config->callback;
|
||||
driver->user_arg = config->callback_arg;
|
||||
|
||||
usb_host_client_config_t client_config = {
|
||||
.async.client_event_callback = client_event_cb,
|
||||
.async.callback_arg = NULL,
|
||||
.max_num_event_msg = 10,
|
||||
};
|
||||
|
||||
driver->end_client_event_handling = false;
|
||||
driver->all_events_handled = xSemaphoreCreateBinary();
|
||||
MSC_GOTO_ON_FALSE(driver->all_events_handled, ESP_ERR_NO_MEM);
|
||||
|
||||
MSC_GOTO_ON_ERROR( usb_host_client_register(&client_config, &driver->client_handle) );
|
||||
|
||||
MSC_ENTER_CRITICAL();
|
||||
MSC_GOTO_ON_FALSE_CRITICAL(!s_msc_driver, ESP_ERR_INVALID_STATE);
|
||||
s_msc_driver = driver;
|
||||
STAILQ_INIT(&s_msc_driver->devices_tailq);
|
||||
MSC_EXIT_CRITICAL();
|
||||
|
||||
if (config->create_backround_task) {
|
||||
BaseType_t task_created = xTaskCreatePinnedToCore(
|
||||
event_handler_task,
|
||||
"USB MSC",
|
||||
config->stack_size,
|
||||
NULL,
|
||||
config->task_priority,
|
||||
NULL,
|
||||
config->core_id);
|
||||
MSC_GOTO_ON_FALSE(task_created, ESP_ERR_NO_MEM);
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
|
||||
fail:
|
||||
s_msc_driver = NULL;
|
||||
usb_host_client_deregister(driver->client_handle);
|
||||
if (driver->all_events_handled) {
|
||||
vSemaphoreDelete(driver->all_events_handled);
|
||||
}
|
||||
free(driver);
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t msc_host_uninstall(void)
|
||||
{
|
||||
// Make sure msc driver is installed,
|
||||
// not being uninstalled from other task
|
||||
// and no msc device is registered
|
||||
MSC_ENTER_CRITICAL();
|
||||
MSC_RETURN_ON_FALSE_CRITICAL( s_msc_driver != NULL, ESP_ERR_INVALID_STATE );
|
||||
MSC_RETURN_ON_FALSE_CRITICAL( !s_msc_driver->end_client_event_handling, ESP_ERR_INVALID_STATE );
|
||||
MSC_RETURN_ON_FALSE_CRITICAL( STAILQ_EMPTY(&s_msc_driver->devices_tailq), ESP_ERR_INVALID_STATE );
|
||||
s_msc_driver->end_client_event_handling = true;
|
||||
MSC_EXIT_CRITICAL();
|
||||
|
||||
if (s_msc_driver->event_handling_started) {
|
||||
ESP_ERROR_CHECK( usb_host_client_unblock(s_msc_driver->client_handle) );
|
||||
// In case the event handling started, we must wait until it finishes
|
||||
xSemaphoreTake(s_msc_driver->all_events_handled, portMAX_DELAY);
|
||||
}
|
||||
vSemaphoreDelete(s_msc_driver->all_events_handled);
|
||||
ESP_ERROR_CHECK( usb_host_client_deregister(s_msc_driver->client_handle) );
|
||||
free(s_msc_driver);
|
||||
s_msc_driver = NULL;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t msc_host_install_device(uint8_t device_address, msc_host_device_handle_t *msc_device_handle)
|
||||
{
|
||||
esp_err_t ret;
|
||||
uint32_t block_size, block_count;
|
||||
const usb_config_desc_t *config_desc;
|
||||
msc_device_t *msc_device;
|
||||
|
||||
MSC_GOTO_ON_FALSE( msc_device = calloc(1, sizeof(msc_device_t)), ESP_ERR_NO_MEM );
|
||||
|
||||
MSC_ENTER_CRITICAL();
|
||||
MSC_GOTO_ON_FALSE_CRITICAL( s_msc_driver, ESP_ERR_INVALID_STATE );
|
||||
MSC_GOTO_ON_FALSE_CRITICAL( s_msc_driver->client_handle, ESP_ERR_INVALID_STATE );
|
||||
STAILQ_INSERT_TAIL(&s_msc_driver->devices_tailq, msc_device, tailq_entry);
|
||||
MSC_EXIT_CRITICAL();
|
||||
|
||||
MSC_GOTO_ON_FALSE( msc_device->transfer_done = xSemaphoreCreateBinary(), ESP_ERR_NO_MEM);
|
||||
MSC_GOTO_ON_ERROR( usb_host_device_open(s_msc_driver->client_handle, device_address, &msc_device->handle) );
|
||||
MSC_GOTO_ON_ERROR( usb_host_get_active_config_descriptor(msc_device->handle, &config_desc) );
|
||||
MSC_GOTO_ON_ERROR( extract_config_from_descriptor(config_desc, &msc_device->config) );
|
||||
MSC_GOTO_ON_ERROR( usb_host_transfer_alloc(DEFAULT_XFER_SIZE, 0, &msc_device->xfer) );
|
||||
MSC_GOTO_ON_ERROR( usb_host_interface_claim(
|
||||
s_msc_driver->client_handle,
|
||||
msc_device->handle,
|
||||
msc_device->config.iface_num, 0) );
|
||||
|
||||
MSC_GOTO_ON_ERROR( scsi_cmd_inquiry(msc_device) );
|
||||
MSC_GOTO_ON_ERROR( msc_wait_for_ready_state(msc_device, WAIT_FOR_READY_TIMEOUT_MS) );
|
||||
MSC_GOTO_ON_ERROR( scsi_cmd_read_capacity(msc_device, &block_size, &block_count) );
|
||||
|
||||
msc_device->disk.block_size = block_size;
|
||||
msc_device->disk.block_count = block_count;
|
||||
*msc_device_handle = msc_device;
|
||||
|
||||
return ESP_OK;
|
||||
|
||||
fail:
|
||||
msc_deinit_device(msc_device, true);
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t msc_host_uninstall_device(msc_host_device_handle_t device)
|
||||
{
|
||||
MSC_RETURN_ON_INVALID_ARG(device);
|
||||
return msc_deinit_device((msc_device_t *)device, false);
|
||||
}
|
||||
|
||||
esp_err_t msc_host_read_sector(msc_host_device_handle_t device, size_t sector, void *data, size_t size)
|
||||
{
|
||||
MSC_RETURN_ON_INVALID_ARG(device);
|
||||
msc_device_t *dev = (msc_device_t *)device;
|
||||
|
||||
return scsi_cmd_read10(dev, data, sector, 1, dev->disk.block_size);
|
||||
}
|
||||
|
||||
esp_err_t msc_host_write_sector(msc_host_device_handle_t device, size_t sector, const void *data, size_t size)
|
||||
{
|
||||
MSC_RETURN_ON_INVALID_ARG(device);
|
||||
msc_device_t *dev = (msc_device_t *)device;
|
||||
|
||||
return scsi_cmd_write10(dev, data, sector, 1, dev->disk.block_size);
|
||||
}
|
||||
|
||||
static void copy_string_desc(wchar_t *dest, const usb_str_desc_t *src)
|
||||
{
|
||||
if (dest == NULL) {
|
||||
return;
|
||||
}
|
||||
if (src != NULL) {
|
||||
size_t len = MIN((src->bLength - USB_STANDARD_DESC_SIZE) / 2, MSC_STR_DESC_SIZE - 1);
|
||||
for (int i = 0; i < len; i++) {
|
||||
dest[i] = (wchar_t)src->wData[i];
|
||||
}
|
||||
if (dest != NULL) { // This should be always true, we just check to avoid LoadProhibited exception
|
||||
dest[len] = 0;
|
||||
}
|
||||
} else {
|
||||
dest[0] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t msc_host_get_device_info(msc_host_device_handle_t device, msc_host_device_info_t *info)
|
||||
{
|
||||
MSC_RETURN_ON_INVALID_ARG(device);
|
||||
MSC_RETURN_ON_INVALID_ARG(info);
|
||||
|
||||
msc_device_t *dev = (msc_device_t *)device;
|
||||
const usb_device_desc_t *desc;
|
||||
usb_device_info_t dev_info;
|
||||
|
||||
MSC_RETURN_ON_ERROR( usb_host_get_device_descriptor(dev->handle, &desc) );
|
||||
MSC_RETURN_ON_ERROR( usb_host_device_info(dev->handle, &dev_info) );
|
||||
|
||||
info->idProduct = desc->idProduct;
|
||||
info->idVendor = desc->idVendor;
|
||||
info->sector_size = dev->disk.block_size;
|
||||
info->sector_count = dev->disk.block_count;
|
||||
|
||||
copy_string_desc(info->iManufacturer, dev_info.str_desc_manufacturer);
|
||||
copy_string_desc(info->iProduct, dev_info.str_desc_product);
|
||||
copy_string_desc(info->iSerialNumber, dev_info.str_desc_serial_num);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t msc_host_print_descriptors(msc_host_device_handle_t device)
|
||||
{
|
||||
msc_device_t *dev = (msc_device_t *)device;
|
||||
const usb_device_desc_t *device_desc;
|
||||
const usb_config_desc_t *config_desc;
|
||||
MSC_RETURN_ON_ERROR( usb_host_get_device_descriptor(dev->handle, &device_desc) );
|
||||
MSC_RETURN_ON_ERROR( usb_host_get_active_config_descriptor(dev->handle, &config_desc) );
|
||||
usb_print_device_descriptor(device_desc);
|
||||
usb_print_config_descriptor(config_desc, NULL);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void transfer_callback(usb_transfer_t *transfer)
|
||||
{
|
||||
msc_device_t *device = (msc_device_t *)transfer->context;
|
||||
|
||||
if (transfer->status != USB_TRANSFER_STATUS_COMPLETED) {
|
||||
ESP_LOGE("Transfer failed", "Status %d", transfer->status);
|
||||
}
|
||||
|
||||
xSemaphoreGive(device->transfer_done);
|
||||
}
|
||||
|
||||
static usb_transfer_status_t wait_for_transfer_done(usb_transfer_t *xfer)
|
||||
{
|
||||
msc_device_t *device = (msc_device_t *)xfer->context;
|
||||
BaseType_t received = xSemaphoreTake(device->transfer_done, pdMS_TO_TICKS(xfer->timeout_ms));
|
||||
usb_transfer_status_t status = xfer->status;
|
||||
|
||||
if (received != pdTRUE) {
|
||||
usb_host_endpoint_halt(xfer->device_handle, xfer->bEndpointAddress);
|
||||
usb_host_endpoint_flush(xfer->device_handle, xfer->bEndpointAddress);
|
||||
usb_host_endpoint_clear(xfer->device_handle, xfer->bEndpointAddress);
|
||||
xSemaphoreTake(device->transfer_done, portMAX_DELAY); // Since we flushed the EP, this should return immediately
|
||||
status = USB_TRANSFER_STATUS_TIMED_OUT;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
esp_err_t msc_bulk_transfer(msc_device_t *device, uint8_t *data, size_t size, msc_endpoint_t ep)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
usb_transfer_t *xfer = device->xfer;
|
||||
size_t transfer_size = (ep == MSC_EP_IN) ? usb_round_up_to_mps(size, device->config.bulk_in_mps) : size;
|
||||
|
||||
if (xfer->data_buffer_size < transfer_size) {
|
||||
// The allocated buffer is not large enough -> realloc
|
||||
MSC_RETURN_ON_ERROR( usb_host_transfer_free(xfer) );
|
||||
MSC_RETURN_ON_ERROR( usb_host_transfer_alloc(transfer_size, 0, &device->xfer) );
|
||||
xfer = device->xfer;
|
||||
}
|
||||
|
||||
if (ep == MSC_EP_IN) {
|
||||
xfer->bEndpointAddress = device->config.bulk_in_ep;
|
||||
} else {
|
||||
xfer->bEndpointAddress = device->config.bulk_out_ep;
|
||||
memcpy(xfer->data_buffer, data, size);
|
||||
}
|
||||
|
||||
xfer->num_bytes = transfer_size;
|
||||
xfer->device_handle = device->handle;
|
||||
xfer->callback = transfer_callback;
|
||||
xfer->timeout_ms = 5000;
|
||||
xfer->context = device;
|
||||
|
||||
MSC_RETURN_ON_ERROR( usb_host_transfer_submit(xfer) );
|
||||
const usb_transfer_status_t status = wait_for_transfer_done(xfer);
|
||||
switch (status) {
|
||||
case USB_TRANSFER_STATUS_COMPLETED:
|
||||
if (ep == MSC_EP_IN) {
|
||||
memcpy(data, xfer->data_buffer, xfer->actual_num_bytes);
|
||||
}
|
||||
ret = ESP_OK;
|
||||
break;
|
||||
case USB_TRANSFER_STATUS_STALL:
|
||||
ret = ESP_ERR_MSC_STALL; break;
|
||||
default:
|
||||
ret = ESP_ERR_MSC_INTERNAL; break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t msc_control_transfer(msc_device_t *device, size_t len)
|
||||
{
|
||||
usb_transfer_t *xfer = device->xfer;
|
||||
xfer->device_handle = device->handle;
|
||||
xfer->bEndpointAddress = 0;
|
||||
xfer->callback = transfer_callback;
|
||||
xfer->timeout_ms = 5000;
|
||||
xfer->num_bytes = len;
|
||||
xfer->context = device;
|
||||
|
||||
MSC_RETURN_ON_ERROR( usb_host_transfer_submit_control(s_msc_driver->client_handle, xfer));
|
||||
return wait_for_transfer_done(xfer) == USB_TRANSFER_STATUS_COMPLETED ? ESP_OK : ESP_ERR_MSC_INTERNAL;
|
||||
}
|
||||
|
||||
esp_err_t msc_host_reset_recovery(msc_host_device_handle_t device)
|
||||
{
|
||||
// USB Mass Storage Class – Bulk Only Transport Revision 1.0
|
||||
// 5.3.4 Reset Recovery
|
||||
// For Reset Recovery the host shall issue in the following order: :
|
||||
// (a) a Bulk-Only Mass Storage Reset
|
||||
// (b) a Clear Feature HALT to the Bulk-In endpoint
|
||||
// (c) a Clear Feature HALT to the Bulk-Out endpoint
|
||||
|
||||
ESP_RETURN_ON_ERROR( msc_mass_reset(device), TAG, "Mass reset failed" );
|
||||
// Clear feature will fail if there is not STALL on the endpoint, so we don't check the errors here
|
||||
clear_feature(device, device->config.bulk_in_ep);
|
||||
clear_feature(device, device->config.bulk_out_ep);
|
||||
MSC_RETURN_ON_ERROR( msc_wait_for_ready_state(device, WAIT_FOR_READY_TIMEOUT_MS) );
|
||||
return ESP_OK;
|
||||
}
|
131
managed_components/espressif__usb_host_msc/src/msc_host_vfs.c
Normal file
131
managed_components/espressif__usb_host_msc/src/msc_host_vfs.c
Normal file
|
@ -0,0 +1,131 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/param.h>
|
||||
#include "msc_common.h"
|
||||
#include "usb/msc_host_vfs.h"
|
||||
#include "diskio_impl.h"
|
||||
#include "ffconf.h"
|
||||
#include "ff.h"
|
||||
#include "esp_idf_version.h"
|
||||
|
||||
#define DRIVE_STR_LEN 3
|
||||
|
||||
typedef struct msc_host_vfs {
|
||||
char drive[DRIVE_STR_LEN];
|
||||
char *base_path;
|
||||
uint8_t pdrv;
|
||||
} msc_host_vfs_t;
|
||||
|
||||
static const char *TAG = "MSC VFS";
|
||||
|
||||
static esp_err_t msc_format_storage(size_t block_size, size_t allocation_size, const char *drv)
|
||||
{
|
||||
void *workbuf = NULL;
|
||||
const size_t workbuf_size = 4096;
|
||||
|
||||
MSC_RETURN_ON_FALSE( workbuf = ff_memalloc(workbuf_size), ESP_ERR_NO_MEM );
|
||||
|
||||
// Valid value of cluster size is between sector_size and 128 * sector_size.
|
||||
size_t cluster_size = MIN(MAX(allocation_size, block_size), 128 * block_size);
|
||||
|
||||
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
FRESULT err = f_mkfs(drv, FM_ANY | FM_SFD, cluster_size, workbuf, workbuf_size);
|
||||
#else
|
||||
const MKFS_PARM opt = {(BYTE)(FM_ANY | FM_SFD), 0, 0, 0, cluster_size};
|
||||
FRESULT err = f_mkfs(drv, &opt, workbuf, workbuf_size);
|
||||
#endif
|
||||
|
||||
if (err) {
|
||||
ESP_LOGE(TAG, "Formatting failed with error: %d", err);
|
||||
free(workbuf);
|
||||
return ESP_ERR_MSC_FORMAT_FAILED;
|
||||
}
|
||||
|
||||
free(workbuf);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void dealloc_msc_vfs(msc_host_vfs_t *vfs)
|
||||
{
|
||||
free(vfs->base_path);
|
||||
free(vfs);
|
||||
}
|
||||
|
||||
esp_err_t msc_host_vfs_register(msc_host_device_handle_t device,
|
||||
const char *base_path,
|
||||
const esp_vfs_fat_mount_config_t *mount_config,
|
||||
msc_host_vfs_handle_t *vfs_handle)
|
||||
{
|
||||
MSC_RETURN_ON_INVALID_ARG(device);
|
||||
MSC_RETURN_ON_INVALID_ARG(base_path);
|
||||
MSC_RETURN_ON_INVALID_ARG(mount_config);
|
||||
MSC_RETURN_ON_INVALID_ARG(vfs_handle);
|
||||
|
||||
FATFS *fs = NULL;
|
||||
BYTE pdrv;
|
||||
bool diskio_registered = false;
|
||||
esp_err_t ret = ESP_ERR_MSC_MOUNT_FAILED;
|
||||
msc_device_t *dev = (msc_device_t *)device;
|
||||
size_t block_size = dev->disk.block_size;
|
||||
size_t alloc_size = mount_config->allocation_unit_size;
|
||||
|
||||
msc_host_vfs_t *vfs = calloc(1, sizeof(msc_host_vfs_t));
|
||||
MSC_RETURN_ON_FALSE(vfs != NULL, ESP_ERR_NO_MEM);
|
||||
|
||||
MSC_GOTO_ON_ERROR( ff_diskio_get_drive(&pdrv) );
|
||||
|
||||
ff_diskio_register_msc(pdrv, &dev->disk);
|
||||
char drive[DRIVE_STR_LEN] = {(char)('0' + pdrv), ':', 0};
|
||||
diskio_registered = true;
|
||||
|
||||
strncpy(vfs->drive, drive, DRIVE_STR_LEN);
|
||||
MSC_GOTO_ON_FALSE( vfs->base_path = strdup(base_path), ESP_ERR_NO_MEM );
|
||||
vfs->pdrv = pdrv;
|
||||
|
||||
MSC_GOTO_ON_ERROR( esp_vfs_fat_register(base_path, drive, mount_config->max_files, &fs) );
|
||||
|
||||
FRESULT fresult = f_mount(fs, drive, 1);
|
||||
|
||||
if ( fresult != FR_OK) {
|
||||
if (mount_config->format_if_mount_failed &&
|
||||
(fresult == FR_NO_FILESYSTEM || fresult == FR_INT_ERR)) {
|
||||
MSC_GOTO_ON_ERROR( msc_format_storage(block_size, alloc_size, drive) );
|
||||
MSC_GOTO_ON_FALSE( f_mount(fs, drive, 0) == FR_OK, ESP_ERR_MSC_MOUNT_FAILED );
|
||||
} else {
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
|
||||
*vfs_handle = vfs;
|
||||
return ESP_OK;
|
||||
|
||||
fail:
|
||||
if (diskio_registered) {
|
||||
ff_diskio_unregister(pdrv);
|
||||
}
|
||||
esp_vfs_fat_unregister_path(base_path);
|
||||
if (fs) {
|
||||
f_mount(NULL, drive, 0);
|
||||
}
|
||||
dealloc_msc_vfs(vfs);
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t msc_host_vfs_unregister(msc_host_vfs_handle_t vfs_handle)
|
||||
{
|
||||
MSC_RETURN_ON_INVALID_ARG(vfs_handle);
|
||||
msc_host_vfs_t *vfs = (msc_host_vfs_t *)vfs_handle;
|
||||
|
||||
f_mount(NULL, vfs->drive, 0);
|
||||
ff_diskio_unregister(vfs->pdrv);
|
||||
esp_vfs_fat_unregister_path(vfs->base_path);
|
||||
dealloc_msc_vfs(vfs);
|
||||
return ESP_OK;
|
||||
}
|
489
managed_components/espressif__usb_host_msc/src/msc_scsi_bot.c
Normal file
489
managed_components/espressif__usb_host_msc/src/msc_scsi_bot.c
Normal file
|
@ -0,0 +1,489 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include "esp_log.h"
|
||||
#include "inttypes.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include "esp_check.h"
|
||||
#include "esp_log.h"
|
||||
#include "msc_common.h"
|
||||
#include "msc_scsi_bot.h"
|
||||
#include "usb/msc_host.h"
|
||||
|
||||
static const char *TAG = "USB_MSC_SCSI";
|
||||
|
||||
/* --------------------------- SCSI Definitions ----------------------------- */
|
||||
#define CMD_SENSE_VALID_BIT (1 << 7)
|
||||
#define SCSI_FLAG_DPO (1<<4)
|
||||
#define SCSI_FLAG_FUA (1<<3)
|
||||
|
||||
#define SCSI_CMD_FORMAT_UNIT 0x04
|
||||
#define SCSI_CMD_INQUIRY 0x12
|
||||
#define SCSI_CMD_MODE_SELECT 0x55
|
||||
#define SCSI_CMD_MODE_SENSE 0x5A
|
||||
#define SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1E
|
||||
#define SCSI_CMD_READ10 0x28
|
||||
#define SCSI_CMD_READ12 0xA8
|
||||
#define SCSI_CMD_READ_CAPACITY 0x25
|
||||
#define SCSI_CMD_READ_FORMAT_CAPACITIES 0x23
|
||||
#define SCSI_CMD_REQUEST_SENSE 0x03
|
||||
#define SCSI_CMD_REZERO 0x01
|
||||
#define SCSI_CMD_SEEK10 0x2B
|
||||
#define SCSI_CMD_SEND_DIAGNOSTIC 0x1D
|
||||
#define SCSI_CMD_START_STOP Unit 0x1B
|
||||
#define SCSI_CMD_TEST_UNIT_READY 0x00
|
||||
#define SCSI_CMD_VERIFY 0x2F
|
||||
#define SCSI_CMD_WRITE10 0x2A
|
||||
#define SCSI_CMD_WRITE12 0xAA
|
||||
#define SCSI_CMD_WRITE_AND_VERIFY 0x2E
|
||||
|
||||
#define IN_DIR CWB_FLAG_DIRECTION_IN
|
||||
#define OUT_DIR 0
|
||||
|
||||
#define INQUIRY_VID_SIZE 8
|
||||
#define INQUIRY_PID_SIZE 16
|
||||
#define INQUIRY_REV_SIZE 4
|
||||
|
||||
#define CBW_CMD_SIZE(cmd) (sizeof(cmd) - sizeof(msc_cbw_t))
|
||||
|
||||
#define CBW_BASE_INIT(dir, cbw_len, data_len) \
|
||||
.base = { \
|
||||
.signature = 0x43425355, \
|
||||
.tag = ++cbw_tag, \
|
||||
.flags = dir, \
|
||||
.lun = 0, \
|
||||
.data_length = data_len, \
|
||||
.cbw_length = cbw_len, \
|
||||
}
|
||||
|
||||
#define CSW_SIGNATURE 0x53425355
|
||||
#define CBW_SIZE 31
|
||||
|
||||
#define CWB_FLAG_DIRECTION_IN (1<<7) // device -> host
|
||||
|
||||
/**
|
||||
* @brief LUT with error codes and descriptions
|
||||
*
|
||||
* @see USB Mass Storage Class – UFI Command Specification, Revision 1.0
|
||||
* Table 51 - Sense Keys, ASC/ASCQ Listing for All Commands (sorted by Key)
|
||||
*
|
||||
*/
|
||||
typedef struct {
|
||||
uint8_t sense_key;
|
||||
uint8_t asc;
|
||||
uint8_t ascq;
|
||||
const char *description;
|
||||
} sense_errors_t;
|
||||
|
||||
const sense_errors_t sense_errors_lut[] = {
|
||||
{0x00, 0x00, 0x00, "NO SENSE"},
|
||||
{0x07, 0x27, 0x00, "WRITE PROTECTED MEDIA"},
|
||||
|
||||
// add more items as needed
|
||||
};
|
||||
|
||||
#define SENSE_ERROR_COUNT (sizeof(sense_errors_lut) / sizeof(sense_errors_t))
|
||||
|
||||
/**
|
||||
* @brief Command Block Wrapper structure
|
||||
*
|
||||
* @see USB Mass Storage Class – Bulk Only Transport, Table 5.1
|
||||
*/
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
uint32_t signature;
|
||||
uint32_t tag;
|
||||
uint32_t data_length;
|
||||
uint8_t flags;
|
||||
uint8_t lun;
|
||||
uint8_t cbw_length;
|
||||
} msc_cbw_t;
|
||||
|
||||
/**
|
||||
* @brief Command Status Wrapper structure
|
||||
*
|
||||
* @see USB Mass Storage Class – Bulk Only Transport, Table 5.2
|
||||
*/
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
uint32_t signature;
|
||||
uint32_t tag;
|
||||
uint32_t dataResidue;
|
||||
uint8_t status;
|
||||
} msc_csw_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
msc_cbw_t base;
|
||||
uint8_t opcode;
|
||||
uint8_t flags;
|
||||
uint32_t address;
|
||||
uint8_t reserved1;
|
||||
uint16_t length;
|
||||
uint8_t reserved2[3];
|
||||
} cbw_read10_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
msc_cbw_t base;
|
||||
uint8_t opcode;
|
||||
uint8_t flags;
|
||||
uint32_t address;
|
||||
uint8_t reserved1;
|
||||
uint16_t length;
|
||||
uint8_t reserved2[1];
|
||||
} cbw_write10_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
msc_cbw_t base;
|
||||
uint8_t opcode;
|
||||
uint8_t flags;
|
||||
uint32_t address;
|
||||
uint8_t reserved[6];
|
||||
} cbw_read_capacity_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
uint32_t block_count;
|
||||
uint32_t block_size;
|
||||
} cbw_read_capacity_response_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
msc_cbw_t base;
|
||||
uint8_t opcode;
|
||||
uint8_t flags;
|
||||
uint8_t reserved[10];
|
||||
} cbw_unit_ready_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
msc_cbw_t base;
|
||||
uint8_t opcode;
|
||||
uint8_t flags;
|
||||
uint8_t reserved_0[2];
|
||||
uint8_t allocation_length;
|
||||
uint8_t reserved_1[7];
|
||||
} cbw_sense_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
uint8_t error_code;
|
||||
uint8_t reserved_0;
|
||||
uint8_t sense_key;
|
||||
uint32_t info;
|
||||
uint8_t sense_len;
|
||||
uint32_t reserved_1;
|
||||
uint8_t sense_code;
|
||||
uint8_t sense_code_qualifier;
|
||||
uint32_t reserved_2;
|
||||
} cbw_sense_response_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
msc_cbw_t base;
|
||||
uint8_t opcode;
|
||||
uint8_t flags;
|
||||
uint8_t page_code;
|
||||
uint8_t reserved_0;
|
||||
uint8_t allocation_length;
|
||||
uint8_t reserved_1[7];
|
||||
} cbw_inquiry_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
msc_cbw_t base;
|
||||
uint8_t opcode;
|
||||
uint8_t flags;
|
||||
uint8_t pc_page_code;
|
||||
uint8_t reserved_1[4];
|
||||
uint16_t parameter_list_length;
|
||||
uint8_t reserved_2[3];
|
||||
} mode_sense_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
uint8_t data[8];
|
||||
} mode_sense_response_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
msc_cbw_t base;
|
||||
uint8_t opcode;
|
||||
uint8_t flags;
|
||||
uint8_t reserved_1[2];
|
||||
uint8_t prevent;
|
||||
uint8_t reserved_2[7];
|
||||
} prevent_allow_medium_removal_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
uint8_t data[36];
|
||||
} cbw_inquiry_response_t;
|
||||
|
||||
// Unique number based on which MSC protocol pairs request and response
|
||||
static uint32_t cbw_tag;
|
||||
|
||||
static esp_err_t check_csw(msc_csw_t *csw, uint32_t tag)
|
||||
{
|
||||
const bool csw_ok = csw->signature == CSW_SIGNATURE && csw->tag == tag &&
|
||||
csw->dataResidue == 0 && csw->status == 0;
|
||||
|
||||
if (!csw_ok) {
|
||||
ESP_LOGV(TAG, "CSW failed: dCSWSignature = 0x%02"PRIx32", dCSWTag = 0x%02"PRIx32", dCSWDataResidue = 0x%02"PRIx32"",
|
||||
csw->signature, csw->tag, csw->dataResidue);
|
||||
ESP_LOGD(TAG, "CSW failed: bCSWStatus 0x%02"PRIx8"", csw->status);
|
||||
}
|
||||
|
||||
return csw_ok ? ESP_OK : ESP_FAIL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Execute BOT command
|
||||
*
|
||||
* There are multiple stages in BOT command:
|
||||
* 1. Command transport
|
||||
* 2. Data transport (optional)
|
||||
* 3. Status transport
|
||||
* 3.1. Error recovery (in case of error)
|
||||
*
|
||||
* This function is not 'static' so it could be called from unit test
|
||||
*
|
||||
* @see USB Mass Storage Class – Bulk Only Transport, Chapter 5.3
|
||||
*
|
||||
* @param[in] device MSC device handle
|
||||
* @param[in] cbw Command Block Wrapper
|
||||
* @param[in] data Data (optional)
|
||||
* @param[in] size Size of data in bytes
|
||||
* @return esp_err_t
|
||||
*/
|
||||
esp_err_t bot_execute_command(msc_device_t *device, msc_cbw_t *cbw, void *data, size_t size)
|
||||
{
|
||||
msc_csw_t csw;
|
||||
msc_endpoint_t ep = (cbw->flags & CWB_FLAG_DIRECTION_IN) ? MSC_EP_IN : MSC_EP_OUT;
|
||||
|
||||
// 1. Command transport
|
||||
MSC_RETURN_ON_ERROR( msc_bulk_transfer(device, (uint8_t *)cbw, CBW_SIZE, MSC_EP_OUT) );
|
||||
|
||||
// 2. Optional data transport
|
||||
if (data) {
|
||||
MSC_RETURN_ON_ERROR( msc_bulk_transfer(device, (uint8_t *)data, size, ep) );
|
||||
}
|
||||
|
||||
// 3. Status transport
|
||||
esp_err_t err = msc_bulk_transfer(device, (uint8_t *)&csw, sizeof(msc_csw_t), MSC_EP_IN);
|
||||
|
||||
// 3.1 Error recovery
|
||||
if (err == ESP_ERR_MSC_STALL) {
|
||||
// In case of the status transport failure, we can try reading the status again after clearing feature
|
||||
ESP_RETURN_ON_ERROR( clear_feature(device, device->config.bulk_in_ep), TAG, "Clear feature failed" );
|
||||
err = msc_bulk_transfer(device, (uint8_t *)&csw, sizeof(msc_csw_t), MSC_EP_IN);
|
||||
if (ESP_OK != err) {
|
||||
// In case the repeated status transport failed we do reset recovery
|
||||
// We don't check the error code here, the command has already failed.
|
||||
msc_host_reset_recovery(device);
|
||||
}
|
||||
}
|
||||
|
||||
MSC_RETURN_ON_ERROR(err);
|
||||
|
||||
return check_csw(&csw, cbw->tag);
|
||||
}
|
||||
|
||||
static const char *decode_sense_keys(cbw_sense_response_t *sense_response)
|
||||
{
|
||||
// Only decode WRITE_PROTECTED_MEDIA sense key, other keys are not implemented
|
||||
for (int i = 0; i < SENSE_ERROR_COUNT; i++) {
|
||||
if (sense_errors_lut[i].sense_key == sense_response->sense_key &&
|
||||
sense_errors_lut[i].asc == sense_response->sense_code &&
|
||||
sense_errors_lut[i].ascq == sense_response->sense_code_qualifier) {
|
||||
return sense_errors_lut[i].description;
|
||||
}
|
||||
}
|
||||
|
||||
return "not found, refer to USB Mass Storage Class – UFI Command Specification (Table 51)";
|
||||
}
|
||||
|
||||
|
||||
esp_err_t scsi_cmd_read10(msc_host_device_handle_t dev,
|
||||
uint8_t *data,
|
||||
uint32_t sector_address,
|
||||
uint32_t num_sectors,
|
||||
uint32_t sector_size)
|
||||
{
|
||||
msc_device_t *device = (msc_device_t *)dev;
|
||||
cbw_read10_t cbw = {
|
||||
CBW_BASE_INIT(IN_DIR, CBW_CMD_SIZE(cbw_read10_t), num_sectors * sector_size),
|
||||
.opcode = SCSI_CMD_READ10,
|
||||
.flags = 0, // lun
|
||||
.address = __builtin_bswap32(sector_address),
|
||||
.length = __builtin_bswap16(num_sectors),
|
||||
};
|
||||
|
||||
esp_err_t ret = bot_execute_command(device, &cbw.base, data, num_sectors * sector_size);
|
||||
|
||||
// In case of an error, get an error code
|
||||
if (unlikely(ret != ESP_OK)) {
|
||||
MSC_RETURN_ON_ERROR( scsi_cmd_sense(device, NULL));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t scsi_cmd_write10(msc_host_device_handle_t dev,
|
||||
const uint8_t *data,
|
||||
uint32_t sector_address,
|
||||
uint32_t num_sectors,
|
||||
uint32_t sector_size)
|
||||
{
|
||||
msc_device_t *device = (msc_device_t *)dev;
|
||||
cbw_write10_t cbw = {
|
||||
CBW_BASE_INIT(OUT_DIR, CBW_CMD_SIZE(cbw_write10_t), num_sectors * sector_size),
|
||||
.opcode = SCSI_CMD_WRITE10,
|
||||
.address = __builtin_bswap32(sector_address),
|
||||
.length = __builtin_bswap16(num_sectors),
|
||||
};
|
||||
|
||||
esp_err_t ret = bot_execute_command(device, &cbw.base, (void *)data, num_sectors * sector_size);
|
||||
|
||||
// In case of an error, get an error code
|
||||
if (unlikely(ret != ESP_OK)) {
|
||||
MSC_RETURN_ON_ERROR( scsi_cmd_sense(device, NULL));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t scsi_cmd_read_capacity(msc_host_device_handle_t dev, uint32_t *block_size, uint32_t *block_count)
|
||||
{
|
||||
msc_device_t *device = (msc_device_t *)dev;
|
||||
cbw_read_capacity_response_t response;
|
||||
|
||||
cbw_read_capacity_t cbw = {
|
||||
CBW_BASE_INIT(IN_DIR, CBW_CMD_SIZE(cbw_read_capacity_t), sizeof(response)),
|
||||
.opcode = SCSI_CMD_READ_CAPACITY,
|
||||
};
|
||||
|
||||
esp_err_t ret = bot_execute_command(device, &cbw.base, &response, sizeof(response));
|
||||
|
||||
// In case of an error, get an error code
|
||||
if (unlikely(ret != ESP_OK)) {
|
||||
MSC_RETURN_ON_ERROR( scsi_cmd_sense(device, NULL));
|
||||
}
|
||||
|
||||
*block_count = __builtin_bswap32(response.block_count);
|
||||
*block_size = __builtin_bswap32(response.block_size);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t scsi_cmd_unit_ready(msc_host_device_handle_t dev)
|
||||
{
|
||||
msc_device_t *device = (msc_device_t *)dev;
|
||||
cbw_unit_ready_t cbw = {
|
||||
CBW_BASE_INIT(IN_DIR, CBW_CMD_SIZE(cbw_unit_ready_t), 0),
|
||||
.opcode = SCSI_CMD_TEST_UNIT_READY,
|
||||
};
|
||||
|
||||
esp_err_t ret = bot_execute_command(device, &cbw.base, NULL, 0);
|
||||
|
||||
// In case of an error, get an error code
|
||||
if (unlikely(ret != ESP_OK)) {
|
||||
MSC_RETURN_ON_ERROR( scsi_cmd_sense(device, NULL));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t scsi_cmd_sense(msc_host_device_handle_t dev, scsi_sense_data_t *sense)
|
||||
{
|
||||
msc_device_t *device = (msc_device_t *)dev;
|
||||
cbw_sense_response_t response;
|
||||
|
||||
cbw_sense_t cbw = {
|
||||
CBW_BASE_INIT(IN_DIR, CBW_CMD_SIZE(cbw_sense_t), sizeof(response)),
|
||||
.opcode = SCSI_CMD_REQUEST_SENSE,
|
||||
.allocation_length = sizeof(response),
|
||||
};
|
||||
|
||||
MSC_RETURN_ON_ERROR( bot_execute_command(device, &cbw.base, &response, sizeof(response)) );
|
||||
|
||||
if (sense == NULL) {
|
||||
ESP_LOGE(TAG, "Sense error codes: Sense Key 0x%02"PRIx8", ASC: 0x%02"PRIx8", ASCQ: 0x%02"PRIx8"",
|
||||
response.sense_key, response.sense_code, response.sense_code_qualifier);
|
||||
const char *error_description = decode_sense_keys(&response);
|
||||
ESP_LOGE(TAG, "Sense error description: %s", error_description);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
sense->key = response.sense_key;
|
||||
sense->code = response.sense_code;
|
||||
sense->code_q = response.sense_code_qualifier;
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t scsi_cmd_inquiry(msc_host_device_handle_t dev)
|
||||
{
|
||||
msc_device_t *device = (msc_device_t *)dev;
|
||||
cbw_inquiry_response_t response = { 0 };
|
||||
|
||||
cbw_inquiry_t cbw = {
|
||||
CBW_BASE_INIT(IN_DIR, CBW_CMD_SIZE(cbw_inquiry_t), sizeof(response)),
|
||||
.opcode = SCSI_CMD_INQUIRY,
|
||||
.allocation_length = sizeof(response),
|
||||
};
|
||||
|
||||
esp_err_t ret = bot_execute_command(device, &cbw.base, &response, sizeof(response) );
|
||||
|
||||
// In case of an error, get an error code
|
||||
if (unlikely(ret != ESP_OK)) {
|
||||
MSC_RETURN_ON_ERROR( scsi_cmd_sense(device, NULL));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t scsi_cmd_mode_sense(msc_host_device_handle_t dev)
|
||||
{
|
||||
msc_device_t *device = (msc_device_t *)dev;
|
||||
mode_sense_response_t response = { 0 };
|
||||
|
||||
mode_sense_t cbw = {
|
||||
CBW_BASE_INIT(IN_DIR, CBW_CMD_SIZE(mode_sense_t), sizeof(response)),
|
||||
.opcode = SCSI_CMD_MODE_SENSE,
|
||||
.pc_page_code = 0x3F,
|
||||
.parameter_list_length = sizeof(response),
|
||||
};
|
||||
|
||||
esp_err_t ret = bot_execute_command(device, &cbw.base, &response, sizeof(response) );
|
||||
|
||||
// In case of an error, get an error code
|
||||
if (unlikely(ret != ESP_OK)) {
|
||||
MSC_RETURN_ON_ERROR( scsi_cmd_sense(device, NULL));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t scsi_cmd_prevent_removal(msc_host_device_handle_t dev, bool prevent)
|
||||
{
|
||||
msc_device_t *device = (msc_device_t *)dev;
|
||||
prevent_allow_medium_removal_t cbw = {
|
||||
CBW_BASE_INIT(OUT_DIR, CBW_CMD_SIZE(prevent_allow_medium_removal_t), 0),
|
||||
.opcode = SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL,
|
||||
.prevent = (uint8_t) prevent,
|
||||
};
|
||||
|
||||
esp_err_t ret = bot_execute_command(device, &cbw.base, NULL, 0);
|
||||
|
||||
// In case of an error, get an error code
|
||||
if (unlikely(ret != ESP_OK)) {
|
||||
MSC_RETURN_ON_ERROR( scsi_cmd_sense(device, NULL));
|
||||
}
|
||||
return ret;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue