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