GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_hid_activate.c Lines: 32 32 100.0 %
Date: 2026-03-06 18:57:10 Branches: 16 16 100.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
/**   HID Class                                                           */
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_class_hid.h"
30
#include "ux_host_stack.h"
31
32
33
/**************************************************************************/
34
/*                                                                        */
35
/*  FUNCTION                                               RELEASE        */
36
/*                                                                        */
37
/*    _ux_host_class_hid_activate                         PORTABLE C      */
38
/*                                                           6.1.12       */
39
/*  AUTHOR                                                                */
40
/*                                                                        */
41
/*    Chaoqiong Xiao, Microsoft Corporation                               */
42
/*                                                                        */
43
/*  DESCRIPTION                                                           */
44
/*                                                                        */
45
/*    This function performs the enumeration of the HID class.            */
46
/*                                                                        */
47
/*  INPUT                                                                 */
48
/*                                                                        */
49
/*    command                               Pointer to command            */
50
/*                                                                        */
51
/*  OUTPUT                                                                */
52
/*                                                                        */
53
/*    Completion Status                                                   */
54
/*                                                                        */
55
/*  CALLS                                                                 */
56
/*                                                                        */
57
/*    _ux_host_class_hid_client_search      HID client search             */
58
/*    _ux_host_class_hid_configure          Configure HID                 */
59
/*    _ux_host_class_hid_descriptor_parse   Parse descriptor              */
60
/*    _ux_host_class_hid_interrupt_endpoint_search  Search endpoint       */
61
/*    _ux_host_class_hid_instance_clean     Clean up instance resources   */
62
/*    _ux_host_stack_class_instance_create  Create class instance         */
63
/*    _ux_host_stack_class_instance_destroy Destroy class instance        */
64
/*    _ux_utility_memory_allocate           Allocate memory block         */
65
/*    _ux_utility_memory_free               Free memory                   */
66
/*    _ux_host_semaphore_create             Create semaphore              */
67
/*                                                                        */
68
/*  CALLED BY                                                             */
69
/*                                                                        */
70
/*    HID Class                                                           */
71
/*                                                                        */
72
/**************************************************************************/
73
714
UINT  _ux_host_class_hid_activate(UX_HOST_CLASS_COMMAND  *command)
74
{
75
76
UX_INTERFACE        *interface_ptr;
77
UX_HOST_CLASS_HID   *hid;
78
UINT                status;
79
80
81
    /* The HID is always activated by the interface descriptor and not the
82
       device descriptor.  */
83
714
    interface_ptr =  (UX_INTERFACE *) command -> ux_host_class_command_container;
84
85
    /* Instantiate this HID class */
86
714
    hid =  _ux_utility_memory_allocate(UX_NO_ALIGN,  UX_REGULAR_MEMORY,sizeof(UX_HOST_CLASS_HID));
87
714
    if (hid == UX_NULL)
88
216
        return(UX_MEMORY_INSUFFICIENT);
89
90
    /* Store the class container into this instance.  */
91
498
    hid -> ux_host_class_hid_class =  command -> ux_host_class_command_class_ptr;
92
93
    /* Store the interface container into the HID class instance.  */
94
498
    hid -> ux_host_class_hid_interface =  interface_ptr;
95
96
    /* Store the device container into the HID class instance.  */
97
498
    hid -> ux_host_class_hid_device =  interface_ptr -> ux_interface_configuration -> ux_configuration_device;
98
99
    /* This instance of the device must also be stored in the interface container.  */
100
498
    interface_ptr -> ux_interface_class_instance =  (VOID *) hid;
101
102
    /* Create this class instance.  */
103
498
    _ux_host_stack_class_instance_create(command -> ux_host_class_command_class_ptr, (VOID *) hid);
104
105
#if defined(UX_HOST_STANDALONE)
106
107
    /* Set class tasks function.  */
108
    hid -> ux_host_class_hid_class -> ux_host_class_task_function = _ux_host_class_hid_tasks_run;
109
110
    /* Set activate state to first step.  */
111
    hid -> ux_host_class_hid_enum_state = UX_STATE_WAIT;
112
113
    status = UX_SUCCESS;
114
    return(status);
115
#else
116
117
    /* Configure the HID.  */
118
498
    status =  _ux_host_class_hid_configure(hid);
119
120
    /* If configure is done success, goes on. */
121
498
    if (status == UX_SUCCESS)
122
    {
123
124
        /* Get the HID descriptor and parse it.  */
125
497
        status =  _ux_host_class_hid_descriptor_parse(hid);
126
    }
127
128
    /* If HID descriptor parse is done success, goes on. */
129
498
    if (status == UX_SUCCESS)
130
    {
131
132
        /* Search the HID interrupt endpoint but do not start it.  */
133
291
        status =  _ux_host_class_hid_interrupt_endpoint_search(hid);
134
    }
135
136
    /* If HID interrupt endpoint is found, goes on. */
137
498
    if (status == UX_SUCCESS)
138
    {
139
140
        /* Create the semaphore to protect multiple threads from accessing the same
141
        storage instance.  */
142
275
        status =  _ux_host_semaphore_create(&hid -> ux_host_class_hid_semaphore, "ux_host_class_hid_semaphore", 1);
143
144
275
        if (status == UX_SUCCESS)
145
        {
146
147
            /* If all is fine, try to locate the HID client for this HID device.  */
148
274
            _ux_host_class_hid_client_search(hid);
149
150
            /* Mark the HID class as live now.  */
151
274
            hid -> ux_host_class_hid_state =  UX_HOST_CLASS_INSTANCE_LIVE;
152
153
            /* We may need to inform the application if a function has been programmed in the system structure.  */
154
274
            if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
155
            {
156
157
                /* Call system change function.  */
158
213
                _ux_system_host ->  ux_system_host_change_function(UX_DEVICE_INSERTION, hid -> ux_host_class_hid_class, (VOID *) hid);
159
            }
160
161
            /* If trace is enabled, insert this event into the trace buffer.  */
162
            UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_HID_ACTIVATE, hid, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
163
164
            /* If trace is enabled, register this object.  */
165
            UX_TRACE_OBJECT_REGISTER(UX_TRACE_HOST_OBJECT_TYPE_INTERFACE, hid, 0, 0, 0)
166
167
            /* Return completion code.  */
168
274
            return(status);
169
        }
170
        else
171
        {
172
1
            status = UX_SEMAPHORE_ERROR;
173
        }
174
    }
175
176
    /* Clean interrupt endpoint.  */
177
224
    if (hid -> ux_host_class_hid_interrupt_endpoint &&
178
7
        hid -> ux_host_class_hid_interrupt_endpoint -> ux_endpoint_transfer_request.ux_transfer_request_data_pointer)
179
1
        _ux_utility_memory_free(hid -> ux_host_class_hid_interrupt_endpoint -> ux_endpoint_transfer_request.ux_transfer_request_data_pointer);
180
181
    /* Clean instance. */
182
224
    _ux_host_class_hid_instance_clean(hid);
183
184
    /* Error, destroy the class instance and return error code. */
185
224
    _ux_host_stack_class_instance_destroy(hid -> ux_host_class_hid_class, (VOID *) hid);
186
187
    /* Unmount instance. */
188
224
    interface_ptr -> ux_interface_class_instance = UX_NULL;
189
190
    /* Free instance. */
191
224
    _ux_utility_memory_free(hid);
192
193
    /* Return error code. */
194
224
    return(status);
195
196
#endif
197
}
198