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