abtn.c revision 1.18.12.2 1 1.18.12.2 jdolecek /* $NetBSD: abtn.c,v 1.18.12.2 2017/12/03 11:36:24 jdolecek Exp $ */
2 1.1 tsubai
3 1.1 tsubai /*-
4 1.1 tsubai * Copyright (C) 1999 Tsubai Masanari. All rights reserved.
5 1.1 tsubai *
6 1.1 tsubai * Redistribution and use in source and binary forms, with or without
7 1.1 tsubai * modification, are permitted provided that the following conditions
8 1.1 tsubai * are met:
9 1.1 tsubai * 1. Redistributions of source code must retain the above copyright
10 1.1 tsubai * notice, this list of conditions and the following disclaimer.
11 1.1 tsubai * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 tsubai * notice, this list of conditions and the following disclaimer in the
13 1.1 tsubai * documentation and/or other materials provided with the distribution.
14 1.1 tsubai * 3. The name of the author may not be used to endorse or promote products
15 1.1 tsubai * derived from this software without specific prior written permission.
16 1.1 tsubai *
17 1.1 tsubai * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 1.1 tsubai * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 1.1 tsubai * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.1 tsubai * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 1.1 tsubai * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 1.1 tsubai * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 1.1 tsubai * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 1.1 tsubai * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 1.1 tsubai * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 1.1 tsubai * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.1 tsubai */
28 1.8 lukem
29 1.8 lukem #include <sys/cdefs.h>
30 1.18.12.2 jdolecek __KERNEL_RCSID(0, "$NetBSD: abtn.c,v 1.18.12.2 2017/12/03 11:36:24 jdolecek Exp $");
31 1.1 tsubai
32 1.1 tsubai #include <sys/param.h>
33 1.1 tsubai #include <sys/device.h>
34 1.1 tsubai #include <sys/systm.h>
35 1.1 tsubai
36 1.9 nathanw #include <macppc/dev/akbdvar.h>
37 1.1 tsubai #include <macppc/dev/adbvar.h>
38 1.3 matt #include <macppc/dev/pm_direct.h>
39 1.1 tsubai
40 1.1 tsubai #define NVRAM_BRIGHTNESS 0x140e
41 1.1 tsubai #define ABTN_HANDLER_ID 31
42 1.1 tsubai
43 1.6 soren #define BUTTON_LOUDER 0x06
44 1.6 soren #define BUTTON_SOFTER 0x07
45 1.6 soren #define BUTTON_MUTE 0x08
46 1.6 soren #define BUTTON_BRIGHTER 0x09
47 1.6 soren #define BUTTON_DIMMER 0x0a
48 1.6 soren #define BUTTON_EJECT 0x0b
49 1.6 soren #define BUTTON_DISPLAY 0x0c
50 1.6 soren #define BUTTON_KEYPAD 0x7f
51 1.6 soren #define BUTTON_DEPRESS 0x80
52 1.6 soren
53 1.1 tsubai struct abtn_softc {
54 1.18.12.1 tls device_t sc_dev;
55 1.1 tsubai
56 1.1 tsubai int origaddr; /* ADB device type */
57 1.1 tsubai int adbaddr; /* current ADB address */
58 1.1 tsubai int handler_id;
59 1.1 tsubai
60 1.1 tsubai int brightness; /* backlight brightness */
61 1.1 tsubai int volume; /* speaker volume (not yet) */
62 1.1 tsubai };
63 1.1 tsubai
64 1.18 matt static int abtn_match(device_t, cfdata_t, void *);
65 1.18 matt static void abtn_attach(device_t, device_t, void *);
66 1.15 dsl static void abtn_adbcomplete(uint8_t *, uint8_t *, int);
67 1.1 tsubai
68 1.18.12.1 tls CFATTACH_DECL_NEW(abtn, sizeof(struct abtn_softc),
69 1.5 thorpej abtn_match, abtn_attach, NULL, NULL);
70 1.1 tsubai
71 1.1 tsubai int
72 1.18 matt abtn_match(device_t parent, cfdata_t cf, void *aux)
73 1.1 tsubai {
74 1.1 tsubai struct adb_attach_args *aa = aux;
75 1.1 tsubai
76 1.1 tsubai if (aa->origaddr == ADBADDR_MISC &&
77 1.1 tsubai aa->handler_id == ABTN_HANDLER_ID)
78 1.1 tsubai return 1;
79 1.1 tsubai
80 1.1 tsubai return 0;
81 1.1 tsubai }
82 1.1 tsubai
83 1.1 tsubai void
84 1.18 matt abtn_attach(device_t parent, device_t self, void *aux)
85 1.1 tsubai {
86 1.18 matt struct abtn_softc *sc = device_private(self);
87 1.1 tsubai struct adb_attach_args *aa = aux;
88 1.1 tsubai ADBSetInfoBlock adbinfo;
89 1.1 tsubai int bright;
90 1.7 soren
91 1.18.12.1 tls sc->sc_dev = self;
92 1.18.12.1 tls
93 1.7 soren printf("buttons\n");
94 1.1 tsubai
95 1.1 tsubai bright = pm_read_nvram(NVRAM_BRIGHTNESS);
96 1.2 tsubai if (bright != 0)
97 1.2 tsubai pm_set_brightness(bright);
98 1.1 tsubai sc->brightness = bright;
99 1.1 tsubai
100 1.1 tsubai sc->origaddr = aa->origaddr;
101 1.1 tsubai sc->adbaddr = aa->adbaddr;
102 1.1 tsubai sc->handler_id = aa->handler_id;
103 1.1 tsubai
104 1.1 tsubai adbinfo.siServiceRtPtr = (Ptr)abtn_adbcomplete;
105 1.12 christos adbinfo.siDataAreaAddr = (void *)sc;
106 1.1 tsubai
107 1.1 tsubai SetADBInfo(&adbinfo, sc->adbaddr);
108 1.1 tsubai }
109 1.1 tsubai
110 1.9 nathanw extern struct cfdriver akbd_cd;
111 1.9 nathanw
112 1.2 tsubai void
113 1.17 dsl abtn_adbcomplete(uint8_t *buffer, uint8_t *data, int adb_command)
114 1.1 tsubai {
115 1.1 tsubai struct abtn_softc *sc = (struct abtn_softc *)data;
116 1.1 tsubai u_int cmd;
117 1.9 nathanw #ifdef FORCE_FUNCTION_KEYS
118 1.9 nathanw int key = 0;
119 1.9 nathanw #endif
120 1.1 tsubai
121 1.1 tsubai cmd = buffer[1];
122 1.9 nathanw
123 1.9 nathanw #ifdef FORCE_FUNCTION_KEYS
124 1.9 nathanw switch (cmd & 0x7f) {
125 1.10 nathanw case 0x0a: /* f1 */
126 1.9 nathanw key = 122;
127 1.9 nathanw break;
128 1.10 nathanw case 0x09: /* f2 */
129 1.9 nathanw key = 120;
130 1.9 nathanw break;
131 1.10 nathanw case 0x08: /* f3 */
132 1.9 nathanw key = 99;
133 1.9 nathanw break;
134 1.10 nathanw case 0x07: /* f4 */
135 1.9 nathanw key = 118;
136 1.9 nathanw break;
137 1.10 nathanw case 0x06: /* f5 */
138 1.9 nathanw key = 96;
139 1.9 nathanw break;
140 1.9 nathanw case 0x7f: /* f6 */
141 1.9 nathanw key = 97;
142 1.9 nathanw break;
143 1.10 nathanw case 0x0b: /* f12 */
144 1.9 nathanw key = 111;
145 1.9 nathanw break;
146 1.9 nathanw }
147 1.9 nathanw if (key != 0) {
148 1.9 nathanw key |= cmd & 0x80;
149 1.14 cegger kbd_passup(device_lookup_private(&akbd_cd, 0),key);
150 1.9 nathanw return;
151 1.9 nathanw }
152 1.9 nathanw #endif
153 1.1 tsubai
154 1.6 soren if (cmd >= BUTTON_DEPRESS)
155 1.6 soren return;
156 1.6 soren
157 1.1 tsubai switch (cmd) {
158 1.6 soren case BUTTON_DIMMER:
159 1.1 tsubai sc->brightness -= 8;
160 1.1 tsubai if (sc->brightness < 8)
161 1.1 tsubai sc->brightness = 8;
162 1.1 tsubai pm_set_brightness(sc->brightness);
163 1.1 tsubai pm_write_nvram(NVRAM_BRIGHTNESS, sc->brightness);
164 1.1 tsubai break;
165 1.1 tsubai
166 1.6 soren case BUTTON_BRIGHTER:
167 1.1 tsubai sc->brightness += 8;
168 1.1 tsubai if (sc->brightness > 0x78)
169 1.1 tsubai sc->brightness = 0x78;
170 1.1 tsubai pm_set_brightness(sc->brightness);
171 1.1 tsubai pm_write_nvram(NVRAM_BRIGHTNESS, sc->brightness);
172 1.1 tsubai break;
173 1.6 soren
174 1.6 soren case BUTTON_MUTE:
175 1.6 soren case BUTTON_SOFTER:
176 1.6 soren case BUTTON_LOUDER:
177 1.6 soren printf("%s: volume setting not implemented\n",
178 1.18.12.1 tls device_xname(sc->sc_dev));
179 1.6 soren break;
180 1.6 soren case BUTTON_DISPLAY:
181 1.6 soren printf("%s: display selection not implemented\n",
182 1.18.12.1 tls device_xname(sc->sc_dev));
183 1.6 soren break;
184 1.6 soren case BUTTON_EJECT:
185 1.6 soren printf("%s: eject not implemented\n",
186 1.18.12.1 tls device_xname(sc->sc_dev));
187 1.6 soren break;
188 1.6 soren
189 1.6 soren /* The keyboard gets wacky when in keypad mode. */
190 1.6 soren case BUTTON_KEYPAD:
191 1.6 soren break;
192 1.6 soren
193 1.6 soren default:
194 1.6 soren printf("%s: unknown button 0x%x\n",
195 1.18.12.2 jdolecek device_xname(sc->sc_dev), cmd);
196 1.1 tsubai }
197 1.1 tsubai }
198