Index: umsboot/src/core/synopsysotg/synopsysotg.c |
— | — | @@ -16,6 +16,7 @@ |
17 | 17 | |
18 | 18 | static void synopsysotg_flush_out_endpoint(const struct usb_instance* instance, int ep) |
19 | 19 | { |
| 20 | + if (!ep) return; |
20 | 21 | const struct synopsysotg_config* data = (const struct synopsysotg_config*)instance->driver_config; |
21 | 22 | if (data->core->outep_regs[ep].doepctl.b.epena) |
22 | 23 | { |
— | — | @@ -24,8 +25,8 @@ |
25 | 26 | synopsysotg_target_disable_irq(instance); |
26 | 27 | data->core->dregs.dctl.b.sgoutnak = 1; |
27 | 28 | while (!(data->core->gregs.gintsts.b.goutnakeff)); |
28 | | - union synopsysotg_depctl doepctl = { .b = { .snak = 1, .epdis = 1 } }; |
29 | | - data->core->outep_regs[ep].doepctl = doepctl; |
| 29 | + data->core->outep_regs[ep].doepctl.b.snak = 1; |
| 30 | + data->core->outep_regs[ep].doepctl.b.epdis = 1; |
30 | 31 | while (!(data->core->outep_regs[ep].doepint.b.epdisabled)); |
31 | 32 | data->core->dregs.dctl.b.cgoutnak = 1; |
32 | 33 | synopsysotg_target_enable_irq(instance); |
— | — | @@ -37,6 +38,7 @@ |
38 | 39 | |
39 | 40 | static void synopsysotg_flush_in_endpoint(const struct usb_instance* instance, int ep) |
40 | 41 | { |
| 42 | + if (!ep) return; |
41 | 43 | const struct synopsysotg_config* data = (const struct synopsysotg_config*)instance->driver_config; |
42 | 44 | if (data->core->inep_regs[ep].diepctl.b.epena) |
43 | 45 | { |
— | — | @@ -47,7 +49,7 @@ |
48 | 50 | while (!(data->core->inep_regs[ep].diepint.b.inepnakeff)); |
49 | 51 | data->core->inep_regs[ep].diepctl.b.epdis = 1; |
50 | 52 | while (!(data->core->inep_regs[ep].diepint.b.epdisabled)); |
51 | | - if (ep) data->core->inep_regs[ep].diepctl.b.usbactep = 0; |
| 53 | + data->core->inep_regs[ep].diepctl.b.usbactep = 0; |
52 | 54 | synopsysotg_target_enable_irq(instance); |
53 | 55 | // Wait for any DMA activity to stop, to make sure nobody will touch the FIFO. |
54 | 56 | while (!data->core->gregs.grstctl.b.ahbidle); |
— | — | @@ -161,7 +163,7 @@ |
162 | 164 | return !!data->core->outep_regs[ep.number].doepctl.b.stall; |
163 | 165 | } |
164 | 166 | |
165 | | -void synopsysotg_set_stall(const struct usb_instance* instance, union usb_endpoint_number ep, int stall) |
| 167 | +void synopsysotg_set_stall(const struct usb_instance* instance, union usb_endpoint_number ep, bool stall) |
166 | 168 | { |
167 | 169 | const struct synopsysotg_config* data = (const struct synopsysotg_config*)instance->driver_config; |
168 | 170 | if (ep.direction == USB_ENDPOINT_DIRECTION_IN) |
— | — | @@ -224,7 +226,7 @@ |
225 | 227 | } |
226 | 228 | } |
227 | 229 | |
228 | | -void synopsysotg_ep0_start_rx(const struct usb_instance* instance, int non_setup) |
| 230 | +void synopsysotg_ep0_start_rx(const struct usb_instance* instance, bool non_setup, int len) |
229 | 231 | { |
230 | 232 | const struct synopsysotg_config* data = (const struct synopsysotg_config*)instance->driver_config; |
231 | 233 | struct synopsysotg_state* state = (struct synopsysotg_state*)instance->driver_state; |
— | — | @@ -232,16 +234,16 @@ |
233 | 235 | // Set up data destination |
234 | 236 | if (data->use_dma) data->core->outep_regs[0].doepdma = instance->buffer; |
235 | 237 | else state->endpoints[0].rxaddr = (uint32_t*)instance->buffer; |
236 | | - union synopsysotg_dep0xfrsiz deptsiz = { .b = { .supcnt = 3, .pktcnt = !!non_setup, .xfersize = 64 } }; |
| 238 | + union synopsysotg_dep0xfrsiz deptsiz = { .b = { .supcnt = 3, .pktcnt = !!non_setup, .xfersize = len } }; |
237 | 239 | data->core->outep_regs[0].doeptsiz.d32 = deptsiz.d32; |
238 | 240 | |
239 | 241 | // Flush CPU cache if necessary |
240 | | - if (data->use_dma) invalidate_dcache(instance->buffer, sizeof(instance->buffer)); |
| 242 | + if (data->use_dma) invalidate_dcache(instance->buffer, len); |
241 | 243 | |
242 | 244 | // Enable the endpoint |
243 | 245 | union synopsysotg_depctl depctl = data->core->outep_regs[0].doepctl; |
244 | 246 | depctl.b.epena = 1; |
245 | | - depctl.b.cnak = non_setup; |
| 247 | + depctl.b.cnak = !!non_setup; |
246 | 248 | data->core->outep_regs[0].doepctl = depctl; |
247 | 249 | } |
248 | 250 | |
— | — | @@ -364,9 +366,9 @@ |
365 | 367 | } |
366 | 368 | union usb_endpoint_number epnum = { .direction = USB_ENDPOINT_DIRECTION_IN, .number = ep }; |
367 | 369 | int bytesleft = data->core->inep_regs[ep].dieptsiz.b.xfersize; |
| 370 | + data->core->inep_regs[ep].diepint = epints; |
368 | 371 | if (epints.b.timeout) usb_handle_timeout(instance, epnum, bytesleft); |
369 | 372 | if (epints.b.xfercompl) usb_handle_xfer_complete(instance, epnum, bytesleft); |
370 | | - data->core->inep_regs[ep].diepint = epints; |
371 | 373 | } |
372 | 374 | } |
373 | 375 | |
— | — | @@ -378,10 +380,11 @@ |
379 | 381 | if (daint.ep.out & (1 << ep)) |
380 | 382 | { |
381 | 383 | union synopsysotg_doepintn epints = data->core->outep_regs[ep].doepint; |
| 384 | + data->core->outep_regs[ep].doepint = epints; |
382 | 385 | union usb_endpoint_number epnum = { .direction = USB_ENDPOINT_DIRECTION_OUT, .number = ep }; |
383 | 386 | if (epints.b.setup) |
384 | 387 | { |
385 | | - if (data->use_dma) invalidate_dcache(instance->buffer, sizeof(instance->buffer)); |
| 388 | + if (data->use_dma) invalidate_dcache((const void*)data->core->inep_regs[ep].diepdma, 8); |
386 | 389 | synopsysotg_flush_in_endpoint(instance, ep); |
387 | 390 | usb_handle_setup_received(instance, epnum); |
388 | 391 | } |
— | — | @@ -390,7 +393,6 @@ |
391 | 394 | int bytesleft = data->core->inep_regs[ep].dieptsiz.b.xfersize; |
392 | 395 | usb_handle_xfer_complete(instance, epnum, bytesleft); |
393 | 396 | } |
394 | | - data->core->outep_regs[ep].doepint = epints; |
395 | 397 | } |
396 | 398 | } |
397 | 399 | |
— | — | @@ -458,11 +460,11 @@ |
459 | 461 | int addr = data->fifosize; |
460 | 462 | for (i = 0; i < 16; i++) |
461 | 463 | { |
| 464 | + data->core->inep_regs[i].diepctl.b.nextep = (i + 1) & 0xf; |
462 | 465 | int size = data->txfifosize[i]; |
463 | 466 | addr -= size; |
464 | 467 | if (size) |
465 | 468 | { |
466 | | - data->core->inep_regs[i].diepctl.b.nextep = (i + 1) & 0xf; |
467 | 469 | union synopsysotg_txfsiz fsiz = { .b = { .startaddr = addr, .depth = size } }; |
468 | 470 | if (!i) data->core->gregs.dieptxf0_hnptxfsiz = fsiz; |
469 | 471 | else data->core->gregs.dieptxf[i - 1] = fsiz; |
— | — | @@ -526,3 +528,4 @@ |
527 | 529 | .unconfigure_ep = synopsysotg_unconfigure_ep, |
528 | 530 | .get_max_transfer_size = synopsysotg_get_max_transfer_size, |
529 | 531 | }; |
| 532 | + |
Index: umsboot/src/app/umsboot/usbglue.c |
— | — | @@ -188,3 +188,4 @@ |
189 | 189 | &usb_c1, |
190 | 190 | }, |
191 | 191 | }; |
| 192 | + |
Index: umsboot/src/protocol/usb/usb.h |
— | — | @@ -235,8 +235,8 @@ |
236 | 236 | |
237 | 237 | struct __attribute__((packed,aligned(4))) usb_state |
238 | 238 | { |
239 | | - bool (*ep0_rx_callback)(const struct usb_instance* data, int bytesleft); |
240 | | - bool (*ep0_tx_callback)(const struct usb_instance* data, int bytesleft); |
| 239 | + bool (*ep0_rx_callback)(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft); |
| 240 | + bool (*ep0_tx_callback)(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft); |
241 | 241 | const void* ep0_tx_ptr; |
242 | 242 | uint16_t ep0_tx_len; |
243 | 243 | uint8_t current_address; |
— | — | @@ -248,12 +248,12 @@ |
249 | 249 | { |
250 | 250 | void (*init)(const struct usb_instance* data); |
251 | 251 | void (*exit)(const struct usb_instance* data); |
252 | | - void (*ep0_start_rx)(const struct usb_instance* data, int non_setup); |
| 252 | + void (*ep0_start_rx)(const struct usb_instance* data, bool non_setup, int len); |
253 | 253 | void (*ep0_start_tx)(const struct usb_instance* data, const void* buf, int len); |
254 | 254 | void (*start_rx)(const struct usb_instance* data, union usb_endpoint_number ep, void* buf, int size); |
255 | 255 | void (*start_tx)(const struct usb_instance* data, union usb_endpoint_number ep, const void* buf, int size); |
256 | 256 | int (*get_stall)(const struct usb_instance* data, union usb_endpoint_number ep); |
257 | | - void (*set_stall)(const struct usb_instance* data, union usb_endpoint_number ep, int stall); |
| 257 | + void (*set_stall)(const struct usb_instance* data, union usb_endpoint_number ep, bool stall); |
258 | 258 | void (*set_address)(const struct usb_instance* data, uint8_t address); |
259 | 259 | void (*configure_ep)(const struct usb_instance* data, union usb_endpoint_number ep, enum usb_endpoint_type type, int maxpacket); |
260 | 260 | void (*unconfigure_ep)(const struct usb_instance* data, union usb_endpoint_number ep); |
— | — | @@ -285,15 +285,17 @@ |
286 | 286 | extern void usb_handle_timeout(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft); |
287 | 287 | extern void usb_handle_xfer_complete(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft); |
288 | 288 | extern void usb_handle_setup_received(const struct usb_instance* data, union usb_endpoint_number epnum); |
289 | | -extern void usb_ep0_start_rx(const struct usb_instance* data, int non_setup, bool (*callback)(const struct usb_instance* data, int bytesleft)); |
290 | | -extern void usb_ep0_start_tx(const struct usb_instance* data, const void* buf, int len, bool last, bool (*callback)(const struct usb_instance* data, int bytesleft)); |
291 | | -extern void usb_ep0_expect_setup(const struct usb_instance* data); |
| 289 | +extern void usb_ep0_start_rx(const struct usb_instance* data, bool non_setup, int len, bool (*callback)(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft)); |
| 290 | +extern void usb_ep0_start_tx(const struct usb_instance* data, const void* buf, int len, bool (*callback)(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft)); |
292 | 291 | extern void usb_start_rx(const struct usb_instance* data, union usb_endpoint_number ep, void* buf, int size); |
293 | 292 | extern void usb_start_tx(const struct usb_instance* data, union usb_endpoint_number ep, const void* buf, int size); |
294 | | -extern void usb_set_stall(const struct usb_instance* data, union usb_endpoint_number ep, int stall); |
| 293 | +extern void usb_set_stall(const struct usb_instance* data, union usb_endpoint_number ep, bool stall); |
295 | 294 | extern void usb_configure_ep(const struct usb_instance* data, union usb_endpoint_number ep, enum usb_endpoint_type type, int maxpacket); |
296 | 295 | extern void usb_unconfigure_ep(const struct usb_instance* data, union usb_endpoint_number ep); |
297 | 296 | extern int usb_get_max_transfer_size(const struct usb_instance* data, union usb_endpoint_number ep); |
| 297 | +extern bool usb_ep0_tx_callback(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft); |
| 298 | +extern bool usb_ep0_short_tx_callback(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft); |
| 299 | +extern bool usb_ep0_ack_callback(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft); |
298 | 300 | |
299 | 301 | |
300 | 302 | #endif |
Index: umsboot/src/protocol/usb/usb.c |
— | — | @@ -12,28 +12,34 @@ |
13 | 13 | data->driver->start_tx(data, ep, buf, size); |
14 | 14 | } |
15 | 15 | |
16 | | -void usb_set_stall(const struct usb_instance* data, union usb_endpoint_number ep, int stall) |
| 16 | +void usb_set_stall(const struct usb_instance* data, union usb_endpoint_number ep, bool stall) |
17 | 17 | { |
18 | 18 | data->driver->set_stall(data, ep, stall); |
19 | 19 | } |
20 | 20 | |
21 | | -void usb_ep0_start_rx(const struct usb_instance* data, int non_setup, bool (*callback)(const struct usb_instance* data, int bytesleft)) |
| 21 | +void usb_ep0_start_rx(const struct usb_instance* data, bool non_setup, int len, bool (*callback)(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft)) |
22 | 22 | { |
23 | 23 | data->state->ep0_rx_callback = callback; |
24 | | - data->driver->ep0_start_rx(data, non_setup); |
| 24 | + data->driver->ep0_start_rx(data, non_setup, len); |
25 | 25 | } |
26 | 26 | |
27 | | -bool usb_ep0_rx_callback(const struct usb_instance* data, int bytesleft) |
| 27 | +bool usb_ep0_ack_callback(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft) |
28 | 28 | { |
29 | | - usb_ep0_start_rx(data, 0, NULL); |
| 29 | + // This function is intended to eat zero-byte ACK packets, |
| 30 | + // which are always the last packet of a transaction. |
| 31 | + // At this point we can STALL all further packets. |
| 32 | + union usb_endpoint_number ep = { .number = 0, .direction = USB_ENDPOINT_DIRECTION_OUT }; |
| 33 | + usb_set_stall(data, ep, true); |
| 34 | + ep.direction = USB_ENDPOINT_DIRECTION_IN; |
| 35 | + usb_set_stall(data, ep, true); |
| 36 | + // If this is an IN endpoint, something else must already be |
| 37 | + // waiting for a SETUP packet anyway, or there could be deadlocks. |
| 38 | + if (epnum.direction == USB_ENDPOINT_DIRECTION_OUT) usb_ep0_start_rx(data, false, 0, NULL); |
30 | 39 | return true; |
31 | 40 | } |
32 | 41 | |
33 | | -void usb_ep0_start_tx(const struct usb_instance* data, const void* buf, int len, bool last, bool (*callback)(const struct usb_instance* data, int bytesleft)) |
| 42 | +void usb_ep0_start_tx(const struct usb_instance* data, const void* buf, int len, bool (*callback)(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft)) |
34 | 43 | { |
35 | | - // Expect zero-length ACK if we are about to actually send data, otherwise expect SETUP. |
36 | | - if (last) usb_ep0_start_rx(data, !!len, usb_ep0_rx_callback); |
37 | | - |
38 | 44 | if (((uint32_t)buf) & (CACHEALIGN_SIZE - 1)) |
39 | 45 | { |
40 | 46 | memcpy(data->buffer->raw, buf, len); |
— | — | @@ -44,13 +50,35 @@ |
45 | 51 | data->driver->ep0_start_tx(data, buf, len); |
46 | 52 | } |
47 | 53 | |
48 | | -bool usb_ep0_tx_callback(const struct usb_instance* data, int bytesleft) |
| 54 | +bool usb_ep0_short_tx_callback(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft) |
49 | 55 | { |
50 | | - if (bytesleft || !data->state->ep0_tx_len) return false; |
| 56 | + // No more data to be sent after a short packet. STALL IN. |
| 57 | + union usb_endpoint_number ep = { .number = 0, .direction = USB_ENDPOINT_DIRECTION_IN }; |
| 58 | + usb_set_stall(data, ep, true); |
| 59 | + // If this was the last regular packet, but a short one, expect zero-length ACK. |
| 60 | + // Otherwise usb_ep0_tx_callback will have taken care of that already. |
| 61 | + if (data->state->ep0_tx_ptr) usb_ep0_start_rx(data, true, 0, usb_ep0_ack_callback); |
| 62 | + return false; |
| 63 | +} |
| 64 | + |
| 65 | +bool usb_ep0_tx_callback(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft) |
| 66 | +{ |
| 67 | + if (bytesleft || !data->state->ep0_tx_len) |
| 68 | + { |
| 69 | + // This was the last packet. Expect zero-length ACK. |
| 70 | + usb_ep0_start_rx(data, true, 0, usb_ep0_ack_callback); |
| 71 | + // If someone might expect to receive more data, send a zero-byte packet. |
| 72 | + if (!data->state->ep0_tx_len) usb_ep0_start_tx(data, NULL, 0, usb_ep0_short_tx_callback); |
| 73 | + // Abuse this as a marker for usb_ep0_short_tx_callback... |
| 74 | + data->state->ep0_tx_ptr = NULL; |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
51 | 78 | int len = MIN(64, data->state->ep0_tx_len); |
| 79 | + data->state->ep0_tx_len -= len; |
| 80 | + usb_ep0_start_tx(data, data->state->ep0_tx_ptr, len, |
| 81 | + len < 64 ? usb_ep0_short_tx_callback : usb_ep0_tx_callback); |
52 | 82 | data->state->ep0_tx_ptr += 64; |
53 | | - data->state->ep0_tx_len -= len; |
54 | | - usb_ep0_start_tx(data, data->state->ep0_tx_ptr, len, !data->state->ep0_tx_len, usb_ep0_tx_callback); |
55 | 83 | return true; |
56 | 84 | } |
57 | 85 | |
— | — | @@ -119,6 +147,7 @@ |
120 | 148 | static void usb_handle_ep0_setup(const struct usb_instance* data, union usb_ep0_buffer* buffer) |
121 | 149 | { |
122 | 150 | // size < -2: do nothing at all (dangerous, you need to take care of priming EP0 yourself!) |
| 151 | + // (this case is required for requests that have an OUT data stage) |
123 | 152 | // size == -2: send STALL |
124 | 153 | // size == -1: try to run default handler, or send STALL if none exists |
125 | 154 | // size == 0: send ACK |
— | — | @@ -275,12 +304,12 @@ |
276 | 305 | break; |
277 | 306 | case USB_SETUP_BREQUEST_CLEAR_FEATURE: |
278 | 307 | if (buffer->setup.wLength || buffer->setup.wValue) break; |
279 | | - data->driver->set_stall(data, ep, false); |
| 308 | + usb_set_stall(data, ep, false); |
280 | 309 | size = 0; |
281 | 310 | break; |
282 | 311 | case USB_SETUP_BREQUEST_SET_FEATURE: |
283 | 312 | if (buffer->setup.wLength || buffer->setup.wValue) break; |
284 | | - data->driver->set_stall(data, ep, true); |
| 313 | + usb_set_stall(data, ep, true); |
285 | 314 | size = 0; |
286 | 315 | break; |
287 | 316 | default: break; |
— | — | @@ -293,22 +322,30 @@ |
294 | 323 | default: break; |
295 | 324 | } |
296 | 325 | } |
297 | | - // See comment at the top of this function |
298 | | - if (size == 0) usb_ep0_start_tx(data, NULL, 0, true, NULL); |
| 326 | + union usb_endpoint_number ep0in = { .number = 0, .direction = USB_ENDPOINT_DIRECTION_IN }; |
| 327 | + union usb_endpoint_number ep0out = { .number = 0, .direction = USB_ENDPOINT_DIRECTION_OUT }; |
| 328 | + // See comment at the top of this function for meanings of size. |
| 329 | + if (size == 0) |
| 330 | + { |
| 331 | + // Send ACK. Stall OUT pipe. Accept SETUP packets though. |
| 332 | + usb_set_stall(data, ep0out, true); |
| 333 | + usb_ep0_start_rx(data, false, 0, NULL); |
| 334 | + usb_ep0_start_tx(data, NULL, 0, usb_ep0_ack_callback); |
| 335 | + } |
299 | 336 | else if (size > 0) |
300 | 337 | { |
301 | | - usb_ep0_start_rx(data, 0, NULL); |
302 | | - int len = MIN(64, size); |
| 338 | + // Send a data stage. Expect to receive only SETUP until we're done. NAK everything else. |
| 339 | + usb_ep0_start_rx(data, false, 0, NULL); |
303 | 340 | data->state->ep0_tx_ptr = addr; |
304 | | - data->state->ep0_tx_len = size - len; |
305 | | - usb_ep0_start_tx(data, addr, len, !data->state->ep0_tx_len, usb_ep0_tx_callback); |
| 341 | + data->state->ep0_tx_len = size; |
| 342 | + usb_ep0_tx_callback(data, ep0in, 0); // A convenient way to start a transfer. |
306 | 343 | } |
307 | 344 | else if (size >= -2) |
308 | 345 | { |
309 | | - union usb_endpoint_number ep = { .number = 0, .direction = USB_ENDPOINT_DIRECTION_IN }; |
310 | | - usb_set_stall(data, ep, 1); |
311 | | - ep.direction = USB_ENDPOINT_DIRECTION_OUT; |
312 | | - usb_set_stall(data, ep, 1); |
| 346 | + // We have no handler, or the handler failed. STALL everything, accept only SETUP packets. |
| 347 | + usb_set_stall(data, ep0in, true); |
| 348 | + usb_set_stall(data, ep0out, true); |
| 349 | + usb_ep0_start_rx(data, false, 0, NULL); |
313 | 350 | } |
314 | 351 | } |
315 | 352 | |
— | — | @@ -329,7 +366,7 @@ |
330 | 367 | } |
331 | 368 | |
332 | 369 | // Prime EP0 for the first setup packet. |
333 | | - usb_ep0_start_rx(data, 0, NULL); |
| 370 | + usb_ep0_start_rx(data, false, 0, NULL); |
334 | 371 | } |
335 | 372 | |
336 | 373 | void usb_handle_timeout(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft) |
— | — | @@ -350,7 +387,7 @@ |
351 | 388 | { |
352 | 389 | if (!epnum.number) |
353 | 390 | { |
354 | | - bool (*callback)(const struct usb_instance* data, int size); |
| 391 | + bool (*callback)(const struct usb_instance* data, union usb_endpoint_number epnum, int size); |
355 | 392 | if (epnum.direction == USB_ENDPOINT_DIRECTION_OUT) |
356 | 393 | { |
357 | 394 | callback = data->state->ep0_rx_callback; |
— | — | @@ -361,7 +398,7 @@ |
362 | 399 | callback = data->state->ep0_tx_callback; |
363 | 400 | data->state->ep0_tx_callback = NULL; |
364 | 401 | } |
365 | | - if (callback) callback(data, bytesleft); |
| 402 | + if (callback) callback(data, epnum, bytesleft); |
366 | 403 | } |
367 | 404 | else |
368 | 405 | { |