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.1.12       */
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 /*                                                                        */
88 /**************************************************************************/
_ux_device_class_pima_interrupt_thread(ULONG pima_class)89 VOID  _ux_device_class_pima_interrupt_thread(ULONG pima_class)
90 {
91 
92 UX_SLAVE_CLASS_PIMA         *pima;
93 UX_SLAVE_DEVICE             *device;
94 UX_SLAVE_TRANSFER           *transfer_request_in;
95 UX_SLAVE_CLASS_PIMA_EVENT   pima_event;
96 UINT                        status;
97 UCHAR                       *buffer;
98 
99     /* Get the pima instance from the calling parameter.  */
100     UX_THREAD_EXTENSION_PTR_GET(pima, UX_SLAVE_CLASS_PIMA, pima_class)
101 
102     /* Allocate the event round robin buffer.  */
103     pima -> ux_device_class_pima_event_array =
104             _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);
105 
106     /* Check for successful allocation.  */
107     if (pima -> ux_device_class_pima_event_array == UX_NULL)
108     {
109         /* Return, no event management.  */
110         return;
111     }
112 
113     /* Allocate the head\tail and end of the round robin buffer.  */
114     pima -> ux_device_class_pima_event_array_head =  pima -> ux_device_class_pima_event_array;
115     pima -> ux_device_class_pima_event_array_tail =  pima -> ux_device_class_pima_event_array;
116     pima -> ux_device_class_pima_event_array_end  =  pima -> ux_device_class_pima_event_array + UX_DEVICE_CLASS_PIMA_MAX_EVENTS_QUEUE;
117 
118     /* This thread runs forever but can be suspended or resumed.  */
119     while(1)
120     {
121 
122         /* Get the pointer to the device.  */
123         device =  &_ux_system_slave -> ux_system_slave_device;
124 
125         /* All PIMA events are on the interrupt endpoint IN, from the host.  */
126         transfer_request_in =  &pima -> ux_device_class_pima_interrupt_endpoint -> ux_slave_endpoint_transfer_request;
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             /* Wait until something has awaken us.  */
133             status =  _ux_device_semaphore_get(&pima -> ux_device_class_pima_interrupt_thread_semaphore, UX_WAIT_FOREVER);
134 
135             /* Check the completion code. */
136             if (status != UX_SUCCESS)
137 
138                 /* Do not proceed.  */
139                 return;
140 
141             /* Check if we have an event to report.  */
142             status = _ux_device_class_pima_event_get(pima, &pima_event);
143 
144             /* We may have an event to report on the interrupt pipe.  */
145             if(status == UX_SUCCESS)
146             {
147 
148                 /* Prepare the event data payload from the pima event structure.  Get a pointer to the buffer area.  */
149                 buffer =  transfer_request_in -> ux_slave_transfer_request_data_pointer;
150 
151                 /* Put the length of the entire event payload.  */
152                 _ux_utility_long_put(buffer + UX_DEVICE_CLASS_PIMA_AEI_DATA_LENGTH, UX_DEVICE_CLASS_PIMA_AEI_MAX_LENGTH);
153 
154                 /* Put the type of packet (Event)   */
155                 _ux_utility_short_put(buffer + UX_DEVICE_CLASS_PIMA_AEI_TYPE, UX_DEVICE_CLASS_PIMA_CT_EVENT_BLOCK);
156 
157                 /* Put the type of event.   */
158                 _ux_utility_short_put(buffer + UX_DEVICE_CLASS_PIMA_AEI_EVENT_CODE, (USHORT)pima_event.ux_device_class_pima_event_code);
159 
160                 /* Put the transaction ID.  */
161                 _ux_utility_long_put(buffer + UX_DEVICE_CLASS_PIMA_AEI_TRANSACTION_ID, pima_event.ux_device_class_pima_event_transaction_id);
162 
163                 /* Put the value of parameter 1.   */
164                 _ux_utility_long_put(buffer + UX_DEVICE_CLASS_PIMA_AEI_PARAMETER_1, pima_event.ux_device_class_pima_event_parameter_1);
165 
166                 /* Put the value of parameter 2.   */
167                 _ux_utility_long_put(buffer + UX_DEVICE_CLASS_PIMA_AEI_PARAMETER_2, pima_event.ux_device_class_pima_event_parameter_3);
168 
169                 /* Put the value of parameter 3.   */
170                 _ux_utility_long_put(buffer + UX_DEVICE_CLASS_PIMA_AEI_PARAMETER_2, pima_event.ux_device_class_pima_event_parameter_3);
171 
172                 /* Send the request to the device controller.  */
173                 status =  _ux_device_stack_transfer_request(transfer_request_in, UX_DEVICE_CLASS_PIMA_AEI_MAX_LENGTH, UX_DEVICE_CLASS_PIMA_AEI_MAX_LENGTH);
174 
175                 /* Check error code. */
176                 if (status != UX_SUCCESS)
177 
178                     /* Error trap. */
179                     _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, status);
180 
181             }
182         }
183 
184     /* We need to suspend ourselves. We will be resumed by the device enumeration module.  */
185     _ux_device_thread_suspend(&pima -> ux_device_class_pima_interrupt_thread);
186     }
187 }
188 #endif
189