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 Mouse Client 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_class_hid_mouse.h"
30 #include "ux_host_stack.h"
31
32
33 /**************************************************************************/
34 /* */
35 /* FUNCTION RELEASE */
36 /* */
37 /* _ux_host_class_hid_mouse_activate PORTABLE C */
38 /* 6.1.11 */
39 /* AUTHOR */
40 /* */
41 /* Chaoqiong Xiao, Microsoft Corporation */
42 /* */
43 /* DESCRIPTION */
44 /* */
45 /* This function performs the enumeration of a HID mouse. */
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_idle_set Set idle rate */
58 /* _ux_host_class_hid_report_callback_register */
59 /* Register report callback */
60 /* _ux_host_class_hid_report_id_get Get report ID */
61 /* _ux_host_class_hid_periodic_report_start */
62 /* Start periodic report */
63 /* _ux_utility_memory_allocate Allocate memory block */
64 /* _ux_utility_memory_free Free memory block */
65 /* */
66 /* CALLED BY */
67 /* */
68 /* HID Mouse Class */
69 /* */
70 /* RELEASE HISTORY */
71 /* */
72 /* DATE NAME DESCRIPTION */
73 /* */
74 /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */
75 /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */
76 /* resulting in version 6.1 */
77 /* 01-31-2022 Chaoqiong Xiao Modified comment(s), */
78 /* added standalone support, */
79 /* resulting in version 6.1.10 */
80 /* 04-25-2022 Chaoqiong Xiao Modified comment(s), */
81 /* fixed clients management, */
82 /* resulting in version 6.1.11 */
83 /* */
84 /**************************************************************************/
_ux_host_class_hid_mouse_activate(UX_HOST_CLASS_HID_CLIENT_COMMAND * command)85 UINT _ux_host_class_hid_mouse_activate(UX_HOST_CLASS_HID_CLIENT_COMMAND *command)
86 {
87
88 #if !defined(UX_HOST_STANDALONE)
89 UX_HOST_CLASS_HID_REPORT_CALLBACK call_back;
90 #endif
91 UX_HOST_CLASS_HID_REPORT_GET_ID report_id;
92 UX_HOST_CLASS_HID *hid;
93 UX_HOST_CLASS_HID_CLIENT *hid_client;
94 UX_HOST_CLASS_HID_CLIENT_MOUSE *client_mouse;
95 UX_HOST_CLASS_HID_MOUSE *mouse_instance;
96 UINT status;
97
98
99 /* Get the instance to the HID class. */
100 hid = command -> ux_host_class_hid_client_command_instance;
101
102 /* Get some memory for both the HID class instance and copy of this client
103 and for the callback. */
104 client_mouse = (UX_HOST_CLASS_HID_CLIENT_MOUSE *)
105 _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY,
106 sizeof(UX_HOST_CLASS_HID_CLIENT_MOUSE));
107 if(client_mouse == UX_NULL)
108 return(UX_MEMORY_INSUFFICIENT);
109
110 /* Get client and mouse instance. */
111 mouse_instance = &client_mouse -> ux_host_class_hid_client_mouse_mouse;
112 hid_client = &client_mouse -> ux_host_class_hid_client_mouse_client;
113 _ux_utility_memory_copy(hid_client, hid -> ux_host_class_hid_client, sizeof(UX_HOST_CLASS_HID_CLIENT)); /* Use case of memcpy is verified. */
114
115 /* Attach the mouse instance to the client instance. */
116 hid_client -> ux_host_class_hid_client_local_instance = (VOID *) mouse_instance;
117
118 /* Save the HID instance in the client instance. */
119 mouse_instance -> ux_host_class_hid_mouse_hid = hid;
120
121 #if defined(UX_HOST_STANDALONE)
122
123 /* The instance is mounting now. */
124 mouse_instance -> ux_host_class_hid_mouse_state = UX_HOST_CLASS_INSTANCE_MOUNTING;
125
126 /* Get the report ID for the mouse. The mouse is a INPUT report.
127 This should be 0 but in case. */
128 report_id.ux_host_class_hid_report_get_report = UX_NULL;
129 report_id.ux_host_class_hid_report_get_type = UX_HOST_CLASS_HID_REPORT_TYPE_INPUT;
130 status = _ux_host_class_hid_report_id_get(hid, &report_id);
131
132 /* The report ID should exist. */
133 if (status == UX_SUCCESS)
134 {
135
136 /* Save the mouse report ID. */
137 mouse_instance -> ux_host_class_hid_mouse_id = (USHORT)report_id.ux_host_class_hid_report_get_id;
138
139 /* Set state for activate wait steps. */
140 mouse_instance -> ux_host_class_hid_mouse_enum_state = UX_STATE_WAIT;
141
142 }
143
144 /* Use our copy of client. */
145 hid -> ux_host_class_hid_client = hid_client;
146 return(status);
147 #else
148
149 /* The instance is live now. */
150 mouse_instance -> ux_host_class_hid_mouse_state = UX_HOST_CLASS_INSTANCE_LIVE;
151
152 /* Get the report ID for the mouse. The mouse is a INPUT report.
153 This should be 0 but in case. */
154 report_id.ux_host_class_hid_report_get_report = UX_NULL;
155 report_id.ux_host_class_hid_report_get_type = UX_HOST_CLASS_HID_REPORT_TYPE_INPUT;
156 status = _ux_host_class_hid_report_id_get(hid, &report_id);
157
158 /* The report ID should exist. */
159 if (status == UX_SUCCESS)
160 {
161
162 /* Save the mouse report ID. */
163 mouse_instance -> ux_host_class_hid_mouse_id = (USHORT)report_id.ux_host_class_hid_report_get_id;
164
165 /* Set the idle rate of the mouse to 0. This way a report is generated only when there is an activity. */
166 status = _ux_host_class_hid_idle_set(hid, 0, mouse_instance -> ux_host_class_hid_mouse_id);
167
168 /* Check for error, accept protocol error since it's optional for mouse. */
169 if (status == UX_TRANSFER_STALLED)
170 status = UX_SUCCESS;
171 }
172
173 /* If we are OK, go on. */
174 if (status == UX_SUCCESS)
175 {
176
177 /* Initialize the report callback. */
178 call_back.ux_host_class_hid_report_callback_id = mouse_instance -> ux_host_class_hid_mouse_id;
179 call_back.ux_host_class_hid_report_callback_function = _ux_host_class_hid_mouse_callback;
180 call_back.ux_host_class_hid_report_callback_buffer = UX_NULL;
181 call_back.ux_host_class_hid_report_callback_flags = UX_HOST_CLASS_HID_REPORT_INDIVIDUAL_USAGE;
182 call_back.ux_host_class_hid_report_callback_length = 0;
183
184 /* Register the report call back when data comes it on this report. */
185 status = _ux_host_class_hid_report_callback_register(hid, &call_back);
186 }
187
188 /* If we are OK, go on. */
189 if (status == UX_SUCCESS)
190 {
191
192 /* Start the periodic report. */
193 status = _ux_host_class_hid_periodic_report_start(hid);
194
195 if (status == UX_SUCCESS)
196 {
197
198 /* Use our copy of client. */
199 hid -> ux_host_class_hid_client = hid_client;
200
201 /* If all is fine and the device is mounted, we may need to inform the application
202 if a function has been programmed in the system structure. */
203 if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
204 {
205
206 /* Call system change function. */
207 _ux_system_host -> ux_system_host_change_function(UX_HID_CLIENT_INSERTION, hid -> ux_host_class_hid_class, (VOID *) hid_client);
208 }
209
210 /* If trace is enabled, insert this event into the trace buffer. */
211 UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_HID_MOUSE_ACTIVATE, hid, mouse_instance, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
212
213 /* Return completion status. */
214 return(status);
215 }
216 }
217
218 /* We are here if there is error. */
219
220 /* Free mouse client instance. */
221 _ux_utility_memory_free(mouse_instance);
222
223 /* Return completion status. */
224 return(status);
225 #endif
226 }
227
228