00001 /* ---------------------------------------------------------------------------- 00002 * SAM Software Package License 00003 * ---------------------------------------------------------------------------- 00004 * Copyright (c) 2014, Atmel Corporation 00005 * 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions are met: 00010 * 00011 * - Redistributions of source code must retain the above copyright notice, 00012 * this list of conditions and the disclaimer below. 00013 * 00014 * Atmel's name may not be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR 00018 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00019 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE 00020 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, 00021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00022 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 00023 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00024 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00025 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 00026 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 * ---------------------------------------------------------------------------- 00028 */ 00029 00030 /** \file 00031 * \section Purpose 00032 * 00033 * Logical Unit Number (LUN) used by the Mass Storage driver and the SCSI 00034 * protocol. Represents a logical hard-drive. 00035 * 00036 * \section Usage 00037 * -# Initialize Memory related pins (see pio & board.h). 00038 * -# Initialize a Media instance for the LUN (see memories). 00039 * -# Initialize the LUN with LUN_Init, and link to the initialized Media. 00040 * -# To read data from the LUN linked media, uses LUN_Read. 00041 * -# To write data to the LUN linked media, uses LUN_Write. 00042 * -# To unlink the media, uses LUN_Eject. 00043 */ 00044 00045 #ifndef MSDLUN_H 00046 #define MSDLUN_H 00047 00048 /** \addtogroup usbd_msd 00049 *@{ 00050 */ 00051 00052 /*------------------------------------------------------------------------------ 00053 * Headers 00054 *------------------------------------------------------------------------------*/ 00055 00056 #include <stdint.h> 00057 00058 #include "SBC.h" 00059 #include "MSDIOFifo.h" 00060 #include "USBD.h" 00061 00062 /*------------------------------------------------------------------------------ 00063 * External Media Definitions 00064 *------------------------------------------------------------------------------*/ 00065 #if !defined(_MEDIA_) 00066 #define MED_STATUS_SUCCESS 0x00 00067 #define MED_STATE_READY 0x00 /// Media is ready for access 00068 #define MED_STATE_BUSY 0x01 /// Media is busy 00069 00070 typedef void (*fMEDCallback)(void *pArg, 00071 uint8_t bStatus, 00072 uint32_t dwTransferred, 00073 uint32_t dwRemaining); 00074 00075 extern uint8_t MED_IsMappedRDSupported(void* pMed); 00076 extern uint8_t MED_IsMappedWRSupported(void* pMed); 00077 extern uint32_t MED_GetMappedAddress(void* pMed, uint32_t dwBlk); 00078 00079 extern uint8_t MED_IsBusy(void* pMed); 00080 extern uint8_t MED_IsProtected(void* pMed); 00081 extern uint8_t MED_GetState(void* pMed); 00082 extern uint32_t MED_GetBlockSize(void* pMed); 00083 extern uint32_t MED_GetSize(void* pMed); 00084 extern uint8_t MED_Write(void* pMed, 00085 uint32_t dwAddr, 00086 void* pData, 00087 uint32_t dwLen, 00088 fMEDCallback fCallback, 00089 void* pArg); 00090 extern uint8_t MED_Read(void* pMed, 00091 uint32_t dwAddr, 00092 void* pData, 00093 uint32_t dwLen, 00094 fMEDCallback fCallback, 00095 void* pArg); 00096 extern uint8_t MED_Flush(void* pMed); 00097 #endif 00098 /*------------------------------------------------------------------------------ 00099 * Definitions 00100 *------------------------------------------------------------------------------*/ 00101 00102 /** LUN RC: success */ 00103 #define LUN_STATUS_SUCCESS 0x00 00104 /** LUN RC: error */ 00105 #define LUN_STATUS_ERROR 0x02 00106 00107 /** Media of LUN is removed */ 00108 #define LUN_NOT_PRESENT 0x00 00109 /** LUN is ejected by host */ 00110 #define LUN_EJECTED 0x01 00111 /** Media of LUN is changed */ 00112 #define LUN_CHANGED 0x10 00113 /** LUN Not Ready to Ready transition */ 00114 #define LUN_TRANS_READY LUN_CHANGED 00115 /** Media of LUN is ready */ 00116 #define LUN_READY 0x11 00117 00118 /*------------------------------------------------------------------------------ 00119 * Types 00120 *------------------------------------------------------------------------------*/ 00121 00122 /** Mass storage device data flow monitor function type 00123 * \param flowDirection 1 - device to host (READ10) 00124 * 0 - host to device (WRITE10) 00125 * \param dataLength Length of data transferred in bytes. 00126 * \param fifoNullCount Times that FIFO is NULL to wait 00127 * \param fifoFullCount Times that FIFO is filled to wait 00128 */ 00129 typedef void(*MSDLunDataMonitorFunction)(uint8_t flowDirection, 00130 uint32_t dataLength, 00131 uint32_t fifoNullCount, 00132 uint32_t fifoFullCount); 00133 00134 /*------------------------------------------------------------------------------ 00135 * Structures 00136 *------------------------------------------------------------------------------*/ 00137 00138 /** \brief LUN structure */ 00139 typedef struct { 00140 00141 /** Pointer to a SBCInquiryData instance. */ 00142 SBCInquiryData *inquiryData; 00143 /** Fifo for USB transfer, must be assigned. */ 00144 MSDIOFifo ioFifo; 00145 /** Pointer to Media instance for the LUN. */ 00146 void *media; 00147 /** Pointer to a Monitor Function to analyse the flow of LUN. 00148 * \param flowDirection 1 - device to host (READ10) 00149 * 0 - host to device (WRITE10) 00150 * \param dataLength Length of data transferred in bytes. 00151 * \param fifoNullCount Times that FIFO is NULL to wait 00152 * \param fifoFullCount Times that FIFO is filled to wait 00153 */ 00154 void (*dataMonitor)(uint8_t flowDirection, 00155 uint32_t dataLength, 00156 uint32_t fifoNullCount, 00157 uint32_t fifoFullCount); 00158 /** The start position of the media (blocks) allocated to the LUN. */ 00159 uint32_t baseAddress; 00160 /** The size of the media (blocks) allocated to the LUN. */ 00161 uint32_t size; 00162 /** Sector size of the media in number of media blocks */ 00163 uint16_t blockSize; 00164 /** The LUN can be read-only even the media is writeable */ 00165 uint8_t protected; 00166 /** The LUN status (Ejected/Changed/) */ 00167 uint8_t status; 00168 00169 /** Data for the RequestSense command. */ 00170 SBCRequestSenseData requestSenseData; 00171 /** Data for the ReadCapacity command. */ 00172 SBCReadCapacity10Data readCapacityData; 00173 00174 } MSDLun; 00175 00176 /*------------------------------------------------------------------------------ 00177 * Exported functions 00178 *------------------------------------------------------------------------------*/ 00179 extern void LUN_Init(MSDLun *lun, 00180 void *media, 00181 uint8_t *ioBuffer, 00182 uint32_t ioBufferSize, 00183 uint32_t baseAddress, 00184 uint32_t size, 00185 uint16_t blockSize, 00186 uint8_t protected, 00187 void (*dataMonitor)(uint8_t flowDirection, 00188 uint32_t dataLength, 00189 uint32_t fifoNullCount, 00190 uint32_t fifoFullCount)); 00191 00192 extern uint32_t LUN_Eject(MSDLun *lun); 00193 00194 extern uint32_t LUN_Write(MSDLun *lun, 00195 uint32_t blockAddress, 00196 void *data, 00197 uint32_t length, 00198 TransferCallback callback, 00199 void *argument); 00200 00201 extern uint32_t LUN_Read(MSDLun *lun, 00202 uint32_t blockAddress, 00203 void *data, 00204 uint32_t length, 00205 TransferCallback callback, 00206 void *argument); 00207 00208 /**@}*/ 00209 00210 #endif /*#ifndef MSDLUN_H */ 00211