Home | History | Annotate | Line # | Download | only in dev
abtn.c revision 1.8
      1 /*	$NetBSD: abtn.c,v 1.8 2003/07/15 02:43:26 lukem 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/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: abtn.c,v 1.8 2003/07/15 02:43:26 lukem Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/device.h>
     34 #include <sys/systm.h>
     35 
     36 #include <macppc/dev/adbvar.h>
     37 #include <macppc/dev/pm_direct.h>
     38 
     39 #define NVRAM_BRIGHTNESS 0x140e
     40 #define ABTN_HANDLER_ID 31
     41 
     42 #define BUTTON_LOUDER	0x06
     43 #define BUTTON_SOFTER	0x07
     44 #define BUTTON_MUTE	0x08
     45 #define BUTTON_BRIGHTER	0x09
     46 #define BUTTON_DIMMER	0x0a
     47 #define BUTTON_EJECT	0x0b
     48 #define BUTTON_DISPLAY	0x0c
     49 #define BUTTON_KEYPAD	0x7f
     50 #define BUTTON_DEPRESS	0x80
     51 
     52 struct abtn_softc {
     53 	struct device sc_dev;
     54 
     55 	int origaddr;		/* ADB device type */
     56 	int adbaddr;		/* current ADB address */
     57 	int handler_id;
     58 
     59 	int brightness;		/* backlight brightness */
     60 	int volume;		/* speaker volume (not yet) */
     61 };
     62 
     63 static int abtn_match __P((struct device *, struct cfdata *, void *));
     64 static void abtn_attach __P((struct device *, struct device *, void *));
     65 static void abtn_adbcomplete __P((caddr_t, caddr_t, int));
     66 
     67 CFATTACH_DECL(abtn, sizeof(struct abtn_softc),
     68     abtn_match, abtn_attach, NULL, NULL);
     69 
     70 int
     71 abtn_match(parent, cf, aux)
     72 	struct device *parent;
     73 	struct cfdata *cf;
     74 	void *aux;
     75 {
     76 	struct adb_attach_args *aa = aux;
     77 
     78 	if (aa->origaddr == ADBADDR_MISC &&
     79 	    aa->handler_id == ABTN_HANDLER_ID)
     80 		return 1;
     81 
     82 	return 0;
     83 }
     84 
     85 void
     86 abtn_attach(parent, self, aux)
     87 	struct device *parent, *self;
     88 	void *aux;
     89 {
     90 	struct abtn_softc *sc = (struct abtn_softc *)self;
     91 	struct adb_attach_args *aa = aux;
     92 	ADBSetInfoBlock adbinfo;
     93 	int bright;
     94 
     95 	printf("buttons\n");
     96 
     97 	bright = pm_read_nvram(NVRAM_BRIGHTNESS);
     98 	if (bright != 0)
     99 		pm_set_brightness(bright);
    100 	sc->brightness = bright;
    101 
    102 	sc->origaddr = aa->origaddr;
    103 	sc->adbaddr = aa->adbaddr;
    104 	sc->handler_id = aa->handler_id;
    105 
    106 	adbinfo.siServiceRtPtr = (Ptr)abtn_adbcomplete;
    107 	adbinfo.siDataAreaAddr = (caddr_t)sc;
    108 
    109 	SetADBInfo(&adbinfo, sc->adbaddr);
    110 }
    111 
    112 void
    113 abtn_adbcomplete(buffer, data, adb_command)
    114 	caddr_t buffer, data;
    115 	int adb_command;
    116 {
    117 	struct abtn_softc *sc = (struct abtn_softc *)data;
    118 	u_int cmd;
    119 
    120 	cmd = buffer[1];
    121 
    122 	if (cmd >= BUTTON_DEPRESS)
    123 		return;
    124 
    125 	switch (cmd) {
    126 	case BUTTON_DIMMER:
    127 		sc->brightness -= 8;
    128 		if (sc->brightness < 8)
    129 			sc->brightness = 8;
    130 		pm_set_brightness(sc->brightness);
    131 		pm_write_nvram(NVRAM_BRIGHTNESS, sc->brightness);
    132 		break;
    133 
    134 	case BUTTON_BRIGHTER:
    135 		sc->brightness += 8;
    136 		if (sc->brightness > 0x78)
    137 			sc->brightness = 0x78;
    138 		pm_set_brightness(sc->brightness);
    139 		pm_write_nvram(NVRAM_BRIGHTNESS, sc->brightness);
    140 		break;
    141 
    142 	case BUTTON_MUTE:
    143 	case BUTTON_SOFTER:
    144 	case BUTTON_LOUDER:
    145 		printf("%s: volume setting not implemented\n",
    146 			sc->sc_dev.dv_xname);
    147 		break;
    148 	case BUTTON_DISPLAY:
    149 		printf("%s: display selection not implemented\n",
    150 			sc->sc_dev.dv_xname);
    151 		break;
    152 	case BUTTON_EJECT:
    153 		printf("%s: eject not implemented\n",
    154 			sc->sc_dev.dv_xname);
    155 		break;
    156 
    157 	/* The keyboard gets wacky when in keypad mode. */
    158 	case BUTTON_KEYPAD:
    159 		break;
    160 
    161 	default:
    162 		printf("%s: unknown button 0x%x\n",
    163 			sc->sc_dev.dv_xname, cmd);
    164 	}
    165 }
    166