GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_storage_configure.c Lines: 19 19 100.0 %
Date: 2024-12-12 17:16:36 Branches: 14 14 100.0 %

Line Branch Exec Source
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
/**   Storage 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_storage.h"
29
#include "ux_host_stack.h"
30
31
32
/**************************************************************************/
33
/*                                                                        */
34
/*  FUNCTION                                               RELEASE        */
35
/*                                                                        */
36
/*    _ux_host_class_storage_configure                    PORTABLE C      */
37
/*                                                           6.1          */
38
/*  AUTHOR                                                                */
39
/*                                                                        */
40
/*    Chaoqiong Xiao, Microsoft Corporation                               */
41
/*                                                                        */
42
/*  DESCRIPTION                                                           */
43
/*                                                                        */
44
/*    This function calls the USBX stack to do a SET_CONFIGURATION to the */
45
/*    storage device. Once the storage is configured, its interface will  */
46
/*    be activated. The bulk endpoints enumerated(1 IN, 1 OUT).           */
47
/*                                                                        */
48
/*  INPUT                                                                 */
49
/*                                                                        */
50
/*    storage                               Pointer to storage class      */
51
/*                                                                        */
52
/*  OUTPUT                                                                */
53
/*                                                                        */
54
/*    Completion Status                                                   */
55
/*                                                                        */
56
/*  CALLS                                                                 */
57
/*                                                                        */
58
/*    _ux_host_stack_configuration_interface_get    Get configuration     */
59
/*    _ux_host_stack_device_configuration_get       Get device config     */
60
/*    _ux_host_stack_device_configuration_select    Select configuration  */
61
/*                                                                        */
62
/*  CALLED BY                                                             */
63
/*                                                                        */
64
/*    Storage Class                                                       */
65
/*                                                                        */
66
/*  RELEASE HISTORY                                                       */
67
/*                                                                        */
68
/*    DATE              NAME                      DESCRIPTION             */
69
/*                                                                        */
70
/*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
71
/*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
72
/*                                            optimized based on compile  */
73
/*                                            definitions,                */
74
/*                                            resulting in version 6.1    */
75
/*                                                                        */
76
/**************************************************************************/
77
133
UINT  _ux_host_class_storage_configure(UX_HOST_CLASS_STORAGE *storage)
78
{
79
80
UINT                    status;
81
UX_CONFIGURATION        *configuration;
82
#if UX_MAX_DEVICES > 1
83
UX_DEVICE               *parent_device;
84
#endif
85
86
87
    /* If the device has been configured already, we don't need to do it
88
       again.  */
89
133
    if (storage -> ux_host_class_storage_device -> ux_device_state == UX_DEVICE_CONFIGURED)
90
125
        return(UX_SUCCESS);
91
92
    /* A storage device normally has one configuration. So retrieve the 1st configuration
93
       only.  */
94
8
    status =  _ux_host_stack_device_configuration_get(storage -> ux_host_class_storage_device, 0, &configuration);
95
96
    /* Check completion status.  */
97
8
    if (status != UX_SUCCESS)
98
    {
99
100
        /* Error trap. */
101
2
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONFIGURATION_HANDLE_UNKNOWN);
102
103
        /* If trace is enabled, insert this event into the trace buffer.  */
104
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONFIGURATION_HANDLE_UNKNOWN, storage -> ux_host_class_storage_device, 0, 0, UX_TRACE_ERRORS, 0, 0)
105
106
2
        return(UX_CONFIGURATION_HANDLE_UNKNOWN);
107
    }
108
109
#if UX_MAX_DEVICES > 1
110
    /* Check the storage device power source and check the parent power source for
111
       incompatible connections.  */
112
6
    if (storage -> ux_host_class_storage_device -> ux_device_power_source == UX_DEVICE_BUS_POWERED)
113
    {
114
115
        /* Pickup pointer to parent device.  */
116
5
        parent_device =  storage -> ux_host_class_storage_device -> ux_device_parent;
117
118
        /* If the device is NULL, the parent is the root hub and we don't have to worry
119
           if the parent is not the root hub, check for its power source.  */
120

5
        if ((parent_device != UX_NULL) && (parent_device -> ux_device_power_source == UX_DEVICE_BUS_POWERED))
121
        {
122
123
            /* Error trap. */
124
1
            _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONNECTION_INCOMPATIBLE);
125
126
            /* If trace is enabled, insert this event into the trace buffer.  */
127
            UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONNECTION_INCOMPATIBLE, storage, 0, 0, UX_TRACE_ERRORS, 0, 0)
128
129
1
            return(UX_CONNECTION_INCOMPATIBLE);
130
        }
131
    }
132
#endif
133
134
    /* We have the valid configuration. Ask the USBX stack to set this configuration.  */
135
5
    status =  _ux_host_stack_device_configuration_select(configuration);
136
5
    if (status != UX_SUCCESS)
137
3
        return(status);
138
139
    /* If the operation went well, the storage device default alternate setting
140
       for the storage device interface is active and the interrupt endpoint is now enabled.
141
       We have to memorize the first interface since the interrupt endpoint is hooked to it.  */
142
2
    status =  _ux_host_stack_configuration_interface_get(configuration, 0, 0, &storage -> ux_host_class_storage_interface);
143
144
    /* Check completion status.  */
145
2
    if (status != UX_SUCCESS)
146
    {
147
        /* Store the instance in the interface container, this is for the USBX stack
148
           when it needs to invoke the class.  */
149
1
        storage -> ux_host_class_storage_interface -> ux_interface_class_instance =  (VOID *) storage;
150
    }
151
152
    /* Return completion status.  */
153
2
    return(status);
154
}
155