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