btmagic.c revision 1.1 1 1.1 plunky /* $NetBSD: btmagic.c,v 1.1 2010/05/22 18:56:01 plunky Exp $ */
2 1.1 plunky
3 1.1 plunky /*-
4 1.1 plunky * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 1.1 plunky * All rights reserved.
6 1.1 plunky *
7 1.1 plunky * This code is derived from software contributed to The NetBSD Foundation
8 1.1 plunky * by Iain Hibbert.
9 1.1 plunky *
10 1.1 plunky * This code is derived from software contributed to The NetBSD Foundation
11 1.1 plunky * by Lennart Augustsson (lennart (at) augustsson.net) at
12 1.1 plunky * Carlstedt Research & Technology.
13 1.1 plunky *
14 1.1 plunky * Redistribution and use in source and binary forms, with or without
15 1.1 plunky * modification, are permitted provided that the following conditions
16 1.1 plunky * are met:
17 1.1 plunky * 1. Redistributions of source code must retain the above copyright
18 1.1 plunky * notice, this list of conditions and the following disclaimer.
19 1.1 plunky * 2. Redistributions in binary form must reproduce the above copyright
20 1.1 plunky * notice, this list of conditions and the following disclaimer in the
21 1.1 plunky * documentation and/or other materials provided with the distribution.
22 1.1 plunky *
23 1.1 plunky * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 1.1 plunky * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 1.1 plunky * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 1.1 plunky * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 1.1 plunky * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 1.1 plunky * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 1.1 plunky * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 1.1 plunky * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 1.1 plunky * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 1.1 plunky * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.1 plunky * POSSIBILITY OF SUCH DAMAGE.
34 1.1 plunky */
35 1.1 plunky /*-
36 1.1 plunky * Copyright (c) 2006 Itronix Inc.
37 1.1 plunky * All rights reserved.
38 1.1 plunky *
39 1.1 plunky * Written by Iain Hibbert for Itronix Inc.
40 1.1 plunky *
41 1.1 plunky * Redistribution and use in source and binary forms, with or without
42 1.1 plunky * modification, are permitted provided that the following conditions
43 1.1 plunky * are met:
44 1.1 plunky * 1. Redistributions of source code must retain the above copyright
45 1.1 plunky * notice, this list of conditions and the following disclaimer.
46 1.1 plunky * 2. Redistributions in binary form must reproduce the above copyright
47 1.1 plunky * notice, this list of conditions and the following disclaimer in the
48 1.1 plunky * documentation and/or other materials provided with the distribution.
49 1.1 plunky * 3. The name of Itronix Inc. may not be used to endorse
50 1.1 plunky * or promote products derived from this software without specific
51 1.1 plunky * prior written permission.
52 1.1 plunky *
53 1.1 plunky * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
54 1.1 plunky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55 1.1 plunky * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56 1.1 plunky * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
57 1.1 plunky * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
58 1.1 plunky * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
59 1.1 plunky * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
60 1.1 plunky * ON ANY THEORY OF LIABILITY, WHETHER IN
61 1.1 plunky * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62 1.1 plunky * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63 1.1 plunky * POSSIBILITY OF SUCH DAMAGE.
64 1.1 plunky */
65 1.1 plunky
66 1.1 plunky
67 1.1 plunky /*****************************************************************************
68 1.1 plunky *
69 1.1 plunky * Apple Bluetooth Magic Mouse driver
70 1.1 plunky *
71 1.1 plunky * The Apple Magic Mouse is a HID device but it doesn't provide a proper HID
72 1.1 plunky * descriptor, and requires extra initializations to enable the proprietary
73 1.1 plunky * touch reports. We match against the vendor-id and product-id and provide
74 1.1 plunky * our own Bluetooth connection handling as the bthidev driver does not cater
75 1.1 plunky * for such complications.
76 1.1 plunky *
77 1.1 plunky * This driver interprets the touch reports only as far as emulating a
78 1.1 plunky * middle mouse button and providing horizontal and vertical scroll action.
79 1.1 plunky * Full gesture support would be more complicated and is left as an exercise
80 1.1 plunky * for the reader.
81 1.1 plunky *
82 1.1 plunky * Credit for decoding the proprietary touch reports goes to Michael Poole
83 1.1 plunky * who wrote the Linux hid-magicmouse input driver.
84 1.1 plunky *
85 1.1 plunky *****************************************************************************/
86 1.1 plunky
87 1.1 plunky #include <sys/cdefs.h>
88 1.1 plunky __KERNEL_RCSID(0, "$NetBSD: btmagic.c,v 1.1 2010/05/22 18:56:01 plunky Exp $");
89 1.1 plunky
90 1.1 plunky #include <sys/param.h>
91 1.1 plunky #include <sys/conf.h>
92 1.1 plunky #include <sys/device.h>
93 1.1 plunky #include <sys/fcntl.h>
94 1.1 plunky #include <sys/kernel.h>
95 1.1 plunky #include <sys/malloc.h>
96 1.1 plunky #include <sys/mbuf.h>
97 1.1 plunky #include <sys/proc.h>
98 1.1 plunky #include <sys/socketvar.h>
99 1.1 plunky #include <sys/systm.h>
100 1.1 plunky #include <sys/sysctl.h>
101 1.1 plunky
102 1.1 plunky #include <prop/proplib.h>
103 1.1 plunky
104 1.1 plunky #include <netbt/bluetooth.h>
105 1.1 plunky #include <netbt/l2cap.h>
106 1.1 plunky
107 1.1 plunky #include <dev/bluetooth/btdev.h>
108 1.1 plunky #include <dev/bluetooth/bthid.h>
109 1.1 plunky #include <dev/bluetooth/bthidev.h>
110 1.1 plunky
111 1.1 plunky #include <dev/usb/hid.h>
112 1.1 plunky #include <dev/usb/usb.h>
113 1.1 plunky #include <dev/usb/usbdevs.h>
114 1.1 plunky
115 1.1 plunky #include <dev/wscons/wsconsio.h>
116 1.1 plunky #include <dev/wscons/wsmousevar.h>
117 1.1 plunky
118 1.1 plunky #undef DPRINTF
119 1.1 plunky #ifdef BTMAGIC_DEBUG
120 1.1 plunky #define DPRINTF(sc, ...) do { \
121 1.1 plunky printf("%s: ", device_xname((sc)->sc_dev)); \
122 1.1 plunky printf(__VA_ARGS__); \
123 1.1 plunky printf("\n"); \
124 1.1 plunky } while (/*CONSTCOND*/0)
125 1.1 plunky #else
126 1.1 plunky #define DPRINTF(...) (void)0
127 1.1 plunky #endif
128 1.1 plunky
129 1.1 plunky struct btmagic_softc {
130 1.1 plunky bdaddr_t sc_laddr; /* local address */
131 1.1 plunky bdaddr_t sc_raddr; /* remote address */
132 1.1 plunky struct sockopt sc_mode; /* link mode */
133 1.1 plunky
134 1.1 plunky device_t sc_dev;
135 1.1 plunky uint16_t sc_state;
136 1.1 plunky uint16_t sc_flags;
137 1.1 plunky
138 1.1 plunky callout_t sc_timeout;
139 1.1 plunky
140 1.1 plunky /* control */
141 1.1 plunky struct l2cap_channel *sc_ctl;
142 1.1 plunky struct l2cap_channel *sc_ctl_l;
143 1.1 plunky
144 1.1 plunky /* interrupt */
145 1.1 plunky struct l2cap_channel *sc_int;
146 1.1 plunky struct l2cap_channel *sc_int_l;
147 1.1 plunky
148 1.1 plunky /* wsmouse child */
149 1.1 plunky device_t sc_wsmouse;
150 1.1 plunky int sc_enabled;
151 1.1 plunky
152 1.1 plunky /* config */
153 1.1 plunky int sc_resolution; /* for soft scaling */
154 1.1 plunky int sc_firm; /* firm touch threshold */
155 1.1 plunky int sc_dist; /* scroll distance threshold */
156 1.1 plunky int sc_scale; /* scroll descaling */
157 1.1 plunky struct sysctllog *sc_log; /* sysctl teardown log */
158 1.1 plunky
159 1.1 plunky /* remainders */
160 1.1 plunky int sc_rx;
161 1.1 plunky int sc_ry;
162 1.1 plunky int sc_rz;
163 1.1 plunky int sc_rw;
164 1.1 plunky
165 1.1 plunky /* previous touches */
166 1.1 plunky uint32_t sc_smask; /* scrolling */
167 1.1 plunky int sc_az[16];
168 1.1 plunky int sc_aw[16];
169 1.1 plunky
170 1.1 plunky /* previous mouse buttons */
171 1.1 plunky uint32_t sc_mb;
172 1.1 plunky };
173 1.1 plunky
174 1.1 plunky /* sc_flags */
175 1.1 plunky #define BTMAGIC_CONNECTING __BIT(0) /* we are connecting */
176 1.1 plunky #define BTMAGIC_ENABLED __BIT(1) /* touch reports enabled */
177 1.1 plunky
178 1.1 plunky /* sc_state */
179 1.1 plunky #define BTMAGIC_CLOSED 0
180 1.1 plunky #define BTMAGIC_WAIT_CTL 1
181 1.1 plunky #define BTMAGIC_WAIT_INT 2
182 1.1 plunky #define BTMAGIC_OPEN 3
183 1.1 plunky
184 1.1 plunky /* autoconf(9) glue */
185 1.1 plunky static int btmagic_match(device_t, cfdata_t, void *);
186 1.1 plunky static void btmagic_attach(device_t, device_t, void *);
187 1.1 plunky static int btmagic_detach(device_t, int);
188 1.1 plunky static int btmagic_listen(struct btmagic_softc *);
189 1.1 plunky static int btmagic_connect(struct btmagic_softc *);
190 1.1 plunky static int btmagic_sysctl_resolution(SYSCTLFN_PROTO);
191 1.1 plunky static int btmagic_sysctl_scale(SYSCTLFN_PROTO);
192 1.1 plunky
193 1.1 plunky CFATTACH_DECL_NEW(btmagic, sizeof(struct btmagic_softc),
194 1.1 plunky btmagic_match, btmagic_attach, btmagic_detach, NULL);
195 1.1 plunky
196 1.1 plunky /* wsmouse(4) accessops */
197 1.1 plunky static int btmagic_wsmouse_enable(void *);
198 1.1 plunky static int btmagic_wsmouse_ioctl(void *, unsigned long, void *, int, struct lwp *);
199 1.1 plunky static void btmagic_wsmouse_disable(void *);
200 1.1 plunky
201 1.1 plunky static const struct wsmouse_accessops btmagic_wsmouse_accessops = {
202 1.1 plunky btmagic_wsmouse_enable,
203 1.1 plunky btmagic_wsmouse_ioctl,
204 1.1 plunky btmagic_wsmouse_disable,
205 1.1 plunky };
206 1.1 plunky
207 1.1 plunky /* bluetooth(9) protocol methods for L2CAP */
208 1.1 plunky static void btmagic_connecting(void *);
209 1.1 plunky static void btmagic_ctl_connected(void *);
210 1.1 plunky static void btmagic_int_connected(void *);
211 1.1 plunky static void btmagic_ctl_disconnected(void *, int);
212 1.1 plunky static void btmagic_int_disconnected(void *, int);
213 1.1 plunky static void *btmagic_ctl_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
214 1.1 plunky static void *btmagic_int_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
215 1.1 plunky static void btmagic_complete(void *, int);
216 1.1 plunky static void btmagic_linkmode(void *, int);
217 1.1 plunky static void btmagic_input(void *, struct mbuf *);
218 1.1 plunky static void btmagic_input_basic(struct btmagic_softc *, uint8_t *, size_t);
219 1.1 plunky static void btmagic_input_magic(struct btmagic_softc *, uint8_t *, size_t);
220 1.1 plunky
221 1.1 plunky static const struct btproto btmagic_ctl_proto = {
222 1.1 plunky btmagic_connecting,
223 1.1 plunky btmagic_ctl_connected,
224 1.1 plunky btmagic_ctl_disconnected,
225 1.1 plunky btmagic_ctl_newconn,
226 1.1 plunky btmagic_complete,
227 1.1 plunky btmagic_linkmode,
228 1.1 plunky btmagic_input,
229 1.1 plunky };
230 1.1 plunky
231 1.1 plunky static const struct btproto btmagic_int_proto = {
232 1.1 plunky btmagic_connecting,
233 1.1 plunky btmagic_int_connected,
234 1.1 plunky btmagic_int_disconnected,
235 1.1 plunky btmagic_int_newconn,
236 1.1 plunky btmagic_complete,
237 1.1 plunky btmagic_linkmode,
238 1.1 plunky btmagic_input,
239 1.1 plunky };
240 1.1 plunky
241 1.1 plunky /* btmagic internals */
242 1.1 plunky static void btmagic_timeout(void *);
243 1.1 plunky static int btmagic_ctl_send(struct btmagic_softc *, const uint8_t *, size_t);
244 1.1 plunky static void btmagic_enable(struct btmagic_softc *);
245 1.1 plunky static void btmagic_check_battery(struct btmagic_softc *);
246 1.1 plunky static int btmagic_scale(int, int *, int);
247 1.1 plunky
248 1.1 plunky
249 1.1 plunky /*****************************************************************************
250 1.1 plunky *
251 1.1 plunky * Magic Mouse autoconf(9) routines
252 1.1 plunky */
253 1.1 plunky
254 1.1 plunky static int
255 1.1 plunky btmagic_match(device_t self, cfdata_t cfdata, void *aux)
256 1.1 plunky {
257 1.1 plunky uint16_t v, p;
258 1.1 plunky
259 1.1 plunky if (prop_dictionary_get_uint16(aux, BTDEVvendor, &v)
260 1.1 plunky && prop_dictionary_get_uint16(aux, BTDEVproduct, &p)
261 1.1 plunky && v == USB_VENDOR_APPLE
262 1.1 plunky && p == USB_PRODUCT_APPLE_MAGICMOUSE)
263 1.1 plunky return 2; /* trump bthidev(4) */
264 1.1 plunky
265 1.1 plunky return 0;
266 1.1 plunky }
267 1.1 plunky
268 1.1 plunky static void
269 1.1 plunky btmagic_attach(device_t parent, device_t self, void *aux)
270 1.1 plunky {
271 1.1 plunky struct btmagic_softc *sc = device_private(self);
272 1.1 plunky struct wsmousedev_attach_args wsma;
273 1.1 plunky const struct sysctlnode *node;
274 1.1 plunky prop_object_t obj;
275 1.1 plunky
276 1.1 plunky /*
277 1.1 plunky * Init softc
278 1.1 plunky */
279 1.1 plunky sc->sc_dev = self;
280 1.1 plunky sc->sc_state = BTMAGIC_CLOSED;
281 1.1 plunky callout_init(&sc->sc_timeout, 0);
282 1.1 plunky callout_setfunc(&sc->sc_timeout, btmagic_timeout, sc);
283 1.1 plunky sockopt_init(&sc->sc_mode, BTPROTO_L2CAP, SO_L2CAP_LM, 0);
284 1.1 plunky
285 1.1 plunky /*
286 1.1 plunky * extract config from proplist
287 1.1 plunky */
288 1.1 plunky obj = prop_dictionary_get(aux, BTDEVladdr);
289 1.1 plunky bdaddr_copy(&sc->sc_laddr, prop_data_data_nocopy(obj));
290 1.1 plunky
291 1.1 plunky obj = prop_dictionary_get(aux, BTDEVraddr);
292 1.1 plunky bdaddr_copy(&sc->sc_raddr, prop_data_data_nocopy(obj));
293 1.1 plunky
294 1.1 plunky obj = prop_dictionary_get(aux, BTDEVmode);
295 1.1 plunky if (prop_object_type(obj) == PROP_TYPE_STRING) {
296 1.1 plunky if (prop_string_equals_cstring(obj, BTDEVauth))
297 1.1 plunky sockopt_setint(&sc->sc_mode, L2CAP_LM_AUTH);
298 1.1 plunky else if (prop_string_equals_cstring(obj, BTDEVencrypt))
299 1.1 plunky sockopt_setint(&sc->sc_mode, L2CAP_LM_ENCRYPT);
300 1.1 plunky else if (prop_string_equals_cstring(obj, BTDEVsecure))
301 1.1 plunky sockopt_setint(&sc->sc_mode, L2CAP_LM_SECURE);
302 1.1 plunky else {
303 1.1 plunky aprint_error(" unknown %s\n", BTDEVmode);
304 1.1 plunky return;
305 1.1 plunky }
306 1.1 plunky
307 1.1 plunky aprint_verbose(" %s %s", BTDEVmode,
308 1.1 plunky prop_string_cstring_nocopy(obj));
309 1.1 plunky }
310 1.1 plunky
311 1.1 plunky aprint_normal(": 3 buttons, W and Z dirs\n");
312 1.1 plunky aprint_naive("\n");
313 1.1 plunky
314 1.1 plunky /*
315 1.1 plunky * set defaults
316 1.1 plunky */
317 1.1 plunky sc->sc_resolution = 650;
318 1.1 plunky sc->sc_firm = 6;
319 1.1 plunky sc->sc_dist = 130;
320 1.1 plunky sc->sc_scale = 20;
321 1.1 plunky
322 1.1 plunky sysctl_createv(&sc->sc_log, 0, NULL, NULL,
323 1.1 plunky CTLFLAG_PERMANENT,
324 1.1 plunky CTLTYPE_NODE, "hw",
325 1.1 plunky NULL,
326 1.1 plunky NULL, 0,
327 1.1 plunky NULL, 0,
328 1.1 plunky CTL_HW, CTL_EOL);
329 1.1 plunky
330 1.1 plunky sysctl_createv(&sc->sc_log, 0, NULL, &node,
331 1.1 plunky 0,
332 1.1 plunky CTLTYPE_NODE, device_xname(self),
333 1.1 plunky NULL,
334 1.1 plunky NULL, 0,
335 1.1 plunky NULL, 0,
336 1.1 plunky CTL_HW,
337 1.1 plunky CTL_CREATE, CTL_EOL);
338 1.1 plunky
339 1.1 plunky if (node != NULL) {
340 1.1 plunky sysctl_createv(&sc->sc_log, 0, NULL, NULL,
341 1.1 plunky CTLFLAG_READWRITE,
342 1.1 plunky CTLTYPE_INT, "soft_resolution",
343 1.1 plunky NULL,
344 1.1 plunky btmagic_sysctl_resolution, 0,
345 1.1 plunky sc, 0,
346 1.1 plunky CTL_HW, node->sysctl_num,
347 1.1 plunky CTL_CREATE, CTL_EOL);
348 1.1 plunky
349 1.1 plunky sysctl_createv(&sc->sc_log, 0, NULL, NULL,
350 1.1 plunky CTLFLAG_READWRITE,
351 1.1 plunky CTLTYPE_INT, "firm_touch_threshold",
352 1.1 plunky NULL,
353 1.1 plunky NULL, 0,
354 1.1 plunky &sc->sc_firm, sizeof(sc->sc_firm),
355 1.1 plunky CTL_HW, node->sysctl_num,
356 1.1 plunky CTL_CREATE, CTL_EOL);
357 1.1 plunky
358 1.1 plunky sysctl_createv(&sc->sc_log, 0, NULL, NULL,
359 1.1 plunky CTLFLAG_READWRITE,
360 1.1 plunky CTLTYPE_INT, "scroll_distance_threshold",
361 1.1 plunky NULL,
362 1.1 plunky NULL, 0,
363 1.1 plunky &sc->sc_dist, sizeof(sc->sc_dist),
364 1.1 plunky CTL_HW, node->sysctl_num,
365 1.1 plunky CTL_CREATE, CTL_EOL);
366 1.1 plunky
367 1.1 plunky sysctl_createv(&sc->sc_log, 0, NULL, NULL,
368 1.1 plunky CTLFLAG_READWRITE,
369 1.1 plunky CTLTYPE_INT, "scroll_downscale_factor",
370 1.1 plunky NULL,
371 1.1 plunky btmagic_sysctl_scale, 0,
372 1.1 plunky sc, 0,
373 1.1 plunky CTL_HW, node->sysctl_num,
374 1.1 plunky CTL_CREATE, CTL_EOL);
375 1.1 plunky }
376 1.1 plunky
377 1.1 plunky /*
378 1.1 plunky * attach the wsmouse
379 1.1 plunky */
380 1.1 plunky wsma.accessops = &btmagic_wsmouse_accessops;
381 1.1 plunky wsma.accesscookie = self;
382 1.1 plunky sc->sc_wsmouse = config_found(self, &wsma, wsmousedevprint);
383 1.1 plunky if (sc->sc_wsmouse == NULL) {
384 1.1 plunky aprint_error_dev(self, "failed to attach wsmouse\n");
385 1.1 plunky return;
386 1.1 plunky }
387 1.1 plunky
388 1.1 plunky /*
389 1.1 plunky * start bluetooth connections
390 1.1 plunky */
391 1.1 plunky mutex_enter(bt_lock);
392 1.1 plunky btmagic_listen(sc);
393 1.1 plunky btmagic_connect(sc);
394 1.1 plunky mutex_exit(bt_lock);
395 1.1 plunky }
396 1.1 plunky
397 1.1 plunky static int
398 1.1 plunky btmagic_detach(device_t self, int flags)
399 1.1 plunky {
400 1.1 plunky struct btmagic_softc *sc = device_private(self);
401 1.1 plunky int err = 0;
402 1.1 plunky
403 1.1 plunky mutex_enter(bt_lock);
404 1.1 plunky
405 1.1 plunky /* release interrupt listen */
406 1.1 plunky if (sc->sc_int_l != NULL) {
407 1.1 plunky l2cap_detach(&sc->sc_int_l);
408 1.1 plunky sc->sc_int_l = NULL;
409 1.1 plunky }
410 1.1 plunky
411 1.1 plunky /* release control listen */
412 1.1 plunky if (sc->sc_ctl_l != NULL) {
413 1.1 plunky l2cap_detach(&sc->sc_ctl_l);
414 1.1 plunky sc->sc_ctl_l = NULL;
415 1.1 plunky }
416 1.1 plunky
417 1.1 plunky /* close interrupt channel */
418 1.1 plunky if (sc->sc_int != NULL) {
419 1.1 plunky l2cap_disconnect(sc->sc_int, 0);
420 1.1 plunky l2cap_detach(&sc->sc_int);
421 1.1 plunky sc->sc_int = NULL;
422 1.1 plunky }
423 1.1 plunky
424 1.1 plunky /* close control channel */
425 1.1 plunky if (sc->sc_ctl != NULL) {
426 1.1 plunky l2cap_disconnect(sc->sc_ctl, 0);
427 1.1 plunky l2cap_detach(&sc->sc_ctl);
428 1.1 plunky sc->sc_ctl = NULL;
429 1.1 plunky }
430 1.1 plunky
431 1.1 plunky callout_halt(&sc->sc_timeout, bt_lock);
432 1.1 plunky callout_destroy(&sc->sc_timeout);
433 1.1 plunky
434 1.1 plunky mutex_exit(bt_lock);
435 1.1 plunky
436 1.1 plunky sockopt_destroy(&sc->sc_mode);
437 1.1 plunky
438 1.1 plunky sysctl_teardown(&sc->sc_log);
439 1.1 plunky
440 1.1 plunky if (sc->sc_wsmouse != NULL) {
441 1.1 plunky err = config_detach(sc->sc_wsmouse, flags);
442 1.1 plunky sc->sc_wsmouse = NULL;
443 1.1 plunky }
444 1.1 plunky
445 1.1 plunky return err;
446 1.1 plunky }
447 1.1 plunky
448 1.1 plunky /*
449 1.1 plunky * listen for our device
450 1.1 plunky *
451 1.1 plunky * bt_lock is held
452 1.1 plunky */
453 1.1 plunky static int
454 1.1 plunky btmagic_listen(struct btmagic_softc *sc)
455 1.1 plunky {
456 1.1 plunky struct sockaddr_bt sa;
457 1.1 plunky int err;
458 1.1 plunky
459 1.1 plunky memset(&sa, 0, sizeof(sa));
460 1.1 plunky sa.bt_len = sizeof(sa);
461 1.1 plunky sa.bt_family = AF_BLUETOOTH;
462 1.1 plunky bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
463 1.1 plunky
464 1.1 plunky /*
465 1.1 plunky * Listen on control PSM
466 1.1 plunky */
467 1.1 plunky err = l2cap_attach(&sc->sc_ctl_l, &btmagic_ctl_proto, sc);
468 1.1 plunky if (err)
469 1.1 plunky return err;
470 1.1 plunky
471 1.1 plunky err = l2cap_setopt(sc->sc_ctl_l, &sc->sc_mode);
472 1.1 plunky if (err)
473 1.1 plunky return err;
474 1.1 plunky
475 1.1 plunky sa.bt_psm = L2CAP_PSM_HID_CNTL;
476 1.1 plunky err = l2cap_bind(sc->sc_ctl_l, &sa);
477 1.1 plunky if (err)
478 1.1 plunky return err;
479 1.1 plunky
480 1.1 plunky err = l2cap_listen(sc->sc_ctl_l);
481 1.1 plunky if (err)
482 1.1 plunky return err;
483 1.1 plunky
484 1.1 plunky /*
485 1.1 plunky * Listen on interrupt PSM
486 1.1 plunky */
487 1.1 plunky err = l2cap_attach(&sc->sc_int_l, &btmagic_int_proto, sc);
488 1.1 plunky if (err)
489 1.1 plunky return err;
490 1.1 plunky
491 1.1 plunky err = l2cap_setopt(sc->sc_int_l, &sc->sc_mode);
492 1.1 plunky if (err)
493 1.1 plunky return err;
494 1.1 plunky
495 1.1 plunky sa.bt_psm = L2CAP_PSM_HID_INTR;
496 1.1 plunky err = l2cap_bind(sc->sc_int_l, &sa);
497 1.1 plunky if (err)
498 1.1 plunky return err;
499 1.1 plunky
500 1.1 plunky err = l2cap_listen(sc->sc_int_l);
501 1.1 plunky if (err)
502 1.1 plunky return err;
503 1.1 plunky
504 1.1 plunky sc->sc_state = BTMAGIC_WAIT_CTL;
505 1.1 plunky return 0;
506 1.1 plunky }
507 1.1 plunky
508 1.1 plunky /*
509 1.1 plunky * start connecting to our device
510 1.1 plunky *
511 1.1 plunky * bt_lock is held
512 1.1 plunky */
513 1.1 plunky static int
514 1.1 plunky btmagic_connect(struct btmagic_softc *sc)
515 1.1 plunky {
516 1.1 plunky struct sockaddr_bt sa;
517 1.1 plunky int err;
518 1.1 plunky
519 1.1 plunky memset(&sa, 0, sizeof(sa));
520 1.1 plunky sa.bt_len = sizeof(sa);
521 1.1 plunky sa.bt_family = AF_BLUETOOTH;
522 1.1 plunky
523 1.1 plunky err = l2cap_attach(&sc->sc_ctl, &btmagic_ctl_proto, sc);
524 1.1 plunky if (err) {
525 1.1 plunky printf("%s: l2cap_attach failed (%d)\n",
526 1.1 plunky device_xname(sc->sc_dev), err);
527 1.1 plunky return err;
528 1.1 plunky }
529 1.1 plunky
530 1.1 plunky err = l2cap_setopt(sc->sc_ctl, &sc->sc_mode);
531 1.1 plunky if (err)
532 1.1 plunky return err;
533 1.1 plunky
534 1.1 plunky bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
535 1.1 plunky err = l2cap_bind(sc->sc_ctl, &sa);
536 1.1 plunky if (err) {
537 1.1 plunky printf("%s: l2cap_bind failed (%d)\n",
538 1.1 plunky device_xname(sc->sc_dev), err);
539 1.1 plunky return err;
540 1.1 plunky }
541 1.1 plunky
542 1.1 plunky sa.bt_psm = L2CAP_PSM_HID_CNTL;
543 1.1 plunky bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
544 1.1 plunky err = l2cap_connect(sc->sc_ctl, &sa);
545 1.1 plunky if (err) {
546 1.1 plunky printf("%s: l2cap_connect failed (%d)\n",
547 1.1 plunky device_xname(sc->sc_dev), err);
548 1.1 plunky return err;
549 1.1 plunky }
550 1.1 plunky
551 1.1 plunky SET(sc->sc_flags, BTMAGIC_CONNECTING);
552 1.1 plunky sc->sc_state = BTMAGIC_WAIT_CTL;
553 1.1 plunky return 0;
554 1.1 plunky }
555 1.1 plunky
556 1.1 plunky /* validate soft_resolution */
557 1.1 plunky static int
558 1.1 plunky btmagic_sysctl_resolution(SYSCTLFN_ARGS)
559 1.1 plunky {
560 1.1 plunky struct sysctlnode node;
561 1.1 plunky struct btmagic_softc *sc;
562 1.1 plunky int t, error;
563 1.1 plunky
564 1.1 plunky node = *rnode;
565 1.1 plunky sc = node.sysctl_data;
566 1.1 plunky
567 1.1 plunky t = sc->sc_resolution;
568 1.1 plunky node.sysctl_data = &t;
569 1.1 plunky error = sysctl_lookup(SYSCTLFN_CALL(&node));
570 1.1 plunky if (error || newp == NULL)
571 1.1 plunky return error;
572 1.1 plunky
573 1.1 plunky if (t < 100 || t > 4000 || (t / sc->sc_scale) == 0)
574 1.1 plunky return EINVAL;
575 1.1 plunky
576 1.1 plunky sc->sc_resolution = t;
577 1.1 plunky DPRINTF(sc, "sc_resolution = %u", t);
578 1.1 plunky return 0;
579 1.1 plunky }
580 1.1 plunky
581 1.1 plunky /* validate scroll_downscale_factor */
582 1.1 plunky static int
583 1.1 plunky btmagic_sysctl_scale(SYSCTLFN_ARGS)
584 1.1 plunky {
585 1.1 plunky struct sysctlnode node;
586 1.1 plunky struct btmagic_softc *sc;
587 1.1 plunky int t, error;
588 1.1 plunky
589 1.1 plunky node = *rnode;
590 1.1 plunky sc = node.sysctl_data;
591 1.1 plunky
592 1.1 plunky t = sc->sc_scale;
593 1.1 plunky node.sysctl_data = &t;
594 1.1 plunky error = sysctl_lookup(SYSCTLFN_CALL(&node));
595 1.1 plunky if (error || newp == NULL)
596 1.1 plunky return error;
597 1.1 plunky
598 1.1 plunky if (t < 1 || t > 40 || (sc->sc_resolution / t) == 0)
599 1.1 plunky return EINVAL;
600 1.1 plunky
601 1.1 plunky sc->sc_scale = t;
602 1.1 plunky DPRINTF(sc, "sc_scale = %u", t);
603 1.1 plunky return 0;
604 1.1 plunky }
605 1.1 plunky
606 1.1 plunky /*****************************************************************************
607 1.1 plunky *
608 1.1 plunky * wsmouse(4) accessops
609 1.1 plunky */
610 1.1 plunky
611 1.1 plunky static int
612 1.1 plunky btmagic_wsmouse_enable(void *self)
613 1.1 plunky {
614 1.1 plunky struct btmagic_softc *sc = device_private(self);
615 1.1 plunky
616 1.1 plunky if (sc->sc_enabled)
617 1.1 plunky return EBUSY;
618 1.1 plunky
619 1.1 plunky sc->sc_enabled = 1;
620 1.1 plunky DPRINTF(sc, "enable");
621 1.1 plunky return 0;
622 1.1 plunky }
623 1.1 plunky
624 1.1 plunky static int
625 1.1 plunky btmagic_wsmouse_ioctl(void *self, unsigned long cmd, void *data,
626 1.1 plunky int flag, struct lwp *l)
627 1.1 plunky {
628 1.1 plunky /* struct btmagic_softc *sc = device_private(self); */
629 1.1 plunky int err;
630 1.1 plunky
631 1.1 plunky switch (cmd) {
632 1.1 plunky case WSMOUSEIO_GTYPE:
633 1.1 plunky *(uint *)data = WSMOUSE_TYPE_BLUETOOTH;
634 1.1 plunky err = 0;
635 1.1 plunky break;
636 1.1 plunky
637 1.1 plunky default:
638 1.1 plunky err = EPASSTHROUGH;
639 1.1 plunky break;
640 1.1 plunky }
641 1.1 plunky
642 1.1 plunky return err;
643 1.1 plunky }
644 1.1 plunky
645 1.1 plunky static void
646 1.1 plunky btmagic_wsmouse_disable(void *self)
647 1.1 plunky {
648 1.1 plunky struct btmagic_softc *sc = device_private(self);
649 1.1 plunky
650 1.1 plunky DPRINTF(sc, "disable");
651 1.1 plunky sc->sc_enabled = 0;
652 1.1 plunky }
653 1.1 plunky
654 1.1 plunky
655 1.1 plunky /*****************************************************************************
656 1.1 plunky *
657 1.1 plunky * setup routines
658 1.1 plunky */
659 1.1 plunky
660 1.1 plunky static void
661 1.1 plunky btmagic_timeout(void *arg)
662 1.1 plunky {
663 1.1 plunky struct btmagic_softc *sc = arg;
664 1.1 plunky
665 1.1 plunky mutex_enter(bt_lock);
666 1.1 plunky callout_ack(&sc->sc_timeout);
667 1.1 plunky
668 1.1 plunky switch (sc->sc_state) {
669 1.1 plunky case BTMAGIC_CLOSED:
670 1.1 plunky if (sc->sc_int != NULL) {
671 1.1 plunky l2cap_disconnect(sc->sc_int, 0);
672 1.1 plunky break;
673 1.1 plunky }
674 1.1 plunky
675 1.1 plunky if (sc->sc_ctl != NULL) {
676 1.1 plunky l2cap_disconnect(sc->sc_ctl, 0);
677 1.1 plunky break;
678 1.1 plunky }
679 1.1 plunky break;
680 1.1 plunky
681 1.1 plunky case BTMAGIC_OPEN:
682 1.1 plunky if (!ISSET(sc->sc_flags, BTMAGIC_ENABLED)) {
683 1.1 plunky btmagic_enable(sc);
684 1.1 plunky break;
685 1.1 plunky }
686 1.1 plunky
687 1.1 plunky btmagic_check_battery(sc);
688 1.1 plunky break;
689 1.1 plunky
690 1.1 plunky case BTMAGIC_WAIT_CTL:
691 1.1 plunky case BTMAGIC_WAIT_INT:
692 1.1 plunky default:
693 1.1 plunky break;
694 1.1 plunky }
695 1.1 plunky mutex_exit(bt_lock);
696 1.1 plunky }
697 1.1 plunky
698 1.1 plunky /*
699 1.1 plunky * Send report on control channel
700 1.1 plunky *
701 1.1 plunky * bt_lock is held
702 1.1 plunky */
703 1.1 plunky static int
704 1.1 plunky btmagic_ctl_send(struct btmagic_softc *sc, const uint8_t *data, size_t len)
705 1.1 plunky {
706 1.1 plunky struct mbuf *m;
707 1.1 plunky
708 1.1 plunky if (len > MLEN)
709 1.1 plunky return EINVAL;
710 1.1 plunky
711 1.1 plunky m = m_gethdr(M_DONTWAIT, MT_DATA);
712 1.1 plunky if (m == NULL)
713 1.1 plunky return ENOMEM;
714 1.1 plunky
715 1.1 plunky #ifdef BTMAGIC_DEBUG
716 1.1 plunky printf("%s: send", device_xname(sc->sc_dev));
717 1.1 plunky for (size_t i = 0; i < len; i++)
718 1.1 plunky printf(" 0x%02x", data[i]);
719 1.1 plunky printf("\n");
720 1.1 plunky #endif
721 1.1 plunky
722 1.1 plunky memcpy(mtod(m, uint8_t *), data, len);
723 1.1 plunky m->m_pkthdr.len = m->m_len = len;
724 1.1 plunky return l2cap_send(sc->sc_ctl, m);
725 1.1 plunky }
726 1.1 plunky
727 1.1 plunky /*
728 1.1 plunky * Enable touch reports by sending the following report
729 1.1 plunky *
730 1.1 plunky * SET_REPORT(FEATURE, 0xd7) = 0x01
731 1.1 plunky *
732 1.1 plunky * bt_lock is held
733 1.1 plunky */
734 1.1 plunky static void
735 1.1 plunky btmagic_enable(struct btmagic_softc *sc)
736 1.1 plunky {
737 1.1 plunky static const uint8_t rep[] = { 0x53, 0xd7, 0x01 };
738 1.1 plunky
739 1.1 plunky if (btmagic_ctl_send(sc, rep, sizeof(rep)) != 0) {
740 1.1 plunky printf("%s: cannot enable touch reports\n",
741 1.1 plunky device_xname(sc->sc_dev));
742 1.1 plunky
743 1.1 plunky return;
744 1.1 plunky }
745 1.1 plunky
746 1.1 plunky SET(sc->sc_flags, BTMAGIC_ENABLED);
747 1.1 plunky }
748 1.1 plunky
749 1.1 plunky /*
750 1.1 plunky * Request the battery level by sending the following report
751 1.1 plunky *
752 1.1 plunky * GET_REPORT(FEATURE, 0x47)
753 1.1 plunky *
754 1.1 plunky * bt_lock is held
755 1.1 plunky */
756 1.1 plunky static void
757 1.1 plunky btmagic_check_battery(struct btmagic_softc *sc)
758 1.1 plunky {
759 1.1 plunky static const uint8_t rep[] = { 0x43, 0x47 };
760 1.1 plunky
761 1.1 plunky if (btmagic_ctl_send(sc, rep, sizeof(rep)) != 0)
762 1.1 plunky printf("%s: cannot request battery level\n",
763 1.1 plunky device_xname(sc->sc_dev));
764 1.1 plunky }
765 1.1 plunky
766 1.1 plunky /*
767 1.1 plunky * the Magic Mouse has a base resolution of 1300dpi which is rather flighty. We
768 1.1 plunky * scale the output to the requested resolution, taking care to account for the
769 1.1 plunky * remainders to prevent loss of small deltas.
770 1.1 plunky */
771 1.1 plunky static int
772 1.1 plunky btmagic_scale(int delta, int *remainder, int resolution)
773 1.1 plunky {
774 1.1 plunky int new;
775 1.1 plunky
776 1.1 plunky delta += *remainder;
777 1.1 plunky new = delta * resolution / 1300;
778 1.1 plunky *remainder = delta - new * 1300 / resolution;
779 1.1 plunky return new;
780 1.1 plunky }
781 1.1 plunky
782 1.1 plunky
783 1.1 plunky /*****************************************************************************
784 1.1 plunky *
785 1.1 plunky * bluetooth(9) callback methods for L2CAP
786 1.1 plunky *
787 1.1 plunky * All these are called from Bluetooth Protocol code, holding bt_lock.
788 1.1 plunky */
789 1.1 plunky
790 1.1 plunky static void
791 1.1 plunky btmagic_connecting(void *arg)
792 1.1 plunky {
793 1.1 plunky
794 1.1 plunky /* dont care */
795 1.1 plunky }
796 1.1 plunky
797 1.1 plunky static void
798 1.1 plunky btmagic_ctl_connected(void *arg)
799 1.1 plunky {
800 1.1 plunky struct sockaddr_bt sa;
801 1.1 plunky struct btmagic_softc *sc = arg;
802 1.1 plunky int err;
803 1.1 plunky
804 1.1 plunky if (sc->sc_state != BTMAGIC_WAIT_CTL)
805 1.1 plunky return;
806 1.1 plunky
807 1.1 plunky KASSERT(sc->sc_ctl != NULL);
808 1.1 plunky KASSERT(sc->sc_int == NULL);
809 1.1 plunky
810 1.1 plunky if (ISSET(sc->sc_flags, BTMAGIC_CONNECTING)) {
811 1.1 plunky /* initiate connect on interrupt PSM */
812 1.1 plunky err = l2cap_attach(&sc->sc_int, &btmagic_int_proto, sc);
813 1.1 plunky if (err)
814 1.1 plunky goto fail;
815 1.1 plunky
816 1.1 plunky err = l2cap_setopt(sc->sc_int, &sc->sc_mode);
817 1.1 plunky if (err)
818 1.1 plunky goto fail;
819 1.1 plunky
820 1.1 plunky memset(&sa, 0, sizeof(sa));
821 1.1 plunky sa.bt_len = sizeof(sa);
822 1.1 plunky sa.bt_family = AF_BLUETOOTH;
823 1.1 plunky bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
824 1.1 plunky
825 1.1 plunky err = l2cap_bind(sc->sc_int, &sa);
826 1.1 plunky if (err)
827 1.1 plunky goto fail;
828 1.1 plunky
829 1.1 plunky sa.bt_psm = L2CAP_PSM_HID_INTR;
830 1.1 plunky bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
831 1.1 plunky err = l2cap_connect(sc->sc_int, &sa);
832 1.1 plunky if (err)
833 1.1 plunky goto fail;
834 1.1 plunky }
835 1.1 plunky
836 1.1 plunky sc->sc_state = BTMAGIC_WAIT_INT;
837 1.1 plunky return;
838 1.1 plunky
839 1.1 plunky fail:
840 1.1 plunky l2cap_detach(&sc->sc_ctl);
841 1.1 plunky sc->sc_ctl = NULL;
842 1.1 plunky
843 1.1 plunky printf("%s: connect failed (%d)\n", device_xname(sc->sc_dev), err);
844 1.1 plunky }
845 1.1 plunky
846 1.1 plunky static void
847 1.1 plunky btmagic_int_connected(void *arg)
848 1.1 plunky {
849 1.1 plunky struct btmagic_softc *sc = arg;
850 1.1 plunky
851 1.1 plunky if (sc->sc_state != BTMAGIC_WAIT_INT)
852 1.1 plunky return;
853 1.1 plunky
854 1.1 plunky KASSERT(sc->sc_ctl != NULL);
855 1.1 plunky KASSERT(sc->sc_int != NULL);
856 1.1 plunky
857 1.1 plunky printf("%s: connected\n", device_xname(sc->sc_dev));
858 1.1 plunky CLR(sc->sc_flags, BTMAGIC_CONNECTING);
859 1.1 plunky sc->sc_state = BTMAGIC_OPEN;
860 1.1 plunky
861 1.1 plunky /* trigger the setup */
862 1.1 plunky CLR(sc->sc_flags, BTMAGIC_ENABLED);
863 1.1 plunky callout_schedule(&sc->sc_timeout, hz);
864 1.1 plunky }
865 1.1 plunky
866 1.1 plunky /*
867 1.1 plunky * Disconnected
868 1.1 plunky *
869 1.1 plunky * Depending on our state, this could mean several things, but essentially
870 1.1 plunky * we are lost. If both channels are closed, schedule another connection.
871 1.1 plunky */
872 1.1 plunky static void
873 1.1 plunky btmagic_ctl_disconnected(void *arg, int err)
874 1.1 plunky {
875 1.1 plunky struct btmagic_softc *sc = arg;
876 1.1 plunky
877 1.1 plunky if (sc->sc_ctl != NULL) {
878 1.1 plunky l2cap_detach(&sc->sc_ctl);
879 1.1 plunky sc->sc_ctl = NULL;
880 1.1 plunky }
881 1.1 plunky
882 1.1 plunky if (sc->sc_int == NULL) {
883 1.1 plunky printf("%s: disconnected\n", device_xname(sc->sc_dev));
884 1.1 plunky CLR(sc->sc_flags, BTMAGIC_CONNECTING);
885 1.1 plunky sc->sc_state = BTMAGIC_WAIT_CTL;
886 1.1 plunky } else {
887 1.1 plunky /*
888 1.1 plunky * The interrupt channel should have been closed first,
889 1.1 plunky * but its potentially unsafe to detach that from here.
890 1.1 plunky * Give them a second to do the right thing or let the
891 1.1 plunky * callout handle it.
892 1.1 plunky */
893 1.1 plunky sc->sc_state = BTMAGIC_CLOSED;
894 1.1 plunky callout_schedule(&sc->sc_timeout, hz);
895 1.1 plunky }
896 1.1 plunky }
897 1.1 plunky
898 1.1 plunky static void
899 1.1 plunky btmagic_int_disconnected(void *arg, int err)
900 1.1 plunky {
901 1.1 plunky struct btmagic_softc *sc = arg;
902 1.1 plunky
903 1.1 plunky if (sc->sc_int != NULL) {
904 1.1 plunky l2cap_detach(&sc->sc_int);
905 1.1 plunky sc->sc_int = NULL;
906 1.1 plunky }
907 1.1 plunky
908 1.1 plunky if (sc->sc_ctl == NULL) {
909 1.1 plunky printf("%s: disconnected\n", device_xname(sc->sc_dev));
910 1.1 plunky CLR(sc->sc_flags, BTMAGIC_CONNECTING);
911 1.1 plunky sc->sc_state = BTMAGIC_WAIT_CTL;
912 1.1 plunky } else {
913 1.1 plunky /*
914 1.1 plunky * The control channel should be closing also, allow
915 1.1 plunky * them a chance to do that before we force it.
916 1.1 plunky */
917 1.1 plunky sc->sc_state = BTMAGIC_CLOSED;
918 1.1 plunky callout_schedule(&sc->sc_timeout, hz);
919 1.1 plunky }
920 1.1 plunky }
921 1.1 plunky
922 1.1 plunky /*
923 1.1 plunky * New Connections
924 1.1 plunky *
925 1.1 plunky * We give a new L2CAP handle back if this matches the BDADDR we are
926 1.1 plunky * listening for and we are in the right state. btmagic_connected will
927 1.1 plunky * be called when the connection is open, so nothing else to do here
928 1.1 plunky */
929 1.1 plunky static void *
930 1.1 plunky btmagic_ctl_newconn(void *arg, struct sockaddr_bt *laddr,
931 1.1 plunky struct sockaddr_bt *raddr)
932 1.1 plunky {
933 1.1 plunky struct btmagic_softc *sc = arg;
934 1.1 plunky
935 1.1 plunky if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0)
936 1.1 plunky return NULL;
937 1.1 plunky
938 1.1 plunky if (sc->sc_state != BTMAGIC_WAIT_CTL
939 1.1 plunky || ISSET(sc->sc_flags, BTMAGIC_CONNECTING)
940 1.1 plunky || sc->sc_ctl != NULL
941 1.1 plunky || sc->sc_int != NULL) {
942 1.1 plunky DPRINTF(sc, "reject ctl newconn %s%s%s%s",
943 1.1 plunky (sc->sc_state == BTMAGIC_WAIT_CTL) ? " (WAITING)": "",
944 1.1 plunky ISSET(sc->sc_flags, BTMAGIC_CONNECTING) ? " (CONNECTING)" : "",
945 1.1 plunky (sc->sc_ctl != NULL) ? " (GOT CONTROL)" : "",
946 1.1 plunky (sc->sc_int != NULL) ? " (GOT INTERRUPT)" : "");
947 1.1 plunky
948 1.1 plunky return NULL;
949 1.1 plunky }
950 1.1 plunky
951 1.1 plunky l2cap_attach(&sc->sc_ctl, &btmagic_ctl_proto, sc);
952 1.1 plunky return sc->sc_ctl;
953 1.1 plunky }
954 1.1 plunky
955 1.1 plunky static void *
956 1.1 plunky btmagic_int_newconn(void *arg, struct sockaddr_bt *laddr,
957 1.1 plunky struct sockaddr_bt *raddr)
958 1.1 plunky {
959 1.1 plunky struct btmagic_softc *sc = arg;
960 1.1 plunky
961 1.1 plunky if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0)
962 1.1 plunky return NULL;
963 1.1 plunky
964 1.1 plunky if (sc->sc_state != BTMAGIC_WAIT_INT
965 1.1 plunky || ISSET(sc->sc_flags, BTMAGIC_CONNECTING)
966 1.1 plunky || sc->sc_ctl == NULL
967 1.1 plunky || sc->sc_int != NULL) {
968 1.1 plunky DPRINTF(sc, "reject int newconn %s%s%s%s",
969 1.1 plunky (sc->sc_state == BTMAGIC_WAIT_INT) ? " (WAITING)": "",
970 1.1 plunky ISSET(sc->sc_flags, BTMAGIC_CONNECTING) ? " (CONNECTING)" : "",
971 1.1 plunky (sc->sc_ctl == NULL) ? " (NO CONTROL)" : "",
972 1.1 plunky (sc->sc_int != NULL) ? " (GOT INTERRUPT)" : "");
973 1.1 plunky
974 1.1 plunky return NULL;
975 1.1 plunky }
976 1.1 plunky
977 1.1 plunky l2cap_attach(&sc->sc_int, &btmagic_int_proto, sc);
978 1.1 plunky return sc->sc_int;
979 1.1 plunky }
980 1.1 plunky
981 1.1 plunky static void
982 1.1 plunky btmagic_complete(void *arg, int count)
983 1.1 plunky {
984 1.1 plunky
985 1.1 plunky /* dont care */
986 1.1 plunky }
987 1.1 plunky
988 1.1 plunky static void
989 1.1 plunky btmagic_linkmode(void *arg, int new)
990 1.1 plunky {
991 1.1 plunky struct btmagic_softc *sc = arg;
992 1.1 plunky int mode;
993 1.1 plunky
994 1.1 plunky (void)sockopt_getint(&sc->sc_mode, &mode);
995 1.1 plunky
996 1.1 plunky if (ISSET(mode, L2CAP_LM_AUTH) && !ISSET(new, L2CAP_LM_AUTH))
997 1.1 plunky printf("%s: auth failed\n", device_xname(sc->sc_dev));
998 1.1 plunky else if (ISSET(mode, L2CAP_LM_ENCRYPT) && !ISSET(new, L2CAP_LM_ENCRYPT))
999 1.1 plunky printf("%s: encrypt off\n", device_xname(sc->sc_dev));
1000 1.1 plunky else if (ISSET(mode, L2CAP_LM_SECURE) && !ISSET(new, L2CAP_LM_SECURE))
1001 1.1 plunky printf("%s: insecure\n", device_xname(sc->sc_dev));
1002 1.1 plunky else
1003 1.1 plunky return;
1004 1.1 plunky
1005 1.1 plunky if (sc->sc_int != NULL)
1006 1.1 plunky l2cap_disconnect(sc->sc_int, 0);
1007 1.1 plunky
1008 1.1 plunky if (sc->sc_ctl != NULL)
1009 1.1 plunky l2cap_disconnect(sc->sc_ctl, 0);
1010 1.1 plunky }
1011 1.1 plunky
1012 1.1 plunky /*
1013 1.1 plunky * Receive transaction from the mouse. We don't differentiate between
1014 1.1 plunky * interrupt and control channel here, there is no need.
1015 1.1 plunky */
1016 1.1 plunky static void
1017 1.1 plunky btmagic_input(void *arg, struct mbuf *m)
1018 1.1 plunky {
1019 1.1 plunky struct btmagic_softc *sc = arg;
1020 1.1 plunky uint8_t *data;
1021 1.1 plunky size_t len;
1022 1.1 plunky
1023 1.1 plunky if (sc->sc_state != BTMAGIC_OPEN
1024 1.1 plunky || sc->sc_wsmouse == NULL
1025 1.1 plunky || sc->sc_enabled == 0)
1026 1.1 plunky goto release;
1027 1.1 plunky
1028 1.1 plunky if (m->m_pkthdr.len > m->m_len)
1029 1.1 plunky printf("%s: truncating input\n", device_xname(sc->sc_dev));
1030 1.1 plunky
1031 1.1 plunky data = mtod(m, uint8_t *);
1032 1.1 plunky len = m->m_len;
1033 1.1 plunky
1034 1.1 plunky if (len < 1)
1035 1.1 plunky goto release;
1036 1.1 plunky
1037 1.1 plunky switch (BTHID_TYPE(data[0])) {
1038 1.1 plunky case BTHID_HANDSHAKE:
1039 1.1 plunky DPRINTF(sc, "Handshake: 0x%x", BTHID_HANDSHAKE_PARAM(data[0]));
1040 1.1 plunky callout_schedule(&sc->sc_timeout, hz);
1041 1.1 plunky break;
1042 1.1 plunky
1043 1.1 plunky case BTHID_DATA:
1044 1.1 plunky if (len < 2)
1045 1.1 plunky break;
1046 1.1 plunky
1047 1.1 plunky switch (data[1]) {
1048 1.1 plunky case 0x10: /* Basic mouse (input) */
1049 1.1 plunky btmagic_input_basic(sc, data + 2, len - 2);
1050 1.1 plunky break;
1051 1.1 plunky
1052 1.1 plunky case 0x29: /* Magic touch (input) */
1053 1.1 plunky btmagic_input_magic(sc, data + 2, len - 2);
1054 1.1 plunky break;
1055 1.1 plunky
1056 1.1 plunky case 0x30: /* Battery status (input) */
1057 1.1 plunky if (len != 3)
1058 1.1 plunky break;
1059 1.1 plunky
1060 1.1 plunky printf("%s: Battery ", device_xname(sc->sc_dev));
1061 1.1 plunky switch (data[2]) {
1062 1.1 plunky case 0: printf("Ok\n"); break;
1063 1.1 plunky case 1: printf("Warning\n"); break;
1064 1.1 plunky case 2: printf("Critical\n"); break;
1065 1.1 plunky default: printf("0x%02x\n", data[2]); break;
1066 1.1 plunky }
1067 1.1 plunky break;
1068 1.1 plunky
1069 1.1 plunky case 0x47: /* Battery strength (feature) */
1070 1.1 plunky if (len != 3)
1071 1.1 plunky break;
1072 1.1 plunky
1073 1.1 plunky printf("%s: Battery %d%%\n", device_xname(sc->sc_dev),
1074 1.1 plunky data[2]);
1075 1.1 plunky break;
1076 1.1 plunky
1077 1.1 plunky case 0x61: /* Surface detection (input) */
1078 1.1 plunky if (len != 3)
1079 1.1 plunky break;
1080 1.1 plunky
1081 1.1 plunky DPRINTF(sc, "Mouse %s",
1082 1.1 plunky (data[2] == 0 ? "lowered" : "raised"));
1083 1.1 plunky break;
1084 1.1 plunky
1085 1.1 plunky case 0x60: /* unknown (input) */
1086 1.1 plunky case 0xf0: /* unknown (feature) */
1087 1.1 plunky case 0xf1: /* unknown (feature) */
1088 1.1 plunky default:
1089 1.1 plunky #if BTMAGIC_DEBUG
1090 1.1 plunky printf("%s: recv", device_xname(sc->sc_dev));
1091 1.1 plunky for (size_t i = 0; i < len; i++)
1092 1.1 plunky printf(" 0x%02x", data[i]);
1093 1.1 plunky printf("\n");
1094 1.1 plunky #endif
1095 1.1 plunky break;
1096 1.1 plunky }
1097 1.1 plunky break;
1098 1.1 plunky
1099 1.1 plunky default:
1100 1.1 plunky DPRINTF(sc, "transaction (type 0x%x)", BTHID_TYPE(data[0]));
1101 1.1 plunky break;
1102 1.1 plunky }
1103 1.1 plunky
1104 1.1 plunky release:
1105 1.1 plunky m_freem(m);
1106 1.1 plunky }
1107 1.1 plunky
1108 1.1 plunky /*
1109 1.1 plunky * parse the Basic report (0x10), which according to the provided
1110 1.1 plunky * HID descriptor is in the following format
1111 1.1 plunky *
1112 1.1 plunky * button 1 1-bit
1113 1.1 plunky * button 2 1-bit
1114 1.1 plunky * padding 6-bits
1115 1.1 plunky * dX 16-bits (signed)
1116 1.1 plunky * dY 16-bits (signed)
1117 1.1 plunky *
1118 1.1 plunky * Even when the magic touch reports are enabled, the basic report is
1119 1.1 plunky * sent for mouse move events where no touches are detected.
1120 1.1 plunky */
1121 1.1 plunky static const struct {
1122 1.1 plunky struct hid_location button1;
1123 1.1 plunky struct hid_location button2;
1124 1.1 plunky struct hid_location dX;
1125 1.1 plunky struct hid_location dY;
1126 1.1 plunky } basic = {
1127 1.1 plunky .button1 = { .pos = 0, .size = 1 },
1128 1.1 plunky .button2 = { .pos = 1, .size = 1 },
1129 1.1 plunky .dX = { .pos = 8, .size = 16 },
1130 1.1 plunky .dY = { .pos = 24, .size = 16 },
1131 1.1 plunky };
1132 1.1 plunky
1133 1.1 plunky static void
1134 1.1 plunky btmagic_input_basic(struct btmagic_softc *sc, uint8_t *data, size_t len)
1135 1.1 plunky {
1136 1.1 plunky int dx, dy;
1137 1.1 plunky uint32_t mb;
1138 1.1 plunky int s;
1139 1.1 plunky
1140 1.1 plunky if (len != 5)
1141 1.1 plunky return;
1142 1.1 plunky
1143 1.1 plunky dx = hid_get_data(data, &basic.dX);
1144 1.1 plunky dx = btmagic_scale(dx, &sc->sc_rx, sc->sc_resolution);
1145 1.1 plunky
1146 1.1 plunky dy = hid_get_data(data, &basic.dY);
1147 1.1 plunky dy = btmagic_scale(dy, &sc->sc_ry, sc->sc_resolution);
1148 1.1 plunky
1149 1.1 plunky mb = 0;
1150 1.1 plunky if (hid_get_udata(data, &basic.button1))
1151 1.1 plunky mb |= __BIT(0);
1152 1.1 plunky if (hid_get_udata(data, &basic.button2))
1153 1.1 plunky mb |= __BIT(2);
1154 1.1 plunky
1155 1.1 plunky if (dx != 0 || dy != 0 || mb != sc->sc_mb) {
1156 1.1 plunky sc->sc_mb = mb;
1157 1.1 plunky
1158 1.1 plunky s = spltty();
1159 1.1 plunky wsmouse_input(sc->sc_wsmouse, mb,
1160 1.1 plunky dx, -dy, 0, 0, WSMOUSE_INPUT_DELTA);
1161 1.1 plunky splx(s);
1162 1.1 plunky }
1163 1.1 plunky }
1164 1.1 plunky
1165 1.1 plunky /*
1166 1.1 plunky * the Magic touch report (0x29), according to the Linux driver
1167 1.1 plunky * written by Michael Poole, is variable length starting with the
1168 1.1 plunky * fixed 40-bit header
1169 1.1 plunky *
1170 1.1 plunky * dX lsb 8-bits (signed)
1171 1.1 plunky * dY lsb 8-bits (signed)
1172 1.1 plunky * button 1 1-bit
1173 1.1 plunky * button 2 1-bit
1174 1.1 plunky * dX msb 2-bits (signed)
1175 1.1 plunky * dY msb 2-bits (signed)
1176 1.1 plunky * timestamp 18-bits
1177 1.1 plunky *
1178 1.1 plunky * followed by (up to 5?) touch reports of 64-bits each
1179 1.1 plunky *
1180 1.1 plunky * abs W 12-bits (signed)
1181 1.1 plunky * abs Z 12-bits (signed)
1182 1.1 plunky * axis major 8-bits
1183 1.1 plunky * axis minor 8-bits
1184 1.1 plunky * pressure 6-bits
1185 1.1 plunky * id 4-bits
1186 1.1 plunky * angle 6-bits (from E(0)->N(32)->W(64))
1187 1.1 plunky * unknown 4-bits
1188 1.1 plunky * phase 4-bits
1189 1.1 plunky */
1190 1.1 plunky
1191 1.1 plunky static const struct {
1192 1.1 plunky struct hid_location dXl;
1193 1.1 plunky struct hid_location dYl;
1194 1.1 plunky struct hid_location button1;
1195 1.1 plunky struct hid_location button2;
1196 1.1 plunky struct hid_location dXm;
1197 1.1 plunky struct hid_location dYm;
1198 1.1 plunky struct hid_location timestamp;
1199 1.1 plunky } magic = {
1200 1.1 plunky .dXl = { .pos = 0, .size = 8 },
1201 1.1 plunky .dYl = { .pos = 8, .size = 8 },
1202 1.1 plunky .button1 = { .pos = 16, .size = 1 },
1203 1.1 plunky .button2 = { .pos = 17, .size = 1 },
1204 1.1 plunky .dXm = { .pos = 18, .size = 2 },
1205 1.1 plunky .dYm = { .pos = 20, .size = 2 },
1206 1.1 plunky .timestamp = { .pos = 22, .size = 18 },
1207 1.1 plunky };
1208 1.1 plunky
1209 1.1 plunky static const struct {
1210 1.1 plunky struct hid_location aW;
1211 1.1 plunky struct hid_location aZ;
1212 1.1 plunky struct hid_location major;
1213 1.1 plunky struct hid_location minor;
1214 1.1 plunky struct hid_location pressure;
1215 1.1 plunky struct hid_location id;
1216 1.1 plunky struct hid_location angle;
1217 1.1 plunky struct hid_location unknown;
1218 1.1 plunky struct hid_location phase;
1219 1.1 plunky } touch = {
1220 1.1 plunky .aW = { .pos = 0, .size = 12 },
1221 1.1 plunky .aZ = { .pos = 12, .size = 12 },
1222 1.1 plunky .major = { .pos = 24, .size = 8 },
1223 1.1 plunky .minor = { .pos = 32, .size = 8 },
1224 1.1 plunky .pressure = { .pos = 40, .size = 6 },
1225 1.1 plunky .id = { .pos = 46, .size = 4 },
1226 1.1 plunky .angle = { .pos = 50, .size = 6 },
1227 1.1 plunky .unknown = { .pos = 56, .size = 4 },
1228 1.1 plunky .phase = { .pos = 60, .size = 4 },
1229 1.1 plunky };
1230 1.1 plunky
1231 1.1 plunky /*
1232 1.1 plunky * the phase of the touch starts at 0x01 as the finger is first detected
1233 1.1 plunky * approaching the mouse, increasing to 0x04 while the finger is touching,
1234 1.1 plunky * then increases towards 0x07 as the finger is lifted, and we get 0x00
1235 1.1 plunky * when the touch is cancelled. The values below seem to be produced for
1236 1.1 plunky * every touch, the others less consistently depending on how fast the
1237 1.1 plunky * approach or departure is.
1238 1.1 plunky *
1239 1.1 plunky * In fact we ignore touches unless they are in the steady 0x04 phase.
1240 1.1 plunky */
1241 1.1 plunky #define BTMAGIC_PHASE_START 0x3
1242 1.1 plunky #define BTMAGIC_PHASE_CONT 0x4
1243 1.1 plunky #define BTMAGIC_PHASE_END 0x7
1244 1.1 plunky #define BTMAGIC_PHASE_CANCEL 0x0
1245 1.1 plunky
1246 1.1 plunky static void
1247 1.1 plunky btmagic_input_magic(struct btmagic_softc *sc, uint8_t *data, size_t len)
1248 1.1 plunky {
1249 1.1 plunky uint32_t mb;
1250 1.1 plunky int dx, dy, dz, dw;
1251 1.1 plunky int id, nf, az, aw, tz, tw;
1252 1.1 plunky int s;
1253 1.1 plunky
1254 1.1 plunky if (((len - 5) % 8) != 0)
1255 1.1 plunky return;
1256 1.1 plunky
1257 1.1 plunky dx = (hid_get_data(data, &magic.dXm) << 8)
1258 1.1 plunky | (hid_get_data(data, &magic.dXl) & 0xff);
1259 1.1 plunky dx = btmagic_scale(dx, &sc->sc_rx, sc->sc_resolution);
1260 1.1 plunky
1261 1.1 plunky dy = (hid_get_data(data, &magic.dYm) << 8)
1262 1.1 plunky | (hid_get_data(data, &magic.dYl) & 0xff);
1263 1.1 plunky dy = btmagic_scale(dy, &sc->sc_ry, sc->sc_resolution);
1264 1.1 plunky
1265 1.1 plunky mb = 0;
1266 1.1 plunky if (hid_get_udata(data, &magic.button1))
1267 1.1 plunky mb |= __BIT(0);
1268 1.1 plunky if (hid_get_udata(data, &magic.button2))
1269 1.1 plunky mb |= __BIT(2);
1270 1.1 plunky
1271 1.1 plunky nf = 0;
1272 1.1 plunky dz = 0;
1273 1.1 plunky dw = 0;
1274 1.1 plunky len = (len - 5) / 8;
1275 1.1 plunky for (data += 5; len-- > 0; data += 8) {
1276 1.1 plunky id = hid_get_udata(data, &touch.id);
1277 1.1 plunky az = hid_get_data(data, &touch.aZ);
1278 1.1 plunky aw = hid_get_data(data, &touch.aW);
1279 1.1 plunky
1280 1.1 plunky /*
1281 1.1 plunky * scrolling is triggered by an established touch moving
1282 1.1 plunky * beyond a minimum distance from its start point and is
1283 1.1 plunky * cancelled as the touch starts to fade.
1284 1.1 plunky *
1285 1.1 plunky * Multiple touches may be scrolling simultaneously, the
1286 1.1 plunky * effect is cumulative.
1287 1.1 plunky */
1288 1.1 plunky
1289 1.1 plunky switch (hid_get_udata(data, &touch.phase)) {
1290 1.1 plunky case BTMAGIC_PHASE_CONT:
1291 1.1 plunky tz = az - sc->sc_az[id];
1292 1.1 plunky tw = aw - sc->sc_aw[id];
1293 1.1 plunky
1294 1.1 plunky if (ISSET(sc->sc_smask, id)) {
1295 1.1 plunky /* scrolling finger */
1296 1.1 plunky dz += btmagic_scale(tz, &sc->sc_rz,
1297 1.1 plunky sc->sc_resolution / sc->sc_scale);
1298 1.1 plunky dw += btmagic_scale(tw, &sc->sc_rw,
1299 1.1 plunky sc->sc_resolution / sc->sc_scale);
1300 1.1 plunky } else if (abs(tz) > sc->sc_dist
1301 1.1 plunky || abs(tw) > sc->sc_dist) {
1302 1.1 plunky /* new scrolling finger */
1303 1.1 plunky if (sc->sc_smask == 0) {
1304 1.1 plunky sc->sc_rz = 0;
1305 1.1 plunky sc->sc_rw = 0;
1306 1.1 plunky }
1307 1.1 plunky
1308 1.1 plunky SET(sc->sc_smask, id);
1309 1.1 plunky } else {
1310 1.1 plunky /* not scrolling finger */
1311 1.1 plunky az = sc->sc_az[id];
1312 1.1 plunky aw = sc->sc_aw[id];
1313 1.1 plunky }
1314 1.1 plunky
1315 1.1 plunky /* count firm touches for middle-click */
1316 1.1 plunky if (hid_get_udata(data, &touch.pressure) > sc->sc_firm)
1317 1.1 plunky nf++;
1318 1.1 plunky
1319 1.1 plunky break;
1320 1.1 plunky
1321 1.1 plunky default:
1322 1.1 plunky CLR(sc->sc_smask, id);
1323 1.1 plunky break;
1324 1.1 plunky }
1325 1.1 plunky
1326 1.1 plunky sc->sc_az[id] = az;
1327 1.1 plunky sc->sc_aw[id] = aw;
1328 1.1 plunky }
1329 1.1 plunky
1330 1.1 plunky /*
1331 1.1 plunky * The mouse only has one click detector, and says left or right but
1332 1.1 plunky * never both. We convert multiple firm touches while clicking into
1333 1.1 plunky * a middle button press, and cancel any scroll effects while click
1334 1.1 plunky * is active.
1335 1.1 plunky */
1336 1.1 plunky if (mb != 0) {
1337 1.1 plunky if (sc->sc_mb != 0)
1338 1.1 plunky mb = sc->sc_mb;
1339 1.1 plunky else if (nf > 1)
1340 1.1 plunky mb = __BIT(1);
1341 1.1 plunky
1342 1.1 plunky sc->sc_smask = 0;
1343 1.1 plunky dz = 0;
1344 1.1 plunky dw = 0;
1345 1.1 plunky }
1346 1.1 plunky
1347 1.1 plunky if (dx != 0 || dy != 0 || dz != 0 || dw != 0 || mb != sc->sc_mb) {
1348 1.1 plunky sc->sc_mb = mb;
1349 1.1 plunky
1350 1.1 plunky s = spltty();
1351 1.1 plunky wsmouse_input(sc->sc_wsmouse, mb,
1352 1.1 plunky dx, -dy, -dz, dw, WSMOUSE_INPUT_DELTA);
1353 1.1 plunky splx(s);
1354 1.1 plunky }
1355 1.1 plunky }
1356