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 /**   Device HID Class                                                    */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define UX_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "ux_api.h"
29 #include "ux_device_class_hid.h"
30 #include "ux_device_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_device_class_hid_report_get                     PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function returns a report to the host.                         */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    descriptor_type                       Descriptor type               */
50 /*    descriptor_index                      Index of descriptor           */
51 /*    host_length                           Length requested by host      */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_device_class_hid_event_get        Get HID event                 */
60 /*    _ux_device_stack_transfer_request     Process transfer request      */
61 /*    _ux_utility_memory_set                Set memory                    */
62 /*    _ux_utility_memory_copy               Copy memory                   */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    Application                                                         */
67 /*    Device Stack                                                        */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
74 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
75 /*                                            verified memset and memcpy  */
76 /*                                            cases,                      */
77 /*                                            resulting in version 6.1    */
78 /*                                                                        */
79 /**************************************************************************/
_ux_device_class_hid_report_get(UX_SLAVE_CLASS_HID * hid,ULONG descriptor_type,ULONG request_index,ULONG host_length)80 UINT  _ux_device_class_hid_report_get(UX_SLAVE_CLASS_HID *hid, ULONG descriptor_type,
81                                             ULONG request_index, ULONG host_length)
82 {
83 
84 UX_SLAVE_DEVICE                 *device;
85 UX_SLAVE_TRANSFER               *transfer_request;
86 UX_SLAVE_ENDPOINT               *endpoint;
87 UCHAR                           report_id;
88 UCHAR                           report_type;
89 UX_SLAVE_CLASS_HID_EVENT        hid_event;
90 ULONG                           hid_event_length;
91 UCHAR                           *buffer;
92 UINT                            status =  UX_ERROR;
93 
94     UX_PARAMETER_NOT_USED(descriptor_type);
95     UX_PARAMETER_NOT_USED(request_index);
96 
97     /* If trace is enabled, insert this event into the trace buffer.  */
98     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_HID_REPORT_GET, hid, descriptor_type, request_index, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
99 
100     /* Get the pointer to the device.  */
101     device =  &_ux_system_slave -> ux_system_slave_device;
102 
103     /* Get the control endpoint associated with the device.  */
104     endpoint =  &device -> ux_slave_device_control_endpoint;
105 
106     /* Get the pointer to the transfer request associated with the endpoint.  */
107     transfer_request =  &endpoint -> ux_slave_endpoint_transfer_request;
108 
109     /* Get report ID (wValue.lower) and report type (wValue.higher).  */
110     report_id   = *(transfer_request -> ux_slave_transfer_request_setup + UX_SETUP_VALUE + 0);
111     report_type = *(transfer_request -> ux_slave_transfer_request_setup + UX_SETUP_VALUE + 1);
112 
113     /* Set the direction to OUT.  */
114     transfer_request -> ux_slave_transfer_request_phase =  UX_TRANSFER_PHASE_DATA_OUT;
115 
116     /* Prepare the event data payload from the hid event structure.  Get a pointer to the buffer area.  */
117     buffer =  transfer_request -> ux_slave_transfer_request_data_pointer;
118 
119     /* Initialize event fields.  */
120     hid_event.ux_device_class_hid_event_report_id   = report_id;
121     hid_event.ux_device_class_hid_event_report_type = report_type;
122     hid_event.ux_device_class_hid_event_length      = UX_DEVICE_CLASS_HID_EVENT_BUFFER_LENGTH;
123 
124     /* If it's input report without ID try to get it from event queue head.  */
125     if (report_type == UX_DEVICE_CLASS_HID_REPORT_TYPE_INPUT &&
126         hid -> ux_device_class_hid_report_id != UX_TRUE)
127 
128         /* Check if we have an event to report.  */
129         status = _ux_device_class_hid_event_get(hid, &hid_event);
130 
131     /* Try to get event from application callback.  */
132     else
133     {
134 
135         /* Let application fill event.  */
136         if (hid -> ux_device_class_hid_get_callback != UX_NULL)
137             status = hid -> ux_device_class_hid_get_callback(hid, &hid_event);
138     }
139 
140     if (status == UX_SUCCESS)
141     {
142 
143         /* Get the length to send back to the host.  */
144         if (host_length < hid_event.ux_device_class_hid_event_length)
145             hid_event_length =  host_length;
146         else
147             hid_event_length =  hid_event.ux_device_class_hid_event_length;
148         if (hid_event_length > UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH)
149             hid_event_length = UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH;
150 
151         /* First reset it.  */
152         _ux_utility_memory_set(buffer, 0, hid_event_length); /* Use case of memset is verified. */
153 
154         /* Copy the event buffer into the target buffer.  */
155         _ux_utility_memory_copy(buffer, hid_event.ux_device_class_hid_event_buffer, hid_event_length); /* Use case of memcpy is verified. */
156     }
157     else
158     {
159 
160         /* There's no event, so send back zero'd memory.  */
161 
162         /* Get the length to send back to the host.  */
163         if (host_length < UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH)
164             hid_event_length =  host_length;
165         else
166             hid_event_length =  UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH;
167 
168         /* Reset it.  */
169         _ux_utility_memory_set(buffer, 0, hid_event_length); /* Use case of memset is verified. */
170     }
171 
172     /* We can send the report.  */
173     status =  _ux_device_stack_transfer_request(transfer_request, hid_event_length, host_length);
174 
175     /* Return the status to the caller.  */
176     return(status);
177 }
178 
179