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 /**                                                                       */
16 /** USBX Component                                                        */
17 /**                                                                       */
18 /**   ASIX Class                                                          */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 /* Include necessary system files.  */
25 
26 #define UX_SOURCE_CODE
27 
28 #include "ux_api.h"
29 #include "ux_host_class_asix.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_asix_interrupt_notification          PORTABLE C      */
38 /*                                                           6.2.0        */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function is called by the stack when an interrupt packet as    */
46 /*    been received.                                                      */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    transfer_request                      Pointer to transfer request   */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    None                                                                */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _ux_host_semaphore_put                Put semaphore                 */
59 /*    _ux_host_stack_transfer_request       Transfer request              */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    USBX stack                                                          */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
70 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            resulting in version 6.1    */
72 /*  10-15-2021     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            use pre-calculated value    */
74 /*                                            instead of wMaxPacketSize,  */
75 /*                                            resulting in version 6.1.9  */
76 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            refined macros names,       */
78 /*                                            resulting in version 6.1.10 */
79 /*  10-31-2022     Chaoqiong Xiao           Modified comment(s),          */
80 /*                                            refined link up/down flow,  */
81 /*                                            refined interrupt flow,     */
82 /*                                            resulting in version 6.2.0  */
83 /*                                                                        */
84 /**************************************************************************/
_ux_host_class_asix_interrupt_notification(UX_TRANSFER * transfer_request)85 VOID  _ux_host_class_asix_interrupt_notification(UX_TRANSFER *transfer_request)
86 {
87 
88 UX_HOST_CLASS_ASIX                      *asix;
89 
90     /* Get the class instance for this transfer request.  */
91     asix =  (UX_HOST_CLASS_ASIX *) transfer_request -> ux_transfer_request_class_instance;
92 
93     /* Check the state of the transfer.  If there is an error, we do not proceed with this notification.  */
94     if (transfer_request -> ux_transfer_request_completion_code != UX_SUCCESS)
95 
96         /* We do not proceed.  */
97         return;
98 
99     /* Check if the class is in shutdown.  */
100     if (asix -> ux_host_class_asix_state ==  UX_HOST_CLASS_INSTANCE_SHUTDOWN)
101 
102         /* We do not proceed.  */
103         return;
104 
105     /* Increment the notification count.   */
106     asix -> ux_host_class_asix_notification_count++;
107 
108     /* Ensure the length of our interrupt pipe data is correct.  */
109     if (transfer_request -> ux_transfer_request_actual_length ==
110             transfer_request -> ux_transfer_request_requested_length)
111     {
112 
113         /* Check if the first byte is a interrupt packet signature.  */
114         if (*(transfer_request -> ux_transfer_request_data_pointer + UX_HOST_CLASS_ASIX_INTERRUPT_SIGNATURE_OFFSET) ==
115                 UX_HOST_CLASS_ASIX_INTERRUPT_SIGNATURE_VALUE)
116         {
117 
118             /* Explore the content of the interrupt packet. We treat the link up/down flag here.  */
119             if (*(transfer_request -> ux_transfer_request_data_pointer + UX_HOST_CLASS_ASIX_INTERRUPT_STATE_OFFSET) &
120                     UX_HOST_CLASS_ASIX_INTERRUPT_STATE_PPLS)
121             {
122 
123                 /* Link is up. See if we know about that.  */
124                 if (asix -> ux_host_class_asix_link_state != UX_HOST_CLASS_ASIX_LINK_STATE_UP &&
125                     asix -> ux_host_class_asix_link_state != UX_HOST_CLASS_ASIX_LINK_STATE_PENDING_UP)
126                 {
127 
128                     /* Memorize the new link state.  */
129                     asix -> ux_host_class_asix_link_state = UX_HOST_CLASS_ASIX_LINK_STATE_PENDING_UP;
130 
131                     /* We need to inform the asix thread of this change.  */
132                     _ux_host_semaphore_put(&asix -> ux_host_class_asix_interrupt_notification_semaphore);
133 
134                     /* Reactivate interrupt pipe after link up handled.  */
135                     return;
136                 }
137             }
138             else
139             {
140 
141                 /* Link is down. See if we know about that.  */
142                 if (asix -> ux_host_class_asix_link_state != UX_HOST_CLASS_ASIX_LINK_STATE_DOWN &&
143                     asix -> ux_host_class_asix_link_state != UX_HOST_CLASS_ASIX_LINK_STATE_PENDING_DOWN)
144                 {
145 
146                     /* Memorize the new link state.  */
147                     asix -> ux_host_class_asix_link_state = UX_HOST_CLASS_ASIX_LINK_STATE_PENDING_DOWN;
148 
149                     /* Abort possible pending bulk in requests.  */
150                     _ux_host_stack_endpoint_transfer_abort(asix -> ux_host_class_asix_bulk_in_endpoint);
151 
152                     /* We need to inform the asix thread of this change.  */
153                     _ux_host_semaphore_put(&asix -> ux_host_class_asix_interrupt_notification_semaphore);
154 
155                     /* Reactivate interrupt pipe after link down handled.  */
156                     return;
157                 }
158             }
159         }
160     }
161 
162     /* Reactivate the ASIX interrupt pipe.  */
163     _ux_host_stack_transfer_request(transfer_request);
164 
165     /* If trace is enabled, insert this event into the trace buffer.  */
166     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_ASIX_INTERRUPT_NOTIFICATION, asix, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
167 
168     /* Return to caller.  */
169     return;
170 }
171 
172