1 /* $OpenBSD: abtn.c,v 1.2 2001/09/01 17:43:08 drahn Exp $ */ 2 /* $NetBSD: abtn.c,v 1.1 1999/07/12 17:48:26 tsubai Exp $ */ 3 4 /*- 5 * Copyright (C) 1999 Tsubai Masanari. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <sys/param.h> 31 #include <sys/device.h> 32 #include <sys/systm.h> 33 34 #include <macppc/dev/adbvar.h> 35 #include <macppc/dev/pm_direct.h> 36 37 #define NVRAM_BRIGHTNESS 0x140e 38 #define ABTN_HANDLER_ID 31 39 40 struct abtn_softc { 41 struct device sc_dev; 42 43 int origaddr; /* ADB device type */ 44 int adbaddr; /* current ADB address */ 45 int handler_id; 46 47 int brightness; /* backlight brightness */ 48 int volume; /* speaker volume (not yet) */ 49 }; 50 51 static int abtn_match __P((struct device *, void *, void *)); 52 static void abtn_attach __P((struct device *, struct device *, void *)); 53 static void abtn_adbcomplete __P((caddr_t, caddr_t, int)); 54 55 struct cfattach abtn_ca = { 56 sizeof(struct abtn_softc), abtn_match, abtn_attach 57 }; 58 struct cfdriver abtn_cd = { 59 NULL, "abtn", DV_DULL 60 }; 61 62 int 63 abtn_match(parent, cf, aux) 64 struct device *parent; 65 void *cf; 66 void *aux; 67 { 68 struct adb_attach_args *aa = aux; 69 70 if (aa->origaddr == ADBADDR_MISC && 71 aa->handler_id == ABTN_HANDLER_ID) 72 return 1; 73 74 return 0; 75 } 76 77 void 78 abtn_attach(parent, self, aux) 79 struct device *parent, *self; 80 void *aux; 81 { 82 struct abtn_softc *sc = (struct abtn_softc *)self; 83 struct adb_attach_args *aa = aux; 84 ADBSetInfoBlock adbinfo; 85 int bright; 86 87 printf("brightness/volume button\n"); 88 89 bright = pm_read_nvram(NVRAM_BRIGHTNESS); 90 pm_set_brightness(bright); 91 sc->brightness = bright; 92 93 sc->origaddr = aa->origaddr; 94 sc->adbaddr = aa->adbaddr; 95 sc->handler_id = aa->handler_id; 96 97 adbinfo.siServiceRtPtr = (Ptr)abtn_adbcomplete; 98 adbinfo.siDataAreaAddr = (caddr_t)sc; 99 100 SetADBInfo(&adbinfo, sc->adbaddr); 101 } 102 103 void 104 abtn_adbcomplete(buffer, data, adb_command) 105 caddr_t buffer, data; 106 int adb_command; 107 { 108 struct abtn_softc *sc = (struct abtn_softc *)data; 109 u_int cmd; 110 111 cmd = buffer[1]; 112 113 switch (cmd) { 114 case 0x0a: 115 sc->brightness -= 8; 116 if (sc->brightness < 8) 117 sc->brightness = 8; 118 pm_set_brightness(sc->brightness); 119 pm_write_nvram(NVRAM_BRIGHTNESS, sc->brightness); 120 break; 121 122 case 0x09: 123 sc->brightness += 8; 124 if (sc->brightness > 0x78) 125 sc->brightness = 0x78; 126 pm_set_brightness(sc->brightness); 127 pm_write_nvram(NVRAM_BRIGHTNESS, sc->brightness); 128 break; 129 } 130 } 131