abtn.c revision 1.7 1 /* $NetBSD: abtn.c,v 1.7 2003/03/05 16:52:16 soren 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/param.h>
30 #include <sys/device.h>
31 #include <sys/systm.h>
32
33 #include <macppc/dev/adbvar.h>
34 #include <macppc/dev/pm_direct.h>
35
36 #define NVRAM_BRIGHTNESS 0x140e
37 #define ABTN_HANDLER_ID 31
38
39 #define BUTTON_LOUDER 0x06
40 #define BUTTON_SOFTER 0x07
41 #define BUTTON_MUTE 0x08
42 #define BUTTON_BRIGHTER 0x09
43 #define BUTTON_DIMMER 0x0a
44 #define BUTTON_EJECT 0x0b
45 #define BUTTON_DISPLAY 0x0c
46 #define BUTTON_KEYPAD 0x7f
47 #define BUTTON_DEPRESS 0x80
48
49 struct abtn_softc {
50 struct device sc_dev;
51
52 int origaddr; /* ADB device type */
53 int adbaddr; /* current ADB address */
54 int handler_id;
55
56 int brightness; /* backlight brightness */
57 int volume; /* speaker volume (not yet) */
58 };
59
60 static int abtn_match __P((struct device *, struct cfdata *, void *));
61 static void abtn_attach __P((struct device *, struct device *, void *));
62 static void abtn_adbcomplete __P((caddr_t, caddr_t, int));
63
64 CFATTACH_DECL(abtn, sizeof(struct abtn_softc),
65 abtn_match, abtn_attach, NULL, NULL);
66
67 int
68 abtn_match(parent, cf, aux)
69 struct device *parent;
70 struct cfdata *cf;
71 void *aux;
72 {
73 struct adb_attach_args *aa = aux;
74
75 if (aa->origaddr == ADBADDR_MISC &&
76 aa->handler_id == ABTN_HANDLER_ID)
77 return 1;
78
79 return 0;
80 }
81
82 void
83 abtn_attach(parent, self, aux)
84 struct device *parent, *self;
85 void *aux;
86 {
87 struct abtn_softc *sc = (struct abtn_softc *)self;
88 struct adb_attach_args *aa = aux;
89 ADBSetInfoBlock adbinfo;
90 int bright;
91
92 printf("buttons\n");
93
94 bright = pm_read_nvram(NVRAM_BRIGHTNESS);
95 if (bright != 0)
96 pm_set_brightness(bright);
97 sc->brightness = bright;
98
99 sc->origaddr = aa->origaddr;
100 sc->adbaddr = aa->adbaddr;
101 sc->handler_id = aa->handler_id;
102
103 adbinfo.siServiceRtPtr = (Ptr)abtn_adbcomplete;
104 adbinfo.siDataAreaAddr = (caddr_t)sc;
105
106 SetADBInfo(&adbinfo, sc->adbaddr);
107 }
108
109 void
110 abtn_adbcomplete(buffer, data, adb_command)
111 caddr_t buffer, data;
112 int adb_command;
113 {
114 struct abtn_softc *sc = (struct abtn_softc *)data;
115 u_int cmd;
116
117 cmd = buffer[1];
118
119 if (cmd >= BUTTON_DEPRESS)
120 return;
121
122 switch (cmd) {
123 case BUTTON_DIMMER:
124 sc->brightness -= 8;
125 if (sc->brightness < 8)
126 sc->brightness = 8;
127 pm_set_brightness(sc->brightness);
128 pm_write_nvram(NVRAM_BRIGHTNESS, sc->brightness);
129 break;
130
131 case BUTTON_BRIGHTER:
132 sc->brightness += 8;
133 if (sc->brightness > 0x78)
134 sc->brightness = 0x78;
135 pm_set_brightness(sc->brightness);
136 pm_write_nvram(NVRAM_BRIGHTNESS, sc->brightness);
137 break;
138
139 case BUTTON_MUTE:
140 case BUTTON_SOFTER:
141 case BUTTON_LOUDER:
142 printf("%s: volume setting not implemented\n",
143 sc->sc_dev.dv_xname);
144 break;
145 case BUTTON_DISPLAY:
146 printf("%s: display selection not implemented\n",
147 sc->sc_dev.dv_xname);
148 break;
149 case BUTTON_EJECT:
150 printf("%s: eject not implemented\n",
151 sc->sc_dev.dv_xname);
152 break;
153
154 /* The keyboard gets wacky when in keypad mode. */
155 case BUTTON_KEYPAD:
156 break;
157
158 default:
159 printf("%s: unknown button 0x%x\n",
160 sc->sc_dev.dv_xname, cmd);
161 }
162 }
163