abtn.c revision 1.16 1 /* $NetBSD: abtn.c,v 1.16 2009/03/14 15:36:09 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.16 2009/03/14 15:36:09 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(struct device *parent, struct cfdata *cf, void *aux)
73 {
74 struct adb_attach_args *aa = aux;
75
76 if (aa->origaddr == ADBADDR_MISC &&
77 aa->handler_id == ABTN_HANDLER_ID)
78 return 1;
79
80 return 0;
81 }
82
83 void
84 abtn_attach(parent, self, aux)
85 struct device *parent, *self;
86 void *aux;
87 {
88 struct abtn_softc *sc = (struct abtn_softc *)self;
89 struct adb_attach_args *aa = aux;
90 ADBSetInfoBlock adbinfo;
91 int bright;
92
93 printf("buttons\n");
94
95 bright = pm_read_nvram(NVRAM_BRIGHTNESS);
96 if (bright != 0)
97 pm_set_brightness(bright);
98 sc->brightness = bright;
99
100 sc->origaddr = aa->origaddr;
101 sc->adbaddr = aa->adbaddr;
102 sc->handler_id = aa->handler_id;
103
104 adbinfo.siServiceRtPtr = (Ptr)abtn_adbcomplete;
105 adbinfo.siDataAreaAddr = (void *)sc;
106
107 SetADBInfo(&adbinfo, sc->adbaddr);
108 }
109
110 extern struct cfdriver akbd_cd;
111
112 void
113 abtn_adbcomplete(buffer, data, adb_command)
114 uint8_t *buffer, *data;
115 int adb_command;
116 {
117 struct abtn_softc *sc = (struct abtn_softc *)data;
118 u_int cmd;
119 #ifdef FORCE_FUNCTION_KEYS
120 int key = 0;
121 #endif
122
123 cmd = buffer[1];
124
125 #ifdef FORCE_FUNCTION_KEYS
126 switch (cmd & 0x7f) {
127 case 0x0a: /* f1 */
128 key = 122;
129 break;
130 case 0x09: /* f2 */
131 key = 120;
132 break;
133 case 0x08: /* f3 */
134 key = 99;
135 break;
136 case 0x07: /* f4 */
137 key = 118;
138 break;
139 case 0x06: /* f5 */
140 key = 96;
141 break;
142 case 0x7f: /* f6 */
143 key = 97;
144 break;
145 case 0x0b: /* f12 */
146 key = 111;
147 break;
148 }
149 if (key != 0) {
150 key |= cmd & 0x80;
151 kbd_passup(device_lookup_private(&akbd_cd, 0),key);
152 return;
153 }
154 #endif
155
156 if (cmd >= BUTTON_DEPRESS)
157 return;
158
159 switch (cmd) {
160 case BUTTON_DIMMER:
161 sc->brightness -= 8;
162 if (sc->brightness < 8)
163 sc->brightness = 8;
164 pm_set_brightness(sc->brightness);
165 pm_write_nvram(NVRAM_BRIGHTNESS, sc->brightness);
166 break;
167
168 case BUTTON_BRIGHTER:
169 sc->brightness += 8;
170 if (sc->brightness > 0x78)
171 sc->brightness = 0x78;
172 pm_set_brightness(sc->brightness);
173 pm_write_nvram(NVRAM_BRIGHTNESS, sc->brightness);
174 break;
175
176 case BUTTON_MUTE:
177 case BUTTON_SOFTER:
178 case BUTTON_LOUDER:
179 printf("%s: volume setting not implemented\n",
180 sc->sc_dev.dv_xname);
181 break;
182 case BUTTON_DISPLAY:
183 printf("%s: display selection not implemented\n",
184 sc->sc_dev.dv_xname);
185 break;
186 case BUTTON_EJECT:
187 printf("%s: eject not implemented\n",
188 sc->sc_dev.dv_xname);
189 break;
190
191 /* The keyboard gets wacky when in keypad mode. */
192 case BUTTON_KEYPAD:
193 break;
194
195 default:
196 printf("%s: unknown button 0x%x\n",
197 sc->sc_dev.dv_xname, cmd);
198 }
199 }
200