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_icc_remove                    PORTABLE C      */
35 /*                                                           6.3.0        */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    Chaoqiong Xiao, Microsoft Corporation                               */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    This function indicates card removal of the USB CCID device.        */
43 /*                                                                        */
44 /*  INPUT                                                                 */
45 /*                                                                        */
46 /*    ccid                                  Pointer to ccid instance      */
47 /*    slot                                  Slot removed                  */
48 /*                                                                        */
49 /*  OUTPUT                                                                */
50 /*                                                                        */
51 /*    Completion Status                                                   */
52 /*                                                                        */
53 /*  CALLS                                                                 */
54 /*                                                                        */
55 /*                                                                        */
56 /*  CALLED BY                                                             */
57 /*                                                                        */
58 /*    Application                                                         */
59 /*                                                                        */
60 /*  RELEASE HISTORY                                                       */
61 /*                                                                        */
62 /*    DATE              NAME                      DESCRIPTION             */
63 /*                                                                        */
64 /*  04-25-2022     Chaoqiong Xiao           Initial Version 6.1.11        */
65 /*  03-08-2023     Chaoqiong Xiao           Modified comment(s),          */
66 /*                                            added standalone support,   */
67 /*                                            resulting in version 6.2.1  */
68 /*  10-31-2023     Yajun Xia                Modified comment(s),          */
69 /*                                            resulting in version 6.3.0  */
70 /*                                                                        */
71 /**************************************************************************/
_ux_device_class_ccid_icc_remove(UX_DEVICE_CLASS_CCID * ccid,ULONG slot)72 UINT _ux_device_class_ccid_icc_remove(UX_DEVICE_CLASS_CCID *ccid, ULONG slot)
73 {
74 
75 UX_DEVICE_CLASS_CCID_SLOT       *ccid_slot;
76 
77     /* Sanity check.  */
78     if (slot >= ccid -> ux_device_class_ccid_parameter.ux_device_class_ccid_max_n_slots)
79         return(UX_INVALID_PARAMETER);
80 
81     /* Get slot instance.  */
82     ccid_slot  = ccid -> ux_device_class_ccid_slots;
83     ccid_slot += slot;
84 
85     /* Lock states.  */
86     _ux_device_class_ccid_lock(ccid);
87 
88     /* Return success if already card removed.  */
89     if (ccid_slot -> ux_device_class_ccid_slot_icc_status == UX_DEVICE_CLASS_CCID_ICC_NOT_PRESENT)
90     {
91         _ux_device_class_ccid_unlock(ccid);
92         return(UX_SUCCESS);
93     }
94 
95     /* Update card status (NOT_PRESENT).  */
96     ccid_slot -> ux_device_class_ccid_slot_icc_status = UX_DEVICE_CLASS_CCID_SLOT_STATUS_ICC_NOT_PRESENT;
97 
98     /* Clear sequencing.  */
99     ccid_slot -> ux_device_class_ccid_slot_flags &= (UCHAR)~UX_DEVICE_CLASS_CCID_FLAG_AUTO_SEQUENCING;
100 
101     /* Clear errors.  */
102     ccid_slot -> ux_device_class_ccid_slot_hw_error = 0;
103     ccid_slot -> ux_device_class_ccid_slot_flags &= (UCHAR)~UX_DEVICE_CLASS_CCID_FLAG_HW_ERROR;
104 
105     /* Notify if interrupt endpoint exists.  */
106     if (ccid -> ux_device_class_ccid_endpoint_notify)
107     {
108         ccid_slot -> ux_device_class_ccid_slot_flags |= UX_DEVICE_CLASS_CCID_FLAG_NOTIFY_CHANGE;
109         _ux_device_class_ccid_unlock(ccid);
110         _ux_device_semaphore_put(&ccid -> ux_device_class_ccid_notify_semaphore);
111 
112 #if defined(UX_DEVICE_STANDALONE)
113         if (ccid -> ux_device_class_ccid_notify_state == UX_DEVICE_CLASS_CCID_NOTIFY_IDLE)
114             ccid -> ux_device_class_ccid_notify_state = UX_DEVICE_CLASS_CCID_NOTIFY_LOCK;
115 #endif
116         return(UX_SUCCESS);
117     }
118     _ux_device_class_ccid_unlock(ccid);
119 
120     /* Return transfer status.  */
121     return(UX_SUCCESS);
122 }
123 
124 /**************************************************************************/
125 /*                                                                        */
126 /*  FUNCTION                                               RELEASE        */
127 /*                                                                        */
128 /*    _uxe_device_class_ccid_icc_remove                   PORTABLE C      */
129 /*                                                           6.3.0        */
130 /*  AUTHOR                                                                */
131 /*                                                                        */
132 /*    Yajun Xia, Microsoft Corporation                                    */
133 /*                                                                        */
134 /*  DESCRIPTION                                                           */
135 /*                                                                        */
136 /*    This function checks errors in CCID card remove function.           */
137 /*                                                                        */
138 /*  INPUT                                                                 */
139 /*                                                                        */
140 /*    ccid                                  Pointer to ccid instance      */
141 /*    slot                                  Slot removed                  */
142 /*                                                                        */
143 /*  OUTPUT                                                                */
144 /*                                                                        */
145 /*    Completion Status                                                   */
146 /*                                                                        */
147 /*  CALLS                                                                 */
148 /*                                                                        */
149 /*    _ux_device_class_ccid_icc_remove      CCID card remove function.    */
150 /*                                                                        */
151 /*  CALLED BY                                                             */
152 /*                                                                        */
153 /*    Application                                                         */
154 /*                                                                        */
155 /*  RELEASE HISTORY                                                       */
156 /*                                                                        */
157 /*    DATE              NAME                      DESCRIPTION             */
158 /*                                                                        */
159 /*  10-31-2023     Yajun Xia                Initial Version 6.3.0         */
160 /*                                                                        */
161 /**************************************************************************/
_uxe_device_class_ccid_icc_remove(UX_DEVICE_CLASS_CCID * ccid,ULONG slot)162 UINT _uxe_device_class_ccid_icc_remove(UX_DEVICE_CLASS_CCID *ccid, ULONG slot)
163 {
164 
165     /* Sanity check.  */
166     if (ccid == UX_NULL)
167         return(UX_INVALID_PARAMETER);
168 
169     return(_ux_device_class_ccid_icc_remove(ccid, slot));
170 }