refactor usb class structure

This commit is contained in:
sakumisu
2021-11-26 23:41:59 +08:00
parent 89f8d74fb8
commit 636b30c3d8
35 changed files with 3552 additions and 2415 deletions

121
class/msc/usb_msc.h Normal file
View File

@@ -0,0 +1,121 @@
/**
* @file
* @brief USB Mass Storage Class public header
*
* Header follows the Mass Storage Class Specification
* (Mass_Storage_Specification_Overview_v1.4_2-19-2010.pdf) and
* Mass Storage Class Bulk-Only Transport Specification
* (usbmassbulk_10.pdf).
* Header is limited to Bulk-Only Transfer protocol.
*/
#ifndef _USB_MSC_H__
#define _USB_MSC_H__
/* MSC Subclass Codes */
#define MSC_SUBCLASS_RBC 0x01 /* Reduced block commands (e.g., flash devices) */
#define MSC_SUBCLASS_SFF8020I_MMC2 0x02 /* SFF-8020i/MMC-2 (ATAPI) (e.g., C/DVD) */
#define MSC_SUBCLASS_QIC157 0x03 /* QIC-157 (e.g., tape device) */
#define MSC_SUBCLASS_UFI 0x04 /* e.g. floppy device */
#define MSC_SUBCLASS_SFF8070I 0x05 /* SFF-8070i (e.g. floppy disk) */
#define MSC_SUBCLASS_SCSI 0x06 /* SCSI transparent */
/* MSC Protocol Codes */
#define MSC_PROTOCOL_CBI_INT 0x00 /* CBI transport with command completion interrupt */
#define MSC_PROTOCOL_CBI_NOINT 0x01 /* CBI transport without command completion interrupt */
#define MSC_PROTOCOL_BULK_ONLY 0x50 /* Bulk only transport */
/* MSC Request Codes */
#define MSC_REQUEST_RESET 0xFF
#define MSC_REQUEST_GET_MAX_LUN 0xFE
/** MSC Command Block Wrapper (CBW) Signature */
#define MSC_CBW_Signature 0x43425355
/** Bulk-only Command Status Wrapper (CSW) Signature */
#define MSC_CSW_Signature 0x53425355
/** MSC Command Block Status Values */
#define CSW_STATUS_CMD_PASSED 0x00
#define CSW_STATUS_CMD_FAILED 0x01
#define CSW_STATUS_PHASE_ERROR 0x02
#define MSC_MAX_CDB_LEN (16) /* Max length of SCSI Command Data Block */
/** MSC Bulk-Only Command Block Wrapper (CBW) */
struct CBW {
uint32_t dSignature; /* 'USBC' = 0x43425355 */
uint32_t dTag; /* Depends on command id */
uint32_t dDataLength; /* Number of bytes that host expects to transfer */
uint8_t bmFlags; /* Bit 7: Direction=IN (other obsolete or reserved) */
uint8_t bLUN; /* LUN (normally 0) */
uint8_t bCBLength; /* len of cdb[] */
uint8_t CB[MSC_MAX_CDB_LEN]; /* Command Data Block */
} __PACKED;
#define USB_SIZEOF_MSC_CBW 31
/** MSC Bulk-Only Command Status Wrapper (CSW) */
struct CSW {
uint32_t dSignature; /* 'USBS' = 0x53425355 */
uint32_t dTag; /* Same tag as original command */
uint32_t dDataResidue; /* Amount not transferred */
uint8_t bStatus; /* Status of transfer */
} __PACKED;
#define USB_SIZEOF_MSC_CSW 13
/*Length of template descriptor: 23 bytes*/
#define MSC_DESCRIPTOR_LEN (9 + 7 + 7)
// clang-format off
#ifndef CONFIG_USB_HS
#define MSC_DESCRIPTOR_INIT(bFirstInterface, out_ep, in_ep,str_idx) \
/* Interface */ \
0x09, /* bLength */ \
USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */ \
bFirstInterface, /* bInterfaceNumber */ \
0x00, /* bAlternateSetting */ \
0x02, /* bNumEndpoints */ \
USB_DEVICE_CLASS_MASS_STORAGE, /* bInterfaceClass */ \
MSC_SUBCLASS_SCSI, /* bInterfaceSubClass */ \
MSC_PROTOCOL_BULK_ONLY, /* bInterfaceProtocol */ \
str_idx, /* iInterface */ \
0x07, /* bLength */ \
USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType */ \
out_ep, /* bEndpointAddress */ \
0x02, /* bmAttributes */ \
0x40, 0x00, /* wMaxPacketSize */ \
0x00, /* bInterval */ \
0x07, /* bLength */ \
USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType */ \
in_ep, /* bEndpointAddress */ \
0x02, /* bmAttributes */ \
0x40, 0x00, /* wMaxPacketSize */ \
0x00 /* bInterval */
#else
#define MSC_DESCRIPTOR_INIT(bFirstInterface, out_ep, in_ep,str_idx) \
/* Interface */ \
0x09, /* bLength */ \
USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */ \
bFirstInterface, /* bInterfaceNumber */ \
0x00, /* bAlternateSetting */ \
0x02, /* bNumEndpoints */ \
USB_DEVICE_CLASS_MASS_STORAGE, /* bInterfaceClass */ \
MSC_SUBCLASS_SCSI, /* bInterfaceSubClass */ \
MSC_PROTOCOL_BULK_ONLY, /* bInterfaceProtocol */ \
str_idx, /* iInterface */ \
0x07, /* bLength */ \
USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType */ \
out_ep, /* bEndpointAddress */ \
0x02, /* bmAttributes */ \
0x00, 0x02, /* wMaxPacketSize */ \
0x00, /* bInterval */ \
0x07, /* bLength */ \
USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType */ \
in_ep, /* bEndpointAddress */ \
0x02, /* bmAttributes */ \
0x00, 0x02, /* wMaxPacketSize */ \
0x00 /* bInterval */
#endif
// clang-format on
#endif /* USB_MSC_H_ */

View File

@@ -9,8 +9,8 @@
* Header is limited to Bulk-Only Transfer protocol.
*/
#ifndef _USBD_SCSI_H_
#define _USBD_SCSI_H_
#ifndef _USB_SCSI_H_
#define _USB_SCSI_H_
/* SCSI Commands */
#define SCSI_TEST_UNIT_READY 0x00
@@ -90,7 +90,7 @@
//--------------------------------------------------------------------+
/// SCSI Test Unit Ready Command
typedef struct __packed {
typedef struct __PACKED {
uint8_t cmd_code; ///< SCSI OpCode for \ref SCSI_CMD_TEST_UNIT_READY
uint8_t lun; ///< Logical Unit
uint8_t reserved[3];
@@ -98,7 +98,7 @@ typedef struct __packed {
} scsi_test_unit_ready_cmd_t;
/// SCSI Inquiry Command
typedef struct __packed {
typedef struct __PACKED {
uint8_t cmd_code; ///< SCSI OpCode for \ref SCSI_CMD_INQUIRY
uint8_t reserved1;
uint8_t page_code;
@@ -108,7 +108,7 @@ typedef struct __packed {
} scsi_inquiry_cmd_t, scsi_request_sense_cmd_t;
/// SCSI Inquiry Response Data
typedef struct __packed {
typedef struct __PACKED {
uint8_t peripheral_device_type : 5;
uint8_t peripheral_qualifier : 3;
@@ -150,7 +150,7 @@ typedef struct __packed {
uint8_t product_rev[4]; ///< 4 bytes of ASCII data defined by the vendor.
} scsi_inquiry_resp_t;
typedef struct __packed {
typedef struct __PACKED {
uint8_t response_code : 7; ///< 70h - current errors, Fixed Format 71h - deferred errors, Fixed Format
uint8_t valid : 1;
@@ -173,7 +173,7 @@ typedef struct __packed {
} scsi_sense_fixed_resp_t;
typedef struct __packed {
typedef struct __PACKED {
uint8_t cmd_code; ///< SCSI OpCode for \ref SCSI_CMD_MODE_SENSE_6
uint8_t : 3;
@@ -189,7 +189,7 @@ typedef struct __packed {
} scsi_mode_sense6_cmd_t;
// This is only a Mode parameter header(6).
typedef struct __packed {
typedef struct __PACKED {
uint8_t data_len;
uint8_t medium_type;
@@ -240,14 +240,14 @@ typedef struct
uint8_t block_desc_length[2];
} scsi_mode_10_resp_t;
typedef struct __packed {
typedef struct __PACKED {
uint8_t cmd_code; ///< SCSI OpCode for \ref SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL
uint8_t reserved[3];
uint8_t prohibit_removal;
uint8_t control;
} scsi_prevent_allow_medium_removal_t;
typedef struct __packed {
typedef struct __PACKED {
uint8_t cmd_code;
uint8_t immded : 1;
@@ -271,14 +271,14 @@ typedef struct __packed {
// SCSI MMC
//--------------------------------------------------------------------+
/// SCSI Read Format Capacity: Write Capacity
typedef struct __packed {
typedef struct __PACKED {
uint8_t cmd_code;
uint8_t reserved[6];
uint16_t alloc_length;
uint8_t control;
} scsi_read_format_capacity_cmd_t;
typedef struct __packed {
typedef struct __PACKED {
uint8_t reserved[3];
uint8_t list_length; /// must be 8*n, length in bytes of formattable capacity descriptor followed it.
@@ -296,7 +296,7 @@ typedef struct __packed {
//--------------------------------------------------------------------+
/// SCSI Read Capacity 10 Command: Read Capacity
typedef struct __packed {
typedef struct __PACKED {
uint8_t cmd_code; ///< SCSI OpCode for \ref SCSI_CMD_READ_CAPACITY_10
uint8_t reserved1;
uint32_t lba; ///< The first Logical Block Address (LBA) accessed by this command
@@ -313,7 +313,7 @@ typedef struct
} scsi_read_capacity10_resp_t;
/// SCSI Read 10 Command
typedef struct __packed {
typedef struct __PACKED {
uint8_t cmd_code; ///< SCSI OpCode
uint8_t reserved; // has LUN according to wiki
uint32_t lba; ///< The first Logical Block Address (LBA) accessed by this command
@@ -322,4 +322,4 @@ typedef struct __packed {
uint8_t control;
} scsi_read10_t, scsi_write10_t, scsi_read_write_10_t;
#endif /* ZEPHYR_INCLUDE_USB_CLASS_USB_CDC_H_ */
#endif /* USB_SCSI_H */

View File

@@ -21,8 +21,8 @@
*
*/
#include "usbd_core.h"
#include "usbd_scsi.h"
#include "usbd_msc.h"
#include "usb_scsi.h"
/* max USB packet size */
#ifndef CONFIG_USB_HS
@@ -46,25 +46,6 @@ enum Stage {
MSC_WAIT_CSW = 4, /* Command Status Wrapper */
};
/** MSC Bulk-Only Command Block Wrapper (CBW) */
struct CBW {
uint32_t dSignature;
uint32_t dTag;
uint32_t dDataLength;
uint8_t bmFlags;
uint8_t bLUN;
uint8_t bCBLength;
uint8_t CB[16];
} __packed;
/** MSC Bulk-Only Command Status Wrapper (CSW) */
struct CSW {
uint32_t dSignature;
uint32_t dTag;
uint32_t dDataResidue;
uint8_t bStatus;
} __packed;
/* Device data structure */
struct usbd_msc_cfg_private {
/* state of the bulk-only state machine */
@@ -970,7 +951,7 @@ static void mass_storage_bulk_in(uint8_t ep)
void msc_storage_notify_handler(uint8_t event, void *arg)
{
switch (event) {
case USB_EVENT_RESET:
case USBD_EVENT_RESET:
usbd_msc_reset();
break;
@@ -1001,4 +982,4 @@ void usbd_msc_class_init(uint8_t out_ep, uint8_t in_ep)
usbd_interface_add_endpoint(&msc_intf, &mass_ep_data[0]);
usbd_interface_add_endpoint(&msc_intf, &mass_ep_data[1]);
}
}

View File

@@ -12,91 +12,12 @@
#ifndef _USBD_MSC_H__
#define _USBD_MSC_H__
#include "usb_msc.h"
#ifdef __cplusplus
extern "C" {
#endif
/* MSC Subclass Codes */
#define MSC_SUBCLASS_RBC 0x01
#define MSC_SUBCLASS_SFF8020I_MMC2 0x02
#define MSC_SUBCLASS_QIC157 0x03
#define MSC_SUBCLASS_UFI 0x04
#define MSC_SUBCLASS_SFF8070I 0x05
#define MSC_SUBCLASS_SCSI 0x06
/* MSC Protocol Codes */
#define MSC_PROTOCOL_CBI_INT 0x00
#define MSC_PROTOCOL_CBI_NOINT 0x01
#define MSC_PROTOCOL_BULK_ONLY 0x50
/* MSC Request Codes */
#define MSC_REQUEST_RESET 0xFF
#define MSC_REQUEST_GET_MAX_LUN 0xFE
/** MSC Command Block Wrapper (CBW) Signature */
#define MSC_CBW_Signature 0x43425355
/** Bulk-only Command Status Wrapper (CSW) Signature */
#define MSC_CSW_Signature 0x53425355
/** MSC Command Block Status Values */
#define CSW_STATUS_CMD_PASSED 0x00
#define CSW_STATUS_CMD_FAILED 0x01
#define CSW_STATUS_PHASE_ERROR 0x02
/*Length of template descriptor: 23 bytes*/
#define MSC_DESCRIPTOR_LEN (9 + 7 + 7)
// clang-format off
#ifndef SUPPORT_USB_HS
#define MSC_DESCRIPTOR_INIT(bFirstInterface, out_ep, in_ep,str_idx) \
/* Interface */ \
0x09, /* bLength */ \
USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */ \
bFirstInterface, /* bInterfaceNumber */ \
0x00, /* bAlternateSetting */ \
0x02, /* bNumEndpoints */ \
USB_DEVICE_CLASS_MASS_STORAGE, /* bInterfaceClass */ \
MSC_SUBCLASS_SCSI, /* bInterfaceSubClass */ \
MSC_PROTOCOL_BULK_ONLY, /* bInterfaceProtocol */ \
str_idx, /* iInterface */ \
0x07, /* bLength */ \
USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType */ \
out_ep, /* bEndpointAddress */ \
0x02, /* bmAttributes */ \
0x40, 0x00, /* wMaxPacketSize */ \
0x00, /* bInterval */ \
0x07, /* bLength */ \
USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType */ \
in_ep, /* bEndpointAddress */ \
0x02, /* bmAttributes */ \
0x40, 0x00, /* wMaxPacketSize */ \
0x00 /* bInterval */
#else
#define MSC_DESCRIPTOR_INIT(bFirstInterface, out_ep, in_ep,str_idx) \
/* Interface */ \
0x09, /* bLength */ \
USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */ \
bFirstInterface, /* bInterfaceNumber */ \
0x00, /* bAlternateSetting */ \
0x02, /* bNumEndpoints */ \
USB_DEVICE_CLASS_MASS_STORAGE, /* bInterfaceClass */ \
MSC_SUBCLASS_SCSI, /* bInterfaceSubClass */ \
MSC_PROTOCOL_BULK_ONLY, /* bInterfaceProtocol */ \
str_idx, /* iInterface */ \
0x07, /* bLength */ \
USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType */ \
out_ep, /* bEndpointAddress */ \
0x02, /* bmAttributes */ \
0x00, 0x02, /* wMaxPacketSize */ \
0x00, /* bInterval */ \
0x07, /* bLength */ \
USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType */ \
in_ep, /* bEndpointAddress */ \
0x02, /* bmAttributes */ \
0x00, 0x02, /* wMaxPacketSize */ \
0x00 /* bInterval */
#endif
// clang-format on
void usbd_msc_class_init(uint8_t out_ep, uint8_t in_ep);
void usbd_msc_get_cap(uint8_t lun, uint32_t *block_num, uint16_t *block_size);
int usbd_msc_sector_read(uint32_t sector, uint8_t *buffer, uint32_t length);
@@ -106,4 +27,4 @@ int usbd_msc_sector_write(uint32_t sector, uint8_t *buffer, uint32_t length);
}
#endif
#endif /* USB_MSC_H_ */
#endif /* USBD_MSC_H_ */