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 /** USBX Component                                                        */
15 /**                                                                       */
16 /**   Device PIMA Class                                                   */
17 /**                                                                       */
18 /**************************************************************************/
19 /**************************************************************************/
20 
21 #define UX_SOURCE_CODE
22 
23 
24 /* Include necessary system files.  */
25 
26 #include "ux_api.h"
27 #include "ux_device_class_pima.h"
28 #include "ux_device_stack.h"
29 
30 
31 #if !defined(UX_DEVICE_STANDALONE)
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_device_class_pima_interrupt_thread              PORTABLE C      */
37 /*                                                           6.3.0        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function is the thread of the pima interrupt endpoint          */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    pima_class                               Address of pima class      */
49 /*                                                container               */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    None                                                                */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    _ux_device_stack_transfer_request     Request transfer              */
58 /*    _ux_utility_memory_allocate           Allocate memory               */
59 /*    _ux_device_semaphore_get              Get semaphore                 */
60 /*    _ux_device_class_pima_event_get       Get PIMA event                */
61 /*    _ux_utility_long_put                  Put 32-bit value              */
62 /*    _ux_utility_short_put                 Put 16-bit value              */
63 /*    _ux_device_thread_suspend             Suspend thread                */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    ThreadX                                                             */
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 /*                                            resulting in version 6.1    */
76 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            refined macros names,       */
78 /*                                            added transaction ID,       */
79 /*                                            resulting in version 6.1.10 */
80 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
81 /*                                            fixed standalone compile,   */
82 /*                                            resulting in version 6.1.11 */
83 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
84 /*                                            fixed event message size,   */
85 /*                                            resulting in version 6.1.12 */
86 /*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
87 /*                                            improved INT EP management, */
88 /*                                            resulting in version 6.3.0  */
89 /*                                                                        */
90 /**************************************************************************/
_ux_device_class_pima_interrupt_thread(ULONG pima_class)91 VOID  _ux_device_class_pima_interrupt_thread(ULONG pima_class)
92 {
93 
94 UX_SLAVE_CLASS_PIMA         *pima;
95 UX_SLAVE_DEVICE             *device;
96 UX_SLAVE_TRANSFER           *transfer_request_in;
97 UX_SLAVE_CLASS_PIMA_EVENT   pima_event;
98 UINT                        status;
99 UCHAR                       *buffer;
100 
101     /* Get the pima instance from the calling parameter.  */
102     UX_THREAD_EXTENSION_PTR_GET(pima, UX_SLAVE_CLASS_PIMA, pima_class)
103 
104     /* Allocate the event round robin buffer.  */
105     pima -> ux_device_class_pima_event_array =
106             _ux_utility_memory_allocate_mulc_safe(UX_NO_ALIGN, UX_REGULAR_MEMORY, sizeof(UX_SLAVE_CLASS_PIMA_EVENT), UX_DEVICE_CLASS_PIMA_MAX_EVENTS_QUEUE);
107 
108     /* Check for successful allocation.  */
109     if (pima -> ux_device_class_pima_event_array == UX_NULL)
110     {
111         /* Return, no event management.  */
112         return;
113     }
114 
115     /* Allocate the head\tail and end of the round robin buffer.  */
116     pima -> ux_device_class_pima_event_array_head =  pima -> ux_device_class_pima_event_array;
117     pima -> ux_device_class_pima_event_array_tail =  pima -> ux_device_class_pima_event_array;
118     pima -> ux_device_class_pima_event_array_end  =  pima -> ux_device_class_pima_event_array + UX_DEVICE_CLASS_PIMA_MAX_EVENTS_QUEUE;
119 
120     /* This thread runs forever but can be suspended or resumed.  */
121     while(1)
122     {
123 
124         /* Get the pointer to the device.  */
125         device =  &_ux_system_slave -> ux_system_slave_device;
126 
127 
128         /* As long as the device is in the CONFIGURED state.  */
129         while (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED)
130         {
131 
132             /* All PIMA events are on the interrupt endpoint IN, from the host.  */
133             if (pima -> ux_device_class_pima_interrupt_endpoint == UX_NULL)
134                 break;
135             transfer_request_in =  &pima -> ux_device_class_pima_interrupt_endpoint -> ux_slave_endpoint_transfer_request;
136 
137             /* Wait until something has awaken us.  */
138             status =  _ux_device_semaphore_get(&pima -> ux_device_class_pima_interrupt_thread_semaphore, UX_WAIT_FOREVER);
139 
140             /* Check the completion code. */
141             if (status != UX_SUCCESS)
142 
143                 /* Do not proceed.  */
144                 return;
145 
146             /* Check if we have an event to report.  */
147             status = _ux_device_class_pima_event_get(pima, &pima_event);
148 
149             /* We may have an event to report on the interrupt pipe.  */
150             if(status == UX_SUCCESS)
151             {
152 
153                 /* Prepare the event data payload from the pima event structure.  Get a pointer to the buffer area.  */
154                 buffer =  transfer_request_in -> ux_slave_transfer_request_data_pointer;
155 
156                 /* Put the length of the entire event payload.  */
157                 _ux_utility_long_put(buffer + UX_DEVICE_CLASS_PIMA_AEI_DATA_LENGTH, UX_DEVICE_CLASS_PIMA_AEI_MAX_LENGTH);
158 
159                 /* Put the type of packet (Event)   */
160                 _ux_utility_short_put(buffer + UX_DEVICE_CLASS_PIMA_AEI_TYPE, UX_DEVICE_CLASS_PIMA_CT_EVENT_BLOCK);
161 
162                 /* Put the type of event.   */
163                 _ux_utility_short_put(buffer + UX_DEVICE_CLASS_PIMA_AEI_EVENT_CODE, (USHORT)pima_event.ux_device_class_pima_event_code);
164 
165                 /* Put the transaction ID.  */
166                 _ux_utility_long_put(buffer + UX_DEVICE_CLASS_PIMA_AEI_TRANSACTION_ID, pima_event.ux_device_class_pima_event_transaction_id);
167 
168                 /* Put the value of parameter 1.   */
169                 _ux_utility_long_put(buffer + UX_DEVICE_CLASS_PIMA_AEI_PARAMETER_1, pima_event.ux_device_class_pima_event_parameter_1);
170 
171                 /* Put the value of parameter 2.   */
172                 _ux_utility_long_put(buffer + UX_DEVICE_CLASS_PIMA_AEI_PARAMETER_2, pima_event.ux_device_class_pima_event_parameter_3);
173 
174                 /* Put the value of parameter 3.   */
175                 _ux_utility_long_put(buffer + UX_DEVICE_CLASS_PIMA_AEI_PARAMETER_2, pima_event.ux_device_class_pima_event_parameter_3);
176 
177                 /* Send the request to the device controller.  */
178                 status =  _ux_device_stack_transfer_request(transfer_request_in, UX_DEVICE_CLASS_PIMA_AEI_MAX_LENGTH, UX_DEVICE_CLASS_PIMA_AEI_MAX_LENGTH);
179 
180                 /* Check error code. */
181                 if (status != UX_SUCCESS)
182 
183                     /* Error trap. */
184                     _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, status);
185 
186             }
187         }
188 
189     /* We need to suspend ourselves. We will be resumed by the device enumeration module.  */
190     _ux_device_thread_suspend(&pima -> ux_device_class_pima_interrupt_thread);
191     }
192 }
193 #endif
194