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 /**   Host Stack                                                          */
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 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_stack_hcd_register                         PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function registers a USB controller driver with the USBX stack */
45 /*    and invokes the HCD driver's initialization function.               */
46 /*                                                                        */
47 /*    Note: The C string of hcd_name must be NULL-terminated and the      */
48 /*    length of it (without the NULL-terminator itself) must be no larger */
49 /*    than UX_MAX_HCD_NAME_LENGTH.                                        */
50 /*                                                                        */
51 /*  INPUT                                                                 */
52 /*                                                                        */
53 /*    hcd_name                              Name of HCD to register       */
54 /*    hcd_entry_function                    Entry function of HCD driver  */
55 /*    hcd_param1                            Parameter 1 of HCD            */
56 /*    hcd_param2                            Parameter 2 of HCD            */
57 /*                                                                        */
58 /*  OUTPUT                                                                */
59 /*                                                                        */
60 /*    Completion Status                                                   */
61 /*                                                                        */
62 /*                                                                        */
63 /*  CALLS                                                                 */
64 /*                                                                        */
65 /*    _ux_utility_string_length_check       Check and return C string     */
66 /*                                          length if no error            */
67 /*    _ux_utility_memory_copy               Copy name into HCD structure  */
68 /*    (hcd_init_function)                   Init function of HCD driver   */
69 /*                                                                        */
70 /*  CALLED BY                                                             */
71 /*                                                                        */
72 /*    Application                                                         */
73 /*                                                                        */
74 /*  RELEASE HISTORY                                                       */
75 /*                                                                        */
76 /*    DATE              NAME                      DESCRIPTION             */
77 /*                                                                        */
78 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
79 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
80 /*                                            optimized based on compile  */
81 /*                                            definitions, verified       */
82 /*                                            memset and memcpy cases,    */
83 /*                                            resulting in version 6.1    */
84 /*                                                                        */
85 /**************************************************************************/
_ux_host_stack_hcd_register(UCHAR * hcd_name,UINT (* hcd_init_function)(struct UX_HCD_STRUCT *),ULONG hcd_param1,ULONG hcd_param2)86 UINT  _ux_host_stack_hcd_register(UCHAR *hcd_name,
87                                     UINT (*hcd_init_function)(struct UX_HCD_STRUCT *), ULONG hcd_param1, ULONG hcd_param2)
88 {
89 
90 UX_HCD      *hcd;
91 UINT        status;
92 #if !defined(UX_NAME_REFERENCED_BY_POINTER)
93 UINT        hcd_name_length =  0;
94 #endif
95 #if UX_MAX_HCD > 1
96 ULONG       hcd_index;
97 #endif
98 
99 
100 
101     /* If trace is enabled, insert this event into the trace buffer.  */
102     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_HCD_REGISTER, hcd_name, hcd_param1, hcd_param2, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0)
103 
104 #if !defined(UX_NAME_REFERENCED_BY_POINTER)
105     /* Get the length of the class name (exclude null-terminator).  */
106     status =  _ux_utility_string_length_check(hcd_name, &hcd_name_length, UX_MAX_HCD_NAME_LENGTH);
107     if (status)
108         return(status);
109 #endif
110 
111     /* Get HCD.  */
112     hcd =  _ux_system_host -> ux_system_host_hcd_array;
113 
114 #if UX_MAX_HCD > 1
115     /* We need to parse the controller driver table to find an empty spot.  */
116     for(hcd_index = 0; hcd_index < _ux_system_host -> ux_system_host_max_hcd; hcd_index++)
117     {
118 #endif
119 
120         /* Is this slot available?  */
121         if(hcd -> ux_hcd_status == UX_UNUSED)
122         {
123 
124             /* Yes, setup the new HCD entry.  */
125 
126 #if defined(UX_NAME_REFERENCED_BY_POINTER)
127             hcd -> ux_hcd_name = (const UCHAR *)hcd_name;
128 #else
129 
130             /* Initialize the array of the new controller with its name (include null-terminator).  */
131             _ux_utility_memory_copy(hcd -> ux_hcd_name, hcd_name, hcd_name_length + 1); /* Use case of memcpy is verified. */
132 #endif
133 
134             /* Store the hardware resources of the controller */
135             hcd -> ux_hcd_io =   hcd_param1;
136             hcd -> ux_hcd_irq =  hcd_param2;
137 
138             /* This controller is now used */
139             hcd -> ux_hcd_status =  UX_USED;
140 
141             /* And we have one new controller registered.  */
142             _ux_system_host -> ux_system_host_registered_hcd++;
143 
144             /* We are now calling the HCD driver initialization.  */
145             status =  hcd_init_function(hcd);
146 
147             /* Return the completion status to the caller.  */
148             return(status);
149         }
150 #if UX_MAX_HCD > 1
151         /* Try the next HCD structure */
152         hcd++;
153     }
154 #endif
155 
156     /* We have exhausted the array of the HCDs, return an error.  */
157     return(UX_MEMORY_INSUFFICIENT);
158 }
159 
160