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 /**   Printer 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_printer.h"
29 #include "ux_host_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_class_printer_configure                    PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function calls the USBX stack to do a SET_CONFIGURATION to the */
45 /*    printer. Once the printer is configured, its interface will be      */
46 /*    activated. The bulk endpoints enumerated(1 IN, 1 OUT ).             */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    printer                               Pointer to printer class      */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    Completion Status                                                   */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _ux_host_stack_configuration_interface_get  Get interface           */
59 /*    _ux_host_stack_device_configuration_get     Get configuration       */
60 /*    _ux_host_stack_device_configuration_select  Select configuration    */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    _ux_host_class_printer_activate       Printer class activate        */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
71 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
72 /*                                            optimized based on compile  */
73 /*                                            definitions,                */
74 /*                                            resulting in version 6.1    */
75 /*                                                                        */
76 /**************************************************************************/
_ux_host_class_printer_configure(UX_HOST_CLASS_PRINTER * printer)77 UINT  _ux_host_class_printer_configure(UX_HOST_CLASS_PRINTER *printer)
78 {
79 
80 UINT                    status;
81 UX_CONFIGURATION        *configuration;
82 #if UX_MAX_DEVICES > 1
83 UX_DEVICE               *parent_device;
84 #endif
85 
86 
87     /* If the device has been configured already, we don't need to do it
88        again. */
89     if (printer -> ux_host_class_printer_device -> ux_device_state == UX_DEVICE_CONFIGURED)
90         return(UX_SUCCESS);
91 
92     /* A printer normally has one configuration. So retrieve the 1st configuration
93        only.  */
94     status =  _ux_host_stack_device_configuration_get(printer -> ux_host_class_printer_device, 0, &configuration);
95     if (status != UX_SUCCESS)
96     {
97 
98         /* Error trap. */
99         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONFIGURATION_HANDLE_UNKNOWN);
100 
101         /* If trace is enabled, insert this event into the trace buffer.  */
102         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONFIGURATION_HANDLE_UNKNOWN, printer -> ux_host_class_printer_device, 0, 0, UX_TRACE_ERRORS, 0, 0)
103 
104         return(UX_CONFIGURATION_HANDLE_UNKNOWN);
105     }
106 
107 #if UX_MAX_DEVICES > 1
108     /* Check the printer power source and check the parent power source for
109        incompatible connections.  */
110     if (printer -> ux_host_class_printer_device -> ux_device_power_source == UX_DEVICE_BUS_POWERED)
111     {
112 
113         /* Get parent device pointer.  */
114         parent_device =  printer -> ux_host_class_printer_device -> ux_device_parent;
115 
116         /* If the device is NULL, the parent is the root printer and we don't have to worry
117            if the parent is not the root printer, check for its power source.  */
118         if ((parent_device != UX_NULL) && (parent_device -> ux_device_power_source == UX_DEVICE_BUS_POWERED))
119         {
120 
121             /* Error trap. */
122             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONNECTION_INCOMPATIBLE);
123 
124             /* If trace is enabled, insert this event into the trace buffer.  */
125             UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONNECTION_INCOMPATIBLE, printer, 0, 0, UX_TRACE_ERRORS, 0, 0)
126 
127             return(UX_CONNECTION_INCOMPATIBLE);
128         }
129     }
130 #endif
131 
132     /* We have the valid configuration. Ask the USBX stack to set this configuration.  */
133     status =  _ux_host_stack_device_configuration_select(configuration);
134     if (status != UX_SUCCESS)
135         return(status);
136 
137     /* If the operation went well, the printer default alternate setting for the printer interface is
138        active and the interrupt endpoint is now enabled. We have to memorize the first interface since
139        the interrupt endpoint is hooked to it. */
140     status =  _ux_host_stack_configuration_interface_get(configuration, 0, 0, &printer -> ux_host_class_printer_interface);
141     if (status != UX_SUCCESS)
142     {
143 
144         /* Store the instance in the interface container, this is for the USB stack
145            when it needs to invoke the class.  */
146         printer -> ux_host_class_printer_interface -> ux_interface_class_instance =  (VOID *) printer;
147     }
148 
149     /* Return completion status.  */
150     return(status);
151 }
152 
153