17 #if !defined(__USBHUB_H__)
21 #include <avr/pgmspace.h>
28 #if defined(ARDUINO) && ARDUINO >=100
34 #define USB_DESCRIPTOR_HUB 0x09 // Hub descriptor type
37 #define bmREQ_CLEAR_HUB_FEATURE USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE
38 #define bmREQ_CLEAR_PORT_FEATURE USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
39 #define bmREQ_CLEAR_TT_BUFFER USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
40 #define bmREQ_GET_HUB_DESCRIPTOR USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE
41 #define bmREQ_GET_HUB_STATUS USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE
42 #define bmREQ_GET_PORT_STATUS USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
43 #define bmREQ_RESET_TT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
44 #define bmREQ_SET_HUB_DESCRIPTOR USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE
45 #define bmREQ_SET_HUB_FEATURE USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE
46 #define bmREQ_SET_PORT_FEATURE USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
47 #define bmREQ_GET_TT_STATE USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
48 #define bmREQ_STOP_TT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
51 #define HUB_REQUEST_CLEAR_TT_BUFFER 8
52 #define HUB_REQUEST_RESET_TT 9
53 #define HUB_REQUEST_GET_TT_STATE 10
54 #define HUB_REQUEST_STOP_TT 11
57 #define HUB_FEATURE_C_HUB_LOCAL_POWER 0
58 #define HUB_FEATURE_C_HUB_OVER_CURRENT 1
59 #define HUB_FEATURE_PORT_CONNECTION 0
60 #define HUB_FEATURE_PORT_ENABLE 1
61 #define HUB_FEATURE_PORT_SUSPEND 2
62 #define HUB_FEATURE_PORT_OVER_CURRENT 3
63 #define HUB_FEATURE_PORT_RESET 4
64 #define HUB_FEATURE_PORT_POWER 8
65 #define HUB_FEATURE_PORT_LOW_SPEED 9
66 #define HUB_FEATURE_C_PORT_CONNECTION 16
67 #define HUB_FEATURE_C_PORT_ENABLE 17
68 #define HUB_FEATURE_C_PORT_SUSPEND 18
69 #define HUB_FEATURE_C_PORT_OVER_CURRENT 19
70 #define HUB_FEATURE_C_PORT_RESET 20
71 #define HUB_FEATURE_PORT_TEST 21
72 #define HUB_FEATURE_PORT_INDICATOR 22
75 #define HUB_PORT_TEST_MODE_J 1
76 #define HUB_PORT_TEST_MODE_K 2
77 #define HUB_PORT_TEST_MODE_SE0_NAK 3
78 #define HUB_PORT_TEST_MODE_PACKET 4
79 #define HUB_PORT_TEST_MODE_FORCE_ENABLE 5
82 #define HUB_PORT_INDICATOR_AUTO 0
83 #define HUB_PORT_INDICATOR_AMBER 1
84 #define HUB_PORT_INDICATOR_GREEN 2
85 #define HUB_PORT_INDICATOR_OFF 3
88 #define bmHUB_PORT_STATUS_PORT_CONNECTION 0x0001
89 #define bmHUB_PORT_STATUS_PORT_ENABLE 0x0002
90 #define bmHUB_PORT_STATUS_PORT_SUSPEND 0x0004
91 #define bmHUB_PORT_STATUS_PORT_OVER_CURRENT 0x0008
92 #define bmHUB_PORT_STATUS_PORT_RESET 0x0010
93 #define bmHUB_PORT_STATUS_PORT_POWER 0x0100
94 #define bmHUB_PORT_STATUS_PORT_LOW_SPEED 0x0200
95 #define bmHUB_PORT_STATUS_PORT_HIGH_SPEED 0x0400
96 #define bmHUB_PORT_STATUS_PORT_TEST 0x0800
97 #define bmHUB_PORT_STATUS_PORT_INDICATOR 0x1000
100 #define bmHUB_PORT_STATUS_C_PORT_CONNECTION 0x0001
101 #define bmHUB_PORT_STATUS_C_PORT_ENABLE 0x0002
102 #define bmHUB_PORT_STATUS_C_PORT_SUSPEND 0x0004
103 #define bmHUB_PORT_STATUS_C_PORT_OVER_CURRENT 0x0008
104 #define bmHUB_PORT_STATUS_C_PORT_RESET 0x0010
107 #define bmHUB_STATUS_LOCAL_POWER_SOURCE 0x01
108 #define bmHUB_STATUS_OVER_CURRENT 0x12
111 #define bmHUB_STATUS_C_LOCAL_POWER_SOURCE 0x01
112 #define bmHUB_STATUS_C_OVER_CURRENT 0x12
116 #define USB_STATE_HUB_PORT_CONFIGURING 0xb0
117 #define USB_STATE_HUB_PORT_POWERED_OFF 0xb1
118 #define USB_STATE_HUB_PORT_WAIT_FOR_POWER_GOOD 0xb2
119 #define USB_STATE_HUB_PORT_DISCONNECTED 0xb3
120 #define USB_STATE_HUB_PORT_DISABLED 0xb4
121 #define USB_STATE_HUB_PORT_RESETTING 0xb5
122 #define USB_STATE_HUB_PORT_ENABLED 0xb6
125 #define HUB_ERROR_PORT_HAS_BEEN_RESET 0xb1
128 #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)
131 #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)
134 #define bmHUB_PORT_STATE_DISABLED (0x0000 | bmHUB_PORT_STATUS_PORT_POWER | bmHUB_PORT_STATUS_PORT_CONNECTION)
137 #define bmHUB_PORT_EVENT_CONNECT (((0UL | bmHUB_PORT_STATUS_C_PORT_CONNECTION) << 16) | bmHUB_PORT_STATUS_PORT_POWER | bmHUB_PORT_STATUS_PORT_CONNECTION)
138 #define bmHUB_PORT_EVENT_DISCONNECT (((0UL | bmHUB_PORT_STATUS_C_PORT_CONNECTION) << 16) | bmHUB_PORT_STATUS_PORT_POWER)
139 #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)
141 #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)
142 #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)
143 #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)
181 static bool bResetInitiated;
190 uint32_t qNextPollTime;
193 uint8_t CheckHubStatus();
194 uint8_t PortStatusChange(uint8_t port,
HubEvent &evt);
201 uint8_t
GetHubDescriptor( uint8_t index, uint16_t nbytes, uint8_t *dataptr );
202 uint8_t
GetHubStatus( uint16_t nbytes, uint8_t* dataptr );
203 uint8_t
GetPortStatus( uint8_t port, uint16_t nbytes, uint8_t* dataptr );
204 uint8_t
SetHubDescriptor( uint8_t port, uint16_t nbytes, uint8_t* dataptr );
206 uint8_t
SetPortFeature( uint8_t fid, uint8_t port, uint8_t sel = 0 );
210 virtual uint8_t
Init(uint8_t parent, uint8_t port,
bool lowspeed);
212 virtual uint8_t
Poll();
219 return( pUsb->
ctrlReq( bAddress, 0,
bmREQ_CLEAR_HUB_FEATURE,
USB_REQUEST_CLEAR_FEATURE, fid, 0, 0, 0, 0, NULL, NULL ));
224 return( pUsb->
ctrlReq( bAddress, 0,
bmREQ_CLEAR_PORT_FEATURE,
USB_REQUEST_CLEAR_FEATURE, fid, 0, ((0x0000|port)|(sel<<8)), 0, 0, NULL, NULL ));
229 return( pUsb->
ctrlReq( bAddress, 0,
bmREQ_GET_HUB_DESCRIPTOR,
USB_REQUEST_GET_DESCRIPTOR, index, 0x29, 0, nbytes, nbytes, dataptr, NULL ));
234 return( pUsb->
ctrlReq( bAddress, 0,
bmREQ_GET_HUB_STATUS,
USB_REQUEST_GET_STATUS, 0, 0, 0x0000, nbytes, nbytes, dataptr, NULL ));
239 return( pUsb->
ctrlReq( bAddress, 0,
bmREQ_GET_PORT_STATUS,
USB_REQUEST_GET_STATUS, 0, 0, port, nbytes, nbytes, dataptr, NULL ));
244 return( pUsb->
ctrlReq( bAddress, 0,
bmREQ_SET_HUB_DESCRIPTOR,
USB_REQUEST_SET_DESCRIPTOR, 0, 0, port, nbytes, nbytes, dataptr, NULL ));
249 return( pUsb->
ctrlReq( bAddress, 0,
bmREQ_SET_HUB_FEATURE,
USB_REQUEST_SET_FEATURE, fid, 0, 0, 0, 0, NULL, NULL ));
254 return( pUsb->
ctrlReq( bAddress, 0,
bmREQ_SET_PORT_FEATURE,
USB_REQUEST_SET_FEATURE, fid, 0, (((0x0000|sel)<<8)|port), 0, 0, NULL, NULL ));
259 #endif // __USBHUB_H__