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 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_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_hid_idle_set                         PORTABLE C      */
38 /*                                                           6.1.10       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function performs a SET_IDLE to the HID device.                */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    hid                                   Pointer to HID class          */
50 /*    idle_time                             Idle time                     */
51 /*    report_id                             Report ID                     */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_host_stack_class_instance_verify  Verify instance is valid      */
60 /*    _ux_host_stack_transfer_request       Process transfer request      */
61 /*    _ux_host_semaphore_get                Get protection semaphore      */
62 /*    _ux_host_semaphore_put                Release protection semaphore  */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    HID Class                                                           */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
73 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            protected default control   */
75 /*                                            endpoint before using it,   */
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 /*                                                                        */
81 /**************************************************************************/
_ux_host_class_hid_idle_set(UX_HOST_CLASS_HID * hid,USHORT idle_time,USHORT report_id)82 UINT  _ux_host_class_hid_idle_set(UX_HOST_CLASS_HID *hid, USHORT idle_time, USHORT report_id)
83 {
84 #if defined(UX_HOST_STANDALONE)
85 UINT            status;
86     do
87     {
88         status = _ux_host_class_hid_idle_set_run(hid, idle_time, report_id);
89     } while(status == UX_STATE_WAIT || status == UX_STATE_LOCK);
90     return(hid -> ux_host_class_hid_status);
91 #else
92 
93 UX_ENDPOINT     *control_endpoint;
94 UX_TRANSFER     *transfer_request;
95 UINT            status;
96 
97     /* If trace is enabled, insert this event into the trace buffer.  */
98     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_HID_IDLE_SET, hid, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
99 
100     /* Ensure the instance is valid.  */
101     if (_ux_host_stack_class_instance_verify(_ux_system_host_class_hid_name, (VOID *) hid) != UX_SUCCESS)
102     {
103 
104         /* If trace is enabled, insert this event into the trace buffer.  */
105         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
106 
107         return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
108     }
109 
110     /* We need to get the default control endpoint transfer request pointer.  */
111     control_endpoint =  &hid -> ux_host_class_hid_device -> ux_device_control_endpoint;
112     transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
113 
114     /* Protect thread reentry to this instance.  */
115     status =  _ux_host_semaphore_get(&hid -> ux_host_class_hid_semaphore, UX_WAIT_FOREVER);
116     if (status != UX_SUCCESS)
117         return(status);
118 
119     /* Protect the control endpoint semaphore here.  It will be unprotected in the
120        transfer request function.  */
121     status =  _ux_host_semaphore_get(&hid -> ux_host_class_hid_device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
122 
123     /* Check for status.  */
124     if (status != UX_SUCCESS)
125     {
126 
127         /* Something went wrong. */
128         /* Unprotect thread reentry to this instance.  */
129         _ux_host_semaphore_put(&hid -> ux_host_class_hid_semaphore);
130 
131         return(status);
132     }
133 
134     /* Create a transfer request for the SET_IDLE request.  */
135     transfer_request -> ux_transfer_request_data_pointer =      UX_NULL;
136     transfer_request -> ux_transfer_request_requested_length =  0;
137     transfer_request -> ux_transfer_request_function =          UX_HOST_CLASS_HID_SET_IDLE;
138     transfer_request -> ux_transfer_request_type =              UX_REQUEST_OUT | UX_REQUEST_TYPE_CLASS | UX_REQUEST_TARGET_INTERFACE;
139     transfer_request -> ux_transfer_request_value =             (UINT)((idle_time << 8) | report_id);
140     transfer_request -> ux_transfer_request_index =             hid -> ux_host_class_hid_interface -> ux_interface_descriptor.bInterfaceNumber;
141 
142     /* Send request to HCD layer.  */
143     status =  _ux_host_stack_transfer_request(transfer_request);
144 
145     /* Unprotect thread reentry to this instance.  */
146     _ux_host_semaphore_put(&hid -> ux_host_class_hid_semaphore);
147 
148     /* Return the function status.  */
149     return(status);
150 #endif
151 }
152