acpi_ged.c revision 1.1
11.1Sjmcneill/* $NetBSD: acpi_ged.c,v 1.1 2018/10/22 22:36:19 jmcneill Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2018 The NetBSD Foundation, Inc.
51.1Sjmcneill * All rights reserved.
61.1Sjmcneill *
71.1Sjmcneill * This code is derived from software contributed to The NetBSD Foundation
81.1Sjmcneill * by Jared McNeill <jmcneill@invisible.ca>.
91.1Sjmcneill *
101.1Sjmcneill * Redistribution and use in source and binary forms, with or without
111.1Sjmcneill * modification, are permitted provided that the following conditions
121.1Sjmcneill * are met:
131.1Sjmcneill * 1. Redistributions of source code must retain the above copyright
141.1Sjmcneill *    notice, this list of conditions and the following disclaimer.
151.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
161.1Sjmcneill *    notice, this list of conditions and the following disclaimer in the
171.1Sjmcneill *    documentation and/or other materials provided with the distribution.
181.1Sjmcneill *
191.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Sjmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Sjmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Sjmcneill * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Sjmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Sjmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Sjmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Sjmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Sjmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Sjmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Sjmcneill * POSSIBILITY OF SUCH DAMAGE.
301.1Sjmcneill */
311.1Sjmcneill
321.1Sjmcneill#include <sys/cdefs.h>
331.1Sjmcneill__KERNEL_RCSID(0, "$NetBSD: acpi_ged.c,v 1.1 2018/10/22 22:36:19 jmcneill Exp $");
341.1Sjmcneill
351.1Sjmcneill#include <sys/param.h>
361.1Sjmcneill#include <sys/bus.h>
371.1Sjmcneill#include <sys/cpu.h>
381.1Sjmcneill#include <sys/device.h>
391.1Sjmcneill#include <sys/intr.h>
401.1Sjmcneill#include <sys/kmem.h>
411.1Sjmcneill
421.1Sjmcneill#include <dev/acpi/acpireg.h>
431.1Sjmcneill#include <dev/acpi/acpivar.h>
441.1Sjmcneill#include <dev/acpi/acpi_event.h>
451.1Sjmcneill
461.1Sjmcneillstatic int	acpi_ged_match(device_t, cfdata_t, void *);
471.1Sjmcneillstatic void	acpi_ged_attach(device_t, device_t, void *);
481.1Sjmcneill
491.1Sjmcneillstatic void	acpi_ged_register_event(void *, struct acpi_event *, struct acpi_irq *);
501.1Sjmcneillstatic int	acpi_ged_intr(void *);
511.1Sjmcneill
521.1SjmcneillCFATTACH_DECL_NEW(acpiged, 0, acpi_ged_match, acpi_ged_attach, NULL, NULL);
531.1Sjmcneill
541.1Sjmcneillstatic const char * const compatible[] = {
551.1Sjmcneill	"ACPI0013",
561.1Sjmcneill	NULL
571.1Sjmcneill};
581.1Sjmcneill
591.1Sjmcneillstatic int
601.1Sjmcneillacpi_ged_match(device_t parent, cfdata_t cf, void *aux)
611.1Sjmcneill{
621.1Sjmcneill	struct acpi_attach_args *aa = aux;
631.1Sjmcneill
641.1Sjmcneill	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
651.1Sjmcneill		return 0;
661.1Sjmcneill
671.1Sjmcneill	return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
681.1Sjmcneill}
691.1Sjmcneill
701.1Sjmcneillstatic void
711.1Sjmcneillacpi_ged_attach(device_t parent, device_t self, void *aux)
721.1Sjmcneill{
731.1Sjmcneill	struct acpi_attach_args * const aa = aux;
741.1Sjmcneill	ACPI_HANDLE handle = aa->aa_node->ad_handle;
751.1Sjmcneill
761.1Sjmcneill        if (ACPI_FAILURE(acpi_event_create_int(self, handle, acpi_ged_register_event, self)))
771.1Sjmcneill                aprint_error_dev(self, "failed to create events\n");
781.1Sjmcneill}
791.1Sjmcneill
801.1Sjmcneillstatic void
811.1Sjmcneillacpi_ged_register_event(void *priv, struct acpi_event *ev, struct acpi_irq *irq)
821.1Sjmcneill{
831.1Sjmcneill	device_t dev = priv;
841.1Sjmcneill	void *ih;
851.1Sjmcneill
861.1Sjmcneill	const int type = (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL;
871.1Sjmcneill	ih = intr_establish(irq->ar_irq, IPL_VM, type, acpi_ged_intr, ev);
881.1Sjmcneill	if (ih == NULL) {
891.1Sjmcneill		aprint_error_dev(dev, "couldn't establish interrupt (irq %d)\n", irq->ar_irq);
901.1Sjmcneill		return;
911.1Sjmcneill	}
921.1Sjmcneill}
931.1Sjmcneill
941.1Sjmcneillstatic int
951.1Sjmcneillacpi_ged_intr(void *priv)
961.1Sjmcneill{
971.1Sjmcneill	struct acpi_event * const ev = priv;
981.1Sjmcneill
991.1Sjmcneill	acpi_event_notify(ev);
1001.1Sjmcneill
1011.1Sjmcneill	return 1;
1021.1Sjmcneill}
103