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 /**                                                                       */
16 /** USBX Component                                                        */
17 /**                                                                       */
18 /**   Slave Simulator Controller Driver                                   */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define UX_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "ux_api.h"
29 #include "ux_dcd_sim_slave.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_dcd_sim_slave_initialize_complete               PORTABLE C      */
37 /*                                                           6.1.9        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function completes the initialization of the slave controller  */
45 /*    simulator.                                                          */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    None                                                                */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    Completion Status                                                   */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    (ux_slave_dcd_function)               DCD dispatch function         */
58 /*    _ux_utility_descriptor_parse          Parse descriptor              */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    Slave Simulator Controller Driver                                   */
63 /*                                                                        */
64 /*  RELEASE HISTORY                                                       */
65 /*                                                                        */
66 /*    DATE              NAME                      DESCRIPTION             */
67 /*                                                                        */
68 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
69 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
70 /*                                            prefixed UX to MS_TO_TICK,  */
71 /*                                            resulting in version 6.1    */
72 /*  04-02-2021     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            added framework init cases, */
74 /*                                            resulting in version 6.1.6  */
75 /*  10-15-2021     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            filled payload size,        */
77 /*                                            resulting in version 6.1.9  */
78 /*                                                                        */
79 /**************************************************************************/
_ux_dcd_sim_slave_initialize_complete(VOID)80 UINT  _ux_dcd_sim_slave_initialize_complete(VOID)
81 {
82 
83 UX_SLAVE_DCD            *dcd;
84 UX_SLAVE_DEVICE         *device;
85 UCHAR *                 device_framework;
86 UX_SLAVE_TRANSFER       *transfer_request;
87 
88 
89     /* Get the pointer to the DCD.  */
90     dcd =  &_ux_system_slave -> ux_system_slave_dcd;
91 
92     /* Get the pointer to the device.  */
93     device =  &_ux_system_slave -> ux_system_slave_device;
94 
95     /* Prepare according to speed.  */
96     if (_ux_system_slave -> ux_system_slave_speed == UX_HIGH_SPEED_DEVICE)
97     {
98         _ux_system_slave -> ux_system_slave_device_framework =
99             _ux_system_slave -> ux_system_slave_device_framework_high_speed;
100         _ux_system_slave -> ux_system_slave_device_framework_length =
101             _ux_system_slave -> ux_system_slave_device_framework_length_high_speed;
102     }
103     else
104     {
105         _ux_system_slave -> ux_system_slave_device_framework =
106             _ux_system_slave -> ux_system_slave_device_framework_full_speed;
107         _ux_system_slave -> ux_system_slave_device_framework_length =
108             _ux_system_slave -> ux_system_slave_device_framework_length_full_speed;
109 
110     }
111 
112     /* Get the device framework pointer.  */
113     device_framework =  _ux_system_slave -> ux_system_slave_device_framework;
114 
115     /* And create the decompressed device descriptor structure.  */
116     _ux_utility_descriptor_parse(device_framework,
117                                 _ux_system_device_descriptor_structure,
118                                 UX_DEVICE_DESCRIPTOR_ENTRIES,
119                                 (UCHAR *) &device -> ux_slave_device_descriptor);
120 
121     /* Now we create a transfer request to accept the first SETUP packet
122        and get the ball running. First get the address of the endpoint
123        transfer request container.  */
124     transfer_request =  &device -> ux_slave_device_control_endpoint.ux_slave_endpoint_transfer_request;
125 
126     /* Set the timeout to be for Control Endpoint.  */
127     transfer_request -> ux_slave_transfer_request_timeout =  UX_MS_TO_TICK(UX_CONTROL_TRANSFER_TIMEOUT);
128 
129     /* Adjust the current data pointer as well.  */
130     transfer_request -> ux_slave_transfer_request_current_data_pointer =
131                             transfer_request -> ux_slave_transfer_request_data_pointer;
132 
133     /* Update the transfer request endpoint pointer with the default endpoint.  */
134     transfer_request -> ux_slave_transfer_request_endpoint =  &device -> ux_slave_device_control_endpoint;
135 
136     /* The control endpoint max packet size needs to be filled manually in its descriptor.  */
137     transfer_request -> ux_slave_transfer_request_endpoint -> ux_slave_endpoint_descriptor.wMaxPacketSize =
138                                 device -> ux_slave_device_descriptor.bMaxPacketSize0;
139 
140     /* On the control endpoint, always expect the maximum.  */
141     transfer_request -> ux_slave_transfer_request_requested_length =
142                                 device -> ux_slave_device_descriptor.bMaxPacketSize0;
143     transfer_request -> ux_slave_transfer_request_transfer_length =
144                                 device -> ux_slave_device_descriptor.bMaxPacketSize0;
145 
146     /* Attach the control endpoint to the transfer request.  */
147     transfer_request -> ux_slave_transfer_request_endpoint =  &device -> ux_slave_device_control_endpoint;
148 
149     /* Create the default control endpoint attached to the device.
150        Once this endpoint is enabled, the host can then send a setup packet
151        The device controller will receive it and will call the setup function
152        module.  */
153     dcd -> ux_slave_dcd_function(dcd, UX_DCD_CREATE_ENDPOINT,
154                                     (VOID *) &device -> ux_slave_device_control_endpoint);
155 
156     /* Ensure the control endpoint is properly reset.  */
157     device -> ux_slave_device_control_endpoint.ux_slave_endpoint_state = UX_ENDPOINT_RESET;
158 
159     /* A SETUP packet is a DATA IN operation.  */
160     transfer_request -> ux_slave_transfer_request_phase =  UX_TRANSFER_PHASE_DATA_IN;
161 
162     /* We are now ready for the USB device to accept the first packet when connected.  */
163     return(UX_SUCCESS);
164 }
165 
166