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 /**   Prolific Class                                                      */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 /* Include necessary system files.  */
25 
26 #define UX_SOURCE_CODE
27 
28 #include "ux_api.h"
29 #include "ux_host_class_prolific.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_prolific_activate                    PORTABLE C      */
38 /*                                                           6.1.10       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function creates the prolific instance, configure the device.  */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    command                                DLC  class command pointer   */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    Completion Status                                                   */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    _ux_host_class_prolific_configure        Configure prolific class   */
58 /*    _ux_host_class_prolific_endpoints_get    Get endpoints of prolific  */
59 /*    _ux_host_class_prolific_setup            Set up prolific device     */
60 /*    _ux_host_stack_class_instance_create     Create class instance      */
61 /*    _ux_host_stack_class_instance_destroy    Destroy the class instance */
62 /*    _ux_utility_memory_allocate              Allocate memory block      */
63 /*    _ux_utility_memory_free                  Free memory block          */
64 /*    _ux_host_semaphore_create                Create prolific semaphore  */
65 /*    _ux_host_class_prolific_ioctl            IOCTL function for DLC     */
66 /*                                                                        */
67 /*  CALLED BY                                                             */
68 /*                                                                        */
69 /*    _ux_host_class_prolific_entry            Entry of prolific class    */
70 /*                                                                        */
71 /*  RELEASE HISTORY                                                       */
72 /*                                                                        */
73 /*    DATE              NAME                      DESCRIPTION             */
74 /*                                                                        */
75 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
76 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            resulting in version 6.1    */
78 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            refined macros names,       */
80 /*                                            resulting in version 6.1.10 */
81 /*                                                                        */
82 /**************************************************************************/
_ux_host_class_prolific_activate(UX_HOST_CLASS_COMMAND * command)83 UINT  _ux_host_class_prolific_activate(UX_HOST_CLASS_COMMAND *command)
84 {
85 
86 UX_DEVICE                           *device;
87 UX_HOST_CLASS_PROLIFIC              *prolific;
88 UX_HOST_CLASS_PROLIFIC_LINE_CODING  line_coding;
89 UX_HOST_CLASS_PROLIFIC_LINE_STATE   line_state;
90 UINT                                status;
91 
92     /* The prolific class is always activated by the device descriptor. */
93     device =  (UX_DEVICE *) command -> ux_host_class_command_container;
94 
95     /* Obtain memory for this class instance.  */
96     prolific =  _ux_utility_memory_allocate(UX_NO_ALIGN, UX_CACHE_SAFE_MEMORY, sizeof(UX_HOST_CLASS_PROLIFIC));
97     if (prolific == UX_NULL)
98         return(UX_MEMORY_INSUFFICIENT);
99 
100     /* Store the class container into this instance.  */
101     prolific -> ux_host_class_prolific_class =  command -> ux_host_class_command_class_ptr;
102 
103     /* Store the device container into the prolific class instance.  */
104     prolific -> ux_host_class_prolific_device =  device;
105 
106     /* Store the instance in the device container, this is for the USBX stack
107        when it needs to invoke the class for deactivation.  */
108     device -> ux_device_class_instance =  (VOID *) prolific;
109 
110     /* Create this class instance.  */
111     _ux_host_stack_class_instance_create(prolific -> ux_host_class_prolific_class, (VOID *) prolific);
112 
113     /* Configure the prolific.  */
114     status =  _ux_host_class_prolific_configure(prolific);
115 
116     /* Get the prolific endpoint(s). We need to search for Bulk Out and Bulk In endpoints
117        and the interrupt endpoint.  */
118     if (status == UX_SUCCESS)
119         status =  _ux_host_class_prolific_endpoints_get(prolific);
120 
121     /* Go on if success.  */
122     if (status == UX_SUCCESS)
123     {
124 
125         /* Store chip version for further reference.  */
126         prolific -> ux_host_class_prolific_version = device -> ux_device_descriptor.bcdDevice;
127 
128         /* Mark the prolific instance as mounting now.  */
129         prolific -> ux_host_class_prolific_state =  UX_HOST_CLASS_INSTANCE_MOUNTING;
130 
131         /* The prolific chip needs to be setup properly.  */
132         status = _ux_host_class_prolific_setup(prolific);
133     }
134 
135     /* Set the default values to the device, first line coding.  */
136     if (status == UX_SUCCESS)
137     {
138         line_coding.ux_host_class_prolific_line_coding_dter      = UX_HOST_CLASS_PROLIFIC_LINE_CODING_DEFAULT_RATE;
139         line_coding.ux_host_class_prolific_line_coding_stop_bit  = UX_HOST_CLASS_PROLIFIC_LINE_CODING_STOP_BIT_0;
140         line_coding.ux_host_class_prolific_line_coding_parity    = UX_HOST_CLASS_PROLIFIC_LINE_CODING_PARITY_NONE;
141         line_coding.ux_host_class_prolific_line_coding_data_bits = UX_HOST_CLASS_PROLIFIC_LINE_CODING_DEFAULT_DATA_BIT;
142         status = _ux_host_class_prolific_ioctl(prolific, UX_HOST_CLASS_PROLIFIC_IOCTL_SET_LINE_CODING, (VOID *) &line_coding);
143     }
144 
145     /* Set the default values to the device, line state.  For the Prolific chip to detect disconnection
146        and reconnection, RTS is low.  */
147     if (status == UX_SUCCESS)
148     {
149         line_state.ux_host_class_prolific_line_state_rts      = 0;
150         line_state.ux_host_class_prolific_line_state_dtr       = 1;
151         status = _ux_host_class_prolific_ioctl(prolific, UX_HOST_CLASS_PROLIFIC_IOCTL_SET_LINE_STATE, (VOID *) &line_state);
152     }
153 
154     /* Create the semaphore to protect 2 threads from accessing the same prolific instance.  */
155     if (status == UX_SUCCESS)
156     {
157         status =  _ux_host_semaphore_create(&prolific -> ux_host_class_prolific_semaphore, "ux_host_class_prolific_semaphore", 1);
158         if (status != UX_SUCCESS)
159             status = UX_SEMAPHORE_ERROR;
160     }
161 
162     /* Success things.  */
163     if (status == UX_SUCCESS)
164     {
165 
166         /* Mark the prolific instance as live now.  */
167         prolific -> ux_host_class_prolific_state =  UX_HOST_CLASS_INSTANCE_LIVE;
168 
169         /* If all is fine and the device is mounted, we may need to inform the application
170         if a function has been programmed in the system structure.  */
171         if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
172         {
173 
174             /* Call system change function.  */
175             _ux_system_host ->  ux_system_host_change_function(UX_DEVICE_INSERTION, prolific -> ux_host_class_prolific_class, (VOID *) prolific);
176         }
177 
178         /* If trace is enabled, insert this event into the trace buffer.  */
179         UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_PROLIFIC_ACTIVATE, prolific, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
180 
181         /* If trace is enabled, register this object.  */
182         UX_TRACE_OBJECT_REGISTER(UX_TRACE_HOST_OBJECT_TYPE_INTERFACE, prolific, 0, 0, 0)
183 
184         /* Return success.  */
185         return(UX_SUCCESS);
186     }
187 
188     /* There was a problem during the configuration, so free the resources.  */
189     /* The last resource, semaphore is not created or created error, no need to free.  */
190     _ux_host_stack_class_instance_destroy(prolific -> ux_host_class_prolific_class, (VOID *) prolific);
191     device -> ux_device_class_instance = UX_NULL;
192     _ux_utility_memory_free(prolific);
193 
194     /* Return completion status.  */
195     return(status);
196 }
197 
198