acpi_ged.c revision 1.2
11.2Sjmcneill/* $NetBSD: acpi_ged.c,v 1.2 2020/10/23 11:00:09 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.2Sjmcneill__KERNEL_RCSID(0, "$NetBSD: acpi_ged.c,v 1.2 2020/10/23 11:00:09 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.2Sjmcneill#include <dev/acpi/acpi_intr.h>
461.1Sjmcneill
471.1Sjmcneillstatic int	acpi_ged_match(device_t, cfdata_t, void *);
481.1Sjmcneillstatic void	acpi_ged_attach(device_t, device_t, void *);
491.1Sjmcneill
501.1Sjmcneillstatic void	acpi_ged_register_event(void *, struct acpi_event *, struct acpi_irq *);
511.1Sjmcneillstatic int	acpi_ged_intr(void *);
521.1Sjmcneill
531.1SjmcneillCFATTACH_DECL_NEW(acpiged, 0, acpi_ged_match, acpi_ged_attach, NULL, NULL);
541.1Sjmcneill
551.1Sjmcneillstatic const char * const compatible[] = {
561.1Sjmcneill	"ACPI0013",
571.1Sjmcneill	NULL
581.1Sjmcneill};
591.1Sjmcneill
601.1Sjmcneillstatic int
611.1Sjmcneillacpi_ged_match(device_t parent, cfdata_t cf, void *aux)
621.1Sjmcneill{
631.1Sjmcneill	struct acpi_attach_args *aa = aux;
641.1Sjmcneill
651.1Sjmcneill	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
661.1Sjmcneill		return 0;
671.1Sjmcneill
681.1Sjmcneill	return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
691.1Sjmcneill}
701.1Sjmcneill
711.1Sjmcneillstatic void
721.1Sjmcneillacpi_ged_attach(device_t parent, device_t self, void *aux)
731.1Sjmcneill{
741.1Sjmcneill	struct acpi_attach_args * const aa = aux;
751.1Sjmcneill	ACPI_HANDLE handle = aa->aa_node->ad_handle;
761.1Sjmcneill
771.1Sjmcneill        if (ACPI_FAILURE(acpi_event_create_int(self, handle, acpi_ged_register_event, self)))
781.1Sjmcneill                aprint_error_dev(self, "failed to create events\n");
791.1Sjmcneill}
801.1Sjmcneill
811.1Sjmcneillstatic void
821.1Sjmcneillacpi_ged_register_event(void *priv, struct acpi_event *ev, struct acpi_irq *irq)
831.1Sjmcneill{
841.1Sjmcneill	device_t dev = priv;
851.1Sjmcneill	void *ih;
861.1Sjmcneill
871.2Sjmcneill	ih = acpi_intr_establish_irq(dev, irq, IPL_VM, true,
881.2Sjmcneill	    acpi_ged_intr, ev, device_xname(dev));
891.1Sjmcneill	if (ih == NULL) {
901.1Sjmcneill		aprint_error_dev(dev, "couldn't establish interrupt (irq %d)\n", irq->ar_irq);
911.1Sjmcneill		return;
921.1Sjmcneill	}
931.1Sjmcneill}
941.1Sjmcneill
951.1Sjmcneillstatic int
961.1Sjmcneillacpi_ged_intr(void *priv)
971.1Sjmcneill{
981.1Sjmcneill	struct acpi_event * const ev = priv;
991.1Sjmcneill
1001.1Sjmcneill	acpi_event_notify(ev);
1011.1Sjmcneill
1021.1Sjmcneill	return 1;
1031.1Sjmcneill}
104