1 |
|
|
/*************************************************************************** |
2 |
|
|
* Copyright (c) 2024 Microsoft Corporation |
3 |
|
|
* Copyright (c) 2026-present Eclipse ThreadX contributors |
4 |
|
|
* |
5 |
|
|
* This program and the accompanying materials are made available under the |
6 |
|
|
* terms of the MIT License which is available at |
7 |
|
|
* https://opensource.org/licenses/MIT. |
8 |
|
|
* |
9 |
|
|
* SPDX-License-Identifier: MIT |
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.3.0 */ |
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 |
|
|
/* 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_device_class_hid_event_get Get HID event */ |
61 |
|
|
/* _ux_device_stack_transfer_request Process transfer request */ |
62 |
|
|
/* _ux_utility_memory_set Set memory */ |
63 |
|
|
/* _ux_utility_memory_copy Copy memory */ |
64 |
|
|
/* */ |
65 |
|
|
/* CALLED BY */ |
66 |
|
|
/* */ |
67 |
|
|
/* Device HID */ |
68 |
|
|
/* */ |
69 |
|
|
/**************************************************************************/ |
70 |
|
17 |
UINT _ux_device_class_hid_report_get(UX_SLAVE_CLASS_HID *hid, ULONG descriptor_type, |
71 |
|
|
ULONG request_index, ULONG host_length) |
72 |
|
|
{ |
73 |
|
|
|
74 |
|
|
UX_SLAVE_DEVICE *device; |
75 |
|
|
UX_SLAVE_TRANSFER *transfer_request; |
76 |
|
|
UX_SLAVE_ENDPOINT *endpoint; |
77 |
|
|
UCHAR report_id; |
78 |
|
|
UCHAR report_type; |
79 |
|
|
UX_SLAVE_CLASS_HID_EVENT hid_event; |
80 |
|
|
ULONG hid_event_length; |
81 |
|
|
UCHAR *buffer; |
82 |
|
17 |
UINT status = UX_ERROR; |
83 |
|
|
|
84 |
|
|
UX_PARAMETER_NOT_USED(descriptor_type); |
85 |
|
|
UX_PARAMETER_NOT_USED(request_index); |
86 |
|
|
|
87 |
|
|
/* If trace is enabled, insert this event into the trace buffer. */ |
88 |
|
|
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) |
89 |
|
|
|
90 |
|
|
/* Get the pointer to the device. */ |
91 |
|
17 |
device = &_ux_system_slave -> ux_system_slave_device; |
92 |
|
|
|
93 |
|
|
/* Get the control endpoint associated with the device. */ |
94 |
|
17 |
endpoint = &device -> ux_slave_device_control_endpoint; |
95 |
|
|
|
96 |
|
|
/* Get the pointer to the transfer request associated with the endpoint. */ |
97 |
|
17 |
transfer_request = &endpoint -> ux_slave_endpoint_transfer_request; |
98 |
|
|
|
99 |
|
|
/* Get report ID (wValue.lower) and report type (wValue.higher). */ |
100 |
|
17 |
report_id = *(transfer_request -> ux_slave_transfer_request_setup + UX_SETUP_VALUE + 0); |
101 |
|
17 |
report_type = *(transfer_request -> ux_slave_transfer_request_setup + UX_SETUP_VALUE + 1); |
102 |
|
|
|
103 |
|
|
/* Set the direction to OUT. */ |
104 |
|
17 |
transfer_request -> ux_slave_transfer_request_phase = UX_TRANSFER_PHASE_DATA_OUT; |
105 |
|
|
|
106 |
|
|
/* Prepare the event data payload from the hid event structure. Get a pointer to the buffer area. */ |
107 |
|
17 |
buffer = transfer_request -> ux_slave_transfer_request_data_pointer; |
108 |
|
|
|
109 |
|
|
/* Initialize event fields. */ |
110 |
|
17 |
hid_event.ux_device_class_hid_event_report_id = report_id; |
111 |
|
17 |
hid_event.ux_device_class_hid_event_report_type = report_type; |
112 |
|
17 |
hid_event.ux_device_class_hid_event_length = UX_DEVICE_CLASS_HID_EVENT_BUFFER_LENGTH; |
113 |
|
|
|
114 |
|
|
/* If it's input report without ID try to get it from event queue head. */ |
115 |
✓✓ |
17 |
if (report_type == UX_DEVICE_CLASS_HID_REPORT_TYPE_INPUT && |
116 |
✓✓ |
13 |
hid -> ux_device_class_hid_report_id != UX_TRUE) |
117 |
|
|
|
118 |
|
|
/* Check if we have an event to report. */ |
119 |
|
12 |
status = _ux_device_class_hid_event_get(hid, &hid_event); |
120 |
|
|
|
121 |
|
|
/* Try to get event from application callback. */ |
122 |
|
|
else |
123 |
|
|
{ |
124 |
|
|
|
125 |
|
|
/* Let application fill event. */ |
126 |
✓✓ |
5 |
if (hid -> ux_device_class_hid_get_callback != UX_NULL) |
127 |
|
3 |
status = hid -> ux_device_class_hid_get_callback(hid, &hid_event); |
128 |
|
|
} |
129 |
|
|
|
130 |
✓✓ |
17 |
if (status == UX_SUCCESS) |
131 |
|
|
{ |
132 |
|
|
|
133 |
|
|
/* Get the length to send back to the host. */ |
134 |
✓✓ |
7 |
if (host_length < hid_event.ux_device_class_hid_event_length) |
135 |
|
2 |
hid_event_length = host_length; |
136 |
|
|
else |
137 |
|
5 |
hid_event_length = hid_event.ux_device_class_hid_event_length; |
138 |
✓✓ |
7 |
if (hid_event_length > UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH) |
139 |
|
1 |
hid_event_length = UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH; |
140 |
|
|
|
141 |
|
|
/* First reset it. */ |
142 |
|
7 |
_ux_utility_memory_set(buffer, 0, hid_event_length); /* Use case of memset is verified. */ |
143 |
|
|
|
144 |
|
|
/* Copy the event buffer into the target buffer. */ |
145 |
|
7 |
_ux_utility_memory_copy(buffer, hid_event.ux_device_class_hid_event_buffer, hid_event_length); /* Use case of memcpy is verified. */ |
146 |
|
|
} |
147 |
|
|
else |
148 |
|
|
{ |
149 |
|
|
|
150 |
|
|
/* There's no event, so send back zero'd memory. */ |
151 |
|
|
|
152 |
|
|
/* Get the length to send back to the host. */ |
153 |
✓✓ |
10 |
if (host_length < UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH) |
154 |
|
9 |
hid_event_length = host_length; |
155 |
|
|
else |
156 |
|
1 |
hid_event_length = UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH; |
157 |
|
|
|
158 |
|
|
/* Reset it. */ |
159 |
|
10 |
_ux_utility_memory_set(buffer, 0, hid_event_length); /* Use case of memset is verified. */ |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
/* We can send the report. */ |
163 |
|
17 |
status = _ux_device_stack_transfer_request(transfer_request, hid_event_length, host_length); |
164 |
|
|
|
165 |
|
|
/* Return the status to the caller. */ |
166 |
|
17 |
return(status); |
167 |
|
|
} |