GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_hid_report_descriptor_get.c Lines: 44 44 100.0 %
Date: 2024-12-12 17:16:36 Branches: 18 18 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
/**   HID 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_hid.h"
29
#include "ux_host_stack.h"
30
31
32
/**************************************************************************/
33
/*                                                                        */
34
/*  FUNCTION                                               RELEASE        */
35
/*                                                                        */
36
/*    _ux_host_class_hid_report_descriptor_get            PORTABLE C      */
37
/*                                                           6.1          */
38
/*  AUTHOR                                                                */
39
/*                                                                        */
40
/*    Chaoqiong Xiao, Microsoft Corporation                               */
41
/*                                                                        */
42
/*  DESCRIPTION                                                           */
43
/*                                                                        */
44
/*    This function gets the report descriptor and analyzes it.           */
45
/*                                                                        */
46
/*  INPUT                                                                 */
47
/*                                                                        */
48
/*    hid                                   Pointer to HID class          */
49
/*    length                                Length of descriptor          */
50
/*                                                                        */
51
/*  OUTPUT                                                                */
52
/*                                                                        */
53
/*    Completion Status                                                   */
54
/*                                                                        */
55
/*  CALLS                                                                 */
56
/*                                                                        */
57
/*    _ux_host_class_hid_global_item_parse  Parse global item             */
58
/*    _ux_host_class_hid_local_item_parse   Parse local item              */
59
/*    _ux_host_class_hid_report_item_analyse Analyze report               */
60
/*    _ux_host_class_hid_resources_free     Free HID resources            */
61
/*    _ux_host_stack_transfer_request       Process transfer request      */
62
/*    _ux_utility_memory_allocate           Allocate memory block         */
63
/*    _ux_utility_memory_free               Release memory block          */
64
/*                                                                        */
65
/*  CALLED BY                                                             */
66
/*                                                                        */
67
/*    HID Class                                                           */
68
/*                                                                        */
69
/*  RELEASE HISTORY                                                       */
70
/*                                                                        */
71
/*    DATE              NAME                      DESCRIPTION             */
72
/*                                                                        */
73
/*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
74
/*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
75
/*                                            resulting in version 6.1    */
76
/*                                                                        */
77
/**************************************************************************/
78
490
UINT  _ux_host_class_hid_report_descriptor_get(UX_HOST_CLASS_HID *hid, ULONG length)
79
{
80
81
UX_ENDPOINT             *control_endpoint;
82
UX_TRANSFER             *transfer_request;
83
UCHAR                   *descriptor;
84
UCHAR                   *start_descriptor;
85
UX_HOST_CLASS_HID_ITEM  item;
86
UINT                    status;
87
88
89
    /* We need to get the default control endpoint transfer request pointer.  */
90
490
    control_endpoint =  &hid -> ux_host_class_hid_device -> ux_device_control_endpoint;
91
490
    transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
92
93
    /* Need to allocate memory for the report descriptor.  */
94
490
    descriptor =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, length);
95
490
    if (descriptor == UX_NULL)
96
7
        return(UX_MEMORY_INSUFFICIENT);
97
98
    /* We need to save the descriptor starting address.  */
99
483
    start_descriptor =  descriptor;
100
101
    /* Create a transfer_request for the GET_DESCRIPTOR request */
102
483
    transfer_request -> ux_transfer_request_data_pointer =      descriptor;
103
483
    transfer_request -> ux_transfer_request_requested_length =  length;
104
483
    transfer_request -> ux_transfer_request_function =          UX_GET_DESCRIPTOR;
105
483
    transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_INTERFACE;
106
483
    transfer_request -> ux_transfer_request_value =             UX_HOST_CLASS_HID_REPORT_DESCRIPTOR << 8;
107
483
    transfer_request -> ux_transfer_request_index =             hid -> ux_host_class_hid_interface -> ux_interface_descriptor.bInterfaceNumber;
108
109
    /* Send request to HCD layer.  */
110
483
    status =  _ux_host_stack_transfer_request(transfer_request);
111
112
    /* Check for correct transfer and entire descriptor returned.  */
113

483
    if ((status == UX_SUCCESS) && (transfer_request -> ux_transfer_request_actual_length == length))
114
    {
115
116
        /* Parse the report descriptor and build the report items.  */
117
14371
        while (length)
118
        {
119
120
            /* Get one item from the report and analyze it.  */
121
14080
            _ux_host_class_hid_report_item_analyse(descriptor, &item);
122
123
            /* Point the descriptor right after the item identifier.  */
124
14080
            descriptor +=  item.ux_host_class_hid_item_report_format;
125
126
            /* Process relative to the item type.  */
127

14080
            switch (item.ux_host_class_hid_item_report_type)
128
            {
129
130
7382
            case UX_HOST_CLASS_HID_TYPE_GLOBAL:
131
132
                /* This is a global item.  */
133
7382
                status =  _ux_host_class_hid_global_item_parse(hid, &item, descriptor);
134
7382
                break;
135
136
137
3269
            case UX_HOST_CLASS_HID_TYPE_MAIN:
138
139
                /* This is a main item.  */
140
3269
                status =  _ux_host_class_hid_main_item_parse(hid, &item, descriptor);
141
3269
                break;
142
143
144
3423
            case UX_HOST_CLASS_HID_TYPE_LOCAL:
145
146
                /* This is a local item.  */
147
3423
                status =  _ux_host_class_hid_local_item_parse(hid, &item, descriptor);
148
3423
                break;
149
150
6
            default:
151
152
                /* This is a reserved item, meaning it shouldn't be used!  */
153
154
                /* Set status to error. The check after this switch statement
155
                    will handle the rest.  */
156
6
                status =  UX_DESCRIPTOR_CORRUPTED;
157
6
                break;
158
            }
159
160
            /* Recheck the status code.  */
161
14080
            if (status != UX_SUCCESS)
162
            {
163
153
                break;
164
            }
165
166
            /* Jump to the next item.  */
167
13927
            descriptor +=  item.ux_host_class_hid_item_report_length;
168
169
            /* Verify that the report descriptor is not corrupted.  */
170
13927
            if (length < item.ux_host_class_hid_item_report_length)
171
            {
172
173
                /* Return error status.  */
174
3
                status = (UX_DESCRIPTOR_CORRUPTED);
175
3
                break;
176
            }
177
178
            /* Adjust the length.  */
179
13924
            length -=  (ULONG)(item.ux_host_class_hid_item_report_length + item.ux_host_class_hid_item_report_format);
180
        }
181
    }
182
    else
183
184
        /* Descriptor length error!  */
185
36
        status = UX_DESCRIPTOR_CORRUPTED;
186
187
483
    if (status != UX_SUCCESS)
188
    {
189
        /* Error trap. */
190
192
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);
191
192
        /* If trace is enabled, insert this event into the trace buffer.  */
193
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor, 0, 0, UX_TRACE_ERRORS, 0, 0)
194
195
        /* The descriptor is corrupted!  */
196
192
        _ux_host_class_hid_resources_free(hid);
197
    }
198
199
    /* Free used resources.  */
200
483
    _ux_utility_memory_free(start_descriptor);
201
202
    /* Return completion status.  */
203
483
    return(status);
204
}
205