Merge the following revisions from kernel-fun branch:
[reactos.git] / reactos / drivers / bus / acpi / busmgr / bus.c
1 /*
2 * acpi_bus.c - ACPI Bus Driver ($Revision: 80 $)
3 *
4 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
5 *
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 *
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 */
24
25 /*
26 * Modified for ReactOS and latest ACPICA
27 * Copyright (C)2009 Samuel Serapion
28 */
29
30 #include <precomp.h>
31
32 #define NDEBUG
33 #include <debug.h>
34
35 #define _COMPONENT ACPI_BUS_COMPONENT
36 ACPI_MODULE_NAME ("acpi_bus")
37
38 #define WALK_UP 0
39 #define WALK_DOWN 1
40
41 #define STRUCT_TO_INT(s) (*((int*)&s))
42 #define HAS_CHILDREN(d) ((d)->children.next != &((d)->children))
43 #define HAS_SIBLINGS(d) (((d)->parent) && ((d)->node.next != &(d)->parent->children))
44 #define NODE_TO_DEVICE(n) (list_entry(n, struct acpi_device, node))
45
46 int event_is_open;
47 extern void acpi_pic_sci_set_trigger(unsigned int irq, UINT16 trigger);
48
49 typedef int (*acpi_bus_walk_callback)(struct acpi_device*, int, void*);
50
51 struct acpi_device *acpi_root;
52 KSPIN_LOCK acpi_bus_event_lock;
53 LIST_HEAD(acpi_bus_event_list);
54 //DECLARE_WAIT_QUEUE_HEAD(acpi_bus_event_queue);
55 KEVENT AcpiEventQueue;
56 KDPC event_dpc;
57
58 int ProcessorCount, PowerDeviceCount, PowerButtonCount, FixedPowerButtonCount;
59 int FixedSleepButtonCount, SleepButtonCount, ThermalZoneCount;
60
61 static int
62 acpi_device_register (
63 struct acpi_device *device,
64 struct acpi_device *parent)
65 {
66 int result = 0;
67
68 if (!device)
69 return_VALUE(AE_BAD_PARAMETER);
70
71 return_VALUE(result);
72 }
73
74
75 static int
76 acpi_device_unregister (
77 struct acpi_device *device)
78 {
79 if (!device)
80 return_VALUE(AE_BAD_PARAMETER);
81
82 #ifdef CONFIG_LDM
83 put_device(&device->dev);
84 #endif /*CONFIG_LDM*/
85
86 return_VALUE(0);
87 }
88
89
90 /* --------------------------------------------------------------------------
91 Device Management
92 -------------------------------------------------------------------------- */
93
94 void
95 acpi_bus_data_handler (
96 ACPI_HANDLE handle,
97 void *context)
98 {
99 DPRINT1("acpi_bus_data_handler not implemented\n");
100
101 /* TBD */
102
103 return;
104 }
105
106
107 int
108 acpi_bus_get_device (
109 ACPI_HANDLE handle,
110 struct acpi_device **device)
111 {
112 ACPI_STATUS status = AE_OK;
113
114 if (!device)
115 return_VALUE(AE_BAD_PARAMETER);
116
117 /* TBD: Support fixed-feature devices */
118
119 status = AcpiGetData(handle, acpi_bus_data_handler, (void**)device);
120 if (ACPI_FAILURE(status) || !*device) {
121 DPRINT( "Error getting context for object [%p]\n",
122 handle);
123 return_VALUE(AE_NOT_FOUND);
124 }
125
126 return 0;
127 }
128
129 ACPI_STATUS acpi_bus_get_status_handle(ACPI_HANDLE handle,
130 unsigned long long *sta)
131 {
132 ACPI_STATUS status;
133
134 status = acpi_evaluate_integer(handle, "_STA", NULL, sta);
135 if (ACPI_SUCCESS(status))
136 return AE_OK;
137
138 if (status == AE_NOT_FOUND) {
139 *sta = ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
140 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
141 return AE_OK;
142 }
143 return status;
144 }
145
146 int
147 acpi_bus_get_status (
148 struct acpi_device *device)
149 {
150 ACPI_STATUS status;
151 unsigned long long sta;
152
153 status = acpi_bus_get_status_handle(device->handle, &sta);
154 if (ACPI_FAILURE(status))
155 return -1;
156
157 STRUCT_TO_INT(device->status) = (int) sta;
158
159 if (device->status.functional && !device->status.present) {
160 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]: "
161 "functional but not present;\n",
162 device->pnp.bus_id,
163 (UINT32) STRUCT_TO_INT(device->status)));
164 }
165
166 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]\n",
167 device->pnp.bus_id,
168 (UINT32) STRUCT_TO_INT(device->status)));
169 return 0;
170 }
171
172 void acpi_bus_private_data_handler(ACPI_HANDLE handle,
173 void *context)
174 {
175 return;
176 }
177
178 int acpi_bus_get_private_data(ACPI_HANDLE handle, void **data)
179 {
180 ACPI_STATUS status = AE_OK;
181
182 if (!*data)
183 return -1;
184
185 status = AcpiGetData(handle, acpi_bus_private_data_handler, data);
186 if (ACPI_FAILURE(status) || !*data) {
187 DPRINT("No context for object [%p]\n", handle);
188 return -1;
189 }
190
191 return 0;
192 }
193 /* --------------------------------------------------------------------------
194 Power Management
195 -------------------------------------------------------------------------- */
196
197 int
198 acpi_bus_get_power (
199 ACPI_HANDLE handle,
200 int *state)
201 {
202 int result = 0;
203 ACPI_STATUS status = 0;
204 struct acpi_device *device = NULL;
205 unsigned long long psc = 0;
206
207 result = acpi_bus_get_device(handle, &device);
208 if (result)
209 return_VALUE(result);
210
211 *state = ACPI_STATE_UNKNOWN;
212
213 if (!device->flags.power_manageable) {
214 /* TBD: Non-recursive algorithm for walking up hierarchy */
215 if (device->parent)
216 *state = device->parent->power.state;
217 else
218 *state = ACPI_STATE_D0;
219 }
220 else {
221 /*
222 * Get the device's power state either directly (via _PSC) or
223 * indirectly (via power resources).
224 */
225 if (device->power.flags.explicit_get) {
226 status = acpi_evaluate_integer(device->handle, "_PSC",
227 NULL, &psc);
228 if (ACPI_FAILURE(status))
229 return_VALUE(AE_NOT_FOUND);
230 device->power.state = (int) psc;
231 }
232 else if (device->power.flags.power_resources) {
233 result = acpi_power_get_inferred_state(device);
234 if (result)
235 return_VALUE(result);
236 }
237
238 *state = device->power.state;
239 }
240
241 DPRINT("Device [%s] power state is D%d\n",
242 device->pnp.bus_id, device->power.state);
243
244 return_VALUE(0);
245 }
246
247
248 int
249 acpi_bus_set_power (
250 ACPI_HANDLE handle,
251 int state)
252 {
253 int result = 0;
254 ACPI_STATUS status = AE_OK;
255 struct acpi_device *device = NULL;
256 char object_name[5] = {'_','P','S','0'+state,'\0'};
257
258
259 result = acpi_bus_get_device(handle, &device);
260 if (result)
261 return_VALUE(result);
262
263 if ((state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
264 return_VALUE(AE_BAD_PARAMETER);
265
266 /* Make sure this is a valid target state */
267
268 if (!device->flags.power_manageable) {
269 DPRINT1( "Device is not power manageable\n");
270 return_VALUE(AE_NOT_FOUND);
271 }
272 /*
273 * Get device's current power state
274 */
275 //if (!acpi_power_nocheck) {
276 /*
277 * Maybe the incorrect power state is returned on the bogus
278 * bios, which is different with the real power state.
279 * For example: the bios returns D0 state and the real power
280 * state is D3. OS expects to set the device to D0 state. In
281 * such case if OS uses the power state returned by the BIOS,
282 * the device can't be transisted to the correct power state.
283 * So if the acpi_power_nocheck is set, it is unnecessary to
284 * get the power state by calling acpi_bus_get_power.
285 */
286 acpi_bus_get_power(device->handle, &device->power.state);
287 //}
288
289 if ((state == device->power.state) && !device->flags.force_power_state) {
290 DPRINT1("Device is already at D%d\n", state);
291 return 0;
292 }
293 if (!device->power.states[state].flags.valid) {
294 DPRINT1( "Device does not support D%d\n", state);
295 return AE_NOT_FOUND;
296 }
297 if (device->parent && (state < device->parent->power.state)) {
298 DPRINT1( "Cannot set device to a higher-powered state than parent\n");
299 return AE_NOT_FOUND;
300 }
301
302 /*
303 * Transition Power
304 * ----------------
305 * On transitions to a high-powered state we first apply power (via
306 * power resources) then evalute _PSx. Conversly for transitions to
307 * a lower-powered state.
308 */
309 if (state < device->power.state) {
310 if (device->power.flags.power_resources) {
311 result = acpi_power_transition(device, state);
312 if (result)
313 goto end;
314 }
315 if (device->power.states[state].flags.explicit_set) {
316 status = AcpiEvaluateObject(device->handle,
317 object_name, NULL, NULL);
318 if (ACPI_FAILURE(status)) {
319 result = AE_NOT_FOUND;
320 goto end;
321 }
322 }
323 }
324 else {
325 if (device->power.states[state].flags.explicit_set) {
326 status = AcpiEvaluateObject(device->handle,
327 object_name, NULL, NULL);
328 if (ACPI_FAILURE(status)) {
329 result = AE_NOT_FOUND;
330 goto end;
331 }
332 }
333 if (device->power.flags.power_resources) {
334 result = acpi_power_transition(device, state);
335 if (result)
336 goto end;
337 }
338 }
339
340 end:
341 if (result)
342 DPRINT( "Error transitioning device [%s] to D%d\n",
343 device->pnp.bus_id, state);
344 else
345 DPRINT("Device [%s] transitioned to D%d\n",
346 device->pnp.bus_id, state);
347
348 return result;
349 }
350
351 BOOLEAN acpi_bus_power_manageable(ACPI_HANDLE handle)
352 {
353 struct acpi_device *device;
354 int result;
355
356 result = acpi_bus_get_device(handle, &device);
357 return result ? 0 : device->flags.power_manageable;
358 }
359
360 BOOLEAN acpi_bus_can_wakeup(ACPI_HANDLE handle)
361 {
362 struct acpi_device *device;
363 int result;
364
365 result = acpi_bus_get_device(handle, &device);
366 return result ? 0 : device->wakeup.flags.valid;
367 }
368
369 static int
370 acpi_bus_get_power_flags (
371 struct acpi_device *device)
372 {
373 ACPI_STATUS status = 0;
374 ACPI_HANDLE handle = 0;
375 UINT32 i = 0;
376
377 if (!device)
378 return AE_NOT_FOUND;
379
380 /*
381 * Power Management Flags
382 */
383 status = AcpiGetHandle(device->handle, "_PSC", &handle);
384 if (ACPI_SUCCESS(status))
385 device->power.flags.explicit_get = 1;
386 status = AcpiGetHandle(device->handle, "_IRC", &handle);
387 if (ACPI_SUCCESS(status))
388 device->power.flags.inrush_current = 1;
389 status = AcpiGetHandle(device->handle, "_PRW", &handle);
390 if (ACPI_SUCCESS(status))
391 device->flags.wake_capable = 1;
392
393 /*
394 * Enumerate supported power management states
395 */
396 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
397 struct acpi_device_power_state *ps = &device->power.states[i];
398 char object_name[5] = {'_','P','R','0'+i,'\0'};
399
400 /* Evaluate "_PRx" to se if power resources are referenced */
401 status = acpi_evaluate_reference(device->handle, object_name, NULL,
402 &ps->resources);
403 if (ACPI_SUCCESS(status) && ps->resources.count) {
404 device->power.flags.power_resources = 1;
405 ps->flags.valid = 1;
406 }
407
408 /* Evaluate "_PSx" to see if we can do explicit sets */
409 object_name[2] = 'S';
410 status = AcpiGetHandle(device->handle, object_name, &handle);
411 if (ACPI_SUCCESS(status)) {
412 ps->flags.explicit_set = 1;
413 ps->flags.valid = 1;
414 }
415
416 /* State is valid if we have some power control */
417 if (ps->resources.count || ps->flags.explicit_set)
418 ps->flags.valid = 1;
419
420 ps->power = -1; /* Unknown - driver assigned */
421 ps->latency = -1; /* Unknown - driver assigned */
422 }
423
424 /* Set defaults for D0 and D3 states (always valid) */
425 device->power.states[ACPI_STATE_D0].flags.valid = 1;
426 device->power.states[ACPI_STATE_D0].power = 100;
427 device->power.states[ACPI_STATE_D3].flags.valid = 1;
428 device->power.states[ACPI_STATE_D3].power = 0;
429
430 device->power.state = ACPI_STATE_UNKNOWN;
431
432 return 0;
433 }
434
435 /* --------------------------------------------------------------------------
436 Performance Management
437 -------------------------------------------------------------------------- */
438
439 static int
440 acpi_bus_get_perf_flags (
441 struct acpi_device *device)
442 {
443 if (!device)
444 return AE_NOT_FOUND;
445
446 device->performance.state = ACPI_STATE_UNKNOWN;
447
448 return 0;
449 }
450
451
452 /* --------------------------------------------------------------------------
453 Event Management
454 -------------------------------------------------------------------------- */
455
456 void
457 NTAPI
458 acpi_bus_generate_event_dpc(PKDPC Dpc,
459 PVOID DeferredContext,
460 PVOID SystemArgument1,
461 PVOID SystemArgument2)
462 {
463 struct acpi_bus_event *event;
464 struct acpi_device *device = SystemArgument1;
465 ULONG_PTR TypeData = (ULONG_PTR)SystemArgument2;
466 KIRQL OldIrql;
467
468 event = ExAllocatePoolWithTag(NonPagedPool,sizeof(struct acpi_bus_event), 'IPCA');
469 if (!event)
470 return;
471
472 sprintf(event->device_class, "%s", device->pnp.device_class);
473 sprintf(event->bus_id, "%s", device->pnp.bus_id);
474 event->type = (TypeData & 0xFF000000) >> 24;
475 event->data = (TypeData & 0x00FFFFFF);
476
477 KeAcquireSpinLock(&acpi_bus_event_lock, &OldIrql);
478 list_add_tail(&event->node, &acpi_bus_event_list);
479 KeReleaseSpinLock(&acpi_bus_event_lock, OldIrql);
480
481 KeSetEvent(&AcpiEventQueue, IO_NO_INCREMENT, FALSE);
482 }
483
484 int
485 acpi_bus_generate_event (
486 struct acpi_device *device,
487 UINT8 type,
488 int data)
489 {
490 ULONG_PTR TypeData = 0;
491
492 DPRINT("acpi_bus_generate_event\n");
493
494 if (!device)
495 return_VALUE(AE_BAD_PARAMETER);
496
497 /* drop event on the floor if no one's listening */
498 if (!event_is_open)
499 return_VALUE(0);
500
501 /* Data shouldn't even get near 24 bits */
502 ASSERT(!(data & 0xFF000000));
503
504 TypeData = data;
505 TypeData |= type << 24;
506
507 KeInsertQueueDpc(&event_dpc, device, (PVOID)TypeData);
508
509 return_VALUE(0);
510 }
511
512 int
513 acpi_bus_receive_event (
514 struct acpi_bus_event *event)
515 {
516 // unsigned long flags = 0;
517 struct acpi_bus_event *entry = NULL;
518 KIRQL OldIrql;
519
520 //DECLARE_WAITQUEUE(wait, current);
521
522 DPRINT("acpi_bus_receive_event\n");
523
524 if (!event)
525 return AE_BAD_PARAMETER;
526
527 event_is_open++;
528 KeWaitForSingleObject(&AcpiEventQueue,
529 Executive,
530 KernelMode,
531 FALSE,
532 NULL);
533 event_is_open--;
534 KeClearEvent(&AcpiEventQueue);
535
536 if (list_empty(&acpi_bus_event_list))
537 return_VALUE(AE_NOT_FOUND);
538
539 // spin_lock_irqsave(&acpi_bus_event_lock, flags);
540 KeAcquireSpinLock(&acpi_bus_event_lock, &OldIrql);
541 entry = list_entry(acpi_bus_event_list.next, struct acpi_bus_event, node);
542 if (entry)
543 list_del(&entry->node);
544 KeReleaseSpinLock(&acpi_bus_event_lock, OldIrql);
545 // spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
546
547 if (!entry)
548 return_VALUE(AE_NOT_FOUND);
549
550 memcpy(event, entry, sizeof(struct acpi_bus_event));
551
552 ExFreePoolWithTag(entry, 'IPCA');
553 return_VALUE(0);
554 }
555
556
557 /* --------------------------------------------------------------------------
558 Namespace Management
559 -------------------------------------------------------------------------- */
560
561
562 /**
563 * acpi_bus_walk
564 * -------------
565 * Used to walk the ACPI Bus's device namespace. Can walk down (depth-first)
566 * or up. Able to parse starting at any node in the namespace. Note that a
567 * callback return value of -249 will terminate the walk.
568 *
569 * @start: starting point
570 * callback: function to call for every device encountered while parsing
571 * direction: direction to parse (up or down)
572 * @data: context for this search operation
573 */
574 static int
575 acpi_bus_walk (
576 struct acpi_device *start,
577 acpi_bus_walk_callback callback,
578 int direction,
579 void *data)
580 {
581 int result = 0;
582 int level = 0;
583 struct acpi_device *device = NULL;
584
585 if (!start || !callback)
586 return AE_BAD_PARAMETER;
587
588 device = start;
589
590 /*
591 * Parse Namespace
592 * ---------------
593 * Parse a given subtree (specified by start) in the given direction.
594 * Walking 'up' simply means that we execute the callback on leaf
595 * devices prior to their parents (useful for things like removing
596 * or powering down a subtree).
597 */
598
599 while (device) {
600
601 if (direction == WALK_DOWN)
602 if (-249 == callback(device, level, data))
603 break;
604
605 /* Depth First */
606
607 if (HAS_CHILDREN(device)) {
608 device = NODE_TO_DEVICE(device->children.next);
609 ++level;
610 continue;
611 }
612
613 if (direction == WALK_UP)
614 if (-249 == callback(device, level, data))
615 break;
616
617 /* Now Breadth */
618
619 if (HAS_SIBLINGS(device)) {
620 device = NODE_TO_DEVICE(device->node.next);
621 continue;
622 }
623
624 /* Scope Exhausted - Find Next */
625
626 while ((device = device->parent)) {
627 --level;
628 if (HAS_SIBLINGS(device)) {
629 device = NODE_TO_DEVICE(device->node.next);
630 break;
631 }
632 }
633 }
634
635 if ((direction == WALK_UP) && (result == 0))
636 callback(start, level, data);
637
638 return result;
639 }
640
641
642 /* --------------------------------------------------------------------------
643 Notification Handling
644 -------------------------------------------------------------------------- */
645
646 static void
647 acpi_bus_check_device (ACPI_HANDLE handle)
648 {
649 struct acpi_device *device;
650 ACPI_STATUS status = 0;
651 struct acpi_device_status old_status;
652
653 if (acpi_bus_get_device(handle, &device))
654 return;
655 if (!device)
656 return;
657
658 old_status = device->status;
659
660 /*
661 * Make sure this device's parent is present before we go about
662 * messing with the device.
663 */
664 if (device->parent && !device->parent->status.present) {
665 device->status = device->parent->status;
666 return;
667 }
668
669 status = acpi_bus_get_status(device);
670 if (ACPI_FAILURE(status))
671 return;
672
673 if (STRUCT_TO_INT(old_status) == STRUCT_TO_INT(device->status))
674 return;
675
676
677 /*
678 * Device Insertion/Removal
679 */
680 if ((device->status.present) && !(old_status.present)) {
681 DPRINT("Device insertion detected\n");
682 /* TBD: Handle device insertion */
683 }
684 else if (!(device->status.present) && (old_status.present)) {
685 DPRINT("Device removal detected\n");
686 /* TBD: Handle device removal */
687 }
688
689 }
690
691
692 static void
693 acpi_bus_check_scope (ACPI_HANDLE handle)
694 {
695 /* Status Change? */
696 acpi_bus_check_device(handle);
697
698 /*
699 * TBD: Enumerate child devices within this device's scope and
700 * run acpi_bus_check_device()'s on them.
701 */
702 }
703
704
705 /**
706 * acpi_bus_notify
707 * ---------------
708 * Callback for all 'system-level' device notifications (values 0x00-0x7F).
709 */
710 static void
711 acpi_bus_notify (
712 ACPI_HANDLE handle,
713 UINT32 type,
714 void *data)
715 {
716 struct acpi_device *device = NULL;
717 struct acpi_driver *driver;
718
719 DPRINT1("Notification %#02x to handle %p\n", type, handle);
720
721 //blocking_notifier_call_chain(&acpi_bus_notify_list,
722 // type, (void *)handle);
723
724 switch (type) {
725
726 case ACPI_NOTIFY_BUS_CHECK:
727 DPRINT("Received BUS CHECK notification for device [%s]\n",
728 device->pnp.bus_id);
729 acpi_bus_check_scope(handle);
730 /*
731 * TBD: We'll need to outsource certain events to non-ACPI
732 * drivers via the device manager (device.c).
733 */
734 break;
735
736 case ACPI_NOTIFY_DEVICE_CHECK:
737 DPRINT("Received DEVICE CHECK notification for device [%s]\n",
738 device->pnp.bus_id);
739 acpi_bus_check_device(handle);
740 /*
741 * TBD: We'll need to outsource certain events to non-ACPI
742 * drivers via the device manager (device.c).
743 */
744 break;
745
746 case ACPI_NOTIFY_DEVICE_WAKE:
747 DPRINT("Received DEVICE WAKE notification for device [%s]\n",
748 device->pnp.bus_id);
749 acpi_bus_check_device(handle);
750 /*
751 * TBD: We'll need to outsource certain events to non-ACPI
752 * drivers via the device manager (device.c).
753 */
754 break;
755
756 case ACPI_NOTIFY_EJECT_REQUEST:
757 DPRINT1("Received EJECT REQUEST notification for device [%s]\n",
758 device->pnp.bus_id);
759 /* TBD */
760 break;
761
762 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
763 DPRINT1("Received DEVICE CHECK LIGHT notification for device [%s]\n",
764 device->pnp.bus_id);
765 /* TBD: Exactly what does 'light' mean? */
766 break;
767
768 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
769 DPRINT1("Received FREQUENCY MISMATCH notification for device [%s]\n",
770 device->pnp.bus_id);
771 /* TBD */
772 break;
773
774 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
775 DPRINT1("Received BUS MODE MISMATCH notification for device [%s]\n",
776 device->pnp.bus_id);
777 /* TBD */
778 break;
779
780 case ACPI_NOTIFY_POWER_FAULT:
781 DPRINT1("Received POWER FAULT notification for device [%s]\n",
782 device->pnp.bus_id);
783 /* TBD */
784 break;
785
786 default:
787 DPRINT1("Received unknown/unsupported notification [%08x]\n",
788 type);
789 break;
790 }
791
792 acpi_bus_get_device(handle, &device);
793 if (device) {
794 driver = device->driver;
795 if (driver && driver->ops.notify &&
796 (driver->flags & ACPI_DRIVER_ALL_NOTIFY_EVENTS))
797 driver->ops.notify(device, type);
798 }
799 }
800
801
802 /* --------------------------------------------------------------------------
803 Driver Management
804 -------------------------------------------------------------------------- */
805
806
807 static LIST_HEAD(acpi_bus_drivers);
808 //static DECLARE_MUTEX(acpi_bus_drivers_lock);
809 static FAST_MUTEX acpi_bus_drivers_lock;
810
811
812 /**
813 * acpi_bus_match
814 * --------------
815 * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it
816 * matches the specified driver's criteria.
817 */
818 static int
819 acpi_bus_match (
820 struct acpi_device *device,
821 struct acpi_driver *driver)
822 {
823 int error = 0;
824
825 if (device->flags.hardware_id)
826 if (strstr(driver->ids, device->pnp.hardware_id))
827 goto Done;
828
829 if (device->flags.compatible_ids) {
830 ACPI_PNP_DEVICE_ID_LIST *cid_list = device->pnp.cid_list;
831 int i;
832
833 /* compare multiple _CID entries against driver ids */
834 for (i = 0; i < cid_list->Count; i++)
835 {
836 if (strstr(driver->ids, cid_list->Ids[i].String))
837 goto Done;
838 }
839 }
840 error = -2;
841
842 Done:
843
844 return error;
845 }
846
847
848 /**
849 * acpi_bus_driver_init
850 * --------------------
851 * Used to initialize a device via its device driver. Called whenever a
852 * driver is bound to a device. Invokes the driver's add() and start() ops.
853 */
854 static int
855 acpi_bus_driver_init (
856 struct acpi_device *device,
857 struct acpi_driver *driver)
858 {
859 int result = 0;
860
861 if (!device || !driver)
862 return_VALUE(AE_BAD_PARAMETER);
863
864 if (!driver->ops.add)
865 return_VALUE(-38);
866
867 result = driver->ops.add(device);
868 if (result) {
869 device->driver = NULL;
870 //acpi_driver_data(device) = NULL;
871 return_VALUE(result);
872 }
873
874 device->driver = driver;
875
876 /*
877 * TBD - Configuration Management: Assign resources to device based
878 * upon possible configuration and currently allocated resources.
879 */
880
881 if (driver->ops.start) {
882 result = driver->ops.start(device);
883 if (result && driver->ops.remove)
884 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
885 return_VALUE(result);
886 }
887
888 DPRINT("Driver successfully bound to device\n");
889
890 if (driver->ops.scan) {
891 driver->ops.scan(device);
892 }
893
894 return_VALUE(0);
895 }
896
897
898 /**
899 * acpi_bus_attach
900 * -------------
901 * Callback for acpi_bus_walk() used to find devices that match a specific
902 * driver's criteria and then attach the driver.
903 */
904 static int
905 acpi_bus_attach (
906 struct acpi_device *device,
907 int level,
908 void *data)
909 {
910 int result = 0;
911 struct acpi_driver *driver = NULL;
912
913 if (!device || !data)
914 return_VALUE(AE_BAD_PARAMETER);
915
916 driver = (struct acpi_driver *) data;
917
918 if (device->driver)
919 return_VALUE(-9);
920
921 if (!device->status.present)
922 return_VALUE(AE_NOT_FOUND);
923
924 result = acpi_bus_match(device, driver);
925 if (result)
926 return_VALUE(result);
927
928 DPRINT("Found driver [%s] for device [%s]\n",
929 driver->name, device->pnp.bus_id);
930
931 result = acpi_bus_driver_init(device, driver);
932 if (result)
933 return_VALUE(result);
934
935 down(&acpi_bus_drivers_lock);
936 ++driver->references;
937 up(&acpi_bus_drivers_lock);
938
939 return_VALUE(0);
940 }
941
942
943 /**
944 * acpi_bus_unattach
945 * -----------------
946 * Callback for acpi_bus_walk() used to find devices that match a specific
947 * driver's criteria and unattach the driver.
948 */
949 static int
950 acpi_bus_unattach (
951 struct acpi_device *device,
952 int level,
953 void *data)
954 {
955 int result = 0;
956 struct acpi_driver *driver = (struct acpi_driver *) data;
957
958 if (!device || !driver)
959 return_VALUE(AE_BAD_PARAMETER);
960
961 if (device->driver != driver)
962 return_VALUE(-6);
963
964 if (!driver->ops.remove)
965 return_VALUE(-23);
966
967 result = driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
968 if (result)
969 return_VALUE(result);
970
971 device->driver = NULL;
972 acpi_driver_data(device) = NULL;
973
974 down(&acpi_bus_drivers_lock);
975 driver->references--;
976 up(&acpi_bus_drivers_lock);
977
978 return_VALUE(0);
979 }
980
981
982 /**
983 * acpi_bus_find_driver
984 * --------------------
985 * Parses the list of registered drivers looking for a driver applicable for
986 * the specified device.
987 */
988 static int
989 acpi_bus_find_driver (
990 struct acpi_device *device)
991 {
992 int result = AE_NOT_FOUND;
993 struct list_head *entry = NULL;
994 struct acpi_driver *driver = NULL;
995
996 if (!device || device->driver)
997 return_VALUE(AE_BAD_PARAMETER);
998
999 down(&acpi_bus_drivers_lock);
1000
1001 list_for_each(entry, &acpi_bus_drivers) {
1002
1003 driver = list_entry(entry, struct acpi_driver, node);
1004
1005 if (acpi_bus_match(device, driver))
1006 continue;
1007
1008 result = acpi_bus_driver_init(device, driver);
1009 if (!result)
1010 ++driver->references;
1011
1012 break;
1013 }
1014
1015 up(&acpi_bus_drivers_lock);
1016
1017 return_VALUE(result);
1018 }
1019
1020
1021 /**
1022 * acpi_bus_register_driver
1023 * ------------------------
1024 * Registers a driver with the ACPI bus. Searches the namespace for all
1025 * devices that match the driver's criteria and binds.
1026 */
1027 int
1028 acpi_bus_register_driver (
1029 struct acpi_driver *driver)
1030 {
1031 if (!driver)
1032 return_VALUE(AE_BAD_PARAMETER);
1033
1034 //if (acpi_disabled)
1035 // return_VALUE(AE_NOT_FOUND);
1036
1037 down(&acpi_bus_drivers_lock);
1038 list_add_tail(&driver->node, &acpi_bus_drivers);
1039 up(&acpi_bus_drivers_lock);
1040
1041 acpi_bus_walk(acpi_root, acpi_bus_attach,
1042 WALK_DOWN, driver);
1043
1044 return_VALUE(driver->references);
1045 }
1046
1047
1048 /**
1049 * acpi_bus_unregister_driver
1050 * --------------------------
1051 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1052 * devices that match the driver's criteria and unbinds.
1053 */
1054 void
1055 acpi_bus_unregister_driver (
1056 struct acpi_driver *driver)
1057 {
1058 if (!driver)
1059 return;
1060
1061 acpi_bus_walk(acpi_root, acpi_bus_unattach, WALK_UP, driver);
1062
1063 if (driver->references)
1064 return;
1065
1066 down(&acpi_bus_drivers_lock);
1067 list_del(&driver->node);
1068 up(&acpi_bus_drivers_lock);
1069
1070 return;
1071 }
1072
1073
1074 /* --------------------------------------------------------------------------
1075 Device Enumeration
1076 -------------------------------------------------------------------------- */
1077
1078 static int
1079 acpi_bus_get_flags (
1080 struct acpi_device *device)
1081 {
1082 ACPI_STATUS status = AE_OK;
1083 ACPI_HANDLE temp = NULL;
1084
1085 /* Presence of _STA indicates 'dynamic_status' */
1086 status = AcpiGetHandle(device->handle, "_STA", &temp);
1087 if (ACPI_SUCCESS(status))
1088 device->flags.dynamic_status = 1;
1089
1090 /* Presence of _CID indicates 'compatible_ids' */
1091 status = AcpiGetHandle(device->handle, "_CID", &temp);
1092 if (ACPI_SUCCESS(status))
1093 device->flags.compatible_ids = 1;
1094
1095 /* Presence of _RMV indicates 'removable' */
1096 status = AcpiGetHandle(device->handle, "_RMV", &temp);
1097 if (ACPI_SUCCESS(status))
1098 device->flags.removable = 1;
1099
1100 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1101 status = AcpiGetHandle(device->handle, "_EJD", &temp);
1102 if (ACPI_SUCCESS(status))
1103 device->flags.ejectable = 1;
1104 else {
1105 status = AcpiGetHandle(device->handle, "_EJ0", &temp);
1106 if (ACPI_SUCCESS(status))
1107 device->flags.ejectable = 1;
1108 }
1109
1110 /* Presence of _LCK indicates 'lockable' */
1111 status = AcpiGetHandle(device->handle, "_LCK", &temp);
1112 if (ACPI_SUCCESS(status))
1113 device->flags.lockable = 1;
1114
1115 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1116 status = AcpiGetHandle(device->handle, "_PS0", &temp);
1117 if (ACPI_FAILURE(status))
1118 status = AcpiGetHandle(device->handle, "_PR0", &temp);
1119 if (ACPI_SUCCESS(status))
1120 device->flags.power_manageable = 1;
1121
1122 /* TBD: Peformance management */
1123
1124 return_VALUE(0);
1125 }
1126
1127
1128 int
1129 acpi_bus_add (
1130 struct acpi_device **child,
1131 struct acpi_device *parent,
1132 ACPI_HANDLE handle,
1133 int type)
1134 {
1135 int result = 0;
1136 ACPI_STATUS status = AE_OK;
1137 struct acpi_device *device = NULL;
1138 char bus_id[5] = {'?',0};
1139 ACPI_BUFFER buffer;
1140 ACPI_DEVICE_INFO *info;
1141 char *hid = NULL;
1142 char *uid = NULL;
1143 ACPI_PNP_DEVICE_ID_LIST *cid_list = NULL;
1144 int i = 0;
1145 acpi_unique_id static_uid_buffer;
1146
1147 if (!child)
1148 return_VALUE(AE_BAD_PARAMETER);
1149
1150 device = ExAllocatePoolWithTag(NonPagedPool,sizeof(struct acpi_device), 'IPCA');
1151 if (!device) {
1152 DPRINT1("Memory allocation error\n");
1153 return_VALUE(-12);
1154 }
1155 memset(device, 0, sizeof(struct acpi_device));
1156
1157 device->handle = handle;
1158 device->parent = parent;
1159
1160 /*
1161 * Bus ID
1162 * ------
1163 * The device's Bus ID is simply the object name.
1164 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1165 */
1166 switch (type) {
1167 case ACPI_BUS_TYPE_SYSTEM:
1168 snprintf(device->pnp.bus_id, sizeof(device->pnp.bus_id), "%s", "ACPI");
1169 break;
1170 case ACPI_BUS_TYPE_POWER_BUTTONF:
1171 case ACPI_BUS_TYPE_POWER_BUTTON:
1172 snprintf(device->pnp.bus_id, sizeof(device->pnp.bus_id), "%s", "PWRF");
1173 break;
1174 case ACPI_BUS_TYPE_SLEEP_BUTTONF:
1175 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1176 snprintf(device->pnp.bus_id, sizeof(device->pnp.bus_id), "%s", "SLPF");
1177 break;
1178 default:
1179 buffer.Length = sizeof(bus_id);
1180 buffer.Pointer = bus_id;
1181 AcpiGetName(handle, ACPI_SINGLE_NAME, &buffer);
1182
1183
1184 /* Clean up trailing underscores (if any) */
1185 for (i = 3; i > 1; i--) {
1186 if (bus_id[i] == '_')
1187 bus_id[i] = '\0';
1188 else
1189 break;
1190 }
1191 snprintf(device->pnp.bus_id, sizeof(device->pnp.bus_id), "%s", bus_id);
1192 buffer.Pointer = NULL;
1193
1194 /* HACK: Skip HPET */
1195 if (strstr(device->pnp.bus_id, "HPET"))
1196 {
1197 DPRINT("Using HPET hack\n");
1198 result = -1;
1199 goto end;
1200 }
1201
1202 break;
1203 }
1204
1205 /*
1206 * Flags
1207 * -----
1208 * Get prior to calling acpi_bus_get_status() so we know whether
1209 * or not _STA is present. Note that we only look for object
1210 * handles -- cannot evaluate objects until we know the device is
1211 * present and properly initialized.
1212 */
1213 result = acpi_bus_get_flags(device);
1214 if (result)
1215 goto end;
1216
1217 /*
1218 * Status
1219 * ------
1220 * See if the device is present. We always assume that non-Device()
1221 * objects (e.g. thermal zones, power resources, processors, etc.) are
1222 * present, functioning, etc. (at least when parent object is present).
1223 * Note that _STA has a different meaning for some objects (e.g.
1224 * power resources) so we need to be careful how we use it.
1225 */
1226 switch (type) {
1227 case ACPI_BUS_TYPE_DEVICE:
1228 result = acpi_bus_get_status(device);
1229 if (result)
1230 goto end;
1231 break;
1232 default:
1233 STRUCT_TO_INT(device->status) = 0x0F;
1234 break;
1235 }
1236 if (!device->status.present) {
1237 result = -2;
1238 goto end;
1239 }
1240
1241 /*
1242 * Initialize Device
1243 * -----------------
1244 * TBD: Synch with Core's enumeration/initialization process.
1245 */
1246
1247 /*
1248 * Hardware ID, Unique ID, & Bus Address
1249 * -------------------------------------
1250 */
1251 switch (type) {
1252 case ACPI_BUS_TYPE_DEVICE:
1253 status = AcpiGetObjectInfo(handle,&info);
1254 if (ACPI_FAILURE(status)) {
1255 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1256 "Error reading device info\n"));
1257 result = AE_NOT_FOUND;
1258 goto end;
1259 }
1260 if (info->Valid & ACPI_VALID_HID)
1261 hid = info->HardwareId.String;
1262 if (info->Valid & ACPI_VALID_UID)
1263 uid = info->UniqueId.String;
1264 if (info->Valid & ACPI_VALID_CID) {
1265 cid_list = &info->CompatibleIdList;
1266 device->pnp.cid_list = ExAllocatePoolWithTag(NonPagedPool,cid_list->ListSize, 'IPCA');
1267 if (device->pnp.cid_list)
1268 memcpy(device->pnp.cid_list, cid_list, cid_list->ListSize);
1269 else
1270 DPRINT("Memory allocation error\n");
1271 }
1272 if (info->Valid & ACPI_VALID_ADR) {
1273 device->pnp.bus_address = info->Address;
1274 device->flags.bus_address = 1;
1275 }
1276 break;
1277 case ACPI_BUS_TYPE_POWER:
1278 hid = ACPI_POWER_HID;
1279 uid = static_uid_buffer;
1280 snprintf(uid, sizeof(static_uid_buffer), "%d", (PowerDeviceCount++));
1281 break;
1282 case ACPI_BUS_TYPE_PROCESSOR:
1283 hid = ACPI_PROCESSOR_HID;
1284 uid = static_uid_buffer;
1285 snprintf(uid, sizeof(static_uid_buffer), "_%d", (ProcessorCount++));
1286 break;
1287 case ACPI_BUS_TYPE_SYSTEM:
1288 hid = ACPI_SYSTEM_HID;
1289 break;
1290 case ACPI_BUS_TYPE_THERMAL:
1291 hid = ACPI_THERMAL_HID;
1292 uid = static_uid_buffer;
1293 snprintf(uid, sizeof(static_uid_buffer), "%d", (ThermalZoneCount++));
1294 break;
1295 case ACPI_BUS_TYPE_POWER_BUTTON:
1296 hid = ACPI_BUTTON_HID_POWER;
1297 uid = static_uid_buffer;
1298 snprintf(uid, sizeof(static_uid_buffer), "%d", (PowerButtonCount++));
1299 break;
1300 case ACPI_BUS_TYPE_POWER_BUTTONF:
1301 hid = ACPI_BUTTON_HID_POWERF;
1302 uid = static_uid_buffer;
1303 snprintf(uid, sizeof(static_uid_buffer), "%d", (FixedPowerButtonCount++));
1304 break;
1305 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1306 hid = ACPI_BUTTON_HID_SLEEP;
1307 uid = static_uid_buffer;
1308 snprintf(uid, sizeof(static_uid_buffer), "%d", (SleepButtonCount++));
1309 break;
1310 case ACPI_BUS_TYPE_SLEEP_BUTTONF:
1311 hid = ACPI_BUTTON_HID_SLEEPF;
1312 uid = static_uid_buffer;
1313 snprintf(uid, sizeof(static_uid_buffer), "%d", (FixedSleepButtonCount++));
1314 break;
1315 }
1316
1317 /*
1318 * \_SB
1319 * ----
1320 * Fix for the system root bus device -- the only root-level device.
1321 */
1322 if (((ACPI_HANDLE)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
1323 hid = ACPI_BUS_HID;
1324 snprintf(device->pnp.device_name, sizeof(device->pnp.device_name), "%s", ACPI_BUS_DEVICE_NAME);
1325 snprintf(device->pnp.device_class, sizeof(device->pnp.device_class), "%s", ACPI_BUS_CLASS);
1326 }
1327
1328 if (hid) {
1329 device->pnp.hardware_id = ExAllocatePoolWithTag(NonPagedPool, strlen(hid) + 1, 'IPCA');
1330 if (device->pnp.hardware_id) {
1331 snprintf(device->pnp.hardware_id, strlen(hid) + 1, "%s", hid);
1332 device->flags.hardware_id = 1;
1333 }
1334 }
1335 if (uid) {
1336 snprintf(device->pnp.unique_id, sizeof(device->pnp.unique_id), "%s", uid);
1337 device->flags.unique_id = 1;
1338 }
1339
1340 /*
1341 * If we called get_object_info, we now are finished with the buffer,
1342 * so we can free it.
1343 */
1344 //if (buffer.Pointer)
1345 //AcpiOsFree(buffer.Pointer);
1346
1347 /*
1348 * Power Management
1349 * ----------------
1350 */
1351 if (device->flags.power_manageable) {
1352 result = acpi_bus_get_power_flags(device);
1353 if (result)
1354 goto end;
1355 }
1356
1357 /*
1358 * Performance Management
1359 * ----------------------
1360 */
1361 if (device->flags.performance_manageable) {
1362 result = acpi_bus_get_perf_flags(device);
1363 if (result)
1364 goto end;
1365 }
1366
1367 /*
1368 * Context
1369 * -------
1370 * Attach this 'struct acpi_device' to the ACPI object. This makes
1371 * resolutions from handle->device very efficient. Note that we need
1372 * to be careful with fixed-feature devices as they all attach to the
1373 * root object.
1374 */
1375 switch (type) {
1376 case ACPI_BUS_TYPE_POWER_BUTTON:
1377 case ACPI_BUS_TYPE_POWER_BUTTONF:
1378 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1379 case ACPI_BUS_TYPE_SLEEP_BUTTONF:
1380 break;
1381 default:
1382 status = AcpiAttachData(device->handle,
1383 acpi_bus_data_handler, device);
1384 break;
1385 }
1386 if (ACPI_FAILURE(status)) {
1387 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1388 "Error attaching device data\n"));
1389 result = AE_NOT_FOUND;
1390 goto end;
1391 }
1392
1393 /*
1394 * Linkage
1395 * -------
1396 * Link this device to its parent and siblings.
1397 */
1398 INIT_LIST_HEAD(&device->children);
1399 if (!device->parent)
1400 INIT_LIST_HEAD(&device->node);
1401 else
1402 list_add_tail(&device->node, &device->parent->children);
1403
1404 /*
1405 * Global Device Hierarchy:
1406 * ------------------------
1407 * Register this device with the global device hierarchy.
1408 */
1409 acpi_device_register(device, parent);
1410
1411 /*
1412 * Bind _ADR-Based Devices
1413 * -----------------------
1414 * If there's a a bus address (_ADR) then we utilize the parent's
1415 * 'bind' function (if exists) to bind the ACPI- and natively-
1416 * enumerated device representations.
1417 */
1418 if (device->flags.bus_address) {
1419 if (device->parent && device->parent->ops.bind)
1420 device->parent->ops.bind(device);
1421 }
1422
1423 /*
1424 * Locate & Attach Driver
1425 * ----------------------
1426 * If there's a hardware id (_HID) or compatible ids (_CID) we check
1427 * to see if there's a driver installed for this kind of device. Note
1428 * that drivers can install before or after a device is enumerated.
1429 *
1430 * TBD: Assumes LDM provides driver hot-plug capability.
1431 */
1432 if (device->flags.hardware_id || device->flags.compatible_ids)
1433 acpi_bus_find_driver(device);
1434
1435 end:
1436 if (result) {
1437 if (device->pnp.cid_list) {
1438 ExFreePoolWithTag(device->pnp.cid_list, 'IPCA');
1439 }
1440 if (device->pnp.hardware_id) {
1441 ExFreePoolWithTag(device->pnp.hardware_id, 'IPCA');
1442 }
1443 ExFreePoolWithTag(device, 'IPCA');
1444 return_VALUE(result);
1445 }
1446 *child = device;
1447
1448 return_VALUE(0);
1449 }
1450
1451
1452 static int
1453 acpi_bus_remove (
1454 struct acpi_device *device,
1455 int type)
1456 {
1457
1458 if (!device)
1459 return_VALUE(AE_NOT_FOUND);
1460
1461 acpi_device_unregister(device);
1462
1463 if (device->pnp.cid_list)
1464 ExFreePoolWithTag(device->pnp.cid_list, 'IPCA');
1465
1466 if (device->pnp.hardware_id)
1467 ExFreePoolWithTag(device->pnp.hardware_id, 'IPCA');
1468
1469 if (device)
1470 ExFreePoolWithTag(device, 'IPCA');
1471
1472 return_VALUE(0);
1473 }
1474
1475
1476 int
1477 acpi_bus_scan (
1478 struct acpi_device *start)
1479 {
1480 ACPI_STATUS status = AE_OK;
1481 struct acpi_device *parent = NULL;
1482 struct acpi_device *child = NULL;
1483 ACPI_HANDLE phandle = 0;
1484 ACPI_HANDLE chandle = 0;
1485 ACPI_OBJECT_TYPE type = 0;
1486 UINT32 level = 1;
1487
1488 if (!start)
1489 return_VALUE(AE_BAD_PARAMETER);
1490
1491 parent = start;
1492 phandle = start->handle;
1493
1494 /*
1495 * Parse through the ACPI namespace, identify all 'devices', and
1496 * create a new 'struct acpi_device' for each.
1497 */
1498 while ((level > 0) && parent) {
1499
1500 status = AcpiGetNextObject(ACPI_TYPE_ANY, phandle,
1501 chandle, &chandle);
1502
1503 /*
1504 * If this scope is exhausted then move our way back up.
1505 */
1506 if (ACPI_FAILURE(status)) {
1507 level--;
1508 chandle = phandle;
1509 AcpiGetParent(phandle, &phandle);
1510 if (parent->parent)
1511 parent = parent->parent;
1512 continue;
1513 }
1514
1515 status = AcpiGetType(chandle, &type);
1516 if (ACPI_FAILURE(status))
1517 continue;
1518
1519 /*
1520 * If this is a scope object then parse it (depth-first).
1521 */
1522 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1523 level++;
1524 phandle = chandle;
1525 chandle = 0;
1526 continue;
1527 }
1528
1529 /*
1530 * We're only interested in objects that we consider 'devices'.
1531 */
1532 switch (type) {
1533 case ACPI_TYPE_DEVICE:
1534 type = ACPI_BUS_TYPE_DEVICE;
1535 break;
1536 case ACPI_TYPE_PROCESSOR:
1537 type = ACPI_BUS_TYPE_PROCESSOR;
1538 break;
1539 case ACPI_TYPE_THERMAL:
1540 type = ACPI_BUS_TYPE_THERMAL;
1541 break;
1542 case ACPI_TYPE_POWER:
1543 type = ACPI_BUS_TYPE_POWER;
1544 break;
1545 default:
1546 continue;
1547 }
1548
1549 status = acpi_bus_add(&child, parent, chandle, type);
1550 if (ACPI_FAILURE(status))
1551 continue;
1552
1553 /*
1554 * If the device is present, enabled, and functioning then
1555 * parse its scope (depth-first). Note that we need to
1556 * represent absent devices to facilitate PnP notifications
1557 * -- but only the subtree head (not all of its children,
1558 * which will be enumerated when the parent is inserted).
1559 *
1560 * TBD: Need notifications and other detection mechanisms
1561 * in place before we can fully implement this.
1562 */
1563 if (child->status.present) {
1564 status = AcpiGetNextObject(ACPI_TYPE_ANY, chandle,
1565 0, NULL);
1566 if (ACPI_SUCCESS(status)) {
1567 level++;
1568 phandle = chandle;
1569 chandle = 0;
1570 parent = child;
1571 }
1572 }
1573 }
1574
1575 return_VALUE(0);
1576 }
1577
1578
1579 static int
1580 acpi_bus_scan_fixed (
1581 struct acpi_device *root)
1582 {
1583 int result = 0;
1584 struct acpi_device *device = NULL;
1585
1586 if (!root)
1587 return_VALUE(AE_NOT_FOUND);
1588
1589 /* If ACPI_FADT_POWER_BUTTON is set, then a control
1590 * method power button is present. Otherwise, a fixed
1591 * power button is present.
1592 */
1593 if (AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON)
1594 result = acpi_bus_add(&device, acpi_root,
1595 NULL, ACPI_BUS_TYPE_POWER_BUTTON);
1596 else
1597 {
1598 /* Enable the fixed power button so we get notified if it is pressed */
1599 AcpiWriteBitRegister(ACPI_BITREG_POWER_BUTTON_ENABLE, 1);
1600
1601 result = acpi_bus_add(&device, acpi_root,
1602 NULL, ACPI_BUS_TYPE_POWER_BUTTONF);
1603 }
1604
1605 /* This one is a bit more complicated and we do it wrong
1606 * right now. If ACPI_FADT_SLEEP_BUTTON is set but no
1607 * device object is present then no sleep button is present, but
1608 * if the flags is clear and there is no device object then it is
1609 * a fixed sleep button. If the flag is set and there is a device object
1610 * the we have a control method button just like above.
1611 */
1612 if (AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON)
1613 result = acpi_bus_add(&device, acpi_root,
1614 NULL, ACPI_BUS_TYPE_SLEEP_BUTTON);
1615 else
1616 {
1617 /* Enable the fixed sleep button so we get notified if it is pressed */
1618 AcpiWriteBitRegister(ACPI_BITREG_SLEEP_BUTTON_ENABLE, 1);
1619
1620 result = acpi_bus_add(&device, acpi_root,
1621 NULL, ACPI_BUS_TYPE_SLEEP_BUTTONF);
1622 }
1623
1624 return_VALUE(result);
1625 }
1626
1627
1628 /* --------------------------------------------------------------------------
1629 Initialization/Cleanup
1630 -------------------------------------------------------------------------- */
1631
1632 int
1633 acpi_bus_init (void)
1634 {
1635 int result = 0;
1636 ACPI_STATUS status = AE_OK;
1637
1638 DPRINT("acpi_bus_init\n");
1639
1640 KeInitializeDpc(&event_dpc, acpi_bus_generate_event_dpc, NULL);
1641
1642 status = AcpiEnableSubsystem(ACPI_FULL_INITIALIZATION);
1643 if (ACPI_FAILURE(status)) {
1644 DPRINT1("Unable to start the ACPI Interpreter\n");
1645 goto error1;
1646 }
1647
1648 /*
1649 * ACPI 2.0 requires the EC driver to be loaded and work before
1650 * the EC device is found in the namespace. This is accomplished
1651 * by looking for the ECDT table, and getting the EC parameters out
1652 * of that.
1653 */
1654 //result = acpi_ec_ecdt_probe();
1655 /* Ignore result. Not having an ECDT is not fatal. */
1656
1657 status = AcpiInitializeObjects(ACPI_FULL_INITIALIZATION);
1658 if (ACPI_FAILURE(status)) {
1659 DPRINT1("Unable to initialize ACPI objects\n");
1660 goto error1;
1661 }
1662
1663 /*
1664 * Maybe EC region is required at bus_scan/acpi_get_devices. So it
1665 * is necessary to enable it as early as possible.
1666 */
1667 //acpi_boot_ec_enable();
1668
1669 /* Initialize sleep structures */
1670 //acpi_sleep_init();
1671
1672 /*
1673 * Register the for all standard device notifications.
1674 */
1675 status = AcpiInstallNotifyHandler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY, &acpi_bus_notify, NULL);
1676 if (ACPI_FAILURE(status)) {
1677 DPRINT1("Unable to register for device notifications\n");
1678 result = AE_NOT_FOUND;
1679 goto error1;
1680 }
1681
1682 /*
1683 * Create the root device in the bus's device tree
1684 */
1685 result = acpi_bus_add(&acpi_root, NULL, ACPI_ROOT_OBJECT,
1686 ACPI_BUS_TYPE_SYSTEM);
1687 if (result)
1688 goto error2;
1689
1690
1691 /*
1692 * Enumerate devices in the ACPI namespace.
1693 */
1694 result = acpi_bus_scan_fixed(acpi_root);
1695 if (result)
1696 DPRINT1("acpi_bus_scan_fixed failed\n");
1697 result = acpi_bus_scan(acpi_root);
1698 if (result)
1699 DPRINT1("acpi_bus_scan failed\n");
1700
1701 return_VALUE(0);
1702
1703 /* Mimic structured exception handling */
1704 error2:
1705 AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT,
1706 ACPI_SYSTEM_NOTIFY, &acpi_bus_notify);
1707 error1:
1708 AcpiTerminate();
1709 return_VALUE(AE_NOT_FOUND);
1710 }
1711
1712 static void
1713 acpi_bus_exit (void)
1714 {
1715 ACPI_STATUS status = AE_OK;
1716
1717 DPRINT1("acpi_bus_exit\n");
1718
1719 status = AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT,
1720 ACPI_SYSTEM_NOTIFY, acpi_bus_notify);
1721 if (ACPI_FAILURE(status))
1722 DPRINT1("Error removing notify handler\n");
1723
1724 #ifdef CONFIG_ACPI_PCI
1725 acpi_pci_root_exit();
1726 acpi_pci_link_exit();
1727 #endif
1728 #ifdef CONFIG_ACPI_EC
1729 acpi_ec_exit();
1730 #endif
1731 //acpi_power_exit();
1732 acpi_system_exit();
1733
1734 acpi_bus_remove(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1735
1736 status = AcpiTerminate();
1737 if (ACPI_FAILURE(status))
1738 DPRINT1("Unable to terminate the ACPI Interpreter\n");
1739 else
1740 DPRINT1("Interpreter disabled\n");
1741
1742 return_VOID;
1743 }
1744
1745
1746 int
1747 acpi_init (void)
1748 {
1749 int result = 0;
1750
1751 DPRINT("acpi_init\n");
1752
1753 DPRINT("Subsystem revision %08x\n",ACPI_CA_VERSION);
1754
1755 KeInitializeSpinLock(&acpi_bus_event_lock);
1756 KeInitializeEvent(&AcpiEventQueue, NotificationEvent, FALSE);
1757 ExInitializeFastMutex(&acpi_bus_drivers_lock);
1758
1759 result = acpi_bus_init();
1760
1761 //if (!result) {
1762 //pci_mmcfg_late_init();
1763 //if (!(pm_flags & PM_APM))
1764 // pm_flags |= PM_ACPI;
1765 //else {
1766 //DPRINT1("APM is already active, exiting\n");
1767 //disable_acpi();
1768 //result = -ENODEV;
1769 //}
1770 //} else
1771 // disable_acpi();
1772
1773 /*
1774 * If the laptop falls into the DMI check table, the power state check
1775 * will be disabled in the course of device power transistion.
1776 */
1777 //dmi_check_system(power_nocheck_dmi_table);
1778
1779 /*
1780 * Install drivers required for proper enumeration of the
1781 * ACPI namespace.
1782 */
1783 acpi_system_init(); /* ACPI System */
1784 acpi_power_init(); /* ACPI Bus Power Management */
1785 acpi_button_init();
1786 //acpi_ec_init(); /* ACPI Embedded Controller */
1787 #ifdef CONFIG_ACPI_PCI
1788 if (!acpi_pci_disabled) {
1789 acpi_pci_link_init(); /* ACPI PCI Interrupt Link */
1790 acpi_pci_root_init(); /* ACPI PCI Root Bridge */
1791 }
1792 #endif
1793
1794 //acpi_scan_init();
1795 //acpi_ec_init();
1796 //acpi_power_init();
1797 //acpi_system_init();
1798 //acpi_debug_init();
1799 //acpi_sleep_proc_init();
1800 //acpi_wakeup_device_init();
1801
1802 return result;
1803 }
1804
1805
1806 void
1807 acpi_exit (void)
1808 {
1809 DPRINT("acpi_exit\n");
1810
1811 #ifdef CONFIG_PM
1812 pm_active = 0;
1813 #endif
1814
1815 acpi_bus_exit();
1816
1817 return_VOID;
1818 }
1819