Skip to content

Commit f80b220

Browse files
jacobly0adriweb
authored andcommitted
Add remaining data structures.
1 parent fae2b1b commit f80b220

File tree

1 file changed

+71
-34
lines changed

1 file changed

+71
-34
lines changed

core/usb/usb.c

Lines changed: 71 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ typedef union usb_qlink {
2525
uint32_t val;
2626
bool term : 1;
2727
struct {
28-
usb_qtype_t : 1;
28+
int : 1;
2929
usb_qtype_t type : 2;
3030
};
3131
struct {
@@ -36,15 +36,19 @@ typedef union usb_qlink {
3636
} usb_qlink_t;
3737
typedef union usb_qbuf {
3838
uint32_t val;
39-
struct { uint32_t off : 12, ptr : 20; }; // index 0
40-
uint8_t c_prog_mask; // index 1
41-
struct { uint32_t frame_tag : 5, s_bytes : 7; }; // index 2
39+
struct { uint32_t off : 12, ptr : 20; }; // qtd/sitd index 0
40+
struct { uint8_t dev_addr : 7, : 1, endpt : 4; }; // itd index 0
41+
uint8_t c_prog_mask; // qtd index 1
42+
struct { uint8_t xact_count : 3, xact_pos : 2; }; // sitd index 1
43+
struct { uint16_t max_pkt_size : 11; bool io : 1; }; // itd index 1
44+
struct { uint32_t frame_tag : 5, s_bytes : 7; }; // qtd index 2
45+
uint8_t mult : 2; // itd index 2
4246
} usb_qbuf_t;
4347
typedef struct usb_qtd {
4448
usb_qlink_t next, alt;
4549
bool ping : 1, split : 1, missed : 1, xact_err : 1, babble : 1, buf_err : 1, halted : 1, active : 1;
46-
uint8_t pid : 2, cerr : 2, c_page : 3, ioc : 1;
47-
uint16_t total_bytes : 15, dt : 1;
50+
uint8_t pid : 2, cerr : 2, page : 3, ioc : 1;
51+
uint16_t length : 15, dt : 1;
4852
usb_qbuf_t bufs[5];
4953
} usb_qtd_t;
5054
typedef struct usb_qh {
@@ -55,20 +59,53 @@ typedef struct usb_qh {
5559
usb_qlink_t cur;
5660
usb_qtd_t overlay;
5761
} usb_qh_t;
62+
typedef struct usb_sitd {
63+
usb_qlink_t next;
64+
uint8_t dev_addr : 7, : 1, endpt : 4, : 4, hub_addr : 7, : 1, port_num : 7;
65+
bool io : 1;
66+
uint8_t s_mask, c_mask;
67+
uint16_t : 16;
68+
bool : 1, split : 1, missed : 1, xact_err : 1, babble : 1, buf_err : 1, err : 1, active : 1;
69+
uint8_t c_prog_mask;
70+
uint16_t length : 10, : 4, page : 1, ioc : 1;
71+
usb_qbuf_t bufs[2];
72+
usb_qlink_t back;
73+
} usb_sitd_t;
74+
typedef struct usb_itd {
75+
usb_qlink_t next;
76+
struct {
77+
uint16_t off : 12, page : 3, ioc : 1, length : 12, xact_err : 1, babble : 1, buf_err : 1, active : 1;
78+
} xacts[8];
79+
usb_qbuf_t bufs[7];
80+
} usb_itd_t;
5881
typedef struct usb_fstn {
5982
usb_qlink_t normal, back;
6083
} usb_fstn_t;
84+
6185
static_assert(sizeof(usb_qbuf_t) == 0x04, "usb_qbuf_t does not have a size of 0x04");
62-
static_assert(offsetof(usb_qtd_t, next) == 0x00, "Next qTD Pointer is not at offset 0x00");
63-
static_assert(offsetof(usb_qtd_t, alt) == 0x04, "Alternate Next qTD Pointer is not at offset 0x04");
64-
static_assert(offsetof(usb_qtd_t, bufs) == 0x0C, "Buffer Pointers are not at offset 0x0C");
86+
static_assert(offsetof(usb_qtd_t, next) == 0x00, "Next qTD Pointer is not at offset 0x00 of usb_qtd_t");
87+
static_assert(offsetof(usb_qtd_t, alt) == 0x04, "Alternate Next qTD Pointer is not at offset 0x04 of usb_qtd_t");
88+
static_assert(offsetof(usb_qtd_t, bufs) == 0x0C, "Buffer Pointers are not at offset 0x0C of usb_qtd_t");
6589
static_assert(sizeof(usb_qtd_t) == 0x20, "usb_qtd_t does not have a size of 0x20");
66-
static_assert(offsetof(usb_qh_t, horiz) == 0x00, "Queue Head Horizontal Link Pointer is not at offset 0x00");
67-
static_assert(offsetof(usb_qh_t, cur) == 0x0C, "Current qTD Pointer is not at offset 0x0C");
68-
static_assert(offsetof(usb_qh_t, overlay) == 0x10, "Transfer Overlay is not at offset 0x10");
90+
static_assert(offsetof(usb_qh_t, horiz) == 0x00, "Queue Head Horizontal Link Pointer is not at offset 0x00 of usb_qh_t");
91+
static_assert(offsetof(usb_qh_t, cur) == 0x0C, "Current qTD Pointer is not at offset 0x0C of usb_qh_t");
92+
static_assert(offsetof(usb_qh_t, s_mask) == 0x08, "µFrame S-mask is not at offset 0x08 of usb_qh_t");
93+
static_assert(offsetof(usb_qh_t, c_mask) == 0x09, "µFrame C-mask is not at offset 0x09 of usb_qh_t");
94+
static_assert(offsetof(usb_qh_t, overlay) == 0x10, "Transfer Overlay is not at offset 0x10 of usb_qh_t");
6995
static_assert(sizeof(usb_qh_t) == 0x30, "usb_qh_t does not have a size of 0x30");
70-
static_assert(offsetof(usb_fstn_t, normal) == 0x00, "Normal Path Link Pointer is not at offset 0x00");
71-
static_assert(offsetof(usb_fstn_t, back) == 0x04, "Back Path Link Pointer is not at offset 0x04");
96+
static_assert(offsetof(usb_sitd_t, next) == 0x00, "Next Link Pointer is not at offset 0x00 of usb_sitd_t");
97+
static_assert(offsetof(usb_sitd_t, s_mask) == 0x08, "µFrame S-mask is not at offset 0x08 of usb_sitd_t");
98+
static_assert(offsetof(usb_sitd_t, c_mask) == 0x09, "µFrame S-mask is not at offset 0x09 of usb_sitd_t");
99+
static_assert(offsetof(usb_sitd_t, c_prog_mask) == 0x0D, "µFrame C-prog-mask is not at offset 0x0D of usb_sitd_t");
100+
static_assert(offsetof(usb_sitd_t, bufs) == 0x10, "Buffer Pointers are not at offset 0x10 of usb_sitd_t");
101+
static_assert(offsetof(usb_sitd_t, back) == 0x18, "Back Pointer is not at offset 0x18 of usb_sitd_t");
102+
static_assert(sizeof(usb_sitd_t) == 0x1C, "usb_sitd_t does not have a size of 0x1C");
103+
static_assert(offsetof(usb_itd_t, next) == 0x00, "Next Link Pointer is not at offset 0x00 of usb_itd_t");
104+
static_assert(offsetof(usb_itd_t, xacts) == 0x04, "Transaction Infos are not at offset 0x04 of usb_itd_t");
105+
static_assert(offsetof(usb_itd_t, bufs) == 0x24, "Buffer Pointers are not at offset 0x24 of usb_itd_t");
106+
static_assert(sizeof(usb_itd_t) == 0x40, "usb_itd_t does not have a size of 0x40");
107+
static_assert(offsetof(usb_fstn_t, normal) == 0x00, "Normal Path Link Pointer is not at offset 0x00 of usb_fstn_t");
108+
static_assert(offsetof(usb_fstn_t, back) == 0x04, "Back Path Link Pointer is not at offset 0x04 of usb_fstn_t");
72109
static_assert(sizeof(usb_fstn_t) == 0x08, "usb_fstn_t does not have a size of 0x08");
73110

74111
typedef struct usb_traversal_state {
@@ -224,7 +261,7 @@ static void usb_write_back_qtd(usb_qh_t *qh) {
224261
qtd.halted = qh->overlay.halted;
225262
qtd.active = qh->overlay.active;
226263
qtd.cerr = qh->overlay.cerr;
227-
qtd.total_bytes = qh->overlay.total_bytes;
264+
qtd.length = qh->overlay.length;
228265
mem_dma_write(&qtd, qh->cur.ptr << 5, sizeof(qtd));
229266
// TODO: defer these interrupts?
230267
if (qh->overlay.halted) {
@@ -245,57 +282,57 @@ static void usb_qh_halted(usb_qh_t *qh) {
245282

246283
static bool usb_qtd_gather(uint8_t *dst, usb_qtd_t *qtd, uint32_t *len) {
247284
uint16_t block_len;
248-
if (qtd->total_bytes > 0x5000) {
285+
if (qtd->length > 0x5000) {
249286
return true;
250287
}
251288
if (qtd->pid & 1) {
252-
*len -= qtd->total_bytes;
289+
*len -= qtd->length;
253290
return false;
254291
}
255-
while (qtd->total_bytes && *len) {
256-
if (qtd->c_page >= 5) {
292+
while (qtd->length && *len) {
293+
if (qtd->page >= 5) {
257294
return true;
258295
}
259296
block_len = (1 << 12) - qtd->bufs[0].off;
260-
if (block_len > qtd->total_bytes) {
261-
block_len = qtd->total_bytes;
297+
if (block_len > qtd->length) {
298+
block_len = qtd->length;
262299
}
263300
if (block_len > *len) {
264301
block_len = *len;
265302
}
266-
mem_dma_read(dst, qtd->bufs[qtd->c_page].ptr << 12 | qtd->bufs[0].off, block_len);
303+
mem_dma_read(dst, qtd->bufs[qtd->page].ptr << 12 | qtd->bufs[0].off, block_len);
267304
dst += block_len;
268305
qtd->bufs[0].off += block_len;
269-
qtd->c_page += !qtd->bufs[0].off;
270-
qtd->total_bytes -= block_len;
306+
qtd->page += !qtd->bufs[0].off;
307+
qtd->length -= block_len;
271308
*len -= block_len;
272309
}
273-
return qtd->total_bytes;
310+
return qtd->length;
274311
}
275312
static bool usb_qtd_scatter(usb_qtd_t *qtd, const uint8_t *src, uint32_t *len) {
276313
uint16_t block_len;
277-
if (qtd->total_bytes > 0x5000) {
314+
if (qtd->length > 0x5000) {
278315
return true;
279316
}
280317
if (!(qtd->pid & 1)) {
281318
return false;
282319
}
283-
while (qtd->total_bytes && *len) {
284-
if (qtd->c_page >= 5) {
320+
while (qtd->length && *len) {
321+
if (qtd->page >= 5) {
285322
return true;
286323
}
287324
block_len = (1 << 12) - qtd->bufs[0].off;
288-
if (block_len > qtd->total_bytes) {
289-
block_len = qtd->total_bytes;
325+
if (block_len > qtd->length) {
326+
block_len = qtd->length;
290327
}
291328
if (block_len > *len) {
292329
block_len = *len;
293330
}
294-
mem_dma_write(src, qtd->bufs[qtd->c_page].ptr << 12 | qtd->bufs[0].off, block_len);
331+
mem_dma_write(src, qtd->bufs[qtd->page].ptr << 12 | qtd->bufs[0].off, block_len);
295332
src += block_len;
296333
qtd->bufs[0].off += block_len;
297-
qtd->c_page += !qtd->bufs[0].off;
298-
qtd->total_bytes -= block_len;
334+
qtd->page += !qtd->bufs[0].off;
335+
qtd->length -= block_len;
299336
*len -= block_len;
300337
}
301338
return *len;
@@ -525,7 +562,7 @@ static void usb_qh_advance(usb_traversal_state_t *state) {
525562
}
526563
usb_qlink_t link;
527564
link.term = true;
528-
if (state->qh.overlay.total_bytes) {
565+
if (state->qh.overlay.length) {
529566
link = state->qh.overlay.alt;
530567
}
531568
if (link.term) {

0 commit comments

Comments
 (0)