summaryrefslogtreecommitdiffhomepage
path: root/digital/zigbit/bitcloud/stack/Components/HAL/drivers/USBClasses/MSD/src/massStorageDevice.c
blob: 94c36c42154dc8301811fac7fe4c905036ed2e7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/****************************************************************************//**
  \file massStorageDevice.c

  \brief Implementation of mass storage API.

  \author
    Atmel Corporation: http://www.atmel.com \n
    Support email: avr@atmel.com

  Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
  Licensed under Atmel's Limited License Agreement (BitCloudTM).

  \internal
  History:
    29/08/11 N. Fomin - Created
*******************************************************************************/
/******************************************************************************
                   Includes section
******************************************************************************/
#include <mscProtocol.h>
#include <usbEnumeration.h>
#include <massStorageDevice.h>
#include <scsiProtocol.h>
#include <abstractMemory.h>
#include <usbDescriptors.h>
#include <mem.h>

/******************************************************************************
                   Defines section
******************************************************************************/
#define CBW_SIZE 31
#define CBW_SIGNATURE 0x43425355

#define CSW_SIZE 13
#define CSW_COMMAND_FAILED_STATUS  1
#define CSW_COMMAND_SUCCESS_STATUS 0

/******************************************************************************
                   Types section
******************************************************************************/
typedef enum
{
  MSD_COMMAND_TRANSPORT,
  MSD_SCSI_ANSWER,
  MSD_DATA_INOUT,
  MSD_STATUS_TRANSPORT
} MsdState_t;

typedef struct
{
  uint32_t bufferOffset;
  uint16_t buffersToTransfer;
  uint32_t blockAddress;
} MsdReadWriteControl_t;

/******************************************************************************
                   Prototypes section
******************************************************************************/
static void msdBufferReadCallback(MemoryStatus_t status);
static void msdBufferWriteCallback(MemoryStatus_t status);

/******************************************************************************
                   External global variables section
******************************************************************************/
static UsbMscRequest_t request;

/******************************************************************************
                   Global variables section
******************************************************************************/
// pointer to application hsmci descriptor
static HAL_HsmciDescriptor_t msdDescr;
HAL_HsmciDescriptor_t *msdPointDescr;
static HAL_HsmciCommandDescriptor_t commandDescr;
static HAL_HsmciDataTransferDescriptor_t dataTransferDescr;
static MsdReadWriteControl_t rwControl;
static MscCSW_t csw;
static MsdState_t msdState;
static MSD_Callback_t msdCallback = NULL;

void msdRcvCallback(void *pArg, uint8_t status, uint16_t transferred, uint16_t remaining);

/******************************************************************************
                   Implementations section
******************************************************************************/
/**************************************************************************//**
\brief Transmitting callback of usb.
\param[in]
  pArg - pointer to some data;
  status - result of the USB transfer.;
  transferred - how much data are transferred;
  remaining - how much data are not transferred.
******************************************************************************/
void msdTmtCallback(void *pArg, uint8_t status, uint16_t transferred, uint16_t remaining)
{
  (void)pArg;
  (void)status;
  (void)remaining;
  (void)transferred;

  if (MSD_DATA_INOUT == msdState)
  {
    rwControl.buffersToTransfer--;
    rwControl.blockAddress += msdDescr.dataTransferDescriptor->length / STANDARD_BLOCK_LENGTH;
    if (0 == rwControl.buffersToTransfer)
    {
      csw.bCSWStatus = CSW_COMMAND_SUCCESS_STATUS;
      msdState = MSD_STATUS_TRANSPORT;
      memcpy(msdDescr.dataTransferDescriptor->buffer, (uint8_t*)&csw, CSW_SIZE);
      HAL_UsbWrite(MSD_TRANSMIT_PIPE, msdDescr.dataTransferDescriptor->buffer, CSW_SIZE, msdTmtCallback, NULL);
      return;
    }
    else
      if (memorySuccessStatus != absMemRead(&msdDescr, rwControl.blockAddress, msdBufferReadCallback))
        msdCallback(MSD_READ_ERROR);
      
  }
  if (MSD_SCSI_ANSWER == msdState)
  {
    csw.bCSWStatus = CSW_COMMAND_SUCCESS_STATUS;
    msdState = MSD_STATUS_TRANSPORT;
    memcpy(msdDescr.dataTransferDescriptor->buffer, (uint8_t*)&csw, CSW_SIZE);
    HAL_UsbWrite(MSD_TRANSMIT_PIPE, msdDescr.dataTransferDescriptor->buffer, CSW_SIZE, msdTmtCallback, NULL);
    return;
  }
  if (MSD_STATUS_TRANSPORT == msdState)
  {
    if (msdCallback)
      msdCallback(MSD_STATUS_SUCCESS);
    msdState = MSD_COMMAND_TRANSPORT;
    HAL_UsbRead(MSD_RECEIVE_PIPE, msdDescr.dataTransferDescriptor->buffer, CBW_SIZE, msdRcvCallback, NULL);
    return;
  }
}

/**************************************************************************//**
\brief Receiving callback of usb.
\param[in]
  pArg - pointer to some data;
  status - result of the USB transfer.;
  transferred - how much data are transferred;
  remaining - how much data are not transferred.
******************************************************************************/
void msdRcvCallback(void *pArg, uint8_t status, uint16_t transferred, uint16_t remaining)
{
  bool validAndMeaningful = true;
  MscCBW_t *cbw = (MscCBW_t *)(msdDescr.dataTransferDescriptor->buffer);
  uint8_t length;

  (void)pArg;
  (void)status;
  (void)remaining;
  
  if (MSD_COMMAND_TRANSPORT == msdState)
  {
    /* check that CBW is valid */
    if ((CBW_SIZE != transferred) || (CBW_SIGNATURE != cbw->dCBWSignature))
      validAndMeaningful = false;
    if (!validAndMeaningful)
    {
      HAL_Stall(ADDRESS_MSC_BULKIN_PIPE & 0x03);
      return;
    }
    /* check that CBW is meaningful */
    if ((cbw->bCBWLUN > 0x0F) || (cbw->bCBWCBLength == 0) || (cbw->bCBWCBLength > 0x10) || (cbw->bmCBWFlags & 0x7F))
      validAndMeaningful = false;
    if (!validAndMeaningful)
      return;
    /* fill csw with parameters from cbw */
    csw.cDSWTag = cbw->cDBWTag;
    csw.dCSWSignature = CBW_SIGNATURE;
    /* check that command is valid */
    if (!scsiIsValidCommand(cbw->CBWCB))
    {
      csw.bCSWStatus = CSW_COMMAND_FAILED_STATUS;
      msdState = MSD_STATUS_TRANSPORT;
      memcpy(msdDescr.dataTransferDescriptor->buffer, (uint8_t*)&csw, CSW_SIZE);
      HAL_UsbWrite(MSD_TRANSMIT_PIPE, msdDescr.dataTransferDescriptor->buffer, CSW_SIZE, msdTmtCallback, NULL);
      return;
    }
    /* check that no data in-out phase is needed */
    if (!scsiIsDataInOutPhaseNeeded(cbw->CBWCB))
    {
      csw.bCSWStatus = CSW_COMMAND_SUCCESS_STATUS;
      msdState = MSD_STATUS_TRANSPORT;
      memcpy(msdDescr.dataTransferDescriptor->buffer, (uint8_t*)&csw, CSW_SIZE);
      HAL_UsbWrite(MSD_TRANSMIT_PIPE, msdDescr.dataTransferDescriptor->buffer, CSW_SIZE, msdTmtCallback, NULL);
      return;
    }
    /* check that command is no read-write command */
    if (!scsiIsReadWriteCommand(cbw->CBWCB))
    {
      csw.bCSWStatus = CSW_COMMAND_SUCCESS_STATUS;
      msdState = MSD_SCSI_ANSWER;
      length = scsiGetCommandResponse(cbw->CBWCB, msdDescr.dataTransferDescriptor->buffer);
      HAL_UsbWrite(MSD_TRANSMIT_PIPE, msdDescr.dataTransferDescriptor->buffer, length, msdTmtCallback, NULL);
      return;
    }
    /* check that command is read command */
    rwControl.buffersToTransfer = scsiBlocksAmount(cbw->CBWCB) * (msdDescr.dataTransferDescriptor->length / STANDARD_BLOCK_LENGTH);
    rwControl.blockAddress = scsiGetBlockAddress(cbw->CBWCB);
    if (scsiIsReadCommand(cbw->CBWCB))
    {
      if (memorySuccessStatus != absMemRead(&msdDescr, rwControl.blockAddress, msdBufferReadCallback))
        msdCallback(MSD_READ_ERROR);
    }
    else 
      HAL_UsbRead(MSD_RECEIVE_PIPE, msdDescr.dataTransferDescriptor->buffer,
                  BULK_SIZE, msdRcvCallback, NULL);
    msdState = MSD_DATA_INOUT;
    return;
  }
  if (MSD_DATA_INOUT == msdState)
  {
    /* increase internal write buffer offset*/
    rwControl.bufferOffset += transferred;
    /* if buffer is full perform write to flash */
    if (rwControl.bufferOffset == msdDescr.dataTransferDescriptor->length)
    {
      if (memorySuccessStatus !=  absMemWrite(&msdDescr, rwControl.blockAddress, msdBufferWriteCallback))
        msdCallback(MSD_WRITE_ERROR);
      /* wait for hsmci bus becomes free */
      rwControl.bufferOffset = 0;
      rwControl.buffersToTransfer--;
      rwControl.blockAddress += msdDescr.dataTransferDescriptor->length / STANDARD_BLOCK_LENGTH;
      return;
    }
    if (0 != rwControl.buffersToTransfer)
      HAL_UsbRead(MSD_RECEIVE_PIPE, msdDescr.dataTransferDescriptor->buffer + rwControl.bufferOffset,
                BULK_SIZE, msdRcvCallback, NULL);
    
  }
}

/**************************************************************************//**
\brief Opens mass storage device.
\param[in]
  callback - pointer to function to notify about MSD errors and transactions;
  responseBuffer - pointer to hsmci command response buffer; it should have
                   a size of four;
  buffer - pointer to buffer for hsmci data transfer; it should be
           a multiplier of 512;
  length - length of buffer for data transfer.
******************************************************************************/
void MSD_Open(MSD_Callback_t callback, uint32_t *responseBuffer, uint8_t *buffer, uint32_t length)
{
  uint32_t lastBlock;

  msdCallback = callback;

  msdDescr.dataTransferDescriptor = &dataTransferDescr;
  msdDescr.commandDescriptor = &commandDescr;
  msdDescr.dataTransferDescriptor->buffer = buffer;
  msdDescr.dataTransferDescriptor->length = length;
  msdDescr.commandDescriptor->responseBuffer = responseBuffer;
  msdDescr.dataTransferDescriptor->commandDescr = &commandDescr;

  msdPointDescr = & msdDescr;

  if (-1 == HAL_OpenHsmci(&msdDescr))
    if (msdCallback)
      msdCallback(MSD_INTERFACE_BUSY);

  if (memorySuccessStatus != absMemInit(&msdDescr))
    if (msdCallback)
      msdCallback(MSD_MEMORY_INIT_ERROR);

  if (memorySuccessStatus != absMemCapacity(&msdDescr, &lastBlock))
    if (msdCallback)
      msdCallback(MSD_READ_CAPACITY_ERROR);

  scsiSetCapacity(lastBlock);

  rwControl.bufferOffset = 0;
  rwControl.buffersToTransfer = 0;
  msdState = MSD_COMMAND_TRANSPORT;
  csw.dCSWDataResidue = 0;

  HAL_RegisterEndOfBusResetHandler(usbBusResetAction);
  if (DEVICE_POWERED != HAL_GetState())
    HAL_UsbInit((uint8_t *)&request);
}

/**************************************************************************//**
\brief Closes mass storage device.
******************************************************************************/
void MSD_Close(void)
{
  if (-1 == HAL_CloseHsmci(&msdDescr))
    if (msdCallback)
      msdCallback(MSD_INTERFACE_BUSY);
}

/**************************************************************************//**
\brief Memory read callback function.
\param[in]
  status - status of memory read procedure.
******************************************************************************/
static void msdBufferReadCallback(MemoryStatus_t status)
{
  if (memorySuccessStatus != status)
    msdCallback(MSD_READ_ERROR);
  HAL_UsbWrite(MSD_TRANSMIT_PIPE, msdDescr.dataTransferDescriptor->buffer,
               msdDescr.dataTransferDescriptor->length, msdTmtCallback, NULL);
}

/**************************************************************************//**
\brief Memory write callback function.
\param[in]
  status - status of memory write procedure.
******************************************************************************/
static void msdBufferWriteCallback(MemoryStatus_t status)
{
  if (memorySuccessStatus != status)
    msdCallback(MSD_WRITE_ERROR);
  if (0 == rwControl.buffersToTransfer)
  {
    csw.bCSWStatus = CSW_COMMAND_SUCCESS_STATUS;
    msdState = MSD_STATUS_TRANSPORT;
    memcpy(msdDescr.dataTransferDescriptor->buffer, (uint8_t*)&csw, CSW_SIZE);
    HAL_UsbWrite(MSD_TRANSMIT_PIPE, msdDescr.dataTransferDescriptor->buffer, CSW_SIZE, msdTmtCallback, NULL);
  }
  else 
    HAL_UsbRead(MSD_RECEIVE_PIPE, msdDescr.dataTransferDescriptor->buffer,
              BULK_SIZE, msdRcvCallback, NULL);
}

// eof massStorageDevice.c