merge ROS Shell without integrated explorer part into trunk
[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 #include "bm.h"
29
30
31 #define _COMPONENT ACPI_BUS_MANAGER
32 MODULE_NAME ("bmsearch")
33
34
35 /****************************************************************************
36 * External Functions
37 ****************************************************************************/
38
39 /****************************************************************************
40 *
41 * FUNCTION: bm_compare
42 *
43 * PARAMETERS: <TBD>
44 *
45 * RETURN: <TBD>
46 *
47 * DESCRIPTION: <TBD>
48 *
49 ****************************************************************************/
50
51 ACPI_STATUS
52 bm_compare (
53 BM_DEVICE *device,
54 BM_DEVICE_ID *criteria)
55 {
56 if (!device || !criteria) {
57 return AE_BAD_PARAMETER;
58 }
59
60 /*
61 * Present?
62 * --------
63 * We're only going to match on devices that are present.
64 * TODO: Optimize in bm_search (don't have to call here).
65 */
66 if (!BM_DEVICE_PRESENT(device)) {
67 return AE_NOT_FOUND;
68 }
69
70 /*
71 * type?
72 */
73 if (criteria->type && !(criteria->type & device->id.type)) {
74 return AE_NOT_FOUND;
75 }
76
77 /*
78 * hid?
79 */
80 if ((criteria->hid[0]) && (0 != STRNCMP(criteria->hid,
81 device->id.hid, sizeof(BM_DEVICE_HID)))) {
82 return AE_NOT_FOUND;
83 }
84
85 /*
86 * adr?
87 */
88 if ((criteria->adr) && (criteria->adr != device->id.adr)) {
89 return AE_NOT_FOUND;
90 }
91
92 return AE_OK;
93 }
94
95
96 /****************************************************************************
97 *
98 * FUNCTION: bm_search
99 *
100 * PARAMETERS: <TBD>
101 *
102 * RETURN: AE_BAD_PARAMETER- invalid input parameter
103 * AE_NOT_EXIST - start_device_handle doesn't exist
104 * AE_NOT_FOUND - no matches to Search_info.criteria found
105 * AE_OK - success
106 *
107 * DESCRIPTION: <TBD>
108 *
109 ****************************************************************************/
110
111 ACPI_STATUS
112 bm_search(
113 BM_HANDLE device_handle,
114 BM_DEVICE_ID *criteria,
115 BM_HANDLE_LIST *results)
116 {
117 ACPI_STATUS status = AE_OK;
118 BM_NODE *node = NULL;
119
120 FUNCTION_TRACE("bm_search");
121
122 if (!criteria || !results) {
123 return_ACPI_STATUS(AE_BAD_PARAMETER);
124 }
125
126 results->count = 0;
127
128 /*
129 * Locate Starting Point:
130 * ----------------------
131 * Locate the node in the hierarchy where we'll begin our search.
132 */
133 status = bm_get_node(device_handle, 0, &node);
134 if (ACPI_FAILURE(status)) {
135 return_ACPI_STATUS(status);
136 }
137
138 /*
139 * Parse Hierarchy:
140 * ----------------
141 * Parse through the node hierarchy looking for matches.
142 */
143 while (node && (results->count<=BM_HANDLES_MAX)) {
144 /*
145 * Depth-first:
146 * ------------
147 * Searches are always performed depth-first.
148 */
149 if (node->scope.head) {
150 status = bm_compare(&(node->device), criteria);
151 if (ACPI_SUCCESS(status)) {
152 results->handles[results->count++] =
153 node->device.handle;
154 }
155 node = node->scope.head;
156 }
157
158 /*
159 * Now Breadth:
160 * ------------
161 * Search all peers until scope is exhausted.
162 */
163 else {
164 status = bm_compare(&(node->device), criteria);
165 if (ACPI_SUCCESS(status)) {
166 results->handles[results->count++] =
167 node->device.handle;
168 }
169
170 /*
171 * Locate Next Device:
172 * -------------------
173 * The next node is either a peer at this level
174 * (node->next is valid), or we work are way back
175 * up the tree until we either find a non-parsed
176 * peer or hit the top (node->parent is NULL).
177 */
178 while (!node->next && node->parent) {
179 node = node->parent;
180 }
181 node = node->next;
182 }
183 }
184
185 if (results->count == 0) {
186 return_ACPI_STATUS(AE_NOT_FOUND);
187 }
188 else {
189 return_ACPI_STATUS(AE_OK);
190 }
191 }