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 Remote Control 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_remote_control.h"
31 #include "ux_host_stack.h"
32 
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _ux_host_class_hid_remote_control_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 Remote Control      */
47 /*    class.                                                              */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    command                               Pointer to command            */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_host_class_hid_periodic_report_start    Start periodic report   */
60 /*    _ux_host_class_hid_report_callback_register Register callback       */
61 /*    _ux_utility_memory_allocate           Allocate memory block         */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    HID Remote Control Class                                            */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
72 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            resulting in version 6.1    */
74 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
75 /*                                            fixed clients management,   */
76 /*                                            resulting in version 6.1.11 */
77 /*                                                                        */
78 /**************************************************************************/
_ux_host_class_hid_remote_control_activate(UX_HOST_CLASS_HID_CLIENT_COMMAND * command)79 UINT  _ux_host_class_hid_remote_control_activate(UX_HOST_CLASS_HID_CLIENT_COMMAND *command)
80 {
81 
82 UX_HOST_CLASS_HID_REPORT_CALLBACK        call_back;
83 UX_HOST_CLASS_HID                        *hid;
84 UX_HOST_CLASS_HID_CLIENT                 *hid_client;
85 UX_HOST_CLASS_HID_CLIENT_REMOTE_CONTROL  *client_remote_control;
86 UX_HOST_CLASS_HID_REMOTE_CONTROL         *remote_control_instance;
87 UINT                                     status = UX_SUCCESS;
88 
89 
90     /* Get the instance to the HID class.  */
91     hid =  command -> ux_host_class_hid_client_command_instance;
92 
93     /* Get some memory for both the HID class instance and copy of this client
94        and for the callback.  */
95     client_remote_control =  (UX_HOST_CLASS_HID_CLIENT_REMOTE_CONTROL *)
96                     _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY,
97                                 sizeof(UX_HOST_CLASS_HID_CLIENT_REMOTE_CONTROL));
98     if (client_remote_control == UX_NULL)
99         return(UX_MEMORY_INSUFFICIENT);
100 
101     /* Get client instance and client copy.  */
102     remote_control_instance = &client_remote_control -> ux_host_class_hid_client_remote_control_remote_control;
103     hid_client = &client_remote_control -> ux_host_class_hid_client_remote_control_client;
104     _ux_utility_memory_copy(hid_client, hid -> ux_host_class_hid_client, sizeof(UX_HOST_CLASS_HID_CLIENT)); /* Use case of memcpy is verified. */
105 
106     /* Attach the remote control instance to the client instance.  */
107     hid_client -> ux_host_class_hid_client_local_instance =  (VOID *) remote_control_instance;
108 
109     /* Save the HID instance in the client instance.  */
110     remote_control_instance -> ux_host_class_hid_remote_control_hid =  hid;
111 
112     /* The instance is live now.  */
113     remote_control_instance -> ux_host_class_hid_remote_control_state =  UX_HOST_CLASS_INSTANCE_LIVE;
114 
115     /* Allocate the round-robin buffer that the remote control instance will use
116      * to store the usages as they come in.
117      * Size calculation overflow is checked near where _USAGE_ARRAY_LENGTH is defined.
118      */
119     remote_control_instance -> ux_host_class_hid_remote_control_usage_array =  (ULONG *)
120                             _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY,
121                                                         UX_HOST_CLASS_HID_REMOTE_CONTROL_USAGE_ARRAY_LENGTH*4);
122 
123     /* Check memory pointer. */
124     if (remote_control_instance -> ux_host_class_hid_remote_control_usage_array == UX_NULL)
125         status = (UX_MEMORY_INSUFFICIENT);
126 
127     /* If there is no error, go on.  */
128     if (status == UX_SUCCESS)
129     {
130 
131         /* Initialize the head and tail of this array.  */
132         remote_control_instance -> ux_host_class_hid_remote_control_usage_array_head =  remote_control_instance -> ux_host_class_hid_remote_control_usage_array;
133         remote_control_instance -> ux_host_class_hid_remote_control_usage_array_tail =  remote_control_instance -> ux_host_class_hid_remote_control_usage_array;
134 
135         /* Initialize the report callback.  */
136         call_back.ux_host_class_hid_report_callback_id =         0;
137         call_back.ux_host_class_hid_report_callback_function =   _ux_host_class_hid_remote_control_callback;
138         call_back.ux_host_class_hid_report_callback_buffer =     UX_NULL;
139         call_back.ux_host_class_hid_report_callback_flags =      UX_HOST_CLASS_HID_REPORT_INDIVIDUAL_USAGE;
140         call_back.ux_host_class_hid_report_callback_length =     0;
141 
142         /* Register the report call back when data comes in on this report.  */
143         status =  _ux_host_class_hid_report_callback_register(hid, &call_back);
144     }
145 
146     /* If there is no error, go on.  */
147     if (status == UX_SUCCESS)
148     {
149 
150         /* Start the periodic report.  */
151         status =  _ux_host_class_hid_periodic_report_start(hid);
152 
153         /* If OK, we are done.  */
154         if (status == UX_SUCCESS)
155         {
156 
157             /* Use out copy of client.  */
158             hid -> ux_host_class_hid_client = hid_client;
159 
160             /* If trace is enabled, insert this event into the trace buffer.  */
161             UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_HID_REMOTE_CONTROL_ACTIVATE, hid, remote_control_instance, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
162 
163             /* If all is fine and the device is mounted, we may need to inform the application
164             if a function has been programmed in the system structure.  */
165             if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
166             {
167 
168                 /* Call system change function.  */
169                 _ux_system_host ->  ux_system_host_change_function(UX_HID_CLIENT_INSERTION, hid -> ux_host_class_hid_class, (VOID *) hid_client);
170             }
171 
172             /* We are done success.  */
173             return (UX_SUCCESS);
174         }
175     }
176 
177     /* We are here when there is error.  */
178 
179     /* Free usage array.  */
180     if (remote_control_instance -> ux_host_class_hid_remote_control_usage_array)
181         _ux_utility_memory_free(remote_control_instance -> ux_host_class_hid_remote_control_usage_array);
182 
183     /* Free instance memory.  */
184     _ux_utility_memory_free(remote_control_instance);
185 
186     /* Return completion status.  */
187     return(status);
188 }
189 
190