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 DFU 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_dfu.h"
27 #include "ux_device_stack.h"
28 
29 
30 /**************************************************************************/
31 /*                                                                        */
32 /*  FUNCTION                                               RELEASE        */
33 /*                                                                        */
34 /*    _ux_device_class_dfu_activate                       PORTABLE C      */
35 /*                                                           6.1.12       */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    Chaoqiong Xiao, Microsoft Corporation                               */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    This function initializes the USB DFU device.                       */
43 /*    This class can be activated either as part of the device primary    */
44 /*    framework or after a PORT_RESET detected.                           */
45 /*    This is detected through the protocol field. If 1, we are in the    */
46 /*    device regular mode. If 2, we are activated through the DFU         */
47 /*    mode.                                                               */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    command                               Pointer to dfu command        */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_utility_memory_allocate           Allocate memory               */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    USBX Source Code                                                    */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
70 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            resulting in version 6.1    */
72 /*  04-02-2021     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            removed block count (it's   */
74 /*                                            from host request wValue),  */
75 /*                                            resulting in version 6.1.6  */
76 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            resulting in version 6.1.10 */
78 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            fixed parameter/variable    */
80 /*                                            names conflict C++ keyword, */
81 /*                                            resulting in version 6.1.12 */
82 /*                                                                        */
83 /**************************************************************************/
_ux_device_class_dfu_activate(UX_SLAVE_CLASS_COMMAND * command)84 UINT  _ux_device_class_dfu_activate(UX_SLAVE_CLASS_COMMAND *command)
85 {
86 
87 UX_SLAVE_INTERFACE                      *interface_ptr;
88 UX_SLAVE_CLASS                          *class_ptr;
89 UX_SLAVE_CLASS_DFU                      *dfu;
90 
91     /* Get the class container.  */
92     class_ptr =  command -> ux_slave_class_command_class_ptr;
93 
94     /* Get the class instance in the container.  */
95     dfu = (UX_SLAVE_CLASS_DFU *) class_ptr -> ux_slave_class_instance;
96 
97     /* Get the interface that owns this instance.  */
98     interface_ptr =  (UX_SLAVE_INTERFACE  *) command -> ux_slave_class_command_interface;
99 
100     /* Store the class instance into the interface.  */
101     interface_ptr -> ux_slave_interface_class_instance =  (VOID *)dfu;
102 
103     /* Now the opposite, store the interface in the class instance.  */
104     dfu -> ux_slave_class_dfu_interface =  interface_ptr;
105 
106     /* Check the protocol activation field to determine in which state of the DFU class
107        we are.  */
108     switch (command -> ux_slave_class_command_protocol)
109     {
110 
111         case UX_SLAVE_CLASS_DFU_PROTOCOL_RUNTIME    :
112 
113             /* In the system, state the DFU state machine to application idle.  */
114             _ux_system_slave -> ux_system_slave_device_dfu_state_machine = UX_SYSTEM_DFU_STATE_APP_IDLE;
115 
116             /* Set the mode to Runtime.  */
117             _ux_system_slave -> ux_system_slave_device_dfu_mode =  UX_DEVICE_CLASS_DFU_MODE_RUNTIME ;
118 
119             break;
120 
121 
122         case UX_SLAVE_CLASS_DFU_PROTOCOL_DFU_MODE    :
123 
124             /* In the system, state the DFU state machine to DFU idle.  */
125             _ux_system_slave -> ux_system_slave_device_dfu_state_machine = UX_SYSTEM_DFU_STATE_DFU_IDLE;
126 
127             /* Set the mode to DFU mode.  */
128             _ux_system_slave -> ux_system_slave_device_dfu_mode =  UX_DEVICE_CLASS_DFU_MODE_DFU ;
129 
130             break;
131 
132         default :
133 
134             /* We should never get here.  */
135             return(UX_ERROR);
136 
137     }
138 
139 
140     /* If there is a activate function call it.  */
141     if (dfu -> ux_slave_class_dfu_instance_activate != UX_NULL)
142     {
143         /* Invoke the application.  */
144         dfu -> ux_slave_class_dfu_instance_activate(dfu);
145     }
146 
147     /* If trace is enabled, insert this event into the trace buffer.  */
148     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_DFU_ACTIVATE, dfu, 0, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
149 
150     /* If trace is enabled, register this object.  */
151     UX_TRACE_OBJECT_REGISTER(UX_TRACE_DEVICE_OBJECT_TYPE_INTERFACE, dfu, 0, 0, 0)
152 
153     /* Return completion status.  */
154     return(UX_SUCCESS);
155 }
156 
157