xref: /netbsd-src/sys/dev/adb/adb_bt.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: adb_bt.c,v 1.1 2007/04/16 23:34:43 macallan 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.1 2007/04/16 23:34:43 macallan 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(struct device *, struct cfdata *, void *);
76 static void adbbt_attach(struct device *, struct device *, void *);
77 
78 struct adbbt_softc {
79 	struct device sc_dev;
80 	struct adb_device *sc_adbdev;
81 	struct adb_bus_accessops *sc_ops;
82 	struct device *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(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(parent, cf, aux)
122 	struct device *parent;
123 	struct cfdata *cf;
124 	void   *aux;
125 {
126 	struct adb_attach_args *aaa = aux;
127 
128 	if ((aaa->dev->original_addr == ADBADDR_MISC) &&
129 	    (aaa->dev->handler_id == 0x1f))
130 		return 100;
131 	else
132 		return 0;
133 }
134 
135 static void
136 adbbt_attach(struct device *parent, struct device *self, void *aux)
137 {
138 	struct adbbt_softc *sc = (struct adbbt_softc *)self;
139 	struct adb_attach_args *aaa = aux;
140 	struct wskbddev_attach_args a;
141 
142 	sc->sc_ops = aaa->ops;
143 	sc->sc_adbdev = aaa->dev;
144 	sc->sc_adbdev->cookie = sc;
145 	sc->sc_adbdev->handler = adbbt_handler;
146 	sc->sc_us = ADBTALK(sc->sc_adbdev->current_addr, 0);
147 
148 	sc->sc_msg_len = 0;
149 	sc->sc_rawkbd = 0;
150 	sc->sc_trans[6]  = 96;	/* F5  */
151 	sc->sc_trans[7]  = 118;	/* F4  */
152 	sc->sc_trans[8]  = 99;	/* F3  */
153 	sc->sc_trans[9]  = 120;	/* F2  */
154 	sc->sc_trans[10] = 122;	/* F1  */
155 	sc->sc_trans[11] = 111;	/* F12 */
156 
157 	printf(" addr %d: button device\n", sc->sc_adbdev->current_addr);
158 
159 	a.console = 0;
160 	a.keymap = &adbbt_keymapdata;
161 	a.accessops = &adbbt_accessops;
162 	a.accesscookie = sc;
163 
164 	sc->sc_wskbddev = config_found_ia(self, "wskbddev", &a, wskbddevprint);
165 }
166 
167 static void
168 adbbt_handler(void *cookie, int len, uint8_t *data)
169 {
170 	struct adbbt_softc *sc = cookie;
171 	int type;
172 	uint8_t k, keyval, scancode;
173 
174 #ifdef ADBBT_DEBUG
175 	int i;
176 	printf("%s: %02x - ", sc->sc_dev.dv_xname, sc->sc_us);
177 	for (i = 0; i < len; i++) {
178 		printf(" %02x", data[i]);
179 	}
180 	printf("\n");
181 #endif
182 	k = data[2];
183 	keyval = ADBK_KEYVAL(k);
184 	if ((keyval < 6) || (keyval > 0x0c))
185 		return;
186 	scancode = sc->sc_trans[keyval];
187 
188 #ifdef WSDISPLAY_COMPAT_RAWKBD
189 	if (sc->sc_rawkbd) {
190 		char cbuf[2];
191 		int s;
192 
193 		cbuf[0] = scancode | (k & 0x80);
194 
195 		s = spltty();
196 		wskbd_rawinput(sc->sc_wskbddev, cbuf, 1);
197 		splx(s);
198 	} else {
199 #endif
200 
201 	/* normal event */
202 	type = ADBK_PRESS(k) ?
203 	    WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
204 	wskbd_input(sc->sc_wskbddev, type, scancode);
205 #ifdef WSDISPLAY_COMPAT_RAWKBD
206 	}
207 #endif
208 }
209 
210 static void
211 adbbt_set_leds(void *cookie, int leds)
212 {
213 	/* we have no LEDs */
214 }
215 
216 static int
217 adbbt_enable(void *v, int on)
218 {
219 	return 0;
220 }
221 
222 static int
223 adbbt_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
224 {
225 #ifdef WSDISPLAY_COMPAT_RAWKBD
226 	struct adbbt_softc *sc = (struct adbbt_softc *) v;
227 #endif
228 
229 	switch (cmd) {
230 
231 	case WSKBDIO_GTYPE:
232 		*(int *)data = WSKBD_TYPE_ADB;
233 		return 0;
234 #ifdef WSDISPLAY_COMPAT_RAWKBD
235 	case WSKBDIO_SETMODE:
236 		sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
237 		return 0;
238 #endif
239 	}
240 
241 	return EPASSTHROUGH;
242 }
243