- Make ACPI use PCH. Cuts down compile time to 9 seconds on gcc.
[reactos.git] / reactos / drivers / bus / acpi / ospm / busmgr / bmsearch.c
1 /******************************************************************************
2 *
3 * Module Name: bmsearch.c
4 * $Revision: 1.1 $
5 *
6 *****************************************************************************/
7
8 /*
9 * Copyright (C) 2000, 2001 Andrew Grover
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26
27 #include <acpi.h>
28
29
30 #define _COMPONENT ACPI_BUS_MANAGER
31 MODULE_NAME ("bmsearch")
32
33
34 /****************************************************************************
35 * External Functions
36 ****************************************************************************/
37
38 /****************************************************************************
39 *
40 * FUNCTION: bm_compare
41 *
42 * PARAMETERS: <TBD>
43 *
44 * RETURN: <TBD>
45 *
46 * DESCRIPTION: <TBD>
47 *
48 ****************************************************************************/
49
50 ACPI_STATUS
51 bm_compare (
52 BM_DEVICE *device,
53 BM_DEVICE_ID *criteria)
54 {
55 if (!device || !criteria) {
56 return AE_BAD_PARAMETER;
57 }
58
59 /*
60 * Present?
61 * --------
62 * We're only going to match on devices that are present.
63 * TODO: Optimize in bm_search (don't have to call here).
64 */
65 if (!BM_DEVICE_PRESENT(device)) {
66 return AE_NOT_FOUND;
67 }
68
69 /*
70 * type?
71 */
72 if (criteria->type && !(criteria->type & device->id.type)) {
73 return AE_NOT_FOUND;
74 }
75
76 /*
77 * hid?
78 */
79 if ((criteria->hid[0]) && (0 != STRNCMP(criteria->hid,
80 device->id.hid, sizeof(BM_DEVICE_HID)))) {
81 return AE_NOT_FOUND;
82 }
83
84 /*
85 * adr?
86 */
87 if ((criteria->adr) && (criteria->adr != device->id.adr)) {
88 return AE_NOT_FOUND;
89 }
90
91 return AE_OK;
92 }
93
94
95 /****************************************************************************
96 *
97 * FUNCTION: bm_search
98 *
99 * PARAMETERS: <TBD>
100 *
101 * RETURN: AE_BAD_PARAMETER- invalid input parameter
102 * AE_NOT_EXIST - start_device_handle doesn't exist
103 * AE_NOT_FOUND - no matches to Search_info.criteria found
104 * AE_OK - success
105 *
106 * DESCRIPTION: <TBD>
107 *
108 ****************************************************************************/
109
110 ACPI_STATUS
111 bm_search(
112 BM_HANDLE device_handle,
113 BM_DEVICE_ID *criteria,
114 BM_HANDLE_LIST *results)
115 {
116 ACPI_STATUS status = AE_OK;
117 BM_NODE *node = NULL;
118
119 FUNCTION_TRACE("bm_search");
120
121 if (!criteria || !results) {
122 return_ACPI_STATUS(AE_BAD_PARAMETER);
123 }
124
125 results->count = 0;
126
127 /*
128 * Locate Starting Point:
129 * ----------------------
130 * Locate the node in the hierarchy where we'll begin our search.
131 */
132 status = bm_get_node(device_handle, 0, &node);
133 if (ACPI_FAILURE(status)) {
134 return_ACPI_STATUS(status);
135 }
136
137 /*
138 * Parse Hierarchy:
139 * ----------------
140 * Parse through the node hierarchy looking for matches.
141 */
142 while (node && (results->count<=BM_HANDLES_MAX)) {
143 /*
144 * Depth-first:
145 * ------------
146 * Searches are always performed depth-first.
147 */
148 if (node->scope.head) {
149 status = bm_compare(&(node->device), criteria);
150 if (ACPI_SUCCESS(status)) {
151 results->handles[results->count++] =
152 node->device.handle;
153 }
154 node = node->scope.head;
155 }
156
157 /*
158 * Now Breadth:
159 * ------------
160 * Search all peers until scope is exhausted.
161 */
162 else {
163 status = bm_compare(&(node->device), criteria);
164 if (ACPI_SUCCESS(status)) {
165 results->handles[results->count++] =
166 node->device.handle;
167 }
168
169 /*
170 * Locate Next Device:
171 * -------------------
172 * The next node is either a peer at this level
173 * (node->next is valid), or we work are way back
174 * up the tree until we either find a non-parsed
175 * peer or hit the top (node->parent is NULL).
176 */
177 while (!node->next && node->parent) {
178 node = node->parent;
179 }
180 node = node->next;
181 }
182 }
183
184 if (results->count == 0) {
185 return_ACPI_STATUS(AE_NOT_FOUND);
186 }
187 else {
188 return_ACPI_STATUS(AE_OK);
189 }
190 }