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 /**                                                                       */
14 /** USBX Component                                                        */
15 /**                                                                       */
16 /**   Device DFU 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_dfu.h"
28 #include "ux_device_stack.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_device_class_dfu_entry                          PORTABLE C      */
36 /*                                                           6.3.0        */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function is the entry point of the dfu class. It               */
44 /*    will be called by the device stack enumeration module when the      */
45 /*    host has sent a SET_CONFIGURATION command and the dfu interface     */
46 /*    needs to be mounted.                                                */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    command                               Pointer to class command      */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    Completion Status                                                   */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _ux_device_class_dfu_initialize       Initialize dfu class          */
59 /*    _ux_device_class_dfu_activate         Activate dfu class            */
60 /*    _ux_device_class_dfu_deactivate       Deactivate dfu class          */
61 /*    _ux_device_class_dfu_control_request  Request control               */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    DFU Class                                                           */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
72 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            resulting in version 6.1    */
74 /*  10-31-2023     Yajun Xia                Modified comment(s),          */
75 /*                                            added error checks support, */
76 /*                                            resulting in version 6.3.0  */
77 /*                                                                        */
78 /**************************************************************************/
_ux_device_class_dfu_entry(UX_SLAVE_CLASS_COMMAND * command)79 UINT  _ux_device_class_dfu_entry(UX_SLAVE_CLASS_COMMAND *command)
80 {
81 
82 UINT        status;
83 
84 
85     /* The command request will tell us we need to do here, either a enumeration
86        query, an activation or a deactivation.  */
87     switch (command -> ux_slave_class_command_request)
88     {
89 
90     case UX_SLAVE_CLASS_COMMAND_INITIALIZE:
91 #ifdef UX_DEVICE_CLASS_DFU_ENABLE_ERROR_CHECKING
92 
93         /* Call the init function of the DFU ACM class.  */
94         status =  _uxe_device_class_dfu_initialize(command);
95 #else
96 
97         /* Call the init function of the DFU ACM class.  */
98         status =  _ux_device_class_dfu_initialize(command);
99 #endif /* UX_DEVICE_CLASS_DFU_ENABLE_ERROR_CHECKING */
100 
101         /* Return the completion status.  */
102         return(status);
103 
104 
105     case UX_SLAVE_CLASS_COMMAND_QUERY:
106 
107         /* Check the CLASS definition in the interface descriptor. */
108         if (command -> ux_slave_class_command_class == UX_SLAVE_CLASS_DFU_CLASS)
109             return(UX_SUCCESS);
110         else
111             return(UX_NO_CLASS_MATCH);
112 
113     case UX_SLAVE_CLASS_COMMAND_ACTIVATE:
114 
115         /* The activate command is used when the host has sent a SET_CONFIGURATION command
116            and this interface has to be mounted. Both Bulk endpoints have to be mounted
117            and the dfu thread needs to be activated.  */
118         status =  _ux_device_class_dfu_activate(command);
119 
120         /* Return the completion status.  */
121         return(status);
122 
123     case UX_SLAVE_CLASS_COMMAND_DEACTIVATE:
124 
125         /* The deactivate command is used when the device has been extracted.
126            The device endpoints have to be dismounted and the dfu thread canceled.  */
127         status =  _ux_device_class_dfu_deactivate(command);
128 
129         /* Return the completion status.  */
130         return(status);
131 
132     case UX_SLAVE_CLASS_COMMAND_REQUEST:
133 
134         /* The request command is used when the host sends a command on the control endpoint.  */
135         status = _ux_device_class_dfu_control_request(command);
136 
137         /* Return the completion status.  */
138         return(status);
139 
140     default:
141 
142         /* If trace is enabled, insert this event into the trace buffer.  */
143         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_FUNCTION_NOT_SUPPORTED, 0, 0, 0, UX_TRACE_ERRORS, 0, 0)
144 
145         /* Return an error.  */
146         return(UX_FUNCTION_NOT_SUPPORTED);
147     }
148 }
149 
150