abtn.c revision 1.15 1 /* $NetBSD: abtn.c,v 1.15 2009/03/14 14:46:01 dsl Exp $ */
2
3 /*-
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
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: abtn.c,v 1.15 2009/03/14 14:46:01 dsl Exp $");
31
32 #include <sys/param.h>
33 #include <sys/device.h>
34 #include <sys/systm.h>
35
36 #include <macppc/dev/akbdvar.h>
37 #include <macppc/dev/adbvar.h>
38 #include <macppc/dev/pm_direct.h>
39
40 #define NVRAM_BRIGHTNESS 0x140e
41 #define ABTN_HANDLER_ID 31
42
43 #define BUTTON_LOUDER 0x06
44 #define BUTTON_SOFTER 0x07
45 #define BUTTON_MUTE 0x08
46 #define BUTTON_BRIGHTER 0x09
47 #define BUTTON_DIMMER 0x0a
48 #define BUTTON_EJECT 0x0b
49 #define BUTTON_DISPLAY 0x0c
50 #define BUTTON_KEYPAD 0x7f
51 #define BUTTON_DEPRESS 0x80
52
53 struct abtn_softc {
54 struct device sc_dev;
55
56 int origaddr; /* ADB device type */
57 int adbaddr; /* current ADB address */
58 int handler_id;
59
60 int brightness; /* backlight brightness */
61 int volume; /* speaker volume (not yet) */
62 };
63
64 static int abtn_match(struct device *, struct cfdata *, void *);
65 static void abtn_attach(struct device *, struct device *, void *);
66 static void abtn_adbcomplete(uint8_t *, uint8_t *, int);
67
68 CFATTACH_DECL(abtn, sizeof(struct abtn_softc),
69 abtn_match, abtn_attach, NULL, NULL);
70
71 int
72 abtn_match(parent, cf, aux)
73 struct device *parent;
74 struct cfdata *cf;
75 void *aux;
76 {
77 struct adb_attach_args *aa = aux;
78
79 if (aa->origaddr == ADBADDR_MISC &&
80 aa->handler_id == ABTN_HANDLER_ID)
81 return 1;
82
83 return 0;
84 }
85
86 void
87 abtn_attach(parent, self, aux)
88 struct device *parent, *self;
89 void *aux;
90 {
91 struct abtn_softc *sc = (struct abtn_softc *)self;
92 struct adb_attach_args *aa = aux;
93 ADBSetInfoBlock adbinfo;
94 int bright;
95
96 printf("buttons\n");
97
98 bright = pm_read_nvram(NVRAM_BRIGHTNESS);
99 if (bright != 0)
100 pm_set_brightness(bright);
101 sc->brightness = bright;
102
103 sc->origaddr = aa->origaddr;
104 sc->adbaddr = aa->adbaddr;
105 sc->handler_id = aa->handler_id;
106
107 adbinfo.siServiceRtPtr = (Ptr)abtn_adbcomplete;
108 adbinfo.siDataAreaAddr = (void *)sc;
109
110 SetADBInfo(&adbinfo, sc->adbaddr);
111 }
112
113 extern struct cfdriver akbd_cd;
114
115 void
116 abtn_adbcomplete(buffer, data, adb_command)
117 uint8_t *buffer, *data;
118 int adb_command;
119 {
120 struct abtn_softc *sc = (struct abtn_softc *)data;
121 u_int cmd;
122 #ifdef FORCE_FUNCTION_KEYS
123 int key = 0;
124 #endif
125
126 cmd = buffer[1];
127
128 #ifdef FORCE_FUNCTION_KEYS
129 switch (cmd & 0x7f) {
130 case 0x0a: /* f1 */
131 key = 122;
132 break;
133 case 0x09: /* f2 */
134 key = 120;
135 break;
136 case 0x08: /* f3 */
137 key = 99;
138 break;
139 case 0x07: /* f4 */
140 key = 118;
141 break;
142 case 0x06: /* f5 */
143 key = 96;
144 break;
145 case 0x7f: /* f6 */
146 key = 97;
147 break;
148 case 0x0b: /* f12 */
149 key = 111;
150 break;
151 }
152 if (key != 0) {
153 key |= cmd & 0x80;
154 kbd_passup(device_lookup_private(&akbd_cd, 0),key);
155 return;
156 }
157 #endif
158
159 if (cmd >= BUTTON_DEPRESS)
160 return;
161
162 switch (cmd) {
163 case BUTTON_DIMMER:
164 sc->brightness -= 8;
165 if (sc->brightness < 8)
166 sc->brightness = 8;
167 pm_set_brightness(sc->brightness);
168 pm_write_nvram(NVRAM_BRIGHTNESS, sc->brightness);
169 break;
170
171 case BUTTON_BRIGHTER:
172 sc->brightness += 8;
173 if (sc->brightness > 0x78)
174 sc->brightness = 0x78;
175 pm_set_brightness(sc->brightness);
176 pm_write_nvram(NVRAM_BRIGHTNESS, sc->brightness);
177 break;
178
179 case BUTTON_MUTE:
180 case BUTTON_SOFTER:
181 case BUTTON_LOUDER:
182 printf("%s: volume setting not implemented\n",
183 sc->sc_dev.dv_xname);
184 break;
185 case BUTTON_DISPLAY:
186 printf("%s: display selection not implemented\n",
187 sc->sc_dev.dv_xname);
188 break;
189 case BUTTON_EJECT:
190 printf("%s: eject not implemented\n",
191 sc->sc_dev.dv_xname);
192 break;
193
194 /* The keyboard gets wacky when in keypad mode. */
195 case BUTTON_KEYPAD:
196 break;
197
198 default:
199 printf("%s: unknown button 0x%x\n",
200 sc->sc_dev.dv_xname, cmd);
201 }
202 }
203