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_set                     PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function gets from the control pipe a report to be set from    */
46 /*    the host.                                                           */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    descriptor_type                       Descriptor type               */
51 /*    descriptor_index                      Index of descriptor           */
52 /*    host_length                           Length requested by host      */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    Completion Status                                                   */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _ux_utility_memory_copy               Memory copy                   */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    Application                                                         */
65 /*    Device Stack                                                        */
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 /*                                            verified memset and memcpy  */
74 /*                                            cases,                      */
75 /*                                            resulting in version 6.1    */
76 /*                                                                        */
77 /**************************************************************************/
_ux_device_class_hid_report_set(UX_SLAVE_CLASS_HID * hid,ULONG descriptor_type,ULONG request_index,ULONG host_length)78 UINT  _ux_device_class_hid_report_set(UX_SLAVE_CLASS_HID *hid, ULONG descriptor_type,
79                                             ULONG request_index, ULONG host_length)
80 {
81 
82 UX_SLAVE_DEVICE                 *device;
83 UX_SLAVE_TRANSFER               *transfer_request;
84 UX_SLAVE_ENDPOINT               *endpoint;
85 UX_SLAVE_CLASS_HID_EVENT        hid_event;
86 UCHAR                           *hid_buffer;
87 
88     UX_PARAMETER_NOT_USED(request_index);
89     UX_PARAMETER_NOT_USED(host_length);
90 
91     /* If trace is enabled, insert this event into the trace buffer.  */
92     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_HID_REPORT_SET, hid, descriptor_type, request_index, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
93 
94     /* Get the pointer to the device.  */
95     device =  &_ux_system_slave -> ux_system_slave_device;
96 
97     /* Get the control endpoint associated with the device.  */
98     endpoint =  &device -> ux_slave_device_control_endpoint;
99 
100     /* Get the pointer to the transfer request associated with the endpoint.  */
101     transfer_request =  &endpoint -> ux_slave_endpoint_transfer_request;
102 
103     /* Set the event type to OUTPUT.  */
104     hid_event.ux_device_class_hid_event_report_type =  descriptor_type;
105 
106     /* Get HID data address.  */
107     hid_buffer = transfer_request -> ux_slave_transfer_request_data_pointer;
108 
109     /* Check for report ID in this HID descriptor.  */
110     if (hid -> ux_device_class_hid_report_id == UX_TRUE)
111     {
112         /* Set the report ID, First byte of data payload.  */
113         hid_event.ux_device_class_hid_event_report_id = (ULONG) *hid_buffer;
114 
115         /* Set the length = total length - report ID. */
116         hid_event.ux_device_class_hid_event_length = transfer_request -> ux_slave_transfer_request_actual_length -1;
117 
118         /* Set HID data after report ID.  */
119         hid_buffer++;
120     }
121 
122     else
123     {
124         /* Set the report ID, not used here.  */
125         hid_event.ux_device_class_hid_event_report_id = 0;
126 
127         /* Set the length.  */
128         hid_event.ux_device_class_hid_event_length = transfer_request -> ux_slave_transfer_request_actual_length;
129     }
130 
131     /* Copy the buffer received from the host.  Check for overflow. */
132     if (hid_event.ux_device_class_hid_event_length > UX_DEVICE_CLASS_HID_EVENT_BUFFER_LENGTH)
133 
134         /* Overflow detected.  */
135         hid_event.ux_device_class_hid_event_length = UX_DEVICE_CLASS_HID_EVENT_BUFFER_LENGTH;
136 
137     /* Now we can safely copy the payload.  */
138     _ux_utility_memory_copy(hid_event.ux_device_class_hid_event_buffer, hid_buffer,
139                                 hid_event.ux_device_class_hid_event_length); /* Use case of memcpy is verified. */
140 
141     /* If there is a callback defined by the application, send the hid event to it.  */
142     if (hid -> ux_device_class_hid_callback != UX_NULL)
143 
144         /* Callback exists. */
145         hid -> ux_device_class_hid_callback(hid, &hid_event);
146 
147     /* Return the status to the caller.  */
148     return(UX_SUCCESS);
149 }
150 
151