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 /** USBX Component                                                        */
14 /**                                                                       */
15 /**   Device CCID Class                                                   */
16 /**                                                                       */
17 /**************************************************************************/
18 /**************************************************************************/
19 
20 #define UX_SOURCE_CODE
21 
22 
23 /* Include necessary system files.  */
24 
25 #include "ux_api.h"
26 #include "ux_device_class_ccid.h"
27 #include "ux_device_stack.h"
28 
29 
30 /**************************************************************************/
31 /*                                                                        */
32 /*  FUNCTION                                               RELEASE        */
33 /*                                                                        */
34 /*    _ux_device_class_ccid_hardware_error                PORTABLE C      */
35 /*                                                           6.3.0        */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    Chaoqiong Xiao, Microsoft Corporation                               */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    This function indicates hardware error of the USB CCID device.      */
43 /*                                                                        */
44 /*  INPUT                                                                 */
45 /*                                                                        */
46 /*    ccid                                  Pointer to ccid instance      */
47 /*    slot                                  Slot inserted                 */
48 /*    error                                 Error code                    */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    Completion Status                                                   */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*                                                                        */
57 /*  CALLED BY                                                             */
58 /*                                                                        */
59 /*    Application                                                         */
60 /*                                                                        */
61 /*  RELEASE HISTORY                                                       */
62 /*                                                                        */
63 /*    DATE              NAME                      DESCRIPTION             */
64 /*                                                                        */
65 /*  04-25-2022     Chaoqiong Xiao           Initial Version 6.1.11        */
66 /*  03-08-2023     Chaoqiong Xiao           Modified comment(s),          */
67 /*                                            added standalone support,   */
68 /*                                            resulting in version 6.2.1  */
69 /*  10-31-2023     Yajun Xia                Modified comment(s),          */
70 /*                                            resulting in version 6.3.0  */
71 /*                                                                        */
72 /**************************************************************************/
_ux_device_class_ccid_hardware_error(UX_DEVICE_CLASS_CCID * ccid,ULONG slot,ULONG error)73 UINT _ux_device_class_ccid_hardware_error(UX_DEVICE_CLASS_CCID *ccid, ULONG slot, ULONG error)
74 {
75 
76 UX_DEVICE_CLASS_CCID_SLOT                           *ccid_slot;
77 UX_DEVICE_CLASS_CCID_RUNNER                         *runner;
78 UX_DEVICE_CLASS_CCID_RDR_TO_PC_SLOT_STATUS_HEADER   *rsp;
79 
80     /* Sanity check.  */
81     if (slot >= ccid -> ux_device_class_ccid_parameter.ux_device_class_ccid_max_n_slots)
82         return(UX_INVALID_PARAMETER);
83 
84     /* Get slot instance.  */
85     ccid_slot  = ccid -> ux_device_class_ccid_slots;
86     ccid_slot += slot;
87 
88     /* Lock states.  */
89     _ux_device_class_ccid_lock(ccid);
90 
91     /* Check error.  */
92     if (!(ccid_slot -> ux_device_class_ccid_slot_flags & UX_DEVICE_CLASS_CCID_FLAG_HW_ERROR))
93     {
94 
95         /* Save error and error code.  */
96         ccid_slot -> ux_device_class_ccid_slot_flags |= UX_DEVICE_CLASS_CCID_FLAG_HW_ERROR;
97         ccid_slot -> ux_device_class_ccid_slot_hw_error = (UCHAR)error;
98         ccid_slot -> ux_device_class_ccid_slot_hw_error_seq = 0;
99 
100         /* Slot deactivated.  */
101         ccid_slot -> ux_device_class_ccid_slot_icc_status = UX_DEVICE_CLASS_CCID_ICC_INACTIVE;
102 
103         /* Check if command is pending, update response buffer.  */
104         if ((signed char)ccid_slot -> ux_device_class_ccid_slot_runner >= 0)
105         {
106 
107             /* Get running things.  */
108             runner = ccid -> ux_device_class_ccid_runners;
109             runner += ccid_slot -> ux_device_class_ccid_slot_runner;
110             rsp = (UX_DEVICE_CLASS_CCID_RDR_TO_PC_SLOT_STATUS_HEADER *)
111                                 runner -> ux_device_class_ccid_runner_response;
112 
113             /* Response: (1,1,HW_ERROR).  */
114             rsp -> bStatus = UX_DEVICE_CLASS_CCID_SLOT_STATUS(1, 1);
115             rsp -> bError = UX_DEVICE_CLASS_CCID_HW_ERROR;
116         }
117     }
118 
119     /* Notify if interrupt endpoint exists.  */
120     if (ccid -> ux_device_class_ccid_endpoint_notify)
121     {
122         ccid_slot -> ux_device_class_ccid_slot_flags |= UX_DEVICE_CLASS_CCID_FLAG_NOTIFY_CHANGE;
123 
124         /* Unlock states.  */
125         _ux_device_class_ccid_unlock(ccid);
126 
127         /* Wakeup interrupt notification.  */
128         _ux_device_semaphore_put(&ccid -> ux_device_class_ccid_notify_semaphore);
129 
130 #if defined(UX_DEVICE_STANDALONE)
131         if (ccid -> ux_device_class_ccid_notify_state == UX_DEVICE_CLASS_CCID_NOTIFY_IDLE)
132             ccid -> ux_device_class_ccid_notify_state = UX_DEVICE_CLASS_CCID_NOTIFY_LOCK;
133 #endif
134         return(UX_SUCCESS);
135     }
136 
137     /* Unlock states.  */
138     _ux_device_class_ccid_unlock(ccid);
139 
140     /* Return transfer status.  */
141     return(UX_SUCCESS);
142 }
143 
144 /**************************************************************************/
145 /*                                                                        */
146 /*  FUNCTION                                               RELEASE        */
147 /*                                                                        */
148 /*    _uxe_device_class_ccid_hardware_error               PORTABLE C      */
149 /*                                                           6.3.0        */
150 /*  AUTHOR                                                                */
151 /*                                                                        */
152 /*    Yajun Xia, Microsoft Corporation                                    */
153 /*                                                                        */
154 /*  DESCRIPTION                                                           */
155 /*                                                                        */
156 /*    This function checks errors in CCID card hardware error function.   */
157 /*                                                                        */
158 /*  INPUT                                                                 */
159 /*                                                                        */
160 /*    ccid                                  Pointer to ccid instance      */
161 /*    slot                                  Slot removed                  */
162 /*                                                                        */
163 /*  OUTPUT                                                                */
164 /*                                                                        */
165 /*    Completion Status                                                   */
166 /*                                                                        */
167 /*  CALLS                                                                 */
168 /*                                                                        */
169 /*    _ux_device_class_ccid_hardware_error  CCID card hardware error      */
170 /*                                          function.                     */
171 /*                                                                        */
172 /*  CALLED BY                                                             */
173 /*                                                                        */
174 /*    Application                                                         */
175 /*                                                                        */
176 /*  RELEASE HISTORY                                                       */
177 /*                                                                        */
178 /*    DATE              NAME                      DESCRIPTION             */
179 /*                                                                        */
180 /*  10-31-2023     Yajun Xia                Initial Version 6.3.0         */
181 /*                                                                        */
182 /**************************************************************************/
_uxe_device_class_ccid_hardware_error(UX_DEVICE_CLASS_CCID * ccid,ULONG slot,ULONG error)183 UINT  _uxe_device_class_ccid_hardware_error(UX_DEVICE_CLASS_CCID *ccid, ULONG slot, ULONG error)
184 {
185 
186     /* Sanity check.  */
187     if (ccid == UX_NULL)
188         return(UX_INVALID_PARAMETER);
189 
190     return(_ux_device_class_ccid_hardware_error(ccid, slot, error));
191 }