forked from wookey-project/libmassstorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusb_bbb.c
252 lines (222 loc) · 6.1 KB
/
usb_bbb.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
/*
*
* Copyright 2018 The wookey project team <[email protected]>
* - Ryad Benadjila
* - Arnauld Michelizza
* - Mathieu Renard
* - Philippe Thierry
* - Philippe Trebuchet
*
* This package is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* the Free Software Foundation; either version 2.1 of the License, or (at
* ur option) any later version.
*
* This package 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along
* with this package; if not, write to the Free Software Foundation, Inc., 51
* Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "libc/stdio.h"
#include "libc/nostd.h"
#include "libc/regutils.h"
#include "usb.h"
#include "usb_bbb.h"
#define BBB_DEBUG 0
enum bbb_state {
READY,
CMD,
DATA,
STATUS,
};
static void (*callback_cmd_received)(uint8_t cdb[], uint8_t cdb_len);
static void (*callback_data_received)(uint32_t size);
static void (*callback_data_sent)(void);
volatile enum bbb_state bbb_state;
uint32_t current_tag;
/* Command Block Wrapper */
struct __packed scsi_cbw {
uint32_t sig;
uint32_t tag;
uint32_t transfer_len;
struct {
uint8_t reserved:7;
uint8_t direction:1;
} flags;
struct {
uint8_t lun:4;
uint8_t reserved:4;
} lun;
struct {
uint8_t cdb_len:5;
uint8_t reserved:3;
} cdb_len;
uint8_t cdb[16]; // FIXME We must handle CDB6 CDB10 CDB12 CDB16 ?
};
static struct scsi_cbw cbw;
#define USB_BBB_CBW_SIG 0x43425355 /* "USBC" */
void read_next_cmd(void)
{
#if BBB_DEBUG
printf("[USB BBB] %s: Reading a command\n", __func__);
#endif
bbb_state = READY;
usb_driver_setup_read(&cbw, sizeof(cbw), 1);
}
extern volatile bool reset_requested;
static void usb_bbb_cmd_received(uint32_t size)
{
if(reset_requested == true){
while(reset_requested == true) {
continue;
}
return;
}
if (size != sizeof(struct scsi_cbw) || cbw.sig != USB_BBB_CBW_SIG) {
aprintf("[USB BBB] %s: CBW not valid\n", __func__ );
return;
}
if (cbw.flags.reserved || cbw.lun.reserved || cbw.cdb_len.reserved || cbw.lun.lun) {
/* XXX: the only valid LUN for our device is 0 */
/* TODO: check that cbw.cdb_len and cbw.cdb are in accordance
* with InferfaceSubClass
*/
#if BBB_DEBUG
aprintf("[USB BBB] %s: CBW not meaningful\n", __func__);
#endif
return;
}
current_tag = cbw.tag;
bbb_state = CMD;
#if BBB_DEBUG
aprintf("[USB BBB] %s: Command received\n", __func__);
#endif
callback_cmd_received(cbw.cdb, cbw.cdb_len.cdb_len);
}
static void usb_bbb_data_received(uint32_t size)
{
if(reset_requested == true){
while(reset_requested == true){
continue;
}
return;
}
#if BBB_DEBUG
aprintf("[USB BBB] %s bbb_state: %x ... \n", __func__, bbb_state);
#endif
switch (bbb_state) {
case READY:
usb_bbb_cmd_received(size);
break;
case STATUS:
bbb_state = READY;
break;
case DATA:
callback_data_received(size);
break;
default:
aprintf("[USB BBB] %s: ERROR usb_bbb_data_received ... \n",
__func__);
}
}
static void usb_bbb_data_sent(void)
{
if(reset_requested == true){
while(reset_requested == true){
continue;
}
return;
}
switch (bbb_state) {
case STATUS:
#if BBB_DEBUG
aprintf("[USB BBB] %s: data sent while in STATUS state\n",
__func__);
#endif
read_next_cmd();
break;
case DATA:
#if BBB_DEBUG
aprintf("[USB BBB] %s: data sent while in DATA state\n", __func__);
#endif
callback_data_sent();
break;
case READY:
#if BBB_DEBUG
aprintf("[USB BBB] %s: data sent while in READY state\n", __func__);
#endif
break;
case CMD:
#if BBB_DEBUG
aprintf("[USB BBB] %s: data sent while in CMD state\n", __func__);
#endif
break;
default:
aprintf("[USB BBB] %s: Unknown bbb_state\n", __func__);
}
}
void usb_bbb_early_init(void (*cmd_received)(uint8_t cdb[], uint8_t cdb_len),
void(*data_received)(uint32_t), void(*data_sent)(void))
{
#if BBB_DEBUG
printf("[USB BBB] %s\n", __func__);
#endif
callback_cmd_received = cmd_received;
callback_data_received = data_received;
callback_data_sent = data_sent;
usb_driver_early_init(usb_bbb_data_received, usb_bbb_data_sent);
}
void usb_bbb_init(void)
{
usb_driver_init();
bbb_state = READY;
/* Read first command */
read_next_cmd();
}
void usb_bbb_reinit(void)
{
bbb_state = READY;
}
/* Command Status Wrapper */
struct __packed scsi_csw {
uint32_t sig;
uint32_t tag;
uint32_t data_residue;
uint8_t status;
};
#define USB_BBB_CSW_SIG 0x53425355 /* "USBS" */
void usb_bbb_send_csw(enum csw_status status, uint32_t data_residue)
{
struct scsi_csw csw = {
.sig = USB_BBB_CSW_SIG,
.tag = current_tag,
.data_residue = data_residue,
.status = status
};
bbb_state = STATUS;
#if BBB_DEBUG
aprintf("[USB BBB] %s: Sending CSW (%x, %x, %x, %x)\n", __func__, csw.sig,
csw.tag, csw.data_residue, csw.status);
#endif
usb_driver_setup_send((uint8_t *) & csw, sizeof(csw), 2);
}
void usb_bbb_send(const uint8_t * src, uint32_t size, uint8_t ep)
{
#if BBB_DEBUG
aprintf("[USB BBB] %s\n", __func__);
#endif
bbb_state = DATA;
usb_driver_setup_send(src, size, ep);
}
void usb_bbb_read(void *dst, uint32_t size, uint8_t ep)
{
#if BBB_DEBUG
aprintf("[USB BBB] %s\n", __func__);
#endif
bbb_state = DATA;
usb_driver_setup_read(dst, size, ep);
}