xref: /netbsd/sys/arch/macppc/dev/abtn.c (revision c4a72b64)
1 /*	$NetBSD: abtn.c,v 1.5 2002/10/02 05:30:38 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 #include <macppc/dev/pm_direct.h>
35 
36 #define NVRAM_BRIGHTNESS 0x140e
37 #define ABTN_HANDLER_ID 31
38 
39 struct abtn_softc {
40 	struct device sc_dev;
41 
42 	int origaddr;		/* ADB device type */
43 	int adbaddr;		/* current ADB address */
44 	int handler_id;
45 
46 	int brightness;		/* backlight brightness */
47 	int volume;		/* speaker volume (not yet) */
48 };
49 
50 static int abtn_match __P((struct device *, struct cfdata *, void *));
51 static void abtn_attach __P((struct device *, struct device *, void *));
52 static void abtn_adbcomplete __P((caddr_t, caddr_t, int));
53 
54 CFATTACH_DECL(abtn, sizeof(struct abtn_softc),
55     abtn_match, abtn_attach, NULL, NULL);
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 	if (bright != 0)
86 		pm_set_brightness(bright);
87 	sc->brightness = bright;
88 
89 	sc->origaddr = aa->origaddr;
90 	sc->adbaddr = aa->adbaddr;
91 	sc->handler_id = aa->handler_id;
92 
93 	adbinfo.siServiceRtPtr = (Ptr)abtn_adbcomplete;
94 	adbinfo.siDataAreaAddr = (caddr_t)sc;
95 
96 	SetADBInfo(&adbinfo, sc->adbaddr);
97 }
98 
99 void
100 abtn_adbcomplete(buffer, data, adb_command)
101 	caddr_t buffer, data;
102 	int adb_command;
103 {
104 	struct abtn_softc *sc = (struct abtn_softc *)data;
105 	u_int cmd;
106 
107 	cmd = buffer[1];
108 
109 	switch (cmd) {
110 	case 0x0a:
111 		sc->brightness -= 8;
112 		if (sc->brightness < 8)
113 			sc->brightness = 8;
114 		pm_set_brightness(sc->brightness);
115 		pm_write_nvram(NVRAM_BRIGHTNESS, sc->brightness);
116 		break;
117 
118 	case 0x09:
119 		sc->brightness += 8;
120 		if (sc->brightness > 0x78)
121 			sc->brightness = 0x78;
122 		pm_set_brightness(sc->brightness);
123 		pm_write_nvram(NVRAM_BRIGHTNESS, sc->brightness);
124 		break;
125 	}
126 }
127