1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** USBX Component                                                        */
17 /**                                                                       */
18 /**   HID Class                                                           */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 /* Include necessary system files.  */
25 
26 #define UX_SOURCE_CODE
27 
28 #include "ux_api.h"
29 #include "ux_host_class_hid.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_hid_configure                        PORTABLE C      */
38 /*                                                           6.1.11       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function calls the USBX stack to do a SET_CONFIGURATION to the */
46 /*    HID. Once the HID is configured, its interface will be activated    */
47 /*    and all the endpoints enumerated (1 interrupt endpoint in the case  */
48 /*    of the HID).                                                        */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    hid                                   Pointer to HID class          */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    Completion Status                                                   */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _ux_host_stack_configuration_interface_get Get interface            */
61 /*    _ux_host_stack_device_configuration_get    Get configuration        */
62 /*    _ux_host_stack_device_configuration_select Select configuration     */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    HID Class                                                           */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
73 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            optimized based on compile  */
75 /*                                            definitions,                */
76 /*                                            resulting in version 6.1    */
77 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
78 /*                                            internal clean up,          */
79 /*                                            resulting in version 6.1.11 */
80 /*                                                                        */
81 /**************************************************************************/
_ux_host_class_hid_configure(UX_HOST_CLASS_HID * hid)82 UINT  _ux_host_class_hid_configure(UX_HOST_CLASS_HID *hid)
83 {
84 
85 UINT                    status;
86 UX_CONFIGURATION        *configuration;
87 #if UX_MAX_DEVICES > 1
88 UX_DEVICE               *device;
89 UX_DEVICE               *parent_device;
90 #endif
91 
92 
93     /* If the device has been configured already, we don't need to do it again. */
94     if (hid -> ux_host_class_hid_device -> ux_device_state == UX_DEVICE_CONFIGURED)
95         return(UX_SUCCESS);
96 
97     /* A HID normally has one configuration. So retrieve the 1st configuration only.  */
98     status =  _ux_host_stack_device_configuration_get(hid -> ux_host_class_hid_device, 0, &configuration);
99     if (status != UX_SUCCESS)
100     {
101 
102         /* Error trap. */
103         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONFIGURATION_HANDLE_UNKNOWN);
104 
105         /* If trace is enabled, insert this event into the trace buffer.  */
106         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONFIGURATION_HANDLE_UNKNOWN, hid -> ux_host_class_hid_device, 0, 0, UX_TRACE_ERRORS, 0, 0)
107 
108         return(UX_CONFIGURATION_HANDLE_UNKNOWN);
109     }
110 
111 #if UX_MAX_DEVICES > 1
112     /* Get the device container for this configuration.  */
113     device =  configuration -> ux_configuration_device;
114 
115     /* Get the parent container for this device.  */
116     parent_device =  device -> ux_device_parent;
117 
118     /* Check the HID power source and check the parent power source for
119        incompatible connections.  */
120     if (hid -> ux_host_class_hid_device -> ux_device_power_source == UX_DEVICE_BUS_POWERED)
121     {
122 
123         /* If the device is NULL, the parent is the root hid and we don't have to worry.
124            If the parent is not the root HID, check for its power source.  */
125         if ((parent_device != UX_NULL) && (parent_device -> ux_device_power_source == UX_DEVICE_BUS_POWERED))
126         {
127 
128             /* Error trap. */
129             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONNECTION_INCOMPATIBLE);
130 
131             /* If trace is enabled, insert this event into the trace buffer.  */
132             UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONNECTION_INCOMPATIBLE, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
133 
134             return(UX_CONNECTION_INCOMPATIBLE);
135         }
136     }
137 #endif
138 
139     /* We have the valid configuration. Ask the USBX stack to set this configuration.  */
140     _ux_host_stack_device_configuration_select(configuration);
141 
142     /* If the operation went well, the hid default alternate setting for the HID interface is active
143        and the interrupt endpoint is now enabled. We have to memorize the first interface since the
144        interrupt endpoint is hooked to it. */
145     status =  _ux_host_stack_configuration_interface_get(configuration, 0, 0, &hid -> ux_host_class_hid_interface);
146 
147     /* Return completion status.  */
148     return(status);
149 }
150 
151