xref: /netbsd-src/sys/dev/adb/adb_bt.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: adb_bt.c,v 1.3 2008/04/05 21:33:33 cegger Exp $ */
2 
3 /*-
4  * Copyright (c) 2006 Michael Lorenz
5  * 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. Neither the name of The NetBSD Foundation nor the names of its
16  *    contributors may be used to endorse or promote products derived
17  *    from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: adb_bt.c,v 1.3 2008/04/05 21:33:33 cegger Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <sys/proc.h>
40 
41 #include <dev/wscons/wsconsio.h>
42 #include <dev/wscons/wskbdvar.h>
43 #include <dev/wscons/wsksymdef.h>
44 #include <dev/wscons/wsksymvar.h>
45 #include <dev/wscons/wsmousevar.h>
46 
47 #include <dev/sysmon/sysmonvar.h>
48 #include <dev/sysmon/sysmon_taskq.h>
49 
50 #include <machine/autoconf.h>
51 #include <machine/keyboard.h>
52 #include <machine/adbsys.h>
53 
54 #include <dev/adb/adbvar.h>
55 #include <dev/adb/adb_keymap.h>
56 
57 #include "opt_wsdisplay_compat.h"
58 #include "adbdebug.h"
59 
60 #ifdef ADBBT_DEBUG
61 #define DPRINTF printf
62 #else
63 #define DPRINTF while (0) printf
64 #endif
65 
66 #define BT_VOL_UP	0x06
67 #define BT_VOL_DOWN	0x07
68 #define BT_VOL_MUTE	0x08
69 #define BT_BRT_UP	0x09
70 #define BT_BRT_DOWN	0x0a
71 #define BT_EJECT	0x0b
72 #define BT_F7		0x0c
73 #define BT_NUMLOCK	0x7f
74 
75 static int adbbt_match(device_t, cfdata_t, void *);
76 static void adbbt_attach(device_t, device_t, void *);
77 
78 struct adbbt_softc {
79 	device_t sc_dev;
80 	struct adb_device *sc_adbdev;
81 	struct adb_bus_accessops *sc_ops;
82 	device_t sc_wskbddev;
83 	int sc_msg_len;
84 	int sc_event;
85 	int sc_poll;
86 	int sc_polled_chars;
87 	int sc_rawkbd;
88 	uint8_t sc_buffer[16];
89 	uint8_t sc_pollbuf[16];
90 	uint8_t sc_trans[16];
91 	uint8_t sc_us;
92 };
93 
94 /* Driver definition. */
95 CFATTACH_DECL_NEW(adbbt, sizeof(struct adbbt_softc),
96     adbbt_match, adbbt_attach, NULL, NULL);
97 
98 extern struct cfdriver adbbt_cd;
99 
100 static int adbbt_enable(void *, int);
101 static void adbbt_set_leds(void *, int);
102 static int adbbt_ioctl(void *, u_long, void *, int, struct lwp *);
103 static void adbbt_handler(void *, int, uint8_t *);
104 
105 struct wskbd_accessops adbbt_accessops = {
106 	adbbt_enable,
107 	adbbt_set_leds,
108 	adbbt_ioctl,
109 };
110 
111 struct wskbd_mapdata adbbt_keymapdata = {
112 	akbd_keydesctab,
113 #ifdef AKBD_LAYOUT
114 	AKBD_LAYOUT,
115 #else
116 	KB_US,
117 #endif
118 };
119 
120 static int
121 adbbt_match(device_t parent, cfdata_t cf, void *aux)
122 {
123 	struct adb_attach_args *aaa = aux;
124 
125 	if ((aaa->dev->original_addr == ADBADDR_MISC) &&
126 	    (aaa->dev->handler_id == 0x1f))
127 		return 100;
128 	else
129 		return 0;
130 }
131 
132 static void
133 adbbt_attach(device_t parent, device_t self, void *aux)
134 {
135 	struct adbbt_softc *sc = device_private(self);
136 	struct adb_attach_args *aaa = aux;
137 	struct wskbddev_attach_args a;
138 
139 	sc->sc_dev = self;
140 	sc->sc_ops = aaa->ops;
141 	sc->sc_adbdev = aaa->dev;
142 	sc->sc_adbdev->cookie = sc;
143 	sc->sc_adbdev->handler = adbbt_handler;
144 	sc->sc_us = ADBTALK(sc->sc_adbdev->current_addr, 0);
145 
146 	sc->sc_msg_len = 0;
147 	sc->sc_rawkbd = 0;
148 	sc->sc_trans[6]  = 96;	/* F5  */
149 	sc->sc_trans[7]  = 118;	/* F4  */
150 	sc->sc_trans[8]  = 99;	/* F3  */
151 	sc->sc_trans[9]  = 120;	/* F2  */
152 	sc->sc_trans[10] = 122;	/* F1  */
153 	sc->sc_trans[11] = 111;	/* F12 */
154 
155 	printf(" addr %d: button device\n", sc->sc_adbdev->current_addr);
156 
157 	a.console = 0;
158 	a.keymap = &adbbt_keymapdata;
159 	a.accessops = &adbbt_accessops;
160 	a.accesscookie = sc;
161 
162 	sc->sc_wskbddev = config_found_ia(self, "wskbddev", &a, wskbddevprint);
163 }
164 
165 static void
166 adbbt_handler(void *cookie, int len, uint8_t *data)
167 {
168 	struct adbbt_softc *sc = cookie;
169 	int type;
170 	uint8_t k, keyval, scancode;
171 
172 #ifdef ADBBT_DEBUG
173 	int i;
174 	printf("%s: %02x - ", device_xname(&sc->sc_dev), sc->sc_us);
175 	for (i = 0; i < len; i++) {
176 		printf(" %02x", data[i]);
177 	}
178 	printf("\n");
179 #endif
180 	k = data[2];
181 	keyval = ADBK_KEYVAL(k);
182 	if ((keyval < 6) || (keyval > 0x0c))
183 		return;
184 	scancode = sc->sc_trans[keyval];
185 
186 #ifdef WSDISPLAY_COMPAT_RAWKBD
187 	if (sc->sc_rawkbd) {
188 		char cbuf[2];
189 		int s;
190 
191 		cbuf[0] = scancode | (k & 0x80);
192 
193 		s = spltty();
194 		wskbd_rawinput(sc->sc_wskbddev, cbuf, 1);
195 		splx(s);
196 	} else {
197 #endif
198 
199 	/* normal event */
200 	type = ADBK_PRESS(k) ?
201 	    WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
202 	wskbd_input(sc->sc_wskbddev, type, scancode);
203 #ifdef WSDISPLAY_COMPAT_RAWKBD
204 	}
205 #endif
206 }
207 
208 static void
209 adbbt_set_leds(void *cookie, int leds)
210 {
211 	/* we have no LEDs */
212 }
213 
214 static int
215 adbbt_enable(void *v, int on)
216 {
217 	return 0;
218 }
219 
220 static int
221 adbbt_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
222 {
223 #ifdef WSDISPLAY_COMPAT_RAWKBD
224 	struct adbbt_softc *sc = (struct adbbt_softc *) v;
225 #endif
226 
227 	switch (cmd) {
228 
229 	case WSKBDIO_GTYPE:
230 		*(int *)data = WSKBD_TYPE_ADB;
231 		return 0;
232 #ifdef WSDISPLAY_COMPAT_RAWKBD
233 	case WSKBDIO_SETMODE:
234 		sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
235 		return 0;
236 #endif
237 	}
238 
239 	return EPASSTHROUGH;
240 }
241