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 /**   HID Class                                                           */
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_class_hid.h"
29 #include "ux_host_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_class_hid_report_callback_register         PORTABLE C      */
37 /*                                                           6.1.10       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function will register a report callback to a HID report.      */
45 /*    This function should be called by a HID client when it has been     */
46 /*    instantiated by a new HID device.                                   */
47 /*                                                                        */
48 /*    The registration process will allow the HID class to call a         */
49 /*    function in the HID client when an asynchronous report is present.  */
50 /*                                                                        */
51 /*  INPUT                                                                 */
52 /*                                                                        */
53 /*    hid                                   Pointer to HID class          */
54 /*    call_back                             HID report callback           */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    Completion Status                                                   */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    _ux_host_stack_class_instance_verify  Verify class instance is valid*/
63 /*    _ux_host_semaphore_get                Get protection semaphore      */
64 /*    _ux_host_semaphore_put                Release protection semaphore  */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    Application                                                         */
69 /*    HID Class                                                           */
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 /*                                            resulting in version 6.1    */
78 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            added standalone support,   */
80 /*                                            resulting in version 6.1.10 */
81 /*                                                                        */
82 /**************************************************************************/
_ux_host_class_hid_report_callback_register(UX_HOST_CLASS_HID * hid,UX_HOST_CLASS_HID_REPORT_CALLBACK * call_back)83 UINT  _ux_host_class_hid_report_callback_register(UX_HOST_CLASS_HID *hid, UX_HOST_CLASS_HID_REPORT_CALLBACK *call_back)
84 {
85 #if defined(UX_HOST_STANDALONE)
86 UX_INTERRUPT_SAVE_AREA
87 #endif
88 UINT                            status;
89 UX_HOST_CLASS_HID_REPORT         *hid_report;
90 
91 
92     /* Ensure the instance is valid.  */
93     if (_ux_host_stack_class_instance_verify(_ux_system_host_class_hid_name, (VOID *) hid) != UX_SUCCESS)
94     {
95 
96         /* Error trap. */
97         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_INSTANCE_UNKNOWN);
98 
99         /* If trace is enabled, insert this event into the trace buffer.  */
100         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
101 
102         return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
103     }
104 
105     /* Protect thread reentry to this instance.  */
106     _ux_host_class_hid_lock_fail_return(hid);
107 
108     /* Search for the report ID. Note that this can only be an Input report!  */
109     hid_report =  hid -> ux_host_class_hid_parser.ux_host_class_hid_parser_input_report;
110 
111     /* Parse all the report IDs in search of the one specified by the user.  */
112     while (hid_report != UX_NULL)
113     {
114 
115         /* Check report ID.  */
116         if (hid_report -> ux_host_class_hid_report_id == call_back -> ux_host_class_hid_report_callback_id)
117         {
118 
119             /* We have found the correct report. Set the call back function, buffer and flags.  */
120             hid_report -> ux_host_class_hid_report_callback_function =   call_back -> ux_host_class_hid_report_callback_function;
121             hid_report -> ux_host_class_hid_report_callback_buffer   =   call_back -> ux_host_class_hid_report_callback_buffer;
122             hid_report -> ux_host_class_hid_report_callback_flags    =   call_back -> ux_host_class_hid_report_callback_flags;
123             hid_report -> ux_host_class_hid_report_callback_length   =   call_back -> ux_host_class_hid_report_callback_length;
124 
125             /* Unprotect thread reentry to this instance.  */
126             _ux_host_class_hid_unlock(hid);
127 
128             /* Tell the user the report was OK and the call back was registered.  */
129             return(UX_SUCCESS);
130         }
131 
132         /* Jump to next report.  */
133         hid_report = hid_report -> ux_host_class_hid_report_next_report;
134     }
135 
136     /* Unprotect thread reentry to this instance.  */
137     _ux_host_class_hid_unlock(hid);
138 
139     /* Error trap. */
140     _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_HID_REPORT_ERROR);
141 
142     /* If trace is enabled, insert this event into the trace buffer.  */
143     UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_HID_REPORT_ERROR, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
144 
145     /* The report ID could not be found amongst the reports.  */
146     return(UX_HOST_CLASS_HID_REPORT_ERROR);
147 }
148 
149 
150 /**************************************************************************/
151 /*                                                                        */
152 /*  FUNCTION                                               RELEASE        */
153 /*                                                                        */
154 /*    _uxe_host_class_hid_report_callback_register        PORTABLE C      */
155 /*                                                           6.3.0        */
156 /*  AUTHOR                                                                */
157 /*                                                                        */
158 /*    Chaoqiong Xiao, Microsoft Corporation                               */
159 /*                                                                        */
160 /*  DESCRIPTION                                                           */
161 /*                                                                        */
162 /*    This function checks errors in HID report callback register function*/
163 /*    call.                                                               */
164 /*                                                                        */
165 /*  INPUT                                                                 */
166 /*                                                                        */
167 /*    hid                                   Pointer to HID class          */
168 /*    call_back                             HID report callback           */
169 /*                                                                        */
170 /*  OUTPUT                                                                */
171 /*                                                                        */
172 /*    Status                                                              */
173 /*                                                                        */
174 /*  CALLS                                                                 */
175 /*                                                                        */
176 /*    _ux_host_class_hid_report_callback_register                         */
177 /*                                          Register a report callback    */
178 /*                                                                        */
179 /*  CALLED BY                                                             */
180 /*                                                                        */
181 /*    Application                                                         */
182 /*                                                                        */
183 /*  RELEASE HISTORY                                                       */
184 /*                                                                        */
185 /*    DATE              NAME                      DESCRIPTION             */
186 /*                                                                        */
187 /*  10-31-2023     Chaoqiong Xiao           Initial Version 6.3.0         */
188 /*                                                                        */
189 /**************************************************************************/
_uxe_host_class_hid_report_callback_register(UX_HOST_CLASS_HID * hid,UX_HOST_CLASS_HID_REPORT_CALLBACK * call_back)190 UINT  _uxe_host_class_hid_report_callback_register(UX_HOST_CLASS_HID *hid, UX_HOST_CLASS_HID_REPORT_CALLBACK *call_back)
191 {
192 
193     /* Sanity check.  */
194     if ((hid == UX_NULL) || (call_back == UX_NULL))
195         return(UX_INVALID_PARAMETER);
196 
197     /* Invoke periodic start function.  */
198     return(_ux_host_class_hid_report_callback_register(hid, call_back));
199 }
200