abtn.c revision 1.6 1 /* $NetBSD: abtn.c,v 1.6 2003/03/05 16:18:24 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 bright = pm_read_nvram(NVRAM_BRIGHTNESS);
93 if (bright != 0)
94 pm_set_brightness(bright);
95 sc->brightness = bright;
96
97 sc->origaddr = aa->origaddr;
98 sc->adbaddr = aa->adbaddr;
99 sc->handler_id = aa->handler_id;
100
101 adbinfo.siServiceRtPtr = (Ptr)abtn_adbcomplete;
102 adbinfo.siDataAreaAddr = (caddr_t)sc;
103
104 SetADBInfo(&adbinfo, sc->adbaddr);
105 }
106
107 void
108 abtn_adbcomplete(buffer, data, adb_command)
109 caddr_t buffer, data;
110 int adb_command;
111 {
112 struct abtn_softc *sc = (struct abtn_softc *)data;
113 u_int cmd;
114
115 cmd = buffer[1];
116
117 if (cmd >= BUTTON_DEPRESS)
118 return;
119
120 switch (cmd) {
121 case BUTTON_DIMMER:
122 sc->brightness -= 8;
123 if (sc->brightness < 8)
124 sc->brightness = 8;
125 pm_set_brightness(sc->brightness);
126 pm_write_nvram(NVRAM_BRIGHTNESS, sc->brightness);
127 break;
128
129 case BUTTON_BRIGHTER:
130 sc->brightness += 8;
131 if (sc->brightness > 0x78)
132 sc->brightness = 0x78;
133 pm_set_brightness(sc->brightness);
134 pm_write_nvram(NVRAM_BRIGHTNESS, sc->brightness);
135 break;
136
137 case BUTTON_MUTE:
138 case BUTTON_SOFTER:
139 case BUTTON_LOUDER:
140 printf("%s: volume setting not implemented\n",
141 sc->sc_dev.dv_xname);
142 break;
143 case BUTTON_DISPLAY:
144 printf("%s: display selection not implemented\n",
145 sc->sc_dev.dv_xname);
146 break;
147 case BUTTON_EJECT:
148 printf("%s: eject not implemented\n",
149 sc->sc_dev.dv_xname);
150 break;
151
152 /* The keyboard gets wacky when in keypad mode. */
153 case BUTTON_KEYPAD:
154 break;
155
156 default:
157 printf("%s: unknown button 0x%x\n",
158 sc->sc_dev.dv_xname, cmd);
159 }
160 }
161