xref: /openbsd/sys/arch/macppc/dev/abtn.c (revision 404b540a)
1 /*	$OpenBSD: abtn.c,v 1.12 2009/01/10 18:00:59 robert Exp $	*/
2 /*	$NetBSD: abtn.c,v 1.1 1999/07/12 17:48:26 tsubai Exp $	*/
3 
4 /*-
5  * Copyright (c) 2002, Miodrag Vallat.
6  * Copyright (C) 1999 Tsubai Masanari.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/param.h>
32 #include <sys/device.h>
33 #include <sys/systm.h>
34 #include <sys/workq.h>
35 
36 #include <machine/bus.h>
37 
38 #include <dev/ofw/openfirm.h>
39 #include <macppc/macppc/ofw_machdep.h>
40 
41 #include <dev/adb/adb.h>
42 
43 #include "audio.h"
44 #include "cd.h"
45 #include "wskbd.h"
46 
47 #define ABTN_HANDLER_ID 31
48 
49 struct abtn_softc {
50 	struct device sc_dev;
51 
52 	int origaddr;		/* ADB device type */
53 	int adbaddr;		/* current ADB address */
54 	int handler_id;
55 };
56 
57 int abtn_match(struct device *, void *, void *);
58 void abtn_attach(struct device *, struct device *, void *);
59 void abtn_adbcomplete(caddr_t, caddr_t, int);
60 
61 #if NWSKBD > 0
62 extern int cd_eject(void);
63 #if NAUDIO > 0
64 extern int wskbd_set_mixervolume(long dir);
65 #endif
66 #endif
67 
68 struct cfattach abtn_ca = {
69 	sizeof(struct abtn_softc), abtn_match, abtn_attach
70 };
71 struct cfdriver abtn_cd = {
72 	NULL, "abtn", DV_DULL
73 };
74 
75 int
76 abtn_match(struct device *parent, void *cf, void *aux)
77 {
78 	struct adb_attach_args *aa = aux;
79 
80 	if (aa->origaddr == ADBADDR_MISC &&
81 	    aa->handler_id == ABTN_HANDLER_ID)
82 		return 1;
83 
84 	return 0;
85 }
86 
87 void
88 abtn_attach(struct device *parent, struct device *self, void *aux)
89 {
90 	struct abtn_softc *sc = (struct abtn_softc *)self;
91 	struct adb_attach_args *aa = aux;
92 	ADBSetInfoBlock adbinfo;
93 
94 	printf(": brightness/volume/eject buttons\n");
95 
96 	sc->origaddr = aa->origaddr;
97 	sc->adbaddr = aa->adbaddr;
98 	sc->handler_id = aa->handler_id;
99 
100 	adbinfo.siServiceRtPtr = (Ptr)abtn_adbcomplete;
101 	adbinfo.siDataAreaAddr = (caddr_t)sc;
102 
103 	set_adb_info(&adbinfo, sc->adbaddr);
104 }
105 
106 void
107 abtn_adbcomplete(caddr_t buffer, caddr_t data, int adb_command)
108 {
109 	u_int cmd, brightness;
110 
111 	cmd = buffer[1];
112 
113 	switch (cmd) {
114 	case 0x0a:	/* decrease brightness */
115 		brightness = cons_brightness;
116 		if (brightness == MAX_BRIGHTNESS)
117 			brightness++;		/* get round values */
118 		brightness -= STEP_BRIGHTNESS;
119 		of_setbrightness(brightness);
120 		break;
121 
122 	case 0x09:	/* increase brightness */
123 		brightness = cons_brightness + STEP_BRIGHTNESS;
124 		of_setbrightness(brightness);
125 		break;
126 
127 #if NAUDIO > 0 && NWSKBD > 0
128 	case 0x08:	/* mute */
129 	case 0x01:	/* mute, AV hardware */
130 		workq_add_task(NULL, 0, (workq_fn)wskbd_set_mixervolume,
131 		    (void *)(long)0, NULL);
132 		break;
133 	case 0x07:	/* decrease volume */
134 	case 0x02:	/* decrease volume, AV hardware */
135 		workq_add_task(NULL, 0, (workq_fn)wskbd_set_mixervolume,
136 		    (void *)(long)-1, NULL);
137 		break;
138 	case 0x06:	/* increase volume */
139 	case 0x03:	/* increase volume, AV hardware */
140 		workq_add_task(NULL, 0, (workq_fn)wskbd_set_mixervolume,
141 		    (void *)(long)1, NULL);
142 		break;
143 #endif
144 	case 0x0c:	/* mirror display key */
145 		/* Need callback to do something with this */
146 		break;
147 #if NWSKBD > 0 && NCD > 0
148 	case 0x0b:	/* eject tray */
149 		workq_add_task(NULL, 0, (workq_fn)cd_eject, NULL, NULL);
150 		break;
151 #endif
152 	case 0x7f:	/* numlock */
153 		/* Need callback to do something with this */
154 		break;
155 
156 	default:
157 #ifdef DEBUG
158 		if ((cmd & ~0x7f) == 0)
159 			printf("unknown ADB button 0x%x\n", cmd);
160 #endif
161 		break;
162 	}
163 }
164