xref: /openbsd/sys/arch/macppc/dev/sysbutton.c (revision 404b540a)
1 /*	$OpenBSD: sysbutton.c,v 1.4 2008/06/13 00:31:09 krw Exp $	*/
2 /*
3  * Copyright (c) 2007 Gordon Willem Klok <gwk@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/types.h>
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/proc.h>
22 #include <sys/device.h>
23 
24 #include <ddb/db_var.h>
25 #include <dev/ofw/openfirm.h>
26 
27 #include <machine/bus.h>
28 #include <machine/autoconf.h>
29 
30 struct sysbutton_softc {
31 	struct device	sc_dev;
32 	int		sc_node;
33 	int 		sc_intr;
34 };
35 
36 int sysbutton_match(struct device *, void *, void *);
37 void sysbutton_attach(struct device *, struct device *, void *);
38 int sysbutton_intr(void *);
39 
40 struct cfattach sysbutton_ca = {
41 	sizeof(struct sysbutton_softc), sysbutton_match,
42 	sysbutton_attach
43 };
44 
45 struct cfdriver sysbutton_cd = {
46 	NULL, "sysbutton", DV_DULL
47 };
48 
49 int
50 sysbutton_match(struct device *parent, void *arg, void *aux)
51 {
52 	struct confargs *ca = aux;
53 
54 	if (strcmp(ca->ca_name, "indicatorSwitch-gpio") == 0)
55 		return 1;
56 
57 	return 0;
58 }
59 
60 void
61 sysbutton_attach(struct device *parent, struct device *self, void *aux)
62 {
63 	struct sysbutton_softc *sc = (struct sysbutton_softc *)self;
64 	struct confargs *ca = aux;
65 	int intr[2];
66 
67 	sc->sc_node = ca->ca_node;
68 
69 	OF_getprop(sc->sc_node, "interrupts", intr, sizeof(intr));
70 	sc->sc_intr = intr[0];
71 
72 	printf(": irq %d\n", sc->sc_intr);
73 
74 	mac_intr_establish(parent, sc->sc_intr, IST_EDGE,
75 	    IPL_NONE, sysbutton_intr, sc, sc->sc_dev.dv_xname);
76 }
77 
78 int
79 sysbutton_intr(void *v)
80 {
81 
82 	/*
83 	 * XXX: Holding this button causes an interrupt storm if
84 	 * ddb.console=0.
85 	 */
86 #ifdef DDB
87 	if (db_console)
88 		Debugger();
89 #endif
90 
91 	return 1;
92 }
93