You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

usbhub.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
  2. This software may be distributed and modified under the terms of the GNU
  3. General Public License version 2 (GPL2) as published by the Free Software
  4. Foundation and appearing in the file GPL2.TXT included in the packaging of
  5. this file. Please note that GPL2 Section 2[b] requires that all works based
  6. on this software must also be made publicly available under the terms of
  7. the GPL2 ("Copyleft").
  8. Contact information
  9. -------------------
  10. Circuits At Home, LTD
  11. Web : http://www.circuitsathome.com
  12. e-mail : [email protected]
  13. */
  14. #if !defined(__USBHUB_H__)
  15. #define __USBHUB_H__
  16. #include "Usb.h"
  17. #define USB_DESCRIPTOR_HUB 0x09 // Hub descriptor type
  18. // Hub Requests
  19. #define bmREQ_CLEAR_HUB_FEATURE USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE
  20. #define bmREQ_CLEAR_PORT_FEATURE USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
  21. #define bmREQ_CLEAR_TT_BUFFER USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
  22. #define bmREQ_GET_HUB_DESCRIPTOR USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE
  23. #define bmREQ_GET_HUB_STATUS USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE
  24. #define bmREQ_GET_PORT_STATUS USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
  25. #define bmREQ_RESET_TT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
  26. #define bmREQ_SET_HUB_DESCRIPTOR USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE
  27. #define bmREQ_SET_HUB_FEATURE USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE
  28. #define bmREQ_SET_PORT_FEATURE USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
  29. #define bmREQ_GET_TT_STATE USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
  30. #define bmREQ_STOP_TT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
  31. // Hub Class Requests
  32. #define HUB_REQUEST_CLEAR_TT_BUFFER 8
  33. #define HUB_REQUEST_RESET_TT 9
  34. #define HUB_REQUEST_GET_TT_STATE 10
  35. #define HUB_REQUEST_STOP_TT 11
  36. // Hub Features
  37. #define HUB_FEATURE_C_HUB_LOCAL_POWER 0
  38. #define HUB_FEATURE_C_HUB_OVER_CURRENT 1
  39. #define HUB_FEATURE_PORT_CONNECTION 0
  40. #define HUB_FEATURE_PORT_ENABLE 1
  41. #define HUB_FEATURE_PORT_SUSPEND 2
  42. #define HUB_FEATURE_PORT_OVER_CURRENT 3
  43. #define HUB_FEATURE_PORT_RESET 4
  44. #define HUB_FEATURE_PORT_POWER 8
  45. #define HUB_FEATURE_PORT_LOW_SPEED 9
  46. #define HUB_FEATURE_C_PORT_CONNECTION 16
  47. #define HUB_FEATURE_C_PORT_ENABLE 17
  48. #define HUB_FEATURE_C_PORT_SUSPEND 18
  49. #define HUB_FEATURE_C_PORT_OVER_CURRENT 19
  50. #define HUB_FEATURE_C_PORT_RESET 20
  51. #define HUB_FEATURE_PORT_TEST 21
  52. #define HUB_FEATURE_PORT_INDICATOR 22
  53. // Hub Port Test Modes
  54. #define HUB_PORT_TEST_MODE_J 1
  55. #define HUB_PORT_TEST_MODE_K 2
  56. #define HUB_PORT_TEST_MODE_SE0_NAK 3
  57. #define HUB_PORT_TEST_MODE_PACKET 4
  58. #define HUB_PORT_TEST_MODE_FORCE_ENABLE 5
  59. // Hub Port Indicator Color
  60. #define HUB_PORT_INDICATOR_AUTO 0
  61. #define HUB_PORT_INDICATOR_AMBER 1
  62. #define HUB_PORT_INDICATOR_GREEN 2
  63. #define HUB_PORT_INDICATOR_OFF 3
  64. // Hub Port Status Bitmasks
  65. #define bmHUB_PORT_STATUS_PORT_CONNECTION 0x0001
  66. #define bmHUB_PORT_STATUS_PORT_ENABLE 0x0002
  67. #define bmHUB_PORT_STATUS_PORT_SUSPEND 0x0004
  68. #define bmHUB_PORT_STATUS_PORT_OVER_CURRENT 0x0008
  69. #define bmHUB_PORT_STATUS_PORT_RESET 0x0010
  70. #define bmHUB_PORT_STATUS_PORT_POWER 0x0100
  71. #define bmHUB_PORT_STATUS_PORT_LOW_SPEED 0x0200
  72. #define bmHUB_PORT_STATUS_PORT_HIGH_SPEED 0x0400
  73. #define bmHUB_PORT_STATUS_PORT_TEST 0x0800
  74. #define bmHUB_PORT_STATUS_PORT_INDICATOR 0x1000
  75. // Hub Port Status Change Bitmasks (used one byte instead of two)
  76. #define bmHUB_PORT_STATUS_C_PORT_CONNECTION 0x0001
  77. #define bmHUB_PORT_STATUS_C_PORT_ENABLE 0x0002
  78. #define bmHUB_PORT_STATUS_C_PORT_SUSPEND 0x0004
  79. #define bmHUB_PORT_STATUS_C_PORT_OVER_CURRENT 0x0008
  80. #define bmHUB_PORT_STATUS_C_PORT_RESET 0x0010
  81. // Hub Status Bitmasks (used one byte instead of two)
  82. #define bmHUB_STATUS_LOCAL_POWER_SOURCE 0x01
  83. #define bmHUB_STATUS_OVER_CURRENT 0x12
  84. // Hub Status Change Bitmasks (used one byte instead of two)
  85. #define bmHUB_STATUS_C_LOCAL_POWER_SOURCE 0x01
  86. #define bmHUB_STATUS_C_OVER_CURRENT 0x12
  87. // Hub Port Configuring Substates
  88. #define USB_STATE_HUB_PORT_CONFIGURING 0xb0
  89. #define USB_STATE_HUB_PORT_POWERED_OFF 0xb1
  90. #define USB_STATE_HUB_PORT_WAIT_FOR_POWER_GOOD 0xb2
  91. #define USB_STATE_HUB_PORT_DISCONNECTED 0xb3
  92. #define USB_STATE_HUB_PORT_DISABLED 0xb4
  93. #define USB_STATE_HUB_PORT_RESETTING 0xb5
  94. #define USB_STATE_HUB_PORT_ENABLED 0xb6
  95. // Additional Error Codes
  96. #define HUB_ERROR_PORT_HAS_BEEN_RESET 0xb1
  97. // The bit mask to check for all necessary state bits
  98. #define bmHUB_PORT_STATUS_ALL_MAIN ((0UL | bmHUB_PORT_STATUS_C_PORT_CONNECTION | bmHUB_PORT_STATUS_C_PORT_ENABLE | bmHUB_PORT_STATUS_C_PORT_SUSPEND | bmHUB_PORT_STATUS_C_PORT_RESET) << 16) | bmHUB_PORT_STATUS_PORT_POWER | bmHUB_PORT_STATUS_PORT_ENABLE | bmHUB_PORT_STATUS_PORT_CONNECTION | bmHUB_PORT_STATUS_PORT_SUSPEND)
  99. // Bit mask to check for DISABLED state in HubEvent::bmStatus field
  100. #define bmHUB_PORT_STATE_CHECK_DISABLED (0x0000 | bmHUB_PORT_STATUS_PORT_POWER | bmHUB_PORT_STATUS_PORT_ENABLE | bmHUB_PORT_STATUS_PORT_CONNECTION | bmHUB_PORT_STATUS_PORT_SUSPEND)
  101. // Hub Port States
  102. #define bmHUB_PORT_STATE_DISABLED (0x0000 | bmHUB_PORT_STATUS_PORT_POWER | bmHUB_PORT_STATUS_PORT_CONNECTION)
  103. // Hub Port Events
  104. #define bmHUB_PORT_EVENT_CONNECT (((0UL | bmHUB_PORT_STATUS_C_PORT_CONNECTION) << 16) | bmHUB_PORT_STATUS_PORT_POWER | bmHUB_PORT_STATUS_PORT_CONNECTION)
  105. #define bmHUB_PORT_EVENT_DISCONNECT (((0UL | bmHUB_PORT_STATUS_C_PORT_CONNECTION) << 16) | bmHUB_PORT_STATUS_PORT_POWER)
  106. #define bmHUB_PORT_EVENT_RESET_COMPLETE (((0UL | bmHUB_PORT_STATUS_C_PORT_RESET) << 16) | bmHUB_PORT_STATUS_PORT_POWER | bmHUB_PORT_STATUS_PORT_ENABLE | bmHUB_PORT_STATUS_PORT_CONNECTION)
  107. #define bmHUB_PORT_EVENT_LS_CONNECT (((0UL | bmHUB_PORT_STATUS_C_PORT_CONNECTION) << 16) | bmHUB_PORT_STATUS_PORT_POWER | bmHUB_PORT_STATUS_PORT_CONNECTION | bmHUB_PORT_STATUS_PORT_LOW_SPEED)
  108. #define bmHUB_PORT_EVENT_LS_RESET_COMPLETE (((0UL | bmHUB_PORT_STATUS_C_PORT_RESET) << 16) | bmHUB_PORT_STATUS_PORT_POWER | bmHUB_PORT_STATUS_PORT_ENABLE | bmHUB_PORT_STATUS_PORT_CONNECTION | bmHUB_PORT_STATUS_PORT_LOW_SPEED)
  109. #define bmHUB_PORT_EVENT_LS_PORT_ENABLED (((0UL | bmHUB_PORT_STATUS_C_PORT_CONNECTION | bmHUB_PORT_STATUS_C_PORT_ENABLE) << 16) | bmHUB_PORT_STATUS_PORT_POWER | bmHUB_PORT_STATUS_PORT_ENABLE | bmHUB_PORT_STATUS_PORT_CONNECTION | bmHUB_PORT_STATUS_PORT_LOW_SPEED)
  110. struct HubDescriptor {
  111. uint8_t bDescLength; // descriptor length
  112. uint8_t bDescriptorType; // descriptor type
  113. uint8_t bNbrPorts; // number of ports a hub equiped with
  114. struct {
  115. uint16_t LogPwrSwitchMode : 2;
  116. uint16_t CompoundDevice : 1;
  117. uint16_t OverCurrentProtectMode : 2;
  118. uint16_t TTThinkTime : 2;
  119. uint16_t PortIndicatorsSupported : 1;
  120. uint16_t Reserved : 8;
  121. } __attribute__((packed));
  122. uint8_t bPwrOn2PwrGood;
  123. uint8_t bHubContrCurrent;
  124. } __attribute__((packed));
  125. struct HubEvent {
  126. union {
  127. struct {
  128. uint16_t bmStatus; // port status bits
  129. uint16_t bmChange; // port status change bits
  130. } __attribute__((packed));
  131. uint32_t bmEvent;
  132. uint8_t evtBuff[4];
  133. };
  134. } __attribute__((packed));
  135. class USBHub : USBDeviceConfig {
  136. static bool bResetInitiated; // True when reset is triggered
  137. USB *pUsb; // USB class instance pointer
  138. EpInfo epInfo[2]; // interrupt endpoint info structure
  139. uint8_t bAddress; // address
  140. uint8_t bNbrPorts; // number of ports
  141. // uint8_t bInitState; // initialization state variable
  142. uint32_t qNextPollTime; // next poll time
  143. bool bPollEnable; // poll enable flag
  144. uint8_t CheckHubStatus();
  145. uint8_t PortStatusChange(uint8_t port, HubEvent &evt);
  146. public:
  147. USBHub(USB *p);
  148. uint8_t ClearHubFeature(uint8_t fid);
  149. uint8_t ClearPortFeature(uint8_t fid, uint8_t port, uint8_t sel = 0);
  150. uint8_t GetHubDescriptor(uint8_t index, uint16_t nbytes, uint8_t *dataptr);
  151. uint8_t GetHubStatus(uint16_t nbytes, uint8_t* dataptr);
  152. uint8_t GetPortStatus(uint8_t port, uint16_t nbytes, uint8_t* dataptr);
  153. uint8_t SetHubDescriptor(uint8_t port, uint16_t nbytes, uint8_t* dataptr);
  154. uint8_t SetHubFeature(uint8_t fid);
  155. uint8_t SetPortFeature(uint8_t fid, uint8_t port, uint8_t sel = 0);
  156. void PrintHubStatus();
  157. uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
  158. uint8_t Release();
  159. uint8_t Poll();
  160. void ResetHubPort(uint8_t port);
  161. virtual uint8_t GetAddress() {
  162. return bAddress;
  163. };
  164. virtual bool DEVCLASSOK(uint8_t klass) {
  165. return (klass == 0x09);
  166. }
  167. };
  168. // Clear Hub Feature
  169. inline uint8_t USBHub::ClearHubFeature(uint8_t fid) {
  170. return ( pUsb->ctrlReq(bAddress, 0, bmREQ_CLEAR_HUB_FEATURE, USB_REQUEST_CLEAR_FEATURE, fid, 0, 0, 0, 0, NULL, NULL));
  171. }
  172. // Clear Port Feature
  173. inline uint8_t USBHub::ClearPortFeature(uint8_t fid, uint8_t port, uint8_t sel) {
  174. return ( pUsb->ctrlReq(bAddress, 0, bmREQ_CLEAR_PORT_FEATURE, USB_REQUEST_CLEAR_FEATURE, fid, 0, ((0x0000 | port) | (sel << 8)), 0, 0, NULL, NULL));
  175. }
  176. // Get Hub Descriptor
  177. inline uint8_t USBHub::GetHubDescriptor(uint8_t index, uint16_t nbytes, uint8_t *dataptr) {
  178. return ( pUsb->ctrlReq(bAddress, 0, bmREQ_GET_HUB_DESCRIPTOR, USB_REQUEST_GET_DESCRIPTOR, index, 0x29, 0, nbytes, nbytes, dataptr, NULL));
  179. }
  180. // Get Hub Status
  181. inline uint8_t USBHub::GetHubStatus(uint16_t nbytes, uint8_t* dataptr) {
  182. return ( pUsb->ctrlReq(bAddress, 0, bmREQ_GET_HUB_STATUS, USB_REQUEST_GET_STATUS, 0, 0, 0x0000, nbytes, nbytes, dataptr, NULL));
  183. }
  184. // Get Port Status
  185. inline uint8_t USBHub::GetPortStatus(uint8_t port, uint16_t nbytes, uint8_t* dataptr) {
  186. return ( pUsb->ctrlReq(bAddress, 0, bmREQ_GET_PORT_STATUS, USB_REQUEST_GET_STATUS, 0, 0, port, nbytes, nbytes, dataptr, NULL));
  187. }
  188. // Set Hub Descriptor
  189. inline uint8_t USBHub::SetHubDescriptor(uint8_t port, uint16_t nbytes, uint8_t* dataptr) {
  190. return ( pUsb->ctrlReq(bAddress, 0, bmREQ_SET_HUB_DESCRIPTOR, USB_REQUEST_SET_DESCRIPTOR, 0, 0, port, nbytes, nbytes, dataptr, NULL));
  191. }
  192. // Set Hub Feature
  193. inline uint8_t USBHub::SetHubFeature(uint8_t fid) {
  194. return ( pUsb->ctrlReq(bAddress, 0, bmREQ_SET_HUB_FEATURE, USB_REQUEST_SET_FEATURE, fid, 0, 0, 0, 0, NULL, NULL));
  195. }
  196. // Set Port Feature
  197. inline uint8_t USBHub::SetPortFeature(uint8_t fid, uint8_t port, uint8_t sel) {
  198. return ( pUsb->ctrlReq(bAddress, 0, bmREQ_SET_PORT_FEATURE, USB_REQUEST_SET_FEATURE, fid, 0, (((0x0000 | sel) << 8) | port), 0, 0, NULL, NULL));
  199. }
  200. void PrintHubPortStatus(USB *usbptr, uint8_t addr, uint8_t port, bool print_changes = false);
  201. #endif // __USBHUB_H__