-
Notifications
You must be signed in to change notification settings - Fork 87
/
ux_host_stack_tasks_run.c
1156 lines (956 loc) · 46.4 KB
/
ux_host_stack_tasks_run.c
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
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Copyright (c) 2024 Microsoft Corporation
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
* SPDX-License-Identifier: MIT
**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/** */
/** USBX Component */
/** */
/** Device Stack */
/** */
/**************************************************************************/
/**************************************************************************/
#define UX_SOURCE_CODE
/* Include necessary system files. */
#include "ux_api.h"
#include "ux_host_stack.h"
#if defined(UX_HOST_STANDALONE)
#define UX_HOST_STACK_ENUM_TRANS_ERROR(t) ( \
(t)->ux_transfer_request_completion_code != UX_SUCCESS || \
(t)->ux_transfer_request_actual_length != \
(t)->ux_transfer_request_requested_length)
static inline VOID _ux_host_stack_port_check_run(VOID);
static inline VOID _ux_host_stack_enum_run(VOID);
static inline VOID _ux_host_stack_pending_transfers_run(VOID);
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _ux_host_stack_tasks_run PORTABLE C */
/* 6.3.0 */
/* AUTHOR */
/* */
/* Chaoqiong Xiao, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function runs host stack and registered classes tasks. */
/* */
/* It's valid only in standalone mode. */
/* It's non-blocking. */
/* */
/* INPUT */
/* */
/* */
/* OUTPUT */
/* */
/* Completion State Status */
/* */
/* CALLS */
/* */
/* (ux_hcd_entry_function) HCD function */
/* (ux_host_class_task_function) Host class task function */
/* (ux_system_host_change_function) Host change callback function */
/* (ux_system_host_enum_hub_function) Host hub enumeration function */
/* _ux_host_stack_rh_change_process Host Root Hub process */
/* _ux_host_stack_device_address_set Start process to set address */
/* _ux_host_stack_configuration_set Start process to set config */
/* _ux_host_stack_device_descriptor_read Start process to read device */
/* descriptor */
/* _ux_host_stack_new_configuration_create */
/* Link a new configuration to */
/* a device */
/* _ux_host_stack_configuration_instance_create */
/* Create configuration instance */
/* _ux_host_stack_transfer_run Runs transfer state machine */
/* _ux_host_stack_class_device_scan Query device class driver */
/* _ux_host_stack_class_interface_scan Query interface class driver */
/* _ux_host_stack_interfaces_scan Parse interfaces in a */
/* configuration */
/* _ux_host_stack_device_remove Remove a device */
/* _ux_utility_descriptor_parse Parse a descriptor */
/* _ux_utility_memory_allocate Allocate memory */
/* _ux_utility_memory_free Free memory */
/* _ux_utility_time_get Get current time tick */
/* _ux_system_error_handler Error trap */
/* */
/* CALLED BY */
/* */
/* Application */
/* */
/* RELEASE HISTORY */
/* */
/* DATE NAME DESCRIPTION */
/* */
/* 01-31-2022 Chaoqiong Xiao Initial Version 6.1.10 */
/* 07-29-2022 Chaoqiong Xiao Modified comment(s), */
/* added standalone hub, */
/* used shared descriptor in */
/* device instance for enum, */
/* resulting in version 6.1.12 */
/* 10-31-2022 Chaoqiong Xiao Modified comment(s), */
/* improved internal logic, */
/* fixed activation issue on */
/* no class linked interfaces, */
/* resulting in version 6.2.0 */
/* 10-31-2023 Chaoqiong Xiao Modified comment(s), */
/* improved enum transfer, */
/* resulting in version 6.3.0 */
/* */
/**************************************************************************/
UINT _ux_host_stack_tasks_run(VOID)
{
UX_HCD *hcd_inst;
ULONG hcd_index;
UX_HOST_CLASS *class_inst;
ULONG class_index;
/* =========== Run all HCD tasks. */
for (hcd_index = 0; hcd_index < UX_SYSTEM_HOST_MAX_HCD_GET(); hcd_index++)
{
hcd_inst = &_ux_system_host->ux_system_host_hcd_array[hcd_index];
if (hcd_inst -> ux_hcd_status != UX_HCD_STATUS_OPERATIONAL ||
hcd_inst -> ux_hcd_entry_function == UX_NULL)
continue;
hcd_inst -> ux_hcd_entry_function(hcd_inst, UX_HCD_TASKS_RUN, UX_NULL);
}
/* =========== Run port check process. */
_ux_host_stack_port_check_run();
/* =========== Run enumeration process. */
_ux_host_stack_enum_run();
/* =========== Run classes tasks. */
for (class_index = 0; class_index < UX_SYSTEM_HOST_MAX_CLASS_GET(); class_index++)
{
class_inst = &_ux_system_host->ux_system_host_class_array[class_index];
if ((class_inst -> ux_host_class_status == UX_UNUSED) ||
(class_inst -> ux_host_class_task_function == UX_NULL))
continue;
class_inst -> ux_host_class_task_function(class_inst);
}
/* =========== Run pending transfer tasks. */
_ux_host_stack_pending_transfers_run();
/* =========== Run background tasks. */
if (_ux_system_host -> ux_system_host_change_function)
{
_ux_system_host -> ux_system_host_change_function(
UX_STANDALONE_WAIT_BACKGROUND_TASK, UX_NULL, UX_NULL);
}
/* No idle report support now. */
return (UX_STATE_WAIT);
}
static inline VOID _ux_host_stack_port_check_run(VOID)
{
#if UX_MAX_DEVICES > 1
/* We try the hub first. For this we look into the USBX project
structure to see if there is at least one hub. */
if (_ux_system_host->ux_system_host_enum_hub_function != UX_NULL)
{
/* Yes, there is a HUB function, call it! */
_ux_system_host->ux_system_host_enum_hub_function();
}
#endif
/* Check root hub changes. */
_ux_host_stack_rh_change_process();
}
static inline UINT _ux_host_stack_rh_port_enable(UX_DEVICE *device)
{
UX_HCD *hcd = UX_DEVICE_HCD_GET(device);
ALIGN_TYPE port_index = (ALIGN_TYPE)UX_DEVICE_PORT_LOCATION_GET(device);
return hcd -> ux_hcd_entry_function(hcd, UX_HCD_ENABLE_PORT, (VOID *)port_index);
}
static inline UINT _ux_host_stack_rh_port_reset(UX_DEVICE *device)
{
UX_HCD *hcd = UX_DEVICE_HCD_GET(device);
ALIGN_TYPE port_index = (ALIGN_TYPE)UX_DEVICE_PORT_LOCATION_GET(device);
return hcd -> ux_hcd_entry_function(hcd, UX_HCD_RESET_PORT, (VOID *)port_index);
}
static inline UINT _ux_host_stack_rh_port_status_get(UX_DEVICE *device)
{
UX_HCD *hcd = UX_DEVICE_HCD_GET(device);
ALIGN_TYPE port_index = (ALIGN_TYPE)UX_DEVICE_PORT_LOCATION_GET(device);
return hcd -> ux_hcd_entry_function(hcd, UX_HCD_GET_PORT_STATUS, (VOID *)port_index);
}
static inline VOID _ux_host_stack_enum_address_sent(UX_DEVICE *device)
{
/* Address (request wValue) applied to device. */
device -> ux_device_address = device -> ux_device_control_endpoint.
ux_endpoint_transfer_request.ux_transfer_request_value & 0x7F;
device -> ux_device_state = UX_DEVICE_ADDRESSED;
/* Unlock enumeration. */
_ux_system_host -> ux_system_host_enum_lock = UX_NULL;
}
static inline VOID _ux_host_stack_enum_device_descriptor_parse0(
UX_DEVICE *device, UCHAR *descriptor)
{
/* Endpoint descriptor updated. */
device -> ux_device_descriptor.bMaxPacketSize0 = descriptor[7];
/* Update EP0 wMaxPacketSize. */
device -> ux_device_control_endpoint.
ux_endpoint_descriptor.wMaxPacketSize = descriptor[7];
}
static inline VOID _ux_host_stack_enum_configuration_descriptor_parse0(
UX_DEVICE *device, UX_CONFIGURATION *configuration, UCHAR *descriptor)
{
/* This configuration must be linked to the device. */
_ux_host_stack_new_configuration_create(device, configuration);
/* The descriptor is in a packed format, parse it locally. */
_ux_utility_descriptor_parse(descriptor,
_ux_system_configuration_descriptor_structure,
UX_CONFIGURATION_DESCRIPTOR_ENTRIES,
(UCHAR *) &configuration -> ux_configuration_descriptor);
}
static inline UINT _ux_host_stack_enum_configuration_read(UX_DEVICE *device)
{
UX_CONFIGURATION *configuration;
UCHAR *descriptor;
UX_ENDPOINT *control_endpoint;
UX_TRANSFER *transfer_request;
/* Allocate configuration containter for the descriptor. */
configuration = _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY,
sizeof(UX_CONFIGURATION));
if (configuration == UX_NULL)
return(UX_MEMORY_INSUFFICIENT);
/* Need to allocate memory for the configuration descriptor the first time we read
only the configuration descriptor when we have the configuration descriptor, we have
the length of the entire configuration\interface\endpoint descriptors. */
descriptor = _ux_utility_memory_allocate(UX_SAFE_ALIGN,
UX_CACHE_SAFE_MEMORY, UX_CONFIGURATION_DESCRIPTOR_LENGTH);
if (descriptor == UX_NULL)
{
_ux_utility_memory_free(configuration);
return(UX_MEMORY_INSUFFICIENT);
}
/* Save allocated configuration container. */
device -> ux_device_enum_inst.configuration = configuration;
/* Retrieve the pointer to the control endpoint and its transfer_request. */
control_endpoint = &device -> ux_device_control_endpoint;
transfer_request = &control_endpoint -> ux_endpoint_transfer_request;
/* Create a transfer_request for the GET_DESCRIPTOR request. */
transfer_request -> ux_transfer_request_data_pointer = descriptor;
transfer_request -> ux_transfer_request_requested_length = UX_CONFIGURATION_DESCRIPTOR_LENGTH;
transfer_request -> ux_transfer_request_function = UX_GET_DESCRIPTOR;
transfer_request -> ux_transfer_request_type = UX_REQUEST_IN |
UX_REQUEST_TYPE_STANDARD |
UX_REQUEST_TARGET_DEVICE;
transfer_request -> ux_transfer_request_value = (UINT)device -> ux_device_enum_index |
(UINT)(UX_CONFIGURATION_DESCRIPTOR_ITEM << 8);
transfer_request -> ux_transfer_request_index = 0;
/* Transfer for wait. */
UX_TRANSFER_STATE_RESET(transfer_request);
device -> ux_device_enum_trans = transfer_request;
return(UX_SUCCESS);
}
static inline VOID _ux_host_stack_configuration_parsed(UX_DEVICE *device)
{
UINT status;
/* By default (error) configuration not activated. */
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_FAIL;
/* Query device class driver. */
status = _ux_host_stack_class_device_scan(device);
if (status == UX_SUCCESS)
{
/* Prepare device activation. */
device -> ux_device_enum_index = 0xFF;
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_ACTIVATE;
device -> ux_device_enum_inst.device = device;
}
else if (status == UX_NO_CLASS_MATCH)
{
/* Query interface class driver. */
status = _ux_host_stack_class_interface_scan(device);
if (status == UX_SUCCESS)
{
/* Prepare interfaces activation. */
device -> ux_device_enum_index = 0;
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_CONFIG_SET;
device -> ux_device_enum_inst.configuration =
device -> ux_device_first_configuration;
}
}
}
static inline VOID _ux_host_stack_device_enumerated(UX_DEVICE *device)
{
/* If trace is enabled, register this object. */
UX_TRACE_OBJECT_REGISTER(UX_TRACE_HOST_OBJECT_TYPE_DEVICE,
UX_DEVICE_HCD_GET(device), UX_DEVICE_PARENT_GET(device),
UX_DEVICE_PORT_LOCATION_GET(device), 0);
/* Check if there is unnecessary resource to free. */
if (device -> ux_device_packed_configuration &&
device -> ux_device_packed_configuration_keep_count == 0)
{
_ux_utility_memory_free(device -> ux_device_packed_configuration);
device -> ux_device_packed_configuration = UX_NULL;
}
/* Reset enumeration state. */
device -> ux_device_enum_state = UX_STATE_IDLE;
/* Unlock device EP0. */
device -> ux_device_control_endpoint.ux_endpoint_transfer_request.
ux_transfer_request_flags &= ~UX_TRANSFER_FLAG_LOCK;
/* Disconnect from enumeration list. */
/* Clear enumeration flag to stop enumeration sequence. */
device -> ux_device_flags &= ~UX_DEVICE_FLAG_ENUM;
/* If HCD is dead or device disconnected, free device. */
if (UX_DEVICE_HCD_GET(device) -> ux_hcd_status != UX_HCD_STATUS_OPERATIONAL ||
(device -> ux_device_enum_port_status & UX_PS_CCS) == 0)
{
_ux_host_stack_device_remove(UX_DEVICE_HCD_GET(device),
UX_DEVICE_PARENT_GET(device),
UX_DEVICE_PORT_LOCATION_GET(device));
/* Done without notification. */
return;
}
/* Device connected and enumerated success/fail. */
/* For root hub, set root hub connection status. */
if (UX_DEVICE_PARENT_IS_ROOTHUB(device))
{
UX_DEVICE_HCD_GET(device) -> ux_hcd_rh_device_connection |=
(ULONG)(1 << UX_DEVICE_PORT_LOCATION_GET(device));
}
/* If change function available, notify device connection. */
if (_ux_system_host -> ux_system_host_change_function)
{
_ux_system_host -> ux_system_host_change_function(
UX_DEVICE_CONNECTION, UX_NULL, (VOID*)device);
}
}
static inline UINT _ux_host_stack_interface_activate_wait(UX_DEVICE *device,
UX_HOST_CLASS_COMMAND *command)
{
UINT status;
UX_INTERFACE *interface_inst;
command -> ux_host_class_command_container = device -> ux_device_enum_inst.ptr;
command -> ux_host_class_command_class_ptr = (device -> ux_device_enum_index == 0xFF) ?
device -> ux_device_class :
device -> ux_device_enum_inst.interface -> ux_interface_class;
command -> ux_host_class_command_request = UX_HOST_CLASS_COMMAND_ACTIVATE_WAIT;
/* If there is no class linked, just next state. */
if (command -> ux_host_class_command_class_ptr != UX_NULL)
status = command -> ux_host_class_command_class_ptr -> ux_host_class_entry_function(command);
else
status = UX_STATE_NEXT;
/* No wait command or ready for next state. */
if (status == UX_FUNCTION_NOT_SUPPORTED ||
status == UX_STATE_NEXT)
{
/* Activate for device class driver. */
if (device -> ux_device_enum_index == 0xFF)
{
/* Enumeration done (class driver controls device configuration). */
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_DONE;
return(UX_STATE_NEXT);
}
else
{
/* Activate next interface (default setting). */
interface_inst = device -> ux_device_enum_inst.interface -> ux_interface_next_interface;
while(interface_inst != UX_NULL &&
interface_inst -> ux_interface_descriptor.bAlternateSetting != 0)
{
interface_inst = interface_inst -> ux_interface_next_interface;
}
if (interface_inst != UX_NULL)
{
device -> ux_device_enum_inst.interface = interface_inst;
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_ACTIVATE;
return(UX_STATE_NEXT);
}
/* Enumeration is done. */
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_DONE;
return(UX_STATE_NEXT);
}
}
/* Error cases. */
if (status < UX_STATE_NEXT)
{
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_RETRY;
return(UX_STATE_NEXT);
}
/* Keep waiting. */
return(UX_STATE_WAIT);
}
static inline VOID _ux_host_stack_device_enum_run(UX_DEVICE *device)
{
UX_INTERRUPT_SAVE_AREA
UINT status;
ULONG current_ms, elapsed_ms;
UX_TRANSFER *trans;
UX_CONFIGURATION *configuration;
UX_HOST_CLASS_COMMAND class_command;
UCHAR *buffer;
INT immediate_state = UX_TRUE;
/* Check if the device enumeration should be processed. */
if ((device -> ux_device_flags & UX_DEVICE_FLAG_ENUM) == 0)
return;
/* Enumeration lock check, devices not addressed should wait. */
UX_DISABLE
if (_ux_system_host -> ux_system_host_enum_lock &&
device -> ux_device_address == 0)
{
/* Check if the device is the one locks, others should wait. */
if (device != _ux_system_host -> ux_system_host_enum_lock)
{
/* Wait, return. */
UX_RESTORE
return;
}
}
UX_RESTORE
while (immediate_state)
{
switch (device -> ux_device_enum_state)
{
case UX_STATE_RESET:
/* Reset retry counts. */
device -> ux_device_enum_retry = UX_RH_ENUMERATION_RETRY;
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_PORT_ENABLE;
device -> ux_device_enum_next_state = UX_HOST_STACK_ENUM_PORT_ENABLE;
device -> ux_device_enum_port_status = UX_PS_CCS;
/* Fall through. */
case UX_HOST_STACK_ENUM_PORT_ENABLE:
/* Lock enumeration any way. */
_ux_system_host -> ux_system_host_enum_lock = device;
#if UX_MAX_DEVICES > 1
if (UX_DEVICE_PARENT_IS_HUB(device))
{
/* Issue a port reset on hub side. */
device -> ux_device_flags |= UX_DEVICE_FLAG_RESET;
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_HUB_OPERATION_WAIT;
return;
}
#endif
/* For device connected to roohub, we may need port enable (OHCI). */
status = _ux_host_stack_rh_port_enable(device);
if (status == UX_PORT_INDEX_UNKNOWN)
{
/* No retry, enumeration fail. */
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_FAIL;
continue;
}
/* Wait a while after port connection. */
device -> ux_device_enum_next_state = UX_HOST_STACK_ENUM_PORT_RESET;
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_WAIT;
device -> ux_device_enum_wait_start = _ux_utility_time_get();
device -> ux_device_enum_wait_ms =
UX_MS_TO_TICK_NON_ZERO(UX_RH_ENUMERATION_RETRY_DELAY);
continue;
#if UX_MAX_DEVICES > 1
case UX_HOST_STACK_ENUM_HUB_OPERATION_WAIT:
/* Keep waiting, state is changed in hub tasks. */
return;
#endif
case UX_HOST_STACK_ENUM_PORT_RESET:
/* Reset may blocking, wait the reset done. */
/* Fall through. */
case UX_HOST_STACK_ENUM_PORT_RESET_WAIT:
/* Run states for reset. */
status = _ux_host_stack_rh_port_reset(device);
/* Check fatal error (exit enumeration). */
if (status < UX_STATE_ERROR)
{
/* Fail no retry. */
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_FAIL;
continue;
}
/* Check error. */
if (status == UX_STATE_ERROR)
{
/* Fail retry. */
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_RETRY;
continue;
}
/* Ready for next state. */
if (status == UX_STATE_NEXT)
{
/* Return device address to 0. */
#if UX_MAX_DEVICES > 1
if (device -> ux_device_address)
{
/* Free the address. */
UX_DEVICE_HCD_GET(device) ->
ux_hcd_address[(device -> ux_device_address-1) >> 3] &=
(UCHAR)(1u << ((device -> ux_device_address-1) & 7u));
}
#endif
/* Get port status. */
status = _ux_host_stack_rh_port_status_get(device);
if (status == UX_PORT_INDEX_UNKNOWN)
{
/* Fail no retry: No connection. */
device -> ux_device_enum_port_status = 0;
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_FAIL;
continue;
}
/* Save port status, next : SetAddress. */
device -> ux_device_enum_port_status = (UCHAR)status;
device -> ux_device_enum_trans = &device ->
ux_device_control_endpoint.ux_endpoint_transfer_request;
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_TRANS_LOCK_WAIT;
device -> ux_device_enum_next_state = UX_HOST_STACK_ENUM_DEVICE_ADDR_SET;
continue;
}
/* Keep waiting. */
return;
case UX_HOST_STACK_ENUM_DEVICE_ADDR_SET:
/* Check port connection status. */
if ((device -> ux_device_enum_port_status & UX_PS_CCS) == 0)
{
/* Port disconnected, no retry, just fail. */
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_FAIL;
continue;
}
/* Set device speed. */
device -> ux_device_speed = device -> ux_device_enum_port_status >> UX_PS_DS;
/* Reset bMaxPacketSize0. */
device -> ux_device_descriptor.bMaxPacketSize0 = 0;
/* Start SetAddress(). */
status = _ux_host_stack_device_address_set(device);
if (status != UX_SUCCESS)
{
/* Request fail, retry. */
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_RETRY;
continue;
}
/* Wait request progress done. */
UX_TRANSFER_STATE_RESET(device -> ux_device_enum_trans);
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_TRANS_WAIT;
device -> ux_device_enum_next_state = UX_HOST_STACK_ENUM_DEVICE_ADDR_SENT;
continue;
case UX_HOST_STACK_ENUM_DEVICE_ADDR_SENT:
/* Transfer error check. */
trans = &device -> ux_device_control_endpoint.ux_endpoint_transfer_request;
if (trans -> ux_transfer_request_completion_code != UX_SUCCESS)
{
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_RETRY;
continue;
}
/* Address is already sent to device. */
_ux_host_stack_enum_address_sent(device);
/* Wait at least 2ms after address is sent. */
device -> ux_device_enum_next_state = UX_HOST_STACK_ENUM_DEVICE_DESCR_READ;
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_WAIT;
device -> ux_device_enum_wait_start = _ux_utility_time_get();
device -> ux_device_enum_wait_ms = UX_MS_TO_TICK_NON_ZERO(2);
continue;
case UX_HOST_STACK_ENUM_DEVICE_DESCR_READ:
/* Start GetDescriptor(Device). */
status = _ux_host_stack_device_descriptor_read(device);
if (status != UX_SUCCESS)
{
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_RETRY;
continue;
}
UX_TRANSFER_STATE_RESET(device -> ux_device_enum_trans);
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_TRANS_WAIT;
device -> ux_device_enum_next_state = UX_HOST_STACK_ENUM_DEVICE_DESCR_PARSE;
continue;
case UX_HOST_STACK_ENUM_DEVICE_DESCR_PARSE:
/* Transfer error check. */
trans = &device -> ux_device_control_endpoint.ux_endpoint_transfer_request;
buffer = trans -> ux_transfer_request_data_pointer;
if (UX_HOST_STACK_ENUM_TRANS_ERROR(trans))
{
_ux_utility_memory_free(buffer);
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_RETRY;
continue;
}
/* Device bMaxPacketSize0 not available, update it. */
if(device -> ux_device_descriptor.bMaxPacketSize0 == 0)
{
/* Validate the bMaxPacketSize0. */
if (buffer[7] != 8 && buffer[7] != 16 && buffer[7] != 32 && buffer[7] != 64)
{
_ux_utility_memory_free(buffer);
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_RETRY;
continue;
}
/* Validate the USB-IF bDeviceClass class code. */
#if defined(UX_HOST_DEVICE_CLASS_CODE_VALIDATION_ENABLE)
switch(buffer[4])
{
case 0x00: /* Fall through. */
case 0x02: /* Fall through. */
case 0x09: /* Fall through. */
case 0x11: /* Fall through. */
case 0xDC: /* Fall through. */
case 0xEF: /* Fall through. */
case 0xFF:
break;
default:
/* Invalid device class code. */
_ux_utility_memory_free(buffer);
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_RETRY;
continue;
}
#endif
_ux_host_stack_enum_device_descriptor_parse0(device, buffer);
/* Update SETUP wLength. */
trans -> ux_transfer_request_requested_length = UX_DEVICE_DESCRIPTOR_LENGTH;
/* Issue GetDescriptor(Device) again and parse it. */
device -> ux_device_enum_trans = trans;
UX_TRANSFER_STATE_RESET(trans);
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_TRANS_WAIT;
device -> ux_device_enum_next_state = UX_HOST_STACK_ENUM_DEVICE_DESCR_PARSE;
continue;
}
/* Parse the device descriptor. */
_ux_utility_descriptor_parse(buffer,
_ux_system_device_descriptor_structure, UX_DEVICE_DESCRIPTOR_ENTRIES,
(UCHAR *) &device -> ux_device_descriptor);
_ux_utility_memory_free(buffer);
trans -> ux_transfer_request_data_pointer = UX_NULL;
/* Start configuration enumeration, from index 0. */
device -> ux_device_enum_index = 0;
/* Fall through. */
case UX_HOST_STACK_ENUM_CONFIG_DESCR_READ:
/* Start GetDescriptor(Configuration, 9). */
status = _ux_host_stack_enum_configuration_read(device);
if (status != UX_SUCCESS)
{
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_RETRY;
continue;
}
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_TRANS_WAIT;
device -> ux_device_enum_next_state = UX_HOST_STACK_ENUM_CONFIG_DESCR_PARSE;
/* In this case, there are two memory blocks allocated floating:
- UX_CONFIGURATION
- configuration descriptor RX buffer. */
continue;
case UX_HOST_STACK_ENUM_CONFIG_DESCR_PARSE:
/* Transfer error check. */
trans = &device -> ux_device_control_endpoint.ux_endpoint_transfer_request;
buffer = trans -> ux_transfer_request_data_pointer;
if (UX_HOST_STACK_ENUM_TRANS_ERROR(trans))
{
_ux_utility_memory_free(buffer);
_ux_utility_memory_free(device -> ux_device_enum_inst.ptr);
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_RETRY;
continue;
}
/* Load current parsing configuration. */
configuration = device -> ux_device_enum_inst.configuration;
/* Get configuration descriptor only. */
if (trans -> ux_transfer_request_actual_length == UX_CONFIGURATION_DESCRIPTOR_LENGTH)
{
/* Parse the configuration descriptor and free descriptor buffer. */
_ux_host_stack_enum_configuration_descriptor_parse0(device, configuration, buffer);
/* Release descriptor buffer. */
_ux_utility_memory_free(buffer);
trans -> ux_transfer_request_data_pointer = UX_NULL;
/* If there is no interface for the configuration, check next. */
if (configuration -> ux_configuration_descriptor.wTotalLength ==
UX_CONFIGURATION_DESCRIPTOR_LENGTH)
{
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_CONFIG_DESCR_NEXT;
continue;
}
/* Allocate new buffer for total configuration. */
buffer = _ux_utility_memory_allocate(UX_SAFE_ALIGN,
UX_CACHE_SAFE_MEMORY,
configuration -> ux_configuration_descriptor.wTotalLength);
if (buffer == UX_NULL)
{
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_RETRY;
continue;
}
trans -> ux_transfer_request_data_pointer = buffer;
/* Modify transfer length for total configuration */
trans -> ux_transfer_request_requested_length =
configuration -> ux_configuration_descriptor.wTotalLength;
/* Start transfer again. */
device -> ux_device_enum_trans = trans;
UX_TRANSFER_STATE_RESET(device -> ux_device_enum_trans);
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_TRANS_WAIT;
device -> ux_device_enum_next_state = UX_HOST_STACK_ENUM_CONFIG_DESCR_PARSE;
/* In this case, there is one memory blocks allocated floating:
- configuration descriptor RX buffer. */
continue;
}
/* Parse configuration and it's interfaces. */
status = _ux_host_stack_interfaces_scan(configuration, buffer);
/* Release descriptor memory. */
_ux_utility_memory_free(buffer);
trans -> ux_transfer_request_data_pointer = UX_NULL;
/* Check operation status. */
if (status != UX_SUCCESS)
{
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_RETRY;
continue;
}
/* Enumerate next configuration. */
/* Fall through. */
case UX_HOST_STACK_ENUM_CONFIG_DESCR_NEXT:
device -> ux_device_enum_index ++;
if (device -> ux_device_enum_index <
device -> ux_device_descriptor.bNumConfigurations)
{
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_CONFIG_DESCR_READ;
continue;
}
/* All configurations are enumerated. */
_ux_host_stack_configuration_parsed(device);
/* Roll back to next state. */
continue;
case UX_HOST_STACK_ENUM_CONFIG_SET:
/* The device is now in the unconfigured state. We need to deal
with the amount of power the device is consuming before allowing
it to be configured. Otherwise we may run the risk of an over
current fault. */
configuration = device -> ux_device_enum_inst.configuration;
if (((configuration -> ux_configuration_descriptor.bmAttributes &
UX_CONFIGURATION_DEVICE_SELF_POWERED) == 0) &&
(configuration -> ux_configuration_descriptor.MaxPower >
UX_DEVICE_MAX_POWER_GET(device)))
{
/* Error trap. */
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_ENUMERATOR, UX_OVER_CURRENT_CONDITION);
/* If trace is enabled, insert this event into the trace buffer. */
UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_OVER_CURRENT_CONDITION, configuration, 0, 0, UX_TRACE_ERRORS, 0, 0)
/* Enumeration fail without retry. */
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_FAIL;
continue;
}
/* Prepare transfer request SetConfiguration(). */
_ux_host_stack_configuration_set(configuration);
/* Roll back to start transfer wait. */
UX_TRANSFER_STATE_RESET(device -> ux_device_enum_trans);
device -> ux_device_enum_next_state = UX_HOST_STACK_ENUM_CONFIG_ACTIVATE;
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_TRANS_WAIT;
continue;
case UX_HOST_STACK_ENUM_CONFIG_ACTIVATE:
trans = &device -> ux_device_control_endpoint.ux_endpoint_transfer_request;
if (UX_HOST_STACK_ENUM_TRANS_ERROR(trans))
{
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_RETRY;
continue;
}
/* Set device state to configured. */
configuration = device -> ux_device_enum_inst.configuration;
device -> ux_device_state = UX_DEVICE_CONFIGURED;
device -> ux_device_current_configuration = configuration;
/* Create the configuration instance. */
status = _ux_host_stack_configuration_instance_create(configuration);
if (status != UX_SUCCESS)
{
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_RETRY;
continue;
}
if (configuration -> ux_configuration_first_interface)
{
/* Activate classes. */
device -> ux_device_enum_index = 0;
device -> ux_device_enum_inst.interface =
configuration -> ux_configuration_first_interface;
}
else
{
/* No activation, done */
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_DONE;
continue;
}
/* Fall through. */
case UX_HOST_STACK_ENUM_ACTIVATE:
/* Class activate. */
class_command.ux_host_class_command_request = UX_HOST_CLASS_COMMAND_ACTIVATE_START;
if (device -> ux_device_enum_index == 0xFF)
{
/* Device class driver. */
class_command.ux_host_class_command_container = device;
class_command.ux_host_class_command_class_ptr = device -> ux_device_class;
}
else
{
/* Interface class driver. */
class_command.ux_host_class_command_container = device -> ux_device_enum_inst.ptr;
class_command.ux_host_class_command_class_ptr =
device -> ux_device_enum_inst.interface -> ux_interface_class;
}
/* If there class linked, start activation. */
if (class_command.ux_host_class_command_class_ptr != UX_NULL)
{
status = class_command.ux_host_class_command_class_ptr ->
ux_host_class_entry_function(&class_command);
if (status != UX_SUCCESS)
{
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_RETRY;
continue;
}
}
/* Class activate execute wait. */
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_ACTIVATE_WAIT;
/* Fall through. */
case UX_HOST_STACK_ENUM_ACTIVATE_WAIT:
/* Class activate execution wait. */
status = _ux_host_stack_interface_activate_wait(device, &class_command);
/* Next state. */
if (status != UX_STATE_WAIT)
continue;
/* Keep waiting. */
return;
case UX_HOST_STACK_ENUM_TRANS_WAIT:
/* Poll transfer task. */
trans = device -> ux_device_enum_trans;
status = _ux_host_stack_transfer_run(trans);
/* Transfer done - next state. */
if (status == UX_STATE_NEXT || status == UX_STATE_IDLE)
{
device -> ux_device_enum_trans = UX_NULL;
device -> ux_device_enum_state = device -> ux_device_enum_next_state;
continue;
}
/* Check error. */
if (status < UX_STATE_ERROR)
{
/* No retry, fail. */
if (trans -> ux_transfer_request_data_pointer)
{
_ux_utility_memory_free(trans -> ux_transfer_request_data_pointer);
trans -> ux_transfer_request_data_pointer = UX_NULL;
}
if (device -> ux_device_enum_next_state == UX_HOST_STACK_ENUM_CONFIG_DESCR_PARSE)
{
_ux_utility_memory_free(device -> ux_device_enum_inst.ptr);
device -> ux_device_enum_inst.ptr = UX_NULL;
}
device -> ux_device_enum_trans = UX_NULL;
device -> ux_device_enum_state = UX_HOST_STACK_ENUM_FAIL;
continue;
}
if (status < UX_STATE_NEXT)
{
/* Error, retry. */
if (trans -> ux_transfer_request_data_pointer)
{
_ux_utility_memory_free(trans -> ux_transfer_request_data_pointer);
trans -> ux_transfer_request_data_pointer = UX_NULL;
}
if (device -> ux_device_enum_next_state == UX_HOST_STACK_ENUM_CONFIG_DESCR_PARSE)