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 |
|
|
#if !defined(UX_DEVICE_STANDALONE) |
34 |
|
|
/**************************************************************************/ |
35 |
|
|
/* */ |
36 |
|
|
/* FUNCTION RELEASE */ |
37 |
|
|
/* */ |
38 |
|
|
/* _ux_device_class_hid_interrupt_thread PORTABLE C */ |
39 |
|
|
/* 6.3.0 */ |
40 |
|
|
/* AUTHOR */ |
41 |
|
|
/* */ |
42 |
|
|
/* Chaoqiong Xiao, Microsoft Corporation */ |
43 |
|
|
/* */ |
44 |
|
|
/* DESCRIPTION */ |
45 |
|
|
/* */ |
46 |
|
|
/* This function is the thread of the hid interrupt (IN) endpoint */ |
47 |
|
|
/* */ |
48 |
|
|
/* It's for RTOS mode. */ |
49 |
|
|
/* */ |
50 |
|
|
/* INPUT */ |
51 |
|
|
/* */ |
52 |
|
|
/* hid_class Address of hid class */ |
53 |
|
|
/* container */ |
54 |
|
|
/* */ |
55 |
|
|
/* OUTPUT */ |
56 |
|
|
/* */ |
57 |
|
|
/* None */ |
58 |
|
|
/* */ |
59 |
|
|
/* CALLS */ |
60 |
|
|
/* */ |
61 |
|
|
/* _ux_utility_event_flags_get Get event flags */ |
62 |
|
|
/* _ux_device_class_hid_event_get Get HID event */ |
63 |
|
|
/* _ux_device_stack_transfer_request Request transfer */ |
64 |
|
|
/* _ux_utility_memory_copy Copy memory */ |
65 |
|
|
/* _ux_device_thread_suspend Suspend thread */ |
66 |
|
|
/* */ |
67 |
|
|
/* CALLED BY */ |
68 |
|
|
/* */ |
69 |
|
|
/* ThreadX */ |
70 |
|
|
/* */ |
71 |
|
|
/**************************************************************************/ |
72 |
|
139 |
VOID _ux_device_class_hid_interrupt_thread(ULONG hid_class) |
73 |
|
|
{ |
74 |
|
|
|
75 |
|
|
UX_SLAVE_CLASS *class_ptr; |
76 |
|
|
UX_SLAVE_CLASS_HID *hid; |
77 |
|
|
UX_SLAVE_DEVICE *device; |
78 |
|
|
UX_SLAVE_TRANSFER *transfer_request_in; |
79 |
|
|
UX_DEVICE_CLASS_HID_EVENT *hid_event; |
80 |
|
|
UINT status; |
81 |
|
|
UCHAR *buffer; |
82 |
|
|
ULONG actual_flags; |
83 |
|
|
|
84 |
|
|
|
85 |
|
|
/* Cast properly the hid instance. */ |
86 |
|
139 |
UX_THREAD_EXTENSION_PTR_GET(class_ptr, UX_SLAVE_CLASS, hid_class) |
87 |
|
|
|
88 |
|
|
/* Get the hid instance from this class container. */ |
89 |
|
139 |
hid = (UX_SLAVE_CLASS_HID *) class_ptr -> ux_slave_class_instance; |
90 |
|
|
|
91 |
|
|
/* Get the pointer to the device. */ |
92 |
|
139 |
device = &_ux_system_slave -> ux_system_slave_device; |
93 |
|
|
|
94 |
|
|
/* This thread runs forever but can be suspended or resumed. */ |
95 |
|
|
while(1) |
96 |
|
|
{ |
97 |
|
|
|
98 |
|
|
/* All HID events are on the interrupt endpoint IN, from the host. */ |
99 |
|
140 |
transfer_request_in = &hid -> ux_device_class_hid_interrupt_endpoint -> ux_slave_endpoint_transfer_request; |
100 |
|
|
|
101 |
|
|
/* As long as the device is in the CONFIGURED state. */ |
102 |
✓✓ |
1060 |
while (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED) |
103 |
|
|
{ |
104 |
|
|
|
105 |
|
|
/* Wait until we have a event sent by the application |
106 |
|
|
or a change in the idle state to send last or empty report. */ |
107 |
|
1058 |
status = _ux_utility_event_flags_get(&hid -> ux_device_class_hid_event_flags_group, |
108 |
|
|
UX_DEVICE_CLASS_HID_EVENTS_MASK, UX_OR_CLEAR, &actual_flags, |
109 |
|
|
hid -> ux_device_class_hid_event_wait_timeout); |
110 |
|
|
|
111 |
|
|
/* If there is no event, check if we have timeout defined. */ |
112 |
✓✓ |
927 |
if (status == UX_NO_EVENTS) |
113 |
|
|
{ |
114 |
|
|
|
115 |
|
|
/* There is no event exists on timeout, insert last. */ |
116 |
|
|
|
117 |
|
|
/* Check if no request been ready. */ |
118 |
✓✓ |
70 |
if (transfer_request_in -> ux_slave_transfer_request_requested_length == 0) |
119 |
|
|
{ |
120 |
|
|
|
121 |
|
|
/* Assume the request use whole interrupt transfer payload. */ |
122 |
|
1 |
transfer_request_in -> ux_slave_transfer_request_requested_length = |
123 |
|
1 |
transfer_request_in -> ux_slave_transfer_request_transfer_length; |
124 |
|
|
|
125 |
|
|
#if (UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1) && defined(UX_DEVICE_CLASS_HID_ZERO_COPY) |
126 |
|
|
|
127 |
|
|
/* Restore endpoint buffer (never touched, filled with zeros). */ |
128 |
|
|
transfer_request_in -> ux_slave_transfer_request_data_pointer = |
129 |
|
|
UX_DEVICE_CLASS_HID_INTERRUPTIN_BUFFER(hid); |
130 |
|
|
#else |
131 |
|
|
|
132 |
|
|
/* Set the data to zeros. */ |
133 |
|
1 |
_ux_utility_memory_set( |
134 |
|
1 |
transfer_request_in -> ux_slave_transfer_request_data_pointer, 0, |
135 |
|
|
transfer_request_in -> ux_slave_transfer_request_requested_length); /* Use case of memset is verified. */ |
136 |
|
|
#endif |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
/* Send the request to the device controller. */ |
140 |
|
70 |
status = _ux_device_stack_transfer_request(transfer_request_in, |
141 |
|
|
transfer_request_in -> ux_slave_transfer_request_requested_length, |
142 |
|
|
transfer_request_in -> ux_slave_transfer_request_requested_length); |
143 |
|
|
|
144 |
|
|
/* Check error code. We don't want to invoke the error callback |
145 |
|
|
if the device was disconnected, since that's expected. */ |
146 |
✓✓✓✓
|
70 |
if (status != UX_SUCCESS && status != UX_TRANSFER_BUS_RESET) |
147 |
|
|
|
148 |
|
|
/* Error trap. */ |
149 |
|
1 |
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, status); |
150 |
|
|
|
151 |
|
|
/* Next: check events. */ |
152 |
|
70 |
continue; |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
/* Check the completion code. */ |
156 |
✓✓ |
857 |
if (status != UX_SUCCESS) |
157 |
|
|
{ |
158 |
|
|
|
159 |
|
|
/* Error trap. */ |
160 |
|
1 |
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, status); |
161 |
|
|
|
162 |
|
|
/* Do not proceed. */ |
163 |
|
1 |
return; |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
|
167 |
|
|
/* Check if we have an event to report. */ |
168 |
✓✓ |
2072 |
while (_ux_device_class_hid_event_check(hid, &hid_event) == UX_SUCCESS) |
169 |
|
|
{ |
170 |
|
|
|
171 |
|
|
#if (UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1) && defined(UX_DEVICE_CLASS_HID_ZERO_COPY) |
172 |
|
|
|
173 |
|
|
/* Directly use the event buffer for transfer. */ |
174 |
|
|
buffer = hid_event -> ux_device_class_hid_event_buffer; |
175 |
|
|
transfer_request_in -> ux_slave_transfer_request_data_pointer = buffer; |
176 |
|
|
#else |
177 |
|
|
/* Prepare the event data payload from the hid event structure. Get a pointer to the buffer area. */ |
178 |
|
1222 |
buffer = transfer_request_in -> ux_slave_transfer_request_data_pointer; |
179 |
|
|
|
180 |
|
|
/* Copy the event buffer into the target buffer. */ |
181 |
|
1222 |
_ux_utility_memory_copy(buffer, UX_DEVICE_CLASS_HID_EVENT_BUFFER(hid_event), hid_event -> ux_device_class_hid_event_length); /* Use case of memcpy is verified. */ |
182 |
|
|
#endif |
183 |
|
|
|
184 |
|
|
/* Send the request to the device controller. */ |
185 |
|
1222 |
status = _ux_device_stack_transfer_request(transfer_request_in, hid_event -> ux_device_class_hid_event_length, |
186 |
|
1222 |
hid_event -> ux_device_class_hid_event_length); |
187 |
|
|
|
188 |
|
|
/* The queue tail is handled and should be freed. */ |
189 |
|
1216 |
_ux_device_class_hid_event_free(hid); |
190 |
|
|
|
191 |
|
|
/* Check error code. We don't want to invoke the error callback |
192 |
|
|
if the device was disconnected, since that's expected. */ |
193 |
✓✓✓✓
|
1216 |
if (status != UX_SUCCESS && status != UX_TRANSFER_BUS_RESET) |
194 |
|
|
|
195 |
|
|
/* Error trap. */ |
196 |
|
2 |
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, status); |
197 |
|
|
} |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
/* We need to suspend ourselves. We will be resumed by the device enumeration module. */ |
201 |
|
2 |
_ux_device_thread_suspend(&class_ptr -> ux_slave_class_thread); |
202 |
|
|
} |
203 |
|
|
} |
204 |
|
|
#endif /* !UX_DEVICE_STANDALONE */ |