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_client_search                    PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function searches for a HID client that wants to own this HID  */
45 /*    device.                                                             */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    hid                                   Pointer to HID class          */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    Completion Status                                                   */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    (ux_host_class_hid_client_handler)    HID client handler            */
58 /*                                                                        */
59 /*  CALLED BY                                                             */
60 /*                                                                        */
61 /*    HID Class                                                           */
62 /*                                                                        */
63 /*  RELEASE HISTORY                                                       */
64 /*                                                                        */
65 /*    DATE              NAME                      DESCRIPTION             */
66 /*                                                                        */
67 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
68 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
69 /*                                            resulting in version 6.1    */
70 /*                                                                        */
71 /**************************************************************************/
_ux_host_class_hid_client_search(UX_HOST_CLASS_HID * hid)72 UINT  _ux_host_class_hid_client_search(UX_HOST_CLASS_HID *hid)
73 {
74 
75 UX_HOST_CLASS_HID_CLIENT            *hid_client;
76 ULONG                               hid_client_index;
77 UINT                                status;
78 UX_HOST_CLASS_HID_CLIENT_COMMAND    hid_client_command;
79 
80 
81     /* In order to search a HID client, we need both the main page and the main usage.  */
82     hid_client_command.ux_host_class_hid_client_command_page =       hid -> ux_host_class_hid_parser.ux_host_class_hid_parser_main_page;
83     hid_client_command.ux_host_class_hid_client_command_usage =      hid -> ux_host_class_hid_parser.ux_host_class_hid_parser_main_usage & 0xffff;
84     hid_client_command.ux_host_class_hid_client_command_instance =   hid;
85     hid_client_command.ux_host_class_hid_client_command_container =  (VOID *) hid -> ux_host_class_hid_class;
86     hid_client_command.ux_host_class_hid_client_command_request =    UX_HOST_CLASS_COMMAND_QUERY;
87 
88     /* Dereference the client pointer into a HID client pointer.  */
89     hid_client =  (UX_HOST_CLASS_HID_CLIENT *) hid -> ux_host_class_hid_class -> ux_host_class_client;
90 
91     /* If the hid_client pointer is NULL, the array of clients was not initialized.  */
92     if (hid_client == UX_NULL)
93     {
94 
95         /* Error trap. */
96         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_HID_UNKNOWN);
97 
98         /* If trace is enabled, insert this event into the trace buffer.  */
99         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_HID_UNKNOWN, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
100 
101         return(UX_HOST_CLASS_HID_UNKNOWN);
102     }
103 
104     /* We need to parse the hid client driver table to find a registered client.  */
105     for (hid_client_index = 0; hid_client_index < UX_HOST_CLASS_HID_MAX_CLIENTS; hid_client_index++)
106     {
107 
108         /* Check if this HID client is registered. */
109         if (hid_client -> ux_host_class_hid_client_status == UX_USED)
110         {
111 
112             /* Call the HID client with a query command.  */
113             status =  hid_client -> ux_host_class_hid_client_handler(&hid_client_command);
114 
115             /* Have we found a HID client?  */
116             if (status == UX_SUCCESS)
117             {
118 
119                 /* Update the command to activate the client.  */
120                 hid_client_command.ux_host_class_hid_client_command_request =  UX_HOST_CLASS_COMMAND_ACTIVATE;
121 
122                 /* Memorize the client for this HID device.  */
123                 hid -> ux_host_class_hid_client =  hid_client;
124 
125                 /* Call the HID client with an activate command.  */
126                 status =  hid_client -> ux_host_class_hid_client_handler(&hid_client_command);
127 
128                 /* Unmount the client if activation fail. */
129                 if (status != UX_SUCCESS)
130                     hid -> ux_host_class_hid_client = UX_NULL;
131 
132                 /* Return completion status.  */
133                 return(status);
134             }
135         }
136 
137         /* Try the next HID client.  */
138         hid_client++;
139     }
140 
141     /* Error trap. */
142     _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_HID_UNKNOWN);
143 
144     /* If trace is enabled, insert this event into the trace buffer.  */
145     UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_HID_UNKNOWN, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
146 
147     /* Return an error.  */
148     return(UX_HOST_CLASS_HID_UNKNOWN);
149 }
150 
151