1 /***************************************************************************
2 * Copyright (c) 2024 Microsoft Corporation
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the MIT License which is available at
6 * https://opensource.org/licenses/MIT.
7 *
8 * SPDX-License-Identifier: MIT
9 **************************************************************************/
10
11
12 /**************************************************************************/
13 /**************************************************************************/
14 /** */
15 /** USBX Component */
16 /** */
17 /** Host Stack */
18 /** */
19 /**************************************************************************/
20 /**************************************************************************/
21
22
23 /* Include necessary system files. */
24
25 #define UX_SOURCE_CODE
26
27 #include "ux_api.h"
28 #include "ux_host_stack.h"
29
30
31 /**************************************************************************/
32 /* */
33 /* FUNCTION RELEASE */
34 /* */
35 /* _ux_host_stack_device_configuration_select PORTABLE C */
36 /* 6.1.10 */
37 /* AUTHOR */
38 /* */
39 /* Chaoqiong Xiao, Microsoft Corporation */
40 /* */
41 /* DESCRIPTION */
42 /* */
43 /* This function selects a specific configuration for a device. */
44 /* When this configuration is set to the device, by default all the */
45 /* device interface and their associated alternate setting 0 is */
46 /* activated on the device. If the device/interface class driver */
47 /* wishes to change the setting of a particular interface, it needs */
48 /* to issue a select interface setting function. */
49 /* */
50 /* INPUT */
51 /* */
52 /* configuration Pointer to configuration */
53 /* */
54 /* OUTPUT */
55 /* */
56 /* Completion Status */
57 /* */
58 /* CALLS */
59 /* */
60 /* _ux_host_stack_configuration_instance_create Create configuration */
61 /* instance */
62 /* _ux_host_stack_configuration_instance_delete Delete configuration */
63 /* instance */
64 /* _ux_host_stack_configuration_set Set configuration */
65 /* */
66 /* CALLED BY */
67 /* */
68 /* Application */
69 /* USBX Components */
70 /* */
71 /* RELEASE HISTORY */
72 /* */
73 /* DATE NAME DESCRIPTION */
74 /* */
75 /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */
76 /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */
77 /* optimized based on compile */
78 /* definitions, */
79 /* resulting in version 6.1 */
80 /* 02-02-2021 Chaoqiong Xiao Modified comment(s), */
81 /* used pointer for current */
82 /* selected configuration, */
83 /* resulting in version 6.1.4 */
84 /* 01-31-2022 Chaoqiong Xiao Modified comment(s), */
85 /* refine power usage check, */
86 /* resulting in version 6.1.10 */
87 /* */
88 /**************************************************************************/
_ux_host_stack_device_configuration_select(UX_CONFIGURATION * configuration)89 UINT _ux_host_stack_device_configuration_select(UX_CONFIGURATION *configuration)
90 {
91
92 UX_DEVICE *device;
93 UX_CONFIGURATION *current_configuration;
94 UINT status;
95
96 /* Check for validity of the configuration handle. */
97 if (configuration -> ux_configuration_handle != (ULONG) (ALIGN_TYPE) configuration)
98 {
99
100 /* Error trap. */
101 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_ENUMERATOR, UX_CONFIGURATION_HANDLE_UNKNOWN);
102
103 /* If trace is enabled, insert this event into the trace buffer. */
104 UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONFIGURATION_HANDLE_UNKNOWN, configuration, 0, 0, UX_TRACE_ERRORS, 0, 0)
105
106 return(UX_CONFIGURATION_HANDLE_UNKNOWN);
107 }
108
109 /* Get the device container for this configuration. */
110 device = configuration -> ux_configuration_device;
111
112 /* If trace is enabled, insert this event into the trace buffer. */
113 UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_DEVICE_CONFIGURATION_SELECT, device, configuration, 0, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0)
114
115 /* We need to check the amount of power the bus powered device is consuming
116 before switch configuration. Otherwise we may run the risk of
117 an over current fault. */
118 if (((configuration -> ux_configuration_descriptor.bmAttributes & UX_CONFIGURATION_DEVICE_SELF_POWERED) == 0) &&
119 (configuration -> ux_configuration_descriptor.MaxPower > UX_DEVICE_MAX_POWER_GET(device)))
120 {
121
122 /* Error trap. */
123 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_ENUMERATOR, UX_OVER_CURRENT_CONDITION);
124
125 /* If trace is enabled, insert this event into the trace buffer. */
126 UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_OVER_CURRENT_CONDITION, configuration, 0, 0, UX_TRACE_ERRORS, 0, 0)
127
128 return(UX_OVER_CURRENT_CONDITION);
129 }
130
131 /* Check for the state of the device . If the device is already configured,
132 we need to cancel the existing configuration before enabling this one. */
133 if (device -> ux_device_state == UX_DEVICE_CONFIGURED)
134 {
135
136 /* The device is configured. Get the first configuration pointer. */
137 current_configuration = device -> ux_device_current_configuration;
138
139 /* Deselect this instance */
140 _ux_host_stack_configuration_instance_delete(current_configuration);
141 }
142
143 /* The device can now be configured. */
144 status = _ux_host_stack_configuration_set(configuration);
145 if (status != UX_SUCCESS)
146 return(status);
147
148 /* Create the configuration instance. */
149 status = _ux_host_stack_configuration_instance_create(configuration);
150
151 /* Return completion status. */
152 return(status);
153 }
154
155