bthidev.c revision 1.23 1 1.23 plunky /* $NetBSD: bthidev.c,v 1.23 2012/12/20 11:13:53 plunky Exp $ */
2 1.1 gdamore
3 1.1 gdamore /*-
4 1.1 gdamore * Copyright (c) 2006 Itronix Inc.
5 1.1 gdamore * All rights reserved.
6 1.1 gdamore *
7 1.1 gdamore * Written by Iain Hibbert for Itronix Inc.
8 1.1 gdamore *
9 1.1 gdamore * Redistribution and use in source and binary forms, with or without
10 1.1 gdamore * modification, are permitted provided that the following conditions
11 1.1 gdamore * are met:
12 1.1 gdamore * 1. Redistributions of source code must retain the above copyright
13 1.1 gdamore * notice, this list of conditions and the following disclaimer.
14 1.1 gdamore * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 gdamore * notice, this list of conditions and the following disclaimer in the
16 1.1 gdamore * documentation and/or other materials provided with the distribution.
17 1.1 gdamore * 3. The name of Itronix Inc. may not be used to endorse
18 1.1 gdamore * or promote products derived from this software without specific
19 1.1 gdamore * prior written permission.
20 1.1 gdamore *
21 1.1 gdamore * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22 1.1 gdamore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 1.1 gdamore * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.1 gdamore * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25 1.1 gdamore * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 1.1 gdamore * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 1.1 gdamore * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 1.1 gdamore * ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1 gdamore * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1 gdamore * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.1 gdamore * POSSIBILITY OF SUCH DAMAGE.
32 1.1 gdamore */
33 1.1 gdamore
34 1.1 gdamore #include <sys/cdefs.h>
35 1.23 plunky __KERNEL_RCSID(0, "$NetBSD: bthidev.c,v 1.23 2012/12/20 11:13:53 plunky Exp $");
36 1.1 gdamore
37 1.1 gdamore #include <sys/param.h>
38 1.21 plunky #include <sys/condvar.h>
39 1.1 gdamore #include <sys/conf.h>
40 1.1 gdamore #include <sys/device.h>
41 1.1 gdamore #include <sys/fcntl.h>
42 1.1 gdamore #include <sys/kernel.h>
43 1.21 plunky #include <sys/kthread.h>
44 1.1 gdamore #include <sys/queue.h>
45 1.1 gdamore #include <sys/malloc.h>
46 1.1 gdamore #include <sys/mbuf.h>
47 1.21 plunky #include <sys/mutex.h>
48 1.1 gdamore #include <sys/proc.h>
49 1.16 plunky #include <sys/socketvar.h>
50 1.1 gdamore #include <sys/systm.h>
51 1.1 gdamore
52 1.2 tron #include <prop/proplib.h>
53 1.2 tron
54 1.1 gdamore #include <netbt/bluetooth.h>
55 1.1 gdamore #include <netbt/l2cap.h>
56 1.1 gdamore
57 1.1 gdamore #include <dev/usb/hid.h>
58 1.1 gdamore #include <dev/bluetooth/btdev.h>
59 1.1 gdamore #include <dev/bluetooth/bthid.h>
60 1.1 gdamore #include <dev/bluetooth/bthidev.h>
61 1.1 gdamore
62 1.1 gdamore #include "locators.h"
63 1.1 gdamore
64 1.1 gdamore /*****************************************************************************
65 1.1 gdamore *
66 1.1 gdamore * Bluetooth HID device
67 1.1 gdamore */
68 1.1 gdamore
69 1.1 gdamore #define MAX_DESCRIPTOR_LEN 1024 /* sanity check */
70 1.1 gdamore
71 1.1 gdamore /* bthidev softc */
72 1.1 gdamore struct bthidev_softc {
73 1.1 gdamore uint16_t sc_state;
74 1.1 gdamore uint16_t sc_flags;
75 1.12 plunky device_t sc_dev;
76 1.1 gdamore
77 1.1 gdamore bdaddr_t sc_laddr; /* local address */
78 1.1 gdamore bdaddr_t sc_raddr; /* remote address */
79 1.16 plunky struct sockopt sc_mode; /* link mode sockopt */
80 1.1 gdamore
81 1.1 gdamore uint16_t sc_ctlpsm; /* control PSM */
82 1.1 gdamore struct l2cap_channel *sc_ctl; /* control channel */
83 1.1 gdamore struct l2cap_channel *sc_ctl_l; /* control listen */
84 1.1 gdamore
85 1.1 gdamore uint16_t sc_intpsm; /* interrupt PSM */
86 1.1 gdamore struct l2cap_channel *sc_int; /* interrupt channel */
87 1.1 gdamore struct l2cap_channel *sc_int_l; /* interrupt listen */
88 1.1 gdamore
89 1.21 plunky MBUFQ_HEAD() sc_inq; /* input queue */
90 1.21 plunky kmutex_t sc_lock; /* input queue lock */
91 1.21 plunky kcondvar_t sc_cv; /* input queue trigger */
92 1.21 plunky lwp_t *sc_lwp; /* input queue processor */
93 1.21 plunky int sc_detach;
94 1.21 plunky
95 1.1 gdamore LIST_HEAD(,bthidev) sc_list; /* child list */
96 1.1 gdamore
97 1.11 plunky callout_t sc_reconnect;
98 1.1 gdamore int sc_attempts; /* connection attempts */
99 1.1 gdamore };
100 1.1 gdamore
101 1.2 tron /* sc_flags */
102 1.2 tron #define BTHID_RECONNECT (1 << 0) /* reconnect on link loss */
103 1.2 tron #define BTHID_CONNECTING (1 << 1) /* we are connecting */
104 1.2 tron
105 1.1 gdamore /* device state */
106 1.1 gdamore #define BTHID_CLOSED 0
107 1.1 gdamore #define BTHID_WAIT_CTL 1
108 1.1 gdamore #define BTHID_WAIT_INT 2
109 1.1 gdamore #define BTHID_OPEN 3
110 1.1 gdamore
111 1.1 gdamore #define BTHID_RETRY_INTERVAL 5 /* seconds between connection attempts */
112 1.1 gdamore
113 1.1 gdamore /* bthidev internals */
114 1.1 gdamore static void bthidev_timeout(void *);
115 1.1 gdamore static int bthidev_listen(struct bthidev_softc *);
116 1.1 gdamore static int bthidev_connect(struct bthidev_softc *);
117 1.1 gdamore static int bthidev_output(struct bthidev *, uint8_t *, int);
118 1.1 gdamore static void bthidev_null(struct bthidev *, uint8_t *, int);
119 1.21 plunky static void bthidev_process(void *);
120 1.21 plunky static void bthidev_process_one(struct bthidev_softc *, struct mbuf *);
121 1.1 gdamore
122 1.1 gdamore /* autoconf(9) glue */
123 1.17 cegger static int bthidev_match(device_t, cfdata_t, void *);
124 1.10 plunky static void bthidev_attach(device_t, device_t, void *);
125 1.10 plunky static int bthidev_detach(device_t, int);
126 1.1 gdamore static int bthidev_print(void *, const char *);
127 1.1 gdamore
128 1.10 plunky CFATTACH_DECL_NEW(bthidev, sizeof(struct bthidev_softc),
129 1.1 gdamore bthidev_match, bthidev_attach, bthidev_detach, NULL);
130 1.1 gdamore
131 1.1 gdamore /* bluetooth(9) protocol methods for L2CAP */
132 1.1 gdamore static void bthidev_connecting(void *);
133 1.1 gdamore static void bthidev_ctl_connected(void *);
134 1.1 gdamore static void bthidev_int_connected(void *);
135 1.1 gdamore static void bthidev_ctl_disconnected(void *, int);
136 1.1 gdamore static void bthidev_int_disconnected(void *, int);
137 1.1 gdamore static void *bthidev_ctl_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
138 1.1 gdamore static void *bthidev_int_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
139 1.1 gdamore static void bthidev_complete(void *, int);
140 1.8 plunky static void bthidev_linkmode(void *, int);
141 1.1 gdamore static void bthidev_input(void *, struct mbuf *);
142 1.1 gdamore
143 1.1 gdamore static const struct btproto bthidev_ctl_proto = {
144 1.1 gdamore bthidev_connecting,
145 1.1 gdamore bthidev_ctl_connected,
146 1.1 gdamore bthidev_ctl_disconnected,
147 1.1 gdamore bthidev_ctl_newconn,
148 1.1 gdamore bthidev_complete,
149 1.8 plunky bthidev_linkmode,
150 1.1 gdamore bthidev_input,
151 1.1 gdamore };
152 1.1 gdamore
153 1.1 gdamore static const struct btproto bthidev_int_proto = {
154 1.1 gdamore bthidev_connecting,
155 1.1 gdamore bthidev_int_connected,
156 1.1 gdamore bthidev_int_disconnected,
157 1.1 gdamore bthidev_int_newconn,
158 1.1 gdamore bthidev_complete,
159 1.8 plunky bthidev_linkmode,
160 1.1 gdamore bthidev_input,
161 1.1 gdamore };
162 1.1 gdamore
163 1.1 gdamore /*****************************************************************************
164 1.1 gdamore *
165 1.1 gdamore * bthidev autoconf(9) routines
166 1.1 gdamore */
167 1.1 gdamore
168 1.1 gdamore static int
169 1.17 cegger bthidev_match(device_t self, cfdata_t cfdata, void *aux)
170 1.1 gdamore {
171 1.2 tron prop_dictionary_t dict = aux;
172 1.2 tron prop_object_t obj;
173 1.1 gdamore
174 1.5 plunky obj = prop_dictionary_get(dict, BTDEVservice);
175 1.5 plunky if (prop_string_equals_cstring(obj, "HID"))
176 1.5 plunky return 1;
177 1.5 plunky
178 1.5 plunky return 0;
179 1.1 gdamore }
180 1.1 gdamore
181 1.1 gdamore static void
182 1.10 plunky bthidev_attach(device_t parent, device_t self, void *aux)
183 1.1 gdamore {
184 1.10 plunky struct bthidev_softc *sc = device_private(self);
185 1.2 tron prop_dictionary_t dict = aux;
186 1.2 tron prop_object_t obj;
187 1.10 plunky device_t dev;
188 1.1 gdamore struct bthidev_attach_args bha;
189 1.10 plunky struct bthidev *hidev;
190 1.1 gdamore struct hid_data *d;
191 1.1 gdamore struct hid_item h;
192 1.2 tron const void *desc;
193 1.1 gdamore int locs[BTHIDBUSCF_NLOCS];
194 1.15 ad int maxid, rep, dlen;
195 1.19 plunky int vendor, product;
196 1.1 gdamore
197 1.1 gdamore /*
198 1.1 gdamore * Init softc
199 1.1 gdamore */
200 1.12 plunky sc->sc_dev = self;
201 1.1 gdamore LIST_INIT(&sc->sc_list);
202 1.21 plunky MBUFQ_INIT(&sc->sc_inq);
203 1.9 ad callout_init(&sc->sc_reconnect, 0);
204 1.1 gdamore callout_setfunc(&sc->sc_reconnect, bthidev_timeout, sc);
205 1.1 gdamore sc->sc_state = BTHID_CLOSED;
206 1.2 tron sc->sc_flags = BTHID_CONNECTING;
207 1.2 tron sc->sc_ctlpsm = L2CAP_PSM_HID_CNTL;
208 1.2 tron sc->sc_intpsm = L2CAP_PSM_HID_INTR;
209 1.1 gdamore
210 1.16 plunky sockopt_init(&sc->sc_mode, BTPROTO_L2CAP, SO_L2CAP_LM, 0);
211 1.21 plunky mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
212 1.21 plunky cv_init(&sc->sc_cv, device_xname(self));
213 1.16 plunky
214 1.1 gdamore /*
215 1.2 tron * extract config from proplist
216 1.1 gdamore */
217 1.3 plunky obj = prop_dictionary_get(dict, BTDEVladdr);
218 1.2 tron bdaddr_copy(&sc->sc_laddr, prop_data_data_nocopy(obj));
219 1.2 tron
220 1.3 plunky obj = prop_dictionary_get(dict, BTDEVraddr);
221 1.2 tron bdaddr_copy(&sc->sc_raddr, prop_data_data_nocopy(obj));
222 1.1 gdamore
223 1.19 plunky obj = prop_dictionary_get(dict, BTDEVvendor);
224 1.19 plunky vendor = (int)prop_number_integer_value(obj);
225 1.19 plunky
226 1.19 plunky obj = prop_dictionary_get(dict, BTDEVproduct);
227 1.19 plunky product = (int)prop_number_integer_value(obj);
228 1.19 plunky
229 1.8 plunky obj = prop_dictionary_get(dict, BTDEVmode);
230 1.8 plunky if (prop_object_type(obj) == PROP_TYPE_STRING) {
231 1.8 plunky if (prop_string_equals_cstring(obj, BTDEVauth))
232 1.16 plunky sockopt_setint(&sc->sc_mode, L2CAP_LM_AUTH);
233 1.8 plunky else if (prop_string_equals_cstring(obj, BTDEVencrypt))
234 1.16 plunky sockopt_setint(&sc->sc_mode, L2CAP_LM_ENCRYPT);
235 1.8 plunky else if (prop_string_equals_cstring(obj, BTDEVsecure))
236 1.16 plunky sockopt_setint(&sc->sc_mode, L2CAP_LM_SECURE);
237 1.8 plunky else {
238 1.8 plunky aprint_error(" unknown %s\n", BTDEVmode);
239 1.8 plunky return;
240 1.8 plunky }
241 1.8 plunky
242 1.8 plunky aprint_verbose(" %s %s", BTDEVmode,
243 1.8 plunky prop_string_cstring_nocopy(obj));
244 1.23 plunky } else
245 1.23 plunky sockopt_setint(&sc->sc_mode, 0);
246 1.8 plunky
247 1.3 plunky obj = prop_dictionary_get(dict, BTHIDEVcontrolpsm);
248 1.3 plunky if (prop_object_type(obj) == PROP_TYPE_NUMBER) {
249 1.2 tron sc->sc_ctlpsm = prop_number_integer_value(obj);
250 1.2 tron if (L2CAP_PSM_INVALID(sc->sc_ctlpsm)) {
251 1.3 plunky aprint_error(" invalid %s\n", BTHIDEVcontrolpsm);
252 1.2 tron return;
253 1.2 tron }
254 1.2 tron }
255 1.1 gdamore
256 1.3 plunky obj = prop_dictionary_get(dict, BTHIDEVinterruptpsm);
257 1.3 plunky if (prop_object_type(obj) == PROP_TYPE_NUMBER) {
258 1.2 tron sc->sc_intpsm = prop_number_integer_value(obj);
259 1.2 tron if (L2CAP_PSM_INVALID(sc->sc_intpsm)) {
260 1.3 plunky aprint_error(" invalid %s\n", BTHIDEVinterruptpsm);
261 1.2 tron return;
262 1.2 tron }
263 1.2 tron }
264 1.2 tron
265 1.3 plunky obj = prop_dictionary_get(dict, BTHIDEVdescriptor);
266 1.3 plunky if (prop_object_type(obj) == PROP_TYPE_DATA) {
267 1.2 tron dlen = prop_data_size(obj);
268 1.2 tron desc = prop_data_data_nocopy(obj);
269 1.2 tron } else {
270 1.3 plunky aprint_error(" no %s\n", BTHIDEVdescriptor);
271 1.1 gdamore return;
272 1.1 gdamore }
273 1.2 tron
274 1.3 plunky obj = prop_dictionary_get(dict, BTHIDEVreconnect);
275 1.3 plunky if (prop_object_type(obj) == PROP_TYPE_BOOL
276 1.2 tron && !prop_bool_true(obj))
277 1.2 tron sc->sc_flags |= BTHID_RECONNECT;
278 1.1 gdamore
279 1.1 gdamore /*
280 1.1 gdamore * Parse the descriptor and attach child devices, one per report.
281 1.1 gdamore */
282 1.1 gdamore maxid = -1;
283 1.1 gdamore h.report_ID = 0;
284 1.2 tron d = hid_start_parse(desc, dlen, hid_none);
285 1.1 gdamore while (hid_get_item(d, &h)) {
286 1.1 gdamore if (h.report_ID > maxid)
287 1.1 gdamore maxid = h.report_ID;
288 1.1 gdamore }
289 1.1 gdamore hid_end_parse(d);
290 1.1 gdamore
291 1.1 gdamore if (maxid < 0) {
292 1.1 gdamore aprint_error(" no reports found\n");
293 1.1 gdamore return;
294 1.1 gdamore }
295 1.1 gdamore
296 1.1 gdamore aprint_normal("\n");
297 1.1 gdamore
298 1.21 plunky if (kthread_create(PRI_NONE, KTHREAD_MUSTJOIN, NULL, bthidev_process,
299 1.21 plunky sc, &sc->sc_lwp, "%s", device_xname(self)) != 0) {
300 1.21 plunky aprint_error_dev(self, "failed to create input thread\n");
301 1.21 plunky return;
302 1.21 plunky }
303 1.21 plunky
304 1.1 gdamore for (rep = 0 ; rep <= maxid ; rep++) {
305 1.2 tron if (hid_report_size(desc, dlen, hid_feature, rep) == 0
306 1.2 tron && hid_report_size(desc, dlen, hid_input, rep) == 0
307 1.2 tron && hid_report_size(desc, dlen, hid_output, rep) == 0)
308 1.1 gdamore continue;
309 1.1 gdamore
310 1.19 plunky bha.ba_vendor = vendor;
311 1.19 plunky bha.ba_product = product;
312 1.2 tron bha.ba_desc = desc;
313 1.2 tron bha.ba_dlen = dlen;
314 1.1 gdamore bha.ba_input = bthidev_null;
315 1.1 gdamore bha.ba_feature = bthidev_null;
316 1.1 gdamore bha.ba_output = bthidev_output;
317 1.1 gdamore bha.ba_id = rep;
318 1.1 gdamore
319 1.1 gdamore locs[BTHIDBUSCF_REPORTID] = rep;
320 1.1 gdamore
321 1.10 plunky dev = config_found_sm_loc(self, "bthidbus",
322 1.1 gdamore locs, &bha, bthidev_print, config_stdsubmatch);
323 1.1 gdamore if (dev != NULL) {
324 1.10 plunky hidev = device_private(dev);
325 1.10 plunky hidev->sc_dev = dev;
326 1.10 plunky hidev->sc_parent = self;
327 1.10 plunky hidev->sc_id = rep;
328 1.10 plunky hidev->sc_input = bha.ba_input;
329 1.10 plunky hidev->sc_feature = bha.ba_feature;
330 1.10 plunky LIST_INSERT_HEAD(&sc->sc_list, hidev, sc_next);
331 1.1 gdamore }
332 1.1 gdamore }
333 1.1 gdamore
334 1.22 plunky pmf_device_register(self, NULL, NULL);
335 1.22 plunky
336 1.1 gdamore /*
337 1.1 gdamore * start bluetooth connections
338 1.1 gdamore */
339 1.15 ad mutex_enter(bt_lock);
340 1.2 tron if ((sc->sc_flags & BTHID_RECONNECT) == 0)
341 1.1 gdamore bthidev_listen(sc);
342 1.1 gdamore
343 1.2 tron if (sc->sc_flags & BTHID_CONNECTING)
344 1.1 gdamore bthidev_connect(sc);
345 1.15 ad mutex_exit(bt_lock);
346 1.1 gdamore }
347 1.1 gdamore
348 1.1 gdamore static int
349 1.10 plunky bthidev_detach(device_t self, int flags)
350 1.1 gdamore {
351 1.10 plunky struct bthidev_softc *sc = device_private(self);
352 1.10 plunky struct bthidev *hidev;
353 1.1 gdamore
354 1.15 ad mutex_enter(bt_lock);
355 1.1 gdamore sc->sc_flags = 0; /* disable reconnecting */
356 1.1 gdamore
357 1.1 gdamore /* release interrupt listen */
358 1.1 gdamore if (sc->sc_int_l != NULL) {
359 1.1 gdamore l2cap_detach(&sc->sc_int_l);
360 1.1 gdamore sc->sc_int_l = NULL;
361 1.1 gdamore }
362 1.1 gdamore
363 1.1 gdamore /* release control listen */
364 1.1 gdamore if (sc->sc_ctl_l != NULL) {
365 1.1 gdamore l2cap_detach(&sc->sc_ctl_l);
366 1.1 gdamore sc->sc_ctl_l = NULL;
367 1.1 gdamore }
368 1.1 gdamore
369 1.1 gdamore /* close interrupt channel */
370 1.1 gdamore if (sc->sc_int != NULL) {
371 1.1 gdamore l2cap_disconnect(sc->sc_int, 0);
372 1.1 gdamore l2cap_detach(&sc->sc_int);
373 1.1 gdamore sc->sc_int = NULL;
374 1.1 gdamore }
375 1.1 gdamore
376 1.1 gdamore /* close control channel */
377 1.1 gdamore if (sc->sc_ctl != NULL) {
378 1.1 gdamore l2cap_disconnect(sc->sc_ctl, 0);
379 1.1 gdamore l2cap_detach(&sc->sc_ctl);
380 1.1 gdamore sc->sc_ctl = NULL;
381 1.1 gdamore }
382 1.1 gdamore
383 1.15 ad callout_halt(&sc->sc_reconnect, bt_lock);
384 1.11 plunky callout_destroy(&sc->sc_reconnect);
385 1.11 plunky
386 1.15 ad mutex_exit(bt_lock);
387 1.1 gdamore
388 1.22 plunky pmf_device_deregister(self);
389 1.22 plunky
390 1.21 plunky /* kill off the input processor */
391 1.21 plunky if (sc->sc_lwp != NULL) {
392 1.21 plunky mutex_enter(&sc->sc_lock);
393 1.21 plunky sc->sc_detach = 1;
394 1.21 plunky cv_signal(&sc->sc_cv);
395 1.21 plunky mutex_exit(&sc->sc_lock);
396 1.21 plunky kthread_join(sc->sc_lwp);
397 1.21 plunky sc->sc_lwp = NULL;
398 1.21 plunky }
399 1.21 plunky
400 1.1 gdamore /* detach children */
401 1.10 plunky while ((hidev = LIST_FIRST(&sc->sc_list)) != NULL) {
402 1.10 plunky LIST_REMOVE(hidev, sc_next);
403 1.10 plunky config_detach(hidev->sc_dev, flags);
404 1.1 gdamore }
405 1.1 gdamore
406 1.21 plunky MBUFQ_DRAIN(&sc->sc_inq);
407 1.21 plunky cv_destroy(&sc->sc_cv);
408 1.21 plunky mutex_destroy(&sc->sc_lock);
409 1.16 plunky sockopt_destroy(&sc->sc_mode);
410 1.16 plunky
411 1.1 gdamore return 0;
412 1.1 gdamore }
413 1.1 gdamore
414 1.1 gdamore /*
415 1.1 gdamore * bthidev config print
416 1.1 gdamore */
417 1.1 gdamore static int
418 1.1 gdamore bthidev_print(void *aux, const char *pnp)
419 1.1 gdamore {
420 1.1 gdamore struct bthidev_attach_args *ba = aux;
421 1.1 gdamore
422 1.1 gdamore if (pnp != NULL)
423 1.1 gdamore aprint_normal("%s:", pnp);
424 1.1 gdamore
425 1.1 gdamore if (ba->ba_id > 0)
426 1.1 gdamore aprint_normal(" reportid %d", ba->ba_id);
427 1.1 gdamore
428 1.1 gdamore return UNCONF;
429 1.1 gdamore }
430 1.1 gdamore
431 1.1 gdamore /*****************************************************************************
432 1.1 gdamore *
433 1.1 gdamore * bluetooth(4) HID attach/detach routines
434 1.1 gdamore */
435 1.1 gdamore
436 1.1 gdamore /*
437 1.4 plunky * callouts are scheduled after connections have been lost, in order
438 1.4 plunky * to clean up and reconnect.
439 1.1 gdamore */
440 1.1 gdamore static void
441 1.1 gdamore bthidev_timeout(void *arg)
442 1.1 gdamore {
443 1.1 gdamore struct bthidev_softc *sc = arg;
444 1.1 gdamore
445 1.15 ad mutex_enter(bt_lock);
446 1.1 gdamore callout_ack(&sc->sc_reconnect);
447 1.1 gdamore
448 1.1 gdamore switch (sc->sc_state) {
449 1.1 gdamore case BTHID_CLOSED:
450 1.4 plunky if (sc->sc_int != NULL) {
451 1.4 plunky l2cap_disconnect(sc->sc_int, 0);
452 1.4 plunky break;
453 1.4 plunky }
454 1.4 plunky
455 1.4 plunky if (sc->sc_ctl != NULL) {
456 1.4 plunky l2cap_disconnect(sc->sc_ctl, 0);
457 1.4 plunky break;
458 1.4 plunky }
459 1.4 plunky
460 1.4 plunky if (sc->sc_flags & BTHID_RECONNECT) {
461 1.4 plunky sc->sc_flags |= BTHID_CONNECTING;
462 1.4 plunky bthidev_connect(sc);
463 1.4 plunky break;
464 1.4 plunky }
465 1.4 plunky
466 1.1 gdamore break;
467 1.1 gdamore
468 1.1 gdamore case BTHID_WAIT_CTL:
469 1.1 gdamore break;
470 1.1 gdamore
471 1.1 gdamore case BTHID_WAIT_INT:
472 1.1 gdamore break;
473 1.1 gdamore
474 1.1 gdamore case BTHID_OPEN:
475 1.1 gdamore break;
476 1.1 gdamore
477 1.1 gdamore default:
478 1.1 gdamore break;
479 1.1 gdamore }
480 1.15 ad mutex_exit(bt_lock);
481 1.1 gdamore }
482 1.1 gdamore
483 1.1 gdamore /*
484 1.1 gdamore * listen for our device
485 1.1 gdamore */
486 1.1 gdamore static int
487 1.1 gdamore bthidev_listen(struct bthidev_softc *sc)
488 1.1 gdamore {
489 1.1 gdamore struct sockaddr_bt sa;
490 1.1 gdamore int err;
491 1.1 gdamore
492 1.1 gdamore memset(&sa, 0, sizeof(sa));
493 1.1 gdamore sa.bt_len = sizeof(sa);
494 1.1 gdamore sa.bt_family = AF_BLUETOOTH;
495 1.1 gdamore bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
496 1.1 gdamore
497 1.1 gdamore /*
498 1.1 gdamore * Listen on control PSM
499 1.1 gdamore */
500 1.1 gdamore err = l2cap_attach(&sc->sc_ctl_l, &bthidev_ctl_proto, sc);
501 1.1 gdamore if (err)
502 1.1 gdamore return err;
503 1.1 gdamore
504 1.16 plunky err = l2cap_setopt(sc->sc_ctl_l, &sc->sc_mode);
505 1.8 plunky if (err)
506 1.8 plunky return err;
507 1.8 plunky
508 1.1 gdamore sa.bt_psm = sc->sc_ctlpsm;
509 1.1 gdamore err = l2cap_bind(sc->sc_ctl_l, &sa);
510 1.1 gdamore if (err)
511 1.1 gdamore return err;
512 1.1 gdamore
513 1.1 gdamore err = l2cap_listen(sc->sc_ctl_l);
514 1.1 gdamore if (err)
515 1.1 gdamore return err;
516 1.1 gdamore
517 1.1 gdamore /*
518 1.1 gdamore * Listen on interrupt PSM
519 1.1 gdamore */
520 1.1 gdamore err = l2cap_attach(&sc->sc_int_l, &bthidev_int_proto, sc);
521 1.1 gdamore if (err)
522 1.1 gdamore return err;
523 1.1 gdamore
524 1.16 plunky err = l2cap_setopt(sc->sc_int_l, &sc->sc_mode);
525 1.8 plunky if (err)
526 1.8 plunky return err;
527 1.8 plunky
528 1.1 gdamore sa.bt_psm = sc->sc_intpsm;
529 1.1 gdamore err = l2cap_bind(sc->sc_int_l, &sa);
530 1.1 gdamore if (err)
531 1.1 gdamore return err;
532 1.1 gdamore
533 1.1 gdamore err = l2cap_listen(sc->sc_int_l);
534 1.1 gdamore if (err)
535 1.1 gdamore return err;
536 1.1 gdamore
537 1.1 gdamore sc->sc_state = BTHID_WAIT_CTL;
538 1.1 gdamore return 0;
539 1.1 gdamore }
540 1.1 gdamore
541 1.1 gdamore /*
542 1.1 gdamore * start connecting to our device
543 1.1 gdamore */
544 1.1 gdamore static int
545 1.1 gdamore bthidev_connect(struct bthidev_softc *sc)
546 1.1 gdamore {
547 1.1 gdamore struct sockaddr_bt sa;
548 1.1 gdamore int err;
549 1.1 gdamore
550 1.1 gdamore if (sc->sc_attempts++ > 0)
551 1.12 plunky aprint_verbose_dev(sc->sc_dev, "connect (#%d)\n", sc->sc_attempts);
552 1.1 gdamore
553 1.1 gdamore memset(&sa, 0, sizeof(sa));
554 1.1 gdamore sa.bt_len = sizeof(sa);
555 1.1 gdamore sa.bt_family = AF_BLUETOOTH;
556 1.1 gdamore
557 1.1 gdamore err = l2cap_attach(&sc->sc_ctl, &bthidev_ctl_proto, sc);
558 1.1 gdamore if (err) {
559 1.12 plunky aprint_error_dev(sc->sc_dev, "l2cap_attach failed (%d)\n", err);
560 1.1 gdamore return err;
561 1.1 gdamore }
562 1.1 gdamore
563 1.16 plunky err = l2cap_setopt(sc->sc_ctl, &sc->sc_mode);
564 1.8 plunky if (err)
565 1.8 plunky return err;
566 1.8 plunky
567 1.1 gdamore bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
568 1.1 gdamore err = l2cap_bind(sc->sc_ctl, &sa);
569 1.1 gdamore if (err) {
570 1.12 plunky aprint_error_dev(sc->sc_dev, "l2cap_bind failed (%d)\n", err);
571 1.1 gdamore return err;
572 1.1 gdamore }
573 1.1 gdamore
574 1.1 gdamore sa.bt_psm = sc->sc_ctlpsm;
575 1.1 gdamore bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
576 1.1 gdamore err = l2cap_connect(sc->sc_ctl, &sa);
577 1.1 gdamore if (err) {
578 1.12 plunky aprint_error_dev(sc->sc_dev, "l2cap_connect failed (%d)\n", err);
579 1.1 gdamore return err;
580 1.1 gdamore }
581 1.1 gdamore
582 1.1 gdamore sc->sc_state = BTHID_WAIT_CTL;
583 1.1 gdamore return 0;
584 1.1 gdamore }
585 1.1 gdamore
586 1.21 plunky /*
587 1.21 plunky * The LWP which processes input reports, forwarding to child devices.
588 1.21 plunky * We are always either processing input reports, holding the lock, or
589 1.21 plunky * waiting for a signal on condvar.
590 1.21 plunky */
591 1.21 plunky static void
592 1.21 plunky bthidev_process(void *arg)
593 1.21 plunky {
594 1.21 plunky struct bthidev_softc *sc = arg;
595 1.21 plunky struct mbuf *m;
596 1.21 plunky
597 1.21 plunky mutex_enter(&sc->sc_lock);
598 1.21 plunky while (sc->sc_detach == 0) {
599 1.21 plunky MBUFQ_DEQUEUE(&sc->sc_inq, m);
600 1.21 plunky if (m == NULL) {
601 1.21 plunky cv_wait(&sc->sc_cv, &sc->sc_lock);
602 1.21 plunky continue;
603 1.21 plunky }
604 1.21 plunky
605 1.21 plunky mutex_exit(&sc->sc_lock);
606 1.21 plunky bthidev_process_one(sc, m);
607 1.21 plunky m_freem(m);
608 1.21 plunky mutex_enter(&sc->sc_lock);
609 1.21 plunky }
610 1.21 plunky mutex_exit(&sc->sc_lock);
611 1.21 plunky kthread_exit(0);
612 1.21 plunky }
613 1.21 plunky
614 1.21 plunky static void
615 1.21 plunky bthidev_process_one(struct bthidev_softc *sc, struct mbuf *m)
616 1.21 plunky {
617 1.21 plunky struct bthidev *hidev;
618 1.21 plunky uint8_t *data;
619 1.21 plunky int len;
620 1.21 plunky
621 1.21 plunky if (sc->sc_state != BTHID_OPEN)
622 1.21 plunky return;
623 1.21 plunky
624 1.21 plunky if (m->m_pkthdr.len > m->m_len)
625 1.21 plunky aprint_error_dev(sc->sc_dev, "truncating HID report\n");
626 1.21 plunky
627 1.21 plunky len = m->m_len;
628 1.21 plunky data = mtod(m, uint8_t *);
629 1.21 plunky
630 1.21 plunky switch (BTHID_TYPE(data[0])) {
631 1.21 plunky case BTHID_DATA:
632 1.21 plunky /*
633 1.21 plunky * data[0] == type / parameter
634 1.21 plunky * data[1] == id
635 1.21 plunky * data[2..len] == report
636 1.21 plunky */
637 1.21 plunky if (len < 3)
638 1.21 plunky break;
639 1.21 plunky
640 1.21 plunky LIST_FOREACH(hidev, &sc->sc_list, sc_next)
641 1.21 plunky if (data[1] == hidev->sc_id)
642 1.21 plunky break;
643 1.21 plunky
644 1.21 plunky if (hidev == NULL) {
645 1.21 plunky aprint_error_dev(sc->sc_dev,
646 1.21 plunky "report id %d, len = %d ignored\n", data[1], len - 2);
647 1.21 plunky
648 1.21 plunky break;
649 1.21 plunky }
650 1.21 plunky
651 1.21 plunky switch (BTHID_DATA_PARAM(data[0])) {
652 1.21 plunky case BTHID_DATA_INPUT:
653 1.21 plunky (*hidev->sc_input)(hidev, data + 2, len - 2);
654 1.21 plunky break;
655 1.21 plunky
656 1.21 plunky case BTHID_DATA_FEATURE:
657 1.21 plunky (*hidev->sc_feature)(hidev, data + 2, len - 2);
658 1.21 plunky break;
659 1.21 plunky
660 1.21 plunky default:
661 1.21 plunky break;
662 1.21 plunky }
663 1.21 plunky
664 1.21 plunky break;
665 1.21 plunky
666 1.21 plunky case BTHID_CONTROL:
667 1.21 plunky if (len < 1)
668 1.21 plunky break;
669 1.21 plunky
670 1.21 plunky switch (BTHID_DATA_PARAM(data[0])) {
671 1.21 plunky case BTHID_CONTROL_UNPLUG:
672 1.21 plunky aprint_normal_dev(sc->sc_dev, "unplugged\n");
673 1.21 plunky
674 1.21 plunky mutex_enter(bt_lock);
675 1.21 plunky /* close interrupt channel */
676 1.21 plunky if (sc->sc_int != NULL) {
677 1.21 plunky l2cap_disconnect(sc->sc_int, 0);
678 1.21 plunky l2cap_detach(&sc->sc_int);
679 1.21 plunky sc->sc_int = NULL;
680 1.21 plunky }
681 1.21 plunky
682 1.21 plunky /* close control channel */
683 1.21 plunky if (sc->sc_ctl != NULL) {
684 1.21 plunky l2cap_disconnect(sc->sc_ctl, 0);
685 1.21 plunky l2cap_detach(&sc->sc_ctl);
686 1.21 plunky sc->sc_ctl = NULL;
687 1.21 plunky }
688 1.21 plunky mutex_exit(bt_lock);
689 1.21 plunky
690 1.21 plunky break;
691 1.21 plunky
692 1.21 plunky default:
693 1.21 plunky break;
694 1.21 plunky }
695 1.21 plunky
696 1.21 plunky break;
697 1.21 plunky
698 1.21 plunky default:
699 1.21 plunky break;
700 1.21 plunky }
701 1.21 plunky }
702 1.21 plunky
703 1.1 gdamore /*****************************************************************************
704 1.1 gdamore *
705 1.1 gdamore * bluetooth(9) callback methods for L2CAP
706 1.1 gdamore *
707 1.1 gdamore * All these are called from Bluetooth Protocol code, in a soft
708 1.1 gdamore * interrupt context at IPL_SOFTNET.
709 1.1 gdamore */
710 1.1 gdamore
711 1.1 gdamore static void
712 1.7 christos bthidev_connecting(void *arg)
713 1.1 gdamore {
714 1.1 gdamore
715 1.1 gdamore /* dont care */
716 1.1 gdamore }
717 1.1 gdamore
718 1.1 gdamore static void
719 1.1 gdamore bthidev_ctl_connected(void *arg)
720 1.1 gdamore {
721 1.1 gdamore struct sockaddr_bt sa;
722 1.1 gdamore struct bthidev_softc *sc = arg;
723 1.1 gdamore int err;
724 1.1 gdamore
725 1.1 gdamore if (sc->sc_state != BTHID_WAIT_CTL)
726 1.1 gdamore return;
727 1.1 gdamore
728 1.1 gdamore KASSERT(sc->sc_ctl != NULL);
729 1.1 gdamore KASSERT(sc->sc_int == NULL);
730 1.1 gdamore
731 1.2 tron if (sc->sc_flags & BTHID_CONNECTING) {
732 1.1 gdamore /* initiate connect on interrupt PSM */
733 1.1 gdamore err = l2cap_attach(&sc->sc_int, &bthidev_int_proto, sc);
734 1.1 gdamore if (err)
735 1.1 gdamore goto fail;
736 1.1 gdamore
737 1.16 plunky err = l2cap_setopt(sc->sc_int, &sc->sc_mode);
738 1.8 plunky if (err)
739 1.8 plunky goto fail;
740 1.8 plunky
741 1.1 gdamore memset(&sa, 0, sizeof(sa));
742 1.1 gdamore sa.bt_len = sizeof(sa);
743 1.1 gdamore sa.bt_family = AF_BLUETOOTH;
744 1.1 gdamore bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
745 1.1 gdamore
746 1.1 gdamore err = l2cap_bind(sc->sc_int, &sa);
747 1.1 gdamore if (err)
748 1.1 gdamore goto fail;
749 1.1 gdamore
750 1.1 gdamore sa.bt_psm = sc->sc_intpsm;
751 1.1 gdamore bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
752 1.1 gdamore err = l2cap_connect(sc->sc_int, &sa);
753 1.1 gdamore if (err)
754 1.1 gdamore goto fail;
755 1.1 gdamore }
756 1.1 gdamore
757 1.1 gdamore sc->sc_state = BTHID_WAIT_INT;
758 1.1 gdamore return;
759 1.1 gdamore
760 1.1 gdamore fail:
761 1.1 gdamore l2cap_detach(&sc->sc_ctl);
762 1.1 gdamore sc->sc_ctl = NULL;
763 1.3 plunky
764 1.12 plunky aprint_error_dev(sc->sc_dev, "connect failed (%d)\n", err);
765 1.1 gdamore }
766 1.1 gdamore
767 1.1 gdamore static void
768 1.1 gdamore bthidev_int_connected(void *arg)
769 1.1 gdamore {
770 1.1 gdamore struct bthidev_softc *sc = arg;
771 1.1 gdamore
772 1.1 gdamore if (sc->sc_state != BTHID_WAIT_INT)
773 1.1 gdamore return;
774 1.1 gdamore
775 1.1 gdamore KASSERT(sc->sc_ctl != NULL);
776 1.1 gdamore KASSERT(sc->sc_int != NULL);
777 1.1 gdamore
778 1.1 gdamore sc->sc_attempts = 0;
779 1.2 tron sc->sc_flags &= ~BTHID_CONNECTING;
780 1.1 gdamore sc->sc_state = BTHID_OPEN;
781 1.1 gdamore
782 1.13 plunky aprint_normal_dev(sc->sc_dev, "connected\n");
783 1.1 gdamore }
784 1.1 gdamore
785 1.1 gdamore /*
786 1.1 gdamore * Disconnected
787 1.1 gdamore *
788 1.1 gdamore * Depending on our state, this could mean several things, but essentially
789 1.2 tron * we are lost. If both channels are closed, and we are marked to reconnect,
790 1.2 tron * schedule another try otherwise just give up. They will contact us.
791 1.1 gdamore */
792 1.1 gdamore static void
793 1.7 christos bthidev_ctl_disconnected(void *arg, int err)
794 1.1 gdamore {
795 1.1 gdamore struct bthidev_softc *sc = arg;
796 1.1 gdamore
797 1.1 gdamore if (sc->sc_ctl != NULL) {
798 1.1 gdamore l2cap_detach(&sc->sc_ctl);
799 1.1 gdamore sc->sc_ctl = NULL;
800 1.1 gdamore }
801 1.1 gdamore
802 1.1 gdamore sc->sc_state = BTHID_CLOSED;
803 1.1 gdamore
804 1.1 gdamore if (sc->sc_int == NULL) {
805 1.13 plunky aprint_normal_dev(sc->sc_dev, "disconnected\n");
806 1.2 tron sc->sc_flags &= ~BTHID_CONNECTING;
807 1.1 gdamore
808 1.2 tron if (sc->sc_flags & BTHID_RECONNECT)
809 1.1 gdamore callout_schedule(&sc->sc_reconnect,
810 1.1 gdamore BTHID_RETRY_INTERVAL * hz);
811 1.1 gdamore else
812 1.1 gdamore sc->sc_state = BTHID_WAIT_CTL;
813 1.2 tron } else {
814 1.2 tron /*
815 1.2 tron * The interrupt channel should have been closed first,
816 1.4 plunky * but its potentially unsafe to detach that from here.
817 1.4 plunky * Give them a second to do the right thing or let the
818 1.4 plunky * callout handle it.
819 1.2 tron */
820 1.4 plunky callout_schedule(&sc->sc_reconnect, hz);
821 1.1 gdamore }
822 1.1 gdamore }
823 1.1 gdamore
824 1.1 gdamore static void
825 1.7 christos bthidev_int_disconnected(void *arg, int err)
826 1.1 gdamore {
827 1.1 gdamore struct bthidev_softc *sc = arg;
828 1.1 gdamore
829 1.1 gdamore if (sc->sc_int != NULL) {
830 1.1 gdamore l2cap_detach(&sc->sc_int);
831 1.1 gdamore sc->sc_int = NULL;
832 1.1 gdamore }
833 1.1 gdamore
834 1.1 gdamore sc->sc_state = BTHID_CLOSED;
835 1.1 gdamore
836 1.1 gdamore if (sc->sc_ctl == NULL) {
837 1.13 plunky aprint_normal_dev(sc->sc_dev, "disconnected\n");
838 1.2 tron sc->sc_flags &= ~BTHID_CONNECTING;
839 1.1 gdamore
840 1.2 tron if (sc->sc_flags & BTHID_RECONNECT)
841 1.1 gdamore callout_schedule(&sc->sc_reconnect,
842 1.1 gdamore BTHID_RETRY_INTERVAL * hz);
843 1.1 gdamore else
844 1.1 gdamore sc->sc_state = BTHID_WAIT_CTL;
845 1.4 plunky } else {
846 1.4 plunky /*
847 1.4 plunky * The control channel should be closing also, allow
848 1.4 plunky * them a chance to do that before we force it.
849 1.4 plunky */
850 1.4 plunky callout_schedule(&sc->sc_reconnect, hz);
851 1.1 gdamore }
852 1.1 gdamore }
853 1.1 gdamore
854 1.1 gdamore /*
855 1.1 gdamore * New Connections
856 1.1 gdamore *
857 1.1 gdamore * We give a new L2CAP handle back if this matches the BDADDR we are
858 1.1 gdamore * listening for and we are in the right state. bthidev_connected will
859 1.1 gdamore * be called when the connection is open, so nothing else to do here
860 1.1 gdamore */
861 1.1 gdamore static void *
862 1.7 christos bthidev_ctl_newconn(void *arg, struct sockaddr_bt *laddr,
863 1.6 christos struct sockaddr_bt *raddr)
864 1.1 gdamore {
865 1.1 gdamore struct bthidev_softc *sc = arg;
866 1.1 gdamore
867 1.18 plunky if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0)
868 1.18 plunky return NULL;
869 1.18 plunky
870 1.18 plunky if ((sc->sc_flags & BTHID_CONNECTING)
871 1.1 gdamore || sc->sc_state != BTHID_WAIT_CTL
872 1.1 gdamore || sc->sc_ctl != NULL
873 1.18 plunky || sc->sc_int != NULL) {
874 1.18 plunky aprint_verbose_dev(sc->sc_dev, "reject ctl newconn %s%s%s%s\n",
875 1.18 plunky (sc->sc_flags & BTHID_CONNECTING) ? " (CONNECTING)" : "",
876 1.18 plunky (sc->sc_state == BTHID_WAIT_CTL) ? " (WAITING)": "",
877 1.18 plunky (sc->sc_ctl != NULL) ? " (GOT CONTROL)" : "",
878 1.18 plunky (sc->sc_int != NULL) ? " (GOT INTERRUPT)" : "");
879 1.18 plunky
880 1.1 gdamore return NULL;
881 1.18 plunky }
882 1.1 gdamore
883 1.1 gdamore l2cap_attach(&sc->sc_ctl, &bthidev_ctl_proto, sc);
884 1.1 gdamore return sc->sc_ctl;
885 1.1 gdamore }
886 1.1 gdamore
887 1.1 gdamore static void *
888 1.7 christos bthidev_int_newconn(void *arg, struct sockaddr_bt *laddr,
889 1.6 christos struct sockaddr_bt *raddr)
890 1.1 gdamore {
891 1.1 gdamore struct bthidev_softc *sc = arg;
892 1.1 gdamore
893 1.18 plunky if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0)
894 1.18 plunky return NULL;
895 1.18 plunky
896 1.18 plunky if ((sc->sc_flags & BTHID_CONNECTING)
897 1.1 gdamore || sc->sc_state != BTHID_WAIT_INT
898 1.1 gdamore || sc->sc_ctl == NULL
899 1.18 plunky || sc->sc_int != NULL) {
900 1.18 plunky aprint_verbose_dev(sc->sc_dev, "reject int newconn %s%s%s%s\n",
901 1.18 plunky (sc->sc_flags & BTHID_CONNECTING) ? " (CONNECTING)" : "",
902 1.18 plunky (sc->sc_state == BTHID_WAIT_INT) ? " (WAITING)": "",
903 1.18 plunky (sc->sc_ctl == NULL) ? " (NO CONTROL)" : "",
904 1.18 plunky (sc->sc_int != NULL) ? " (GOT INTERRUPT)" : "");
905 1.18 plunky
906 1.1 gdamore return NULL;
907 1.18 plunky }
908 1.1 gdamore
909 1.1 gdamore l2cap_attach(&sc->sc_int, &bthidev_int_proto, sc);
910 1.1 gdamore return sc->sc_int;
911 1.1 gdamore }
912 1.1 gdamore
913 1.1 gdamore static void
914 1.7 christos bthidev_complete(void *arg, int count)
915 1.1 gdamore {
916 1.1 gdamore
917 1.1 gdamore /* dont care */
918 1.1 gdamore }
919 1.1 gdamore
920 1.8 plunky static void
921 1.8 plunky bthidev_linkmode(void *arg, int new)
922 1.8 plunky {
923 1.8 plunky struct bthidev_softc *sc = arg;
924 1.16 plunky int mode;
925 1.16 plunky
926 1.16 plunky (void)sockopt_getint(&sc->sc_mode, &mode);
927 1.8 plunky
928 1.16 plunky if ((mode & L2CAP_LM_AUTH) && !(new & L2CAP_LM_AUTH))
929 1.12 plunky aprint_error_dev(sc->sc_dev, "auth failed\n");
930 1.16 plunky else if ((mode & L2CAP_LM_ENCRYPT) && !(new & L2CAP_LM_ENCRYPT))
931 1.12 plunky aprint_error_dev(sc->sc_dev, "encrypt off\n");
932 1.16 plunky else if ((mode & L2CAP_LM_SECURE) && !(new & L2CAP_LM_SECURE))
933 1.12 plunky aprint_error_dev(sc->sc_dev, "insecure\n");
934 1.8 plunky else
935 1.8 plunky return;
936 1.8 plunky
937 1.8 plunky if (sc->sc_int != NULL)
938 1.8 plunky l2cap_disconnect(sc->sc_int, 0);
939 1.8 plunky
940 1.8 plunky if (sc->sc_ctl != NULL)
941 1.8 plunky l2cap_disconnect(sc->sc_ctl, 0);
942 1.8 plunky }
943 1.8 plunky
944 1.1 gdamore /*
945 1.21 plunky * Receive reports from the protocol stack. Because this will be called
946 1.21 plunky * with bt_lock held, we queue the mbuf and process it with a kernel thread
947 1.1 gdamore */
948 1.1 gdamore static void
949 1.1 gdamore bthidev_input(void *arg, struct mbuf *m)
950 1.1 gdamore {
951 1.1 gdamore struct bthidev_softc *sc = arg;
952 1.1 gdamore
953 1.21 plunky if (sc->sc_state != BTHID_OPEN) {
954 1.21 plunky m_freem(m);
955 1.21 plunky return;
956 1.1 gdamore }
957 1.1 gdamore
958 1.21 plunky mutex_enter(&sc->sc_lock);
959 1.21 plunky MBUFQ_ENQUEUE(&sc->sc_inq, m);
960 1.21 plunky cv_signal(&sc->sc_cv);
961 1.21 plunky mutex_exit(&sc->sc_lock);
962 1.1 gdamore }
963 1.1 gdamore
964 1.1 gdamore /*****************************************************************************
965 1.1 gdamore *
966 1.1 gdamore * IO routines
967 1.1 gdamore */
968 1.1 gdamore
969 1.1 gdamore static void
970 1.10 plunky bthidev_null(struct bthidev *hidev, uint8_t *report, int len)
971 1.1 gdamore {
972 1.1 gdamore
973 1.1 gdamore /*
974 1.1 gdamore * empty routine just in case the device
975 1.1 gdamore * provided no method to handle this report
976 1.1 gdamore */
977 1.1 gdamore }
978 1.1 gdamore
979 1.1 gdamore static int
980 1.10 plunky bthidev_output(struct bthidev *hidev, uint8_t *report, int rlen)
981 1.1 gdamore {
982 1.10 plunky struct bthidev_softc *sc = device_private(hidev->sc_parent);
983 1.1 gdamore struct mbuf *m;
984 1.15 ad int err;
985 1.1 gdamore
986 1.1 gdamore if (sc == NULL || sc->sc_state != BTHID_OPEN)
987 1.1 gdamore return ENOTCONN;
988 1.1 gdamore
989 1.1 gdamore KASSERT(sc->sc_ctl != NULL);
990 1.1 gdamore KASSERT(sc->sc_int != NULL);
991 1.1 gdamore
992 1.1 gdamore if (rlen == 0 || report == NULL)
993 1.1 gdamore return 0;
994 1.1 gdamore
995 1.1 gdamore if (rlen > MHLEN - 2) {
996 1.12 plunky aprint_error_dev(sc->sc_dev,
997 1.12 plunky "output report too long (%d)!\n", rlen);
998 1.1 gdamore return EMSGSIZE;
999 1.1 gdamore }
1000 1.1 gdamore
1001 1.1 gdamore m = m_gethdr(M_DONTWAIT, MT_DATA);
1002 1.1 gdamore if (m == NULL)
1003 1.1 gdamore return ENOMEM;
1004 1.1 gdamore
1005 1.1 gdamore /*
1006 1.1 gdamore * data[0] = type / parameter
1007 1.1 gdamore * data[1] = id
1008 1.1 gdamore * data[2..N] = report
1009 1.1 gdamore */
1010 1.1 gdamore mtod(m, uint8_t *)[0] = (uint8_t)((BTHID_DATA << 4) | BTHID_DATA_OUTPUT);
1011 1.10 plunky mtod(m, uint8_t *)[1] = hidev->sc_id;
1012 1.1 gdamore memcpy(mtod(m, uint8_t *) + 2, report, rlen);
1013 1.1 gdamore m->m_pkthdr.len = m->m_len = rlen + 2;
1014 1.1 gdamore
1015 1.21 plunky mutex_enter(bt_lock);
1016 1.1 gdamore err = l2cap_send(sc->sc_int, m);
1017 1.21 plunky mutex_exit(bt_lock);
1018 1.1 gdamore
1019 1.1 gdamore return err;
1020 1.1 gdamore }
1021