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 <inttypes.h>
21 #include <avr/pgmspace.h>
22 #include "avrpins.h"
23 #include "max3421e.h"
24 #include "usbhost.h"
25 #include "usb_ch9.h"
26 #include "Usb.h"
27 
28 #if defined(ARDUINO) && ARDUINO >=100
29 #include "Arduino.h"
30 #else
31 #include <WProgram.h>
32 #endif
33 
34 #define USB_DESCRIPTOR_HUB 0x09 // Hub descriptor type
35 
36 // Hub Requests
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
49 
50 // Hub Class Requests
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
55 
56 // Hub Features
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
73 
74 // Hub Port Test Modes
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
80 
81 // Hub Port Indicator Color
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
86 
87 // Hub Port Status Bitmasks
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
98 
99 // Hub Port Status Change Bitmasks (used one byte instead of two)
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
105 
106 // Hub Status Bitmasks (used one byte instead of two)
107 #define bmHUB_STATUS_LOCAL_POWER_SOURCE 0x01
108 #define bmHUB_STATUS_OVER_CURRENT 0x12
109 
110 // Hub Status Change Bitmasks (used one byte instead of two)
111 #define bmHUB_STATUS_C_LOCAL_POWER_SOURCE 0x01
112 #define bmHUB_STATUS_C_OVER_CURRENT 0x12
113 
114 
115 // Hub Port Configuring Substates
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
123 
124 // Additional Error Codes
125 #define HUB_ERROR_PORT_HAS_BEEN_RESET 0xb1
126 
127 // The bit mask to check for all necessary state bits
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)
129 
130 // Bit mask to check for DISABLED state in HubEvent::bmStatus field
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)
132 
133 // Hub Port States
134 #define bmHUB_PORT_STATE_DISABLED (0x0000 | bmHUB_PORT_STATUS_PORT_POWER | bmHUB_PORT_STATUS_PORT_CONNECTION)
135 
136 // Hub Port Events
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)
140 
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)
144 
146 {
147  uint8_t bDescLength; // descriptor length
148  uint8_t bDescriptorType; // descriptor type
149  uint8_t bNbrPorts; // number of ports a hub equiped with
150 
151  struct
152  {
153  uint16_t LogPwrSwitchMode : 2;
154  uint16_t CompoundDevice : 1;
156  uint16_t TTThinkTime : 2;
158  uint16_t Reserved : 8;
159  };
160 
161  uint8_t bPwrOn2PwrGood;
163 };
164 
165 struct HubEvent
166 {
167  union
168  {
169  struct
170  {
171  uint16_t bmStatus; // port status bits
172  uint16_t bmChange; // port status change bits
173  };
174  uint32_t bmEvent;
175  uint8_t evtBuff[4];
176  };
177 };
178 
180 {
181  static bool bResetInitiated; // True when reset is triggered
182 
183  USB *pUsb; // USB class instance pointer
184 
185  EpInfo epInfo[2]; // interrupt endpoint info structure
186 
187  uint8_t bAddress; // address
188  uint8_t bNbrPorts; // number of ports
189  uint8_t bInitState; // initialization state variable
190  uint32_t qNextPollTime; // next poll time
191  bool bPollEnable; // poll enable flag
192 
193  uint8_t CheckHubStatus();
194  uint8_t PortStatusChange(uint8_t port, HubEvent &evt);
195 
196 public:
197  USBHub(USB *p);
198 
199  uint8_t ClearHubFeature( uint8_t fid );
200  uint8_t ClearPortFeature( uint8_t fid, uint8_t port, uint8_t sel = 0 );
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 );
205  uint8_t SetHubFeature( uint8_t fid );
206  uint8_t SetPortFeature( uint8_t fid, uint8_t port, uint8_t sel = 0 );
207 
208  void PrintHubStatus();
209 
210  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
211  virtual uint8_t Release();
212  virtual uint8_t Poll();
213  virtual uint8_t GetAddress() { return bAddress; };
214 };
215 
216 // Clear Hub Feature
217 inline uint8_t USBHub::ClearHubFeature( uint8_t fid )
218 {
219  return( pUsb->ctrlReq( bAddress, 0, bmREQ_CLEAR_HUB_FEATURE, USB_REQUEST_CLEAR_FEATURE, fid, 0, 0, 0, 0, NULL, NULL ));
220 }
221 // Clear Port Feature
222 inline uint8_t USBHub::ClearPortFeature( uint8_t fid, uint8_t port, uint8_t sel )
223 {
224  return( pUsb->ctrlReq( bAddress, 0, bmREQ_CLEAR_PORT_FEATURE, USB_REQUEST_CLEAR_FEATURE, fid, 0, ((0x0000|port)|(sel<<8)), 0, 0, NULL, NULL ));
225 }
226 // Get Hub Descriptor
227 inline uint8_t USBHub::GetHubDescriptor( uint8_t index, uint16_t nbytes, uint8_t *dataptr )
228 {
229  return( pUsb->ctrlReq( bAddress, 0, bmREQ_GET_HUB_DESCRIPTOR, USB_REQUEST_GET_DESCRIPTOR, index, 0x29, 0, nbytes, nbytes, dataptr, NULL ));
230 }
231 // Get Hub Status
232 inline uint8_t USBHub::GetHubStatus( uint16_t nbytes, uint8_t* dataptr )
233 {
234  return( pUsb->ctrlReq( bAddress, 0, bmREQ_GET_HUB_STATUS, USB_REQUEST_GET_STATUS, 0, 0, 0x0000, nbytes, nbytes, dataptr, NULL ));
235 }
236 // Get Port Status
237 inline uint8_t USBHub::GetPortStatus( uint8_t port, uint16_t nbytes, uint8_t* dataptr )
238 {
239  return( pUsb->ctrlReq( bAddress, 0, bmREQ_GET_PORT_STATUS, USB_REQUEST_GET_STATUS, 0, 0, port, nbytes, nbytes, dataptr, NULL ));
240 }
241 // Set Hub Descriptor
242 inline uint8_t USBHub::SetHubDescriptor( uint8_t port, uint16_t nbytes, uint8_t* dataptr )
243 {
244  return( pUsb->ctrlReq( bAddress, 0, bmREQ_SET_HUB_DESCRIPTOR, USB_REQUEST_SET_DESCRIPTOR, 0, 0, port, nbytes, nbytes, dataptr, NULL ));
245 }
246 // Set Hub Feature
247 inline uint8_t USBHub::SetHubFeature( uint8_t fid )
248 {
249  return( pUsb->ctrlReq( bAddress, 0, bmREQ_SET_HUB_FEATURE, USB_REQUEST_SET_FEATURE, fid, 0, 0, 0, 0, NULL, NULL ));
250 }
251 // Set Port Feature
252 inline uint8_t USBHub::SetPortFeature( uint8_t fid, uint8_t port, uint8_t sel )
253 {
254  return( pUsb->ctrlReq( bAddress, 0, bmREQ_SET_PORT_FEATURE, USB_REQUEST_SET_FEATURE, fid, 0, (((0x0000|sel)<<8)|port), 0, 0, NULL, NULL ));
255 }
256 
257 void PrintHubPortStatus(USB *usbptr, uint8_t addr, uint8_t port, bool print_changes = false);
258 
259 #endif // __USBHUB_H__