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_activate                         PORTABLE C      */
38 /*                                                           6.1.12       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function performs the enumeration of the HID class.            */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    command                               Pointer to command            */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    Completion Status                                                   */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    _ux_host_class_hid_client_search      HID client search             */
58 /*    _ux_host_class_hid_configure          Configure HID                 */
59 /*    _ux_host_class_hid_descriptor_parse   Parse descriptor              */
60 /*    _ux_host_class_hid_interrupt_endpoint_search  Search endpoint       */
61 /*    _ux_host_class_hid_instance_clean     Clean up instance resources   */
62 /*    _ux_host_stack_class_instance_create  Create class instance         */
63 /*    _ux_host_stack_class_instance_destroy Destroy class instance        */
64 /*    _ux_utility_memory_allocate           Allocate memory block         */
65 /*    _ux_utility_memory_free               Free memory                   */
66 /*    _ux_host_semaphore_create             Create semaphore              */
67 /*                                                                        */
68 /*  CALLED BY                                                             */
69 /*                                                                        */
70 /*    HID Class                                                           */
71 /*                                                                        */
72 /*  RELEASE HISTORY                                                       */
73 /*                                                                        */
74 /*    DATE              NAME                      DESCRIPTION             */
75 /*                                                                        */
76 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
77 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
78 /*                                            resulting in version 6.1    */
79 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
80 /*                                            added standalone support,   */
81 /*                                            resulting in version 6.1.10 */
82 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
83 /*                                            fixed parameter/variable    */
84 /*                                            names conflict C++ keyword, */
85 /*                                            resulting in version 6.1.12 */
86 /*                                                                        */
87 /**************************************************************************/
_ux_host_class_hid_activate(UX_HOST_CLASS_COMMAND * command)88 UINT  _ux_host_class_hid_activate(UX_HOST_CLASS_COMMAND  *command)
89 {
90 
91 UX_INTERFACE        *interface_ptr;
92 UX_HOST_CLASS_HID   *hid;
93 UINT                status;
94 
95 
96     /* The HID is always activated by the interface descriptor and not the
97        device descriptor.  */
98     interface_ptr =  (UX_INTERFACE *) command -> ux_host_class_command_container;
99 
100     /* Instantiate this HID class */
101     hid =  _ux_utility_memory_allocate(UX_NO_ALIGN,  UX_REGULAR_MEMORY,sizeof(UX_HOST_CLASS_HID));
102     if (hid == UX_NULL)
103         return(UX_MEMORY_INSUFFICIENT);
104 
105     /* Store the class container into this instance.  */
106     hid -> ux_host_class_hid_class =  command -> ux_host_class_command_class_ptr;
107 
108     /* Store the interface container into the HID class instance.  */
109     hid -> ux_host_class_hid_interface =  interface_ptr;
110 
111     /* Store the device container into the HID class instance.  */
112     hid -> ux_host_class_hid_device =  interface_ptr -> ux_interface_configuration -> ux_configuration_device;
113 
114     /* This instance of the device must also be stored in the interface container.  */
115     interface_ptr -> ux_interface_class_instance =  (VOID *) hid;
116 
117     /* Create this class instance.  */
118     _ux_host_stack_class_instance_create(command -> ux_host_class_command_class_ptr, (VOID *) hid);
119 
120 #if defined(UX_HOST_STANDALONE)
121 
122     /* Set class tasks function.  */
123     hid -> ux_host_class_hid_class -> ux_host_class_task_function = _ux_host_class_hid_tasks_run;
124 
125     /* Set activate state to first step.  */
126     hid -> ux_host_class_hid_enum_state = UX_STATE_WAIT;
127 
128     status = UX_SUCCESS;
129     return(status);
130 #else
131 
132     /* Configure the HID.  */
133     status =  _ux_host_class_hid_configure(hid);
134 
135     /* If configure is done success, goes on. */
136     if (status == UX_SUCCESS)
137     {
138 
139         /* Get the HID descriptor and parse it.  */
140         status =  _ux_host_class_hid_descriptor_parse(hid);
141     }
142 
143     /* If HID descriptor parse is done success, goes on. */
144     if (status == UX_SUCCESS)
145     {
146 
147         /* Search the HID interrupt endpoint but do not start it.  */
148         status =  _ux_host_class_hid_interrupt_endpoint_search(hid);
149     }
150 
151     /* If HID interrupt endpoint is found, goes on. */
152     if (status == UX_SUCCESS)
153     {
154 
155         /* Create the semaphore to protect multiple threads from accessing the same
156         storage instance.  */
157         status =  _ux_host_semaphore_create(&hid -> ux_host_class_hid_semaphore, "ux_host_class_hid_semaphore", 1);
158 
159         if (status == UX_SUCCESS)
160         {
161 
162             /* If all is fine, try to locate the HID client for this HID device.  */
163             _ux_host_class_hid_client_search(hid);
164 
165             /* Mark the HID class as live now.  */
166             hid -> ux_host_class_hid_state =  UX_HOST_CLASS_INSTANCE_LIVE;
167 
168             /* We may need to inform the application if a function has been programmed in the system structure.  */
169             if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
170             {
171 
172                 /* Call system change function.  */
173                 _ux_system_host ->  ux_system_host_change_function(UX_DEVICE_INSERTION, hid -> ux_host_class_hid_class, (VOID *) hid);
174             }
175 
176             /* If trace is enabled, insert this event into the trace buffer.  */
177             UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_HID_ACTIVATE, hid, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
178 
179             /* If trace is enabled, register this object.  */
180             UX_TRACE_OBJECT_REGISTER(UX_TRACE_HOST_OBJECT_TYPE_INTERFACE, hid, 0, 0, 0)
181 
182             /* Return completion code.  */
183             return(status);
184         }
185         else
186         {
187             status = UX_SEMAPHORE_ERROR;
188         }
189     }
190 
191     /* Clean interrupt endpoint.  */
192     if (hid -> ux_host_class_hid_interrupt_endpoint &&
193         hid -> ux_host_class_hid_interrupt_endpoint -> ux_endpoint_transfer_request.ux_transfer_request_data_pointer)
194         _ux_utility_memory_free(hid -> ux_host_class_hid_interrupt_endpoint -> ux_endpoint_transfer_request.ux_transfer_request_data_pointer);
195 
196     /* Clean instance. */
197     _ux_host_class_hid_instance_clean(hid);
198 
199     /* Error, destroy the class instance and return error code. */
200     _ux_host_stack_class_instance_destroy(hid -> ux_host_class_hid_class, (VOID *) hid);
201 
202     /* Unmount instance. */
203     interface_ptr -> ux_interface_class_instance = UX_NULL;
204 
205     /* Free instance. */
206     _ux_utility_memory_free(hid);
207 
208     /* Return error code. */
209     return(status);
210 
211 #endif
212 }
213 
214