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 /** USBX Component                                                        */
14 /**                                                                       */
15 /**   Device HID Class                                                    */
16 /**                                                                       */
17 /**************************************************************************/
18 /**************************************************************************/
19 
20 #define UX_SOURCE_CODE
21 
22 
23 /* Include necessary system files.  */
24 
25 #include "ux_api.h"
26 #include "ux_device_class_hid.h"
27 #include "ux_device_stack.h"
28 
29 
30 /**************************************************************************/
31 /*                                                                        */
32 /*  FUNCTION                                               RELEASE        */
33 /*                                                                        */
34 /*    _ux_device_class_hid_receiver_initialize            PORTABLE C      */
35 /*                                                           6.3.0        */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    Chaoqiong Xiao, Microsoft Corporation                               */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    This function initializes the USB HID device receiver.              */
43 /*    This function is called by the class register function. It is only  */
44 /*    done once.                                                          */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    hid                                  Pointer to hid instance        */
49 /*    parameter                            Pointer to hid parameter       */
50 /*    receiver                             Pointer to fill pointer to     */
51 /*                                           allocated receiver instance  */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_utility_memory_allocate           Allocate memory               */
60 /*    _ux_utility_memory_free               Free memory                   */
61 /*    _ux_utility_thread_create             Create thread                 */
62 /*    _ux_utility_thread_delete             Delete thread                 */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    USBX Source Code                                                    */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  01-31-2022     Chaoqiong Xiao           Initial Version 6.1.10        */
73 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            added receiver callback,    */
75 /*                                            resulting in version 6.1.11 */
76 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            added standalone receiver,  */
78 /*                                            resulting in version 6.1.12 */
79 /*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
80 /*                                            added zero copy support,    */
81 /*                                            resulting in version 6.3.0  */
82 /*                                                                        */
83 /**************************************************************************/
_ux_device_class_hid_receiver_initialize(UX_SLAVE_CLASS_HID * hid,UX_SLAVE_CLASS_HID_PARAMETER * parameter,UX_DEVICE_CLASS_HID_RECEIVER ** receiver)84 UINT  _ux_device_class_hid_receiver_initialize(UX_SLAVE_CLASS_HID *hid,
85                                     UX_SLAVE_CLASS_HID_PARAMETER *parameter,
86                                     UX_DEVICE_CLASS_HID_RECEIVER **receiver)
87 {
88 #if !defined(UX_DEVICE_CLASS_HID_INTERRUPT_OUT_SUPPORT)
89     UX_PARAMETER_NOT_USED(hid);
90     UX_PARAMETER_NOT_USED(parameter);
91     UX_PARAMETER_NOT_USED(receiver);
92     return(UX_FUNCTION_NOT_SUPPORTED);
93 #else
94 ULONG                                   memory_size;
95 ULONG                                   events_size;
96 UCHAR                                   *memory_receiver;
97 UCHAR                                   *memory_events;
98 #if !defined(UX_DEVICE_STANDALONE)
99 UCHAR                                   *memory_stack;
100 #endif
101 #if (UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1) && defined(UX_DEVICE_CLASS_HID_ZERO_COPY)
102 UX_DEVICE_CLASS_HID_RECEIVED_EVENT      *events_head;
103 UCHAR                                   *buffer;
104 UINT                                    i;
105 #endif
106 UINT                                    status = UX_SUCCESS;
107 
108 
109     /* Validate parameters.  */
110     UX_ASSERT(parameter -> ux_device_class_hid_parameter_receiver_event_max_length <= UX_SLAVE_REQUEST_DATA_MAX_LENGTH);
111 
112     /* Allocate memory for receiver and receiver events.  */
113 
114     /* Memory of thread stack and receiver instance.  */
115 #if !defined(UX_DEVICE_STANDALONE)
116     UX_ASSERT(!UX_OVERFLOW_CHECK_ADD_ULONG(UX_DEVICE_CLASS_HID_RECEIVER_THREAD_STACK_SIZE, sizeof(UX_DEVICE_CLASS_HID_RECEIVER)));
117     memory_size = UX_DEVICE_CLASS_HID_RECEIVER_THREAD_STACK_SIZE +
118                   sizeof(UX_DEVICE_CLASS_HID_RECEIVER);
119 #else
120     memory_size = sizeof(UX_DEVICE_CLASS_HID_RECEIVER);
121 #endif
122     UX_ASSERT(!UX_OVERFLOW_CHECK_ADD_ULONG(parameter -> ux_device_class_hid_parameter_receiver_event_max_length, sizeof(ULONG)));
123 
124 #if (UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1) && defined(UX_DEVICE_CLASS_HID_ZERO_COPY)
125 
126     /* Events structs are in regular memory.  */
127     UX_ASSERT(!UX_OVERFLOW_CHECK_MULV_ULONG(sizeof(UX_DEVICE_CLASS_HID_RECEIVED_EVENT), parameter -> ux_device_class_hid_parameter_receiver_event_max_number));
128     events_size = sizeof(UX_DEVICE_CLASS_HID_RECEIVED_EVENT) * parameter -> ux_device_class_hid_parameter_receiver_event_max_number;
129 
130     /* Memory of events are allocated later as cache safe memory.  */
131 #else
132 
133     /* Memory of events.  */
134     events_size  = parameter -> ux_device_class_hid_parameter_receiver_event_max_length + sizeof(ULONG);
135     UX_ASSERT(!UX_OVERFLOW_CHECK_MULV_ULONG(events_size, parameter -> ux_device_class_hid_parameter_receiver_event_max_number));
136     events_size *= parameter -> ux_device_class_hid_parameter_receiver_event_max_number;
137 #endif
138     UX_ASSERT(!UX_OVERFLOW_CHECK_ADD_ULONG(memory_size, events_size));
139     memory_size += events_size;
140 
141     /* Allocate memory.  */
142     memory_receiver = _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, memory_size);
143     if (memory_receiver == UX_NULL)
144         return(UX_MEMORY_INSUFFICIENT);
145 #if !defined(UX_DEVICE_STANDALONE)
146     memory_stack = memory_receiver + sizeof(UX_DEVICE_CLASS_HID_RECEIVER);
147     memory_events = memory_stack + UX_DEVICE_CLASS_HID_RECEIVER_THREAD_STACK_SIZE;
148 #else
149     memory_events = memory_receiver + sizeof(UX_DEVICE_CLASS_HID_RECEIVER);
150 #endif
151 
152 #if (UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1) && defined(UX_DEVICE_CLASS_HID_ZERO_COPY)
153 
154     /* Allocate cache safe memory.  */
155 
156     /* Total buffer size calculate.  */
157     events_size  = parameter -> ux_device_class_hid_parameter_receiver_event_max_length;
158     UX_ASSERT(!UX_OVERFLOW_CHECK_MULV_ULONG(events_size, parameter -> ux_device_class_hid_parameter_receiver_event_max_number));
159     events_size *= parameter -> ux_device_class_hid_parameter_receiver_event_max_number;
160 
161     /* Allocate buffer.  */
162     buffer = _ux_utility_memory_allocate(UX_NO_ALIGN, UX_CACHE_SAFE_MEMORY, events_size);
163     if (buffer == UX_NULL)
164     {
165         _ux_utility_memory_free(memory_receiver);
166         return(UX_MEMORY_INSUFFICIENT);
167     }
168 
169     /* Assign events buffers.  */
170     events_head = (UX_DEVICE_CLASS_HID_RECEIVED_EVENT *) memory_events;
171     for (i = 0; i < parameter -> ux_device_class_hid_parameter_receiver_event_max_number; i++)
172     {
173 
174         /* Assign event buffer.  */
175         events_head -> ux_device_class_hid_received_event_data = buffer;
176 
177         /* Move to next event and next buffer.  */
178         buffer += parameter -> ux_device_class_hid_parameter_receiver_event_max_length;
179         events_head ++;
180     }
181 #endif
182 
183     /* Store receiver instance pointer.  */
184     (*receiver) = (UX_DEVICE_CLASS_HID_RECEIVER *)memory_receiver;
185 
186 #if !defined(UX_DEVICE_STANDALONE)
187 
188     /* This instance needs to be running in a different thread. So start
189        a new thread. We pass a pointer to the class to the new thread.  This thread
190        does not start until we have a instance of the class. */
191     if (status == UX_SUCCESS)
192         status =  _ux_utility_thread_create(&(*receiver) -> ux_device_class_hid_receiver_thread,
193                     "ux_device_class_hid_receiver_thread",
194                     _ux_device_class_hid_receiver_thread,
195                     (ULONG) (ALIGN_TYPE) hid, (VOID *) memory_stack,
196                     UX_DEVICE_CLASS_HID_RECEIVER_THREAD_STACK_SIZE, UX_THREAD_PRIORITY_CLASS,
197                     UX_THREAD_PRIORITY_CLASS, UX_NO_TIME_SLICE, UX_DONT_START);
198 #endif
199 
200     /* Check the creation of this thread.  */
201     if (status == UX_SUCCESS)
202     {
203 
204 #if !defined(UX_DEVICE_STANDALONE)
205         UX_THREAD_EXTENSION_PTR_SET(&((*receiver) -> ux_device_class_hid_receiver_thread), hid)
206 #else
207         hid -> ux_device_class_hid_read_state = UX_DEVICE_CLASS_HID_RECEIVER_START;
208         (*receiver) -> ux_device_class_hid_receiver_tasks_run = _ux_device_class_hid_receiver_tasks_run;
209 #endif
210 
211         /* Initialize event buffer size.  */
212         (*receiver) -> ux_device_class_hid_receiver_event_buffer_size =
213                     parameter -> ux_device_class_hid_parameter_receiver_event_max_length;
214 
215         /* Initialize events.  */
216         (*receiver) -> ux_device_class_hid_receiver_events =
217                         (UX_DEVICE_CLASS_HID_RECEIVED_EVENT *)(memory_events);
218         (*receiver) -> ux_device_class_hid_receiver_events_end =
219                         (UX_DEVICE_CLASS_HID_RECEIVED_EVENT *)(memory_receiver + memory_size);
220         (*receiver) -> ux_device_class_hid_receiver_event_read_pos =
221                     (*receiver) -> ux_device_class_hid_receiver_events;
222         (*receiver) -> ux_device_class_hid_receiver_event_save_pos =
223                     (*receiver) -> ux_device_class_hid_receiver_events;
224 
225         /* Initialize uninitialize function.  */
226         (*receiver) -> ux_device_class_hid_receiver_uninitialize = _ux_device_class_hid_receiver_uninitialize;
227 
228         /* Initialize callback function.  */
229         (*receiver) -> ux_device_class_hid_receiver_event_callback =
230                     parameter -> ux_device_class_hid_parameter_receiver_event_callback;
231 
232         /* Done success.  */
233         return(UX_SUCCESS);
234     }
235     else
236         status = (UX_THREAD_ERROR);
237 
238     /* Free allocated memory. */
239     _ux_utility_memory_free(*receiver);
240     (*receiver) =  UX_NULL;
241 
242     /* Return completion status.  */
243     return(status);
244 #endif
245 }
246