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 /** */
15 /** USBX Component */
16 /** */
17 /** Device HID Class */
18 /** */
19 /**************************************************************************/
20 /**************************************************************************/
21
22 #define UX_SOURCE_CODE
23
24
25 /* Include necessary system files. */
26
27 #include "ux_api.h"
28 #include "ux_device_class_hid.h"
29 #include "ux_device_stack.h"
30
31
32 /**************************************************************************/
33 /* */
34 /* FUNCTION RELEASE */
35 /* */
36 /* _ux_device_class_hid_report_set PORTABLE C */
37 /* 6.3.0 */
38 /* AUTHOR */
39 /* */
40 /* Chaoqiong Xiao, Microsoft Corporation */
41 /* */
42 /* DESCRIPTION */
43 /* */
44 /* This function gets from the control pipe a report to be set from */
45 /* the host. */
46 /* */
47 /* INPUT */
48 /* */
49 /* hid Pointer to hid instance */
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 /* Device HID */
65 /* */
66 /* RELEASE HISTORY */
67 /* */
68 /* DATE NAME DESCRIPTION */
69 /* */
70 /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */
71 /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */
72 /* verified memset and memcpy */
73 /* cases, */
74 /* resulting in version 6.1 */
75 /* 10-31-2023 Chaoqiong Xiao Modified comment(s), */
76 /* resulting in version 6.3.0 */
77 /* */
78 /**************************************************************************/
_ux_device_class_hid_report_set(UX_SLAVE_CLASS_HID * hid,ULONG descriptor_type,ULONG request_index,ULONG host_length)79 UINT _ux_device_class_hid_report_set(UX_SLAVE_CLASS_HID *hid, ULONG descriptor_type,
80 ULONG request_index, ULONG host_length)
81 {
82
83 UX_SLAVE_DEVICE *device;
84 UX_SLAVE_TRANSFER *transfer_request;
85 UX_SLAVE_ENDPOINT *endpoint;
86 UX_SLAVE_CLASS_HID_EVENT hid_event;
87 UCHAR *hid_buffer;
88
89 UX_PARAMETER_NOT_USED(request_index);
90 UX_PARAMETER_NOT_USED(host_length);
91
92 /* If trace is enabled, insert this event into the trace buffer. */
93 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)
94
95 /* Get the pointer to the device. */
96 device = &_ux_system_slave -> ux_system_slave_device;
97
98 /* Get the control endpoint associated with the device. */
99 endpoint = &device -> ux_slave_device_control_endpoint;
100
101 /* Get the pointer to the transfer request associated with the endpoint. */
102 transfer_request = &endpoint -> ux_slave_endpoint_transfer_request;
103
104 /* Set the event type to OUTPUT. */
105 hid_event.ux_device_class_hid_event_report_type = descriptor_type;
106
107 /* Get HID data address. */
108 hid_buffer = transfer_request -> ux_slave_transfer_request_data_pointer;
109
110 /* Check for report ID in this HID descriptor. */
111 if (hid -> ux_device_class_hid_report_id == UX_TRUE)
112 {
113 /* Set the report ID, First byte of data payload. */
114 hid_event.ux_device_class_hid_event_report_id = (ULONG) *hid_buffer;
115
116 /* Set the length = total length - report ID. */
117 hid_event.ux_device_class_hid_event_length = transfer_request -> ux_slave_transfer_request_actual_length -1;
118
119 /* Set HID data after report ID. */
120 hid_buffer++;
121 }
122
123 else
124 {
125 /* Set the report ID, not used here. */
126 hid_event.ux_device_class_hid_event_report_id = 0;
127
128 /* Set the length. */
129 hid_event.ux_device_class_hid_event_length = transfer_request -> ux_slave_transfer_request_actual_length;
130 }
131
132 /* Copy the buffer received from the host. Check for overflow. */
133 if (hid_event.ux_device_class_hid_event_length > UX_DEVICE_CLASS_HID_EVENT_BUFFER_LENGTH)
134
135 /* Overflow detected. */
136 hid_event.ux_device_class_hid_event_length = UX_DEVICE_CLASS_HID_EVENT_BUFFER_LENGTH;
137
138 /* Now we can safely copy the payload. */
139 _ux_utility_memory_copy(hid_event.ux_device_class_hid_event_buffer, hid_buffer,
140 hid_event.ux_device_class_hid_event_length); /* Use case of memcpy is verified. */
141
142 /* If there is a callback defined by the application, send the hid event to it. */
143 if (hid -> ux_device_class_hid_callback != UX_NULL)
144
145 /* Callback exists. */
146 hid -> ux_device_class_hid_callback(hid, &hid_event);
147
148 /* Return the status to the caller. */
149 return(UX_SUCCESS);
150 }
151