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_hcd_ohci.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_hcd_ohci_regular_td_obtain                      PORTABLE C      */
38 /*                                                           6.1.11       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*     This function obtains a free TD from the regular TD list.          */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    hcd_ohci                              Pointer to OHCI controller    */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    *UX_OHCI_TD                           Regular TD pointer            */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    _ux_utility_memory_set                Set memory block              */
58 /*    _ux_host_mutex_on                     Get protection mutex          */
59 /*    _ux_host_mutex_off                    Release protection mutex      */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    OHCI Controller Driver                                              */
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 /*                                            verified memset and memcpy  */
72 /*                                            cases,                      */
73 /*                                            resulting in version 6.1    */
74 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
75 /*                                            fixed standalone compile,   */
76 /*                                            resulting in version 6.1.11 */
77 /*                                                                        */
78 /**************************************************************************/
_ux_hcd_ohci_regular_td_obtain(UX_HCD_OHCI * hcd_ohci)79 UX_OHCI_TD  *_ux_hcd_ohci_regular_td_obtain(UX_HCD_OHCI *hcd_ohci)
80 {
81 
82 UX_OHCI_TD      *td;
83 ULONG           td_index;
84 
85 
86     /* Set the Mutex as this is a critical section.  */
87     _ux_host_mutex_on(&_ux_system -> ux_system_mutex);
88 
89     /* Start the search from the beginning of the regular TD list.  */
90     td =  hcd_ohci -> ux_hcd_ohci_td_list;
91 
92     for (td_index = 0; td_index < _ux_system_host -> ux_system_host_max_td; td_index++)
93     {
94 
95         /* Check the TD status, a free TD is marked with the UNUSED flag.  */
96         if (td -> ux_ohci_td_status == UX_UNUSED)
97         {
98 
99             /* The TD may have been used, so we reset all fields.  */
100             _ux_utility_memory_set(td, 0, sizeof(UX_OHCI_TD)); /* Use case of memset is verified. */
101 
102             /* This TD is now marked as USED.  */
103             td -> ux_ohci_td_status =  UX_USED;
104 
105             /* Release the protection.  */
106             _ux_host_mutex_off(&_ux_system -> ux_system_mutex);
107 
108             /* Return TD pointer - success!  */
109             return(td);
110         }
111 
112         /* Move to the next TD.  */
113         td++;
114     }
115 
116     /* There is no available TD in the TD list.  */
117 
118     /* Release protection.  */
119     _ux_host_mutex_off(&_ux_system -> ux_system_mutex);
120 
121     /* Return NULL to caller.  */
122     return(UX_NULL);
123 }
124 
125