Home | History | Annotate | Line # | Download | only in acpi
acpi_wakedev.c revision 1.2.4.2
      1  1.2.4.2  bouyer /* $NetBSD: acpi_wakedev.c,v 1.2.4.2 2009/09/05 11:34:26 bouyer Exp $ */
      2  1.2.4.2  bouyer 
      3  1.2.4.2  bouyer /*-
      4  1.2.4.2  bouyer  * Copyright (c) 2009 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.2.4.2  bouyer  * All rights reserved.
      6  1.2.4.2  bouyer  *
      7  1.2.4.2  bouyer  * Redistribution and use in source and binary forms, with or without
      8  1.2.4.2  bouyer  * modification, are permitted provided that the following conditions
      9  1.2.4.2  bouyer  * are met:
     10  1.2.4.2  bouyer  * 1. Redistributions of source code must retain the above copyright
     11  1.2.4.2  bouyer  *    notice, this list of conditions and the following disclaimer.
     12  1.2.4.2  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.2.4.2  bouyer  *    notice, this list of conditions and the following disclaimer in the
     14  1.2.4.2  bouyer  *    documentation and/or other materials provided with the distribution.
     15  1.2.4.2  bouyer  *
     16  1.2.4.2  bouyer  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.2.4.2  bouyer  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.2.4.2  bouyer  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.2.4.2  bouyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.2.4.2  bouyer  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.2.4.2  bouyer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.2.4.2  bouyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.2.4.2  bouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.2.4.2  bouyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.2.4.2  bouyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.2.4.2  bouyer  * POSSIBILITY OF SUCH DAMAGE.
     27  1.2.4.2  bouyer  */
     28  1.2.4.2  bouyer 
     29  1.2.4.2  bouyer #include <sys/cdefs.h>
     30  1.2.4.2  bouyer __KERNEL_RCSID(0, "$NetBSD: acpi_wakedev.c,v 1.2.4.2 2009/09/05 11:34:26 bouyer Exp $");
     31  1.2.4.2  bouyer 
     32  1.2.4.2  bouyer #include <sys/types.h>
     33  1.2.4.2  bouyer #include <sys/param.h>
     34  1.2.4.2  bouyer #include <sys/kernel.h>
     35  1.2.4.2  bouyer #include <sys/kmem.h>
     36  1.2.4.2  bouyer #include <sys/device.h>
     37  1.2.4.2  bouyer #include <sys/queue.h>
     38  1.2.4.2  bouyer #include <sys/sysctl.h>
     39  1.2.4.2  bouyer 
     40  1.2.4.2  bouyer #include <dev/acpi/acpivar.h>
     41  1.2.4.2  bouyer #include <dev/acpi/acpireg.h>
     42  1.2.4.2  bouyer #include <dev/acpi/acpi_wakedev.h>
     43  1.2.4.2  bouyer 
     44  1.2.4.2  bouyer struct acpi_wakedev;
     45  1.2.4.2  bouyer 
     46  1.2.4.2  bouyer static TAILQ_HEAD(, acpi_wakedev) acpi_wakedevlist =
     47  1.2.4.2  bouyer     TAILQ_HEAD_INITIALIZER(acpi_wakedevlist);;
     48  1.2.4.2  bouyer static int acpi_wakedev_node = -1;
     49  1.2.4.2  bouyer 
     50  1.2.4.2  bouyer struct acpi_wakedev {
     51  1.2.4.2  bouyer 	struct acpi_devnode	*aw_node;
     52  1.2.4.2  bouyer 	int			aw_enabled;
     53  1.2.4.2  bouyer 	struct sysctllog	*aw_sysctllog;
     54  1.2.4.2  bouyer 	TAILQ_ENTRY(acpi_wakedev) aw_list;
     55  1.2.4.2  bouyer };
     56  1.2.4.2  bouyer 
     57  1.2.4.2  bouyer static const char * const acpi_wakedev_default[] = {
     58  1.2.4.2  bouyer 	"PNP0C0C",	/* power button */
     59  1.2.4.2  bouyer 	"PNP0C0E",	/* sleep button */
     60  1.2.4.2  bouyer 	"PNP0C0D",	/* lid switch */
     61  1.2.4.2  bouyer 	"PNP03??",	/* PC KBD port */
     62  1.2.4.2  bouyer 	NULL,
     63  1.2.4.2  bouyer };
     64  1.2.4.2  bouyer 
     65  1.2.4.2  bouyer SYSCTL_SETUP(sysctl_acpi_wakedev_setup, "sysctl hw.wake subtree setup")
     66  1.2.4.2  bouyer {
     67  1.2.4.2  bouyer 	const struct sysctlnode *rnode;
     68  1.2.4.2  bouyer 	int err;
     69  1.2.4.2  bouyer 
     70  1.2.4.2  bouyer 	err = sysctl_createv(NULL, 0, NULL, NULL,
     71  1.2.4.2  bouyer 	    CTLFLAG_PERMANENT,
     72  1.2.4.2  bouyer 	    CTLTYPE_NODE, "hw", NULL,
     73  1.2.4.2  bouyer 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL);
     74  1.2.4.2  bouyer 	if (err)
     75  1.2.4.2  bouyer 		return;
     76  1.2.4.2  bouyer 	err = sysctl_createv(NULL, 0, NULL, &rnode,
     77  1.2.4.2  bouyer 	    CTLFLAG_PERMANENT,
     78  1.2.4.2  bouyer 	    CTLTYPE_NODE, "wake", NULL,
     79  1.2.4.2  bouyer 	    NULL, 0, NULL, 0,
     80  1.2.4.2  bouyer 	    CTL_HW, CTL_CREATE, CTL_EOL);
     81  1.2.4.2  bouyer 	if (err)
     82  1.2.4.2  bouyer 		return;
     83  1.2.4.2  bouyer 	acpi_wakedev_node = rnode->sysctl_num;
     84  1.2.4.2  bouyer }
     85  1.2.4.2  bouyer 
     86  1.2.4.2  bouyer static void
     87  1.2.4.2  bouyer acpi_wakedev_sysctl_add(struct acpi_wakedev *aw)
     88  1.2.4.2  bouyer {
     89  1.2.4.2  bouyer 	int err;
     90  1.2.4.2  bouyer 
     91  1.2.4.2  bouyer 	if (acpi_wakedev_node == -1)
     92  1.2.4.2  bouyer 		return;
     93  1.2.4.2  bouyer 
     94  1.2.4.2  bouyer 	err = sysctl_createv(&aw->aw_sysctllog, 0, NULL, NULL,
     95  1.2.4.2  bouyer 	    CTLFLAG_READWRITE, CTLTYPE_INT, aw->aw_node->ad_name,
     96  1.2.4.2  bouyer 	    NULL, NULL, 0, &aw->aw_enabled, 0,
     97  1.2.4.2  bouyer 	    CTL_HW, acpi_wakedev_node, CTL_CREATE, CTL_EOL);
     98  1.2.4.2  bouyer 	if (err)
     99  1.2.4.2  bouyer 		printf("acpi_wakedev_sysctl_add: "
    100  1.2.4.2  bouyer 		    "sysctl_createv(hw.wake.%s) failed. (%d)\n",
    101  1.2.4.2  bouyer 		    aw->aw_node->ad_name, err);
    102  1.2.4.2  bouyer }
    103  1.2.4.2  bouyer 
    104  1.2.4.2  bouyer static bool
    105  1.2.4.2  bouyer acpi_wakedev_add(struct acpi_softc *sc, struct acpi_devnode *ad)
    106  1.2.4.2  bouyer {
    107  1.2.4.2  bouyer 	struct acpi_wakedev *aw;
    108  1.2.4.2  bouyer 	ACPI_HANDLE hdl;
    109  1.2.4.2  bouyer 
    110  1.2.4.2  bouyer 	if (ACPI_FAILURE(AcpiGetHandle(ad->ad_handle, "_PRW", &hdl)))
    111  1.2.4.2  bouyer 		return false;
    112  1.2.4.2  bouyer 
    113  1.2.4.2  bouyer 	aw = kmem_alloc(sizeof(*aw), KM_SLEEP);
    114  1.2.4.2  bouyer 	if (aw == NULL) {
    115  1.2.4.2  bouyer 		aprint_error("acpi_wakedev_add: kmem_alloc failed\n");
    116  1.2.4.2  bouyer 		return false;
    117  1.2.4.2  bouyer 	}
    118  1.2.4.2  bouyer 	aw->aw_node = ad;
    119  1.2.4.2  bouyer 	aw->aw_sysctllog = NULL;
    120  1.2.4.2  bouyer 	if (acpi_match_hid(ad->ad_devinfo, acpi_wakedev_default))
    121  1.2.4.2  bouyer 		aw->aw_enabled = 1;
    122  1.2.4.2  bouyer 	else
    123  1.2.4.2  bouyer 		aw->aw_enabled = 0;
    124  1.2.4.2  bouyer 
    125  1.2.4.2  bouyer 	TAILQ_INSERT_TAIL(&acpi_wakedevlist, aw, aw_list);
    126  1.2.4.2  bouyer 
    127  1.2.4.2  bouyer 	acpi_wakedev_sysctl_add(aw);
    128  1.2.4.2  bouyer 
    129  1.2.4.2  bouyer 	return true;
    130  1.2.4.2  bouyer }
    131  1.2.4.2  bouyer 
    132  1.2.4.2  bouyer static void
    133  1.2.4.2  bouyer acpi_wakedev_print(struct acpi_wakedev *aw)
    134  1.2.4.2  bouyer {
    135  1.2.4.2  bouyer 	aprint_debug(" %s", aw->aw_node->ad_name);
    136  1.2.4.2  bouyer }
    137  1.2.4.2  bouyer 
    138  1.2.4.2  bouyer int
    139  1.2.4.2  bouyer acpi_wakedev_scan(struct acpi_softc *sc)
    140  1.2.4.2  bouyer {
    141  1.2.4.2  bouyer 	struct acpi_scope *as;
    142  1.2.4.2  bouyer 	struct acpi_devnode *ad;
    143  1.2.4.2  bouyer 	struct acpi_wakedev *aw;
    144  1.2.4.2  bouyer 	ACPI_DEVICE_INFO *di;
    145  1.2.4.2  bouyer 	int count = 0;
    146  1.2.4.2  bouyer 
    147  1.2.4.2  bouyer #define ACPI_STA_DEV_VALID	\
    148  1.2.4.2  bouyer 	(ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|ACPI_STA_DEV_OK)
    149  1.2.4.2  bouyer 
    150  1.2.4.2  bouyer 	TAILQ_FOREACH(as, &sc->sc_scopes, as_list)
    151  1.2.4.2  bouyer 		TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) {
    152  1.2.4.2  bouyer 			di = ad->ad_devinfo;
    153  1.2.4.2  bouyer 			if (di->Type != ACPI_TYPE_DEVICE)
    154  1.2.4.2  bouyer 				continue;
    155  1.2.4.2  bouyer 			if ((di->Valid & ACPI_VALID_STA) != 0 &&
    156  1.2.4.2  bouyer 			    (di->CurrentStatus & ACPI_STA_DEV_VALID) !=
    157  1.2.4.2  bouyer 			     ACPI_STA_DEV_VALID)
    158  1.2.4.2  bouyer 				continue;
    159  1.2.4.2  bouyer 			if (acpi_wakedev_add(sc, ad) == true)
    160  1.2.4.2  bouyer 				++count;
    161  1.2.4.2  bouyer 		}
    162  1.2.4.2  bouyer 
    163  1.2.4.2  bouyer #undef ACPI_STA_DEV_VALID
    164  1.2.4.2  bouyer 
    165  1.2.4.2  bouyer 	if (count == 0)
    166  1.2.4.2  bouyer 		return 0;
    167  1.2.4.2  bouyer 
    168  1.2.4.2  bouyer 	aprint_debug_dev(sc->sc_dev, "wakeup devices:");
    169  1.2.4.2  bouyer 	TAILQ_FOREACH(aw, &acpi_wakedevlist, aw_list)
    170  1.2.4.2  bouyer 		acpi_wakedev_print(aw);
    171  1.2.4.2  bouyer 	aprint_debug("\n");
    172  1.2.4.2  bouyer 
    173  1.2.4.2  bouyer 	return count;
    174  1.2.4.2  bouyer }
    175  1.2.4.2  bouyer 
    176  1.2.4.2  bouyer void
    177  1.2.4.2  bouyer acpi_wakedev_commit(struct acpi_softc *sc)
    178  1.2.4.2  bouyer {
    179  1.2.4.2  bouyer 	struct acpi_wakedev *aw;
    180  1.2.4.2  bouyer 
    181  1.2.4.2  bouyer 	TAILQ_FOREACH(aw, &acpi_wakedevlist, aw_list) {
    182  1.2.4.2  bouyer 		if (aw->aw_enabled) {
    183  1.2.4.2  bouyer 			aprint_debug_dev(sc->sc_dev, "set wake GPE (%s)\n",
    184  1.2.4.2  bouyer 			    aw->aw_node->ad_name);
    185  1.2.4.2  bouyer 			acpi_set_wake_gpe(aw->aw_node->ad_handle);
    186  1.2.4.2  bouyer 		} else
    187  1.2.4.2  bouyer 			acpi_clear_wake_gpe(aw->aw_node->ad_handle);
    188  1.2.4.2  bouyer 	}
    189  1.2.4.2  bouyer }
    190