1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
|
/*
* This file is part of the libusbhost library
* hosted at http://github.com/libusbhost/libusbhost
*
* Copyright (C) 2015 Amir Hammad <amir.hammad@hotmail.com>
*
*
* libusbhost is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "usbh_driver_hub_private.h"
#include "driver/usbh_device_driver.h"
#include "usart_helpers.h"
#include "usbh_config.h"
#include <stdint.h>
static hub_device_t hub_device[USBH_MAX_HUBS];
static bool initialized = false;
void hub_driver_init(void)
{
uint32_t i;
initialized = true;
for (i = 0; i < USBH_MAX_HUBS; i++) {
hub_device[i].device[0] = 0;
hub_device[i].ports_num = 0;
hub_device[i].current_port = -1;
}
}
/**
*
*
*/
static void *init(void *usbh_dev)
{
if (!initialized) {
LOG_PRINTF("\n%s/%d : driver not initialized\r\n", __FILE__, __LINE__);
return 0;
}
uint32_t i;
hub_device_t *drvdata = 0;
// find free data space for hub device
for (i = 0; i < USBH_MAX_HUBS; i++) {
if (hub_device[i].device[0] == 0) {
break;
}
}
LOG_PRINTF("%{%d}",i);
LOG_FLUSH();
if (i == USBH_MAX_HUBS) {
LOG_PRINTF("ERRRRRRR");
return 0;
}
drvdata = &hub_device[i];
drvdata->state = 0;
drvdata->ports_num = 0;
drvdata->device[0] = (usbh_device_t *)usbh_dev;
drvdata->busy = 0;
drvdata->endpoint_in_address = 0;
drvdata->endpoint_in_maxpacketsize = 0;
// for (i = 1; i < USBH_MAX_HUBS + 1; i++) {
// drvdata->device[i]->address = 0;
// drvdata->device[i]->state = 0;
// }
return drvdata;
}
/**
* Returns true if all needed data are parsed
*/
static bool analyze_descriptor(void *drvdata, void *descriptor)
{
hub_device_t *hub = (hub_device_t *)drvdata;
uint8_t desc_type = ((uint8_t *)descriptor)[1];
switch (desc_type) {
case USB_DT_CONFIGURATION:
{
struct usb_config_descriptor *cfg = (struct usb_config_descriptor*)descriptor;
hub->buffer[0] = cfg->bConfigurationValue;
}
break;
case USB_DT_ENDPOINT:
{
struct usb_endpoint_descriptor *ep = (struct usb_endpoint_descriptor *)descriptor;
if ((ep->bmAttributes&0x03) == USB_ENDPOINT_ATTR_INTERRUPT) {
uint8_t epaddr = ep->bEndpointAddress;
if (epaddr & (1<<7)) {
hub->endpoint_in_address = epaddr&0x7f;
hub->endpoint_in_maxpacketsize = ep->wMaxPacketSize;
}
}
LOG_PRINTF("ENDPOINT DESCRIPTOR FOUND\n");
}
break;
case USB_DT_HUB:
{
struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)descriptor;
//~ hub->ports_num = desc->head.bNbrPorts;
if ( desc->head.bNbrPorts <= USBH_HUB_MAX_DEVICES) {
hub->ports_num = desc->head.bNbrPorts;
} else {
LOG_PRINTF("INCREASE NUMBER OF ENABLED PORTS\n");
hub->ports_num = USBH_HUB_MAX_DEVICES;
}
LOG_PRINTF("HUB DESCRIPTOR FOUND \n");
}
break;
default:
LOG_PRINTF("TYPE: %02X \n",desc_type);
break;
}
if (hub->endpoint_in_address) {
hub->state = 1;
LOG_PRINTF("end enum");
return true;
}
return false;
}
// Enumerate
static void event(usbh_device_t *dev, usbh_packet_callback_data_t cb_data)
{
//~ usbh_device_t *dev = arg;
hub_device_t *hub = (hub_device_t *)dev->drvdata;
LOG_PRINTF("\nHUB->STATE = %d\n", hub->state);
switch (hub->state) {
case 26:
switch (cb_data.status) {
case USBH_PACKET_CALLBACK_STATUS_OK:
{
uint8_t i;
uint8_t *buf = hub->buffer;
uint32_t psc = 0; // Limit: up to 4 bytes...
for (i = 0; i < cb_data.transferred_length; i++) {
psc += buf[i] << (i*8);
}
int8_t port = 0;
LOG_PRINTF("psc:%d\n",psc);
// Driver error... port not found
if (!psc) {
// Continue reading status change endpoint
hub->state = 25;
break;
}
for (i = 0; i <= hub->ports_num; i++) {
if (psc & (1<<i)) {
port = i;
psc = 0;
break;
}
}
if (hub->current_port >= 1) {
if (hub->current_port != port) {
LOG_PRINTF("X");
hub->state = 25;
break;
}
}
struct usb_setup_data setup_data;
// If regular port event, else hub event
if (port) {
setup_data.bmRequestType = 0b10100011;
} else {
setup_data.bmRequestType = 0b10100000;
}
//~ LOG_PRINTF("port:%d", port);
setup_data.bRequest = USB_REQ_GET_STATUS;
setup_data.wValue = 0;
setup_data.wIndex = port;
setup_data.wLength = 4;
hub->state = 31;
hub->current_port = port;
LOG_PRINTF("\n\nPORT FOUND: %d\n", port);
device_xfer_control_write(&setup_data, sizeof(setup_data), event, dev);
}
break;
case USBH_PACKET_CALLBACK_STATUS_EFATAL:
case USBH_PACKET_CALLBACK_STATUS_ERRSIZ:
ERROR(cb_data.status);
hub->state = 0;
break;
case USBH_PACKET_CALLBACK_STATUS_EAGAIN:
// In case of EAGAIN error, retry read on status endpoint
hub->state = 25;
LOG_PRINTF("HUB: Retrying...\n");
break;
}
break;
case EMPTY_PACKET_READ_STATE:
{
LOG_PRINTF("|empty packet read|");
switch (cb_data.status) {
case USBH_PACKET_CALLBACK_STATUS_OK:
device_xfer_control_read(0, 0, event, dev);
hub->state = hub->state_after_empty_read;
hub->state_after_empty_read = 0;
break;
case USBH_PACKET_CALLBACK_STATUS_EFATAL:
case USBH_PACKET_CALLBACK_STATUS_EAGAIN:
case USBH_PACKET_CALLBACK_STATUS_ERRSIZ:
hub->state = hub->state_after_empty_read;
event(dev, cb_data);
break;
}
}
break;
case 3: // Get HUB Descriptor write
{
switch (cb_data.status) {
case USBH_PACKET_CALLBACK_STATUS_OK:
if (hub->ports_num) {
hub->index = 0;
hub->state = 6;
LOG_PRINTF("No need to get HUB DESC\n");
event(dev, cb_data);
} else {
hub->endpoint_in_toggle = 0;
struct usb_setup_data setup_data;
hub->desc_len = hub->device[0]->packet_size_max0;
setup_data.bmRequestType = 0b10100000;
setup_data.bRequest = USB_REQ_GET_DESCRIPTOR;
setup_data.wValue = 0x29<<8;
setup_data.wIndex = 0;
setup_data.wLength = hub->desc_len;
hub->state++;
device_xfer_control_write(&setup_data, sizeof(setup_data), event, dev);
LOG_PRINTF("DO Need to get HUB DESC\n");
}
break;
case USBH_PACKET_CALLBACK_STATUS_EFATAL:
case USBH_PACKET_CALLBACK_STATUS_EAGAIN:
case USBH_PACKET_CALLBACK_STATUS_ERRSIZ:
ERROR(cb_data.status);
break;
}
}
break;
case 4: // Get HUB Descriptor read
{
switch (cb_data.status) {
case USBH_PACKET_CALLBACK_STATUS_OK:
hub->state++;
device_xfer_control_read(hub->buffer, hub->desc_len, event, dev); // "error dynamic size" - bad comment, investigate
break;
case USBH_PACKET_CALLBACK_STATUS_EFATAL:
case USBH_PACKET_CALLBACK_STATUS_EAGAIN:
case USBH_PACKET_CALLBACK_STATUS_ERRSIZ:
ERROR(cb_data.status);
break;
}
}
break;
case 5:// Hub descriptor found
{
switch (cb_data.status) {
case USBH_PACKET_CALLBACK_STATUS_OK:
{
struct usb_hub_descriptor *hub_descriptor =
(struct usb_hub_descriptor *)hub->buffer;
// Check size
if (hub_descriptor->head.bDescLength > hub->desc_len) {
struct usb_setup_data setup_data;
hub->desc_len = hub_descriptor->head.bDescLength;
setup_data.bmRequestType = 0b10100000;
setup_data.bRequest = USB_REQ_GET_DESCRIPTOR;
setup_data.wValue = 0x29<<8;
setup_data.wIndex = 0;
setup_data.wLength = hub->desc_len;
hub->state = 4;
device_xfer_control_write(&setup_data, sizeof(setup_data), event, dev);
break;
} else if (hub_descriptor->head.bDescLength == hub->desc_len) {
hub->ports_num = hub_descriptor->head.bNbrPorts;
hub->state++;
hub->index = 0;
cb_data.status = USBH_PACKET_CALLBACK_STATUS_OK;
event(dev, cb_data);
} else {
//try again
}
}
break;
case USBH_PACKET_CALLBACK_STATUS_ERRSIZ:
{
LOG_PRINTF("->\t\t\t\t\t ERRSIZ: deschub\n");
struct usb_hub_descriptor*hub_descriptor =
(struct usb_hub_descriptor *)hub->buffer;
if (cb_data.transferred_length >= sizeof(struct usb_hub_descriptor_head)) {
if (cb_data.transferred_length == hub_descriptor->head.bDescLength) {
// Process HUB descriptor
if ( hub_descriptor->head.bNbrPorts <= USBH_HUB_MAX_DEVICES) {
hub->ports_num = hub_descriptor->head.bNbrPorts;
} else {
LOG_PRINTF("INCREASE NUMBER OF ENABLED PORTS\n");
hub->ports_num = USBH_HUB_MAX_DEVICES;
}
hub->state++;
hub->index = 0;
cb_data.status = USBH_PACKET_CALLBACK_STATUS_OK;
event(dev, cb_data);
}
}
}
break;
case USBH_PACKET_CALLBACK_STATUS_EFATAL:
case USBH_PACKET_CALLBACK_STATUS_EAGAIN:
ERROR(cb_data.status);
break;
}
}
break;
case 6:// enable ports
{
switch (cb_data.status) {
case USBH_PACKET_CALLBACK_STATUS_OK:
if (hub->index < hub->ports_num) {
hub->index++;
struct usb_setup_data setup_data;
LOG_PRINTF("[!%d!]",hub->index);
setup_data.bmRequestType = 0b00100011;
setup_data.bRequest = HUB_REQ_SET_FEATURE;
setup_data.wValue = HUB_FEATURE_PORT_POWER;
setup_data.wIndex = hub->index;
setup_data.wLength = 0;
hub->state_after_empty_read = hub->state;
hub->state = EMPTY_PACKET_READ_STATE;
device_xfer_control_write(&setup_data, sizeof(setup_data), event, dev);
} else {
hub->state++;
// TODO:
// Delay Based on hub descriptor field bPwr2PwrGood
// delay_ms_busy_loop(200);
LOG_PRINTF("\nHUB CONFIGURED & PORTS POWERED\n");
cb_data.status = USBH_PACKET_CALLBACK_STATUS_OK;
event(dev, cb_data);
}
break;
case USBH_PACKET_CALLBACK_STATUS_EFATAL:
case USBH_PACKET_CALLBACK_STATUS_EAGAIN:
case USBH_PACKET_CALLBACK_STATUS_ERRSIZ:
ERROR(cb_data.status);
break;
}
}
break;
case 7:
{
switch (cb_data.status) {
case USBH_PACKET_CALLBACK_STATUS_OK:
{
struct usb_setup_data setup_data;
setup_data.bmRequestType = 0b10100000;
setup_data.bRequest = USB_REQ_GET_STATUS;
setup_data.wValue = 0;
setup_data.wIndex = 0;
setup_data.wLength = 4;
hub->state++;
device_xfer_control_write(&setup_data, sizeof(setup_data), event, dev);
}
break;
case USBH_PACKET_CALLBACK_STATUS_EFATAL:
case USBH_PACKET_CALLBACK_STATUS_EAGAIN:
case USBH_PACKET_CALLBACK_STATUS_ERRSIZ:
ERROR(cb_data.status);
break;
}
}
break;
case 8:
{
switch (cb_data.status) {
case USBH_PACKET_CALLBACK_STATUS_OK:
device_xfer_control_read(hub->buffer, 4, event, dev);
hub->index = 0;
hub->state++;
break;
case USBH_PACKET_CALLBACK_STATUS_EFATAL:
case USBH_PACKET_CALLBACK_STATUS_EAGAIN:
case USBH_PACKET_CALLBACK_STATUS_ERRSIZ:
ERROR(cb_data.status);
break;
}
}
break;
case 9:
{
switch (cb_data.status) {
case USBH_PACKET_CALLBACK_STATUS_OK:
{
struct usb_setup_data setup_data;
setup_data.bmRequestType = 0b10100011;
setup_data.bRequest = USB_REQ_GET_STATUS;
setup_data.wValue = 0;
setup_data.wIndex = ++hub->index;
setup_data.wLength = 4;
hub->state++;
device_xfer_control_write(&setup_data, sizeof(setup_data), event, dev);
}
break;
case USBH_PACKET_CALLBACK_STATUS_EFATAL:
case USBH_PACKET_CALLBACK_STATUS_EAGAIN:
case USBH_PACKET_CALLBACK_STATUS_ERRSIZ:
ERROR(cb_data.status);
break;
}
}
break;
case 10:
{
switch (cb_data.status) {
case USBH_PACKET_CALLBACK_STATUS_OK:
device_xfer_control_read(hub->buffer, 4, event, dev);
hub->state++;
break;
case USBH_PACKET_CALLBACK_STATUS_EFATAL:
case USBH_PACKET_CALLBACK_STATUS_EAGAIN:
case USBH_PACKET_CALLBACK_STATUS_ERRSIZ:
ERROR(cb_data.status);
break;
}
}
break;
case 11:
{
switch (cb_data.status) {
case USBH_PACKET_CALLBACK_STATUS_OK:
if (hub->index < hub->ports_num) {
hub->state = 9;
// process data contained in hub->buffer
// TODO:
cb_data.status = USBH_PACKET_CALLBACK_STATUS_OK;
event(dev, cb_data);
} else {
hub->busy = 0;
hub->state = 25;
}
break;
case USBH_PACKET_CALLBACK_STATUS_EFATAL:
case USBH_PACKET_CALLBACK_STATUS_EAGAIN:
case USBH_PACKET_CALLBACK_STATUS_ERRSIZ:
ERROR(cb_data.status);
break;
}
}
break;
case 31: // Read port status
{
switch (cb_data.status) {
case USBH_PACKET_CALLBACK_STATUS_OK:
{
int8_t port = hub->current_port;
hub->state++;
// TODO: rework to endianess aware,
// (maybe whole library is affected by this)
// Detail:
// Isn't universal. Here is endianess ok,
// but on another architecture must not be
device_xfer_control_read(&hub->hub_and_port_status[port], 4, event, dev);
}
break;
case USBH_PACKET_CALLBACK_STATUS_EFATAL:
case USBH_PACKET_CALLBACK_STATUS_EAGAIN:
case USBH_PACKET_CALLBACK_STATUS_ERRSIZ:
ERROR(cb_data.status);
// continue
hub->state = 25;
break;
}
}
break;
case 32:
{
switch (cb_data.status) {
case USBH_PACKET_CALLBACK_STATUS_OK:
{
int8_t port = hub->current_port;
LOG_PRINTF("|%d",port);
// Get Port status, else Get Hub status
if (port) {
uint16_t stc = hub->hub_and_port_status[port].stc;
// Connection status changed
if (stc & (1<<HUB_FEATURE_PORT_CONNECTION)) {
// Check, whether device is in connected state
if (!hub->device[port]) {
if (!usbh_enum_available() || hub->busy) {
LOG_PRINTF("\n\t\t\tCannot enumerate %d %d\n", !usbh_enum_available(), hub->busy);
hub->state = 25;
break;
}
}
// clear feature C_PORT_CONNECTION
struct usb_setup_data setup_data;
setup_data.bmRequestType = 0b00100011;
setup_data.bRequest = HUB_REQ_CLEAR_FEATURE;
setup_data.wValue = HUB_FEATURE_C_PORT_CONNECTION;
setup_data.wIndex = port;
setup_data.wLength = 0;
hub->state_after_empty_read = 33;
hub->state = EMPTY_PACKET_READ_STATE;
device_xfer_control_write(&setup_data, sizeof(setup_data), event, dev);
} else if(stc & (1<<HUB_FEATURE_PORT_RESET)) {
// clear feature C_PORT_RESET
// Reset processing is complete, enumerate device
struct usb_setup_data setup_data;
setup_data.bmRequestType = 0b00100011;
setup_data.bRequest = HUB_REQ_CLEAR_FEATURE;
setup_data.wValue = HUB_FEATURE_C_PORT_RESET;
setup_data.wIndex = port;
setup_data.wLength = 0;
hub->state_after_empty_read = 35;
hub->state = EMPTY_PACKET_READ_STATE;
LOG_PRINTF("RESET");
device_xfer_control_write(&setup_data, sizeof(setup_data), event, dev);
} else {
LOG_PRINTF("another STC %d\n", stc);
}
} else {
hub->state = 25;
LOG_PRINTF("HUB status change\n");
}
}
break;
case USBH_PACKET_CALLBACK_STATUS_EFATAL:
case USBH_PACKET_CALLBACK_STATUS_EAGAIN:
case USBH_PACKET_CALLBACK_STATUS_ERRSIZ:
ERROR(cb_data.status);
// continue
hub->state = 25;
break;
}
}
break;
case 33:
{
switch (cb_data.status) {
case USBH_PACKET_CALLBACK_STATUS_OK:
{
int8_t port = hub->current_port;
uint16_t stc = hub->hub_and_port_status[port].stc;
if (!hub->device[port]) {
if ((stc) & (1<<HUB_FEATURE_PORT_CONNECTION)) {
struct usb_setup_data setup_data;
setup_data.bmRequestType = 0b00100011;
setup_data.bRequest = HUB_REQ_SET_FEATURE;
setup_data.wValue = HUB_FEATURE_PORT_RESET;
setup_data.wIndex = port;
setup_data.wLength = 0;
hub->state_after_empty_read = 11;
hub->state = EMPTY_PACKET_READ_STATE;
LOG_PRINTF("CONN");
hub->busy = 1;
device_xfer_control_write(&setup_data, sizeof(setup_data), event, dev);
}
} else {
LOG_PRINTF("\t\t\t\tDISCONNECT EVENT\n");
if (hub->device[port]->drv && hub->device[port]->drvdata) {
hub->device[port]->drv->remove(hub->device[port]->drvdata);
}
hub->device[port]->address = -1;
hub->device[port]->drv = 0;
hub->device[port]->drvdata = 0;
hub->device[port] = 0;
hub->current_port = CURRENT_PORT_NONE;
hub->state = 25;
hub->busy = 0;
}
}
break;
case USBH_PACKET_CALLBACK_STATUS_EFATAL:
case USBH_PACKET_CALLBACK_STATUS_EAGAIN:
case USBH_PACKET_CALLBACK_STATUS_ERRSIZ:
ERROR(cb_data.status);
// continue
hub->state = 25;
break;
}
}
break;
case 35: // RESET COMPLETE, start enumeration
{
switch (cb_data.status) {
case USBH_PACKET_CALLBACK_STATUS_OK:
{
LOG_PRINTF("\nPOLL\n");
int8_t port = hub->current_port;
uint16_t sts = hub->hub_and_port_status[port].sts;
if (sts & (1<<HUB_FEATURE_PORT_ENABLE)) {
hub->device[port] = usbh_get_free_device(dev);
if (!hub->device[port]) {
LOG_PRINTF("\nFATAL ERROR\n");
return;// DEAD END
}
if ((sts & (1<<(HUB_FEATURE_PORT_LOWSPEED))) &&
!(sts & (1<<(HUB_FEATURE_PORT_HIGHSPEED)))) {
LOG_PRINTF("Low speed device");
// Disable Low speed device immediately
struct usb_setup_data setup_data;
setup_data.bmRequestType = 0b00100011;
setup_data.bRequest = HUB_REQ_CLEAR_FEATURE;
setup_data.wValue = HUB_FEATURE_PORT_ENABLE;
setup_data.wIndex = port;
setup_data.wLength = 0;
// After write process another devices, poll for events
hub->state_after_empty_read = 11;//Expecting all ports are powered (constant/non-changeable after init)
hub->state = EMPTY_PACKET_READ_STATE;
hub->current_port = CURRENT_PORT_NONE;
device_xfer_control_write(&setup_data, sizeof(setup_data), event, dev);
} else if (!(sts & (1<<(HUB_FEATURE_PORT_LOWSPEED))) &&
!(sts & (1<<(HUB_FEATURE_PORT_HIGHSPEED)))) {
hub->device[port]->speed = USBH_SPEED_FULL;
LOG_PRINTF("Full speed device");
hub->timestamp_us = hub->time_curr_us;
hub->state = 100; // schedule wait for 500ms
}
} else {
LOG_PRINTF("%s:%d Do not know what to do, when device is disabled after reset\n", __FILE__, __LINE__);
hub->state = 25;
return;
}
}
break;
case USBH_PACKET_CALLBACK_STATUS_EFATAL:
case USBH_PACKET_CALLBACK_STATUS_EAGAIN:
case USBH_PACKET_CALLBACK_STATUS_ERRSIZ:
ERROR(cb_data.status);
// continue
hub->state = 25;
break;
}
}
break;
default:
LOG_PRINTF("UNHANDLED EVENT %d\n",hub->state);
break;
}
}
static void read_ep1(void *drvdata)
{
hub_device_t *hub = (hub_device_t *)drvdata;
usbh_packet_t packet;
packet.address = hub->device[0]->address;
packet.data = hub->buffer;
packet.datalen = hub->endpoint_in_maxpacketsize;
packet.endpoint_address = hub->endpoint_in_address;
packet.endpoint_size_max = hub->endpoint_in_maxpacketsize;
packet.endpoint_type = USBH_EPTYP_INTERRUPT;
packet.speed = hub->device[0]->speed;
packet.callback = event;
packet.callback_arg = hub->device[0];
packet.toggle = &hub->endpoint_in_toggle;
hub->state = 26;
usbh_read(hub->device[0], &packet);
LOG_PRINTF("@hub %d/EP1 | \n", hub->device[0]->address);
}
/**
* \param time_curr_us - monotically rising time (see usbh_hubbed.h)
* unit is microseconds
*/
static void poll(void *drvdata, uint32_t time_curr_us)
{
hub_device_t *hub = (hub_device_t *)drvdata;
usbh_device_t *dev = hub->device[0];
hub->time_curr_us = time_curr_us;
switch (hub->state) {
case 25:
{
if (usbh_enum_available()) {
read_ep1(hub);
} else {
LOG_PRINTF("enum not available\n");
}
}
break;
case 1:
{
LOG_PRINTF("CFGVAL: %d\n", hub->buffer[0]);
struct usb_setup_data setup_data;
setup_data.bmRequestType = 0b00000000;
setup_data.bRequest = USB_REQ_SET_CONFIGURATION;
setup_data.wValue = hub->buffer[0];
setup_data.wIndex = 0;
setup_data.wLength = 0;
hub->state = EMPTY_PACKET_READ_STATE;
hub->state_after_empty_read = 3;
device_xfer_control_write(&setup_data, sizeof(setup_data), event, dev);
}
break;
case 100:
if (hub->time_curr_us - hub->timestamp_us > 500000) {
int8_t port = hub->current_port;
LOG_PRINTF("PORT: %d", port);
LOG_PRINTF("\nNEW device at address: %d\n", hub->device[port]->address);
hub->device[port]->lld = hub->device[0]->lld;
device_enumeration_start(hub->device[port]);
hub->current_port = CURRENT_PORT_NONE;
// Maybe error, when assigning address is taking too long
//
// Detail:
// USB hub cannot enable another port while the device
// the current one is also in address state (has address==0)
// Only one device on bus can have address==0
hub->busy = 0;
hub->state = 25;
}
break;
default:
break;
}
if (usbh_enum_available()) {
uint32_t i;
for (i = 1; i < USBH_HUB_MAX_DEVICES + 1; i++) {
if (hub->device[i]) {
if (hub->device[i]->drv && hub->device[i]->drvdata) {
hub->device[i]->drv->poll(hub->device[i]->drvdata, time_curr_us);
}
}
}
}
}
static void remove(void *drvdata)
{
hub_device_t *hub = (hub_device_t *)drvdata;
uint8_t i;
// Call fast... to avoid polling
hub->state = 0;
hub->endpoint_in_address = 0;
hub->busy = 0;
for (i = 1; i < USBH_HUB_MAX_DEVICES + 1; i++) {
if (hub->device[i]) {
if (hub->device[i]->drv && hub->device[i]->drvdata) {
if (hub->device[i]->drv->remove != remove) {
LOG_PRINTF("\t\t\t\tHUB REMOVE %d\n",hub->device[i]->address);
hub->device[i]->drv->remove(hub->device[i]->drvdata);
}
}
hub->device[i] = 0;
}
hub->device[0]->drv = 0;
hub->device[0]->drvdata = 0;
hub->device[0] = 0;
}
}
static const usbh_dev_driver_info_t driver_info = {
.deviceClass = 0x09,
.deviceSubClass = -1,
.deviceProtocol = -1,
.idVendor = -1,
.idProduct = -1,
.ifaceClass = 0x09,
.ifaceSubClass = -1,
.ifaceProtocol = -1
};
const usbh_dev_driver_t usbh_hub_driver = {
.init = init,
.analyze_descriptor = analyze_descriptor,
.poll = poll,
.remove = remove,
.info = &driver_info
};
|