summaryrefslogtreecommitdiff
path: root/chip/stm32/usb_ms.c
blob: 8c0abbe1d549859e96000cb9a5f71ad263059489 (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
/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "clock.h"
#include "common.h"
#include "config.h"
#include "console.h"
#include "gpio.h"
#include "hooks.h"
#include "link_defs.h"
#include "registers.h"
#include "task.h"
#include "timer.h"
#include "util.h"
#include "usb.h"
#include "usb_ms.h"
#include "usb_ms_scsi.h"

/*
 * Implements the USB Mass Storage Class specification using the
 * Bulk-Only Transport (BBB) protocol with the transparent SCSI command set.
 */

/* Console output macros */
#define CPUTS(outstr) cputs(CC_USBMS, outstr)
#define CPRINTF(format, args...) cprintf(CC_USBMS, format, ## args)

/* Mass storage descriptors */
const struct usb_interface_descriptor USB_IFACE_DESC(USB_IFACE_MS) = {
	.bLength = USB_DT_INTERFACE_SIZE,
	.bDescriptorType = USB_DT_INTERFACE,
	.bInterfaceNumber = USB_IFACE_MS,
	.bAlternateSetting = 0,
	.bNumEndpoints = 2,
	.bInterfaceClass = USB_CLASS_MASS_STORAGE,
	.bInterfaceSubClass = USB_MS_SUBCLASS_SCSI,
	.bInterfaceProtocol = USB_MS_PROTOCOL_BBB,
	.iInterface = 0,
};
const struct usb_endpoint_descriptor USB_EP_DESC(USB_IFACE_MS, USB_EP_MS_TX) = {
	.bLength = USB_DT_ENDPOINT_SIZE,
	.bDescriptorType = USB_DT_ENDPOINT,
	.bEndpointAddress = USB_DIR_IN | USB_EP_MS_TX,
	.bmAttributes = 0x02 /* Bulk */,
	.wMaxPacketSize = USB_MS_PACKET_SIZE,
	.bInterval = 0,
};
const struct usb_endpoint_descriptor USB_EP_DESC(USB_IFACE_MS, USB_EP_MS_RX) = {
	.bLength = USB_DT_ENDPOINT_SIZE,
	.bDescriptorType = USB_DT_ENDPOINT,
	.bEndpointAddress = USB_EP_MS_RX,
	.bmAttributes = 0x02 /* Bulk */,
	.wMaxPacketSize = USB_MS_PACKET_SIZE,
	.bInterval = 0,
};

/* USB mass storage state machine */
static enum usb_ms_state {
	USB_MS_STATE_IDLE,
	USB_MS_STATE_BUSY,
	USB_MS_STATE_ERROR, /* received an invalid CBW */
	USB_MS_STATE_PHASE_ERROR,
} ms_state = USB_MS_STATE_IDLE;

/* Hardware buffers for USB endpoints */
usb_uint ms_ep_tx[USB_MS_PACKET_SIZE] __usb_ram;
usb_uint ms_ep_rx[USB_MS_PACKET_SIZE] __usb_ram;

static void ms_tx_reset(void)
{
	btable_ep[USB_EP_MS_TX].tx_addr = usb_sram_addr(ms_ep_tx);
	btable_ep[USB_EP_MS_TX].tx_count = 0;
	btable_ep[USB_EP_MS_TX].rx_count = 0;

	STM32_USB_EP(USB_EP_MS_TX) =
				(USB_EP_MS_TX << 0) /* Endpoint Address */ |
				(2 << 4) /* TX NAK */ |
				(0 << 9) /* Bulk EP */ |
				(0 << 12) /* RX Disabled */;

	ms_state = USB_MS_STATE_IDLE;
	scsi_reset();
}

static void ms_rx_reset(void)
{
	btable_ep[USB_EP_MS_RX].rx_addr = usb_sram_addr(ms_ep_rx);
	btable_ep[USB_EP_MS_RX].rx_count = 0x8000 |
				((USB_MS_PACKET_SIZE/32-1) << 10);
	btable_ep[USB_EP_MS_RX].tx_count = 0;

	STM32_USB_EP(USB_EP_MS_RX) =
				(USB_EP_MS_RX << 0) /* Endpoint Address */ |
				(0 << 4) /* TX Disabled */ |
				(0 << 9) /* Bulk EP */ |
				(3 << 12) /* RX VALID */;

	ms_state = USB_MS_STATE_IDLE;
	scsi_reset();
}

/*
 * Construct and send a CSW.
 */
static void ms_send_csw(int ms_tag, int ms_xfer_len,
	int scsi_rv, int scsi_xfer_len)
{
	struct usb_ms_csw *resp = (struct usb_ms_csw *) ms_ep_tx;

	/* construct CSW response */
	resp->signature = UBS_MS_CSW_SIGNATURE;
	resp->tag = ms_tag;
	resp->data_residue = (ms_xfer_len > scsi_xfer_len) ?
				(ms_xfer_len - scsi_xfer_len) :
				(scsi_xfer_len - ms_xfer_len);
	if (scsi_rv != SCSI_SENSE_HARDWARE_ERROR)
		resp->status = (scsi_rv == SCSI_SENSE_NO_SENSE) ?
				USB_MS_CSW_CMD_PASSED :
				USB_MS_CSW_CMD_FAILED;
	else {
		ms_state = USB_MS_STATE_PHASE_ERROR;
		resp->status = USB_MS_CSW_CMD_PHASE_ERR;
	}

	/* set CSW response length */
	btable_ep[USB_EP_MS_TX].tx_count = USB_MS_CSW_LENGTH;

	/* wait for data to be read */
	STM32_TOGGLE_EP(USB_EP_MS_TX, EP_TX_MASK, EP_TX_VALID, 0);
}

/*
 * Send data already in the output buffer.
 */
static void ms_send_data(int ms_xfer_len, int *scsi_xfer_len)
{
	/* truncate if necessary */
	if (btable_ep[USB_EP_MS_TX].tx_count > ms_xfer_len)
		btable_ep[USB_EP_MS_TX].tx_count = ms_xfer_len;

	/* increment sent data counter with actual length */
	*scsi_xfer_len += btable_ep[USB_EP_MS_TX].tx_count;

	/* wait for data to be read */
	STM32_TOGGLE_EP(USB_EP_MS_TX, EP_TX_MASK, EP_TX_VALID, 0);
}

static void ms_tx(void)
{
	task_set_event(TASK_ID_USB_MS, TASK_EVENT_CUSTOM(USB_MS_EVENT_TX), 0);

	STM32_USB_EP(USB_EP_MS_TX) &= EP_MASK;
}

static void ms_rx(void)
{
	task_set_event(TASK_ID_USB_MS, TASK_EVENT_CUSTOM(USB_MS_EVENT_RX), 0);

	STM32_USB_EP(USB_EP_MS_RX) &= EP_MASK;
}
USB_DECLARE_EP(USB_EP_MS_TX, ms_tx, ms_tx, ms_tx_reset);
USB_DECLARE_EP(USB_EP_MS_RX, ms_rx, ms_rx, ms_rx_reset);

static void ms_iface_request(usb_uint *ep0_buf_rx, usb_uint *ep0_buf_tx)
{
	uint16_t *req = (uint16_t *) ep0_buf_rx;

	if ((req[0] & (USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS)) ==
	    (USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS)) {
		switch (req[0] >> 8) {
		case USB_MS_REQ_RESET:
			if (req[1] == 0 && req[2] == USB_IFACE_MS &&
			    req[3] == 0) {
				ms_rx_reset();
			}
		break;
		case USB_MS_REQ_GET_MAX_LUN:
			if (req[1] == 0 && req[2] == USB_IFACE_MS &&
			    req[3] == 1) {
				ep0_buf_tx[0] = SCSI_MAX_LUN;
				btable_ep[0].tx_count = sizeof(uint8_t);
				STM32_TOGGLE_EP(USB_EP_CONTROL, EP_TX_RX_MASK,
					EP_TX_RX_VALID, 0);
			}
		break;
		}
	} else {
		CPRINTF("ms stalling: %x %x %x %x\n",
			req[0], req[1], req[2], req[3]);
		STM32_TOGGLE_EP(USB_EP_CONTROL, EP_TX_RX_MASK,
			EP_RX_VALID | EP_TX_STALL, 0);
	}
}
USB_DECLARE_IFACE(USB_IFACE_MS, ms_iface_request);

void ms_task(void)
{
	struct usb_ms_cbw *req = (struct usb_ms_cbw *) ms_ep_rx;
	int scsi_rv, scsi_xfer_len = 0;
	uint32_t ms_xfer_len = 0, ms_tag = 0;
	uint8_t ms_dir = 0, evt;

	while (1) {
		/* wait for event or usb reset */
		evt = (task_wait_event(-1) & 0xff);

		switch (ms_state) {
		case USB_MS_STATE_IDLE:
			/* receiving data */
			if (evt & USB_MS_EVENT_RX) {
				/* CBW is not valid or meaningful */
				if ((btable_ep[USB_EP_MS_RX].rx_count & 0x3ff)
				     != USB_MS_CBW_LENGTH ||
				     req->signature
				     != USB_MS_CBW_SIGNATURE ||
				     req->LUN & 0xf0 ||
				     req->length & 0xe0 ||
				     req->LUN > SCSI_MAX_LUN) {

					ms_state = USB_MS_STATE_ERROR;
					STM32_TOGGLE_EP(USB_EP_MS_TX,
						EP_TX_MASK, EP_TX_STALL, 0);
					STM32_TOGGLE_EP(USB_EP_MS_RX,
						EP_RX_MASK, EP_RX_STALL, 0);
					break;
				}

				/* have new packet */
				ms_state = USB_MS_STATE_BUSY;

				/* record packet details */
				ms_tag = req->tag;
				ms_xfer_len = req->data_transfer_length;
				ms_dir = req->flags;
				scsi_xfer_len = 0;

				/* parse message and get next state */
				scsi_rv = scsi_parse(req->command_block,
					req->length);
				if (scsi_rv == SCSI_STATUS_CONTINUE) {
					if (ms_dir & USB_MS_CBW_DATA_IN)
						/* send out data */
						ms_send_data(ms_xfer_len,
							&scsi_xfer_len);
					else
						/* receive more data */
						STM32_TOGGLE_EP(USB_EP_MS_RX,
						 EP_RX_MASK, EP_RX_VALID, 0);
				} else {
					/* send message response */
					ms_state = USB_MS_STATE_IDLE;
					ms_send_csw(ms_tag, ms_xfer_len,
						scsi_rv, scsi_xfer_len);
				}
			} else if (evt & USB_MS_EVENT_TX) {
				/* just sent CSW, wait for next CBW */
				STM32_TOGGLE_EP(USB_EP_MS_RX, EP_RX_MASK,
					EP_RX_VALID, 0);
			}
		break;
		case USB_MS_STATE_BUSY:
			/* receiving data */
			if (evt & USB_MS_EVENT_RX) {
				/*
				 * received at least two CBW's in a row,
				 * go to error state
				 */
				if (ms_dir & USB_MS_CBW_DATA_IN) {
					ms_state = USB_MS_STATE_ERROR;
					STM32_TOGGLE_EP(USB_EP_MS_TX,
						EP_TX_MASK, EP_TX_STALL, 0);
					STM32_TOGGLE_EP(USB_EP_MS_RX,
						EP_RX_MASK, EP_RX_STALL, 0);
					break;
				}
				/* receive data */
				scsi_xfer_len +=
				 (btable_ep[USB_EP_MS_RX].rx_count & 0x3ff);
				scsi_rv = scsi_parse(NULL, 0);
				if (scsi_rv != SCSI_STATUS_CONTINUE) {
					ms_state = USB_MS_STATE_IDLE;
					ms_send_csw(ms_tag, ms_xfer_len,
						scsi_rv, scsi_xfer_len);
				}

				/* wait for more data */
				STM32_TOGGLE_EP(USB_EP_MS_RX,
					EP_RX_MASK, EP_RX_VALID, 0);
			} else if (evt & USB_MS_EVENT_TX) {
				/* reparse message and get next state */
				scsi_rv = scsi_parse(req->command_block,
					req->length);
				if (scsi_rv == SCSI_STATUS_CONTINUE) {
					ms_send_data(ms_xfer_len,
						&scsi_xfer_len);
				} else {
					ms_state = USB_MS_STATE_IDLE;
					ms_send_csw(ms_tag, ms_xfer_len,
						scsi_rv, scsi_xfer_len);
				}
			}
		break;
		case USB_MS_STATE_ERROR:
			/* maintain error state until reset recovery */
		break;
		case USB_MS_STATE_PHASE_ERROR:
			CPUTS("phase error!\n");

			STM32_TOGGLE_EP(USB_EP_MS_TX, EP_TX_MASK,
				EP_TX_STALL, 0);
			STM32_TOGGLE_EP(USB_EP_MS_RX, EP_RX_MASK,
				EP_RX_STALL, 0);
		break;
		default:
		break;
		}
	}
}