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 
29 #if defined(ARDUINO) && ARDUINO >=100
30 #include "Arduino.h"
31 #else
32 #include <WProgram.h>
33 #endif
34 
35 #define USB_DESCRIPTOR_HUB 0x09 // Hub descriptor type
36 
37 // Hub Requests
38 #define bmREQ_CLEAR_HUB_FEATURE USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE
39 #define bmREQ_CLEAR_PORT_FEATURE USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
40 #define bmREQ_CLEAR_TT_BUFFER USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
41 #define bmREQ_GET_HUB_DESCRIPTOR USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE
42 #define bmREQ_GET_HUB_STATUS USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE
43 #define bmREQ_GET_PORT_STATUS USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
44 #define bmREQ_RESET_TT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
45 #define bmREQ_SET_HUB_DESCRIPTOR USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE
46 #define bmREQ_SET_HUB_FEATURE USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE
47 #define bmREQ_SET_PORT_FEATURE USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
48 #define bmREQ_GET_TT_STATE USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
49 #define bmREQ_STOP_TT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_OTHER
50 
51 // Hub Class Requests
52 #define HUB_REQUEST_CLEAR_TT_BUFFER 8
53 #define HUB_REQUEST_RESET_TT 9
54 #define HUB_REQUEST_GET_TT_STATE 10
55 #define HUB_REQUEST_STOP_TT 11
56 
57 // Hub Features
58 #define HUB_FEATURE_C_HUB_LOCAL_POWER 0
59 #define HUB_FEATURE_C_HUB_OVER_CURRENT 1
60 #define HUB_FEATURE_PORT_CONNECTION 0
61 #define HUB_FEATURE_PORT_ENABLE 1
62 #define HUB_FEATURE_PORT_SUSPEND 2
63 #define HUB_FEATURE_PORT_OVER_CURRENT 3
64 #define HUB_FEATURE_PORT_RESET 4
65 #define HUB_FEATURE_PORT_POWER 8
66 #define HUB_FEATURE_PORT_LOW_SPEED 9
67 #define HUB_FEATURE_C_PORT_CONNECTION 16
68 #define HUB_FEATURE_C_PORT_ENABLE 17
69 #define HUB_FEATURE_C_PORT_SUSPEND 18
70 #define HUB_FEATURE_C_PORT_OVER_CURRENT 19
71 #define HUB_FEATURE_C_PORT_RESET 20
72 #define HUB_FEATURE_PORT_TEST 21
73 #define HUB_FEATURE_PORT_INDICATOR 22
74 
75 // Hub Port Test Modes
76 #define HUB_PORT_TEST_MODE_J 1
77 #define HUB_PORT_TEST_MODE_K 2
78 #define HUB_PORT_TEST_MODE_SE0_NAK 3
79 #define HUB_PORT_TEST_MODE_PACKET 4
80 #define HUB_PORT_TEST_MODE_FORCE_ENABLE 5
81 
82 // Hub Port Indicator Color
83 #define HUB_PORT_INDICATOR_AUTO 0
84 #define HUB_PORT_INDICATOR_AMBER 1
85 #define HUB_PORT_INDICATOR_GREEN 2
86 #define HUB_PORT_INDICATOR_OFF 3
87 
88 // Hub Port Status Bitmasks
89 #define bmHUB_PORT_STATUS_PORT_CONNECTION 0x0001
90 #define bmHUB_PORT_STATUS_PORT_ENABLE 0x0002
91 #define bmHUB_PORT_STATUS_PORT_SUSPEND 0x0004
92 #define bmHUB_PORT_STATUS_PORT_OVER_CURRENT 0x0008
93 #define bmHUB_PORT_STATUS_PORT_RESET 0x0010
94 #define bmHUB_PORT_STATUS_PORT_POWER 0x0100
95 #define bmHUB_PORT_STATUS_PORT_LOW_SPEED 0x0200
96 #define bmHUB_PORT_STATUS_PORT_HIGH_SPEED 0x0400
97 #define bmHUB_PORT_STATUS_PORT_TEST 0x0800
98 #define bmHUB_PORT_STATUS_PORT_INDICATOR 0x1000
99 
100 // Hub Port Status Change Bitmasks (used one byte instead of two)
101 #define bmHUB_PORT_STATUS_C_PORT_CONNECTION 0x0001
102 #define bmHUB_PORT_STATUS_C_PORT_ENABLE 0x0002
103 #define bmHUB_PORT_STATUS_C_PORT_SUSPEND 0x0004
104 #define bmHUB_PORT_STATUS_C_PORT_OVER_CURRENT 0x0008
105 #define bmHUB_PORT_STATUS_C_PORT_RESET 0x0010
106 
107 // Hub Status Bitmasks (used one byte instead of two)
108 #define bmHUB_STATUS_LOCAL_POWER_SOURCE 0x01
109 #define bmHUB_STATUS_OVER_CURRENT 0x12
110 
111 // Hub Status Change Bitmasks (used one byte instead of two)
112 #define bmHUB_STATUS_C_LOCAL_POWER_SOURCE 0x01
113 #define bmHUB_STATUS_C_OVER_CURRENT 0x12
114 
115 
116 // Hub Port Configuring Substates
117 #define USB_STATE_HUB_PORT_CONFIGURING 0xb0
118 #define USB_STATE_HUB_PORT_POWERED_OFF 0xb1
119 #define USB_STATE_HUB_PORT_WAIT_FOR_POWER_GOOD 0xb2
120 #define USB_STATE_HUB_PORT_DISCONNECTED 0xb3
121 #define USB_STATE_HUB_PORT_DISABLED 0xb4
122 #define USB_STATE_HUB_PORT_RESETTING 0xb5
123 #define USB_STATE_HUB_PORT_ENABLED 0xb6
124 
125 // Additional Error Codes
126 #define HUB_ERROR_PORT_HAS_BEEN_RESET 0xb1
127 
128 // The bit mask to check for all necessary state bits
129 #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)
130 
131 // Bit mask to check for DISABLED state in HubEvent::bmStatus field
132 #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)
133 
134 // Hub Port States
135 #define bmHUB_PORT_STATE_DISABLED (0x0000 | bmHUB_PORT_STATUS_PORT_POWER | bmHUB_PORT_STATUS_PORT_CONNECTION)
136 
137 // Hub Port Events
138 #define bmHUB_PORT_EVENT_CONNECT (((0UL | bmHUB_PORT_STATUS_C_PORT_CONNECTION) << 16) | bmHUB_PORT_STATUS_PORT_POWER | bmHUB_PORT_STATUS_PORT_CONNECTION)
139 #define bmHUB_PORT_EVENT_DISCONNECT (((0UL | bmHUB_PORT_STATUS_C_PORT_CONNECTION) << 16) | bmHUB_PORT_STATUS_PORT_POWER)
140 #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 
142 #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)
143 #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)
144 #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)
145 
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  uint16_t LogPwrSwitchMode : 2;
153  uint16_t CompoundDevice : 1;
155  uint16_t TTThinkTime : 2;
157  uint16_t Reserved : 8;
158  } __attribute__((packed));
159 
160  uint8_t bPwrOn2PwrGood;
162 } __attribute__((packed));
163 
164 struct HubEvent {
165 
166  union {
167 
168  struct {
169  uint16_t bmStatus; // port status bits
170  uint16_t bmChange; // port status change bits
171  } __attribute__((packed));
172  uint32_t bmEvent;
173  uint8_t evtBuff[4];
174  };
175 } __attribute__((packed));
176 
178  static bool bResetInitiated; // True when reset is triggered
179 
180  USB *pUsb; // USB class instance pointer
181 
182  EpInfo epInfo[2]; // interrupt endpoint info structure
183 
184  uint8_t bAddress; // address
185  uint8_t bNbrPorts; // number of ports
186 // uint8_t bInitState; // initialization state variable
187  uint32_t qNextPollTime; // next poll time
188  bool bPollEnable; // poll enable flag
189 
190  uint8_t CheckHubStatus();
191  uint8_t PortStatusChange(uint8_t port, HubEvent &evt);
192 
193 public:
194  USBHub(USB *p);
195 
196  uint8_t ClearHubFeature(uint8_t fid);
197  uint8_t ClearPortFeature(uint8_t fid, uint8_t port, uint8_t sel = 0);
198  uint8_t GetHubDescriptor(uint8_t index, uint16_t nbytes, uint8_t *dataptr);
199  uint8_t GetHubStatus(uint16_t nbytes, uint8_t* dataptr);
200  uint8_t GetPortStatus(uint8_t port, uint16_t nbytes, uint8_t* dataptr);
201  uint8_t SetHubDescriptor(uint8_t port, uint16_t nbytes, uint8_t* dataptr);
202  uint8_t SetHubFeature(uint8_t fid);
203  uint8_t SetPortFeature(uint8_t fid, uint8_t port, uint8_t sel = 0);
204 
205  void PrintHubStatus();
206 
207  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
208  virtual uint8_t Release();
209  virtual uint8_t Poll();
210  virtual void ResetHubPort(uint8_t port);
211  virtual uint8_t GetAddress() {
212  return bAddress;
213  };
214  virtual boolean DEVCLASSOK(uint8_t klass) { return (klass == 0x09); }
215 
216 };
217 
218 // Clear Hub Feature
219 
220 inline uint8_t USBHub::ClearHubFeature(uint8_t fid) {
221  return( pUsb->ctrlReq(bAddress, 0, bmREQ_CLEAR_HUB_FEATURE, USB_REQUEST_CLEAR_FEATURE, fid, 0, 0, 0, 0, NULL, NULL));
222 }
223 // Clear Port Feature
224 
225 inline uint8_t USBHub::ClearPortFeature(uint8_t fid, uint8_t port, uint8_t sel) {
226  return( pUsb->ctrlReq(bAddress, 0, bmREQ_CLEAR_PORT_FEATURE, USB_REQUEST_CLEAR_FEATURE, fid, 0, ((0x0000 | port) | (sel << 8)), 0, 0, NULL, NULL));
227 }
228 // Get Hub Descriptor
229 
230 inline uint8_t USBHub::GetHubDescriptor(uint8_t index, uint16_t nbytes, uint8_t *dataptr) {
231  return( pUsb->ctrlReq(bAddress, 0, bmREQ_GET_HUB_DESCRIPTOR, USB_REQUEST_GET_DESCRIPTOR, index, 0x29, 0, nbytes, nbytes, dataptr, NULL));
232 }
233 // Get Hub Status
234 
235 inline uint8_t USBHub::GetHubStatus(uint16_t nbytes, uint8_t* dataptr) {
236  return( pUsb->ctrlReq(bAddress, 0, bmREQ_GET_HUB_STATUS, USB_REQUEST_GET_STATUS, 0, 0, 0x0000, nbytes, nbytes, dataptr, NULL));
237 }
238 // Get Port Status
239 
240 inline uint8_t USBHub::GetPortStatus(uint8_t port, uint16_t nbytes, uint8_t* dataptr) {
241  return( pUsb->ctrlReq(bAddress, 0, bmREQ_GET_PORT_STATUS, USB_REQUEST_GET_STATUS, 0, 0, port, nbytes, nbytes, dataptr, NULL));
242 }
243 // Set Hub Descriptor
244 
245 inline uint8_t USBHub::SetHubDescriptor(uint8_t port, uint16_t nbytes, uint8_t* dataptr) {
246  return( pUsb->ctrlReq(bAddress, 0, bmREQ_SET_HUB_DESCRIPTOR, USB_REQUEST_SET_DESCRIPTOR, 0, 0, port, nbytes, nbytes, dataptr, NULL));
247 }
248 // Set Hub Feature
249 
250 inline uint8_t USBHub::SetHubFeature(uint8_t fid) {
251  return( pUsb->ctrlReq(bAddress, 0, bmREQ_SET_HUB_FEATURE, USB_REQUEST_SET_FEATURE, fid, 0, 0, 0, 0, NULL, NULL));
252 }
253 // Set Port Feature
254 
255 inline uint8_t USBHub::SetPortFeature(uint8_t fid, uint8_t port, uint8_t sel) {
256  return( pUsb->ctrlReq(bAddress, 0, bmREQ_SET_PORT_FEATURE, USB_REQUEST_SET_FEATURE, fid, 0, (((0x0000 | sel) << 8) | port), 0, 0, NULL, NULL));
257 }
258 
259 void PrintHubPortStatus(USB *usbptr, uint8_t addr, uint8_t port, bool print_changes = false);
260 
261 #endif // __USBHUB_H__