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 /**                                                                       */
15 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   Device CCID Class                                                   */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define UX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "ux_api.h"
28 #include "ux_device_class_ccid.h"
29 #include "ux_device_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_device_class_ccid_control_request               PORTABLE C      */
37 /*                                                           6.1.11       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function manages the requests sent by the host on the control  */
45 /*    endpoints with a CLASS or VENDOR SPECIFIC type.                     */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    ccid                                  Pointer to ccid class         */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    None                                                                */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    _ux_device_stack_transfer_request     Transfer request              */
58 /*    _ux_device_class_ccid_request_abort   Abort slot                    */
59 /*    _ux_utility_memory_copy               Copy memory                   */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    CCID Class                                                          */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  04-25-2022     Chaoqiong Xiao           Initial Version 6.1.11        */
70 /*                                                                        */
71 /**************************************************************************/
_ux_device_class_ccid_control_request(UX_SLAVE_CLASS_COMMAND * command)72 UINT  _ux_device_class_ccid_control_request(UX_SLAVE_CLASS_COMMAND *command)
73 {
74 UX_DEVICE_CLASS_CCID                    *ccid;
75 UX_DEVICE_CLASS_CCID_PARAMETER          *parameter;
76 UX_SLAVE_CLASS                          *ccid_class;
77 UX_SLAVE_TRANSFER                       *transfer_request;
78 UX_SLAVE_DEVICE                         *device;
79 ULONG                                   request;
80 UCHAR                                   seq, slot;
81 ULONG                                   request_length;
82 ULONG                                   transmit_length;
83 UCHAR                                   *transmit_buffer;
84 
85     /* Get the class container.  */
86     ccid_class =  command -> ux_slave_class_command_class_ptr;
87 
88     /* Get the class instance in the container.  */
89     ccid = (UX_DEVICE_CLASS_CCID *) ccid_class -> ux_slave_class_instance;
90 
91     /* Get the pointer to the device.  */
92     device =  &_ux_system_slave -> ux_system_slave_device;
93 
94     /* Get the pointer to the transfer request associated with the control endpoint.  */
95     transfer_request =  &device -> ux_slave_device_control_endpoint.ux_slave_endpoint_transfer_request;
96 
97     /* Extract all necessary fields of the request.  */
98     request =  *(transfer_request -> ux_slave_transfer_request_setup + UX_SETUP_REQUEST);
99 
100     /* Pickup the request length.  */
101     request_length =   _ux_utility_short_get(transfer_request -> ux_slave_transfer_request_setup + UX_SETUP_LENGTH);
102 
103     /* Here we proceed only the standard request we know of at the device level.  */
104     switch (request)
105     {
106 
107     case UX_DEVICE_CLASS_CCID_ABORT:
108         slot = transfer_request -> ux_slave_transfer_request_setup[UX_SETUP_VALUE];
109         seq = transfer_request -> ux_slave_transfer_request_setup[UX_SETUP_VALUE + 1];
110         return _ux_device_class_ccid_control_abort(ccid, slot, seq);
111 
112     case UX_DEVICE_CLASS_CCID_GET_CLOCK_FREQUENCIES:
113         parameter = &ccid -> ux_device_class_ccid_parameter;
114 
115         /* Check bNumClockSuppored.  */
116         if (parameter -> ux_device_class_ccid_clocks == UX_NULL ||
117             parameter -> ux_device_class_ccid_n_clocks == 0)
118             return(UX_ERROR);
119 
120         /* Calculate transmit length.  */
121         transmit_length = parameter -> ux_device_class_ccid_n_clocks;
122         if (UX_OVERFLOW_CHECK_MULC_ULONG(transmit_length, 4))
123             return(UX_ERROR);
124         transmit_length <<= 2;
125 
126         /* Update transmit buffer.  */
127         transmit_buffer = (UCHAR *)parameter -> ux_device_class_ccid_clocks;
128         break;
129 
130     case UX_DEVICE_CLASS_CCID_GET_DATA_RATES:
131         parameter = &ccid -> ux_device_class_ccid_parameter;
132 
133         /* Check bNumDataRateSuppored.  */
134         if (parameter -> ux_device_class_ccid_data_rates == UX_NULL ||
135             parameter -> ux_device_class_ccid_n_data_rates == 0)
136             return(UX_ERROR);
137 
138         /* Calculate transmit length.  */
139         transmit_length = parameter -> ux_device_class_ccid_n_data_rates;
140         if (UX_OVERFLOW_CHECK_MULC_ULONG(transmit_length, 4))
141             return(UX_ERROR);
142         transmit_length <<= 2;
143 
144         /* Update transmit buffer.  */
145         transmit_buffer = (UCHAR *)parameter -> ux_device_class_ccid_data_rates;
146         break ;
147 
148     default:
149 
150         /* Unknown function. It's not handled.  */
151         return(UX_ERROR);
152     }
153 
154     /* Limit transmit length.  */
155     transmit_length = UX_MIN(transmit_length, UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH);
156     transmit_length = UX_MIN(transmit_length, request_length);
157 
158     /* Copy data to transmit.  */
159     _ux_utility_memory_copy(transfer_request -> ux_slave_transfer_request_data_pointer,
160             transmit_buffer, transmit_length); /* Use case of memcpy is verified. */
161 
162     /* Transmit.  */
163     _ux_device_stack_transfer_request(transfer_request, transmit_length, request_length);
164 
165     /* It's handled.  */
166     return(UX_SUCCESS);
167 }
168