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