xref: /openbsd/sys/arch/macppc/dev/abtn.c (revision 89ed722c)
1 /*	$OpenBSD: abtn.c,v 1.18 2022/03/13 12:33:01 mpi 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/task.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, long);
65 #endif
66 #endif
67 
68 const 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 struct task eject_task =
76     TASK_INITIALIZER((void (*)(void *))cd_eject, NULL);
77 
78 int
abtn_match(struct device * parent,void * cf,void * aux)79 abtn_match(struct device *parent, void *cf, void *aux)
80 {
81 	struct adb_attach_args *aa = aux;
82 
83 	if (strcmp(aa->name, adb_device_name) != 0)
84 		return (0);
85 
86 	if (aa->origaddr == ADBADDR_MISC &&
87 	    aa->handler_id == ABTN_HANDLER_ID)
88 		return 1;
89 
90 	return 0;
91 }
92 
93 void
abtn_attach(struct device * parent,struct device * self,void * aux)94 abtn_attach(struct device *parent, struct device *self, void *aux)
95 {
96 	struct abtn_softc *sc = (struct abtn_softc *)self;
97 	struct adb_attach_args *aa = aux;
98 	ADBSetInfoBlock adbinfo;
99 
100 	printf(": brightness/volume/eject buttons\n");
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 	set_adb_info(&adbinfo, sc->adbaddr);
110 }
111 
112 void
abtn_adbcomplete(caddr_t buffer,caddr_t data,int adb_command)113 abtn_adbcomplete(caddr_t buffer, caddr_t data, int adb_command)
114 {
115 	u_int cmd, brightness;
116 
117 	cmd = buffer[1];
118 
119 	switch (cmd) {
120 	case 0x0a:	/* decrease brightness */
121 		brightness = cons_brightness;
122 		if (brightness == MAX_BRIGHTNESS)
123 			brightness++;		/* get round values */
124 		brightness -= STEP_BRIGHTNESS;
125 		of_setbrightness(brightness);
126 		break;
127 
128 	case 0x09:	/* increase brightness */
129 		brightness = cons_brightness + STEP_BRIGHTNESS;
130 		of_setbrightness(brightness);
131 		break;
132 
133 #if NAUDIO > 0 && NWSKBD > 0
134 	case 0x08:	/* mute */
135 	case 0x01:	/* mute, AV hardware */
136 		wskbd_set_mixervolume(0, 1);
137 		break;
138 	case 0x07:	/* decrease volume */
139 	case 0x02:	/* decrease volume, AV hardware */
140 		wskbd_set_mixervolume(-1, 1);
141 		break;
142 	case 0x06:	/* increase volume */
143 	case 0x03:	/* increase volume, AV hardware */
144 		wskbd_set_mixervolume(1, 1);
145 		break;
146 #endif
147 	case 0x0c:	/* mirror display key */
148 		/* Need callback to do something with this */
149 		break;
150 #if NWSKBD > 0 && NCD > 0
151 	case 0x0b:	/* eject tray */
152 		task_add(systq, &eject_task);
153 		break;
154 #endif
155 	case 0x7f:	/* numlock */
156 		/* Need callback to do something with this */
157 		break;
158 
159 	default:
160 #ifdef DEBUG
161 		if ((cmd & ~0x7f) == 0)
162 			printf("unknown ADB button 0x%x\n", cmd);
163 #endif
164 		break;
165 	}
166 }
167