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.bmHub = 0;
349  a.bmParent = bAddress;
350  a.bmAddress = port;
351  pUsb->ReleaseDevice(a.devAddress);
352  return 0;
353 
354  // Reset complete event
359 
360  delay(20);
361 
362  a.devAddress = bAddress;
363 
365  bResetInitiated = false;
366  break;
367 
368  } // switch (evt.bmEvent)
369  return 0;
370 }
371 
372 void PrintHubPortStatus(USBHub *hubptr, uint8_t addr, uint8_t port, bool print_changes) {
373  uint8_t rcode = 0;
374  HubEvent evt;
375 
376  rcode = hubptr->GetPortStatus(port, 4, evt.evtBuff);
377 
378  if (rcode) {
379  USB_HOST_SERIAL.println("ERROR!");
380  return;
381  }
382  USB_HOST_SERIAL.print("\r\nPort ");
383  USB_HOST_SERIAL.println(port, DEC);
384 
385  USB_HOST_SERIAL.println("Status");
386  USB_HOST_SERIAL.print("CONNECTION:\t");
388  USB_HOST_SERIAL.print("ENABLE:\t\t");
389  USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_ENABLE) > 0, DEC);
390  USB_HOST_SERIAL.print("SUSPEND:\t");
391  USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_SUSPEND) > 0, DEC);
392  USB_HOST_SERIAL.print("OVER_CURRENT:\t");
394  USB_HOST_SERIAL.print("RESET:\t\t");
395  USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_RESET) > 0, DEC);
396  USB_HOST_SERIAL.print("POWER:\t\t");
397  USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_POWER) > 0, DEC);
398  USB_HOST_SERIAL.print("LOW_SPEED:\t");
400  USB_HOST_SERIAL.print("HIGH_SPEED:\t");
402  USB_HOST_SERIAL.print("TEST:\t\t");
403  USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_TEST) > 0, DEC);
404  USB_HOST_SERIAL.print("INDICATOR:\t");
406 
407  if (!print_changes)
408  return;
409 
410  USB_HOST_SERIAL.println("\r\nChange");
411  USB_HOST_SERIAL.print("CONNECTION:\t");
413  USB_HOST_SERIAL.print("ENABLE:\t\t");
414  USB_HOST_SERIAL.println((evt.bmChange & bmHUB_PORT_STATUS_C_PORT_ENABLE) > 0, DEC);
415  USB_HOST_SERIAL.print("SUSPEND:\t");
417  USB_HOST_SERIAL.print("OVER_CURRENT:\t");
419  USB_HOST_SERIAL.print("RESET:\t\t");
420  USB_HOST_SERIAL.println((evt.bmChange & bmHUB_PORT_STATUS_C_PORT_RESET) > 0, DEC);
421 }