xref: /freebsd/sys/dev/adb/adb_buttons.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /*-
2  * Copyright (c) 2011, Justin Hibbits.
3  * Copyright (c) 2002, Miodrag Vallat.
4  * Copyright (C) 1999 Tsubai Masanari.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * OpenBSD: abtn.c,v 1.12 2009/01/10 18:00:59 robert Exp
29  * NetBSD: abtn.c,v 1.1 1999/07/12 17:48:26 tsubai Exp
30  *
31  * $FreeBSD$
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/module.h>
37 #include <sys/kernel.h>
38 #include <sys/bus.h>
39 
40 #include <machine/bus.h>
41 
42 #include <dev/ofw/openfirm.h>
43 #include <machine/ofw_machdep.h>
44 
45 #include <dev/adb/adb.h>
46 
47 #define ABTN_HANDLER_ID 31
48 
49 struct abtn_softc {
50 	device_t sc_dev;
51 
52 	int handler_id;
53 };
54 
55 static int abtn_probe(device_t dev);
56 static int abtn_attach(device_t dev);
57 static u_int abtn_receive_packet(device_t dev, u_char status,
58     u_char command, u_char reg, int len, u_char *data);
59 
60 static device_method_t abtn_methods[] = {
61 	/* Device interface */
62 	DEVMETHOD(device_probe,         abtn_probe),
63         DEVMETHOD(device_attach,        abtn_attach),
64         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
65         DEVMETHOD(device_suspend,       bus_generic_suspend),
66         DEVMETHOD(device_resume,        bus_generic_resume),
67 
68 	/* ADB interface */
69 	DEVMETHOD(adb_receive_packet,	abtn_receive_packet),
70 
71 	{ 0, 0 }
72 };
73 
74 static driver_t abtn_driver = {
75 	"abtn",
76 	abtn_methods,
77 	sizeof(struct abtn_softc),
78 };
79 
80 static devclass_t abtn_devclass;
81 
82 DRIVER_MODULE(abtn, adb, abtn_driver, abtn_devclass, 0, 0);
83 
84 static int
85 abtn_probe(device_t dev)
86 {
87 	uint8_t type;
88 
89 	type = adb_get_device_type(dev);
90 
91 	if (type != ADB_DEVICE_MISC)
92 		return (ENXIO);
93 
94 	device_set_desc(dev, "ADB Brightness/Volume/Eject Buttons");
95 	return (0);
96 }
97 
98 static int
99 abtn_attach(device_t dev)
100 {
101 	struct abtn_softc *sc;
102 
103 	sc = device_get_softc(dev);
104 	sc->sc_dev = dev;
105 
106 	sc->handler_id = adb_get_device_handler(dev);
107 
108 	return 0;
109 }
110 
111 static u_int
112 abtn_receive_packet(device_t dev, u_char status,
113     u_char command, u_char reg, int len, u_char *data)
114 {
115 	u_int cmd;
116 
117 	cmd = data[0];
118 
119 	switch (cmd) {
120 	case 0x0a:	/* decrease brightness */
121 		devctl_notify("PMU", "keys", "brightness",
122 		    "notify=down");
123 		break;
124 
125 	case 0x09:	/* increase brightness */
126 		devctl_notify("PMU", "keys", "brightness", "notify=up");
127 		break;
128 
129 	case 0x08:	/* mute */
130 	case 0x01:	/* mute, AV hardware */
131 		devctl_notify("PMU", "keys", "mute", NULL);
132 		break;
133 	case 0x07:	/* decrease volume */
134 	case 0x02:	/* decrease volume, AV hardware */
135 		devctl_notify("PMU", "keys", "volume", "notify=down");
136 		break;
137 	case 0x06:	/* increase volume */
138 	case 0x03:	/* increase volume, AV hardware */
139 		devctl_notify("PMU", "keys", "volume", "notify=up");
140 		break;
141 	case 0x0c:	/* mirror display key */
142 		/* Need callback to do something with this */
143 		break;
144 	case 0x0b:	/* eject tray */
145 		devctl_notify("PMU", "keys", "eject", NULL);
146 		break;
147 	case 0x7f:	/* numlock */
148 		/* Need callback to do something with this */
149 		break;
150 
151 	default:
152 #ifdef DEBUG
153 		if ((cmd & ~0x7f) == 0)
154 			device_printf(dev, "unknown ADB button 0x%x\n", cmd);
155 #endif
156 		break;
157 	}
158 	return 0;
159 }
160 
161