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 /**  HUB 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_hub.h"
29 #include "ux_host_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_class_hub_interrupt_endpoint_start         PORTABLE C      */
37 /*                                                           6.1.12       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function search for the handle of the only interrupt endpoint  */
45 /*    in the default alternate setting of the HUB interface. The          */
46 /*    interrupt endpoint should always be there. When it is located, the  */
47 /*    first transfer for this endpoint is made. The HUB will report status*/
48 /*    changes on this pipe.                                               */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    hub                                   Pointer to HUB class          */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    Completion Status                                                   */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _ux_host_stack_interface_endpoint_get Get endpoint of interface     */
61 /*    _ux_host_stack_transfer_request       Process transfer request      */
62 /*    _ux_utility_memory_allocate           Allocate memory block         */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    HUB 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 /*  10-15-2021     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            use pre-calculated value    */
77 /*                                            instead of wMaxPacketSize,  */
78 /*                                            resulting in version 6.1.9  */
79 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
80 /*                                            added timeout value init,   */
81 /*                                            resulting in version 6.1.12 */
82 /*                                                                        */
83 /**************************************************************************/
_ux_host_class_hub_interrupt_endpoint_start(UX_HOST_CLASS_HUB * hub)84 UINT  _ux_host_class_hub_interrupt_endpoint_start(UX_HOST_CLASS_HUB *hub)
85 {
86 
87 UINT            status;
88 UX_TRANSFER     *transfer_request;
89 
90 
91     /* Search the interrupt endpoint. It is attached to the interface container.  */
92     status =  _ux_host_stack_interface_endpoint_get(hub -> ux_host_class_hub_interface, 0, &hub -> ux_host_class_hub_interrupt_endpoint);
93 
94     /* Check completion status.  */
95     if (status != UX_SUCCESS)
96         return(status);
97 
98     /* Do a sanity check on the nature of the endpoint. Must be interrupt and its direction must be IN.  */
99     if (((hub -> ux_host_class_hub_interrupt_endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_IN) &&
100         ((hub -> ux_host_class_hub_interrupt_endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_INTERRUPT_ENDPOINT))
101     {
102 
103         /* The endpoint is correct, fill in the transfer_request with the length requested for this endpoint.  */
104         transfer_request =  &hub -> ux_host_class_hub_interrupt_endpoint -> ux_endpoint_transfer_request;
105         transfer_request -> ux_transfer_request_requested_length = transfer_request -> ux_transfer_request_packet_length;
106         transfer_request -> ux_transfer_request_actual_length =  0;
107 
108         /* Set timeout - wait forever.  */
109         transfer_request -> ux_transfer_request_timeout_value = UX_WAIT_FOREVER;
110 
111         /* Since this transfer_request has a callback, we need the HUB instance to be stored in the transfer request.  */
112         transfer_request -> ux_transfer_request_class_instance =  (VOID *) hub;
113 
114         /* This transfer request always has the IN direction.  */
115         hub -> ux_host_class_hub_interrupt_endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type =  UX_REQUEST_IN;
116 
117         /* Interrupt transactions have a completion routine.  */
118         transfer_request -> ux_transfer_request_completion_function =  _ux_host_class_hub_transfer_request_completed;
119 
120         /* Obtain a buffer for this transaction. The buffer will always be reused.  */
121         transfer_request -> ux_transfer_request_data_pointer =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY,
122                                                                 transfer_request -> ux_transfer_request_requested_length);
123 
124         /* Check the memory pointer.  */
125         if (transfer_request -> ux_transfer_request_data_pointer == UX_NULL)
126         {
127 
128             /* Clear the interrupt endpoint.  */
129             hub -> ux_host_class_hub_interrupt_endpoint = UX_NULL;
130 
131             /* Return error.  */
132             return(UX_MEMORY_INSUFFICIENT);
133         }
134 
135         /* Send the request to the stack.   */
136         status =  _ux_host_stack_transfer_request(transfer_request);
137 
138         /* Return completion status.  */
139         return(status);
140     }
141 
142     /* Error trap. */
143     _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_HUB, UX_ENDPOINT_HANDLE_UNKNOWN);
144 
145     /* If trace is enabled, insert this event into the trace buffer.  */
146     UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, hub -> ux_host_class_hub_interrupt_endpoint, 0, 0, UX_TRACE_ERRORS, 0, 0)
147 
148     /* Return error.  */
149     return(UX_ENDPOINT_HANDLE_UNKNOWN);
150 }
151