summaryrefslogtreecommitdiff
path: root/common/usbx_device_classes/src/ux_device_class_ccid_control_request.c
blob: 8059b0bb1b1de903939ee7e9c2c4481655dcbf83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2026-present Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/

/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** USBX Component                                                        */
/**                                                                       */
/**   Device CCID Class                                                   */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define UX_SOURCE_CODE


/* Include necessary system files.  */

#include "ux_api.h"
#include "ux_device_class_ccid.h"
#include "ux_device_stack.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_device_class_ccid_control_request               PORTABLE C      */
/*                                                           6.1.11       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function manages the requests sent by the host on the control  */
/*    endpoints with a CLASS or VENDOR SPECIFIC type.                     */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    ccid                                  Pointer to ccid class         */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_device_stack_transfer_request     Transfer request              */
/*    _ux_device_class_ccid_request_abort   Abort slot                    */
/*    _ux_utility_memory_copy               Copy memory                   */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    CCID Class                                                          */
/*                                                                        */
/**************************************************************************/
UINT  _ux_device_class_ccid_control_request(UX_SLAVE_CLASS_COMMAND *command)
{
UX_DEVICE_CLASS_CCID                    *ccid;
UX_DEVICE_CLASS_CCID_PARAMETER          *parameter;
UX_SLAVE_CLASS                          *ccid_class;
UX_SLAVE_TRANSFER                       *transfer_request;
UX_SLAVE_DEVICE                         *device;
ULONG                                   request;
UCHAR                                   seq, slot;
ULONG                                   request_length;
ULONG                                   transmit_length;
UCHAR                                   *transmit_buffer;

    /* Get the class container.  */
    ccid_class =  command -> ux_slave_class_command_class_ptr;

    /* Get the class instance in the container.  */
    ccid = (UX_DEVICE_CLASS_CCID *) ccid_class -> ux_slave_class_instance;

    /* Get the pointer to the device.  */
    device =  &_ux_system_slave -> ux_system_slave_device;

    /* Get the pointer to the transfer request associated with the control endpoint.  */
    transfer_request =  &device -> ux_slave_device_control_endpoint.ux_slave_endpoint_transfer_request;

    /* Extract all necessary fields of the request.  */
    request =  *(transfer_request -> ux_slave_transfer_request_setup + UX_SETUP_REQUEST);

    /* Pickup the request length.  */
    request_length =   _ux_utility_short_get(transfer_request -> ux_slave_transfer_request_setup + UX_SETUP_LENGTH);

    /* Here we proceed only the standard request we know of at the device level.  */
    switch (request)
    {

    case UX_DEVICE_CLASS_CCID_ABORT:
        slot = transfer_request -> ux_slave_transfer_request_setup[UX_SETUP_VALUE];
        seq = transfer_request -> ux_slave_transfer_request_setup[UX_SETUP_VALUE + 1];
        return _ux_device_class_ccid_control_abort(ccid, slot, seq);

    case UX_DEVICE_CLASS_CCID_GET_CLOCK_FREQUENCIES:
        parameter = &ccid -> ux_device_class_ccid_parameter;

        /* Check bNumClockSuppored.  */
        if (parameter -> ux_device_class_ccid_clocks == UX_NULL ||
            parameter -> ux_device_class_ccid_n_clocks == 0)
            return(UX_ERROR);

        /* Calculate transmit length.  */
        transmit_length = parameter -> ux_device_class_ccid_n_clocks;
        if (UX_OVERFLOW_CHECK_MULC_ULONG(transmit_length, 4))
            return(UX_ERROR);
        transmit_length <<= 2;

        /* Update transmit buffer.  */
        transmit_buffer = (UCHAR *)parameter -> ux_device_class_ccid_clocks;
        break;

    case UX_DEVICE_CLASS_CCID_GET_DATA_RATES:
        parameter = &ccid -> ux_device_class_ccid_parameter;

        /* Check bNumDataRateSuppored.  */
        if (parameter -> ux_device_class_ccid_data_rates == UX_NULL ||
            parameter -> ux_device_class_ccid_n_data_rates == 0)
            return(UX_ERROR);

        /* Calculate transmit length.  */
        transmit_length = parameter -> ux_device_class_ccid_n_data_rates;
        if (UX_OVERFLOW_CHECK_MULC_ULONG(transmit_length, 4))
            return(UX_ERROR);
        transmit_length <<= 2;

        /* Update transmit buffer.  */
        transmit_buffer = (UCHAR *)parameter -> ux_device_class_ccid_data_rates;
        break ;

    default:

        /* Unknown function. It's not handled.  */
        return(UX_ERROR);
    }

    /* Limit transmit length.  */
    transmit_length = UX_MIN(transmit_length, UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH);
    transmit_length = UX_MIN(transmit_length, request_length);

    /* Copy data to transmit.  */
    _ux_utility_memory_copy(transfer_request -> ux_slave_transfer_request_data_pointer,
            transmit_buffer, transmit_length); /* Use case of memcpy is verified. */

    /* Transmit.  */
    _ux_device_stack_transfer_request(transfer_request, transmit_length, request_length);

    /* It's handled.  */
    return(UX_SUCCESS);
}