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