summaryrefslogtreecommitdiff
path: root/tools/pkg/2/xhci_driver/include/xhci.hpp
blob: da858b5e4324e52fac612db016132294b3b23919 (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
#include <stdint.h>

#pragma once

#define XHCI_RESET_TIMEOUT 1000

typedef struct {
    uint32_t maxslots : 8;
    uint32_t maxintrs : 11;
    uint32_t reserved1 : 5;
    uint32_t maxports : 8;
} __attribute__((packed)) hcsparams1_t;

typedef struct {
    uint32_t ist : 4;
    uint32_t erstmax : 4;
    uint32_t reserved1 : 13;
    uint32_t max_scratchpad_hi : 5;
    uint32_t spr : 1;
    uint32_t max_scratchpad_lo : 5;
} __attribute__((packed)) hcsparams2_t;

typedef struct {
    uint32_t _64bitcap : 1;
    uint32_t bnc : 1;
    uint32_t contextsize : 1;
    uint32_t portpowercontrol : 1;
    uint32_t portindicator : 1;
    uint32_t lhrc : 1;
    uint32_t ltc : 1;
    uint32_t nss : 1;
    uint32_t pae : 1;
    uint32_t spc : 1;
    uint32_t sec : 1;
    uint32_t cfc : 1;
    uint32_t max_psa_size : 4;
    uint32_t xECP : 16;
} __attribute__((packed)) hccparams1_t;

typedef struct {
    uint8_t caplength;
    uint8_t reserved;
    uint16_t hciversion;
    hcsparams1_t hcsparams1;
    hcsparams2_t hcsparams2;
    uint32_t hcsparams3;
    hccparams1_t hccparams1;
    uint32_t dboff;
    uint32_t rtsoff;
    uint32_t hccparms2;
} xhci_cap_regs_t;

typedef struct {
    uint32_t usbcmd;
    uint32_t usbsts;
    uint32_t pagesize;
    uint32_t reserved1[2];
    uint32_t dnctrl;
    uint64_t crcr;
    uint32_t reserved2[4];
    uint64_t dcbaap;
    uint32_t config;
} xhci_op_regs_t;

#define XHCI_USBCMD_RS (1 << 0)
#define XHCI_USBCMD_HOSTCONTROLLERREST (1 << 1)
#define XHCI_USB_SPEED_FULL_SPEED           1 
#define XHCI_USB_SPEED_LOW_SPEED            2 
#define XHCI_USB_SPEED_HIGH_SPEED           3 
#define XHCI_USB_SPEED_SUPER_SPEED          4 
#define XHCI_USB_SPEED_SUPER_SPEED_PLUS     5 

typedef struct {
    uint32_t portsc;
    uint32_t portpmsc;
    uint32_t portli;
} xhci_port_regs_t;

typedef struct {
    uint64_t erst_segment : 3;
    uint64_t event_busy : 1;
    uint64_t event_ring_pointer : 60;
} xhci_erdp_t;

typedef struct {
    uint32_t iman;   
    uint32_t imod;    
    uint32_t erstsz; 
    uint32_t reserved0;
    uint64_t erstba;   
    union {
        xhci_erdp_t erdp;
        uint64_t erdp_val;
    };
} IR_t;

typedef struct {
    uint32_t mfindex;     
    uint32_t reserved1[7]; 
    IR_t int_regs[1024];
} xhci_runtime_regs_t;

typedef struct {
    uint64_t base; 
    uint32_t size;         
    uint32_t reserved0;
} xhci_erst_t;

typedef struct {
    uint32_t cycle : 1;
    uint32_t nexttrb : 1;
    uint32_t interruptonshort : 1;
    uint32_t nosnoop : 1;
    uint32_t chain : 1;
    uint32_t intoncompletion : 1;
    uint32_t immediate : 1;
    uint32_t reserved1 : 2;
    uint32_t blockeventint : 1;
    uint32_t type : 6;
    uint32_t reserved2 : 16;
} xhci_trb_info_t;

typedef struct {
    uint64_t base; 
    union {
        struct {
            uint32_t reserved0 : 24;
            uint32_t ret_code : 8;
        };
        uint32_t status; 
    };   
    union {
        xhci_trb_info_t info_s;
        uint32_t info;
    };
} xhci_trb_t;

typedef struct {
    uint32_t cycle : 1;
    uint32_t reserved1 : 9;
    uint32_t type : 6;
    uint32_t vfid : 8;
    uint32_t slotid : 8;
} xhci_slot_info_trb_t;

typedef struct {
    uint64_t base;
    uint32_t CCP : 24;
    uint32_t ret_code : 8;
    union {
        xhci_slot_info_trb_t info_s;
        uint32_t info;
    };
} xhci_slot_trb_t;

typedef struct {
    uint32_t cycle : 1;
    uint32_t reserved0 : 8;
    uint32_t bsr : 1;
    uint32_t type : 6;
    uint8_t reserved1;
    uint8_t slotid;
} xhci_set_addr_info_trb_t;

typedef struct {
    uint64_t base;
    uint32_t status;
    union {
        xhci_set_addr_info_trb_t info_s;
        uint32_t info;
    };
} xhci_set_addr_trb_t;

typedef struct {
    uint8_t cycle;
    uint16_t trb_limit;
    uint64_t queue;
    uint64_t phys;
    xhci_trb_t* trb;
} xhci_command_ring_ctx_t;

typedef struct {
    uint16_t trb_limit;
    uint64_t queue;
    uint8_t cycle;
    volatile IR_t* father;
    xhci_trb_t* trb;
    xhci_erst_t* table;
} xhci_event_ring_ctx_t;

typedef struct {
    uint16_t trb_limit;
    uint64_t queue;
    uint8_t cycle;
    uint32_t slot;
    uint64_t phys;
    xhci_trb_t* trb;
} xhci_port_ring_ctx_t;

typedef struct {
    uint32_t D;
    uint32_t A;
    uint32_t reserved0[5];
    uint8_t configvalue;
    uint8_t interfacenum;
    uint8_t altsetting;
    uint8_t reserved1;
} __attribute__((packed)) xhci_input_control_ctx_t;

typedef struct {
    uint32_t D;
    uint32_t A;
    uint32_t reserved0[5];
    uint8_t configvalue;
    uint8_t interfacenum;
    uint8_t altsetting;
    uint8_t reserved1;
    uint32_t align[8];
} __attribute__((packed)) xhci_input_control_ctx64_t;

typedef struct {
    uint32_t routestring : 20;
    uint8_t speed : 4;
    uint8_t reserved0 : 1;
    uint8_t multitt : 1;
    uint8_t hub : 1;
    uint8_t contextentries : 5;
    uint16_t maxexitlat;
    uint8_t porthubnum;
    uint8_t numofports;
    uint8_t parenthubslot;
    uint8_t parentportnum;
    uint8_t thinktime : 2;
    uint8_t reserved1 : 4;
    uint16_t irtarget : 10;
    uint8_t address;
    uint32_t reserved2 : 19;
    uint8_t slotstate : 5;
    uint32_t reserved3[4];
} __attribute__((packed)) xhci_slot_ctx_t;

typedef struct {
    uint32_t routestring : 20;
    uint8_t speed : 4;
    uint8_t reserved0 : 1;
    uint8_t multitt : 1;
    uint8_t hub : 1;
    uint8_t contextentries : 5;
    uint16_t maxexitlat;
    uint8_t porthubnum;
    uint8_t numofports;
    uint8_t parenthubslot;
    uint8_t parentportnum;
    uint8_t thinktime : 2;
    uint8_t reserved1 : 4;
    uint16_t irtarget : 10;
    uint8_t address;
    uint32_t reserved2 : 19;
    uint8_t slotstate : 5;
    uint32_t reserved3[4];
    uint32_t align[8];
} __attribute__((packed)) xhci_slot_ctx64_t;

typedef struct {
    uint32_t state : 3;
    uint32_t reserved0 : 5;
    uint32_t mult : 2;
    uint32_t maxprimarystreams : 5;
    uint32_t linear : 1;
    uint32_t interval : 8;
    uint32_t some_shit_with_long_name : 8;
    uint32_t reserved1 : 1;
    uint32_t cerr : 2;
    uint32_t endpointtype : 3;
    uint32_t reserved2 : 1;
    uint32_t hid : 1;
    uint32_t maxburstsize : 8;
    uint32_t maxpacketsize : 16;
    uint64_t base;
    uint16_t averagetrblen;
    uint16_t some_shit_with_long_name_lo;
    uint32_t align[3];
} __attribute__((packed)) xhci_endpoint_ctx_t;

typedef struct {
    uint32_t state : 3;
    uint32_t reserved0 : 5;
    uint32_t mult : 2;
    uint32_t maxprimarystreams : 5;
    uint32_t linear : 1;
    uint32_t interval : 8;
    uint32_t some_shit_with_long_name : 8;
    uint32_t reserved1 : 1;
    uint32_t cerr : 2;
    uint32_t endpointtype : 3;
    uint32_t reserved2 : 1;
    uint32_t hid : 1;
    uint32_t maxburstsize : 8;
    uint32_t maxpacketsize : 16;
    uint64_t base;
    uint16_t averagetrblen;
    uint16_t some_shit_with_long_name_lo;
    uint32_t align[11];
} __attribute__((packed)) xhci_endpoint_ctx64_t;

typedef struct {
    xhci_input_control_ctx_t input_ctx;
    xhci_slot_ctx_t slot;
    xhci_endpoint_ctx_t ep0;
    xhci_endpoint_ctx_t ep[30];
} __attribute__((packed)) xhci_input_ctx_t;

typedef struct {
    xhci_input_control_ctx64_t input_ctx;
    xhci_slot_ctx64_t slot;
    xhci_endpoint_ctx64_t ep0;
    xhci_endpoint_ctx64_t ep[30];
} __attribute__((packed)) xhci_input_ctx64_t;

typedef struct {
    uint8_t len;
    uint8_t type;
} xhci_usb_descriptor_header;

typedef struct {
    xhci_usb_descriptor_header head;
    uint16_t usb;
    uint8_t deviceclass;
    uint8_t devicesubclass;
    uint8_t protocol;
    uint8_t maxpacketsize;
    uint16_t vendor;
    uint16_t product0;
    uint16_t device;
    uint8_t manufacter;
    uint8_t product1;
    uint8_t serialnum;
    uint8_t numconfigs;
} xhci_usb_descriptor_t;

typedef struct {
    xhci_usb_descriptor_header head;
    uint16_t lang[128];
} xhci_lang_descriptor_t;

typedef struct {
    xhci_usb_descriptor_header head;
    uint16_t str[128];
} xhci_string_descriptor_t;

typedef struct {
    xhci_usb_descriptor_header head;
    uint16_t len;
    uint8_t numinterfaces;
    uint8_t configval;
    uint8_t config;
    uint8_t attributes;
    uint8_t maxpower;
    uint8_t data[1024];
} xhci_config_descriptor_t;

typedef struct {
    uint8_t desctype;
    uint8_t desclen;
} xhci_hid_sub_desc;

typedef struct {
    xhci_usb_descriptor_header head;
    uint16_t hid;
    uint8_t country;
    uint8_t numdesc;
    xhci_hid_sub_desc desc[1024];
} xhci_hid_descriptor_t;

typedef struct {
    xhci_usb_descriptor_header head;
    uint8_t endpointaddr;
    uint8_t attributes;
    uint16_t maxpacketsize;
    uint8_t interval;
} xhci_endpoint_descriptor_t;

typedef struct {
    xhci_usb_descriptor_header head;
    uint8_t num;
    uint8_t altsetting;
    uint8_t numendpoints;
    uint8_t interclass;
    uint8_t intersubclass;
    uint8_t protocol;
    uint8_t interface;
} xhci_interface_descriptor_t;

typedef struct {
    uint32_t cycle : 1;
    uint32_t reserved0 : 8;
    uint32_t deconfigure : 1;
    uint32_t type : 6;
    uint32_t reserved1 : 8;
    uint32_t slot : 8;
} xhci_configure_endpoints_info_trb_t;

typedef struct {
    uint64_t base;
    uint32_t status;
    union {
        xhci_configure_endpoints_info_trb_t info_s;
        uint32_t info;
    };
} xhci_configure_endpoints_trb_t;

typedef struct xhci_device {
    uint64_t xhci_phys_base;
    uint64_t xhci_virt_base;
    uint64_t* dcbaa;
    uint16_t calculated_scratchpad_count;
    volatile xhci_cap_regs_t* cap;
    volatile xhci_op_regs_t* op;
    volatile xhci_port_regs_t* port;
    volatile xhci_runtime_regs_t* runtime;
    uint32_t* doorbell;
    xhci_command_ring_ctx_t* com_ring;
    xhci_event_ring_ctx_t* event_ring;

    uint32_t max_ports; 

    uint8_t usb3ports[64];

    struct xhci_device* next;
} __attribute__((packed)) xhci_device_t;

typedef struct xhci_interface {
    uint8_t type;
    void* data;

    uint64_t len;
    void* buffer;
    
    xhci_interface* next;
} xhci_interface_t;

#define USB_TYPE_KEYBOARD 4
#define USB_TYPE_MOUSE 3

typedef struct xhci_usb_device {
    xhci_port_ring_ctx_t* transfer_ring;
    xhci_usb_descriptor_t* desc;
    xhci_config_descriptor_t* config;
    xhci_device_t* dev;
    xhci_input_ctx_t* input_ctx;
    xhci_port_ring_ctx_t* ep_ctx[30];
    uint64_t phys_buffers[30];
    uint8_t* buffers[30];
    uint8_t main_ep;
    uint16_t buffers_need_size[30];
    uint8_t doorbell_values[30];
    xhci_interface_t* interface;
    struct xhci_usb_device* next;
    uint64_t phys_input_ctx;
    uint32_t slotid;
    uint32_t portnum;
    uint32_t type;
    uint8_t _is64byte;
    uint8_t add_buffer[8];
} xhci_usb_device_t;

typedef struct {
    uint32_t cycle : 1;
    uint32_t reserved0 : 1;
    uint32_t eventdata : 1;
    uint32_t reserved1 : 7;
    uint32_t type : 6;
    uint32_t ep_id : 5;
    uint32_t reserved2 : 3;
    uint32_t slot : 8;
} xhci_done_info_trb_t;

typedef struct {
    uint64_t base;
    struct {
        uint32_t seek : 24;
        uint32_t ret_code : 8;
    };
    union {
        xhci_done_info_trb_t info_s;
        uint32_t info;
    };
} xhci_done_trb_t;

typedef struct {
    union {
        struct {
            uint8_t id;
            uint8_t nextcap;
            uint16_t info;
        };
        uint32_t full;
    };
} xhci_ext_cap_t;

typedef struct {
    union {
        struct {
            uint8_t id;
            uint8_t nextcap;
            uint8_t minor;
            uint8_t major;
        };
        uint32_t firsthalf;
    };

    uint32_t name;

    union {
        struct {
            uint8_t portoffset;
            uint8_t portcount;
            uint8_t protocoldefine;
            uint8_t protocolspeedid;
        };
        uint32_t thirdhalf;
    };

    union {
        struct {
            uint8_t slottype : 4;
            uint32_t reserved0 : 28;
        };
        uint32_t fourhalf;
    };
} xhci_usb_cap_t;

typedef struct {
    uint8_t type;
    uint8_t request;
    uint16_t value;
    uint16_t index;
    uint16_t len;
} xhci_usb_command_t;

typedef struct {
    xhci_usb_command_t command;
    uint32_t len : 17;
    uint32_t reserved0 : 5;
    uint32_t target : 10;
    uint32_t cycle : 1;
    uint32_t reserved1 : 4;
    uint32_t intoncomp : 1;
    uint32_t imdata : 1;
    uint32_t reserved2 : 3;
    uint32_t type : 6;
    uint32_t trt : 2;
    uint32_t reserved3 : 14;
} xhci_setupstage_trb_t;

typedef struct {
    uint64_t data;
    uint32_t len : 17;
    uint32_t tdsize : 5;
    uint32_t target : 10;
    uint32_t cycle : 1;
    uint32_t evnexttrb : 1;
    uint32_t intrshortpacket : 1;
    uint32_t nosnoop : 1;
    uint32_t chain : 1;
    uint32_t introncomp : 1;
    uint32_t imdata : 1;
    uint32_t reserved0 : 3;
    uint32_t type : 6;
    uint32_t direction : 1;
    uint32_t reserved1 : 15;
} xhci_datastage_trb_t;

typedef struct {
    uint64_t base;
    uint32_t reserved0 : 22;
    uint32_t target : 10;
    uint32_t cycle : 1;
    uint32_t evnext : 1;
    uint32_t reserved1 : 2;
    uint32_t chain : 1;
    uint32_t intoncomp : 1;
    uint32_t reserved2 : 3;
    uint32_t blockevent : 1;
    uint32_t type : 6;
    uint32_t reserved3 : 16;
} xhci_eventdata_trb_t;

typedef struct {
    uint64_t reserved0;
    uint32_t reserved1 : 22;
    uint32_t target : 10;
    uint32_t cycle : 1;
    uint32_t evnext : 1;
    uint32_t reserved2 : 2;
    uint32_t chain : 1;
    uint32_t intoncomp : 1;
    uint32_t reserved3 : 4;
    uint32_t type : 6;
    uint32_t direction : 1;
    uint32_t reserved4 : 15;
} xhci_statusstage_trb_t;

typedef struct {
    uint32_t cycle : 1;
    uint32_t ent : 1;
    uint32_t isp : 1;
    uint32_t nosnoop : 1;
    uint32_t chain : 1;
    uint32_t ioc : 1;
    uint32_t imdata : 1;
    uint32_t reserved0 : 2;
    uint32_t bei : 1;
    uint32_t type : 6;
    uint32_t dir : 1;
    uint32_t reserved1 : 15;
} xhci_normal_info_trb_t;

typedef struct {
    uint64_t base;
    uint32_t trbtransferlen : 17;
    uint32_t tdsize : 5;
    uint32_t target : 10;
    union {
        xhci_normal_info_trb_t info_s;
        uint32_t info;
    };
} xhci_normal_trb_t;

typedef struct {
    uint32_t cycle : 1;
    uint32_t reserved0 : 9;
    uint32_t type : 6;
    uint32_t reserved1 : 16;
} xhci_port_change_info_t;

typedef struct {
    struct {
        uint32_t reserved0 : 24;
        uint32_t port : 8;
    };
    uint32_t status;
    union {
        xhci_port_change_info_t info_s;
        uint32_t info;
    };
} xhci_port_change_trb_t;

typedef struct xhci_hid_driver {
    void (*func)(xhci_usb_device_t* usbdev,xhci_done_trb_t* trb);
    int type;
    struct xhci_hid_driver* next;
} xhci_hid_driver_t;

#define XHCI_ENDPOINTTYPE_ISOCHRONOUS_OUT  1
#define XHCI_ENDPOINTTYPE_BULK_OUT         2
#define XHCI_ENDPOINTTYPE_INTERRUPT_OUT    3
#define XHCI_ENDPOINTTYPE_ISOCHRONOUS_IN   5
#define XHCI_ENDPOINTTYPE_BULK_IN          6
#define XHCI_ENDPOINTTYPE_INTERRUPT_IN     7
#define TRB_NORMAL_TYPE 1 
#define TRB_SETUPSTAGE_TYPE 2
#define TRB_DATASTAGE_TYPE 3
#define TRB_STATUSSTAGE_TYPE 4
#define TRB_ISOCH_TYPE 5
#define TRB_LINK_TYPE 6
#define TRB_EVENTDATA_TYPE 7
#define TRB_NOOP_TYPE 8
#define TRB_ENABLESLOTCOMMAND_TYPE 9
#define TRB_DISABLESLOTCOMMAND_TYPE 10
#define TRB_ADDRESSDEVICECOMMAND_TYPE 11
#define TRB_CONFIGUREENDPOINTCOMMAND_TYPE 12
#define TRB_EVALUATECONTEXTCOMMAND_TYPE 13
#define TRB_RESETENDPOINTCOMMAND_TYPE 14
#define TRB_STOPENDPOINTCOMMAND_TYPE 15
#define TRB_SETTRDEQPOINTERCOMMAND_TYPE 16
#define TRB_RESETDEVICECOMMAND_TYPE 17
#define TRB_FORCEEVENTCOMMAND_TYPE 18
#define TRB_NEGBANDWIDTHCOMMAND_TYPE 19
#define TRB_SETLATENCYCOMMAND_TYPE 20
#define TRB_GETPORTBANDWIDTHCOMMAND_TYPE 21
#define TRB_FORCEHEADERCOMMAND_TYPE 22
#define TRB_NOOPCOMMAND_TYPE 23
#define TRB_TRANSFEREVENT_TYPE 32
#define TRB_COMMANDCOMPLETIONEVENT_TYPE 33
#define TRB_PORTSTATUSCHANGEEVENT_TYPE 34
#define TRB_HOSTCONTROLLEREVENT_TYPE 37
#define TRB_DEVICENOTIFICATIONEVENT_TYPE 38
#define TRB_MFINDEXWARPEVENT_TYPE 39