USB Host Shield 2.0
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  USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
45  HubDescriptor* hd = reinterpret_cast<HubDescriptor*>(buf);
46  USB_CONFIGURATION_DESCRIPTOR * ucd = reinterpret_cast<USB_CONFIGURATION_DESCRIPTOR*>(buf);
47  uint8_t rcode;
48  UsbDevice *p = NULL;
49  EpInfo *oldep_ptr = NULL;
50  uint8_t len = 0;
51  uint16_t cd_len = 0;
52 
53  //USBTRACE("\r\nHub Init Start ");
54  //D_PrintHex<uint8_t > (bInitState, 0x80);
55 
56  AddressPool &addrPool = pUsb->GetAddressPool();
57 
58  //switch (bInitState) {
59  // case 0:
60  if(bAddress)
62 
63  // Get pointer to pseudo device with address 0 assigned
64  p = addrPool.GetUsbDevicePtr(0);
65 
66  if(!p)
68 
69  if(!p->epinfo)
71 
72  // Save old pointer to EP_RECORD of address 0
73  oldep_ptr = p->epinfo;
74 
75  // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
76  p->epinfo = epInfo;
77 
78  p->lowspeed = lowspeed;
79 
80  // Get device descriptor
81  rcode = pUsb->getDevDescr(0, 0, 8, (uint8_t*)buf);
82 
83  p->lowspeed = false;
84 
85  if(!rcode)
86  len = (buf[0] > 32) ? 32 : buf[0];
87 
88  if(rcode) {
89  // Restore p->epinfo
90  p->epinfo = oldep_ptr;
91  return rcode;
92  }
93 
94  // Extract device class from device descriptor
95  // If device class is not a hub return
96  if(udd->bDeviceClass != 0x09)
98 
99  // Allocate new address according to device class
100  bAddress = addrPool.AllocAddress(parent, (udd->bDeviceClass == 0x09) ? true : false, port);
101 
102  if(!bAddress)
104 
105  // Extract Max Packet Size from the device descriptor
106  epInfo[0].maxPktSize = udd->bMaxPacketSize0;
107 
108  // Assign new address to the device
109  rcode = pUsb->setAddr(0, 0, bAddress);
110 
111  if(rcode) {
112  // Restore p->epinfo
113  p->epinfo = oldep_ptr;
114  addrPool.FreeAddress(bAddress);
115  bAddress = 0;
116  return rcode;
117  }
118 
119  //USBTRACE2("\r\nHub address: ", bAddress );
120 
121  // Restore p->epinfo
122  p->epinfo = oldep_ptr;
123 
124  if(len)
125  rcode = pUsb->getDevDescr(bAddress, 0, len, (uint8_t*)buf);
126 
127  if(rcode)
128  goto FailGetDevDescr;
129 
130  // Assign epInfo to epinfo pointer
131  rcode = pUsb->setEpInfoEntry(bAddress, 2, epInfo);
132 
133  if(rcode)
134  goto FailSetDevTblEntry;
135 
136  // bInitState = 1;
137 
138  // case 1:
139  // Get hub descriptor
140  rcode = GetHubDescriptor(0, 8, buf);
141 
142  if(rcode)
143  goto FailGetHubDescr;
144 
145  // Save number of ports for future use
146  bNbrPorts = hd->bNbrPorts;
147 
148  // bInitState = 2;
149 
150  // case 2:
151  // Read configuration Descriptor in Order To Obtain Proper Configuration Value
152  rcode = pUsb->getConfDescr(bAddress, 0, 8, 0, buf);
153 
154  if(!rcode) {
155  cd_len = ucd->wTotalLength;
156  rcode = pUsb->getConfDescr(bAddress, 0, cd_len, 0, buf);
157  }
158  if(rcode)
159  goto FailGetConfDescr;
160 
161  // The following code is of no practical use in real life applications.
162  // It only intended for the usb protocol sniffer to properly parse hub-class requests.
163  {
164  uint8_t buf2[24];
165 
166  rcode = pUsb->getConfDescr(bAddress, 0, buf[0], 0, buf2);
167 
168  if(rcode)
169  goto FailGetConfDescr;
170  }
171 
172  // Set Configuration Value
173  rcode = pUsb->setConf(bAddress, 0, buf[5]);
174 
175  if(rcode)
176  goto FailSetConfDescr;
177 
178  // bInitState = 3;
179 
180  // case 3:
181  // Power on all ports
182  for(uint8_t j = 1; j <= bNbrPorts; j++)
183  SetPortFeature(HUB_FEATURE_PORT_POWER, j, 0); //HubPortPowerOn(j);
184 
185  pUsb->SetHubPreMask();
186  bPollEnable = true;
187  // bInitState = 0;
188  //}
189  //bInitState = 0;
190  //USBTRACE("...OK\r\n");
191  return 0;
192 
193  // Oleg, No debugging?? -- xxxajk
194 FailGetDevDescr:
195  goto Fail;
196 
197 FailSetDevTblEntry:
198  goto Fail;
199 
200 FailGetHubDescr:
201  goto Fail;
202 
203 FailGetConfDescr:
204  goto Fail;
205 
206 FailSetConfDescr:
207  goto Fail;
208 
209 Fail:
210  USBTRACE("...FAIL\r\n");
211  return rcode;
212 }
213 
214 uint8_t USBHub::Release() {
215  pUsb->GetAddressPool().FreeAddress(bAddress);
216 
217  if(bAddress == 0x41)
218  pUsb->SetHubPreMask();
219 
220  bAddress = 0;
221  bNbrPorts = 0;
222  qNextPollTime = 0;
223  bPollEnable = false;
224  return 0;
225 }
226 
227 uint8_t USBHub::Poll() {
228  uint8_t rcode = 0;
229 
230  if(!bPollEnable)
231  return 0;
232 
233  if(((long)(millis() - qNextPollTime) >= 0L)) {
234  rcode = CheckHubStatus();
235  qNextPollTime = millis() + 100;
236  }
237  return rcode;
238 }
239 
240 uint8_t USBHub::CheckHubStatus() {
241  uint8_t rcode;
242  uint8_t buf[8];
243  uint16_t read = 1;
244 
245  rcode = pUsb->inTransfer(bAddress, 1, &read, buf);
246 
247  if(rcode)
248  return rcode;
249 
250  //if (buf[0] & 0x01) // Hub Status Change
251  //{
252  // pUsb->PrintHubStatus(addr);
253  // rcode = GetHubStatus(1, 0, 1, 4, buf);
254  // if (rcode)
255  // {
256  // USB_HOST_SERIAL.print("GetHubStatus Error");
257  // USB_HOST_SERIAL.println(rcode, HEX);
258  // return rcode;
259  // }
260  //}
261  for(uint8_t port = 1, mask = 0x02; port < 8; mask <<= 1, port++) {
262  if(buf[0] & mask) {
263  HubEvent evt;
264  evt.bmEvent = 0;
265 
266  rcode = GetPortStatus(port, 4, evt.evtBuff);
267 
268  if(rcode)
269  continue;
270 
271  rcode = PortStatusChange(port, evt);
272 
273  if(rcode == HUB_ERROR_PORT_HAS_BEEN_RESET)
274  return 0;
275 
276  if(rcode)
277  return rcode;
278  }
279  } // for
280 
281  for(uint8_t port = 1; port <= bNbrPorts; port++) {
282  HubEvent evt;
283  evt.bmEvent = 0;
284 
285  rcode = GetPortStatus(port, 4, evt.evtBuff);
286 
287  if(rcode)
288  continue;
289 
291  continue;
292 
293  // Emulate connection event for the port
295 
296  rcode = PortStatusChange(port, evt);
297 
298  if(rcode == HUB_ERROR_PORT_HAS_BEEN_RESET)
299  return 0;
300 
301  if(rcode)
302  return rcode;
303  } // for
304  return 0;
305 }
306 
307 void USBHub::ResetHubPort(uint8_t port) {
308  HubEvent evt;
309  evt.bmEvent = 0;
310  uint8_t rcode;
311 
315 
316 
317  for(int i = 0; i < 3; i++) {
318  rcode = GetPortStatus(port, 4, evt.evtBuff);
319  if(rcode) break; // Some kind of error, bail.
321  break;
322  }
323  delay(100); // simulate polling.
324  }
327  delay(20);
328 }
329 
330 uint8_t USBHub::PortStatusChange(uint8_t port, HubEvent &evt) {
331  switch(evt.bmEvent) {
332  // Device connected event
335  if(bResetInitiated)
336  return 0;
337 
341  bResetInitiated = true;
343 
344  // Device disconnected event
348  bResetInitiated = false;
349 
351  a.devAddress = 0;
352  a.bmHub = 0;
353  a.bmParent = bAddress;
354  a.bmAddress = port;
355  pUsb->ReleaseDevice(a.devAddress);
356  return 0;
357 
358  // Reset complete event
363 
364  delay(20);
365 
366  a.devAddress = bAddress;
367 
369  bResetInitiated = false;
370  break;
371 
372  } // switch (evt.bmEvent)
373  return 0;
374 }
375 
376 void PrintHubPortStatus(USBHub *hubptr, uint8_t addr, uint8_t port, bool print_changes) {
377  uint8_t rcode = 0;
378  HubEvent evt;
379 
380  rcode = hubptr->GetPortStatus(port, 4, evt.evtBuff);
381 
382  if(rcode) {
383  USB_HOST_SERIAL.println("ERROR!");
384  return;
385  }
386  USB_HOST_SERIAL.print("\r\nPort ");
387  USB_HOST_SERIAL.println(port, DEC);
388 
389  USB_HOST_SERIAL.println("Status");
390  USB_HOST_SERIAL.print("CONNECTION:\t");
392  USB_HOST_SERIAL.print("ENABLE:\t\t");
393  USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_ENABLE) > 0, DEC);
394  USB_HOST_SERIAL.print("SUSPEND:\t");
395  USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_SUSPEND) > 0, DEC);
396  USB_HOST_SERIAL.print("OVER_CURRENT:\t");
398  USB_HOST_SERIAL.print("RESET:\t\t");
399  USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_RESET) > 0, DEC);
400  USB_HOST_SERIAL.print("POWER:\t\t");
401  USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_POWER) > 0, DEC);
402  USB_HOST_SERIAL.print("LOW_SPEED:\t");
404  USB_HOST_SERIAL.print("HIGH_SPEED:\t");
406  USB_HOST_SERIAL.print("TEST:\t\t");
407  USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_TEST) > 0, DEC);
408  USB_HOST_SERIAL.print("INDICATOR:\t");
410 
411  if(!print_changes)
412  return;
413 
414  USB_HOST_SERIAL.println("\r\nChange");
415  USB_HOST_SERIAL.print("CONNECTION:\t");
417  USB_HOST_SERIAL.print("ENABLE:\t\t");
418  USB_HOST_SERIAL.println((evt.bmChange & bmHUB_PORT_STATUS_C_PORT_ENABLE) > 0, DEC);
419  USB_HOST_SERIAL.print("SUSPEND:\t");
421  USB_HOST_SERIAL.print("OVER_CURRENT:\t");
423  USB_HOST_SERIAL.print("RESET:\t\t");
424  USB_HOST_SERIAL.println((evt.bmChange & bmHUB_PORT_STATUS_C_PORT_RESET) > 0, DEC);
425 }
uint8_t getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t *dataptr)
Definition: Usb.cpp:769
#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:83
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
uint8_t bMaxPacketSize0
Definition: usb_ch9.h:105
#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:808
void ResetHubPort(uint8_t port)
Definition: usbhub.cpp:307
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 bNbrPorts
Definition: usbhub.h:136
uint8_t setAddr(uint8_t oldaddr, uint8_t ep, uint8_t newaddr)
Definition: Usb.cpp:799
#define USB_HOST_SERIAL
Definition: settings.h:34
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:205
uint8_t GetHubDescriptor(uint8_t index, uint16_t nbytes, uint8_t *dataptr)
Definition: usbhub.h:221
#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:216
#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:85
#define bmHUB_PORT_STATUS_C_PORT_RESET
Definition: usbhub.h:92
uint8_t bDeviceClass
Definition: usb_ch9.h:102
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:376
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:82
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:77
uint8_t bmParent
Definition: address.h:63
uint8_t GetPortStatus(uint8_t port, uint16_t nbytes, uint8_t *dataptr)
Definition: usbhub.h:231
#define bmHUB_PORT_EVENT_CONNECT
Definition: usbhub.h:125
uint8_t Release()
Definition: usbhub.cpp:214
#define HUB_FEATURE_PORT_RESET
Definition: usbhub.h:51
#define USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL
Definition: UsbCore.h:80
#define bmHUB_PORT_STATUS_PORT_ENABLE
Definition: usbhub.h:77
uint8_t maxPktSize
Definition: address.h:34
AddressPool & GetAddressPool()
Definition: UsbCore.h:213
uint8_t Configuring(uint8_t parent, uint8_t port, bool lowspeed)
Definition: Usb.cpp:652
Definition: UsbCore.h:197
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:217
#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:246
#define HUB_FEATURE_C_PORT_ENABLE
Definition: usbhub.h:55
#define USBTRACE(s)
Definition: macros.h:75
uint8_t getDevDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *dataptr)
defined(USB_METHODS_INLINE)
Definition: Usb.cpp:764
uint8_t bmHub
Definition: address.h:64
uint8_t Poll()
Definition: usbhub.cpp:227
USBHub(USB *p)
Definition: usbhub.cpp:21
uint8_t ReleaseDevice(uint8_t addr)
Definition: Usb.cpp:749
#define bmHUB_PORT_EVENT_DISCONNECT
Definition: usbhub.h:126