USB Mass Storage Class (MSC) Device

The USB device stack includes a mass storage interface for implementing the mass storage class. The interface handles all requests from the attached host and forwards all disk I/O operations to a specified disk driver.

Media Storage

The mass storage interface uses a disk driver for the physical storage of the file system data. A media disk driver is assigned to a logical unit when it is added to a mass storage interface. The mass storage interface does support multiple logical units (LUNs).

The media disk drivers provide status information about the physical disk, such as media presence and write protection, to the mass storage interface. The mass storage interface automatically provides this information to the host.

Basic Usage

The code snippet below shows how to initialize a USB device stack instance along with a mass storage class interface for a given media disk driver.

#include "USB/Device/usbd.h"
#include "USB/Device/usbd_msc.h"
#include <assert.h>

/* Define a block information transferred to an attached host */
const BYTE SCSIInfo[] = {

    0x00,                                           /* Direct-access block device */
    0x80,                                           /* Removable medium */
    0x00,                                           /* ISO, ECMA, and ANSI-approved versions */
    0x01,                                           /* AEC, TrmIOP, and Response data format */

    0x1F,                                           /* Additional length */
    0x00,
    0x00,
    0x00,

    'D', 'Z', 'X', ' ', ' ', ' ', ' ', ' ',         /* Vendor identification (ASCII) (bytes 8-15) */

    'S', 'D', ' ', 'D', 'I', 'S', 'K', ' ',         /* Product identification (ASCII) (bytes 16-31) */
    ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
	
    '1', '.', '0', ' '                              /* Product revision level (bytes 32-35) */
};

static USBDDEVICE usb;                              /* Allocate a USB device stack instance */
static MSCDINTERFACE massintf;                      /* Allocate a mass storage interface instance */
static MSCDLUN lun;                                 /* Allocate a mass storage logical unit (LUN) */


void APP_InitMSC(DISK* disk)
{
    STATUS status;

                            
    status = USBD_Create(&usb,                      /* Create and initialize the USB device stack */
                         0,                         /* Port number */
                         0x0000,                    /* Device vendor ID */
                         0x0001);                   /* Device product ID */
    assert(status == SUCCESS);

    status = USBD_SetDeviceName(&usb,               /* Assign text names to the usb device */
                                "DZX",              /* Manufacturer name */
                                "MSC Demo",         /* Product name */
                                "123456789ABCD",    /* Serial number */
    assert(status == SUCCESS);

    status = MSCD_CreateInterface(&usb,             /* Create and initialize the mass storage interface */
                                  &massintf,        /* A pointer to the mass storage interface */
                                  0x01);            /* Logical endpoint number used by the interface */
    assert(status == SUCCESS);

    status = MSCD_AddLUN(&massintf,                 /* Add a logical unit to the mass storage interface */
                         &lun,                      /* A pointer to the logical unit instance to be initialized */
                         &disk.base,                /* A pointer to the media storage disk to handle I/O operations */
                         SCSIInfo,                  /* A pointer to the SCSI info */
                         sizeof(SCSIInfo));         /* Size, in bytes, of the SCSI info */
    assert(status == SUCCESS);

    status = USBD_Open(&usb);                       /* Enable USB device communications */
    assert(status == SUCCESS);
}                   

API Reference

STATUS MSCD_CreateInterface(USBD* device, MSCDINTERFACE* intf, BYTE epnum)
Creates and initializes a Mass Storage Class (MSC) interface for a USB device.
PARAMETERS
device A pointer to a USB device that will receive the interface.
intf A pointer to a caller allocated mass storage interface to be initialized.
epnum The number of the endpoints to be created for the interface (must be non-zero).
RETURNS
SUCCESS The interface was created and initialized.
ERR_NULLREFERENCE The argument 'device' or 'intf' was found to be NULL.
ERR_INVALIDARGUMENT An invalid argument was specified.
STATUS MSCD_AddLUN(MSCDINTERFACE* intf, MSCDLUN* lun, DISK* disk, const BYTE* info, UINT32 infolen)
Adds a logical unit to a mass storage interface.
PARAMETERS
intf A pointer to the mass storage interface to receive the logical unit.
lun A pointer to a caller allocated logical unit to be initialized and added to the specified interface.
disk A pointer to the media disk driver to be used by the logical unit.
info A pointer to the SCSI inquiry information for the logical unit.
infolen The size, in bytes, of the inquiry information.
RETURNS
SUCCESS The logical unit was initialized and added to the given interface.
ERR_NULLREFERENCE The argument 'intf', 'lun', 'disk' or 'info' was found to be NULL.
STATUS MSCD_RemoveLUN(MSCDINTERFACE* intf, MSCDLUN* lun)
Removes a logical unit from a mass storage interface.
PARAMETERS
intf A pointer to the mass storage interface that contains the logical unit.
lun A pointer to the logical unit to be removed.
RETURNS
SUCCESS The logical unit was removed from the interface.
ERR_NULLREFERENCE The argument 'intf' or 'lun' was found to be NULL.
STATUS MSCD_SetEnabled(MSCDLUN* lun, BOOLEAN enabled)
Sets the enable/disable state for a logical unit within a mass storage interface.
PARAMETERS
lun A pointer to the logical unit to be enabled or disabled.
enabled The new enabled state for the logical unit.
RETURNS
SUCCESS The enabled state for the logical unit has been updated.
ERR_NULLREFERENCE The argument 'lun' was found to be NULL.
REMARKS
A disabled logical unit will appear as media not present to a host
BOOLEAN MSCD_Enabled(MSCDLUN* lun)
Returns the enable/disable state for a logical unit within a mass storage interface.
PARAMETERS
lun A pointer to the target logical unit.
RETURNS
TRUE if the logical unit is currently enabled; otherwise FALSE.