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 /** USBX Component                                                        */
15 /**                                                                       */
16 /**   Device HID Class                                                    */
17 /**                                                                       */
18 /**************************************************************************/
19 /**************************************************************************/
20 
21 #define UX_SOURCE_CODE
22 
23 
24 /* Include necessary system files.  */
25 
26 #include "ux_api.h"
27 #include "ux_device_class_hid.h"
28 #include "ux_device_stack.h"
29 
30 
31 #if !defined(UX_DEVICE_STANDALONE)
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_device_class_hid_interrupt_thread               PORTABLE C      */
37 /*                                                           6.3.0        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function is the thread of the hid interrupt (IN) endpoint      */
45 /*                                                                        */
46 /*    It's for RTOS mode.                                                 */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    hid_class                                 Address of hid class      */
51 /*                                                container               */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    None                                                                */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_utility_event_flags_get           Get event flags               */
60 /*    _ux_device_class_hid_event_get        Get HID event                 */
61 /*    _ux_device_stack_transfer_request     Request transfer              */
62 /*    _ux_utility_memory_copy               Copy memory                   */
63 /*    _ux_device_thread_suspend             Suspend thread                */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    ThreadX                                                             */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
74 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
75 /*                                            verified memset and memcpy  */
76 /*                                            cases, used UX prefix to    */
77 /*                                            refer to TX symbols instead */
78 /*                                            of using them directly,     */
79 /*                                            resulting in version 6.1    */
80 /*  10-15-2021     Chaoqiong Xiao           Modified comment(s),          */
81 /*                                            improved idle generation,   */
82 /*                                            resulting in version 6.1.9  */
83 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
84 /*                                            off for standalone compile, */
85 /*                                            resulting in version 6.1.10 */
86 /*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
87 /*                                            added zero copy support,    */
88 /*                                            resulting in version 6.3.0  */
89 /*                                                                        */
90 /**************************************************************************/
_ux_device_class_hid_interrupt_thread(ULONG hid_class)91 VOID  _ux_device_class_hid_interrupt_thread(ULONG hid_class)
92 {
93 
94 UX_SLAVE_CLASS              *class_ptr;
95 UX_SLAVE_CLASS_HID          *hid;
96 UX_SLAVE_DEVICE             *device;
97 UX_SLAVE_TRANSFER           *transfer_request_in;
98 UX_DEVICE_CLASS_HID_EVENT   *hid_event;
99 UINT                        status;
100 UCHAR                       *buffer;
101 ULONG                       actual_flags;
102 
103 
104     /* Cast properly the hid instance.  */
105     UX_THREAD_EXTENSION_PTR_GET(class_ptr, UX_SLAVE_CLASS, hid_class)
106 
107     /* Get the hid instance from this class container.  */
108     hid =  (UX_SLAVE_CLASS_HID *) class_ptr -> ux_slave_class_instance;
109 
110     /* Get the pointer to the device.  */
111     device =  &_ux_system_slave -> ux_system_slave_device;
112 
113     /* This thread runs forever but can be suspended or resumed.  */
114     while(1)
115     {
116 
117         /* All HID events are on the interrupt endpoint IN, from the host.  */
118         transfer_request_in =  &hid -> ux_device_class_hid_interrupt_endpoint -> ux_slave_endpoint_transfer_request;
119 
120         /* As long as the device is in the CONFIGURED state.  */
121         while (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED)
122         {
123 
124             /* Wait until we have a event sent by the application
125                or a change in the idle state to send last or empty report.  */
126             status =  _ux_utility_event_flags_get(&hid -> ux_device_class_hid_event_flags_group,
127                                                     UX_DEVICE_CLASS_HID_EVENTS_MASK, UX_OR_CLEAR, &actual_flags,
128                                                     hid -> ux_device_class_hid_event_wait_timeout);
129 
130             /* If there is no event, check if we have timeout defined.  */
131             if (status == UX_NO_EVENTS)
132             {
133 
134                 /* There is no event exists on timeout, insert last.  */
135 
136                 /* Check if no request been ready.  */
137                 if (transfer_request_in -> ux_slave_transfer_request_requested_length == 0)
138                 {
139 
140                     /* Assume the request use whole interrupt transfer payload.  */
141                     transfer_request_in -> ux_slave_transfer_request_requested_length =
142                             transfer_request_in -> ux_slave_transfer_request_transfer_length;
143 
144 #if (UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1) && defined(UX_DEVICE_CLASS_HID_ZERO_COPY)
145 
146                     /* Restore endpoint buffer (never touched, filled with zeros).  */
147                     transfer_request_in -> ux_slave_transfer_request_data_pointer =
148                                     UX_DEVICE_CLASS_HID_INTERRUPTIN_BUFFER(hid);
149 #else
150 
151                     /* Set the data to zeros.  */
152                     _ux_utility_memory_set(
153                         transfer_request_in -> ux_slave_transfer_request_data_pointer, 0,
154                         transfer_request_in -> ux_slave_transfer_request_requested_length); /* Use case of memset is verified. */
155 #endif
156                 }
157 
158                 /* Send the request to the device controller.  */
159                 status =  _ux_device_stack_transfer_request(transfer_request_in,
160                                 transfer_request_in -> ux_slave_transfer_request_requested_length,
161                                 transfer_request_in -> ux_slave_transfer_request_requested_length);
162 
163                 /* Check error code. We don't want to invoke the error callback
164                    if the device was disconnected, since that's expected.  */
165                 if (status != UX_SUCCESS && status != UX_TRANSFER_BUS_RESET)
166 
167                     /* Error trap. */
168                     _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, status);
169 
170                 /* Next: check events.  */
171                 continue;
172             }
173 
174             /* Check the completion code. */
175             if (status != UX_SUCCESS)
176             {
177 
178                 /* Error trap. */
179                 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, status);
180 
181                 /* Do not proceed.  */
182                 return;
183             }
184 
185 
186             /* Check if we have an event to report.  */
187             while (_ux_device_class_hid_event_check(hid, &hid_event) == UX_SUCCESS)
188             {
189 
190 #if (UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1) && defined(UX_DEVICE_CLASS_HID_ZERO_COPY)
191 
192                 /* Directly use the event buffer for transfer.  */
193                 buffer = hid_event -> ux_device_class_hid_event_buffer;
194                 transfer_request_in -> ux_slave_transfer_request_data_pointer = buffer;
195 #else
196                 /* Prepare the event data payload from the hid event structure.  Get a pointer to the buffer area.  */
197                 buffer =  transfer_request_in -> ux_slave_transfer_request_data_pointer;
198 
199                 /* Copy the event buffer into the target buffer.  */
200                 _ux_utility_memory_copy(buffer, UX_DEVICE_CLASS_HID_EVENT_BUFFER(hid_event), hid_event -> ux_device_class_hid_event_length); /* Use case of memcpy is verified. */
201 #endif
202 
203                 /* Send the request to the device controller.  */
204                 status =  _ux_device_stack_transfer_request(transfer_request_in, hid_event -> ux_device_class_hid_event_length,
205                                                             hid_event -> ux_device_class_hid_event_length);
206 
207                 /* The queue tail is handled and should be freed.  */
208                 _ux_device_class_hid_event_free(hid);
209 
210                 /* Check error code. We don't want to invoke the error callback
211                    if the device was disconnected, since that's expected.  */
212                 if (status != UX_SUCCESS && status != UX_TRANSFER_BUS_RESET)
213 
214                     /* Error trap. */
215                     _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, status);
216             }
217         }
218 
219         /* We need to suspend ourselves. We will be resumed by the device enumeration module.  */
220         _ux_device_thread_suspend(&class_ptr -> ux_slave_class_thread);
221     }
222 }
223 #endif
224