1 /* $NetBSD: abtn.c,v 1.3 2001/06/08 00:32:01 matt 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 struct cfattach abtn_ca = { 55 sizeof(struct abtn_softc), abtn_match, abtn_attach 56 }; 57 58 int 59 abtn_match(parent, cf, aux) 60 struct device *parent; 61 struct cfdata *cf; 62 void *aux; 63 { 64 struct adb_attach_args *aa = aux; 65 66 if (aa->origaddr == ADBADDR_MISC && 67 aa->handler_id == ABTN_HANDLER_ID) 68 return 1; 69 70 return 0; 71 } 72 73 void 74 abtn_attach(parent, self, aux) 75 struct device *parent, *self; 76 void *aux; 77 { 78 struct abtn_softc *sc = (struct abtn_softc *)self; 79 struct adb_attach_args *aa = aux; 80 ADBSetInfoBlock adbinfo; 81 int bright; 82 83 printf("brightness/volume button\n"); 84 85 bright = pm_read_nvram(NVRAM_BRIGHTNESS); 86 if (bright != 0) 87 pm_set_brightness(bright); 88 sc->brightness = bright; 89 90 sc->origaddr = aa->origaddr; 91 sc->adbaddr = aa->adbaddr; 92 sc->handler_id = aa->handler_id; 93 94 adbinfo.siServiceRtPtr = (Ptr)abtn_adbcomplete; 95 adbinfo.siDataAreaAddr = (caddr_t)sc; 96 97 SetADBInfo(&adbinfo, sc->adbaddr); 98 } 99 100 void 101 abtn_adbcomplete(buffer, data, adb_command) 102 caddr_t buffer, data; 103 int adb_command; 104 { 105 struct abtn_softc *sc = (struct abtn_softc *)data; 106 u_int cmd; 107 108 cmd = buffer[1]; 109 110 switch (cmd) { 111 case 0x0a: 112 sc->brightness -= 8; 113 if (sc->brightness < 8) 114 sc->brightness = 8; 115 pm_set_brightness(sc->brightness); 116 pm_write_nvram(NVRAM_BRIGHTNESS, sc->brightness); 117 break; 118 119 case 0x09: 120 sc->brightness += 8; 121 if (sc->brightness > 0x78) 122 sc->brightness = 0x78; 123 pm_set_brightness(sc->brightness); 124 pm_write_nvram(NVRAM_BRIGHTNESS, sc->brightness); 125 break; 126 } 127 } 128