xhci.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. #pragma once
  2. #include <driver/usb/usb.h>
  3. #include <driver/pci/pci.h>
  4. #include <driver/pci/msi.h>
  5. // #pragma GCC optimize("O0")
  6. #define XHCI_MAX_HOST_CONTROLLERS 4 // 本驱动程序最大支持4个xhci root hub controller
  7. #define XHCI_MAX_ROOT_HUB_PORTS 128 // 本驱动程序最大支持127个root hub 端口(第0个保留)
  8. // ========== irq BEGIN ===========
  9. #define XHCI_IRQ_DONE (1U << 31) // 当command trb 的status的第31位被驱动程序置位时,表明该trb已经执行完成(这是由于xhci规定,第31位可以由驱动程序自行决定用途)
  10. /**
  11. * @brief 每个xhci控制器的中断向量号
  12. *
  13. */
  14. const uint8_t xhci_controller_irq_num[XHCI_MAX_HOST_CONTROLLERS] = {157, 158, 159, 160};
  15. /**
  16. * @brief 通过irq号寻找对应的主机控制器id
  17. *
  18. */
  19. #define xhci_find_hcid_by_irq_num(irq_num) ({ \
  20. int retval = -1; \
  21. for (int i = 0; i < XHCI_MAX_HOST_CONTROLLERS; ++i) \
  22. if (xhci_controller_irq_num[i] == irq_num) \
  23. retval = i; \
  24. retval; \
  25. })
  26. struct xhci_hc_irq_install_info_t
  27. {
  28. int processor; // 中断目标处理器
  29. int8_t edge_trigger; // 是否边缘触发
  30. int8_t assert; // 是否高电平触发
  31. };
  32. // ========== irq END ===========
  33. // ======== Capability Register Set BEGIN ============
  34. // xhci Capability Registers offset
  35. #define XHCI_CAPS_CAPLENGTH 0x00 // Cap 寄存器组的长度
  36. #define XHCI_CAPS_RESERVED 0x01
  37. #define XHCI_CAPS_HCIVERSION 0x02 // 接口版本号
  38. #define XHCI_CAPS_HCSPARAMS1 0x04
  39. #define XHCI_CAPS_HCSPARAMS2 0x08
  40. #define XHCI_CAPS_HCSPARAMS3 0x0c
  41. #define XHCI_CAPS_HCCPARAMS1 0x10 // capability params 1
  42. #define XHCI_CAPS_DBOFF 0x14 // Doorbell offset
  43. #define XHCI_CAPS_RTSOFF 0x18 // Runtime register space offset
  44. #define XHCI_CAPS_HCCPARAMS2 0x1c // capability params 2
  45. struct xhci_caps_HCSPARAMS1_reg_t
  46. {
  47. unsigned max_slots : 8; // 最大插槽数
  48. unsigned max_intrs : 11; // 最大中断数
  49. unsigned reserved : 5;
  50. unsigned max_ports : 8; // 最大端口数
  51. } __attribute__((packed));
  52. struct xhci_caps_HCSPARAMS2_reg_t
  53. {
  54. unsigned ist : 4; // 同步调度阈值
  55. unsigned ERST_Max : 4; // Event Ring Segment Table: Max segs
  56. unsigned Reserved : 13;
  57. unsigned max_scratchpad_buf_HI5 : 5; // 草稿行buffer地址(高5bit)
  58. unsigned spr : 1; // scratchpad restore
  59. unsigned max_scratchpad_buf_LO5 : 5; // 草稿行buffer地址(低5bit)
  60. } __attribute__((packed));
  61. struct xhci_caps_HCSPARAMS3_reg_t
  62. {
  63. uint8_t u1_device_exit_latency; // 0~10ms
  64. uint8_t Reserved;
  65. uint16_t u2_device_exit_latency; // 0~2047ms
  66. } __attribute__((packed));
  67. struct xhci_caps_HCCPARAMS1_reg_t
  68. {
  69. unsigned int ac64 : 1; // 64-bit addressing capability
  70. unsigned int bnc : 1; // bw negotiation capability
  71. unsigned int csz : 1; // context size
  72. unsigned int ppc : 1; // 端口电源控制
  73. unsigned int pind : 1; // port indicators
  74. unsigned int lhrc : 1; // Light HC reset capability
  75. unsigned int ltc : 1; // latency tolerance messaging capability
  76. unsigned int nss : 1; // no secondary SID support
  77. unsigned int pae : 1; // parse all event data
  78. unsigned int spc : 1; // Stopped - Short packet capability
  79. unsigned int sec : 1; // Stopped EDTLA capability
  80. unsigned int cfc : 1; // Continuous Frame ID capability
  81. unsigned int MaxPSASize : 4; // Max Primary Stream Array Size
  82. uint16_t xECP; // xhci extended capabilities pointer
  83. } __attribute__((packed));
  84. struct xhci_caps_HCCPARAMS2_reg_t
  85. {
  86. unsigned u3c : 1; // U3 Entry Capability
  87. unsigned cmc : 1; // ConfigEP command Max exit latency too large
  88. unsigned fsc : 1; // Force Save Context Capability
  89. unsigned ctc : 1; // Compliance Transition Capability
  90. unsigned lec : 1; // large ESIT payload capability
  91. unsigned cic : 1; // configuration information capability
  92. unsigned Reserved : 26;
  93. } __attribute__((packed));
  94. // ======== Capability Register Set END ============
  95. // ======== Operational Register Set BEGIN =========
  96. // xhci operational registers offset
  97. #define XHCI_OPS_USBCMD 0x00 // USB Command
  98. #define XHCI_OPS_USBSTS 0x04 // USB status
  99. #define XHCI_OPS_PAGESIZE 0x08 // Page size
  100. #define XHCI_OPS_DNCTRL 0x14 // Device notification control
  101. #define XHCI_OPS_CRCR 0x18 // Command ring control
  102. #define XHCI_OPS_DCBAAP 0x30 // Device context base address array pointer
  103. #define XHCI_OPS_CONFIG 0x38 // configuire
  104. #define XHCI_OPS_PRS 0x400 // Port register sets
  105. struct xhci_ops_usbcmd_reg_t
  106. {
  107. unsigned rs : 1; // Run/Stop
  108. unsigned hcrst : 1; // host controller reset
  109. unsigned inte : 1; // Interrupt enable
  110. unsigned hsee : 1; // Host system error enable
  111. unsigned rsvd_psvd1 : 3; // Reserved and preserved
  112. unsigned lhcrst : 1; // light host controller reset
  113. unsigned css : 1; // controller save state
  114. unsigned crs : 1; // controller restore state
  115. unsigned ewe : 1; // enable wrap event
  116. unsigned ue3s : 1; // enable U3 MFINDEX Stop
  117. unsigned spe : 1; // stopped short packet enable
  118. unsigned cme : 1; // CEM Enable
  119. unsigned rsvd_psvd2 : 18; // Reserved and preserved
  120. } __attribute__((packed));
  121. struct xhci_ops_usbsts_reg_t
  122. {
  123. unsigned HCHalted : 1;
  124. unsigned rsvd_psvd1 : 1; // Reserved and preserved
  125. unsigned hse : 1; // Host system error
  126. unsigned eint : 1; // event interrupt
  127. unsigned pcd : 1; // Port change detected
  128. unsigned rsvd_zerod : 3; // Reserved and Zero'd
  129. unsigned sss : 1; // Save State Status
  130. unsigned rss : 1; // restore state status
  131. unsigned sre : 1; // save/restore error
  132. unsigned cnr : 1; // controller not ready
  133. unsigned hce : 1; // host controller error
  134. unsigned rsvd_psvd2 : 19; // Reserved and Preserved
  135. } __attribute__((packed));
  136. struct xhci_ops_pagesize_reg_t
  137. {
  138. uint16_t page_size; // The actual pagesize is ((this field)<<12)
  139. uint16_t reserved;
  140. } __attribute__((packed));
  141. struct xhci_ops_dnctrl_reg_t
  142. {
  143. uint16_t value;
  144. uint16_t reserved;
  145. } __attribute__((packed));
  146. struct xhci_ops_config_reg_t
  147. {
  148. uint8_t MaxSlotsEn; // Max slots enabled
  149. unsigned u3e : 1; // U3 Entry Enable
  150. unsigned cie : 1; // Configuration information enable
  151. unsigned rsvd_psvd : 22; // Reserved and Preserved
  152. } __attribute__((packed));
  153. // ======== Operational Register Set END =========
  154. // ========= TRB begin ===========
  155. // TRB的Transfer Type可用值定义
  156. #define XHCI_TRB_TRT_NO_DATA 0
  157. #define XHCI_TRB_TRT_RESERVED 1
  158. #define XHCI_TRB_TRT_OUT_DATA 2
  159. #define XHCI_TRB_TRT_IN_DATA 3
  160. #define XHCI_CMND_RING_TRBS 128 // TRB num of command ring, not more than 4096
  161. #define XHCI_TRBS_PER_RING 256
  162. #define XHCI_TRB_CYCLE_OFF 0
  163. #define XHCI_TRB_CYCLE_ON 1
  164. // 获取、设置trb中的status部分的complete code
  165. #define xhci_get_comp_code(status) (((status) >> 24) & 0x7f)
  166. #define xhci_set_comp_code(code) ((code & 0x7f) << 24)
  167. /**
  168. * @brief xhci通用TRB结构
  169. *
  170. */
  171. struct xhci_TRB_t
  172. {
  173. uint64_t param; // 参数
  174. uint32_t status;
  175. uint32_t command;
  176. } __attribute__((packed));
  177. struct xhci_TRB_normal_t
  178. {
  179. uint64_t buf_paddr; // 数据缓冲区物理地址
  180. unsigned transfer_length : 17; // 传输数据长度
  181. unsigned TD_size : 5; // 传输描述符中剩余的数据包的数量
  182. unsigned intr_target : 10; // 中断目标 [0:MaxIntrs-1]
  183. unsigned cycle : 1; // used to mark the enqueue pointer of transfer ring
  184. unsigned ent : 1; // evaluate next TRB before updating the endpoint's state
  185. unsigned isp : 1; // Interrupt on short packet bit
  186. unsigned ns : 1; // No snoop
  187. unsigned chain : 1; // The chain bit is used to tell the controller that this
  188. // TRB is associated with the next TRB in the TD
  189. unsigned ioc : 1; // 完成时发起中断
  190. unsigned idt : 1; // Immediate Data
  191. unsigned resv : 2; // Reserved and zero'd
  192. unsigned bei : 1; // Block event interrupt
  193. unsigned TRB_type : 6; // TRB类型
  194. uint16_t Reserved; // 保留且置为0
  195. } __attribute__((packed));
  196. struct xhci_TRB_setup_stage_t
  197. {
  198. uint8_t bmRequestType;
  199. uint8_t bRequest;
  200. uint16_t wValue;
  201. uint16_t wIndex;
  202. uint16_t wLength;
  203. unsigned transfer_legth : 17; // TRB transfer length
  204. unsigned resv1 : 5; // Reserved and zero'd
  205. unsigned intr_target : 10;
  206. unsigned cycle : 1;
  207. unsigned resv2 : 4; // Reserved and zero'd
  208. unsigned ioc : 1; // interrupt on complete
  209. unsigned idt : 1; // immediate data (should always set for setup TRB)
  210. unsigned resv3 : 3; // Reserved and zero'd
  211. unsigned TRB_type : 6;
  212. unsigned trt : 2; // Transfer type
  213. unsigned resv4 : 14; // Reserved and zero'd
  214. } __attribute__((packed));
  215. struct xhci_TRB_data_stage_t
  216. {
  217. uint64_t buf_paddr; // 数据缓冲区物理地址
  218. unsigned transfer_length : 17; // 传输数据长度
  219. unsigned TD_size : 5; // 传输描述符中剩余的数据包的数量
  220. unsigned intr_target : 10; // 中断目标 [0:MaxIntrs-1]
  221. unsigned cycle : 1; // used to mark the enqueue pointer of transfer ring
  222. unsigned ent : 1; // evaluate next TRB before updating the endpoint's state
  223. unsigned isp : 1; // Interrupt on short packet bit
  224. unsigned ns : 1; // No snoop
  225. unsigned chain : 1; // The chain bit is used to tell the controller that this
  226. // TRB is associated with the next TRB in the TD
  227. unsigned ioc : 1; // 完成时发起中断
  228. unsigned idt : 1; // Immediate Data
  229. unsigned resv : 3; // Reserved and zero'd
  230. unsigned TRB_type : 6; // TRB类型
  231. unsigned dir : 1; // 0 -> out packet
  232. // 1 -> in packet
  233. unsigned Reserved : 15; // 保留且置为0
  234. } __attribute__((packed));
  235. struct xhci_TRB_status_stage_t
  236. {
  237. uint64_t resv1; // Reserved and zero'd
  238. unsigned resv2 : 22; // Reserved and zero'd
  239. unsigned intr_target : 10; // 中断目标 [0:MaxIntrs-1]
  240. unsigned cycle : 1; // used to mark the enqueue pointer of transfer ring
  241. unsigned ent : 1; // evaluate next TRB before updating the endpoint's state
  242. unsigned resv3 : 2; // Reserved and zero'd
  243. unsigned chain : 1; // The chain bit is used to tell the controller that this
  244. // TRB is associated with the next TRB in the TD
  245. unsigned ioc : 1; // 完成时发起中断
  246. unsigned resv4 : 4; // Reserved and zero'd
  247. unsigned TRB_type : 6; // TRB类型
  248. unsigned dir : 1; // 0 -> out packet
  249. // 1 -> in packet
  250. unsigned Reserved : 15; // 保留且置为0
  251. } __attribute__((packed));
  252. struct xhci_TRB_cmd_complete_t
  253. {
  254. uint64_t cmd_trb_pointer_paddr; // 指向生成当前Event TRB的TRB的物理地址(16bytes对齐)
  255. unsigned resv1 : 24; // Reserved and zero'd
  256. uint8_t code; // Completion code
  257. unsigned cycle : 1; // cycle bit
  258. unsigned resv2 : 9; // Reserved and zero'd
  259. unsigned TRB_type : 6; // TRB类型
  260. uint8_t VF_ID;
  261. uint8_t slot_id; // the id of the slot associated with the
  262. // command that generated the event
  263. } __attribute__((packed));
  264. // ========= TRB end ===========
  265. // ======== Runtime Register Set Begin =========
  266. #define XHCI_RT_IR0 0x20 // 中断寄存器组0距离runtime Register set起始位置的偏移量
  267. #define XHCI_IR_SIZE 32 // 中断寄存器组大小
  268. // 中断寄存器组内的偏移量
  269. #define XHCI_IR_MAN 0x00 // Interrupter Management Register
  270. #define XHCI_IR_MOD 0x04 // Interrupter Moderation
  271. #define XHCI_IR_TABLE_SIZE 0x08 // Event Ring Segment Table size (count of segments)
  272. #define XHCI_IR_TABLE_ADDR 0x10 // Event Ring Segment Table Base Address
  273. #define XHCI_IR_DEQUEUE 0x18 // Event Ring Dequeue Pointer
  274. // MAN寄存器内的bit的含义
  275. #define XHCI_IR_IMR_PENDING (1 << 0) // Interrupt pending bit in Management Register
  276. #define XHCI_IR_IMR_ENABLE (1 << 1) // Interrupt enable bit in Management Register
  277. struct xhci_intr_moderation_t
  278. {
  279. uint16_t interval; // 产生一个中断的时间,是interval*250ns (wait before next interrupt)
  280. uint16_t counter;
  281. } __attribute__((packed));
  282. // ======== Runtime Register Set END =========
  283. // ======= xhci Extended Capabilities List BEGIN========
  284. // ID 部分的含义定义
  285. #define XHCI_XECP_ID_RESERVED 0
  286. #define XHCI_XECP_ID_LEGACY 1 // USB Legacy Support
  287. #define XHCI_XECP_ID_PROTOCOL 2 // Supported protocol
  288. #define XHCI_XECP_ID_POWER 3 // Extended power management
  289. #define XHCI_XECP_ID_IOVIRT 4 // I/0 virtualization
  290. #define XHCI_XECP_ID_MSG 5 // Message interrupt
  291. #define XHCI_XECP_ID_LOCAL_MEM 6 // local memory
  292. #define XHCI_XECP_ID_DEBUG 10 // USB Debug capability
  293. #define XHCI_XECP_ID_EXTMSG 17 // Extended message interrupt
  294. #define XHCI_XECP_LEGACY_TIMEOUT 10 // 设置legacy状态的等待时间
  295. #define XHCI_XECP_LEGACY_BIOS_OWNED (1 << 16) // 当bios控制着该hc时,该位被置位
  296. #define XHCI_XECP_LEGACY_OS_OWNED (1 << 24) // 当系统控制着该hc时,该位被置位
  297. #define XHCI_XECP_LEGACY_OWNING_MASK (XHCI_XECP_LEGACY_BIOS_OWNED | XHCI_XECP_LEGACY_OS_OWNED)
  298. // ======= xhci Extended Capabilities List END ========
  299. // ======= Port status and control registers BEGIN ====
  300. #define XHCI_PORT_PORTSC 0x00 // Port status and control
  301. #define XHCI_PORT_PORTPMSC 0x04 // Port power management status and control
  302. #define XHCI_PORT_PORTLI 0x08 // Port Link info
  303. #define XHCI_PORT_PORTHLMPC 0x0c // Port hardware LPM control (version 1.10 only
  304. #define XHCI_PORTUSB_CHANGE_BITS ((1 << 17) | (1 << 18) | (1 << 20) | (1 << 21) | (1 << 22))
  305. // 存储于portsc中的端口速度的可用值
  306. #define XHCI_PORT_SPEED_FULL 1
  307. #define XHCI_PORT_SPEED_LOW 2
  308. #define XHCI_PORT_SPEED_HI 3
  309. #define XHCI_PORT_SPEED_SUPER 4
  310. // ======= Port status and control registers END ====
  311. // ======= Device Slot Context BEGIN ====
  312. /**
  313. * @brief 设备上下文结构体
  314. *
  315. */
  316. struct xhci_slot_context_t
  317. {
  318. unsigned route_string : 20;
  319. unsigned speed : 4;
  320. unsigned Rsvd0 : 1; // Reserved and zero'd
  321. unsigned mtt : 1; // multi-TT
  322. unsigned hub : 1;
  323. unsigned entries : 5; // count of context entries
  324. uint16_t max_exit_latency;
  325. uint8_t rh_port_num; // root hub port number
  326. uint8_t num_ports; // number of ports
  327. uint8_t tt_hub_slot_id;
  328. uint8_t tt_port_num;
  329. unsigned ttt : 2; // TT Think Time
  330. unsigned Rsvd2 : 4;
  331. unsigned int_target : 10; // Interrupter target
  332. uint8_t device_address;
  333. unsigned Rsvd1 : 19;
  334. unsigned slot_state : 5;
  335. } __attribute__((packed));
  336. #define XHCI_SLOT_STATE_DISABLED_OR_ENABLED 0
  337. #define XHCI_SLOT_STATE_DEFAULT 1
  338. #define XHCI_SLOT_STATE_ADDRESSED 2
  339. #define XHCI_SLOT_STATE_CONFIGURED 3
  340. // ======= Device Slot Context END ====
  341. // ======= Device Endpoint Context BEGIN ====
  342. #define XHCI_EP_STATE_DISABLED 0
  343. #define XHCI_EP_STATE_RUNNING 1
  344. #define XHCI_EP_STATE_HALTED 2
  345. #define XHCI_EP_STATE_STOPPED 3
  346. #define XHCI_EP_STATE_ERROR 4
  347. // End Point Doorbell numbers
  348. #define XHCI_SLOT_CNTX 0
  349. #define XHCI_EP_CONTROL 1
  350. #define XHCI_EP1_OUT 2
  351. #define XHCI_EP1_IN 3
  352. #define XHCI_EP2_OUT 4
  353. #define XHCI_EP2_IN 5
  354. #define XHCI_EP3_OUT 6
  355. #define XHCI_EP3_IN 7
  356. #define XHCI_EP4_OUT 8
  357. #define XHCI_EP4_IN 9
  358. #define XHCI_EP5_OUT 10
  359. #define XHCI_EP5_IN 11
  360. #define XHCI_EP6_OUT 12
  361. #define XHCI_EP6_IN 13
  362. #define XHCI_EP7_OUT 14
  363. #define XHCI_EP7_IN 15
  364. #define XHCI_EP8_OUT 16
  365. #define XHCI_EP8_IN 17
  366. #define XHCI_EP9_OUT 18
  367. #define XHCI_EP9_IN 19
  368. #define XHCI_EP10_OUT 20
  369. #define XHCI_EP10_IN 21
  370. #define XHCI_EP11_OUT 22
  371. #define XHCI_EP11_IN 23
  372. #define XHCI_EP12_OUT 24
  373. #define XHCI_EP12_IN 25
  374. #define XHCI_EP13_OUT 26
  375. #define XHCI_EP13_IN 27
  376. #define XHCI_EP14_OUT 28
  377. #define XHCI_EP14_IN 29
  378. #define XHCI_EP15_OUT 30
  379. #define XHCI_EP15_IN 31
  380. // xhci 传输方向(用于setup stage TRB)
  381. #define XHCI_DIR_NO_DATA 0
  382. #define XHCI_DIR_OUT 2
  383. #define XHCI_DIR_IN 3
  384. // xhci传输方向(单个bit的表示)
  385. #define XHCI_DIR_OUT_BIT 0
  386. #define XHCI_DIR_IN_BIT 1
  387. /**
  388. * @brief xhci 端点上下文结构体
  389. *
  390. */
  391. struct xhci_ep_context_t
  392. {
  393. unsigned ep_state : 3;
  394. unsigned Rsvd0 : 5; // Reserved and zero'd
  395. unsigned mult : 2; // the maximum supported number of bursts within an interval
  396. unsigned max_primary_streams : 5;
  397. unsigned linear_stream_array : 1;
  398. uint8_t interval;
  399. uint8_t max_esti_payload_hi; // Max Endpoint Service Time Interval Payload (High 8bit)
  400. unsigned Rsvd1 : 1;
  401. unsigned err_cnt : 2; // error count. 当错误发生时,该位会自减。当减为0时,控制器会把这个端点挂起
  402. unsigned ep_type : 3; // endpoint type
  403. unsigned Rsvd2 : 1;
  404. unsigned hid : 1; // Host Initiate Disable
  405. uint8_t max_burst_size;
  406. uint16_t max_packet_size;
  407. uint64_t tr_dequeue_ptr; // 第0bit为dequeue cycle state, 第1~3bit应保留。
  408. uint16_t average_trb_len; // 平均TRB长度。该部分不应为0
  409. uint16_t max_esti_payload_lo; // Max Endpoint Service Time Interval Payload (Low 16bit)
  410. } __attribute__((packed));
  411. // ======= Device Endpoint Context END ====
  412. // 端口信息标志位
  413. #define XHCI_PROTOCOL_USB2 0
  414. #define XHCI_PROTOCOL_USB3 1
  415. #define XHCI_PROTOCOL_INFO (1 << 0) // 1->usb3, 0->usb2
  416. #define XHCI_PROTOCOL_HSO (1 << 1) // 1-> usb2 high speed only
  417. #define XHCI_PROTOCOL_HAS_PAIR (1 << 2) // 当前位被置位,意味着当前端口具有一个与之配对的端口
  418. #define XHCI_PROTOCOL_ACTIVE (1 << 3) // 当前端口是这个配对中,被激活的端口
  419. struct xhci_ep_info_t
  420. {
  421. uint64_t ep_ring_vbase; // transfer ring的基地址
  422. uint64_t current_ep_ring_vaddr; // transfer ring下一个要写入的地址
  423. uint8_t current_ep_ring_cycle; // 当前ep的cycle bit
  424. };
  425. /**
  426. * @brief xhci端口信息
  427. *
  428. */
  429. struct xhci_port_info_t
  430. {
  431. uint8_t flags; // port flags
  432. uint8_t paired_port_num; // 与当前端口所配对的另一个端口(相同物理接口的不同速度的port)
  433. uint8_t offset; // offset of this port within this protocal
  434. uint8_t reserved;
  435. uint8_t slot_id; // address device获得的slot id
  436. struct usb_device_desc *dev_desc; // 指向设备描述符结构体的指针
  437. struct xhci_ep_info_t ep_info[32]; // 各个端点的信息
  438. } __attribute__((packed));
  439. struct xhci_host_controller_t
  440. {
  441. struct pci_device_structure_general_device_t *pci_dev_hdr; // 指向pci header结构体的指针
  442. int controller_id; // 操作系统给controller的编号
  443. uint64_t vbase; // 虚拟地址base(bar0映射到的虚拟地址)
  444. uint64_t vbase_op; // Operational registers 起始虚拟地址
  445. uint32_t rts_offset; // Runtime Register Space offset
  446. uint32_t db_offset; // Doorbell offset
  447. uint32_t ext_caps_off; // 扩展能力寄存器偏移量
  448. uint16_t port_num; // 总的端口数量
  449. uint8_t context_size; // 设备上下文大小
  450. uint8_t port_num_u2; // usb 2.0端口数量
  451. uint8_t port_num_u3; // usb 3端口数量
  452. uint8_t current_event_ring_cycle; // 当前event ring cycle
  453. uint8_t cmd_trb_cycle; // 当前command ring cycle
  454. uint32_t page_size; // page size
  455. uint64_t dcbaap_vaddr; // Device Context Base Address Array Pointer的虚拟地址
  456. uint64_t cmd_ring_vaddr; // command ring的虚拟地址
  457. uint64_t cmd_trb_vaddr; // 下一个要写入的trb的虚拟地址
  458. uint64_t event_ring_vaddr; // event ring的虚拟地址
  459. uint64_t event_ring_table_vaddr; // event ring table的虚拟地址
  460. uint64_t current_event_ring_vaddr; // 下一个要读取的event TRB的虚拟地址
  461. uint64_t scratchpad_buf_array_vaddr; // 草稿行缓冲区数组的虚拟地址
  462. struct xhci_port_info_t ports[XHCI_MAX_ROOT_HUB_PORTS]; // 指向端口信息数组的指针(由于端口offset是从1开始的,因此该数组第0项为空)
  463. };
  464. // Common TRB types
  465. enum
  466. {
  467. TRB_TYPE_NORMAL = 1,
  468. TRB_TYPE_SETUP_STAGE,
  469. TRB_TYPE_DATA_STAGE,
  470. TRB_TYPE_STATUS_STAGE,
  471. TRB_TYPE_ISOCH,
  472. TRB_TYPE_LINK,
  473. TRB_TYPE_EVENT_DATA,
  474. TRB_TYPE_NO_OP,
  475. TRB_TYPE_ENABLE_SLOT,
  476. TRB_TYPE_DISABLE_SLOT = 10,
  477. TRB_TYPE_ADDRESS_DEVICE = 11,
  478. TRB_TYPE_CONFIG_EP,
  479. TRB_TYPE_EVALUATE_CONTEXT,
  480. TRB_TYPE_RESET_EP,
  481. TRB_TYPE_STOP_EP = 15,
  482. TRB_TYPE_SET_TR_DEQUEUE,
  483. TRB_TYPE_RESET_DEVICE,
  484. TRB_TYPE_FORCE_EVENT,
  485. TRB_TYPE_DEG_BANDWIDTH,
  486. TRB_TYPE_SET_LAT_TOLERANCE = 20,
  487. TRB_TYPE_GET_PORT_BAND = 21,
  488. TRB_TYPE_FORCE_HEADER,
  489. TRB_TYPE_NO_OP_CMD, // 24 - 31 = reserved
  490. TRB_TYPE_TRANS_EVENT = 32,
  491. TRB_TYPE_COMMAND_COMPLETION,
  492. TRB_TYPE_PORT_STATUS_CHANGE,
  493. TRB_TYPE_BANDWIDTH_REQUEST,
  494. TRB_TYPE_DOORBELL_EVENT,
  495. TRB_TYPE_HOST_CONTROLLER_EVENT = 37,
  496. TRB_TYPE_DEVICE_NOTIFICATION,
  497. TRB_TYPE_MFINDEX_WRAP,
  498. // 40 - 47 = reserved
  499. // 48 - 63 = Vendor Defined
  500. };
  501. // event ring trb的完成码
  502. enum
  503. {
  504. TRB_COMP_TRB_SUCCESS = 1,
  505. TRB_COMP_DATA_BUFFER_ERROR,
  506. TRB_COMP_BABBLE_DETECTION,
  507. TRB_COMP_TRANSACTION_ERROR,
  508. TRB_COMP_TRB_ERROR,
  509. TRB_COMP_STALL_ERROR,
  510. TRB_COMP_RESOURCE_ERROR = 7,
  511. TRB_COMP_BANDWIDTH_ERROR,
  512. TRB_COMP_NO_SLOTS_ERROR,
  513. TRB_COMP_INVALID_STREAM_TYPE,
  514. TRB_COMP_SLOT_NOT_ENABLED,
  515. TRB_COMP_EP_NOT_ENABLED,
  516. TRB_COMP_SHORT_PACKET = 13,
  517. TRB_COMP_RING_UNDERRUN,
  518. TRB_COMP_RUNG_OVERRUN,
  519. TRB_COMP_VF_EVENT_RING_FULL,
  520. TRB_COMP_PARAMETER_ERROR,
  521. TRB_COMP_BANDWITDH_OVERRUN,
  522. TRB_COMP_CONTEXT_STATE_ERROR = 19,
  523. TRB_COMP_NO_PING_RESPONSE,
  524. TRB_COMP_EVENT_RING_FULL,
  525. TRB_COMP_INCOMPATIBLE_DEVICE,
  526. TRB_COMP_MISSED_SERVICE,
  527. TRB_COMP_COMMAND_RING_STOPPED = 24,
  528. TRB_COMP_COMMAND_ABORTED,
  529. TRB_COMP_STOPPED,
  530. TRB_COMP_STOPPER_LENGTH_ERROR,
  531. TRB_COMP_RESERVED,
  532. TRB_COMP_ISOCH_BUFFER_OVERRUN,
  533. TRB_COMP_EVERN_LOST = 32,
  534. TRB_COMP_UNDEFINED,
  535. TRB_COMP_INVALID_STREAM_ID,
  536. TRB_COMP_SECONDARY_BANDWIDTH,
  537. TRB_COMP_SPLIT_TRANSACTION
  538. /* 37 - 191 reserved */
  539. /* 192 - 223 vender defined errors */
  540. /* 224 - 225 vendor defined info */
  541. };
  542. /**
  543. * @brief xhci endpoint类型
  544. *
  545. */
  546. enum
  547. {
  548. XHCI_EP_TYPE_INVALID = 0,
  549. XHCI_EP_TYPE_ISO_OUT,
  550. XHCI_EP_TYPE_BULK_OUT,
  551. XHCI_EP_TYPE_INTR_OUT,
  552. XHCI_EP_TYPE_CONTROL,
  553. XHCI_EP_TYPE_ISO_IN,
  554. XHCI_EP_TYPE_BULK_IN,
  555. XHCI_EP_TYPE_INTR_IN,
  556. };
  557. /**
  558. * @brief 初始化xhci控制器
  559. *
  560. * @param header 指定控制器的pci device头部
  561. */
  562. void xhci_init(struct pci_device_structure_general_device_t *header);