usb_hub.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2022, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef USB_HUB_H
  7. #define USB_HUB_H
  8. /* HUB Class Descriptor Types */
  9. #define HUB_DESCRIPTOR_TYPE_HUB 0x29
  10. #define HUB_DESCRIPTOR_TYPE_HUB3 0x2A
  11. #define HUB_MAX_DEPTH 5
  12. #define HUB_SUBCLASS 0x00
  13. #define HUB_PROTOCOL_STT 0x01
  14. #define HUB_PROTOCOL_MTT 0x02
  15. /* Hub class requests */
  16. #define HUB_REQUEST_GET_STATUS USB_REQUEST_GET_STATUS
  17. #define HUB_REQUEST_CLEAR_FEATURE USB_REQUEST_CLEAR_FEATURE
  18. #define HUB_REQUEST_SET_FEATURE USB_REQUEST_SET_FEATURE
  19. #define HUB_REQUEST_GET_DESCRIPTOR USB_REQUEST_GET_DESCRIPTOR
  20. #define HUB_REQUEST_SET_DESCRIPTOR USB_REQUEST_SET_DESCRIPTOR
  21. #define HUB_REQUEST_CLEAR_TT_BUFFER (0x08)
  22. #define HUB_REQUEST_RESET_TT (0x09)
  23. #define HUB_REQUEST_GET_TT_STATE (0x0a)
  24. #define HUB_REQUEST_STOP_TT (0x0b)
  25. #define HUB_REQUEST_SET_HUB_DEPTH (0x0C)
  26. /* Hub class features */
  27. #define HUB_FEATURE_HUB_C_LOCALPOWER (0x0)
  28. #define HUB_FEATURE_HUB_C_OVERCURRENT (0x1)
  29. /* Port features */
  30. #define HUB_PORT_FEATURE_CONNECTION (0x00)
  31. #define HUB_PORT_FEATURE_ENABLE (0x01)
  32. #define HUB_PORT_FEATURE_SUSPEND (0x02)
  33. #define HUB_PORT_FEATURE_OVERCURRENT (0x03)
  34. #define HUB_PORT_FEATURE_RESET (0x04)
  35. #define HUB_PORT_FEATURE_L1 (0x05) /* USB 2.0 only */
  36. #define HUB_PORT_FEATURE_POWER (0x08) /* USB 2.0 only */
  37. #define HUB_PORT_FEATURE_POWER_SS (0x09) /* USB 3.0 only */
  38. /* This is a bit tricky because HUB_PORT_FEATURE_POWER_SS and
  39. HUB_PORT_FEATURE_LOWSPEED share the same bit. */
  40. #define HUB_PORT_FEATURE_LOWSPEED (0x09) /* USB 2.0 only */
  41. #define HUB_PORT_FEATURE_HIGHSPEED (0x0a) /* USB 2.0 only */
  42. #define HUB_PORT_FEATURE_TEST (0x0b) /* USB 2.0 only */
  43. #define HUB_PORT_FEATURE_INDICATOR (0x0c) /* USB 2.0 only */
  44. /* Port status change (wPortChange) */
  45. #define HUB_PORT_FEATURE_C_CONNECTION (0x10)
  46. #define HUB_PORT_FEATURE_C_ENABLE (0x11) /* USB 2.0 only */
  47. #define HUB_PORT_FEATURE_C_SUSPEND (0x12) /* USB 2.0 only */
  48. #define HUB_PORT_FEATURE_C_OVER_CURREN (0x13)
  49. #define HUB_PORT_FEATURE_C_RESET (0x14)
  50. #define HUB_PORT_FEATURE_C_BH_RESET (0x15) /* USB 3.0 only */
  51. #define HUB_PORT_FEATURE_C_LINK_STATE (0x16) /* USB 3.0 only */
  52. #define HUB_PORT_FEATURE_C_CONFIG_ERR (0x17) /* USB 3.0 only */
  53. /* Hub status */
  54. #define HUB_STATUS_LOCALPOWER (1 << 0)
  55. #define HUB_STATUS_OVERCURRENT (1 << 1)
  56. /* Hub status change */
  57. #define HUB_STATUS_C_LOCALPOWER (1 << 0)
  58. #define HUB_STATUS_C_OVERCURRENT (1 << 1)
  59. /* Hub port status */
  60. #define HUB_PORT_STATUS_CONNECTION (1 << 0)
  61. #define HUB_PORT_STATUS_ENABLE (1 << 1)
  62. #define HUB_PORT_STATUS_SUSPEND (1 << 2) /* USB 2.0 only */
  63. #define HUB_PORT_STATUS_OVERCURRENT (1 << 3)
  64. #define HUB_PORT_STATUS_RESET (1 << 4)
  65. #define HUB_PORT_STATUS_L1 (1 << 5) /* USB 2.0 only */
  66. /* Port Link State (PORT_LINK_STATE), USB 3.0 only */
  67. #define HUB_PORT_STATUS_LS_U0 (0x00 << 5)
  68. #define HUB_PORT_STATUS_LS_U1 (0x01 << 5)
  69. #define HUB_PORT_STATUS_LS_U2 (0x02 << 5)
  70. #define HUB_PORT_STATUS_LS_U3 (0x03 << 5)
  71. #define HUB_PORT_STATUS_LS_SS_DISABLED (0x04 << 5)
  72. #define HUB_PORT_STATUS_LS_RX_DETECT (0x05 << 5)
  73. #define HUB_PORT_STATUS_LS_SS_INACTIVE (0x06 << 5)
  74. #define HUB_PORT_STATUS_LS_POLLING (0x07 << 5)
  75. #define HUB_PORT_STATUS_LS_RECOVERY (0x08 << 5)
  76. #define HUB_PORT_STATUS_LS_HOT_RESET (0x09 << 5)
  77. #define HUB_PORT_STATUS_LS_COMP_MOD (0x0a << 5)
  78. #define HUB_PORT_STATUS_LS_LOOPBACK (0x0b << 5)
  79. #define HUB_PORT_STATUS_POWER (1 << 8)
  80. #define HUB_PORT_STATUS_POWER_SS (1 << 9) /* USB 3.0 only */
  81. #define HUB_PORT_STATUS_LOW_SPEED (1 << 9) /* USB 2.0 only */
  82. #define HUB_PORT_STATUS_HIGH_SPEED (1 << 10) /* USB 2.0 only */
  83. #define HUB_PORT_STATUS_TEST (1 << 11) /* USB 2.0 only */
  84. #define HUB_PORT_STATUS_INDICATOR (1 << 12) /* USB 2.0 only */
  85. /* Hub port status change */
  86. #define HUB_PORT_STATUS_C_CONNECTION (1 << 0)
  87. #define HUB_PORT_STATUS_C_ENABLE (1 << 1) /* USB 2.0 only */
  88. #define HUB_PORT_STATUS_C_SUSPEND (1 << 2) /* USB 2.0 only */
  89. #define HUB_PORT_STATUS_C_OVERCURRENT (1 << 3)
  90. #define HUB_PORT_STATUS_C_RESET (1 << 4)
  91. #define HUB_PORT_STATUS_C_L1 (1 << 5) /* USB 2.0 only */
  92. #define HUB_PORT_STATUS_C_BH_RESET (1 << 5) /* USB 3.0 only */
  93. #define HUB_PORT_STATUS_C_PORTLINK (1 << 6) /* USB 3.0 only */
  94. #define HUB_PORT_STATUS_C_CONFIGERR (1 << 7) /* USB 3.0 only */
  95. /* Hub characteristics */
  96. #define HUB_CHAR_LPSM_SHIFT (0) /* Bits 0-1: Logical Power Switching Mode */
  97. #define HUB_CHAR_LPSM_MASK (3 << HUB_CHAR_LPSM_SHIFT)
  98. #define HUB_CHAR_LPSM_GANGED (0 << HUB_CHAR_LPSM_SHIFT)
  99. #define HUB_CHAR_LPSM_INDIVIDUAL (1 << HUB_CHAR_LPSM_SHIFT)
  100. #define HUB_CHAR_COMPOUND (1 << 2) /* Bit 2: Compound device */
  101. #define HUB_CHAR_OCPM_SHIFT (3) /* Bits 3-4: Over-current Protection Mode */
  102. #define HUB_CHAR_OCPM_MASK (3 << HUB_CHAR_OCPM_SHIFT)
  103. #define HUB_CHAR_OCPM_GLOBAL (0 << HUB_CHAR_OCPM_SHIFT)
  104. #define HUB_CHAR_OCPM_INDIVIDUAL (1 << HUB_CHAR_OCPM_SHIFT)
  105. #define HUB_CHAR_TTTT_SHIFT (5) /* Bits 5-6: TT Think Time */
  106. #define HUB_CHAR_TTTT_MASK (3 << HUB_CHAR_TTTT_SHIFT)
  107. #define HUB_CHAR_TTTT_8_BITS (0 << HUB_CHAR_TTTT_SHIFT)
  108. #define HUB_CHAR_TTTT_16_BITS (1 << HUB_CHAR_TTTT_SHIFT)
  109. #define HUB_CHAR_TTTT_24_BITS (2 << HUB_CHAR_TTTT_SHIFT)
  110. #define HUB_CHAR_TTTT_32_BITS (3 << HUB_CHAR_TTTT_SHIFT)
  111. #define HUB_CHAR_PORTIND (1 << 7) /* Bit 7: Port Indicators Supported */
  112. /* Hub descriptor */
  113. struct usb_hub_descriptor {
  114. uint8_t bLength;
  115. uint8_t bDescriptorType;
  116. uint8_t bNbrPorts;
  117. uint16_t wHubCharacteristics;
  118. uint8_t bPwrOn2PwrGood;
  119. uint8_t bHubContrCurrent;
  120. uint8_t DeviceRemovable;
  121. uint8_t PortPwrCtrlMask;
  122. } __PACKED;
  123. #define USB_SIZEOF_HUB_DESC 9
  124. /* Super speed Hub descriptor */
  125. struct usb_hub_ss_descriptor {
  126. uint8_t bLength;
  127. uint8_t bDescriptorType;
  128. uint8_t bNbrPorts;
  129. uint16_t wHubCharacteristics;
  130. uint8_t bPwrOn2PwrGood;
  131. uint8_t bHubContrCurrent;
  132. uint8_t bHubHdrDecLat;
  133. uint16_t wHubDelay;
  134. uint8_t DeviceRemovable;
  135. } __PACKED;
  136. #define USB_SIZEOF_HUB_SS_DESC 11
  137. /* Hub status */
  138. struct hub_status {
  139. uint16_t wPortStatus;
  140. uint16_t wPortChange;
  141. };
  142. /* Hub port status */
  143. struct hub_port_status {
  144. uint16_t wPortStatus;
  145. uint16_t wPortChange;
  146. };
  147. #endif /* USB_HUB_H */