interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / aienux / crates / aienos-kernel / usb_xhci.om

1711 lines · 42404 bytes Raw Blame History
1note USB and xHCI stack for aienos-kernel in pure OM-2
2note Ported from crates/aienos-kernel/src/usb/{mod,descriptor,hid,audio}.rs
3note and crates/aienos-kernel/src/usb/xhci/{mod,trb,ring,context,controller}.rs
4note Library unit: composed with si/directives/om2.om; no om2 header, no fn main.
5
6note === Structs (must precede all fn declarations; <=16 fields, <=64 cells) ===
7
8struct KuxBootKbd ok bool cfg uchar iface uchar ep_addr uchar max_pkt ushort ival uchar end
9
10struct KuxKeyEvent kind uchar ch uchar end
11
12struct KuxKbdDec prev uchar 6 end
13
14struct KuxTextSink buf uchar 48 cnt ulong cap ulong end
15
16struct KuxAudioStream ok bool ver uchar ctrl_if uchar iface uchar alt uchar ep_addr uchar max_pkt ushort ival uchar chans uchar subframe uchar bit_res uchar has_clk bool clk_src uchar rates uint 8 rate_cnt uchar end
17
18struct KuxPcmFramer ok bool rate uint chans uchar sbytes uchar ival_us uint rem_val ulong end
19
20struct KuxTestTone ok bool phase uint step uint end
21
22struct KuxCaps ok bool op_off ulong rt_off ulong db_off ulong max_slots uchar max_ports uchar scratch ushort addr64 bool ctx_bytes ulong end
23
24struct KuxTrb w uint 4 end
25
26struct KuxSetup req_type uchar req uchar val ushort idx ushort wlen ushort end
27
28struct KuxProdRing ok bool ring_len ulong dma ulong enq ulong cycle bool end
29
30struct KuxEventRing ok bool ring_len ulong dma ulong deq ulong cycle bool end
31
32struct KuxEpCfg ep_type uchar max_pkt ushort max_burst uchar ival uchar deq ulong avg_trb ushort max_esit ushort end
33
34struct KuxInCtx ok bool stride ulong end
35
36struct KuxDeadline start ulong ticks ulong polls uint end
37
38struct KuxCtrlState state uchar err uchar err_code uchar ctx_bytes ulong slot uchar dci uchar port uchar ep_addr uchar has_dev bool retire_pend bool is_halted bool last_ev uint 4 db_cnt uint erdp_cnt uint last_erdp ulong end
39
40note === Text tables for HID boot keyboard ===
41
42text kux_keymap "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ1!2@3#4$5%6^7&8*9(0)"
43text kux_punct " -_=+[{]}\\|#~;:'\"`~,<.>/?"
44
45note === USB Descriptor constants ===
46
47const KUX_DESC_DEVICE uchar 1
48const KUX_DESC_CONFIG uchar 2
49const KUX_DESC_IFACE uchar 4
50const KUX_DESC_ENDPOINT uchar 5
51const KUX_DESC_CS_IFACE uchar 0x24
52const KUX_DESC_CS_EP uchar 0x25
53const KUX_CLASS_HID uchar 3
54const KUX_SUBCLASS_BOOT uchar 1
55const KUX_PROTO_KBD uchar 1
56const KUX_XFER_INTERRUPT uchar 3
57
58note === HID Boot Keyboard constants ===
59
60const KUX_MOD_LCTRL uchar 1
61const KUX_MOD_LSHIFT uchar 2
62const KUX_MOD_LALT uchar 4
63const KUX_MOD_RCTRL uchar 16
64const KUX_MOD_RSHIFT uchar 32
65const KUX_MOD_RALT uchar 64
66const KUX_MOD_ANY_SHIFT uchar 34
67const KUX_BOOT_REPORT_LEN ulong 8
68const KUX_MAX_KEYS ulong 6
69
70const KUX_KEY_NONE uchar 0
71const KUX_KEY_CHAR uchar 1
72const KUX_KEY_ENTER uchar 2
73const KUX_KEY_BACKSPACE uchar 3
74const KUX_KEY_TAB uchar 4
75const KUX_KEY_ESCAPE uchar 5
76const KUX_KEY_UP uchar 6
77const KUX_KEY_DOWN uchar 7
78const KUX_KEY_LEFT uchar 8
79const KUX_KEY_RIGHT uchar 9
80
81const KUX_USAGE_ENTER uchar 0x28
82const KUX_USAGE_ESCAPE uchar 0x29
83const KUX_USAGE_BACKSPACE uchar 0x2a
84const KUX_USAGE_TAB uchar 0x2b
85const KUX_USAGE_CAPS_LOCK uchar 0x39
86const KUX_USAGE_F1 uchar 0x3a
87const KUX_USAGE_F12 uchar 0x45
88const KUX_USAGE_RIGHT_ARROW uchar 0x4f
89const KUX_USAGE_LEFT_ARROW uchar 0x50
90const KUX_USAGE_DOWN_ARROW uchar 0x51
91const KUX_USAGE_UP_ARROW uchar 0x52
92
93note === USB Audio Class constants ===
94
95const KUX_AUDIO_NONE uchar 0
96const KUX_AUDIO_UAC1 uchar 1
97const KUX_AUDIO_UAC2 uchar 2
98
99note === xHCI PCI / MMIO / Register constants ===
100
101const KUX_XHCI_CLASS_CODE uint 0x0c0330
102const KUX_PCI_CMD_MEMORY ushort 0x0002
103const KUX_PCI_CMD_BUS_MASTER ushort 0x0004
104
105const KUX_OP_USBCMD ulong 0x00
106const KUX_OP_USBSTS ulong 0x04
107const KUX_OP_CRCR ulong 0x18
108const KUX_OP_DCBAAP ulong 0x30
109const KUX_OP_CONFIG ulong 0x38
110const KUX_USBCMD_RUN uint 1
111const KUX_USBCMD_RESET uint 2
112const KUX_USBSTS_HALTED uint 1
113const KUX_USBSTS_HOST_ERROR uint 4
114const KUX_USBSTS_NOT_READY uint 0x800
115
116const KUX_RT_IMAN ulong 0x20
117const KUX_RT_ERSTSZ ulong 0x28
118const KUX_RT_ERSTBA ulong 0x30
119const KUX_RT_ERDP ulong 0x38
120const KUX_RT_ERDP_BUSY ulong 8
121
122const KUX_PORTSC_CONNECTED uint 1
123const KUX_PORTSC_ENABLED uint 2
124const KUX_PORTSC_RESET uint 16
125const KUX_PORTSC_POWER uint 0x200
126const KUX_PORTSC_RESET_CHANGE uint 0x200000
127const KUX_PORTSC_CHANGES uint 0xfe0000
128const KUX_PORTSC_READ_ONLY uint 0x40003c09
129const KUX_PORTSC_PRESERVED uint 0x0e00c3e0
130
131note === xHCI TRB constants ===
132
133const KUX_TRB_NORMAL uchar 1
134const KUX_TRB_SETUP_STAGE uchar 2
135const KUX_TRB_DATA_STAGE uchar 3
136const KUX_TRB_STATUS_STAGE uchar 4
137const KUX_TRB_ISOCH uchar 5
138const KUX_TRB_LINK uchar 6
139const KUX_TRB_ENABLE_SLOT uchar 9
140const KUX_TRB_DISABLE_SLOT uchar 10
141const KUX_TRB_ADDRESS_DEVICE uchar 11
142const KUX_TRB_CONFIGURE_EP uchar 12
143const KUX_TRB_EVALUATE_CTX uchar 13
144const KUX_TRB_TRANSFER_EVENT uchar 32
145const KUX_TRB_CMD_COMPLETION uchar 33
146const KUX_TRB_PORT_STATUS_CHG uchar 34
147
148const KUX_CC_SUCCESS uchar 1
149const KUX_CC_SHORT_PACKET uchar 13
150
151const KUX_TRB_CYCLE uint 1
152const KUX_TRB_TOGGLE_CYCLE uint 2
153const KUX_TRB_ISP uint 4
154const KUX_TRB_CHAIN uint 16
155const KUX_TRB_IOC uint 32
156const KUX_TRB_IDT uint 64
157const KUX_TRB_DIR_IN uint 0x10000
158const KUX_TRB_TRT_OUT uint 0x20000
159const KUX_TRB_TRT_IN uint 0x30000
160const KUX_TRB_SIA uint 0x80000000
161
162const KUX_REQ_GET_DESC uchar 0x06
163const KUX_REQ_SET_CFG uchar 0x09
164const KUX_REQ_HID_SET_IDLE uchar 0x0a
165const KUX_REQ_HID_SET_PROTO uchar 0x0b
166
167const KUX_TRB_BYTES ulong 16
168
169note === xHCI Context constants ===
170
171const KUX_SPEED_FULL uchar 1
172const KUX_SPEED_LOW uchar 2
173const KUX_SPEED_HIGH uchar 3
174const KUX_SPEED_SUPER uchar 4
175
176const KUX_EP_ISOCH_OUT uchar 1
177const KUX_EP_CONTROL uchar 4
178const KUX_EP_INTR_IN uchar 7
179const KUX_CERR uint 3
180const KUX_CONTROL_DCI uchar 1
181const KUX_MAX_DCI uchar 31
182
183note === xHCI Controller State Machine constants ===
184
185const KUX_ST_INIT uchar 0
186const KUX_ST_HALTED uchar 1
187const KUX_ST_RESET_DONE uchar 2
188const KUX_ST_RUNNING uchar 3
189const KUX_ST_ATTACHED uchar 4
190const KUX_ST_ERROR uchar 5
191
192const KUX_ERR_NONE uchar 0
193const KUX_ERR_ABSENT uchar 1
194const KUX_ERR_UNSUPPORTED uchar 2
195const KUX_ERR_TIMEOUT uchar 3
196const KUX_ERR_HOST_SYS uchar 4
197const KUX_ERR_CMD_TIMEOUT uchar 5
198const KUX_ERR_FAILED uchar 6
199const KUX_ERR_NO_KEYBOARD uchar 7
200
201const KUX_EV_IGNORED uchar 0
202const KUX_EV_OTHER uchar 1
203const KUX_EV_HALTED uchar 2
204const KUX_EV_REPORT_READY uchar 3
205const KUX_EV_SHORT_REARM uchar 4
206
207note ========================================================================
208note 1. Byte helpers and USB descriptor parsing (usb/descriptor.rs)
209note ========================================================================
210
211fn kux_u16_le lo uchar hi uchar to ushort
212 ret or ushort lo shl ushort hi 8
213end
214
215fn kux_le24 b view uchar * off ulong to uint
216 b0 = uint get b off
217 b1 = shl uint get b add off 1 8
218 b2 = shl uint get b add off 2 16
219 ret or b0 or b1 b2
220end
221
222fn kux_control_max_packet desc view uchar * to uchar
223 if lt len desc 8
224 ret uchar 0
225 end
226 dlen = get desc 0
227 dtype = get desc 1
228 sz = get desc 7
229 if lt dlen 8
230 ret uchar 0
231 end
232 if ne dtype KUX_DESC_DEVICE
233 ret uchar 0
234 end
235 if eq sz 0
236 ret uchar 0
237 end
238 ret sz
239end
240
241fn kux_try_kbd_ep bytes view uchar * pos ulong res edit KuxBootKbd to bool
242 ep = get bytes add pos 2
243 if eq and ep uchar 0x80 uchar 0
244 ret false
245 end
246 attr = and get bytes add pos 3 uchar 3
247 if ne attr KUX_XFER_INTERRUPT
248 ret false
249 end
250 mp = and kux_u16_le get bytes add pos 4 get bytes add pos 5 ushort 0x7ff
251 put res ok true
252 put res ep_addr ep
253 put res max_pkt mp
254 put res ival get bytes add pos 6
255 ret true
256end
257
258fn kux_find_boot_keyboard bytes view uchar * res edit KuxBootKbd to bool
259 put res ok false
260 put res cfg uchar 0
261 put res iface uchar 0
262 put res ep_addr uchar 0
263 put res max_pkt ushort 0
264 put res ival uchar 0
265 if lt len bytes 9
266 ret false
267 end
268 if lt get bytes 0 9
269 ret false
270 end
271 if ne get bytes 1 KUX_DESC_CONFIG
272 ret false
273 end
274 total = ulong kux_u16_le get bytes 2 get bytes 3
275 if gt total len bytes
276 total = len bytes
277 end
278 put res cfg get bytes 5
279 has_if = false
280 pos = ulong 0
281 note Each pass advances pos by at least 2 bytes; 65536-byte descriptor cap -> 32768 passes.
282 while le add pos 2 total bound 32768
283 dlen = ulong get bytes pos
284 if lt dlen 2
285 ret false
286 end
287 if gt add pos dlen total
288 ret false
289 end
290 dtype = get bytes add pos 1
291 if eq dtype KUX_DESC_IFACE
292 if ge dlen 9
293 has_if = false
294 if eq get bytes add pos 5 KUX_CLASS_HID
295 if eq get bytes add pos 6 KUX_SUBCLASS_BOOT
296 if eq get bytes add pos 7 KUX_PROTO_KBD
297 has_if = true
298 put res iface get bytes add pos 2
299 end
300 end
301 end
302 end
303 else
304 if eq dtype KUX_DESC_ENDPOINT
305 if ge dlen 7
306 if has_if
307 if kux_try_kbd_ep bytes pos res
308 ret true
309 end
310 end
311 end
312 end
313 end
314 pos = add pos dlen
315 end
316 ret false
317end
318
319note ========================================================================
320note 2. HID boot-protocol keyboard decoder and TextSink (usb/hid.rs)
321note ========================================================================
322
323fn kux_decode_usage usage uchar shift bool ev edit KuxKeyEvent to bool
324 put ev kind KUX_KEY_NONE
325 put ev ch uchar 0
326 sh = ulong 0
327 if shift
328 sh = ulong 1
329 end
330 if ge usage uchar 0x04
331 if le usage uchar 0x27
332 idx = add mul ulong sub usage uchar 0x04 2 sh
333 put ev kind KUX_KEY_CHAR
334 put ev ch get kux_keymap idx
335 ret true
336 end
337 end
338 if eq usage KUX_USAGE_ENTER
339 put ev kind KUX_KEY_ENTER
340 ret true
341 end
342 if eq usage KUX_USAGE_ESCAPE
343 put ev kind KUX_KEY_ESCAPE
344 ret true
345 end
346 if eq usage KUX_USAGE_BACKSPACE
347 put ev kind KUX_KEY_BACKSPACE
348 ret true
349 end
350 if eq usage KUX_USAGE_TAB
351 put ev kind KUX_KEY_TAB
352 ret true
353 end
354 if ge usage uchar 0x2c
355 if le usage uchar 0x38
356 idx = add mul ulong sub usage uchar 0x2c 2 sh
357 put ev kind KUX_KEY_CHAR
358 put ev ch get kux_punct idx
359 ret true
360 end
361 end
362 if eq usage KUX_USAGE_CAPS_LOCK
363 ret false
364 end
365 if ge usage KUX_USAGE_F1
366 if le usage KUX_USAGE_F12
367 ret false
368 end
369 end
370 if eq usage KUX_USAGE_RIGHT_ARROW
371 put ev kind KUX_KEY_RIGHT
372 ret true
373 end
374 if eq usage KUX_USAGE_LEFT_ARROW
375 put ev kind KUX_KEY_LEFT
376 ret true
377 end
378 if eq usage KUX_USAGE_DOWN_ARROW
379 put ev kind KUX_KEY_DOWN
380 ret true
381 end
382 if eq usage KUX_USAGE_UP_ARROW
383 put ev kind KUX_KEY_UP
384 ret true
385 end
386 ret false
387end
388
389fn kux_sink_init sink edit KuxTextSink cap ulong to void
390 c = cap
391 if gt c 48
392 c = ulong 48
393 end
394 put sink cnt ulong 0
395 put sink cap c
396 for i ulong 48
397 put sink buf i uchar 0
398 end
399end
400
401fn kux_sink_push sink edit KuxTextSink b uchar to void
402 n = get sink cnt
403 if lt n get sink cap
404 put sink buf n b
405 put sink cnt add n 1
406 end
407end
408
409fn kux_sink_record sink edit KuxTextSink ev view KuxKeyEvent to void
410 k = get ev kind
411 if eq k KUX_KEY_CHAR
412 kux_sink_push sink get ev ch
413 ret
414 end
415 if eq k KUX_KEY_ENTER
416 kux_sink_push sink uchar 10
417 ret
418 end
419 if eq k KUX_KEY_BACKSPACE
420 kux_sink_push sink uchar 8
421 ret
422 end
423 if eq k KUX_KEY_TAB
424 kux_sink_push sink uchar 9
425 ret
426 end
427 if eq k KUX_KEY_ESCAPE
428 kux_sink_push sink uchar 0x1b
429 ret
430 end
431 if eq k KUX_KEY_UP
432 kux_sink_push sink uchar 0x1b
433 kux_sink_push sink uchar 91
434 kux_sink_push sink uchar 65
435 ret
436 end
437 if eq k KUX_KEY_DOWN
438 kux_sink_push sink uchar 0x1b
439 kux_sink_push sink uchar 91
440 kux_sink_push sink uchar 66
441 ret
442 end
443 if eq k KUX_KEY_RIGHT
444 kux_sink_push sink uchar 0x1b
445 kux_sink_push sink uchar 91
446 kux_sink_push sink uchar 67
447 ret
448 end
449 if eq k KUX_KEY_LEFT
450 kux_sink_push sink uchar 0x1b
451 kux_sink_push sink uchar 91
452 kux_sink_push sink uchar 68
453 ret
454 end
455end
456
457fn kux_kbd_init dec edit KuxKbdDec to void
458 for i ulong 6
459 put dec prev i uchar 0
460 end
461end
462
463fn kux_kbd_prev_has dec view KuxKbdDec usage uchar to bool
464 for i ulong 6
465 if eq get dec prev i usage
466 ret true
467 end
468 end
469 ret false
470end
471
472fn kux_kbd_handle_report dec edit KuxKbdDec report view uchar * sink edit KuxTextSink to ulong
473 ev = new KuxKeyEvent 0 0
474 if ne len report KUX_BOOT_REPORT_LEN
475 ret ulong 0
476 end
477 if eq get report 2 uchar 1
478 ret ulong 0
479 end
480 shift = ne and get report 0 KUX_MOD_ANY_SHIFT uchar 0
481 emitted = ulong 0
482 for i ulong 6
483 u = get report add i 2
484 if ne u uchar 0
485 if not kux_kbd_prev_has dec u
486 if kux_decode_usage u shift ev
487 kux_sink_record sink ev
488 emitted = add emitted 1
489 end
490 end
491 end
492 end
493 for i ulong 6
494 put dec prev i get report add i 2
495 end
496 ret emitted
497end
498
499note ========================================================================
500note 3. USB Audio Class output selection, PCM framing, Tone (usb/audio.rs)
501note ========================================================================
502
503fn kux_audio_clear s edit KuxAudioStream to void
504 put s ok false
505 put s ver KUX_AUDIO_NONE
506 put s ctrl_if uchar 0
507 put s iface uchar 0
508 put s alt uchar 0
509 put s ep_addr uchar 0
510 put s max_pkt ushort 0
511 put s ival uchar 0
512 put s chans uchar 0
513 put s subframe uchar 0
514 put s bit_res uchar 0
515 put s has_clk false
516 put s clk_src uchar 0
517 put s rate_cnt uchar 0
518 for i ulong 8
519 put s rates i uint 0
520 end
521end
522
523fn kux_audio_copy dst edit KuxAudioStream src view KuxAudioStream to void
524 put dst ok get src ok
525 put dst ver get src ver
526 put dst ctrl_if get src ctrl_if
527 put dst iface get src iface
528 put dst alt get src alt
529 put dst ep_addr get src ep_addr
530 put dst max_pkt get src max_pkt
531 put dst ival get src ival
532 put dst chans get src chans
533 put dst subframe get src subframe
534 put dst bit_res get src bit_res
535 put dst has_clk get src has_clk
536 put dst clk_src get src clk_src
537 put dst rate_cnt get src rate_cnt
538 for i ulong 8
539 put dst rates i get src rates i
540 end
541end
542
543fn kux_audio_matches_filter s view KuxAudioStream to bool
544 if lt get s chans uchar 2
545 ret false
546 end
547 if lt get s subframe uchar 2
548 ret false
549 end
550 if lt get s bit_res uchar 16
551 ret false
552 end
553 rc = ulong get s rate_cnt
554 if eq rc 0
555 ret true
556 end
557 if gt rc 8
558 rc = ulong 8
559 end
560 for i ulong rc bound 8
561 if eq get s rates i uint 48000
562 ret true
563 end
564 end
565 ret false
566end
567
568fn kux_audio_is_better cand view KuxAudioStream best view KuxAudioStream to bool
569 if not get best ok
570 ret true
571 end
572 c_ch = uint 0
573 if ne get cand chans uchar 2
574 c_ch = uint 1
575 end
576 b_ch = uint 0
577 if ne get best chans uchar 2
578 b_ch = uint 1
579 end
580 if lt c_ch b_ch
581 ret true
582 end
583 if gt c_ch b_ch
584 ret false
585 end
586 c_bit = uint 0
587 if ne get cand bit_res uchar 16
588 c_bit = uint 1
589 end
590 b_bit = uint 0
591 if ne get best bit_res uchar 16
592 b_bit = uint 1
593 end
594 if lt c_bit b_bit
595 ret true
596 end
597 if gt c_bit b_bit
598 ret false
599 end
600 ret lt get cand max_pkt get best max_pkt
601end
602
603fn kux_audio_parse_uac1_rates bytes view uchar * pos ulong dlen ulong cur edit KuxAudioStream to void
604 if lt dlen 8
605 ret
606 end
607 nr = ulong get bytes add pos 7
608 if eq nr 0
609 ret
610 end
611 if lt dlen add ulong 8 mul nr 3
612 ret
613 end
614 if gt nr 8
615 nr = ulong 8
616 end
617 put cur rate_cnt uchar and nr 255
618 for i ulong nr bound 8
619 off = add pos add ulong 8 mul i 3
620 put cur rates i kux_le24 bytes off
621 end
622end
623
624fn kux_audio_parse_fmt bytes view uchar * pos ulong dlen ulong ver uchar cur edit KuxAudioStream to void
625 if eq ver KUX_AUDIO_UAC2
626 if lt dlen 6
627 ret
628 end
629 put cur subframe get bytes add pos 4
630 put cur bit_res get bytes add pos 5
631 put cur rates 0 uint 48000
632 put cur rate_cnt uchar 1
633 for i ulong 7
634 put cur rates add i 1 uint 0
635 end
636 put cur ok true
637 ret
638 end
639 if lt dlen 8
640 ret
641 end
642 put cur chans get bytes add pos 4
643 put cur subframe get bytes add pos 5
644 put cur bit_res get bytes add pos 6
645 for i ulong 8
646 put cur rates i uint 0
647 end
648 put cur rate_cnt uchar 0
649 if eq ver KUX_AUDIO_UAC1
650 kux_audio_parse_uac1_rates bytes pos dlen cur
651 end
652 put cur ok true
653end
654
655fn kux_audio_try_ep bytes view uchar * pos ulong cur view KuxAudioStream best edit KuxAudioStream to bool
656 if not get cur ok
657 ret false
658 end
659 if eq get cur alt uchar 0
660 ret false
661 end
662 ep = get bytes add pos 2
663 if ne and ep uchar 0x80 uchar 0
664 ret false
665 end
666 if ne and get bytes add pos 3 uchar 3 uchar 1
667 ret false
668 end
669 cand = new KuxAudioStream false 0 0 0 0 0 0 0 0 0 0 false 0 0 0
670 kux_audio_copy cand cur
671 put cand ep_addr ep
672 mp = and kux_u16_le get bytes add pos 4 get bytes add pos 5 ushort 0x7ff
673 put cand max_pkt mp
674 put cand ival get bytes add pos 6
675 if eq get cand ver KUX_AUDIO_UAC2
676 if eq get cand rate_cnt uchar 1
677 put cand rates 0 uint 48000
678 end
679 end
680 if kux_audio_matches_filter cand
681 if kux_audio_is_better cand best
682 kux_audio_copy best cand
683 end
684 end
685 ret true
686end
687
688fn kux_audio_handle_cs bytes view uchar * pos ulong dlen ulong flags uchar cur edit KuxAudioStream to void
689 if lt dlen 3
690 ret
691 end
692 subt = get bytes add pos 2
693 is_as = ne and flags uchar 2 uchar 0
694 has_if = ne and flags uchar 1 uchar 0
695 if eq subt uchar 1
696 if is_as
697 if eq get cur ver KUX_AUDIO_UAC2
698 if ge dlen 11
699 put cur chans get bytes add pos 10
700 end
701 end
702 ret
703 end
704 if has_if
705 if ge dlen 8
706 bcd = kux_u16_le get bytes add pos 3 get bytes add pos 4
707 if ge bcd ushort 0x0200
708 put cur ver KUX_AUDIO_UAC2
709 put cur has_clk true
710 put cur clk_src get bytes add pos 7
711 else
712 put cur ver KUX_AUDIO_UAC1
713 end
714 end
715 end
716 ret
717 end
718 if eq subt uchar 2
719 if is_as
720 kux_audio_parse_fmt bytes pos dlen get cur ver cur
721 end
722 ret
723 end
724 if eq subt uchar 0x0a
725 if ge dlen 4
726 if eq get cur ver KUX_AUDIO_UAC2
727 put cur has_clk true
728 put cur clk_src get bytes add pos 3
729 end
730 end
731 end
732end
733
734fn kux_audio_select_output bytes view uchar * best edit KuxAudioStream to bool
735 cur = new KuxAudioStream false 0 0 0 0 0 0 0 0 0 0 false 0 0 0
736 kux_audio_clear best
737 if lt len bytes 9
738 ret false
739 end
740 if lt get bytes 0 9
741 ret false
742 end
743 if ne get bytes 1 KUX_DESC_CONFIG
744 ret false
745 end
746 total = ulong kux_u16_le get bytes 2 get bytes 3
747 if gt total len bytes
748 total = len bytes
749 end
750 flags = uchar 0
751 cand_n = ulong 0
752 pos = ulong 0
753 note Each pass advances pos by at least 2 bytes; 65536-byte descriptor cap -> 32768 passes.
754 while le add pos 2 total bound 32768
755 dlen = ulong get bytes pos
756 if lt dlen 2
757 kux_audio_clear best
758 ret false
759 end
760 if gt add pos dlen total
761 kux_audio_clear best
762 ret false
763 end
764 dtype = get bytes add pos 1
765 if eq dtype KUX_DESC_IFACE
766 if ge dlen 9
767 flags = or and flags uchar 4 uchar 1
768 if eq get bytes add pos 5 uchar 1
769 if eq get bytes add pos 6 uchar 2
770 flags = or flags uchar 2
771 end
772 if eq get bytes add pos 6 uchar 1
773 flags = or flags uchar 4
774 put cur ctrl_if get bytes add pos 2
775 end
776 end
777 put cur iface get bytes add pos 2
778 put cur alt get bytes add pos 3
779 put cur ok false
780 put cur chans uchar 0
781 put cur subframe uchar 0
782 put cur bit_res uchar 0
783 put cur rate_cnt uchar 0
784 end
785 else
786 if eq dtype KUX_DESC_CS_IFACE
787 kux_audio_handle_cs bytes pos dlen flags cur
788 else
789 if eq dtype KUX_DESC_ENDPOINT
790 if ge dlen 7
791 if eq and flags uchar 6 uchar 6
792 if ne get cur ver KUX_AUDIO_NONE
793 if lt cand_n 16
794 if kux_audio_try_ep bytes pos cur best
795 cand_n = add cand_n 1
796 end
797 end
798 end
799 end
800 end
801 end
802 end
803 end
804 pos = add pos dlen
805 end
806 ret get best ok
807end
808
809fn kux_service_interval_us high_speed bool ival uchar to uint
810 if eq ival uchar 0
811 ret uint 0
812 end
813 if high_speed
814 if gt ival uchar 16
815 ret uint 0
816 end
817 sh = uint sub ival uchar 1
818 ret shl uint 125 sh
819 end
820 ret uint 1000
821end
822
823fn kux_pcm_framer_init f edit KuxPcmFramer rate uint chans uchar sbytes uchar ival_us uint to bool
824 put f ok false
825 put f rate rate
826 put f chans chans
827 put f sbytes sbytes
828 put f ival_us ival_us
829 put f rem_val ulong 0
830 if eq rate uint 0
831 ret false
832 end
833 if eq chans uchar 0
834 ret false
835 end
836 if eq sbytes uchar 0
837 ret false
838 end
839 if eq ival_us uint 0
840 ret false
841 end
842 put f ok true
843 ret true
844end
845
846fn kux_pcm_peek_bytes f view KuxPcmFramer to ulong
847 num = add mul ulong get f rate ulong get f ival_us get f rem_val
848 frames = div num ulong 1000000
849 ret mul mul frames ulong get f chans ulong get f sbytes
850end
851
852fn kux_pcm_next_bytes f edit KuxPcmFramer to ulong
853 num = add mul ulong get f rate ulong get f ival_us get f rem_val
854 frames = div num ulong 1000000
855 put f rem_val rem num ulong 1000000
856 ret mul mul frames ulong get f chans ulong get f sbytes
857end
858
859fn kux_pcm_fill_i16 f edit KuxPcmFramer ring view short * cur_arr edit ulong 1 dst edit short * to ulong
860 bytes = kux_pcm_next_bytes f
861 samples = div bytes ulong 2
862 dlen = len dst
863 if gt samples dlen
864 samples = dlen
865 end
866 rlen = len ring
867 if eq rlen 0
868 for i ulong samples bound 65536
869 put dst i short 0
870 end
871 ret samples
872 end
873 c = get cur_arr 0
874 for i ulong samples bound 65536
875 put dst i get ring rem c rlen
876 c = rem add c 1 rlen
877 end
878 put cur_arr 0 c
879 ret samples
880end
881
882fn kux_isoch_mark_last trbs edit uint * count ulong to void
883 if eq count 0
884 ret
885 end
886 base = mul sub count 1 4
887 dw3 = get trbs add base 3
888 put trbs add base 3 or dw3 KUX_TRB_IOC
889end
890
891fn kux_tone_init t edit KuxTestTone freq_hz uint sample_rate uint to bool
892 put t ok false
893 put t phase uint 0
894 put t step uint 0
895 if eq sample_rate uint 0
896 ret false
897 end
898 if ge freq_hz div sample_rate uint 2
899 ret false
900 end
901 st = div shl ulong freq_hz 32 ulong sample_rate
902 put t step uint and st ulong 0xffffffff
903 put t ok true
904 ret true
905end
906
907fn kux_tone_next t edit KuxTestTone to short
908 ph = add get t phase get t step
909 put t phase ph
910 x = long shr ph 16
911 tri = x
912 if ge x long 32768
913 tri = sub long 65535 x
914 end
915 p1 = mul long 4 tri
916 p2 = mul p1 sub long 32767 tri
917 y = sub div mul p2 long 2 long 32767 long 32767
918 if lt y long -32767
919 y = long -32767
920 end
921 if gt y long 32767
922 y = long 32767
923 end
924 ret short y
925end
926
927note ========================================================================
928note 4. xHCI PCI / Capabilities / PORTSC (usb/xhci/mod.rs)
929note ========================================================================
930
931fn kux_op_portsc port uchar to ulong
932 ret add ulong 0x400 mul ulong 0x10 ulong sub port uchar 1
933end
934
935fn kux_xhci_mmio_base class_rev uint bar0_lo uint bar0_hi uint to ulong
936 if eq class_rev uint 0xffffffff
937 ret ulong 0
938 end
939 if ne shr class_rev 8 KUX_XHCI_CLASS_CODE
940 ret ulong 0
941 end
942 if eq bar0_lo uint 0xffffffff
943 ret ulong 0
944 end
945 btype = and bar0_lo uint 7
946 lo_mask = uint 0xfffffff0
947 base = ulong 0
948 if eq btype uint 0
949 base = ulong and bar0_lo lo_mask
950 else
951 if eq btype uint 4
952 base = or shl ulong bar0_hi 32 ulong and bar0_lo lo_mask
953 else
954 ret ulong 0
955 end
956 end
957 if eq base ulong 0
958 ret ulong 0
959 end
960 ret base
961end
962
963fn kux_caps_decode regs view uint 6 caps edit KuxCaps to bool
964 put caps ok false
965 if eq get regs 0 uint 0xffffffff
966 ret false
967 end
968 if lt and get regs 0 uint 0xff uint 0x20
969 ret false
970 end
971 if lt shr get regs 0 16 uint 0x0090
972 ret false
973 end
974 if eq and get regs 1 uint 0xff uint 0
975 ret false
976 end
977 if eq and shr get regs 1 24 uint 0xff uint 0
978 ret false
979 end
980 put caps op_off ulong and get regs 0 uint 0xff
981 put caps rt_off ulong and get regs 5 uint 0xffffffe0
982 put caps db_off ulong and get regs 4 uint 0xfffffffc
983 put caps max_slots uchar and get regs 1 uint 0xff
984 put caps max_ports uchar and shr get regs 1 24 uint 0xff
985 sp = or shl and shr get regs 2 21 uint 0x1f 5 and shr get regs 2 27 uint 0x1f
986 put caps scratch ushort and sp uint 0xffff
987 put caps addr64 ne and get regs 3 uint 1 uint 0
988 if ne and get regs 3 uint 4 uint 0
989 put caps ctx_bytes ulong 64
990 else
991 put caps ctx_bytes ulong 32
992 end
993 put caps ok true
994 ret true
995end
996
997fn kux_portsc_neutral val uint to uint
998 ret and val or KUX_PORTSC_READ_ONLY KUX_PORTSC_PRESERVED
999end
1000
1001fn kux_portsc_speed val uint to uchar
1002 ret uchar and shr val 10 uint 0xf
1003end
1004
1005note ========================================================================
1006note 5. xHCI TRBs and USB SetupPacket (usb/xhci/trb.rs)
1007note ========================================================================
1008
1009fn kux_setup_get_desc s edit KuxSetup dtype uchar idx uchar wlen ushort to void
1010 put s req_type uchar 0x80
1011 put s req KUX_REQ_GET_DESC
1012 put s val or shl ushort dtype 8 ushort idx
1013 put s idx ushort 0
1014 put s wlen wlen
1015end
1016
1017fn kux_setup_set_cfg s edit KuxSetup val uchar to void
1018 put s req_type uchar 0x00
1019 put s req KUX_REQ_SET_CFG
1020 put s val ushort val
1021 put s idx ushort 0
1022 put s wlen ushort 0
1023end
1024
1025fn kux_setup_set_boot_proto s edit KuxSetup iface uchar to void
1026 put s req_type uchar 0x21
1027 put s req KUX_REQ_HID_SET_PROTO
1028 put s val ushort 0
1029 put s idx ushort iface
1030 put s wlen ushort 0
1031end
1032
1033fn kux_setup_set_idle_forever s edit KuxSetup iface uchar to void
1034 put s req_type uchar 0x21
1035 put s req KUX_REQ_HID_SET_IDLE
1036 put s val ushort 0
1037 put s idx ushort iface
1038 put s wlen ushort 0
1039end
1040
1041fn kux_setup_is_in s view KuxSetup to bool
1042 ret ne and get s req_type uchar 0x80 uchar 0
1043end
1044
1045fn kux_setup_dw0 s view KuxSetup to uint
1046 ret or uint get s req_type or shl uint get s req 8 shl uint get s val 16
1047end
1048
1049fn kux_setup_dw1 s view KuxSetup to uint
1050 ret or uint get s idx shl uint get s wlen 16
1051end
1052
1053fn kux_trb_make t edit KuxTrb param ulong status uint kind uchar flags uint to void
1054 put t w 0 uint and param ulong 0xffffffff
1055 put t w 1 uint and shr param 32 ulong 0xffffffff
1056 put t w 2 status
1057 put t w 3 or shl uint kind 10 flags
1058end
1059
1060fn kux_trb_link t edit KuxTrb target ulong to void
1061 kux_trb_make t target uint 0 KUX_TRB_LINK KUX_TRB_TOGGLE_CYCLE
1062end
1063
1064fn kux_trb_enable_slot t edit KuxTrb to void
1065 kux_trb_make t ulong 0 uint 0 KUX_TRB_ENABLE_SLOT uint 0
1066end
1067
1068fn kux_trb_disable_slot t edit KuxTrb slot uchar to void
1069 kux_trb_make t ulong 0 uint 0 KUX_TRB_DISABLE_SLOT shl uint slot 24
1070end
1071
1072fn kux_trb_address_device t edit KuxTrb in_ctx ulong slot uchar to void
1073 kux_trb_make t in_ctx uint 0 KUX_TRB_ADDRESS_DEVICE shl uint slot 24
1074end
1075
1076fn kux_trb_configure_ep t edit KuxTrb in_ctx ulong slot uchar to void
1077 kux_trb_make t in_ctx uint 0 KUX_TRB_CONFIGURE_EP shl uint slot 24
1078end
1079
1080fn kux_trb_evaluate_ctx t edit KuxTrb in_ctx ulong slot uchar to void
1081 kux_trb_make t in_ctx uint 0 KUX_TRB_EVALUATE_CTX shl uint slot 24
1082end
1083
1084fn kux_trb_setup_stage t edit KuxTrb s view KuxSetup to void
1085 trt = uint 0
1086 if ne get s wlen ushort 0
1087 if kux_setup_is_in s
1088 trt = KUX_TRB_TRT_IN
1089 else
1090 trt = KUX_TRB_TRT_OUT
1091 end
1092 end
1093 put t w 0 kux_setup_dw0 s
1094 put t w 1 kux_setup_dw1 s
1095 put t w 2 uint 8
1096 put t w 3 or shl uint KUX_TRB_SETUP_STAGE 10 or KUX_TRB_IDT trt
1097end
1098
1099fn kux_trb_data_stage t edit KuxTrb s view KuxSetup buf ulong to void
1100 dir = uint 0
1101 if kux_setup_is_in s
1102 dir = KUX_TRB_DIR_IN
1103 end
1104 kux_trb_make t buf uint get s wlen KUX_TRB_DATA_STAGE dir
1105end
1106
1107fn kux_trb_status_stage t edit KuxTrb s view KuxSetup to void
1108 dir = KUX_TRB_DIR_IN
1109 if gt get s wlen ushort 0
1110 if kux_setup_is_in s
1111 dir = uint 0
1112 end
1113 end
1114 kux_trb_make t ulong 0 uint 0 KUX_TRB_STATUS_STAGE or dir KUX_TRB_IOC
1115end
1116
1117fn kux_trb_normal t edit KuxTrb buf ulong bytes uint to void
1118 kux_trb_make t buf and bytes uint 0x1ffff KUX_TRB_NORMAL or KUX_TRB_IOC KUX_TRB_ISP
1119end
1120
1121fn kux_trb_isoch t edit KuxTrb buf ulong bytes uint frame_id ushort sia bool is_last bool to void
1122 flags = shl uint and frame_id ushort 0x7ff 20
1123 if sia
1124 flags = or flags KUX_TRB_SIA
1125 end
1126 if is_last
1127 flags = or flags KUX_TRB_IOC
1128 end
1129 flags = and flags uint 0xfffffc7f
1130 kux_trb_make t buf and bytes uint 0x1ffff KUX_TRB_ISOCH flags
1131end
1132
1133fn kux_trb_kind t view KuxTrb to uchar
1134 ret uchar and shr get t w 3 10 uint 0x3f
1135end
1136
1137fn kux_trb_cycle t view KuxTrb to bool
1138 ret ne and get t w 3 KUX_TRB_CYCLE uint 0
1139end
1140
1141fn kux_trb_set_cycle t edit KuxTrb cyc bool to void
1142 v = and get t w 3 uint 0xfffffffe
1143 if cyc
1144 v = or v KUX_TRB_CYCLE
1145 end
1146 put t w 3 v
1147end
1148
1149fn kux_trb_cc t view KuxTrb to uchar
1150 ret uchar and shr get t w 2 24 uint 0xff
1151end
1152
1153fn kux_trb_slot_id t view KuxTrb to uchar
1154 ret uchar and shr get t w 3 24 uint 0xff
1155end
1156
1157fn kux_trb_ep_id t view KuxTrb to uchar
1158 ret uchar and shr get t w 3 16 uint 0x1f
1159end
1160
1161fn kux_trb_ptr t view KuxTrb to ulong
1162 ret or shl ulong get t w 1 32 ulong get t w 0
1163end
1164
1165fn kux_trb_residual t view KuxTrb to uint
1166 ret and get t w 2 uint 0x00ffffff
1167end
1168
1169fn kux_trb_completed_ok t view KuxTrb to bool
1170 cc = kux_trb_cc t
1171 if eq cc KUX_CC_SUCCESS
1172 ret true
1173 end
1174 if eq cc KUX_CC_SHORT_PACKET
1175 ret true
1176 end
1177 ret false
1178end
1179
1180note ========================================================================
1181note 6. xHCI ProducerRing and EventRing (usb/xhci/ring.rs)
1182note ========================================================================
1183
1184fn kux_ring_store_trb mem edit uint * idx ulong trb view KuxTrb to void
1185 base = mul idx 4
1186 put mem base get trb w 0
1187 put mem add base 1 get trb w 1
1188 put mem add base 2 get trb w 2
1189 put mem add base 3 get trb w 3
1190end
1191
1192fn kux_ring_load_trb mem view uint * idx ulong dst edit KuxTrb to void
1193 base = mul idx 4
1194 put dst w 0 get mem base
1195 put dst w 1 get mem add base 1
1196 put dst w 2 get mem add base 2
1197 put dst w 3 get mem add base 3
1198end
1199
1200fn kux_prod_ring_init r edit KuxProdRing mem edit uint * ring_len ulong dma ulong to bool
1201 lnk = new KuxTrb 0
1202 put r ok false
1203 put r ring_len ulong 0
1204 put r dma ulong 0
1205 put r enq ulong 0
1206 put r cycle false
1207 if lt ring_len 2
1208 ret false
1209 end
1210 words = mul ring_len 4
1211 if gt words len mem
1212 ret false
1213 end
1214 for i ulong words bound 65536
1215 put mem i uint 0
1216 end
1217 kux_trb_link lnk dma
1218 kux_ring_store_trb mem sub ring_len 1 lnk
1219 put r ok true
1220 put r ring_len ring_len
1221 put r dma dma
1222 put r enq ulong 0
1223 put r cycle true
1224 ret true
1225end
1226
1227fn kux_prod_ring_deq_ptr r view KuxProdRing to ulong
1228 p = add get r dma mul get r enq KUX_TRB_BYTES
1229 if get r cycle
1230 ret or p ulong 1
1231 end
1232 ret p
1233end
1234
1235fn kux_prod_ring_write_owned r view KuxProdRing mem edit uint * idx ulong trb view KuxTrb to void
1236 base = mul idx 4
1237 put mem base get trb w 0
1238 put mem add base 1 get trb w 1
1239 put mem add base 2 get trb w 2
1240 dw3 = and get trb w 3 uint 0xfffffffe
1241 if get r cycle
1242 dw3 = or dw3 KUX_TRB_CYCLE
1243 end
1244 put mem add base 3 dw3
1245end
1246
1247fn kux_prod_ring_push r edit KuxProdRing mem edit uint * trb view KuxTrb to ulong
1248 lnk = new KuxTrb 0
1249 idx = get r enq
1250 addr = add get r dma mul idx KUX_TRB_BYTES
1251 kux_prod_ring_write_owned r mem idx trb
1252 nxt = add idx 1
1253 put r enq nxt
1254 last = sub get r ring_len 1
1255 if eq nxt last
1256 kux_ring_load_trb mem last lnk
1257 kux_prod_ring_write_owned r mem last lnk
1258 put r enq ulong 0
1259 put r cycle not get r cycle
1260 end
1261 ret addr
1262end
1263
1264fn kux_prod_ring_push_isoch_one r edit KuxProdRing mem edit uint * trb view KuxTrb is_last bool to ulong
1265 tmp = new KuxTrb 0
1266 put tmp w 0 get trb w 0
1267 put tmp w 1 get trb w 1
1268 put tmp w 2 get trb w 2
1269 dw3 = and get trb w 3 uint 0xffffffcf
1270 if is_last
1271 dw3 = or dw3 KUX_TRB_IOC
1272 else
1273 dw3 = or dw3 KUX_TRB_CHAIN
1274 end
1275 put tmp w 3 dw3
1276 ret kux_prod_ring_push r mem tmp
1277end
1278
1279fn kux_event_ring_init r edit KuxEventRing mem edit uint * ring_len ulong dma ulong to bool
1280 put r ok false
1281 put r ring_len ulong 0
1282 put r dma ulong 0
1283 put r deq ulong 0
1284 put r cycle false
1285 if eq ring_len 0
1286 ret false
1287 end
1288 if gt ring_len 65535
1289 ret false
1290 end
1291 words = mul ring_len 4
1292 if gt words len mem
1293 ret false
1294 end
1295 for i ulong words bound 262144
1296 put mem i uint 0
1297 end
1298 put r ok true
1299 put r ring_len ring_len
1300 put r dma dma
1301 put r deq ulong 0
1302 put r cycle true
1303 ret true
1304end
1305
1306fn kux_event_ring_erst r view KuxEventRing entry edit uint 4 to void
1307 dma = get r dma
1308 put entry 0 uint and dma ulong 0xffffffff
1309 put entry 1 uint and shr dma 32 ulong 0xffffffff
1310 put entry 2 uint and get r ring_len ulong 0xffffffff
1311 put entry 3 uint 0
1312end
1313
1314fn kux_event_ring_deq_ptr r view KuxEventRing to ulong
1315 ret add get r dma mul get r deq KUX_TRB_BYTES
1316end
1317
1318fn kux_event_ring_pop r edit KuxEventRing mem view uint * dst edit KuxTrb to bool
1319 base = mul get r deq 4
1320 ctrl = get mem add base 3
1321 cyc = ne and ctrl KUX_TRB_CYCLE uint 0
1322 if ne cyc get r cycle
1323 ret false
1324 end
1325 put dst w 0 get mem base
1326 put dst w 1 get mem add base 1
1327 put dst w 2 get mem add base 2
1328 put dst w 3 ctrl
1329 nxt = add get r deq 1
1330 if eq nxt get r ring_len
1331 put r deq ulong 0
1332 put r cycle not get r cycle
1333 else
1334 put r deq nxt
1335 end
1336 ret true
1337end
1338
1339note ========================================================================
1340note 7. xHCI Slot / Endpoint / Input Contexts (usb/xhci/context.rs)
1341note ========================================================================
1342
1343fn kux_default_ctrl_max_pkt port_speed uchar to ushort
1344 if eq port_speed KUX_SPEED_HIGH
1345 ret ushort 64
1346 end
1347 if ge port_speed KUX_SPEED_SUPER
1348 ret ushort 512
1349 end
1350 ret ushort 8
1351end
1352
1353fn kux_ilog2_u32 v uint to uchar
1354 r = uchar 0
1355 x = v
1356 while gt x uint 1 bound 32
1357 x = shr x 1
1358 r = add r uchar 1
1359 end
1360 ret r
1361end
1362
1363fn kux_interrupt_interval port_speed uchar b_ival uchar to uchar
1364 is_fl = false
1365 if eq port_speed KUX_SPEED_FULL
1366 is_fl = true
1367 end
1368 if eq port_speed KUX_SPEED_LOW
1369 is_fl = true
1370 end
1371 if is_fl
1372 b = b_ival
1373 if lt b uchar 1
1374 b = uchar 1
1375 end
1376 mf = mul uint b uint 8
1377 lg = kux_ilog2_u32 mf
1378 if lt lg uchar 3
1379 ret uchar 3
1380 end
1381 if gt lg uchar 10
1382 ret uchar 10
1383 end
1384 ret lg
1385 end
1386 b = b_ival
1387 if lt b uchar 1
1388 b = uchar 1
1389 end
1390 if gt b uchar 16
1391 b = uchar 16
1392 end
1393 ret sub b uchar 1
1394end
1395
1396fn kux_isoch_interval port_speed uchar b_ival uchar to uchar
1397 b = b_ival
1398 if lt b uchar 1
1399 b = uchar 1
1400 end
1401 if gt b uchar 16
1402 b = uchar 16
1403 end
1404 if eq port_speed KUX_SPEED_FULL
1405 ret add b uchar 2
1406 end
1407 if eq port_speed KUX_SPEED_LOW
1408 ret add b uchar 2
1409 end
1410 ret sub b uchar 1
1411end
1412
1413fn kux_isoch_out_ep cfg edit KuxEpCfg port_speed uchar b_ival uchar w_max_pkt ushort max_esit ushort deq ulong to void
1414 hb = uchar 0
1415 if eq port_speed KUX_SPEED_HIGH
1416 hb = uchar and shr w_max_pkt 11 ushort 3
1417 end
1418 put cfg ep_type KUX_EP_ISOCH_OUT
1419 put cfg max_pkt and w_max_pkt ushort 0x7ff
1420 put cfg max_burst hb
1421 put cfg ival kux_isoch_interval port_speed b_ival
1422 put cfg deq deq
1423 put cfg avg_trb max_esit
1424 put cfg max_esit max_esit
1425end
1426
1427fn kux_endpoint_dci ep_addr uchar to uchar
1428 num = and ep_addr uchar 0x0f
1429 if eq num uchar 0
1430 ret uchar 0
1431 end
1432 dir = uchar 0
1433 if ne and ep_addr uchar 0x80 uchar 0
1434 dir = uchar 1
1435 end
1436 ret add mul num uchar 2 dir
1437end
1438
1439fn kux_in_ctx_init ctx edit KuxInCtx words edit uint * ctx_bytes ulong to bool
1440 put ctx ok false
1441 put ctx stride ulong 0
1442 st = ulong 0
1443 if eq ctx_bytes ulong 32
1444 st = ulong 8
1445 else
1446 if eq ctx_bytes ulong 64
1447 st = ulong 16
1448 else
1449 ret false
1450 end
1451 end
1452 need = mul st ulong 33
1453 if lt len words need
1454 ret false
1455 end
1456 wlen = len words
1457 for i ulong wlen bound 4096
1458 put words i uint 0
1459 end
1460 put ctx ok true
1461 put ctx stride st
1462 ret true
1463end
1464
1465fn kux_in_ctx_add ctx view KuxInCtx words edit uint * flags uint to void
1466 if not get ctx ok
1467 ret
1468 end
1469 put words 1 flags
1470end
1471
1472fn kux_in_ctx_slot ctx view KuxInCtx words edit uint * port_speed uchar root_port uchar ctx_entries uchar to void
1473 if not get ctx ok
1474 ret
1475 end
1476 base = get ctx stride
1477 w0 = or shl uint and ctx_entries uchar 0x1f 27 shl uint and port_speed uchar 0x0f 20
1478 w1 = shl uint root_port 16
1479 put words base w0
1480 put words add base 1 w1
1481end
1482
1483fn kux_in_ctx_ep ctx view KuxInCtx words edit uint * dci uchar cfg view KuxEpCfg to void
1484 if not get ctx ok
1485 ret
1486 end
1487 if lt dci uchar 1
1488 ret
1489 end
1490 if gt dci KUX_MAX_DCI
1491 ret
1492 end
1493 base = mul add ulong dci 1 get ctx stride
1494 put words base shl uint get cfg ival 16
1495 w1 = shl KUX_CERR 1
1496 w1 = or w1 shl uint and get cfg ep_type uchar 7 3
1497 w1 = or w1 shl uint get cfg max_burst 8
1498 w1 = or w1 shl uint get cfg max_pkt 16
1499 put words add base 1 w1
1500 put words add base 2 uint and get cfg deq ulong 0xffffffff
1501 put words add base 3 uint and shr get cfg deq 32 ulong 0xffffffff
1502 put words add base 4 or uint get cfg avg_trb shl uint get cfg max_esit 16
1503end
1504
1505note ========================================================================
1506note 8. xHCI Controller State Machine and Deadline (usb/xhci/controller.rs)
1507note ========================================================================
1508
1509fn kux_deadline_init dl edit KuxDeadline start ulong freq_hz ulong ms ulong to void
1510 put dl start start
1511 put dl ticks div mul freq_hz ms ulong 1000
1512 put dl polls uint 0
1513end
1514
1515fn kux_deadline_expired dl edit KuxDeadline now ulong to bool
1516 p = get dl polls
1517 if lt p uint 0xffffffff
1518 p = add p uint 1
1519 end
1520 put dl polls p
1521 t = get dl ticks
1522 if eq t ulong 0
1523 ret gt p uint 1000000
1524 end
1525 ret gt sub now get dl start t
1526end
1527
1528fn kux_ctrl_init st edit KuxCtrlState to void
1529 put st state KUX_ST_INIT
1530 put st err KUX_ERR_NONE
1531 put st err_code uchar 0
1532 put st ctx_bytes ulong 32
1533 put st slot uchar 0
1534 put st dci uchar 0
1535 put st port uchar 0
1536 put st ep_addr uchar 0
1537 put st has_dev false
1538 put st retire_pend false
1539 put st is_halted false
1540 for i ulong 4
1541 put st last_ev i uint 0
1542 end
1543 put st db_cnt uint 0
1544 put st erdp_cnt uint 0
1545 put st last_erdp ulong 0
1546end
1547
1548fn kux_ctrl_validate_caps st edit KuxCtrlState caps view KuxCaps dma_end ulong to bool
1549 if not get caps ok
1550 put st state KUX_ST_ERROR
1551 put st err KUX_ERR_ABSENT
1552 ret false
1553 end
1554 if ne get caps scratch ushort 0
1555 put st state KUX_ST_ERROR
1556 put st err KUX_ERR_UNSUPPORTED
1557 ret false
1558 end
1559 if not get caps addr64
1560 if gt dma_end ulong 0xffffffff
1561 put st state KUX_ST_ERROR
1562 put st err KUX_ERR_UNSUPPORTED
1563 ret false
1564 end
1565 end
1566 put st ctx_bytes get caps ctx_bytes
1567 ret true
1568end
1569
1570fn kux_ctrl_step_reset st edit KuxCtrlState usbcmd uint usbsts uint to uchar
1571 if eq usbsts uint 0xffffffff
1572 put st state KUX_ST_ERROR
1573 put st err KUX_ERR_ABSENT
1574 ret KUX_ST_ERROR
1575 end
1576 cur = get st state
1577 if eq cur KUX_ST_INIT
1578 if ne and usbsts KUX_USBSTS_HALTED uint 0
1579 put st state KUX_ST_HALTED
1580 ret KUX_ST_HALTED
1581 end
1582 ret cur
1583 end
1584 if eq cur KUX_ST_HALTED
1585 if eq and usbcmd KUX_USBCMD_RESET uint 0
1586 if eq and usbsts KUX_USBSTS_NOT_READY uint 0
1587 put st state KUX_ST_RESET_DONE
1588 ret KUX_ST_RESET_DONE
1589 end
1590 end
1591 end
1592 ret get st state
1593end
1594
1595fn kux_ctrl_step_run st edit KuxCtrlState usbsts uint to bool
1596 if ne get st state KUX_ST_RESET_DONE
1597 ret false
1598 end
1599 if eq and usbsts KUX_USBSTS_HALTED uint 0
1600 put st state KUX_ST_RUNNING
1601 ret true
1602 end
1603 ret false
1604end
1605
1606fn kux_ctrl_reported_max_pkt speed uchar cur_max ushort dev_desc view uchar * to ushort
1607 sz = kux_control_max_packet dev_desc
1608 if eq sz uchar 0
1609 ret cur_max
1610 end
1611 if ge speed KUX_SPEED_SUPER
1612 s = sz
1613 if gt s uchar 9
1614 s = uchar 9
1615 end
1616 ret shl ushort 1 uint s
1617 end
1618 ret ushort sz
1619end
1620
1621fn kux_ctrl_build_ep0_ctx st view KuxCtrlState words edit uint * speed uchar add_flags uint cfg view KuxEpCfg to bool
1622 ctx = new KuxInCtx false 0
1623 if not kux_in_ctx_init ctx words get st ctx_bytes
1624 ret false
1625 end
1626 kux_in_ctx_add ctx words add_flags
1627 kux_in_ctx_slot ctx words speed get st port uchar 1
1628 kux_in_ctx_ep ctx words KUX_CONTROL_DCI cfg
1629 ret true
1630end
1631
1632fn kux_ctrl_build_kbd_ctx st edit KuxCtrlState words edit uint * speed uchar kbd view KuxBootKbd deq ulong to bool
1633 ctx = new KuxInCtx false 0
1634 epc = new KuxEpCfg 0 0 0 0 0 0 0
1635 if not get kbd ok
1636 put st err KUX_ERR_NO_KEYBOARD
1637 ret false
1638 end
1639 dci = kux_endpoint_dci get kbd ep_addr
1640 if eq dci uchar 0
1641 put st err KUX_ERR_NO_KEYBOARD
1642 ret false
1643 end
1644 if not kux_in_ctx_init ctx words get st ctx_bytes
1645 put st err KUX_ERR_UNSUPPORTED
1646 ret false
1647 end
1648 add_f = or uint 1 shl uint 1 uint dci
1649 kux_in_ctx_add ctx words add_f
1650 kux_in_ctx_slot ctx words speed get st port dci
1651 put epc ep_type KUX_EP_INTR_IN
1652 put epc max_burst uchar 0
1653 put epc max_pkt get kbd max_pkt
1654 put epc ival kux_interrupt_interval speed get kbd ival
1655 put epc deq deq
1656 put epc avg_trb ushort 8
1657 put epc max_esit get kbd max_pkt
1658 kux_in_ctx_ep ctx words dci epc
1659 put st dci dci
1660 put st ep_addr get kbd ep_addr
1661 put st has_dev true
1662 put st state KUX_ST_ATTACHED
1663 ret true
1664end
1665
1666fn kux_ctrl_arm_report st edit KuxCtrlState rep_ring edit KuxProdRing rep_mem edit uint * report_buf_dma ulong to ulong
1667 trb = new KuxTrb 0
1668 kux_trb_normal trb report_buf_dma uint 8
1669 addr = kux_prod_ring_push rep_ring rep_mem trb
1670 put st db_cnt add get st db_cnt uint 1
1671 ret addr
1672end
1673
1674fn kux_ctrl_handle_event st edit KuxCtrlState ev view KuxTrb to uchar
1675 if get st is_halted
1676 ret KUX_EV_IGNORED
1677 end
1678 put st retire_pend true
1679 for i ulong 4
1680 put st last_ev i get ev w i
1681 end
1682 if ne kux_trb_kind ev KUX_TRB_TRANSFER_EVENT
1683 ret KUX_EV_OTHER
1684 end
1685 if ne kux_trb_slot_id ev get st slot
1686 ret KUX_EV_OTHER
1687 end
1688 if ne kux_trb_ep_id ev get st dci
1689 ret KUX_EV_OTHER
1690 end
1691 if not kux_trb_completed_ok ev
1692 put st is_halted true
1693 put st err KUX_ERR_FAILED
1694 put st err_code kux_trb_cc ev
1695 ret KUX_EV_HALTED
1696 end
1697 if eq kux_trb_residual ev uint 0
1698 ret KUX_EV_REPORT_READY
1699 end
1700 ret KUX_EV_SHORT_REARM
1701end
1702
1703fn kux_ctrl_retire st edit KuxCtrlState er view KuxEventRing to bool
1704 if not get st retire_pend
1705 ret false
1706 end
1707 put st last_erdp or kux_event_ring_deq_ptr er KUX_RT_ERDP_BUSY
1708 put st erdp_cnt add get st erdp_cnt uint 1
1709 put st retire_pend false
1710 ret true
1711end