USB Host Shield 2.0
Loading...
Searching...
No Matches
usbhub.cpp
Go to the documentation of this file.
1/* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
2
3This software may be distributed and modified under the terms of the GNU
4General Public License version 2 (GPL2) as published by the Free Software
5Foundation and appearing in the file GPL2.TXT included in the packaging of
6this file. Please note that GPL2 Section 2[b] requires that all works based
7on this software must also be made publicly available under the terms of
8the GPL2 ("Copyleft").
9
10Contact information
11-------------------
12
13Circuits At Home, LTD
14Web : http://www.circuitsathome.com
15e-mail : support@circuitsathome.com
16 */
17#include "usbhub.h"
18
19bool USBHub::bResetInitiated = false;
20
22pUsb(p),
23bAddress(0),
24bNbrPorts(0),
25//bInitState(0),
26qNextPollTime(0),
27bPollEnable(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
45 uint8_t buf[32];
46 USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
47 HubDescriptor* hd = reinterpret_cast<HubDescriptor*>(buf);
50 UsbDevice *p = 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)
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
197 goto Fail;
198
200 goto Fail;
201
203 goto Fail;
204
206 goto Fail;
207
209 goto Fail;
210
211Fail:
212 USBTRACE("...FAIL\r\n");
213 return rcode;
214}
215
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
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
242uint8_t USBHub::CheckHubStatus() {
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) {
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
276 return 0;
277
278 if(rcode)
279 return rcode;
280 }
281 } // for
282
283 for(uint8_t port = 1; port <= bNbrPorts; port++) {
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
301 return 0;
302
303 if(rcode)
304 return rcode;
305 } // for
306 return 0;
307}
308
311 evt.bmEvent = 0;
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
332uint8_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
379 uint8_t rcode = 0;
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");
393 USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_CONNECTION) > 0, DEC);
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");
405 USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_LOW_SPEED) > 0, DEC);
406 USB_HOST_SERIAL.print("HIGH_SPEED:\t");
407 USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_HIGH_SPEED) > 0, DEC);
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");
411 USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_INDICATOR) > 0, DEC);
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");
422 USB_HOST_SERIAL.println((evt.bmChange & bmHUB_PORT_STATUS_C_PORT_SUSPEND) > 0, DEC);
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}
#define USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL
Definition UsbCore.h:103
#define USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE
Definition UsbCore.h:108
#define USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED
Definition UsbCore.h:100
#define USB_ERROR_EPINFO_IS_NULL
Definition UsbCore.h:106
#define USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL
Definition UsbCore.h:105
#define USB_NAK_MAX_POWER
Definition address.h:34
#define USB_NAK_NOWAIT
Definition address.h:36
virtual void FreeAddress(uint8_t addr)=0
virtual UsbDevice * GetUsbDevicePtr(uint8_t addr)=0
virtual uint8_t AllocAddress(uint8_t parent, bool is_hub=false, uint8_t port=0)=0
Definition UsbCore.h:220
AddressPool & GetAddressPool()
Definition UsbCore.h:236
uint8_t getDevDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *dataptr)
defined(USB_METHODS_INLINE)
Definition Usb.cpp:801
uint8_t setConf(uint8_t addr, uint8_t ep, uint8_t conf_value)
Definition Usb.cpp:850
uint8_t setAddr(uint8_t oldaddr, uint8_t ep, uint8_t newaddr)
Definition Usb.cpp:841
uint8_t RegisterDeviceClass(USBDeviceConfig *pdev)
Definition UsbCore.h:240
uint8_t getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t *dataptr)
Definition Usb.cpp:806
uint8_t setEpInfoEntry(uint8_t addr, uint8_t epcount, EpInfo *eprecord_ptr)
Definition Usb.cpp:64
uint8_t ReleaseDevice(uint8_t addr)
Definition Usb.cpp:786
uint8_t inTransfer(uint8_t addr, uint8_t ep, uint16_t *nbytesptr, uint8_t *data, uint8_t bInterval=0)
Definition Usb.cpp:209
void SetHubPreMask()
Definition UsbCore.h:228
uint8_t Configuring(uint8_t parent, uint8_t port, bool lowspeed)
Definition Usb.cpp:688
uint8_t GetPortStatus(uint8_t port, uint16_t nbytes, uint8_t *dataptr)
Definition usbhub.h:231
uint8_t ClearPortFeature(uint8_t fid, uint8_t port, uint8_t sel=0)
Definition usbhub.h:216
uint8_t Release()
Definition usbhub.cpp:216
uint8_t GetHubDescriptor(uint8_t index, uint16_t nbytes, uint8_t *dataptr)
Definition usbhub.h:221
uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed)
Definition usbhub.cpp:44
uint8_t SetPortFeature(uint8_t fid, uint8_t port, uint8_t sel=0)
Definition usbhub.h:246
uint8_t Poll()
Definition usbhub.cpp:229
void ResetHubPort(uint8_t port)
Definition usbhub.cpp:309
USBHub(USB *p)
Definition usbhub.cpp:21
#define USBTRACE(s)
Definition macros.h:82
#define USB_HOST_SERIAL
Definition settings.h:49
uint8_t bmNakPower
Definition address.h:49
uint8_t bmRcvToggle
Definition address.h:48
uint8_t epAddr
Definition address.h:40
uint8_t maxPktSize
Definition address.h:41
uint8_t bmSndToggle
Definition address.h:47
uint8_t bmHub
Definition address.h:71
uint8_t devAddress
Definition address.h:74
uint8_t bmParent
Definition address.h:70
uint8_t bmAddress
Definition address.h:69
void PrintHubPortStatus(USBHub *hubptr, uint8_t addr, uint8_t port, bool print_changes)
Definition usbhub.cpp:378
#define bmHUB_PORT_EVENT_CONNECT
Definition usbhub.h:125
#define HUB_FEATURE_PORT_RESET
Definition usbhub.h:51
#define bmHUB_PORT_STATUS_C_PORT_ENABLE
Definition usbhub.h:89
#define bmHUB_PORT_STATUS_PORT_SUSPEND
Definition usbhub.h:78
#define bmHUB_PORT_STATUS_PORT_POWER
Definition usbhub.h:81
#define bmHUB_PORT_EVENT_DISCONNECT
Definition usbhub.h:126
#define bmHUB_PORT_STATUS_PORT_ENABLE
Definition usbhub.h:77
#define bmHUB_PORT_STATE_DISABLED
Definition usbhub.h:122
#define bmHUB_PORT_STATUS_PORT_RESET
Definition usbhub.h:80
#define bmHUB_PORT_STATUS_C_PORT_OVER_CURRENT
Definition usbhub.h:91
#define bmHUB_PORT_STATUS_PORT_INDICATOR
Definition usbhub.h:85
#define bmHUB_PORT_STATUS_PORT_LOW_SPEED
Definition usbhub.h:82
#define HUB_ERROR_PORT_HAS_BEEN_RESET
Definition usbhub.h:113
#define bmHUB_PORT_STATUS_PORT_OVER_CURRENT
Definition usbhub.h:79
#define bmHUB_PORT_STATUS_PORT_HIGH_SPEED
Definition usbhub.h:83
#define HUB_FEATURE_C_PORT_RESET
Definition usbhub.h:58
#define bmHUB_PORT_STATUS_PORT_CONNECTION
Definition usbhub.h:76
#define bmHUB_PORT_STATE_CHECK_DISABLED
Definition usbhub.h:119
#define bmHUB_PORT_STATUS_C_PORT_RESET
Definition usbhub.h:92
#define bmHUB_PORT_EVENT_RESET_COMPLETE
Definition usbhub.h:127
#define bmHUB_PORT_EVENT_LS_CONNECT
Definition usbhub.h:129
#define HUB_FEATURE_C_PORT_ENABLE
Definition usbhub.h:55
#define bmHUB_PORT_STATUS_PORT_TEST
Definition usbhub.h:84
#define bmHUB_PORT_STATUS_C_PORT_SUSPEND
Definition usbhub.h:90
#define bmHUB_PORT_STATUS_C_PORT_CONNECTION
Definition usbhub.h:88
#define HUB_FEATURE_PORT_POWER
Definition usbhub.h:52
#define HUB_FEATURE_C_PORT_CONNECTION
Definition usbhub.h:54
#define bmHUB_PORT_EVENT_LS_RESET_COMPLETE
Definition usbhub.h:130