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