summaryrefslogtreecommitdiff
path: root/common/usbx_device_classes/src/ux_device_class_ccid_notify_thread_entry.c
blob: 98edd4d77fed4b9777e785bee44c43940c3819a8 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/***************************************************************************
 * 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"

#if !defined(UX_DEVICE_STANDALONE)
#include "ux_device_class_ccid.h"
#include "ux_device_stack.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_device_class_ccid_notify_thread_entry           PORTABLE C      */
/*                                                           6.2.1        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function is the thread of the CCID interrupt IN endpoint. It   */
/*    is waiting for the application event to start interrupt IN.         */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    ccid_inst                             Address of ccid class         */
/*                                            container (32-bit)          */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_device_stack_transfer_request     Request transfer              */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    ThreadX                                                             */
/*                                                                        */
/**************************************************************************/
VOID  _ux_device_class_ccid_notify_thread_entry(ULONG ccid_inst)
{

UX_SLAVE_DEVICE                                     *device;
UX_DEVICE_CLASS_CCID                                *ccid;
UX_DEVICE_CLASS_CCID_SLOT                           *slot;
UX_DEVICE_CLASS_CCID_PARAMETER                      *parameter;
UX_SLAVE_ENDPOINT                                   *endpoint;
UX_SLAVE_TRANSFER                                   *transfer;
UCHAR                                               *buffer;
ULONG                                               length;
INT                                                 i;
UCHAR                                               icc_mask;
UINT                                                byte_pos, bits_pos;
UINT                                                status;

    /* Cast properly the ccid instance.  */
    UX_THREAD_EXTENSION_PTR_GET(ccid, UX_DEVICE_CLASS_CCID, ccid_inst)

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

    /* This thread runs forever but can be suspended or resumed by the user application.  */
    status = UX_SUCCESS;
    while(1)
    {

        /* Error cases.  */
        if (status != UX_SUCCESS)
        {

            /* We need to suspend ourselves. We will be resumed by the
               application if needed.  */
            _ux_utility_thread_suspend(&ccid -> ux_device_class_ccid_notify_thread);
            status = UX_SUCCESS;
            continue;
        }
        status = UX_ERROR;

        /* Check device state.  */
        if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
            continue;

        /* Check endpoint.  */
        endpoint = ccid -> ux_device_class_ccid_endpoint_notify;
        if (endpoint == UX_NULL)
            continue;
        transfer = &endpoint -> ux_slave_endpoint_transfer_request;

        /* Wait event.  */
        status = _ux_utility_semaphore_get(&ccid -> ux_device_class_ccid_notify_semaphore,
                                           UX_WAIT_FOREVER);
        if (status != UX_SUCCESS)
            continue;

        /* Build slot change/hardware error message.  */
        buffer = transfer -> ux_slave_transfer_request_data_pointer;
        length = 0;

        /* By default no message.  */
        buffer[UX_DEVICE_CLASS_CCID_OFFSET_MESSAGE_TYPE] = 0;

        /* Build slot hardware error message.  */
        parameter = &ccid -> ux_device_class_ccid_parameter;

        _ux_device_class_ccid_lock(ccid);

        slot = ccid -> ux_device_class_ccid_slots;
        for (i = 0; i < parameter -> ux_device_class_ccid_max_n_slots; i ++)
        {

            /* Check if there is hardware error notification.  */
            if (slot -> ux_device_class_ccid_slot_flags &
                UX_DEVICE_CLASS_CCID_FLAG_NOTIFY_HW_ERROR)
            {
                slot -> ux_device_class_ccid_slot_flags &=
                                (UCHAR)~UX_DEVICE_CLASS_CCID_FLAG_NOTIFY_HW_ERROR;
                buffer[UX_DEVICE_CLASS_CCID_OFFSET_MESSAGE_TYPE] =
                                UX_DEVICE_CLASS_CCID_RDR_TO_PC_HARDWARE_ERROR;
                buffer[UX_DEVICE_CLASS_CCID_OFFSET_HW_ERROR_SLOT] = (UCHAR)i;
                buffer[UX_DEVICE_CLASS_CCID_OFFSET_HW_ERROR_SEQ] =
                                slot -> ux_device_class_ccid_slot_hw_error_seq;
                buffer[UX_DEVICE_CLASS_CCID_OFFSET_HW_ERROR_CODE] =
                                slot -> ux_device_class_ccid_slot_hw_error;
                length = 4;
                break;
            }

            /* Next slot.  */
            slot ++;
        }

        /* Build slot changes message.  */
        if (buffer[UX_DEVICE_CLASS_CCID_OFFSET_MESSAGE_TYPE] == 0)
        {

            /* Scan slots.  */
            slot = ccid -> ux_device_class_ccid_slots;
            for (i = 0, byte_pos = 1; ; byte_pos ++)
            {
                /* Reset bits.  */
                buffer[byte_pos] = 0;

                /* Scan 4 slots.  */
                for(bits_pos = 0; bits_pos < 8; bits_pos += 2)
                {

                    /* Slot state bit.  */
                    icc_mask = (UCHAR)((slot -> ux_device_class_ccid_slot_icc_status ==
                            UX_DEVICE_CLASS_CCID_SLOT_STATUS_ICC_NOT_PRESENT) ?
                            0u : 1u);

                    /* Check if there is change notification.  */
                    if (slot -> ux_device_class_ccid_slot_flags &
                        UX_DEVICE_CLASS_CCID_FLAG_NOTIFY_CHANGE)
                    {
                        slot -> ux_device_class_ccid_slot_flags &=
                                    (UCHAR)~UX_DEVICE_CLASS_CCID_FLAG_NOTIFY_CHANGE;

                        /* Message type.  */
                        buffer[UX_DEVICE_CLASS_CCID_OFFSET_MESSAGE_TYPE] =
                            UX_DEVICE_CLASS_CCID_RDR_TO_PC_NOTIFY_SLOT_CHANGE;

                        /* Slot change bit.  */
                        icc_mask |= (UCHAR)0x02u;
                    }

                    /* Modify bits.  */
                    buffer[byte_pos] |= (UCHAR)(icc_mask << bits_pos);

                    /* Next slot.  */
                    i ++;
                    if (i >= parameter -> ux_device_class_ccid_max_n_slots)
                        break;
                    slot ++;
                }
                if (i >= parameter -> ux_device_class_ccid_max_n_slots)
                    break;
            }

            /* Buffer length should include last byte.  */
            length = byte_pos + 1;
        }

        _ux_device_class_ccid_unlock(ccid);

        /* Check message to see if there is message to send.  */
        if (buffer[UX_DEVICE_CLASS_CCID_OFFSET_MESSAGE_TYPE] == 0)
            continue;

        /* Send request.  */
        status = _ux_device_stack_transfer_request(transfer, length, length);
    }
}
#endif