1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 /**************************************************************************/
13 /**                                                                       */
14 /** USBX Component                                                        */
15 /**                                                                       */
16 /**   Device CCID Class                                                   */
17 /**                                                                       */
18 /**************************************************************************/
19 /**************************************************************************/
20 
21 #define UX_SOURCE_CODE
22 
23 
24 /* Include necessary system files.  */
25 
26 #include "ux_api.h"
27 #include "ux_device_class_ccid.h"
28 #include "ux_device_stack.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_device_class_ccid_hardware_error                PORTABLE C      */
36 /*                                                           6.2.1        */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function indicates hardware error of the USB CCID device.      */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    ccid                                  Pointer to ccid instance      */
48 /*    slot                                  Slot inserted                 */
49 /*    error                                 Error code                    */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    Completion Status                                                   */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*                                                                        */
58 /*  CALLED BY                                                             */
59 /*                                                                        */
60 /*    USBX Source Code                                                    */
61 /*                                                                        */
62 /*  RELEASE HISTORY                                                       */
63 /*                                                                        */
64 /*    DATE              NAME                      DESCRIPTION             */
65 /*                                                                        */
66 /*  04-25-2022     Chaoqiong Xiao           Initial Version 6.1.11        */
67 /*  03-08-2023     Chaoqiong Xiao           Modified comment(s),          */
68 /*                                            added standalone support,   */
69 /*                                            resulting in version 6.2.1  */
70 /*                                                                        */
71 /**************************************************************************/
_ux_device_class_ccid_hardware_error(UX_DEVICE_CLASS_CCID * ccid,ULONG slot,ULONG error)72 UINT _ux_device_class_ccid_hardware_error(UX_DEVICE_CLASS_CCID *ccid, ULONG slot, ULONG error)
73 {
74 
75 UX_DEVICE_CLASS_CCID_SLOT                           *ccid_slot;
76 UX_DEVICE_CLASS_CCID_RUNNER                         *runner;
77 UX_DEVICE_CLASS_CCID_RDR_TO_PC_SLOT_STATUS_HEADER   *rsp;
78 
79     /* Sanity check.  */
80     if (slot >= ccid -> ux_device_class_ccid_parameter.ux_device_class_ccid_max_n_slots)
81         return(UX_INVALID_PARAMETER);
82 
83     /* Get slot instance.  */
84     ccid_slot  = ccid -> ux_device_class_ccid_slots;
85     ccid_slot += slot;
86 
87     /* Lock states.  */
88     _ux_device_class_ccid_lock(ccid);
89 
90     /* Check error.  */
91     if (!(ccid_slot -> ux_device_class_ccid_slot_flags & UX_DEVICE_CLASS_CCID_FLAG_HW_ERROR))
92     {
93 
94         /* Save error and error code.  */
95         ccid_slot -> ux_device_class_ccid_slot_flags |= UX_DEVICE_CLASS_CCID_FLAG_HW_ERROR;
96         ccid_slot -> ux_device_class_ccid_slot_hw_error = (UCHAR)error;
97         ccid_slot -> ux_device_class_ccid_slot_hw_error_seq = 0;
98 
99         /* Slot deactivated.  */
100         ccid_slot -> ux_device_class_ccid_slot_icc_status = UX_DEVICE_CLASS_CCID_ICC_INACTIVE;
101 
102         /* Check if command is pending, update response buffer.  */
103         if ((signed char)ccid_slot -> ux_device_class_ccid_slot_runner >= 0)
104         {
105 
106             /* Get running things.  */
107             runner = ccid -> ux_device_class_ccid_runners;
108             runner += ccid_slot -> ux_device_class_ccid_slot_runner;
109             rsp = (UX_DEVICE_CLASS_CCID_RDR_TO_PC_SLOT_STATUS_HEADER *)
110                                 runner -> ux_device_class_ccid_runner_response;
111 
112             /* Response: (1,1,HW_ERROR).  */
113             rsp -> bStatus = UX_DEVICE_CLASS_CCID_SLOT_STATUS(1, 1);
114             rsp -> bError = UX_DEVICE_CLASS_CCID_HW_ERROR;
115         }
116     }
117 
118     /* Notify if interrupt endpoint exists.  */
119     if (ccid -> ux_device_class_ccid_endpoint_notify)
120     {
121         ccid_slot -> ux_device_class_ccid_slot_flags |= UX_DEVICE_CLASS_CCID_FLAG_NOTIFY_CHANGE;
122 
123         /* Unlock states.  */
124         _ux_device_class_ccid_unlock(ccid);
125 
126         /* Wakeup interrupt notification.  */
127         _ux_device_semaphore_put(&ccid -> ux_device_class_ccid_notify_semaphore);
128 
129 #if defined(UX_DEVICE_STANDALONE)
130         if (ccid -> ux_device_class_ccid_notify_state == UX_DEVICE_CLASS_CCID_NOTIFY_IDLE)
131             ccid -> ux_device_class_ccid_notify_state = UX_DEVICE_CLASS_CCID_NOTIFY_LOCK;
132 #endif
133         return(UX_SUCCESS);
134     }
135 
136     /* Unlock states.  */
137     _ux_device_class_ccid_unlock(ccid);
138 
139     /* Return transfer status.  */
140     return(UX_SUCCESS);
141 }
142