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 /** 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 /**************************************************************************/
32 /* */
33 /* FUNCTION RELEASE */
34 /* */
35 /* _ux_device_class_hid_activate PORTABLE C */
36 /* 6.1.12 */
37 /* AUTHOR */
38 /* */
39 /* Chaoqiong Xiao, Microsoft Corporation */
40 /* */
41 /* DESCRIPTION */
42 /* */
43 /* This function activates an instance of a HID device. */
44 /* */
45 /* INPUT */
46 /* */
47 /* command Pointer to hid command */
48 /* */
49 /* OUTPUT */
50 /* */
51 /* Completion Status */
52 /* */
53 /* CALLS */
54 /* */
55 /* _ux_device_thread_resume Resume thread */
56 /* */
57 /* CALLED BY */
58 /* */
59 /* USBX Source Code */
60 /* */
61 /* RELEASE HISTORY */
62 /* */
63 /* DATE NAME DESCRIPTION */
64 /* */
65 /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */
66 /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */
67 /* resulting in version 6.1 */
68 /* 12-31-2020 Chaoqiong Xiao Modified comment(s), */
69 /* added Get/Set Protocol */
70 /* request support, */
71 /* resulting in version 6.1.3 */
72 /* 10-15-2021 Chaoqiong Xiao Modified comment(s), */
73 /* added packet size assert, */
74 /* resulting in version 6.1.9 */
75 /* 01-31-2022 Chaoqiong Xiao Modified comment(s), */
76 /* added standalone support, */
77 /* added interrupt OUT support,*/
78 /* added packet size assert, */
79 /* resulting in version 6.1.10 */
80 /* 07-29-2022 Chaoqiong Xiao Modified comment(s), */
81 /* added standalone receiver, */
82 /* fixed standalone tx length, */
83 /* fixed standalone EP IN init,*/
84 /* fixed parameter/variable */
85 /* names conflict C++ keyword, */
86 /* resulting in version 6.1.12 */
87 /* */
88 /**************************************************************************/
_ux_device_class_hid_activate(UX_SLAVE_CLASS_COMMAND * command)89 UINT _ux_device_class_hid_activate(UX_SLAVE_CLASS_COMMAND *command)
90 {
91
92 UX_SLAVE_INTERFACE *interface_ptr;
93 UX_SLAVE_CLASS *class_ptr;
94 UX_SLAVE_CLASS_HID *hid;
95 UX_SLAVE_ENDPOINT *endpoint_interrupt;
96 UX_SLAVE_ENDPOINT *endpoint_in = UX_NULL;
97 #if defined(UX_DEVICE_CLASS_HID_INTERRUPT_OUT_SUPPORT)
98 UX_SLAVE_ENDPOINT *endpoint_out = UX_NULL;
99 #endif
100
101 /* Get the class container. */
102 class_ptr = command -> ux_slave_class_command_class_ptr;
103
104 /* Get the class instance in the container. */
105 hid = (UX_SLAVE_CLASS_HID *) class_ptr -> ux_slave_class_instance;
106
107 /* Get the interface that owns this instance. */
108 interface_ptr = (UX_SLAVE_INTERFACE *) command -> ux_slave_class_command_interface;
109
110 /* Store the class instance into the interface. */
111 interface_ptr -> ux_slave_interface_class_instance = (VOID *)hid;
112
113 /* Now the opposite, store the interface in the class instance. */
114 hid -> ux_slave_class_hid_interface = interface_ptr;
115
116 /* Locate the endpoints. */
117 endpoint_interrupt = interface_ptr -> ux_slave_interface_first_endpoint;
118
119 /* Check if interrupt IN endpoint exists. */
120 while (endpoint_interrupt != UX_NULL)
121 {
122 if ((endpoint_interrupt -> ux_slave_endpoint_descriptor.bmAttributes &
123 UX_MASK_ENDPOINT_TYPE) == UX_INTERRUPT_ENDPOINT)
124 {
125 if ((endpoint_interrupt -> ux_slave_endpoint_descriptor.bEndpointAddress &
126 UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_IN)
127 {
128
129 /* It's interrupt IN endpoint we need. */
130 endpoint_in = endpoint_interrupt;
131 #if defined(UX_DEVICE_CLASS_HID_INTERRUPT_OUT_SUPPORT)
132 if (endpoint_out != UX_NULL)
133 #endif
134 break;
135 }
136 #if defined(UX_DEVICE_CLASS_HID_INTERRUPT_OUT_SUPPORT)
137 else
138 {
139
140 /* It's optional interrupt OUT endpoint. */
141 endpoint_out = endpoint_interrupt;
142 if (endpoint_in != UX_NULL)
143 break;
144 }
145 #endif
146 }
147
148 /* Try next endpoint. */
149 endpoint_interrupt = endpoint_interrupt -> ux_slave_endpoint_next_endpoint;
150 }
151
152 /* Check if we found right endpoint. */
153 if (endpoint_in == UX_NULL)
154 return (UX_ERROR);
155
156 /* Validate event buffer size (fits largest transfer payload size). */
157 UX_ASSERT(UX_DEVICE_CLASS_HID_EVENT_BUFFER_LENGTH >=
158 endpoint_in -> ux_slave_endpoint_transfer_request.
159 ux_slave_transfer_request_transfer_length);
160
161 /* Default HID protocol is report protocol. */
162 hid -> ux_device_class_hid_protocol = UX_DEVICE_CLASS_HID_PROTOCOL_REPORT;
163
164 /* Save the endpoints in the hid instance. */
165 hid -> ux_device_class_hid_interrupt_endpoint = endpoint_in;
166
167 #if defined(UX_DEVICE_CLASS_HID_INTERRUPT_OUT_SUPPORT)
168
169 /* Save endpoint OUT. */
170 hid -> ux_device_class_hid_read_endpoint = endpoint_out;
171
172 /* Resume receiver thread/task (if present). */
173 if (hid -> ux_device_class_hid_receiver && endpoint_out)
174 {
175
176 /* Reset events. */
177 hid -> ux_device_class_hid_receiver -> ux_device_class_hid_receiver_event_save_pos =
178 hid -> ux_device_class_hid_receiver -> ux_device_class_hid_receiver_events;
179 hid -> ux_device_class_hid_receiver -> ux_device_class_hid_receiver_event_read_pos =
180 hid -> ux_device_class_hid_receiver -> ux_device_class_hid_receiver_events;
181 _ux_utility_memory_set(
182 hid -> ux_device_class_hid_receiver -> ux_device_class_hid_receiver_events, 0x00,
183 (ALIGN_TYPE)hid -> ux_device_class_hid_receiver -> ux_device_class_hid_receiver_events_end -
184 (ALIGN_TYPE)hid -> ux_device_class_hid_receiver -> ux_device_class_hid_receiver_events); /* Use case of memset is verified. */
185
186 #if !defined(UX_DEVICE_STANDALONE)
187
188 /* Resume thread. */
189 _ux_utility_thread_resume(&hid -> ux_device_class_hid_receiver -> ux_device_class_hid_receiver_thread);
190 #else
191
192 /* Setup read state for receiver. */
193 hid -> ux_device_class_hid_read_state = UX_DEVICE_CLASS_HID_RECEIVER_START;
194 #endif
195 }
196 #endif
197
198 #if !defined(UX_DEVICE_STANDALONE)
199
200 /* Resume thread. */
201 _ux_device_thread_resume(&class_ptr -> ux_slave_class_thread);
202 #else
203
204 /* Reset event buffered for background transfer. */
205 _ux_utility_memory_set((VOID *)&hid -> ux_device_class_hid_event, 0,
206 sizeof(UX_SLAVE_CLASS_HID_EVENT)); /* Use case of memset is verified. */
207 hid -> ux_device_class_hid_event.ux_device_class_hid_event_length =
208 endpoint_in -> ux_slave_endpoint_transfer_request.
209 ux_slave_transfer_request_transfer_length;
210
211 /* Reset event sending state. */
212 hid -> ux_device_class_hid_event_state = UX_STATE_RESET;
213 #endif
214
215
216 /* If there is a activate function call it. */
217 if (hid -> ux_slave_class_hid_instance_activate != UX_NULL)
218 {
219
220 /* Invoke the application. */
221 hid -> ux_slave_class_hid_instance_activate(hid);
222 }
223
224 /* If trace is enabled, insert this event into the trace buffer. */
225 UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_HID_ACTIVATE, hid, 0, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
226
227 /* If trace is enabled, register this object. */
228 UX_TRACE_OBJECT_REGISTER(UX_TRACE_DEVICE_OBJECT_TYPE_INTERFACE, hid, 0, 0, 0)
229
230 /* Return completion status. */
231 return(UX_SUCCESS);
232 }
233