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 /** USBX Component                                                        */
15 /**                                                                       */
16 /**   Device HID Class                                                    */
17 /**                                                                       */
18 /**************************************************************************/
19 /**************************************************************************/
20 
21 #define UX_SOURCE_CODE
22 
23 
24 /* Include necessary system files.  */
25 
26 #include "ux_api.h"
27 #include "ux_device_class_hid.h"
28 #include "ux_device_stack.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_device_class_hid_receiver_event_get             PORTABLE C      */
36 /*                                                           6.3.0        */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function checks if there was an event from the interrupt OUT,  */
44 /*    if so return first received event length and it's buffer entry      */
45 /*    pointer.                                                            */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    hid                                      Address of hid class       */
50 /*    event                                    Pointer of the event       */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    status                                   UX_SUCCESS with event      */
55 /*                                             UX_ERROR without event     */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_utility_memory_copy                  Copy memory                */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    ThreadX                                                             */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  01-31-2022     Chaoqiong Xiao           Initial Version 6.1.10        */
70 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            cleaned compile with TRACE, */
72 /*                                            resulting in version 6.1.12 */
73 /*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            added zero copy support,    */
75 /*                                            resulting in version 6.3.0  */
76 /*                                                                        */
77 /**************************************************************************/
_ux_device_class_hid_receiver_event_get(UX_SLAVE_CLASS_HID * hid,UX_DEVICE_CLASS_HID_RECEIVED_EVENT * event)78 UINT  _ux_device_class_hid_receiver_event_get(UX_SLAVE_CLASS_HID *hid,
79                                 UX_DEVICE_CLASS_HID_RECEIVED_EVENT *event)
80 {
81 #if !defined(UX_DEVICE_CLASS_HID_INTERRUPT_OUT_SUPPORT)
82     UX_PARAMETER_NOT_USED(hid);
83     UX_PARAMETER_NOT_USED(event);
84     return(UX_FUNCTION_NOT_SUPPORTED);
85 #else
86 
87 
88 UX_DEVICE_CLASS_HID_RECEIVER            *receiver;
89 UX_DEVICE_CLASS_HID_RECEIVED_EVENT      *pos;
90 
91 
92     /* If trace is enabled, insert this event into the trace buffer.  */
93     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_HID_RECEIVER_EVENT_GET, hid, event, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
94 
95     /* Get receiver.  */
96     receiver = hid -> ux_device_class_hid_receiver;
97 
98     /* Get current reading position.  */
99     pos = receiver -> ux_device_class_hid_receiver_event_read_pos;
100 
101     /* Check if it's available.  */
102     if (pos -> ux_device_class_hid_received_event_length != 0)
103     {
104 
105         /* Fill event structure to return.  */
106         event -> ux_device_class_hid_received_event_length = pos -> ux_device_class_hid_received_event_length;
107 
108         /* Fill data buffer address to return.  */
109         event -> ux_device_class_hid_received_event_data = UX_DEVICE_CLASS_HID_RECEIVED_QUEUE_ITEM_BUFFER(pos);
110 
111         return(UX_SUCCESS);
112     }
113 
114     return(UX_ERROR);
115 #endif
116 }
117 
118 
119 /**************************************************************************/
120 /*                                                                        */
121 /*  FUNCTION                                               RELEASE        */
122 /*                                                                        */
123 /*    _uxe_device_class_hid_receiver_event_get            PORTABLE C      */
124 /*                                                           6.3.0        */
125 /*  AUTHOR                                                                */
126 /*                                                                        */
127 /*    Chaoqiong Xiao, Microsoft Corporation                               */
128 /*                                                                        */
129 /*  DESCRIPTION                                                           */
130 /*                                                                        */
131 /*    This function checks errors in HID event get function call.         */
132 /*                                                                        */
133 /*  INPUT                                                                 */
134 /*                                                                        */
135 /*    hid                                   Pointer to hid instance       */
136 /*    event                                 Pointer of the event          */
137 /*                                                                        */
138 /*  OUTPUT                                                                */
139 /*                                                                        */
140 /*    None                                                                */
141 /*                                                                        */
142 /*  CALLS                                                                 */
143 /*                                                                        */
144 /*    _ux_device_class_hid_receiver_event_get                             */
145 /*                                          Get a receiver event          */
146 /*                                                                        */
147 /*  CALLED BY                                                             */
148 /*                                                                        */
149 /*    Application                                                         */
150 /*                                                                        */
151 /*  RELEASE HISTORY                                                       */
152 /*                                                                        */
153 /*    DATE              NAME                      DESCRIPTION             */
154 /*                                                                        */
155 /*  10-31-2023     Chaoqiong Xiao           Initial Version 6.3.0         */
156 /*                                                                        */
157 /**************************************************************************/
_uxe_device_class_hid_receiver_event_get(UX_SLAVE_CLASS_HID * hid,UX_DEVICE_CLASS_HID_RECEIVED_EVENT * event)158 UINT  _uxe_device_class_hid_receiver_event_get(UX_SLAVE_CLASS_HID *hid,
159                                 UX_DEVICE_CLASS_HID_RECEIVED_EVENT *event)
160 {
161 
162     /* Sanity check.  */
163     if ((hid == UX_NULL) || (event == UX_NULL))
164         return(UX_INVALID_PARAMETER);
165 
166     /* Invoke function to free HID event.  */
167     return(_ux_device_class_hid_receiver_event_get(hid, event));
168 }
169