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 #if defined(UX_HOST_STANDALONE)
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _ux_host_class_hid_idle_set_run                     PORTABLE C      */
39 /*                                                           6.1.10       */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    Chaoqiong Xiao, Microsoft Corporation                               */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function performs a SET_IDLE to the HID device.                */
47 /*                                                                        */
48 /*    It's for standalone mode.                                           */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    hid                                   Pointer to HID class          */
53 /*    idle_time                             Idle time                     */
54 /*    report_id                             Report ID                     */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    Completion Status                                                   */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    _ux_host_stack_class_instance_verify  Verify instance is valid      */
63 /*    _ux_host_stack_transfer_run           Process transfer states       */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    HID Class                                                           */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  01-31-2022     Chaoqiong Xiao           Initial Version 6.1.10        */
74 /*                                                                        */
75 /**************************************************************************/
_ux_host_class_hid_idle_set_run(UX_HOST_CLASS_HID * hid,USHORT idle_time,USHORT report_id)76 UINT _ux_host_class_hid_idle_set_run(UX_HOST_CLASS_HID *hid, USHORT idle_time, USHORT report_id)
77 {
78 
79 UX_INTERRUPT_SAVE_AREA
80 UX_DEVICE       *device;
81 UX_ENDPOINT     *control_endpoint;
82 UX_TRANSFER     *transfer_request;
83 UINT            status;
84 
85     /* If trace is enabled, insert this event into the trace buffer.  */
86     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_HID_IDLE_SET, hid, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
87 
88     /* Ensure the instance is valid.  */
89     if (_ux_host_stack_class_instance_verify(_ux_system_host_class_hid_name, (VOID *) hid) != UX_SUCCESS)
90     {
91 
92         /* If trace is enabled, insert this event into the trace buffer.  */
93         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
94 
95         hid -> ux_host_class_hid_status = UX_HOST_CLASS_INSTANCE_UNKNOWN;
96         return(UX_STATE_EXIT);
97     }
98 
99     /* Get device.  */
100     device = hid -> ux_host_class_hid_device;
101 
102     /* Sanity check.  */
103     if (device == UX_NULL ||
104         device -> ux_device_handle != (ULONG)(ALIGN_TYPE)device)
105     {
106         hid -> ux_host_class_hid_status = UX_DEVICE_HANDLE_UNKNOWN;
107         return(UX_STATE_EXIT);
108     }
109 
110     /* We need to get the default control endpoint transfer request pointer.  */
111     control_endpoint =  &device -> ux_device_control_endpoint;
112     transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
113 
114     /* Waiting transfer done.  */
115     if (hid -> ux_host_class_hid_cmd_state == UX_STATE_WAIT)
116     {
117 
118         /* Process background tasks.  */
119         _ux_system_tasks_run();
120 
121         /* Check if transfer is done.  */
122         if (transfer_request -> ux_transfer_request_state < UX_STATE_WAIT)
123         {
124 
125             /* Unlock.  */
126             hid -> ux_host_class_hid_flags &= ~UX_HOST_CLASS_HID_FLAG_LOCK;
127             device -> ux_device_flags &= ~UX_DEVICE_FLAG_LOCK;
128 
129             /* We are done.  */
130             hid -> ux_host_class_hid_cmd_state = UX_STATE_IDLE;
131             hid -> ux_host_class_hid_status =
132                         transfer_request -> ux_transfer_request_completion_code;
133             return(UX_STATE_NEXT);
134         }
135 
136         /* Keep waiting.  */
137         return(UX_STATE_WAIT);
138     }
139 
140     /* Check state or protection status.  */
141     UX_DISABLE
142     if ((hid -> ux_host_class_hid_flags & UX_HOST_CLASS_HID_FLAG_LOCK) ||
143         (device -> ux_device_flags & UX_DEVICE_FLAG_LOCK))
144     {
145 
146         /* Locked.  */
147         UX_RESTORE
148         return(UX_STATE_LOCK);
149     }
150     hid -> ux_host_class_hid_flags |= UX_HOST_CLASS_HID_FLAG_LOCK;
151     device -> ux_device_flags |= UX_DEVICE_FLAG_LOCK;
152     hid -> ux_host_class_hid_cmd_state = UX_STATE_WAIT;
153     UX_RESTORE
154 
155     /* Create a transfer request for the SET_IDLE request.  */
156     transfer_request -> ux_transfer_request_data_pointer =     UX_NULL;
157     transfer_request -> ux_transfer_request_requested_length = 0;
158     transfer_request -> ux_transfer_request_function =         UX_HOST_CLASS_HID_SET_IDLE;
159     transfer_request -> ux_transfer_request_type =             UX_REQUEST_OUT | UX_REQUEST_TYPE_CLASS | UX_REQUEST_TARGET_INTERFACE;
160     transfer_request -> ux_transfer_request_value =            (UINT)((idle_time << 8) | report_id);
161     transfer_request -> ux_transfer_request_index =            hid -> ux_host_class_hid_interface -> ux_interface_descriptor.bInterfaceNumber;
162     UX_TRANSFER_STATE_RESET(transfer_request);
163 
164     /* Send request to HCD layer.  */
165     status =  _ux_host_stack_transfer_run(transfer_request);
166     if (status != UX_STATE_WAIT)
167     {
168         hid -> ux_host_class_hid_status =
169                     transfer_request -> ux_transfer_request_completion_code;
170 
171         /* Unlock.  */
172         hid -> ux_host_class_hid_flags &= ~UX_HOST_CLASS_HID_FLAG_LOCK;
173         device -> ux_device_flags &= ~UX_DEVICE_FLAG_LOCK;
174 
175         hid -> ux_host_class_hid_cmd_state = (UCHAR)status;
176     }
177 
178     /* Return the function status.  */
179     return(status);
180 }
181 #endif
182