abtn.c revision 1.1.2.2 1 /* $NetBSD: abtn.c,v 1.1.2.2 1999/08/02 19:55:12 thorpej 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
35 #define NVRAM_BRIGHTNESS 0x140e
36 #define ABTN_HANDLER_ID 31
37
38 struct abtn_softc {
39 struct device sc_dev;
40
41 int origaddr; /* ADB device type */
42 int adbaddr; /* current ADB address */
43 int handler_id;
44
45 int brightness; /* backlight brightness */
46 int volume; /* speaker volume (not yet) */
47 };
48
49 static int abtn_match __P((struct device *, struct cfdata *, void *));
50 static void abtn_attach __P((struct device *, struct device *, void *));
51 static void abtn_adbcomplete __P((caddr_t, caddr_t, int));
52
53 struct cfattach abtn_ca = {
54 sizeof(struct abtn_softc), abtn_match, abtn_attach
55 };
56
57 int
58 abtn_match(parent, cf, aux)
59 struct device *parent;
60 struct cfdata *cf;
61 void *aux;
62 {
63 struct adb_attach_args *aa = aux;
64
65 if (aa->origaddr == ADBADDR_MISC &&
66 aa->handler_id == ABTN_HANDLER_ID)
67 return 1;
68
69 return 0;
70 }
71
72 void
73 abtn_attach(parent, self, aux)
74 struct device *parent, *self;
75 void *aux;
76 {
77 struct abtn_softc *sc = (struct abtn_softc *)self;
78 struct adb_attach_args *aa = aux;
79 ADBSetInfoBlock adbinfo;
80 int bright;
81
82 printf("brightness/volume button\n");
83
84 bright = pm_read_nvram(NVRAM_BRIGHTNESS);
85 pm_set_brightness(bright);
86 sc->brightness = bright;
87
88 sc->origaddr = aa->origaddr;
89 sc->adbaddr = aa->adbaddr;
90 sc->handler_id = aa->handler_id;
91
92 adbinfo.siServiceRtPtr = (Ptr)abtn_adbcomplete;
93 adbinfo.siDataAreaAddr = (caddr_t)sc;
94
95 SetADBInfo(&adbinfo, sc->adbaddr);
96 }
97
98 void
99 abtn_adbcomplete(buffer, data, adb_command)
100 caddr_t buffer, data;
101 int adb_command;
102 {
103 struct abtn_softc *sc = (struct abtn_softc *)data;
104 u_int cmd;
105
106 cmd = buffer[1];
107
108 switch (cmd) {
109 case 0x0a:
110 sc->brightness -= 8;
111 if (sc->brightness < 8)
112 sc->brightness = 8;
113 pm_set_brightness(sc->brightness);
114 pm_write_nvram(NVRAM_BRIGHTNESS, sc->brightness);
115 break;
116
117 case 0x09:
118 sc->brightness += 8;
119 if (sc->brightness > 0x78)
120 sc->brightness = 0x78;
121 pm_set_brightness(sc->brightness);
122 pm_write_nvram(NVRAM_BRIGHTNESS, sc->brightness);
123 break;
124 }
125 }
126