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 /**   OHCI Controller Driver                                              */
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_stack.h"
30 #include "ux_hcd_ohci.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_hcd_ohci_asynchronous_endpoint_destroy          PORTABLE C      */
38 /*                                                           6.1.11       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*     This function will destroy an asynchronous endpoint. The control   */
46 /*     and bulk endpoints fall into this category.                        */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    hcd_ohci                              Pointer to OHCI HCD           */
51 /*    endpoint                              Pointer to endpoint           */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_hcd_ohci_register_read            Read OHCI register            */
60 /*    _ux_hcd_ohci_register_write           Write OHCI register           */
61 /*    _ux_utility_virtual_address           Get virtual address           */
62 /*    _ux_utility_delay_ms                  Delay ms                      */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    OHCI Controller Driver                                              */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
73 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            fixed physical and virtual  */
75 /*                                            address conversion,         */
76 /*                                            resulting in version 6.1    */
77 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
78 /*                                            fixed an addressing issue,  */
79 /*                                            resulting in version 6.1.11 */
80 /*                                                                        */
81 /**************************************************************************/
_ux_hcd_ohci_asynchronous_endpoint_destroy(UX_HCD_OHCI * hcd_ohci,UX_ENDPOINT * endpoint)82 UINT  _ux_hcd_ohci_asynchronous_endpoint_destroy(UX_HCD_OHCI *hcd_ohci, UX_ENDPOINT *endpoint)
83 {
84 
85 UX_OHCI_ED      *ed;
86 UX_OHCI_ED      *previous_ed;
87 UX_OHCI_ED      *next_ed;
88 UX_OHCI_TD      *tail_td;
89 UX_OHCI_TD      *head_td;
90 ULONG           value_td;
91 ULONG           ohci_register;
92 
93 
94     /* From the endpoint container fetch the OHCI ED descriptor.  */
95     ed =  (UX_OHCI_ED*) endpoint -> ux_endpoint_ed;
96 
97     /* Check if this physical endpoint has been initialized properly!  */
98     if (ed == UX_NULL)
99     {
100 
101         /* Error trap. */
102         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_HCD, UX_ENDPOINT_HANDLE_UNKNOWN);
103 
104         /* If trace is enabled, insert this event into the trace buffer.  */
105         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, endpoint, 0, 0, UX_TRACE_ERRORS, 0, 0)
106 
107         return(UX_ENDPOINT_HANDLE_UNKNOWN);
108     }
109 
110     /* The endpoint may be active. If so, set the skip bit.  */
111     ed -> ux_ohci_ed_dw0 |=  UX_OHCI_ED_SKIP;
112 
113     /* Wait for the controller to finish the current frame processing.  */
114     _ux_utility_delay_ms(1);
115 
116     /* Get the previous ED in the list for this ED.  */
117     previous_ed =  ed -> ux_ohci_ed_previous_ed;
118 
119     /* Get the next ED in the list for this ED.  */
120     next_ed =  ed -> ux_ohci_ed_next_ed;
121 
122     /* If the previous ED is NULL, we are at trying to remove the head ED.  */
123     if (previous_ed == UX_NULL)
124     {
125 
126         switch ((endpoint -> ux_endpoint_descriptor.bmAttributes) & UX_MASK_ENDPOINT_TYPE)
127         {
128 
129         case UX_CONTROL_ENDPOINT:
130 
131             /* Check if the next control endpoint is NULL.  */
132             if (next_ed == UX_NULL)
133             {
134 
135                 ohci_register =   _ux_hcd_ohci_register_read(hcd_ohci, OHCI_HC_CONTROL);
136                 ohci_register &=  ~OHCI_HC_CR_CLE;
137                 _ux_hcd_ohci_register_write(hcd_ohci, OHCI_HC_CONTROL, ohci_register);
138             }
139 
140             /* Store the new endpoint in the Control list.  */
141             _ux_hcd_ohci_register_write(hcd_ohci, OHCI_HC_CONTROL_HEAD_ED, (ULONG) next_ed);
142             break;
143 
144 
145         case UX_BULK_ENDPOINT:
146 
147             /* Check if the next bulk endpoint is NULL.  */
148             if (next_ed == UX_NULL)
149             {
150 
151                 ohci_register =   _ux_hcd_ohci_register_read(hcd_ohci, OHCI_HC_CONTROL);
152                 ohci_register &=  ~OHCI_HC_CR_BLE;
153                 _ux_hcd_ohci_register_write(hcd_ohci, OHCI_HC_CONTROL, ohci_register);
154             }
155 
156             /* Store the new endpoint in the Bulk list */
157             _ux_hcd_ohci_register_write(hcd_ohci, OHCI_HC_BULK_HEAD_ED, (ULONG) next_ed);
158             break;
159         }
160     }
161     else
162     {
163 
164         /* The previous ED points now to the ED after the ED we are removing.  */
165         previous_ed -> ux_ohci_ed_next_ed =  next_ed;
166     }
167 
168     /* Update the previous ED pointer in the next ED if exists.  */
169     if (next_ed != UX_NULL)
170     {
171         next_ed = _ux_utility_virtual_address(next_ed);
172         next_ed -> ux_ohci_ed_previous_ed =  previous_ed;
173     }
174 
175     /* Ensure that the potential Halt bit is removed in the head ED.  */
176     value_td =  (ULONG) _ux_utility_virtual_address(ed -> ux_ohci_ed_head_td) & UX_OHCI_ED_MASK_TD;
177     head_td =   (UX_OHCI_TD *) value_td;
178 
179     /* Remove all the tds from this ED and leave the head and tail pointing
180        to the dummy TD.  */
181     tail_td =  _ux_utility_virtual_address(ed -> ux_ohci_ed_tail_td);
182 
183     /* Free all tds attached to the ED */
184     while (head_td != tail_td)
185     {
186 
187         /* Update the head TD with the next TD.  */
188         ed -> ux_ohci_ed_head_td =  head_td -> ux_ohci_td_next_td;
189 
190         /* Mark the current head TD as free.  */
191         head_td -> ux_ohci_td_status =  UX_UNUSED;
192 
193         /* Now the new head TD is the next TD in the chain.  */
194         head_td =  _ux_utility_virtual_address(ed -> ux_ohci_ed_head_td);
195     }
196 
197     /* We need to free the dummy TD that was attached to the ED.  */
198     tail_td -> ux_ohci_td_status =  UX_UNUSED;
199 
200 
201     /* Now we can safely make the ED free.  */
202     ed -> ux_ohci_ed_status =  UX_UNUSED;
203 
204     /* Return successful completion.  */
205     return(UX_SUCCESS);
206 }
207 
208