summaryrefslogtreecommitdiff
path: root/sys/dev/acpi/acpige.c
blob: 1606f1db55cd668192a1d266ee23fc87bb1d4051 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*	$OpenBSD: acpige.c,v 1.1 2020/11/15 18:04:10 patrick Exp $	*/
/*
 * Copyright (c) 2020 Patrick Wildt <patrick@blueri.se>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/systm.h>
#include <sys/tty.h>

#include <dev/acpi/acpireg.h>
#include <dev/acpi/acpivar.h>
#include <dev/acpi/acpidev.h>
#include <dev/acpi/amltypes.h>
#include <dev/acpi/dsdt.h>

struct acpige_softc;
struct acpige_irq {
	struct acpige_softc	*ai_sc;
	void			*ai_ih;
	uint32_t		ai_irq;
};

struct acpige_softc {
	struct device		sc_dev;
	struct acpi_softc	*sc_acpi;
	struct aml_node		*sc_devnode;
	struct acpige_irq	*sc_irq;
};

int	acpige_match(struct device *, void *, void *);
void	acpige_attach(struct device *, struct device *, void *);

int	acpige_intr(void *);
void	acpige_event_task(void *, int);

struct cfattach acpige_ca = {
	sizeof(struct acpige_softc), acpige_match, acpige_attach
};

struct cfdriver acpige_cd = {
	NULL, "acpige", DV_DULL
};

const char *acpige_hids[] = {
	"ACPI0013",
	NULL
};

int
acpige_match(struct device *parent, void *match, void *aux)
{
	struct acpi_attach_args *aaa = aux;
	struct cfdata *cf = match;

	return acpi_matchhids(aaa, acpige_hids, cf->cf_driver->cd_name);
}

void
acpige_attach(struct device *parent, struct device *self, void *aux)
{
	struct acpige_softc *sc = (struct acpige_softc *)self;
	struct acpi_attach_args *aaa = aux;
	struct acpige_irq *ai;
	int i;

	sc->sc_acpi = (struct acpi_softc *)parent;
	sc->sc_devnode = aaa->aaa_node;

	if (aaa->aaa_nirq < 1) {
		printf(": no interrupt\n");
		return;
	}

	sc->sc_irq = mallocarray(aaa->aaa_nirq, sizeof(struct acpige_irq),
	    M_DEVBUF, M_WAITOK);

	for (i = 0; i < aaa->aaa_nirq; i++) {
		printf("%s %d", i ? "," : " irq", aaa->aaa_irq[i]);
		ai = &sc->sc_irq[i];
		ai->ai_sc = sc;
		ai->ai_irq = aaa->aaa_irq[i];
		ai->ai_ih = acpi_intr_establish(aaa->aaa_irq[i],
		    aaa->aaa_irq_flags[i], IPL_BIO, acpige_intr, ai,
		    sc->sc_dev.dv_xname);
		if (ai->ai_ih == NULL) {
			printf(": can't establish interrupt\n");
			return;
		}
	}

	printf("\n");
}

int
acpige_intr(void *arg)
{
	struct acpige_irq *ai = arg;
	struct acpige_softc *sc = ai->ai_sc;

	acpi_addtask(sc->sc_acpi, acpige_event_task, sc, ai->ai_irq);
	acpi_wakeup(sc->sc_acpi);
	return 1;
}

void
acpige_event_task(void *arg0, int irq)
{
	struct acpige_softc *sc = arg0;
	struct aml_value cmd;

	memset(&cmd, 0, sizeof cmd);
	cmd.v_integer = irq;
	cmd.type = AML_OBJTYPE_INTEGER;
	aml_evalname(sc->sc_acpi, sc->sc_devnode, "_EVT", 1, &cmd, NULL);
}