GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: core/src/ux_host_stack_hcd_register.c Lines: 17 21 81.0 %
Date: 2026-03-06 18:57:10 Branches: 6 10 60.0 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 * Copyright (c) 2026-present Eclipse ThreadX contributors
4
 *
5
 * This program and the accompanying materials are made available under the
6
 * terms of the MIT License which is available at
7
 * https://opensource.org/licenses/MIT.
8
 *
9
 * SPDX-License-Identifier: MIT
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
/**************************************************************************/
75
376
UINT  _ux_host_stack_hcd_register(UCHAR *hcd_name,
76
                                    UINT (*hcd_init_function)(struct UX_HCD_STRUCT *), ULONG hcd_param1, ULONG hcd_param2)
77
{
78
79
UX_HCD      *hcd;
80
UINT        status;
81
#if !defined(UX_NAME_REFERENCED_BY_POINTER)
82
376
UINT        hcd_name_length =  0;
83
#endif
84
#if UX_MAX_HCD > 1
85
ULONG       hcd_index;
86
#endif
87
88
89
90
    /* If trace is enabled, insert this event into the trace buffer.  */
91
    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)
92
93
#if !defined(UX_NAME_REFERENCED_BY_POINTER)
94
    /* Get the length of the class name (exclude null-terminator).  */
95
376
    status =  _ux_utility_string_length_check(hcd_name, &hcd_name_length, UX_MAX_HCD_NAME_LENGTH);
96
376
    if (status)
97
1
        return(status);
98
#endif
99
100
    /* Get HCD.  */
101
375
    hcd =  _ux_system_host -> ux_system_host_hcd_array;
102
103
#if UX_MAX_HCD > 1
104
    /* We need to parse the controller driver table to find an empty spot.  */
105
381
    for(hcd_index = 0; hcd_index < _ux_system_host -> ux_system_host_max_hcd; hcd_index++)
106
    {
107
#endif
108
109
        /* Is this slot available?  */
110
380
        if(hcd -> ux_hcd_status == UX_UNUSED)
111
        {
112
113
            /* Yes, setup the new HCD entry.  */
114
115
#if defined(UX_NAME_REFERENCED_BY_POINTER)
116
            hcd -> ux_hcd_name = (const UCHAR *)hcd_name;
117
#else
118
119
            /* Initialize the array of the new controller with its name (include null-terminator).  */
120
374
            _ux_utility_memory_copy(hcd -> ux_hcd_name, hcd_name, hcd_name_length + 1); /* Use case of memcpy is verified. */
121
#endif
122
123
            /* Store the hardware resources of the controller */
124
374
            hcd -> ux_hcd_io =   hcd_param1;
125
374
            hcd -> ux_hcd_irq =  hcd_param2;
126
127
            /* This controller is now used */
128
374
            hcd -> ux_hcd_status =  UX_USED;
129
130
            /* And we have one new controller registered.  */
131
374
            _ux_system_host -> ux_system_host_registered_hcd++;
132
133
            /* We are now calling the HCD driver initialization.  */
134
374
            status =  hcd_init_function(hcd);
135
136
            /* Return the completion status to the caller.  */
137
374
            return(status);
138
        }
139
#if UX_MAX_HCD > 1
140
        /* Try the next HCD structure */
141
6
        hcd++;
142
    }
143
#endif
144
145
    /* We have exhausted the array of the HCDs, return an error.  */
146
1
    return(UX_MEMORY_INSUFFICIENT);
147
}
148
149
150
/**************************************************************************/
151
/*                                                                        */
152
/*  FUNCTION                                               RELEASE        */
153
/*                                                                        */
154
/*    _uxe_host_stack_hcd_register                        PORTABLE C      */
155
/*                                                           6.3.0        */
156
/*  AUTHOR                                                                */
157
/*                                                                        */
158
/*    Chaoqiong Xiao, Microsoft Corporation                               */
159
/*                                                                        */
160
/*  DESCRIPTION                                                           */
161
/*                                                                        */
162
/*    This function checks errors in host stack HCD register function     */
163
/*    call.                                                               */
164
/*                                                                        */
165
/*  INPUT                                                                 */
166
/*                                                                        */
167
/*    endpoint                              Endpoint to abort transfer    */
168
/*                                                                        */
169
/*  OUTPUT                                                                */
170
/*                                                                        */
171
/*    None                                                                */
172
/*                                                                        */
173
/*  CALLS                                                                 */
174
/*                                                                        */
175
/*    _ux_host_stack_hcd_register           HCD register                  */
176
/*                                                                        */
177
/*  CALLED BY                                                             */
178
/*                                                                        */
179
/*    Application                                                         */
180
/*                                                                        */
181
/**************************************************************************/
182
UINT  _uxe_host_stack_hcd_register(UCHAR *hcd_name,
183
                                    UINT (*hcd_init_function)(struct UX_HCD_STRUCT *), ULONG hcd_param1, ULONG hcd_param2)
184
{
185
186
    /* Sanity check.  */
187
    if ((hcd_name == UX_NULL) || (hcd_init_function == UX_NULL))
188
        return(UX_INVALID_PARAMETER);
189
190
    /* Invoke HCD register function.  */
191
    return(_ux_host_stack_hcd_register(hcd_name, hcd_init_function, hcd_param1, hcd_param2));
192
}