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