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