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 /**  HID 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_hid.h"
29 #include "ux_host_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_class_hid_interrupt_endpoint_search        PORTABLE C      */
37 /*                                                           6.1.12       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function searches for the handle of the only interrupt         */
45 /*    endpoint in the default alternate setting of the HID interface.     */
46 /*    The interrupt endpoint should always be there. The actual first     */
47 /*    transfer on the interrupt endpoint does not start until a HID       */
48 /*    client has claimed ownership of the HID device.                     */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    hid                                   Pointer to HID class          */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    Completion Status                                                   */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _ux_utility_memory_allocate           Allocate memory block         */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    HID Class                                                           */
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 /*                                            resulting in version 6.1    */
73 /*  01-31-2022     Xiuwen Cai, CQ Xiao      Modified comment(s),          */
74 /*                                            added interrupt OUT support,*/
75 /*                                            added timeout initialize,   */
76 /*                                            resulting in version 6.1.10 */
77 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
78 /*                                            fixed parameter/variable    */
79 /*                                            names conflict C++ keyword, */
80 /*                                            resulting in version 6.1.12 */
81 /*                                                                        */
82 /**************************************************************************/
_ux_host_class_hid_interrupt_endpoint_search(UX_HOST_CLASS_HID * hid)83 UINT  _ux_host_class_hid_interrupt_endpoint_search(UX_HOST_CLASS_HID *hid)
84 {
85 
86 UINT            status = UX_ENDPOINT_HANDLE_UNKNOWN;
87 UX_INTERFACE    *interface_ptr;
88 UX_ENDPOINT     *endpoint;
89 UX_TRANSFER     *transfer_request;
90 
91 
92     /* Search the interrupt endpoint. It is attached to the interface container.  */
93     interface_ptr = hid -> ux_host_class_hid_interface;
94     endpoint = interface_ptr -> ux_interface_first_endpoint;
95     while(endpoint != UX_NULL)
96     {
97 
98         /* Find interrupt IN endpoint.  */
99         if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_IN) &&
100             ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_INTERRUPT_ENDPOINT))
101         {
102 
103             /* The endpoint is correct, save it.  */
104             hid -> ux_host_class_hid_interrupt_endpoint = endpoint;
105 
106             /* Fill in the transfer request with the length requested for this endpoint.  */
107             transfer_request =  &endpoint -> ux_endpoint_transfer_request;
108             transfer_request -> ux_transfer_request_requested_length =  hid -> ux_host_class_hid_interrupt_endpoint -> ux_endpoint_descriptor.wMaxPacketSize;
109             transfer_request -> ux_transfer_request_actual_length =     0;
110 
111             /* The direction is always IN for the HID interrupt endpoint.  */
112             transfer_request -> ux_transfer_request_type =  UX_REQUEST_IN;
113 
114             /* There is a callback function associated with the transfer request, so we need the class instance.  */
115             transfer_request -> ux_transfer_request_class_instance =  (VOID *) hid;
116 
117             /* Interrupt transactions have a completion routine. */
118             transfer_request -> ux_transfer_request_completion_function =  _ux_host_class_hid_transfer_request_completed;
119 
120             /* Transfer timeout : wait forever.  */
121             transfer_request -> ux_transfer_request_timeout_value = UX_WAIT_FOREVER;
122 
123             /* Obtain a buffer for this transaction. The buffer will always be reused.  */
124             transfer_request -> ux_transfer_request_data_pointer =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY,
125                                                                         transfer_request -> ux_transfer_request_requested_length);
126 
127             /* If the endpoint is available and we have memory, we mark the interrupt endpoint as ready.  */
128             if (transfer_request -> ux_transfer_request_data_pointer != UX_NULL)
129             {
130                 hid -> ux_host_class_hid_interrupt_endpoint_status =  UX_HOST_CLASS_HID_INTERRUPT_ENDPOINT_READY;
131                 status = UX_SUCCESS;
132 #if !defined(UX_HOST_CLASS_HID_INTERRUPT_OUT_SUPPORT)
133 
134                 /* We have found the interrupt IN endpoint, just stop searching.  */
135                 break;
136 #endif
137             }
138             else
139             {
140                 status = UX_MEMORY_INSUFFICIENT;
141                 break;
142             }
143         }
144 #if defined(UX_HOST_CLASS_HID_INTERRUPT_OUT_SUPPORT)
145         else if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_OUT) &&
146             ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_INTERRUPT_ENDPOINT))
147         {
148 
149             /* Found the interrupt OUT endpoint, save it.  */
150             hid -> ux_host_class_hid_interrupt_out_endpoint = endpoint;
151         }
152 #endif
153 
154         /* Check next endpoint.  */
155         endpoint = endpoint -> ux_endpoint_next_endpoint;
156     }
157 
158     /* Return completion status.  */
159     return(status);
160 }
161 
162