USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
usbhub.cpp
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 #include "usbhub.h"
18 
19 bool USBHub::bResetInitiated = false;
20 
22 pUsb(p),
23 bAddress(0),
24 bNbrPorts(0),
25 //bInitState(0),
26 qNextPollTime(0),
27 bPollEnable(false) {
28  epInfo[0].epAddr = 0;
29  epInfo[0].maxPktSize = 8;
30  epInfo[0].epAttribs = 0;
31  epInfo[0].bmNakPower = USB_NAK_MAX_POWER;
32 
33  epInfo[1].epAddr = 1;
34  epInfo[1].maxPktSize = 8; //kludge
35  epInfo[1].epAttribs = 0;
36  epInfo[1].bmNakPower = USB_NAK_NOWAIT;
37 
38  if (pUsb)
39  pUsb->RegisterDeviceClass(this);
40 }
41 
42 uint8_t USBHub::Init(uint8_t parent, uint8_t port, bool lowspeed) {
43  uint8_t buf[32];
44  uint8_t rcode;
45  UsbDevice *p = NULL;
46  EpInfo *oldep_ptr = NULL;
47  uint8_t len = 0;
48  uint16_t cd_len = 0;
49 
50  //USBTRACE("\r\nHub Init Start ");
51  //D_PrintHex<uint8_t > (bInitState, 0x80);
52 
53  AddressPool &addrPool = pUsb->GetAddressPool();
54 
55  //switch (bInitState) {
56  // case 0:
57  if (bAddress)
59 
60  // Get pointer to pseudo device with address 0 assigned
61  p = addrPool.GetUsbDevicePtr(0);
62 
63  if (!p)
65 
66  if (!p->epinfo)
68 
69  // Save old pointer to EP_RECORD of address 0
70  oldep_ptr = p->epinfo;
71 
72  // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
73  p->epinfo = epInfo;
74 
75  p->lowspeed = lowspeed;
76 
77  // Get device descriptor
78  rcode = pUsb->getDevDescr(0, 0, 8, (uint8_t*)buf);
79 
80  p->lowspeed = false;
81 
82  if (!rcode)
83  len = (buf[0] > 32) ? 32 : buf[0];
84 
85  if (rcode) {
86  // Restore p->epinfo
87  p->epinfo = oldep_ptr;
88  return rcode;
89  }
90 
91  // Extract device class from device descriptor
92  // If device class is not a hub return
93  if (((USB_DEVICE_DESCRIPTOR*)buf)->bDeviceClass != 0x09)
95 
96  // Allocate new address according to device class
97  bAddress = addrPool.AllocAddress(parent, (((USB_DEVICE_DESCRIPTOR*)buf)->bDeviceClass == 0x09) ? true : false, port);
98 
99  if (!bAddress)
101 
102  // Extract Max Packet Size from the device descriptor
103  epInfo[0].maxPktSize = ((USB_DEVICE_DESCRIPTOR*)buf)->bMaxPacketSize0;
104 
105  // Assign new address to the device
106  rcode = pUsb->setAddr(0, 0, bAddress);
107 
108  if (rcode) {
109  // Restore p->epinfo
110  p->epinfo = oldep_ptr;
111  addrPool.FreeAddress(bAddress);
112  bAddress = 0;
113  return rcode;
114  }
115 
116  //USBTRACE2("\r\nHub address: ", bAddress );
117 
118  // Restore p->epinfo
119  p->epinfo = oldep_ptr;
120 
121  if (len)
122  rcode = pUsb->getDevDescr(bAddress, 0, len, (uint8_t*)buf);
123 
124  if (rcode)
125  goto FailGetDevDescr;
126 
127  // Assign epInfo to epinfo pointer
128  rcode = pUsb->setEpInfoEntry(bAddress, 2, epInfo);
129 
130  if (rcode)
131  goto FailSetDevTblEntry;
132 
133  // bInitState = 1;
134 
135  // case 1:
136  // Get hub descriptor
137  rcode = GetHubDescriptor(0, 8, buf);
138 
139  if (rcode)
140  goto FailGetHubDescr;
141 
142  // Save number of ports for future use
143  bNbrPorts = ((HubDescriptor*)buf)->bNbrPorts;
144 
145  // bInitState = 2;
146 
147  // case 2:
148  // Read configuration Descriptor in Order To Obtain Proper Configuration Value
149  rcode = pUsb->getConfDescr(bAddress, 0, 8, 0, buf);
150 
151  if (!rcode) {
152  cd_len = ((USB_CONFIGURATION_DESCRIPTOR*)buf)->wTotalLength;
153  rcode = pUsb->getConfDescr(bAddress, 0, cd_len, 0, buf);
154  }
155  if (rcode)
156  goto FailGetConfDescr;
157 
158  // The following code is of no practical use in real life applications.
159  // It only intended for the usb protocol sniffer to properly parse hub-class requests.
160  {
161  uint8_t buf2[24];
162 
163  rcode = pUsb->getConfDescr(bAddress, 0, buf[0], 0, buf2);
164 
165  if (rcode)
166  goto FailGetConfDescr;
167  }
168 
169  // Set Configuration Value
170  rcode = pUsb->setConf(bAddress, 0, buf[5]);
171 
172  if (rcode)
173  goto FailSetConfDescr;
174 
175  // bInitState = 3;
176 
177  // case 3:
178  // Power on all ports
179  for (uint8_t j = 1; j <= bNbrPorts; j++)
180  SetPortFeature(HUB_FEATURE_PORT_POWER, j, 0); //HubPortPowerOn(j);
181 
182  pUsb->SetHubPreMask();
183  bPollEnable = true;
184  // bInitState = 0;
185  //}
186  //bInitState = 0;
187  //USBTRACE("...OK\r\n");
188  return 0;
189 
190  // Oleg, No debugging?? -- xxxajk
191 FailGetDevDescr:
192  goto Fail;
193 
194 FailSetDevTblEntry:
195  goto Fail;
196 
197 FailGetHubDescr:
198  goto Fail;
199 
200 FailGetConfDescr:
201  goto Fail;
202 
203 FailSetConfDescr:
204  goto Fail;
205 
206 Fail:
207  USBTRACE("...FAIL\r\n");
208  return rcode;
209 }
210 
211 uint8_t USBHub::Release() {
212  pUsb->GetAddressPool().FreeAddress(bAddress);
213 
214  if (bAddress == 0x41)
215  pUsb->SetHubPreMask();
216 
217  bAddress = 0;
218  bNbrPorts = 0;
219  qNextPollTime = 0;
220  bPollEnable = false;
221  return 0;
222 }
223 
224 uint8_t USBHub::Poll() {
225  uint8_t rcode = 0;
226 
227  if (!bPollEnable)
228  return 0;
229 
230  if (qNextPollTime <= millis()) {
231  rcode = CheckHubStatus();
232  qNextPollTime = millis() + 100;
233  }
234  return rcode;
235 }
236 
237 uint8_t USBHub::CheckHubStatus() {
238  uint8_t rcode;
239  uint8_t buf[8];
240  uint16_t read = 1;
241 
242  rcode = pUsb->inTransfer(bAddress, 1, &read, buf);
243 
244  if (rcode)
245  return rcode;
246 
247  //if (buf[0] & 0x01) // Hub Status Change
248  //{
249  // pUsb->PrintHubStatus(addr);
250  // rcode = GetHubStatus(1, 0, 1, 4, buf);
251  // if (rcode)
252  // {
253  // USB_HOST_SERIAL.print("GetHubStatus Error");
254  // USB_HOST_SERIAL.println(rcode, HEX);
255  // return rcode;
256  // }
257  //}
258  for (uint8_t port = 1, mask = 0x02; port < 8; mask <<= 1, port++) {
259  if (buf[0] & mask) {
260  HubEvent evt;
261  evt.bmEvent = 0;
262 
263  rcode = GetPortStatus(port, 4, evt.evtBuff);
264 
265  if (rcode)
266  continue;
267 
268  rcode = PortStatusChange(port, evt);
269 
270  if (rcode == HUB_ERROR_PORT_HAS_BEEN_RESET)
271  return 0;
272 
273  if (rcode)
274  return rcode;
275  }
276  } // for
277 
278  for (uint8_t port = 1; port <= bNbrPorts; port++) {
279  HubEvent evt;
280  evt.bmEvent = 0;
281 
282  rcode = GetPortStatus(port, 4, evt.evtBuff);
283 
284  if (rcode)
285  continue;
286 
288  continue;
289 
290  // Emulate connection event for the port
292 
293  rcode = PortStatusChange(port, evt);
294 
295  if (rcode == HUB_ERROR_PORT_HAS_BEEN_RESET)
296  return 0;
297 
298  if (rcode)
299  return rcode;
300  } // for
301  return 0;
302 }
303 
304 void USBHub::ResetHubPort(uint8_t port) {
305  HubEvent evt;
306  evt.bmEvent = 0;
307  uint8_t rcode;
308 
312 
313 
314  for (int i = 0; i < 3; i++) {
315  rcode = GetPortStatus(port, 4, evt.evtBuff);
316  if (rcode) break; // Some kind of error, bail.
318  break;
319  }
320  delay(100); // simulate polling.
321  }
324  delay(20);
325 }
326 
327 uint8_t USBHub::PortStatusChange(uint8_t port, HubEvent &evt) {
328  switch (evt.bmEvent) {
329  // Device connected event
332  if (bResetInitiated)
333  return 0;
334 
338  bResetInitiated = true;
340 
341  // Device disconnected event
345  bResetInitiated = false;
346 
348  a.devAddress = 0;
349  a.bmHub = 0;
350  a.bmParent = bAddress;
351  a.bmAddress = port;
352  pUsb->ReleaseDevice(a.devAddress);
353  return 0;
354 
355  // Reset complete event
360 
361  delay(20);
362 
363  a.devAddress = bAddress;
364 
366  bResetInitiated = false;
367  break;
368 
369  } // switch (evt.bmEvent)
370  return 0;
371 }
372 
373 void PrintHubPortStatus(USBHub *hubptr, uint8_t addr, uint8_t port, bool print_changes) {
374  uint8_t rcode = 0;
375  HubEvent evt;
376 
377  rcode = hubptr->GetPortStatus(port, 4, evt.evtBuff);
378 
379  if (rcode) {
380  USB_HOST_SERIAL.println("ERROR!");
381  return;
382  }
383  USB_HOST_SERIAL.print("\r\nPort ");
384  USB_HOST_SERIAL.println(port, DEC);
385 
386  USB_HOST_SERIAL.println("Status");
387  USB_HOST_SERIAL.print("CONNECTION:\t");
389  USB_HOST_SERIAL.print("ENABLE:\t\t");
390  USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_ENABLE) > 0, DEC);
391  USB_HOST_SERIAL.print("SUSPEND:\t");
392  USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_SUSPEND) > 0, DEC);
393  USB_HOST_SERIAL.print("OVER_CURRENT:\t");
395  USB_HOST_SERIAL.print("RESET:\t\t");
396  USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_RESET) > 0, DEC);
397  USB_HOST_SERIAL.print("POWER:\t\t");
398  USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_POWER) > 0, DEC);
399  USB_HOST_SERIAL.print("LOW_SPEED:\t");
401  USB_HOST_SERIAL.print("HIGH_SPEED:\t");
403  USB_HOST_SERIAL.print("TEST:\t\t");
404  USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_TEST) > 0, DEC);
405  USB_HOST_SERIAL.print("INDICATOR:\t");
407 
408  if (!print_changes)
409  return;
410 
411  USB_HOST_SERIAL.println("\r\nChange");
412  USB_HOST_SERIAL.print("CONNECTION:\t");
414  USB_HOST_SERIAL.print("ENABLE:\t\t");
415  USB_HOST_SERIAL.println((evt.bmChange & bmHUB_PORT_STATUS_C_PORT_ENABLE) > 0, DEC);
416  USB_HOST_SERIAL.print("SUSPEND:\t");
418  USB_HOST_SERIAL.print("OVER_CURRENT:\t");
420  USB_HOST_SERIAL.print("RESET:\t\t");
421  USB_HOST_SERIAL.println((evt.bmChange & bmHUB_PORT_STATUS_C_PORT_RESET) > 0, DEC);
422 }
uint8_t getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t *dataptr)
Definition: Usb.cpp:761
#define bmHUB_PORT_STATE_CHECK_DISABLED
Definition: usbhub.h:119
#define bmHUB_PORT_EVENT_LS_RESET_COMPLETE
Definition: usbhub.h:130
#define bmHUB_PORT_STATUS_PORT_CONNECTION
Definition: usbhub.h:76
#define bmHUB_PORT_STATUS_C_PORT_SUSPEND
Definition: usbhub.h:90
EpInfo * epinfo
Definition: address.h:76
bool lowspeed
Definition: address.h:79
#define USB_ERROR_EPINFO_IS_NULL
Definition: UsbCore.h:67
uint8_t bmNakPower
Definition: address.h:42
uint16_t bmChange
Definition: usbhub.h:157
#define HUB_ERROR_PORT_HAS_BEEN_RESET
Definition: usbhub.h:113
#define HUB_FEATURE_PORT_POWER
Definition: usbhub.h:52
#define bmHUB_PORT_STATUS_PORT_LOW_SPEED
Definition: usbhub.h:82
#define bmHUB_PORT_STATUS_PORT_SUSPEND
Definition: usbhub.h:78
#define HUB_FEATURE_C_PORT_RESET
Definition: usbhub.h:58
#define bmHUB_PORT_STATUS_PORT_OVER_CURRENT
Definition: usbhub.h:79
uint8_t setConf(uint8_t addr, uint8_t ep, uint8_t conf_value)
Definition: Usb.cpp:795
virtual void ResetHubPort(uint8_t port)
Definition: usbhub.cpp:304
uint8_t setEpInfoEntry(uint8_t addr, uint8_t epcount, EpInfo *eprecord_ptr)
Definition: Usb.cpp:64
virtual void FreeAddress(uint8_t addr)=0
#define bmHUB_PORT_STATUS_C_PORT_ENABLE
Definition: usbhub.h:89
uint8_t epAttribs
Definition: address.h:37
virtual UsbDevice * GetUsbDevicePtr(uint8_t addr)=0
uint8_t setAddr(uint8_t oldaddr, uint8_t ep, uint8_t newaddr)
Definition: Usb.cpp:790
#define USB_HOST_SERIAL
Definition: settings.h:24
uint8_t epAddr
Definition: address.h:33
#define USB_NAK_MAX_POWER
Definition: address.h:27
#define bmHUB_PORT_STATE_DISABLED
Definition: usbhub.h:122
void SetHubPreMask()
Definition: UsbCore.h:160
uint8_t GetHubDescriptor(uint8_t index, uint16_t nbytes, uint8_t *dataptr)
Definition: usbhub.h:217
#define bmHUB_PORT_STATUS_PORT_POWER
Definition: usbhub.h:81
Definition: address.h:32
#define bmHUB_PORT_STATUS_PORT_HIGH_SPEED
Definition: usbhub.h:83
uint8_t ClearPortFeature(uint8_t fid, uint8_t port, uint8_t sel=0)
Definition: usbhub.h:212
#define bmHUB_PORT_STATUS_C_PORT_OVER_CURRENT
Definition: usbhub.h:91
uint32_t bmEvent
Definition: usbhub.h:159
virtual uint8_t AllocAddress(uint8_t parent, bool is_hub=false, uint8_t port=0)=0
#define USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE
Definition: UsbCore.h:69
#define bmHUB_PORT_STATUS_C_PORT_RESET
Definition: usbhub.h:92
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
#define HUB_FEATURE_C_PORT_CONNECTION
Definition: usbhub.h:54
void PrintHubPortStatus(USBHub *hubptr, uint8_t addr, uint8_t port, bool print_changes)
Definition: usbhub.cpp:373
uint8_t devAddress
Definition: address.h:67
uint8_t bmAddress
Definition: address.h:62
#define USB_NAK_NOWAIT
Definition: address.h:29
#define bmHUB_PORT_STATUS_PORT_RESET
Definition: usbhub.h:80
#define USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL
Definition: UsbCore.h:66
uint8_t inTransfer(uint8_t addr, uint8_t ep, uint16_t *nbytesptr, uint8_t *data)
Definition: Usb.cpp:206
#define USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED
Definition: UsbCore.h:61
uint8_t bmParent
Definition: address.h:63
uint8_t GetPortStatus(uint8_t port, uint16_t nbytes, uint8_t *dataptr)
Definition: usbhub.h:227
#define bmHUB_PORT_EVENT_CONNECT
Definition: usbhub.h:125
virtual uint8_t Release()
Definition: usbhub.cpp:211
#define HUB_FEATURE_PORT_RESET
Definition: usbhub.h:51
#define USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL
Definition: UsbCore.h:64
#define bmHUB_PORT_STATUS_PORT_ENABLE
Definition: usbhub.h:77
uint8_t maxPktSize
Definition: address.h:34
AddressPool & GetAddressPool()
Definition: UsbCore.h:168
uint8_t Configuring(uint8_t parent, uint8_t port, bool lowspeed)
Definition: Usb.cpp:647
Definition: UsbCore.h:152
uint8_t evtBuff[4]
Definition: usbhub.h:160
#define bmHUB_PORT_STATUS_PORT_INDICATOR
Definition: usbhub.h:85
uint8_t RegisterDeviceClass(USBDeviceConfig *pdev)
Definition: UsbCore.h:172
#define bmHUB_PORT_STATUS_PORT_TEST
Definition: usbhub.h:84
#define bmHUB_PORT_EVENT_LS_CONNECT
Definition: usbhub.h:129
#define bmHUB_PORT_STATUS_C_PORT_CONNECTION
Definition: usbhub.h:88
#define bmHUB_PORT_EVENT_RESET_COMPLETE
Definition: usbhub.h:127
uint8_t SetPortFeature(uint8_t fid, uint8_t port, uint8_t sel=0)
Definition: usbhub.h:242
#define HUB_FEATURE_C_PORT_ENABLE
Definition: usbhub.h:55
#define USBTRACE(s)
Definition: macros.h:60
uint8_t getDevDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *dataptr)
defined(USB_METHODS_INLINE)
Definition: Usb.cpp:756
uint8_t bmHub
Definition: address.h:64
virtual uint8_t Poll()
Definition: usbhub.cpp:224
USBHub(USB *p)
Definition: usbhub.cpp:21
uint8_t ReleaseDevice(uint8_t addr)
Definition: Usb.cpp:741
#define bmHUB_PORT_EVENT_DISCONNECT
Definition: usbhub.h:126