Home | History | Annotate | Line # | Download | only in dev
abtn.c revision 1.6
      1  1.6    soren /*	$NetBSD: abtn.c,v 1.6 2003/03/05 16:18:24 soren Exp $	*/
      2  1.1   tsubai 
      3  1.1   tsubai /*-
      4  1.1   tsubai  * Copyright (C) 1999 Tsubai Masanari.  All rights reserved.
      5  1.1   tsubai  *
      6  1.1   tsubai  * Redistribution and use in source and binary forms, with or without
      7  1.1   tsubai  * modification, are permitted provided that the following conditions
      8  1.1   tsubai  * are met:
      9  1.1   tsubai  * 1. Redistributions of source code must retain the above copyright
     10  1.1   tsubai  *    notice, this list of conditions and the following disclaimer.
     11  1.1   tsubai  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1   tsubai  *    notice, this list of conditions and the following disclaimer in the
     13  1.1   tsubai  *    documentation and/or other materials provided with the distribution.
     14  1.1   tsubai  * 3. The name of the author may not be used to endorse or promote products
     15  1.1   tsubai  *    derived from this software without specific prior written permission.
     16  1.1   tsubai  *
     17  1.1   tsubai  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  1.1   tsubai  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  1.1   tsubai  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1   tsubai  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  1.1   tsubai  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  1.1   tsubai  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  1.1   tsubai  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  1.1   tsubai  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  1.1   tsubai  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  1.1   tsubai  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1   tsubai  */
     28  1.1   tsubai 
     29  1.1   tsubai #include <sys/param.h>
     30  1.1   tsubai #include <sys/device.h>
     31  1.1   tsubai #include <sys/systm.h>
     32  1.1   tsubai 
     33  1.1   tsubai #include <macppc/dev/adbvar.h>
     34  1.3     matt #include <macppc/dev/pm_direct.h>
     35  1.1   tsubai 
     36  1.1   tsubai #define NVRAM_BRIGHTNESS 0x140e
     37  1.1   tsubai #define ABTN_HANDLER_ID 31
     38  1.1   tsubai 
     39  1.6    soren #define BUTTON_LOUDER	0x06
     40  1.6    soren #define BUTTON_SOFTER	0x07
     41  1.6    soren #define BUTTON_MUTE	0x08
     42  1.6    soren #define BUTTON_BRIGHTER	0x09
     43  1.6    soren #define BUTTON_DIMMER	0x0a
     44  1.6    soren #define BUTTON_EJECT	0x0b
     45  1.6    soren #define BUTTON_DISPLAY	0x0c
     46  1.6    soren #define BUTTON_KEYPAD	0x7f
     47  1.6    soren #define BUTTON_DEPRESS	0x80
     48  1.6    soren 
     49  1.1   tsubai struct abtn_softc {
     50  1.1   tsubai 	struct device sc_dev;
     51  1.1   tsubai 
     52  1.1   tsubai 	int origaddr;		/* ADB device type */
     53  1.1   tsubai 	int adbaddr;		/* current ADB address */
     54  1.1   tsubai 	int handler_id;
     55  1.1   tsubai 
     56  1.1   tsubai 	int brightness;		/* backlight brightness */
     57  1.1   tsubai 	int volume;		/* speaker volume (not yet) */
     58  1.1   tsubai };
     59  1.1   tsubai 
     60  1.1   tsubai static int abtn_match __P((struct device *, struct cfdata *, void *));
     61  1.1   tsubai static void abtn_attach __P((struct device *, struct device *, void *));
     62  1.1   tsubai static void abtn_adbcomplete __P((caddr_t, caddr_t, int));
     63  1.1   tsubai 
     64  1.5  thorpej CFATTACH_DECL(abtn, sizeof(struct abtn_softc),
     65  1.5  thorpej     abtn_match, abtn_attach, NULL, NULL);
     66  1.1   tsubai 
     67  1.1   tsubai int
     68  1.1   tsubai abtn_match(parent, cf, aux)
     69  1.1   tsubai 	struct device *parent;
     70  1.1   tsubai 	struct cfdata *cf;
     71  1.1   tsubai 	void *aux;
     72  1.1   tsubai {
     73  1.1   tsubai 	struct adb_attach_args *aa = aux;
     74  1.1   tsubai 
     75  1.1   tsubai 	if (aa->origaddr == ADBADDR_MISC &&
     76  1.1   tsubai 	    aa->handler_id == ABTN_HANDLER_ID)
     77  1.1   tsubai 		return 1;
     78  1.1   tsubai 
     79  1.1   tsubai 	return 0;
     80  1.1   tsubai }
     81  1.1   tsubai 
     82  1.1   tsubai void
     83  1.1   tsubai abtn_attach(parent, self, aux)
     84  1.1   tsubai 	struct device *parent, *self;
     85  1.1   tsubai 	void *aux;
     86  1.1   tsubai {
     87  1.1   tsubai 	struct abtn_softc *sc = (struct abtn_softc *)self;
     88  1.1   tsubai 	struct adb_attach_args *aa = aux;
     89  1.1   tsubai 	ADBSetInfoBlock adbinfo;
     90  1.1   tsubai 	int bright;
     91  1.1   tsubai 
     92  1.1   tsubai 	bright = pm_read_nvram(NVRAM_BRIGHTNESS);
     93  1.2   tsubai 	if (bright != 0)
     94  1.2   tsubai 		pm_set_brightness(bright);
     95  1.1   tsubai 	sc->brightness = bright;
     96  1.1   tsubai 
     97  1.1   tsubai 	sc->origaddr = aa->origaddr;
     98  1.1   tsubai 	sc->adbaddr = aa->adbaddr;
     99  1.1   tsubai 	sc->handler_id = aa->handler_id;
    100  1.1   tsubai 
    101  1.1   tsubai 	adbinfo.siServiceRtPtr = (Ptr)abtn_adbcomplete;
    102  1.1   tsubai 	adbinfo.siDataAreaAddr = (caddr_t)sc;
    103  1.1   tsubai 
    104  1.1   tsubai 	SetADBInfo(&adbinfo, sc->adbaddr);
    105  1.1   tsubai }
    106  1.1   tsubai 
    107  1.2   tsubai void
    108  1.1   tsubai abtn_adbcomplete(buffer, data, adb_command)
    109  1.1   tsubai 	caddr_t buffer, data;
    110  1.1   tsubai 	int adb_command;
    111  1.1   tsubai {
    112  1.1   tsubai 	struct abtn_softc *sc = (struct abtn_softc *)data;
    113  1.1   tsubai 	u_int cmd;
    114  1.1   tsubai 
    115  1.1   tsubai 	cmd = buffer[1];
    116  1.1   tsubai 
    117  1.6    soren 	if (cmd >= BUTTON_DEPRESS)
    118  1.6    soren 		return;
    119  1.6    soren 
    120  1.1   tsubai 	switch (cmd) {
    121  1.6    soren 	case BUTTON_DIMMER:
    122  1.1   tsubai 		sc->brightness -= 8;
    123  1.1   tsubai 		if (sc->brightness < 8)
    124  1.1   tsubai 			sc->brightness = 8;
    125  1.1   tsubai 		pm_set_brightness(sc->brightness);
    126  1.1   tsubai 		pm_write_nvram(NVRAM_BRIGHTNESS, sc->brightness);
    127  1.1   tsubai 		break;
    128  1.1   tsubai 
    129  1.6    soren 	case BUTTON_BRIGHTER:
    130  1.1   tsubai 		sc->brightness += 8;
    131  1.1   tsubai 		if (sc->brightness > 0x78)
    132  1.1   tsubai 			sc->brightness = 0x78;
    133  1.1   tsubai 		pm_set_brightness(sc->brightness);
    134  1.1   tsubai 		pm_write_nvram(NVRAM_BRIGHTNESS, sc->brightness);
    135  1.1   tsubai 		break;
    136  1.6    soren 
    137  1.6    soren 	case BUTTON_MUTE:
    138  1.6    soren 	case BUTTON_SOFTER:
    139  1.6    soren 	case BUTTON_LOUDER:
    140  1.6    soren 		printf("%s: volume setting not implemented\n",
    141  1.6    soren 			sc->sc_dev.dv_xname);
    142  1.6    soren 		break;
    143  1.6    soren 	case BUTTON_DISPLAY:
    144  1.6    soren 		printf("%s: display selection not implemented\n",
    145  1.6    soren 			sc->sc_dev.dv_xname);
    146  1.6    soren 		break;
    147  1.6    soren 	case BUTTON_EJECT:
    148  1.6    soren 		printf("%s: eject not implemented\n",
    149  1.6    soren 			sc->sc_dev.dv_xname);
    150  1.6    soren 		break;
    151  1.6    soren 
    152  1.6    soren 	/* The keyboard gets wacky when in keypad mode. */
    153  1.6    soren 	case BUTTON_KEYPAD:
    154  1.6    soren 		break;
    155  1.6    soren 
    156  1.6    soren 	default:
    157  1.6    soren 		printf("%s: unknown button 0x%x\n",
    158  1.6    soren 			sc->sc_dev.dv_xname, cmd);
    159  1.1   tsubai 	}
    160  1.1   tsubai }
    161