adb_bt.c revision 1.8 1 /* $NetBSD: adb_bt.c,v 1.8 2025/06/11 13:45:02 macallan Exp $ */
2
3 /*-
4 * Copyright (c) 2006 Michael Lorenz
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: adb_bt.c,v 1.8 2025/06/11 13:45:02 macallan Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/device.h>
36 #include <sys/proc.h>
37
38 #include <machine/autoconf.h>
39 #include <machine/keyboard.h>
40
41 #include <dev/adb/adbvar.h>
42
43 #include "ioconf.h"
44
45 #include "opt_wsdisplay_compat.h"
46 #include "adbdebug.h"
47
48 #ifdef ADBBT_DEBUG
49 #define DPRINTF printf
50 #else
51 #define DPRINTF while (0) printf
52 #endif
53
54 #define BT_VOL_UP 0x06
55 #define BT_VOL_DOWN 0x07
56 #define BT_VOL_MUTE 0x08
57 #define BT_BRT_UP 0x09
58 #define BT_BRT_DOWN 0x0a
59 #define BT_EJECT 0x0b
60 #define BT_F7 0x0c
61 #define BT_NUMLOCK 0x7f
62
63 static int adbbt_match(device_t, cfdata_t, void *);
64 static void adbbt_attach(device_t, device_t, void *);
65
66 struct adbbt_softc {
67 device_t sc_dev;
68 struct adb_device *sc_adbdev;
69 struct adb_bus_accessops *sc_ops;
70 int sc_msg_len;
71 int sc_event;
72 uint8_t sc_buffer[16];
73 uint8_t sc_us;
74 };
75
76 /* Driver definition. */
77 CFATTACH_DECL_NEW(adbbt, sizeof(struct adbbt_softc),
78 adbbt_match, adbbt_attach, NULL, NULL);
79
80 static void adbbt_handler(void *, int, uint8_t *);
81
82 static int
83 adbbt_match(device_t parent, cfdata_t cf, void *aux)
84 {
85 struct adb_attach_args *aaa = aux;
86
87 if ((aaa->dev->original_addr == ADBADDR_MISC) &&
88 (aaa->dev->handler_id == 0x1f))
89 return 100;
90 else
91 return 0;
92 }
93
94 static void
95 adbbt_attach(device_t parent, device_t self, void *aux)
96 {
97 struct adbbt_softc *sc = device_private(self);
98 struct adb_attach_args *aaa = aux;
99
100 sc->sc_dev = self;
101 sc->sc_ops = aaa->ops;
102 sc->sc_adbdev = aaa->dev;
103 sc->sc_adbdev->cookie = sc;
104 sc->sc_adbdev->handler = adbbt_handler;
105 sc->sc_us = ADBTALK(sc->sc_adbdev->current_addr, 0);
106
107 sc->sc_msg_len = 0;
108
109 printf(" addr %d: button device\n", sc->sc_adbdev->current_addr);
110 }
111
112 static void
113 adbbt_handler(void *cookie, int len, uint8_t *data)
114 {
115 /* struct adbbt_softc *sc = cookie; */
116 uint8_t k, scancode;
117
118 #ifdef ADBBT_DEBUG
119 struct adbbt_softc *sc = cookie;
120 int i;
121 printf("%s: %02x - ", device_xname(sc->sc_dev), sc->sc_us);
122 for (i = 0; i < len; i++) {
123 printf(" %02x", data[i]);
124 }
125 printf("\n");
126 #endif
127 k = data[2];
128 scancode = ADBK_KEYVAL(k);
129 if ((scancode < 6) || (scancode > 0x0c))
130 return;
131
132 if (ADBK_PRESS(k)) {
133
134 switch (scancode) {
135 case BT_VOL_UP:
136 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_UP);
137 break;
138 case BT_VOL_DOWN:
139 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_DOWN);
140 break;
141 case BT_VOL_MUTE:
142 pmf_event_inject(NULL,
143 PMFE_AUDIO_VOLUME_TOGGLE);
144 break;
145 case BT_BRT_UP:
146 pmf_event_inject(NULL,
147 PMFE_DISPLAY_BRIGHTNESS_UP);
148 break;
149 case BT_BRT_DOWN:
150 pmf_event_inject(NULL,
151 PMFE_DISPLAY_BRIGHTNESS_DOWN);
152 break;
153 }
154 }
155 }
156