acpi.c revision 1.101.16.16 1 /* $NetBSD: acpi.c,v 1.101.16.16 2007/09/30 17:24:09 joerg Exp $ */
2
3 /*-
4 * Copyright (c) 2003, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum of By Noon Software, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright 2001, 2003 Wasabi Systems, Inc.
41 * All rights reserved.
42 *
43 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. All advertising materials mentioning features or use of this software
54 * must display the following acknowledgement:
55 * This product includes software developed for the NetBSD Project by
56 * Wasabi Systems, Inc.
57 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
58 * or promote products derived from this software without specific prior
59 * written permission.
60 *
61 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
63 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
64 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
65 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
66 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
67 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
68 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
69 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
70 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
71 * POSSIBILITY OF SUCH DAMAGE.
72 */
73
74 /*
75 * Autoconfiguration support for the Intel ACPI Component Architecture
76 * ACPI reference implementation.
77 */
78
79 #include <sys/cdefs.h>
80 __KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.101.16.16 2007/09/30 17:24:09 joerg Exp $");
81
82 #include "opt_acpi.h"
83 #include "opt_pcifixup.h"
84
85 #include <sys/param.h>
86 #include <sys/systm.h>
87 #include <sys/device.h>
88 #include <sys/malloc.h>
89 #include <sys/mutex.h>
90 #include <sys/kernel.h>
91 #include <sys/proc.h>
92 #include <sys/sysctl.h>
93
94 #include <dev/acpi/acpica.h>
95 #include <dev/acpi/acpireg.h>
96 #include <dev/acpi/acpivar.h>
97 #include <dev/acpi/acpi_osd.h>
98 #include <dev/acpi/acpi_timer.h>
99 #ifdef ACPIVERBOSE
100 #include <dev/acpi/acpidevs_data.h>
101 #endif
102
103 #if defined(ACPI_PCI_FIXUP)
104 #error The option ACPI_PCI_FIXUP has been obsoleted by PCI_INTR_FIXUP_DISABLED. Please adjust your kernel configuration file.
105 #endif
106
107 #ifdef PCI_INTR_FIXUP_DISABLED
108 #include <dev/pci/pcidevs.h>
109 #endif
110
111 MALLOC_DECLARE(M_ACPI);
112
113 #include <machine/acpi_machdep.h>
114
115 #ifdef ACPI_DEBUGGER
116 #define ACPI_DBGR_INIT 0x01
117 #define ACPI_DBGR_TABLES 0x02
118 #define ACPI_DBGR_ENABLE 0x04
119 #define ACPI_DBGR_PROBE 0x08
120 #define ACPI_DBGR_RUNNING 0x10
121
122 static int acpi_dbgr = 0x00;
123 #endif
124
125 static int acpi_match(struct device *, struct cfdata *, void *);
126 static void acpi_attach(struct device *, struct device *, void *);
127
128 static int acpi_print(void *aux, const char *);
129
130 static int sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS);
131
132 extern struct cfdriver acpi_cd;
133
134 CFATTACH_DECL(acpi, sizeof(struct acpi_softc),
135 acpi_match, acpi_attach, NULL, NULL);
136
137 /*
138 * This is a flag we set when the ACPI subsystem is active. Machine
139 * dependent code may wish to skip other steps (such as attaching
140 * subsystems that ACPI supercedes) when ACPI is active.
141 */
142 int acpi_active;
143 int acpi_force_load;
144
145 /*
146 * Pointer to the ACPI subsystem's state. There can be only
147 * one ACPI instance.
148 */
149 struct acpi_softc *acpi_softc;
150
151 /*
152 * Locking stuff.
153 */
154 static kmutex_t acpi_slock;
155 static int acpi_locked;
156 extern kmutex_t acpi_interrupt_list_mtx;
157
158 /*
159 * sysctl-related information
160 */
161
162 static int acpi_node = CTL_EOL;
163 static uint64_t acpi_root_pointer; /* found as hw.acpi.root */
164 static int acpi_sleepstate = ACPI_STATE_S0;
165
166 /*
167 * Prototypes.
168 */
169 static void acpi_shutdown(void *);
170 static void acpi_build_tree(struct acpi_softc *);
171 static ACPI_STATUS acpi_make_devnode(ACPI_HANDLE, UINT32, void *, void **);
172
173 static void acpi_enable_fixed_events(struct acpi_softc *);
174
175 /*
176 * acpi_probe:
177 *
178 * Probe for ACPI support. This is called by the
179 * machine-dependent ACPI front-end. All of the
180 * actual work is done by ACPICA.
181 *
182 * NOTE: This is not an autoconfiguration interface function.
183 */
184 int
185 acpi_probe(void)
186 {
187 static int beenhere;
188 ACPI_STATUS rv;
189
190 if (beenhere != 0)
191 panic("acpi_probe: ACPI has already been probed");
192 beenhere = 1;
193
194 mutex_init(&acpi_slock, MUTEX_DRIVER, IPL_NONE);
195 mutex_init(&acpi_interrupt_list_mtx, MUTEX_DRIVER, IPL_NONE);
196 acpi_locked = 0;
197
198 /*
199 * Start up ACPICA.
200 */
201 #ifdef ACPI_DEBUGGER
202 if (acpi_dbgr & ACPI_DBGR_INIT)
203 acpi_osd_debugger();
204 #endif
205
206 rv = AcpiInitializeSubsystem();
207 if (ACPI_FAILURE(rv)) {
208 printf("ACPI: unable to initialize ACPICA: %s\n",
209 AcpiFormatException(rv));
210 return 0;
211 }
212
213 #ifdef ACPI_DEBUGGER
214 if (acpi_dbgr & ACPI_DBGR_TABLES)
215 acpi_osd_debugger();
216 #endif
217
218 rv = AcpiLoadTables();
219 if (ACPI_FAILURE(rv)) {
220 printf("ACPI: unable to load tables: %s\n",
221 AcpiFormatException(rv));
222 return 0;
223 }
224
225 if (!acpi_force_load && (acpi_find_quirks() & ACPI_QUIRK_BROKEN)) {
226 printf("ACPI: BIOS implementation in listed as broken:\n");
227 printf("ACPI: X/RSDT: OemId <%6.6s,%8.8s,%08x>, "
228 "AslId <%4.4s,%08x>\n",
229 AcpiGbl_XSDT->OemId, AcpiGbl_XSDT->OemTableId,
230 AcpiGbl_XSDT->OemRevision,
231 AcpiGbl_XSDT->AslCompilerId,
232 AcpiGbl_XSDT->AslCompilerRevision);
233 printf("ACPI: not used. set acpi_force_load to use anyway.\n");
234 return 0;
235 }
236
237 /* Install the default address space handlers. */
238 rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
239 ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
240 if (ACPI_FAILURE(rv)) {
241 printf("ACPI: unable to initialise SystemMemory handler: %s\n",
242 AcpiFormatException(rv));
243 return 0;
244 }
245 rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
246 ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
247 if (ACPI_FAILURE(rv)) {
248 printf("ACPI: unable to initialise SystemIO handler: %s\n",
249 AcpiFormatException(rv));
250 return 0;
251 }
252 rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
253 ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
254 if (ACPI_FAILURE(rv)) {
255 printf("ACPI: unabled to initialise PciConfig handler: %s\n",
256 AcpiFormatException(rv));
257 return 0;
258 }
259
260 /*
261 * Looks like we have ACPI!
262 */
263
264 return 1;
265 }
266
267 static int
268 acpi_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
269 {
270 struct cfattach *ca;
271
272 ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
273 return (ca == &acpi_ca);
274 }
275
276 int
277 acpi_check(device_t parent, const char *ifattr)
278 {
279 return (config_search_ia(acpi_submatch, parent, ifattr, NULL) != NULL);
280 }
281
282 ACPI_STATUS
283 acpi_OsGetRootPointer(UINT32 Flags, ACPI_POINTER *PhysicalAddress)
284 {
285 ACPI_STATUS rv;
286
287 /*
288 * IA-32: Use AcpiFindRootPointer() to locate the RSDP.
289 *
290 * IA-64: Use the EFI.
291 *
292 * We let MD code handle this since there are multiple
293 * ways to do it.
294 */
295
296 rv = acpi_md_OsGetRootPointer(Flags, PhysicalAddress);
297
298 if (acpi_root_pointer == 0 && ACPI_SUCCESS(rv))
299 acpi_root_pointer =
300 (uint64_t)PhysicalAddress->Pointer.Physical;
301
302 return rv;
303 }
304
305 /*
306 * acpi_match:
307 *
308 * Autoconfiguration `match' routine.
309 */
310 static int
311 acpi_match(struct device *parent, struct cfdata *match,
312 void *aux)
313 {
314 /*
315 * XXX Check other locators? Hard to know -- machine
316 * dependent code has already checked for the presence
317 * of ACPI by calling acpi_probe(), so I suppose we
318 * don't really have to do anything else.
319 */
320 return 1;
321 }
322
323 /*
324 * acpi_attach:
325 *
326 * Autoconfiguration `attach' routine. Finish initializing
327 * ACPICA (some initialization was done in acpi_probe(),
328 * which was required to check for the presence of ACPI),
329 * and enable the ACPI subsystem.
330 */
331 static void
332 acpi_attach(struct device *parent, struct device *self, void *aux)
333 {
334 struct acpi_softc *sc = (void *) self;
335 struct acpibus_attach_args *aa = aux;
336 ACPI_STATUS rv;
337
338 aprint_naive(": Advanced Configuration and Power Interface\n");
339 aprint_normal(": Advanced Configuration and Power Interface\n");
340
341 if (acpi_softc != NULL)
342 panic("acpi_attach: ACPI has already been attached");
343
344 sysmon_power_settype("acpi");
345
346 aprint_verbose("%s: using Intel ACPI CA subsystem version %08x\n",
347 sc->sc_dev.dv_xname, ACPI_CA_VERSION);
348
349 aprint_verbose("%s: X/RSDT: OemId <%6.6s,%8.8s,%08x>, AslId <%4.4s,%08x>\n",
350 sc->sc_dev.dv_xname,
351 AcpiGbl_XSDT->OemId, AcpiGbl_XSDT->OemTableId,
352 AcpiGbl_XSDT->OemRevision,
353 AcpiGbl_XSDT->AslCompilerId, AcpiGbl_XSDT->AslCompilerRevision);
354
355 sc->sc_quirks = acpi_find_quirks();
356
357 sc->sc_iot = aa->aa_iot;
358 sc->sc_memt = aa->aa_memt;
359 sc->sc_pc = aa->aa_pc;
360 sc->sc_pciflags = aa->aa_pciflags;
361 sc->sc_ic = aa->aa_ic;
362
363 acpi_softc = sc;
364
365 /*
366 * Register null power management handler
367 */
368 (void)pnp_register(self, pnp_generic_power);
369
370 /*
371 * Bring ACPI on-line.
372 */
373 #ifdef ACPI_DEBUGGER
374 if (acpi_dbgr & ACPI_DBGR_ENABLE)
375 acpi_osd_debugger();
376 #endif
377
378 #define ACPI_ENABLE_PHASE1 \
379 (ACPI_NO_HANDLER_INIT | ACPI_NO_EVENT_INIT)
380 #define ACPI_ENABLE_PHASE2 \
381 (ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE | \
382 ACPI_NO_ADDRESS_SPACE_INIT)
383
384 rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE1);
385 if (ACPI_FAILURE(rv)) {
386 aprint_error("%s: unable to enable ACPI: %s\n",
387 sc->sc_dev.dv_xname, AcpiFormatException(rv));
388 return;
389 }
390
391 acpi_md_callback();
392
393 rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE2);
394 if (ACPI_FAILURE(rv)) {
395 aprint_error("%s: unable to enable ACPI: %s\n",
396 sc->sc_dev.dv_xname, AcpiFormatException(rv));
397 return;
398 }
399 /* early EC handler initialization if ECDT table is available */
400 #if NACPIEC > 0
401 acpiec_early_attach(&sc->sc_dev);
402 #endif
403
404 rv = AcpiInitializeObjects(0);
405 if (ACPI_FAILURE(rv)) {
406 aprint_error("%s: unable to initialize ACPI objects: %s\n",
407 sc->sc_dev.dv_xname, AcpiFormatException(rv));
408 return;
409 }
410 acpi_active = 1;
411
412 /* Our current state is "awake". */
413 sc->sc_sleepstate = ACPI_STATE_S0;
414
415 /* Show SCI interrupt. */
416 if (AcpiGbl_FADT != NULL)
417 aprint_verbose("%s: SCI interrupting at int %d\n",
418 sc->sc_dev.dv_xname, AcpiGbl_FADT->SciInt);
419
420 /*
421 * Check for fixed-hardware features.
422 */
423 acpi_enable_fixed_events(sc);
424 acpitimer_init();
425
426 /*
427 * Scan the namespace and build our device tree.
428 */
429 #ifdef ACPI_DEBUGGER
430 if (acpi_dbgr & ACPI_DBGR_PROBE)
431 acpi_osd_debugger();
432 #endif
433 acpi_build_tree(sc);
434
435 if (acpi_root_pointer != 0 && acpi_node != CTL_EOL) {
436 (void)sysctl_createv(NULL, 0, NULL, NULL,
437 CTLFLAG_IMMEDIATE,
438 CTLTYPE_QUAD, "root", NULL, NULL,
439 acpi_root_pointer, NULL, 0,
440 CTL_HW, acpi_node, CTL_CREATE, CTL_EOL);
441 }
442
443
444 /*
445 * Register a shutdown hook that disables certain ACPI
446 * events that might happen and confuse us while we're
447 * trying to shut down.
448 */
449 sc->sc_sdhook = shutdownhook_establish(acpi_shutdown, sc);
450 if (sc->sc_sdhook == NULL)
451 aprint_error("%s: WARNING: unable to register shutdown hook\n",
452 sc->sc_dev.dv_xname);
453
454 #ifdef ACPI_DEBUGGER
455 if (acpi_dbgr & ACPI_DBGR_RUNNING)
456 acpi_osd_debugger();
457 #endif
458 }
459
460 /*
461 * acpi_shutdown:
462 *
463 * Shutdown hook for ACPI -- disable some events that
464 * might confuse us.
465 */
466 static void
467 acpi_shutdown(void *arg)
468 {
469 /* nothing */
470 }
471
472 #if 0
473 /*
474 * acpi_disable:
475 *
476 * Disable ACPI.
477 */
478 static ACPI_STATUS
479 acpi_disable(struct acpi_softc *sc)
480 {
481 ACPI_STATUS rv = AE_OK;
482
483 if (acpi_active) {
484 rv = AcpiDisable();
485 if (ACPI_SUCCESS(rv))
486 acpi_active = 0;
487 }
488 return rv;
489 }
490 #endif
491
492 struct acpi_make_devnode_state {
493 struct acpi_softc *softc;
494 struct acpi_scope *scope;
495 };
496
497 /*
498 * acpi_build_tree:
499 *
500 * Scan relevant portions of the ACPI namespace and attach
501 * child devices.
502 */
503 static void
504 acpi_build_tree(struct acpi_softc *sc)
505 {
506 static const char *scopes[] = {
507 "\\_PR_", /* ACPI 1.0 processor namespace */
508 "\\_SB_", /* system bus namespace */
509 "\\_SI_", /* system indicator namespace */
510 "\\_TZ_", /* ACPI 1.0 thermal zone namespace */
511 NULL,
512 };
513 struct acpi_attach_args aa;
514 struct acpi_make_devnode_state state;
515 struct acpi_scope *as;
516 struct acpi_devnode *ad;
517 ACPI_HANDLE parent;
518 ACPI_STATUS rv;
519 int i;
520
521 TAILQ_INIT(&sc->sc_scopes);
522
523 state.softc = sc;
524
525 /*
526 * Scan the namespace and build our tree.
527 */
528 for (i = 0; scopes[i] != NULL; i++) {
529 as = malloc(sizeof(*as), M_ACPI, M_WAITOK);
530 as->as_name = scopes[i];
531 TAILQ_INIT(&as->as_devnodes);
532
533 TAILQ_INSERT_TAIL(&sc->sc_scopes, as, as_list);
534
535 state.scope = as;
536
537 rv = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i],
538 &parent);
539 if (ACPI_SUCCESS(rv)) {
540 AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100,
541 acpi_make_devnode, &state, NULL);
542 }
543
544 /* Now, for this namespace, try and attach the devices. */
545 TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) {
546 aa.aa_node = ad;
547 aa.aa_iot = sc->sc_iot;
548 aa.aa_memt = sc->sc_memt;
549 aa.aa_pc = sc->sc_pc;
550 aa.aa_pciflags = sc->sc_pciflags;
551 aa.aa_ic = sc->sc_ic;
552
553 if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE) {
554 /*
555 * XXX We only attach devices which are:
556 *
557 * - present
558 * - enabled
559 * - functioning properly
560 *
561 * However, if enabled, it's decoding resources,
562 * so we should claim them, if possible.
563 * Requires changes to bus_space(9).
564 */
565 if ((ad->ad_devinfo->Valid & ACPI_VALID_STA) ==
566 ACPI_VALID_STA &&
567 (ad->ad_devinfo->CurrentStatus &
568 (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
569 ACPI_STA_DEV_OK)) !=
570 (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
571 ACPI_STA_DEV_OK))
572 continue;
573 }
574
575 /*
576 * XXX Same problem as above...
577 */
578 if ((ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
579 continue;
580
581 ad->ad_device = config_found_ia(&sc->sc_dev,
582 "acpinodebus", &aa, acpi_print);
583 }
584 }
585 config_found_ia(&sc->sc_dev, "acpiapmbus", NULL, NULL);
586 }
587
588 #ifdef ACPI_ACTIVATE_DEV
589 static void
590 acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
591 {
592 ACPI_STATUS rv;
593 ACPI_BUFFER buf;
594
595 buf.Pointer = NULL;
596 buf.Length = ACPI_ALLOCATE_BUFFER;
597
598 #ifdef ACPI_DEBUG
599 aprint_normal("acpi_activate_device: %s, old status=%x\n",
600 (*di)->HardwareId.Value, (*di)->CurrentStatus);
601 #endif
602
603 rv = acpi_allocate_resources(handle);
604 if (ACPI_FAILURE(rv)) {
605 aprint_error("acpi: activate failed for %s\n",
606 (*di)->HardwareId.Value);
607 } else {
608 aprint_verbose("acpi: activated %s\n",
609 (*di)->HardwareId.Value);
610 }
611
612 (void)AcpiGetObjectInfo(handle, &buf);
613 AcpiOsFree(*di);
614 *di = buf.Pointer;
615
616 #ifdef ACPI_DEBUG
617 aprint_normal("acpi_activate_device: %s, new status=%x\n",
618 (*di)->HardwareId.Value, (*di)->CurrentStatus);
619 #endif
620 }
621 #endif /* ACPI_ACTIVATE_DEV */
622
623 /*
624 * acpi_make_devnode:
625 *
626 * Make an ACPI devnode.
627 */
628 static ACPI_STATUS
629 acpi_make_devnode(ACPI_HANDLE handle, UINT32 level, void *context,
630 void **status)
631 {
632 struct acpi_make_devnode_state *state = context;
633 #if defined(ACPI_DEBUG) || defined(ACPI_EXTRA_DEBUG)
634 struct acpi_softc *sc = state->softc;
635 #endif
636 struct acpi_scope *as = state->scope;
637 struct acpi_devnode *ad;
638 ACPI_OBJECT_TYPE type;
639 ACPI_BUFFER buf;
640 ACPI_DEVICE_INFO *devinfo;
641 ACPI_STATUS rv;
642 ACPI_NAME_UNION *anu;
643 int i, clear = 0;
644
645 rv = AcpiGetType(handle, &type);
646 if (ACPI_SUCCESS(rv)) {
647 buf.Pointer = NULL;
648 buf.Length = ACPI_ALLOCATE_BUFFER;
649 rv = AcpiGetObjectInfo(handle, &buf);
650 if (ACPI_FAILURE(rv)) {
651 #ifdef ACPI_DEBUG
652 aprint_normal("%s: AcpiGetObjectInfo failed: %s\n",
653 sc->sc_dev.dv_xname, AcpiFormatException(rv));
654 #endif
655 goto out; /* XXX why return OK */
656 }
657
658 devinfo = buf.Pointer;
659
660 switch (type) {
661 case ACPI_TYPE_DEVICE:
662 #ifdef ACPI_ACTIVATE_DEV
663 if ((devinfo->Valid & (ACPI_VALID_STA|ACPI_VALID_HID)) ==
664 (ACPI_VALID_STA|ACPI_VALID_HID) &&
665 (devinfo->CurrentStatus &
666 (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED)) ==
667 ACPI_STA_DEV_PRESENT)
668 acpi_activate_device(handle, &devinfo);
669
670 /* FALLTHROUGH */
671 #endif
672
673 case ACPI_TYPE_PROCESSOR:
674 case ACPI_TYPE_THERMAL:
675 case ACPI_TYPE_POWER:
676 ad = malloc(sizeof(*ad), M_ACPI, M_NOWAIT|M_ZERO);
677 if (ad == NULL)
678 return AE_NO_MEMORY;
679
680 ad->ad_devinfo = devinfo;
681 ad->ad_handle = handle;
682 ad->ad_level = level;
683 ad->ad_scope = as;
684 ad->ad_type = type;
685
686 anu = (ACPI_NAME_UNION *)&devinfo->Name;
687 ad->ad_name[4] = '\0';
688 for (i = 3, clear = 0; i >= 0; i--) {
689 if (!clear && anu->Ascii[i] == '_')
690 ad->ad_name[i] = '\0';
691 else {
692 ad->ad_name[i] = anu->Ascii[i];
693 clear = 1;
694 }
695 }
696 if (ad->ad_name[0] == '\0')
697 ad->ad_name[0] = '_';
698
699 TAILQ_INSERT_TAIL(&as->as_devnodes, ad, ad_list);
700
701 if ((ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
702 goto out;
703
704 #ifdef ACPI_EXTRA_DEBUG
705 aprint_normal("%s: HID %s found in scope %s level %d\n",
706 sc->sc_dev.dv_xname,
707 ad->ad_devinfo->HardwareId.Value,
708 as->as_name, ad->ad_level);
709 if (ad->ad_devinfo->Valid & ACPI_VALID_UID)
710 aprint_normal(" UID %s\n",
711 ad->ad_devinfo->UniqueId.Value);
712 if (ad->ad_devinfo->Valid & ACPI_VALID_ADR)
713 aprint_normal(" ADR 0x%016qx\n",
714 ad->ad_devinfo->Address);
715 if (ad->ad_devinfo->Valid & ACPI_VALID_STA)
716 aprint_normal(" STA 0x%08x\n",
717 ad->ad_devinfo->CurrentStatus);
718 #endif
719 }
720 }
721 out:
722 return AE_OK;
723 }
724
725 /*
726 * acpi_print:
727 *
728 * Autoconfiguration print routine for ACPI node bus.
729 */
730 static int
731 acpi_print(void *aux, const char *pnp)
732 {
733 struct acpi_attach_args *aa = aux;
734 ACPI_STATUS rv;
735
736 if (pnp) {
737 if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
738 char *pnpstr =
739 aa->aa_node->ad_devinfo->HardwareId.Value;
740 char *str;
741
742 aprint_normal("%s (%s) ", aa->aa_node->ad_name,
743 pnpstr);
744 rv = acpi_eval_string(aa->aa_node->ad_handle,
745 "_STR", &str);
746 if (ACPI_SUCCESS(rv)) {
747 aprint_normal("[%s] ", str);
748 AcpiOsFree(str);
749 }
750 #ifdef ACPIVERBOSE
751 else {
752 int i;
753
754 for (i = 0; i < sizeof(acpi_knowndevs) /
755 sizeof(acpi_knowndevs[0]); i++) {
756 if (strcmp(acpi_knowndevs[i].pnp,
757 pnpstr) == 0) {
758 aprint_normal("[%s] ",
759 acpi_knowndevs[i].str);
760 }
761 }
762 }
763
764 #endif
765 aprint_normal("at %s", pnp);
766 } else if (aa->aa_node->ad_devinfo->Type != ACPI_TYPE_DEVICE) {
767 aprint_normal("%s (ACPI Object Type '%s' "
768 "[0x%02x]) ", aa->aa_node->ad_name,
769 AcpiUtGetTypeName(aa->aa_node->ad_devinfo->Type),
770 aa->aa_node->ad_devinfo->Type);
771 aprint_normal("at %s", pnp);
772 } else
773 return 0;
774 } else {
775 aprint_normal(" (%s", aa->aa_node->ad_name);
776 if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
777 aprint_normal(", %s", aa->aa_node->ad_devinfo->HardwareId.Value);
778 if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_UID) {
779 const char *uid;
780
781 uid = aa->aa_node->ad_devinfo->UniqueId.Value;
782 if (uid[0] == '\0')
783 uid = "<null>";
784 aprint_normal("-%s", uid);
785 }
786 }
787 aprint_normal(")");
788 }
789
790 return UNCONF;
791 }
792
793 /*****************************************************************************
794 * ACPI fixed-hardware feature handlers
795 *****************************************************************************/
796
797 static UINT32 acpi_fixed_button_handler(void *);
798 static void acpi_fixed_button_pressed(void *);
799
800 /*
801 * acpi_enable_fixed_events:
802 *
803 * Enable any fixed-hardware feature handlers.
804 */
805 static void
806 acpi_enable_fixed_events(struct acpi_softc *sc)
807 {
808 static int beenhere;
809 ACPI_STATUS rv;
810
811 KASSERT(beenhere == 0);
812 beenhere = 1;
813
814 /*
815 * Check for fixed-hardware buttons.
816 */
817
818 if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
819 aprint_verbose("%s: fixed-feature power button present\n",
820 sc->sc_dev.dv_xname);
821 sc->sc_smpsw_power.smpsw_name = sc->sc_dev.dv_xname;
822 sc->sc_smpsw_power.smpsw_type = PSWITCH_TYPE_POWER;
823 if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
824 aprint_error("%s: unable to register fixed power "
825 "button with sysmon\n", sc->sc_dev.dv_xname);
826 } else {
827 rv = AcpiInstallFixedEventHandler(
828 ACPI_EVENT_POWER_BUTTON,
829 acpi_fixed_button_handler, &sc->sc_smpsw_power);
830 if (ACPI_FAILURE(rv)) {
831 aprint_error("%s: unable to install handler "
832 "for fixed power button: %s\n",
833 sc->sc_dev.dv_xname,
834 AcpiFormatException(rv));
835 }
836 }
837 }
838
839 if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
840 aprint_verbose("%s: fixed-feature sleep button present\n",
841 sc->sc_dev.dv_xname);
842 sc->sc_smpsw_sleep.smpsw_name = sc->sc_dev.dv_xname;
843 sc->sc_smpsw_sleep.smpsw_type = PSWITCH_TYPE_SLEEP;
844 if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
845 aprint_error("%s: unable to register fixed sleep "
846 "button with sysmon\n", sc->sc_dev.dv_xname);
847 } else {
848 rv = AcpiInstallFixedEventHandler(
849 ACPI_EVENT_SLEEP_BUTTON,
850 acpi_fixed_button_handler, &sc->sc_smpsw_sleep);
851 if (ACPI_FAILURE(rv)) {
852 aprint_error("%s: unable to install handler "
853 "for fixed sleep button: %s\n",
854 sc->sc_dev.dv_xname,
855 AcpiFormatException(rv));
856 }
857 }
858 }
859 }
860
861 /*
862 * acpi_fixed_button_handler:
863 *
864 * Event handler for the fixed buttons.
865 */
866 static UINT32
867 acpi_fixed_button_handler(void *context)
868 {
869 struct sysmon_pswitch *smpsw = context;
870 int rv;
871
872 #ifdef ACPI_BUT_DEBUG
873 printf("%s: fixed button handler\n", smpsw->smpsw_name);
874 #endif
875
876 rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
877 acpi_fixed_button_pressed, smpsw);
878 if (ACPI_FAILURE(rv))
879 printf("%s: WARNING: unable to queue fixed button pressed "
880 "callback: %s\n", smpsw->smpsw_name,
881 AcpiFormatException(rv));
882
883 return ACPI_INTERRUPT_HANDLED;
884 }
885
886 /*
887 * acpi_fixed_button_pressed:
888 *
889 * Deal with a fixed button being pressed.
890 */
891 static void
892 acpi_fixed_button_pressed(void *context)
893 {
894 struct sysmon_pswitch *smpsw = context;
895
896 #ifdef ACPI_BUT_DEBUG
897 printf("%s: fixed button pressed, calling sysmon\n",
898 smpsw->smpsw_name);
899 #endif
900
901 sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
902 }
903
904 /*****************************************************************************
905 * ACPI utility routines.
906 *****************************************************************************/
907
908 /*
909 * acpi_eval_integer:
910 *
911 * Evaluate an integer object.
912 */
913 ACPI_STATUS
914 acpi_eval_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER *valp)
915 {
916 ACPI_STATUS rv;
917 ACPI_BUFFER buf;
918 ACPI_OBJECT param;
919
920 if (handle == NULL)
921 handle = ACPI_ROOT_OBJECT;
922
923 buf.Pointer = ¶m;
924 buf.Length = sizeof(param);
925
926 rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_INTEGER);
927 if (ACPI_SUCCESS(rv))
928 *valp = param.Integer.Value;
929
930 return rv;
931 }
932
933 /*
934 * acpi_eval_string:
935 *
936 * Evaluate a (Unicode) string object.
937 */
938 ACPI_STATUS
939 acpi_eval_string(ACPI_HANDLE handle, const char *path, char **stringp)
940 {
941 ACPI_STATUS rv;
942 ACPI_BUFFER buf;
943
944 if (handle == NULL)
945 handle = ACPI_ROOT_OBJECT;
946
947 buf.Pointer = NULL;
948 buf.Length = ACPI_ALLOCATE_BUFFER;
949
950 rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_STRING);
951 if (ACPI_SUCCESS(rv)) {
952 ACPI_OBJECT *param = buf.Pointer;
953 const char *ptr = param->String.Pointer;
954 size_t len = param->String.Length;
955 if ((*stringp = AcpiOsAllocate(len)) == NULL)
956 rv = AE_NO_MEMORY;
957 else
958 (void)memcpy(*stringp, ptr, len);
959 AcpiOsFree(param);
960 }
961
962 return rv;
963 }
964
965
966 /*
967 * acpi_eval_struct:
968 *
969 * Evaluate a more complex structure.
970 * Caller must free buf.Pointer by AcpiOsFree().
971 */
972 ACPI_STATUS
973 acpi_eval_struct(ACPI_HANDLE handle, const char *path, ACPI_BUFFER *bufp)
974 {
975 ACPI_STATUS rv;
976
977 if (handle == NULL)
978 handle = ACPI_ROOT_OBJECT;
979
980 bufp->Pointer = NULL;
981 bufp->Length = ACPI_ALLOCATE_BUFFER;
982
983 rv = AcpiEvaluateObject(handle, path, NULL, bufp);
984
985 return rv;
986 }
987
988 /*
989 * acpi_foreach_package_object:
990 *
991 * Iterate over all objects in a in a packages and pass then all
992 * to a function. If the called function returns non AE_OK, the
993 * iteration is stopped and that value is returned.
994 */
995
996 ACPI_STATUS
997 acpi_foreach_package_object(ACPI_OBJECT *pkg,
998 ACPI_STATUS (*func)(ACPI_OBJECT *, void *),
999 void *arg)
1000 {
1001 ACPI_STATUS rv = AE_OK;
1002 int i;
1003
1004 if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
1005 return AE_BAD_PARAMETER;
1006
1007 for (i = 0; i < pkg->Package.Count; i++) {
1008 rv = (*func)(&pkg->Package.Elements[i], arg);
1009 if (ACPI_FAILURE(rv))
1010 break;
1011 }
1012
1013 return rv;
1014 }
1015
1016 const char *
1017 acpi_name(ACPI_HANDLE handle)
1018 {
1019 static char buffer[80];
1020 ACPI_BUFFER buf;
1021 ACPI_STATUS rv;
1022
1023 buf.Length = sizeof(buffer);
1024 buf.Pointer = buffer;
1025
1026 rv = AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf);
1027 if (ACPI_FAILURE(rv))
1028 return "(unknown acpi path)";
1029 return buffer;
1030 }
1031
1032 /*
1033 * acpi_get:
1034 *
1035 * Fetch data info the specified (empty) ACPI buffer.
1036 * Caller must free buf.Pointer by AcpiOsFree().
1037 */
1038 ACPI_STATUS
1039 acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
1040 ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
1041 {
1042 buf->Pointer = NULL;
1043 buf->Length = ACPI_ALLOCATE_BUFFER;
1044
1045 return (*getit)(handle, buf);
1046 }
1047
1048
1049 /*
1050 * acpi_match_hid
1051 *
1052 * Match given ids against _HID and _CIDs
1053 */
1054 int
1055 acpi_match_hid(ACPI_DEVICE_INFO *ad, const char * const *ids)
1056 {
1057 int i;
1058
1059 while (*ids) {
1060 if (ad->Valid & ACPI_VALID_HID) {
1061 if (pmatch(ad->HardwareId.Value, *ids, NULL) == 2)
1062 return 1;
1063 }
1064
1065 if (ad->Valid & ACPI_VALID_CID) {
1066 for (i = 0; i < ad->CompatibilityId.Count; i++) {
1067 if (pmatch(ad->CompatibilityId.Id[i].Value, *ids, NULL) == 2)
1068 return 1;
1069 }
1070 }
1071 ids++;
1072 }
1073
1074 return 0;
1075 }
1076
1077 /*
1078 * acpi_set_wake_gpe
1079 *
1080 * Set GPE as both Runtime and Wake
1081 */
1082 void
1083 acpi_set_wake_gpe(ACPI_HANDLE handle)
1084 {
1085 ACPI_BUFFER buf;
1086 ACPI_STATUS rv;
1087 ACPI_OBJECT *p, *elt;
1088
1089 rv = acpi_eval_struct(handle, METHOD_NAME__PRW, &buf);
1090 if (ACPI_FAILURE(rv))
1091 return; /* just ignore */
1092
1093 p = buf.Pointer;
1094 if (p->Type != ACPI_TYPE_PACKAGE || p->Package.Count < 2)
1095 goto out; /* just ignore */
1096
1097 elt = p->Package.Elements;
1098
1099 /* TBD: package support */
1100 AcpiSetGpeType(NULL, elt[0].Integer.Value, ACPI_GPE_TYPE_WAKE_RUN);
1101 AcpiEnableGpe(NULL, elt[0].Integer.Value, ACPI_NOT_ISR);
1102
1103 out:
1104 AcpiOsFree(buf.Pointer);
1105 }
1106
1107
1108 /*****************************************************************************
1109 * ACPI sleep support.
1110 *****************************************************************************/
1111
1112 static int
1113 is_available_state(struct acpi_softc *sc, int state)
1114 {
1115 UINT8 type_a, type_b;
1116
1117 return ACPI_SUCCESS(AcpiGetSleepTypeData((UINT8)state,
1118 &type_a, &type_b));
1119 }
1120
1121 /*
1122 * acpi_enter_sleep_state:
1123 *
1124 * enter to the specified sleep state.
1125 */
1126
1127 ACPI_STATUS
1128 acpi_enter_sleep_state(struct acpi_softc *sc, int state)
1129 {
1130 int err, s;
1131 ACPI_STATUS ret = AE_OK;
1132
1133 if (state == acpi_sleepstate)
1134 return AE_OK;
1135
1136 aprint_normal("%s: entering state %d\n", sc->sc_dev.dv_xname, state);
1137
1138 switch (state) {
1139 case ACPI_STATE_S0:
1140 break;
1141 case ACPI_STATE_S1:
1142 case ACPI_STATE_S2:
1143 case ACPI_STATE_S3:
1144 case ACPI_STATE_S4:
1145 if (!is_available_state(sc, state)) {
1146 aprint_error("%s: cannot enter the sleep state (%d).\n",
1147 sc->sc_dev.dv_xname, state);
1148 break;
1149 }
1150
1151 if (state != ACPI_STATE_S1)
1152 pnp_global_transition(PNP_STATE_D3);
1153
1154 ret = AcpiEnterSleepStatePrep(state);
1155 if (ACPI_FAILURE(ret)) {
1156 aprint_error("%s: failed preparing to sleep (%s)\n",
1157 sc->sc_dev.dv_xname, AcpiFormatException(ret));
1158 break;
1159 }
1160
1161 DELAY(1000000);
1162 acpi_sleepstate = state;
1163 if (state == ACPI_STATE_S1) {
1164 /* just enter the state */
1165 acpi_md_OsDisableInterrupt();
1166 AcpiEnterSleepState((UINT8)state);
1167 AcpiLeaveSleepState((UINT8)state);
1168 } else {
1169 s = splhigh();
1170 err = acpi_md_sleep(state);
1171 if (err == 0 && state == ACPI_STATE_S3)
1172 AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
1173 splx(s);
1174 if (state == ACPI_STATE_S4)
1175 AcpiEnable();
1176 AcpiLeaveSleepState((UINT8)state);
1177 pnp_global_transition(PNP_STATE_D0);
1178 }
1179
1180 break;
1181 case ACPI_STATE_S5:
1182 ret = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1183 if (ACPI_FAILURE(ret)) {
1184 aprint_error("%s: failed preparing to sleep (%s)\n",
1185 sc->sc_dev.dv_xname, AcpiFormatException(ret));
1186 break;
1187 }
1188 DELAY(1000000);
1189 acpi_sleepstate = state;
1190 acpi_md_OsDisableInterrupt();
1191 AcpiEnterSleepState(ACPI_STATE_S5);
1192 aprint_error("%s: WARNING powerdown failed!\n",
1193 sc->sc_dev.dv_xname);
1194 break;
1195 }
1196
1197 acpi_sleepstate = ACPI_STATE_S0;
1198 return ret;
1199 }
1200
1201 #if defined(ACPI_ACTIVATE_DEV)
1202 /* XXX This very incomplete */
1203 ACPI_STATUS
1204 acpi_allocate_resources(ACPI_HANDLE handle)
1205 {
1206 ACPI_BUFFER bufp, bufc, bufn;
1207 ACPI_RESOURCE *resp, *resc, *resn;
1208 ACPI_RESOURCE_IRQ *irq;
1209 ACPI_RESOURCE_EXTENDED_IRQ *xirq;
1210 ACPI_STATUS rv;
1211 uint delta;
1212
1213 rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
1214 if (ACPI_FAILURE(rv))
1215 goto out;
1216 rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
1217 if (ACPI_FAILURE(rv)) {
1218 goto out1;
1219 }
1220
1221 bufn.Length = 1000;
1222 bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
1223 resp = bufp.Pointer;
1224 resc = bufc.Pointer;
1225 while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG &&
1226 resp->Type != ACPI_RESOURCE_TYPE_END_TAG) {
1227 while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG)
1228 resp = ACPI_NEXT_RESOURCE(resp);
1229 if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG)
1230 break;
1231 /* Found identical Id */
1232 resn->Type = resc->Type;
1233 switch (resc->Type) {
1234 case ACPI_RESOURCE_TYPE_IRQ:
1235 memcpy(&resn->Data, &resp->Data,
1236 sizeof(ACPI_RESOURCE_IRQ));
1237 irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
1238 irq->Interrupts[0] =
1239 ((ACPI_RESOURCE_IRQ *)&resp->Data)->
1240 Interrupts[irq->InterruptCount-1];
1241 irq->InterruptCount = 1;
1242 resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
1243 break;
1244 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
1245 memcpy(&resn->Data, &resp->Data,
1246 sizeof(ACPI_RESOURCE_EXTENDED_IRQ));
1247 xirq = (ACPI_RESOURCE_EXTENDED_IRQ *)&resn->Data;
1248 #if 0
1249 /*
1250 * XXX not duplicating the interrupt logic above
1251 * because its not clear what it accomplishes.
1252 */
1253 xirq->Interrupts[0] =
1254 ((ACPI_RESOURCE_EXT_IRQ *)&resp->Data)->
1255 Interrupts[irq->NumberOfInterrupts-1];
1256 xirq->NumberOfInterrupts = 1;
1257 #endif
1258 resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
1259 break;
1260 case ACPI_RESOURCE_TYPE_IO:
1261 memcpy(&resn->Data, &resp->Data,
1262 sizeof(ACPI_RESOURCE_IO));
1263 resn->Length = resp->Length;
1264 break;
1265 default:
1266 printf("acpi_allocate_resources: res=%d\n", resc->Type);
1267 rv = AE_BAD_DATA;
1268 goto out2;
1269 }
1270 resc = ACPI_NEXT_RESOURCE(resc);
1271 resn = ACPI_NEXT_RESOURCE(resn);
1272 resp = ACPI_NEXT_RESOURCE(resp);
1273 delta = (UINT8 *)resn - (UINT8 *)bufn.Pointer;
1274 if (delta >=
1275 bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) {
1276 bufn.Length *= 2;
1277 bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
1278 M_ACPI, M_WAITOK);
1279 resn = (ACPI_RESOURCE *)((UINT8 *)bufn.Pointer + delta);
1280 }
1281 }
1282 if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) {
1283 printf("acpi_allocate_resources: resc not exhausted\n");
1284 rv = AE_BAD_DATA;
1285 goto out3;
1286 }
1287
1288 resn->Type = ACPI_RESOURCE_TYPE_END_TAG;
1289 rv = AcpiSetCurrentResources(handle, &bufn);
1290 if (ACPI_FAILURE(rv)) {
1291 printf("acpi_allocate_resources: AcpiSetCurrentResources %s\n",
1292 AcpiFormatException(rv));
1293 }
1294
1295 out3:
1296 free(bufn.Pointer, M_ACPI);
1297 out2:
1298 AcpiOsFree(bufc.Pointer);
1299 out1:
1300 AcpiOsFree(bufp.Pointer);
1301 out:
1302 return rv;
1303 }
1304 #endif /* ACPI_ACTIVATE_DEV */
1305
1306 SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
1307 {
1308 const struct sysctlnode *node;
1309 const struct sysctlnode *ssnode;
1310
1311 if (sysctl_createv(clog, 0, NULL, NULL,
1312 CTLFLAG_PERMANENT,
1313 CTLTYPE_NODE, "hw", NULL,
1314 NULL, 0, NULL, 0,
1315 CTL_HW, CTL_EOL) != 0)
1316 return;
1317
1318 if (sysctl_createv(clog, 0, NULL, &node,
1319 CTLFLAG_PERMANENT,
1320 CTLTYPE_NODE, "acpi", NULL,
1321 NULL, 0, NULL, 0,
1322 CTL_HW, CTL_CREATE, CTL_EOL) != 0)
1323 return;
1324
1325 acpi_node = node->sysctl_num;
1326
1327 /* ACPI sleepstate sysctl */
1328 if (sysctl_createv(NULL, 0, NULL, &node,
1329 CTLFLAG_PERMANENT,
1330 CTLTYPE_NODE, "machdep", NULL,
1331 NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL) != 0)
1332 return;
1333 if (sysctl_createv(NULL, 0, &node, &ssnode,
1334 CTLFLAG_READWRITE, CTLTYPE_INT, "sleep_state",
1335 NULL, sysctl_hw_acpi_sleepstate, 0, NULL, 0, CTL_CREATE,
1336 CTL_EOL) != 0)
1337 return;
1338 }
1339
1340 static int
1341 sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
1342 {
1343 int error, t;
1344 struct sysctlnode node;
1345
1346 node = *rnode;
1347 t = acpi_sleepstate;
1348 node.sysctl_data = &t;
1349 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1350 if (error || newp == NULL)
1351 return error;
1352
1353 if (acpi_softc == NULL)
1354 return ENOSYS;
1355
1356 acpi_enter_sleep_state(acpi_softc, t);
1357
1358 return 0;
1359 }
1360