-
Notifications
You must be signed in to change notification settings - Fork 2
/
usb_start.c
289 lines (252 loc) · 7.71 KB
/
usb_start.c
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
/*
Copyright 2017 Nicholas W. Sayer
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "atmel_start.h"
#include "usb_start.h"
#include <Crypto.h>
#include <MCI.h>
static enum usb_volume_state vol_state;
enum xfer_dirs { IDLE, READ, WRITE };
volatile static enum xfer_dirs xfer_dir;
volatile static uint32_t xfer_addr;
volatile static uint32_t num_blocks;
volatile static bool xfer_busy;
COMPILER_ALIGNED(4)
volatile static uint8_t __attribute__((section(".dtcm"))) blockbuf[SECTOR_SIZE];
static uint8_t single_desc_bytes[] = {
/* Device descriptors and Configuration descriptors list. */
MSC_DESCES_LS_FS,
};
static struct usbd_descriptors single_desc = {single_desc_bytes, single_desc_bytes + sizeof(single_desc_bytes)};
/* Ctrl endpoint buffer */
static uint8_t ctrl_buffer[64];
#define DISK_INFORMATION(n) \
{ \
CONF_USB_MSC_LUN##n##_TYPE, (CONF_USB_MSC_LUN##n##_RMB << 7), \
((CONF_USB_MSC_LUN##n##_ISO << 6) + (CONF_USB_MSC_LUN##n##_ECMA << 3) + CONF_USB_MSC_LUN##n##_ANSI), \
CONF_USB_MSC_LUN##n##_REPO, 31, 0x00, 0x00, 0x00, CONF_USB_MSC_LUN##n##_FACTORY, \
CONF_USB_MSC_LUN##n##_PRODUCT, CONF_USB_MSC_LUN##n##_PRODUCT_VERSION \
}
/* Inquiry Information */
static uint8_t inquiry_info[CONF_USB_MSC_MAX_LUN + 1][36] = { DISK_INFORMATION(0) };
static bool in_attention;
void set_state(enum usb_volume_state st_in) {
vol_state = st_in;
in_attention = true;
}
/**
* \brief Eject Disk
* \param[in] lun logic unit number
* \return Operation status.
*/
static int32_t disk_eject(uint8_t lun)
{
if (lun > CONF_USB_MSC_MAX_LUN) {
return ERR_NOT_FOUND;
}
// No, we can't just spit the cards out for you.
return ERR_UNSUPPORTED_OP;
}
static int32_t check_state() {
int32_t ret = ERR_NOT_FOUND;
if (in_attention) {
ret = ERR_ABORTED;
} else if (vol_state == NOT_READY) {
ret = ERR_NOT_READY;
} else {
ret = ERR_NONE;
}
return ret;
}
/**
* \brief Inquiry whether Disk is ready
* \param[in] lun logic unit number
* \return Operation status.
*/
static int32_t disk_is_ready(uint8_t lun)
{
if (lun > CONF_USB_MSC_MAX_LUN) {
return ERR_NOT_FOUND;
}
int32_t ret = check_state();
// Once we've been checked here, an ATTENTION state is cleared.
in_attention = false;
return ret;
}
/**
* \brief Callback invoked when a new read blocks command received
* \param[in] lun logic unit number
* \param[in] addr start address of disk to be read
* \param[in] nblocks block amount to be read
* \return Operation status.
*/
static int32_t msc_new_read(uint8_t lun, uint32_t addr, uint32_t nblocks)
{
int32_t ret = check_state();
if (ret != ERR_NONE) return ret;
if (lun > CONF_USB_MSC_MAX_LUN) {
return ERR_NOT_READY;
}
xfer_dir = READ;
xfer_addr = addr;
num_blocks = nblocks;
xfer_busy = false;
return ERR_NONE;
}
/**
* \brief Callback invoked when a new write blocks command received
* \param[in] lun logic unit number
* \param[in] addr start address of disk to be written
* \param[in] nblocks block amount to be written
* \return Operation status.
*/
static int32_t msc_new_write(uint8_t lun, uint32_t addr, uint32_t nblocks)
{
int32_t ret = check_state();
if (ret != ERR_NONE) return ret;
if (lun > CONF_USB_MSC_MAX_LUN) {
return ERR_NOT_READY;
}
xfer_dir = WRITE;
xfer_addr = addr;
num_blocks = nblocks;
xfer_busy = true;
int32_t res = mscdf_xfer_blocks(false, blockbuf, 1);
ASSERT(res == ERR_NONE);
return ERR_NONE;
}
/**
* \brief Callback invoked when a blocks transfer is done
* \param[in] lun logic unit number
* \return Operation status.
*/
static int32_t msc_xfer_done(uint8_t lun)
{
if (lun > CONF_USB_MSC_MAX_LUN) {
return ERR_DENIED;
}
xfer_busy = false;
return ERR_NONE;
}
/**
* \brief Disk loop
*/
__attribute__((noinline)) __attribute__((section(".itcm"))) void disk_task(void)
{
bool res_b;
int32_t res_i;
if (!mscdf_is_enabled()) {
xfer_dir = IDLE;
xfer_busy = false;
return;
}
if (xfer_busy) return; // USB is busy
switch(xfer_dir) {
case READ:
res_b = readVolumeBlock(xfer_addr++, blockbuf);
ASSERT(res_b);
xfer_busy = true;
res_i = mscdf_xfer_blocks(true, blockbuf, 1);
ASSERT(res_i == ERR_NONE);
if (--num_blocks == 0) {
// we're done (once we're no longer busy).
xfer_dir = IDLE;
}
break;
case WRITE:
// We previously did a transfer into blockbuf
res_b = writeVolumeBlock(xfer_addr++, blockbuf);
ASSERT(res_b);
if (--num_blocks > 0) {
// Fetch the next block in the background
xfer_busy = true;
res_i = mscdf_xfer_blocks(false, blockbuf, 1);
ASSERT(res_i == ERR_NONE);
} else {
// This special call tells the MSC system that the write
// is committed and the ACK can be sent to the host.
xfer_busy = true;
res_i = mscdf_xfer_blocks(false, blockbuf, 0);
ASSERT(res_i == ERR_NONE);
xfer_dir = IDLE;
}
break;
case IDLE:
// do nothing.
break;
}
}
/**
* \brief Callback invoked when inquiry data command received
* \param[in] lun logic unit number
* \return Operation status.
*/
static uint8_t *msc_inquiry_info(uint8_t lun)
{
if (lun > CONF_USB_MSC_MAX_LUN) {
return NULL;
} else {
num_blocks = -1;
xfer_busy = false;
return &inquiry_info[lun][0];
}
}
static uint8_t cap_buffer[8];
/**
* \brief Callback invoked when read format capacities command received
* \param[in] lun logic unit number
* \return Operation status.
*/
static uint8_t *msc_get_capacity(uint8_t lun)
{
if (lun > CONF_USB_MSC_MAX_LUN || vol_state != READY) {
return NULL;
} else {
cap_buffer[0] = (uint8_t)((volume_size - 1) >> 24);
cap_buffer[1] = (uint8_t)((volume_size - 1) >> 16);
cap_buffer[2] = (uint8_t)((volume_size - 1) >> 8);
cap_buffer[3] = (uint8_t)((volume_size - 1) >> 0);
cap_buffer[4] = (uint8_t)(SECTOR_SIZE >> 24);
cap_buffer[5] = (uint8_t)(SECTOR_SIZE >> 16);
cap_buffer[6] = (uint8_t)(SECTOR_SIZE >> 8);
cap_buffer[7] = (uint8_t)(SECTOR_SIZE >> 0);
return cap_buffer;
}
}
/**
* \brief USB MSC Init
*/
void usbd_msc_init(void)
{
/* usb stack init */
usbdc_init(ctrl_buffer);
/* usbdc_register_funcion inside */
mscdf_init(CONF_USB_MSC_MAX_LUN);
}
void usb_init(void)
{
xfer_busy = false;
xfer_dir = IDLE;
vol_state = NOT_READY;
usbd_msc_init();
mscdf_register_callback(MSCDF_CB_INQUIRY_DISK, (FUNC_PTR)msc_inquiry_info);
mscdf_register_callback(MSCDF_CB_GET_DISK_CAPACITY, (FUNC_PTR)msc_get_capacity);
mscdf_register_callback(MSCDF_CB_START_READ_DISK, (FUNC_PTR)msc_new_read);
mscdf_register_callback(MSCDF_CB_START_WRITE_DISK, (FUNC_PTR)msc_new_write);
mscdf_register_callback(MSCDF_CB_EJECT_DISK, (FUNC_PTR)disk_eject);
mscdf_register_callback(MSCDF_CB_TEST_DISK_READY, (FUNC_PTR)disk_is_ready);
mscdf_register_callback(MSCDF_CB_XFER_BLOCKS_DONE, (FUNC_PTR)msc_xfer_done);
usbdc_start(&single_desc);
usbdc_attach();
}