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