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 /**                                                                       */
15 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   Host Sierra Wireless AR module class                                */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 
23 /* Include necessary system files.  */
24 
25 #define UX_SOURCE_CODE
26 
27 #include "ux_api.h"
28 #include "ux_host_class_swar.h"
29 #include "ux_host_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_class_swar_activate                        PORTABLE C      */
37 /*                                                           6.1.10       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function calls the USBX stack to activate the class.           */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    command                               Dpump class command pointer   */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    Completion Status                                                   */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*    _ux_host_class_swar_configure           Configure swar class        */
57 /*    _ux_host_class_swar_endpoints_get       Get endpoints of swar       */
58 /*    _ux_host_stack_class_instance_create    Create class instance       */
59 /*    _ux_host_stack_class_instance_destroy   Destroy the class instance  */
60 /*    _ux_utility_memory_allocate             Allocate memory block       */
61 /*    _ux_utility_memory_free                 Free memory block           */
62 /*    _ux_host_semaphore_create               Create swar semaphore       */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    _ux_host_class_swar_entry            Entry of swar class            */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
73 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            resulting in version 6.1    */
75 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            refined macros names,       */
77 /*                                            resulting in version 6.1.10 */
78 /*                                                                        */
79 /**************************************************************************/
_ux_host_class_swar_activate(UX_HOST_CLASS_COMMAND * command)80 UINT  _ux_host_class_swar_activate(UX_HOST_CLASS_COMMAND *command)
81 {
82 
83 UX_DEVICE                           *device;
84 UX_HOST_CLASS_SWAR                  *swar;
85 UINT                                status;
86 
87 
88     /* The Sierra Wireless class is always activated by the device descriptor. */
89     device =  (UX_DEVICE *) command -> ux_host_class_command_container;
90 
91     /* Obtain memory for this class instance.  */
92     swar =  _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, sizeof(UX_HOST_CLASS_SWAR));
93     if (swar == UX_NULL)
94         return(UX_MEMORY_INSUFFICIENT);
95 
96     /* Store the class container into this instance.  */
97     swar -> ux_host_class_swar_class =  command -> ux_host_class_command_class_ptr;
98 
99     /* Store the device container into the swar class instance.  */
100     swar -> ux_host_class_swar_device =  device;
101 
102     /* Store the instance in the device container, this is for the USBX stack
103        when it needs to invoke the class for deactivation.  */
104     device -> ux_device_class_instance =  (VOID *) swar;
105 
106     /* Create this class instance.  */
107     _ux_host_stack_class_instance_create(swar -> ux_host_class_swar_class, (VOID *) swar);
108 
109     /* Configure the swar.  */
110     status =  _ux_host_class_swar_configure(swar);
111 
112     /* Get the swar endpoint(s). We will need to search for Bulk Out and Bulk In endpoints on interface .  */
113     if (status == UX_SUCCESS)
114         status =  _ux_host_class_swar_endpoints_get(swar);
115 
116     /* Create the semaphore to protect 2 threads from accessing the same swar instance.  */
117     if (status == UX_SUCCESS)
118     {
119         status =  _ux_host_semaphore_create(&swar -> ux_host_class_swar_semaphore, "ux_host_class_swar_semaphore", 1);
120         if (status != UX_SUCCESS)
121             status = UX_SEMAPHORE_ERROR;
122     }
123 
124     /* Success things.  */
125     if (status == UX_SUCCESS)
126     {
127 
128         /* Mark the swar as live now.  */
129         swar -> ux_host_class_swar_state =  UX_HOST_CLASS_INSTANCE_LIVE;
130 
131         /* If all is fine and the device is mounted, we may need to inform the application
132         if a function has been programmed in the system structure.  */
133         if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
134         {
135 
136             /* Call system change function.  */
137             _ux_system_host ->  ux_system_host_change_function(UX_DEVICE_INSERTION, swar -> ux_host_class_swar_class, (VOID *) swar);
138         }
139 
140         /* If trace is enabled, insert this event into the trace buffer.  */
141         UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_SWAR_ACTIVATE, swar, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
142 
143         /* If trace is enabled, register this object.  */
144         UX_TRACE_OBJECT_REGISTER(UX_TRACE_HOST_OBJECT_TYPE_INTERFACE, swar, 0, 0, 0)
145 
146         /* Return success.  */
147         return(UX_SUCCESS);
148     }
149 
150     /* There was a problem during the configuration, so free the resources.  */
151     /* The last resource, semaphore is not created or created error, no need to free.  */
152     _ux_host_stack_class_instance_destroy(swar -> ux_host_class_swar_class, (VOID *) swar);
153     device -> ux_device_class_instance = UX_NULL;
154     _ux_utility_memory_free(swar);
155 
156     /* Return completion status.  */
157     return(status);
158 }
159 
160