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