bthidev.c revision 1.32.4.1 1 1.32.4.1 thorpej /* $NetBSD: bthidev.c,v 1.32.4.1 2021/03/22 02:00:59 thorpej 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.32.4.1 thorpej __KERNEL_RCSID(0, "$NetBSD: bthidev.c,v 1.32.4.1 2021/03/22 02:00:59 thorpej 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.30 bouyer #include <dev/hid/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.32 thorpej if (prop_string_equals_string(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.24 plunky int err;
197 1.1 gdamore
198 1.1 gdamore /*
199 1.1 gdamore * Init softc
200 1.1 gdamore */
201 1.12 plunky sc->sc_dev = self;
202 1.1 gdamore LIST_INIT(&sc->sc_list);
203 1.21 plunky MBUFQ_INIT(&sc->sc_inq);
204 1.9 ad callout_init(&sc->sc_reconnect, 0);
205 1.1 gdamore callout_setfunc(&sc->sc_reconnect, bthidev_timeout, sc);
206 1.1 gdamore sc->sc_state = BTHID_CLOSED;
207 1.2 tron sc->sc_flags = BTHID_CONNECTING;
208 1.2 tron sc->sc_ctlpsm = L2CAP_PSM_HID_CNTL;
209 1.2 tron sc->sc_intpsm = L2CAP_PSM_HID_INTR;
210 1.1 gdamore
211 1.16 plunky sockopt_init(&sc->sc_mode, BTPROTO_L2CAP, SO_L2CAP_LM, 0);
212 1.21 plunky mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
213 1.21 plunky cv_init(&sc->sc_cv, device_xname(self));
214 1.16 plunky
215 1.1 gdamore /*
216 1.2 tron * extract config from proplist
217 1.1 gdamore */
218 1.3 plunky obj = prop_dictionary_get(dict, BTDEVladdr);
219 1.32 thorpej bdaddr_copy(&sc->sc_laddr, prop_data_value(obj));
220 1.2 tron
221 1.3 plunky obj = prop_dictionary_get(dict, BTDEVraddr);
222 1.32 thorpej bdaddr_copy(&sc->sc_raddr, prop_data_value(obj));
223 1.1 gdamore
224 1.19 plunky obj = prop_dictionary_get(dict, BTDEVvendor);
225 1.32 thorpej vendor = (int)prop_number_signed_value(obj);
226 1.19 plunky
227 1.19 plunky obj = prop_dictionary_get(dict, BTDEVproduct);
228 1.32 thorpej product = (int)prop_number_signed_value(obj);
229 1.19 plunky
230 1.8 plunky obj = prop_dictionary_get(dict, BTDEVmode);
231 1.8 plunky if (prop_object_type(obj) == PROP_TYPE_STRING) {
232 1.32 thorpej if (prop_string_equals_string(obj, BTDEVauth))
233 1.16 plunky sockopt_setint(&sc->sc_mode, L2CAP_LM_AUTH);
234 1.32 thorpej else if (prop_string_equals_string(obj, BTDEVencrypt))
235 1.16 plunky sockopt_setint(&sc->sc_mode, L2CAP_LM_ENCRYPT);
236 1.32 thorpej else if (prop_string_equals_string(obj, BTDEVsecure))
237 1.16 plunky sockopt_setint(&sc->sc_mode, L2CAP_LM_SECURE);
238 1.8 plunky else {
239 1.8 plunky aprint_error(" unknown %s\n", BTDEVmode);
240 1.8 plunky return;
241 1.8 plunky }
242 1.8 plunky
243 1.8 plunky aprint_verbose(" %s %s", BTDEVmode,
244 1.32 thorpej prop_string_value(obj));
245 1.23 plunky } else
246 1.23 plunky sockopt_setint(&sc->sc_mode, 0);
247 1.8 plunky
248 1.3 plunky obj = prop_dictionary_get(dict, BTHIDEVcontrolpsm);
249 1.3 plunky if (prop_object_type(obj) == PROP_TYPE_NUMBER) {
250 1.32 thorpej sc->sc_ctlpsm = prop_number_signed_value(obj);
251 1.2 tron if (L2CAP_PSM_INVALID(sc->sc_ctlpsm)) {
252 1.3 plunky aprint_error(" invalid %s\n", BTHIDEVcontrolpsm);
253 1.2 tron return;
254 1.2 tron }
255 1.2 tron }
256 1.1 gdamore
257 1.3 plunky obj = prop_dictionary_get(dict, BTHIDEVinterruptpsm);
258 1.3 plunky if (prop_object_type(obj) == PROP_TYPE_NUMBER) {
259 1.32 thorpej sc->sc_intpsm = prop_number_signed_value(obj);
260 1.2 tron if (L2CAP_PSM_INVALID(sc->sc_intpsm)) {
261 1.3 plunky aprint_error(" invalid %s\n", BTHIDEVinterruptpsm);
262 1.2 tron return;
263 1.2 tron }
264 1.2 tron }
265 1.2 tron
266 1.3 plunky obj = prop_dictionary_get(dict, BTHIDEVdescriptor);
267 1.3 plunky if (prop_object_type(obj) == PROP_TYPE_DATA) {
268 1.2 tron dlen = prop_data_size(obj);
269 1.32 thorpej desc = prop_data_value(obj);
270 1.2 tron } else {
271 1.3 plunky aprint_error(" no %s\n", BTHIDEVdescriptor);
272 1.1 gdamore return;
273 1.1 gdamore }
274 1.2 tron
275 1.3 plunky obj = prop_dictionary_get(dict, BTHIDEVreconnect);
276 1.3 plunky if (prop_object_type(obj) == PROP_TYPE_BOOL
277 1.2 tron && !prop_bool_true(obj))
278 1.2 tron sc->sc_flags |= BTHID_RECONNECT;
279 1.1 gdamore
280 1.1 gdamore /*
281 1.1 gdamore * Parse the descriptor and attach child devices, one per report.
282 1.1 gdamore */
283 1.1 gdamore maxid = -1;
284 1.1 gdamore h.report_ID = 0;
285 1.2 tron d = hid_start_parse(desc, dlen, hid_none);
286 1.1 gdamore while (hid_get_item(d, &h)) {
287 1.31 jakllsch if ((int)h.report_ID > maxid)
288 1.1 gdamore maxid = h.report_ID;
289 1.1 gdamore }
290 1.1 gdamore hid_end_parse(d);
291 1.1 gdamore
292 1.1 gdamore if (maxid < 0) {
293 1.1 gdamore aprint_error(" no reports found\n");
294 1.1 gdamore return;
295 1.1 gdamore }
296 1.1 gdamore
297 1.1 gdamore aprint_normal("\n");
298 1.1 gdamore
299 1.21 plunky if (kthread_create(PRI_NONE, KTHREAD_MUSTJOIN, NULL, bthidev_process,
300 1.21 plunky sc, &sc->sc_lwp, "%s", device_xname(self)) != 0) {
301 1.21 plunky aprint_error_dev(self, "failed to create input thread\n");
302 1.21 plunky return;
303 1.21 plunky }
304 1.21 plunky
305 1.1 gdamore for (rep = 0 ; rep <= maxid ; rep++) {
306 1.2 tron if (hid_report_size(desc, dlen, hid_feature, rep) == 0
307 1.2 tron && hid_report_size(desc, dlen, hid_input, rep) == 0
308 1.2 tron && hid_report_size(desc, dlen, hid_output, rep) == 0)
309 1.1 gdamore continue;
310 1.1 gdamore
311 1.19 plunky bha.ba_vendor = vendor;
312 1.19 plunky bha.ba_product = product;
313 1.2 tron bha.ba_desc = desc;
314 1.2 tron bha.ba_dlen = dlen;
315 1.1 gdamore bha.ba_input = bthidev_null;
316 1.1 gdamore bha.ba_feature = bthidev_null;
317 1.1 gdamore bha.ba_output = bthidev_output;
318 1.1 gdamore bha.ba_id = rep;
319 1.1 gdamore
320 1.1 gdamore locs[BTHIDBUSCF_REPORTID] = rep;
321 1.1 gdamore
322 1.32.4.1 thorpej dev = config_found(self, &bha, bthidev_print,
323 1.32.4.1 thorpej CFARG_SUBMATCH, config_stdsubmatch,
324 1.32.4.1 thorpej CFARG_IATTR, "bthidbus",
325 1.32.4.1 thorpej CFARG_LOCATORS, locs,
326 1.32.4.1 thorpej CFARG_EOL);
327 1.1 gdamore if (dev != NULL) {
328 1.10 plunky hidev = device_private(dev);
329 1.10 plunky hidev->sc_dev = dev;
330 1.10 plunky hidev->sc_parent = self;
331 1.10 plunky hidev->sc_id = rep;
332 1.10 plunky hidev->sc_input = bha.ba_input;
333 1.10 plunky hidev->sc_feature = bha.ba_feature;
334 1.10 plunky LIST_INSERT_HEAD(&sc->sc_list, hidev, sc_next);
335 1.1 gdamore }
336 1.1 gdamore }
337 1.1 gdamore
338 1.22 plunky pmf_device_register(self, NULL, NULL);
339 1.22 plunky
340 1.1 gdamore /*
341 1.1 gdamore * start bluetooth connections
342 1.1 gdamore */
343 1.15 ad mutex_enter(bt_lock);
344 1.24 plunky if ((sc->sc_flags & BTHID_RECONNECT) == 0
345 1.24 plunky && (err = bthidev_listen(sc)) != 0)
346 1.24 plunky aprint_error_dev(self, "failed to listen (%d)\n", err);
347 1.1 gdamore
348 1.2 tron if (sc->sc_flags & BTHID_CONNECTING)
349 1.1 gdamore bthidev_connect(sc);
350 1.15 ad mutex_exit(bt_lock);
351 1.1 gdamore }
352 1.1 gdamore
353 1.1 gdamore static int
354 1.10 plunky bthidev_detach(device_t self, int flags)
355 1.1 gdamore {
356 1.10 plunky struct bthidev_softc *sc = device_private(self);
357 1.10 plunky struct bthidev *hidev;
358 1.1 gdamore
359 1.15 ad mutex_enter(bt_lock);
360 1.1 gdamore sc->sc_flags = 0; /* disable reconnecting */
361 1.1 gdamore
362 1.1 gdamore /* release interrupt listen */
363 1.1 gdamore if (sc->sc_int_l != NULL) {
364 1.25 rmind l2cap_detach_pcb(&sc->sc_int_l);
365 1.1 gdamore sc->sc_int_l = NULL;
366 1.1 gdamore }
367 1.1 gdamore
368 1.1 gdamore /* release control listen */
369 1.1 gdamore if (sc->sc_ctl_l != NULL) {
370 1.25 rmind l2cap_detach_pcb(&sc->sc_ctl_l);
371 1.1 gdamore sc->sc_ctl_l = NULL;
372 1.1 gdamore }
373 1.1 gdamore
374 1.1 gdamore /* close interrupt channel */
375 1.1 gdamore if (sc->sc_int != NULL) {
376 1.28 rtr l2cap_disconnect_pcb(sc->sc_int, 0);
377 1.25 rmind l2cap_detach_pcb(&sc->sc_int);
378 1.1 gdamore sc->sc_int = NULL;
379 1.1 gdamore }
380 1.1 gdamore
381 1.1 gdamore /* close control channel */
382 1.1 gdamore if (sc->sc_ctl != NULL) {
383 1.28 rtr l2cap_disconnect_pcb(sc->sc_ctl, 0);
384 1.25 rmind l2cap_detach_pcb(&sc->sc_ctl);
385 1.1 gdamore sc->sc_ctl = NULL;
386 1.1 gdamore }
387 1.1 gdamore
388 1.15 ad callout_halt(&sc->sc_reconnect, bt_lock);
389 1.11 plunky callout_destroy(&sc->sc_reconnect);
390 1.11 plunky
391 1.15 ad mutex_exit(bt_lock);
392 1.1 gdamore
393 1.22 plunky pmf_device_deregister(self);
394 1.22 plunky
395 1.21 plunky /* kill off the input processor */
396 1.21 plunky if (sc->sc_lwp != NULL) {
397 1.21 plunky mutex_enter(&sc->sc_lock);
398 1.21 plunky sc->sc_detach = 1;
399 1.21 plunky cv_signal(&sc->sc_cv);
400 1.21 plunky mutex_exit(&sc->sc_lock);
401 1.21 plunky kthread_join(sc->sc_lwp);
402 1.21 plunky sc->sc_lwp = NULL;
403 1.21 plunky }
404 1.21 plunky
405 1.1 gdamore /* detach children */
406 1.10 plunky while ((hidev = LIST_FIRST(&sc->sc_list)) != NULL) {
407 1.10 plunky LIST_REMOVE(hidev, sc_next);
408 1.10 plunky config_detach(hidev->sc_dev, flags);
409 1.1 gdamore }
410 1.1 gdamore
411 1.21 plunky MBUFQ_DRAIN(&sc->sc_inq);
412 1.21 plunky cv_destroy(&sc->sc_cv);
413 1.21 plunky mutex_destroy(&sc->sc_lock);
414 1.16 plunky sockopt_destroy(&sc->sc_mode);
415 1.16 plunky
416 1.1 gdamore return 0;
417 1.1 gdamore }
418 1.1 gdamore
419 1.1 gdamore /*
420 1.1 gdamore * bthidev config print
421 1.1 gdamore */
422 1.1 gdamore static int
423 1.1 gdamore bthidev_print(void *aux, const char *pnp)
424 1.1 gdamore {
425 1.1 gdamore struct bthidev_attach_args *ba = aux;
426 1.1 gdamore
427 1.1 gdamore if (pnp != NULL)
428 1.1 gdamore aprint_normal("%s:", pnp);
429 1.1 gdamore
430 1.1 gdamore if (ba->ba_id > 0)
431 1.1 gdamore aprint_normal(" reportid %d", ba->ba_id);
432 1.1 gdamore
433 1.1 gdamore return UNCONF;
434 1.1 gdamore }
435 1.1 gdamore
436 1.1 gdamore /*****************************************************************************
437 1.1 gdamore *
438 1.1 gdamore * bluetooth(4) HID attach/detach routines
439 1.1 gdamore */
440 1.1 gdamore
441 1.1 gdamore /*
442 1.4 plunky * callouts are scheduled after connections have been lost, in order
443 1.4 plunky * to clean up and reconnect.
444 1.1 gdamore */
445 1.1 gdamore static void
446 1.1 gdamore bthidev_timeout(void *arg)
447 1.1 gdamore {
448 1.1 gdamore struct bthidev_softc *sc = arg;
449 1.1 gdamore
450 1.15 ad mutex_enter(bt_lock);
451 1.1 gdamore callout_ack(&sc->sc_reconnect);
452 1.1 gdamore
453 1.1 gdamore switch (sc->sc_state) {
454 1.1 gdamore case BTHID_CLOSED:
455 1.4 plunky if (sc->sc_int != NULL) {
456 1.28 rtr l2cap_disconnect_pcb(sc->sc_int, 0);
457 1.4 plunky break;
458 1.4 plunky }
459 1.4 plunky
460 1.4 plunky if (sc->sc_ctl != NULL) {
461 1.28 rtr l2cap_disconnect_pcb(sc->sc_ctl, 0);
462 1.4 plunky break;
463 1.4 plunky }
464 1.4 plunky
465 1.4 plunky if (sc->sc_flags & BTHID_RECONNECT) {
466 1.4 plunky sc->sc_flags |= BTHID_CONNECTING;
467 1.4 plunky bthidev_connect(sc);
468 1.4 plunky break;
469 1.4 plunky }
470 1.4 plunky
471 1.1 gdamore break;
472 1.1 gdamore
473 1.1 gdamore case BTHID_WAIT_CTL:
474 1.1 gdamore break;
475 1.1 gdamore
476 1.1 gdamore case BTHID_WAIT_INT:
477 1.1 gdamore break;
478 1.1 gdamore
479 1.1 gdamore case BTHID_OPEN:
480 1.1 gdamore break;
481 1.1 gdamore
482 1.1 gdamore default:
483 1.1 gdamore break;
484 1.1 gdamore }
485 1.15 ad mutex_exit(bt_lock);
486 1.1 gdamore }
487 1.1 gdamore
488 1.1 gdamore /*
489 1.1 gdamore * listen for our device
490 1.1 gdamore */
491 1.1 gdamore static int
492 1.1 gdamore bthidev_listen(struct bthidev_softc *sc)
493 1.1 gdamore {
494 1.1 gdamore struct sockaddr_bt sa;
495 1.1 gdamore int err;
496 1.1 gdamore
497 1.1 gdamore memset(&sa, 0, sizeof(sa));
498 1.1 gdamore sa.bt_len = sizeof(sa);
499 1.1 gdamore sa.bt_family = AF_BLUETOOTH;
500 1.1 gdamore bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
501 1.1 gdamore
502 1.1 gdamore /*
503 1.1 gdamore * Listen on control PSM
504 1.1 gdamore */
505 1.25 rmind err = l2cap_attach_pcb(&sc->sc_ctl_l, &bthidev_ctl_proto, sc);
506 1.1 gdamore if (err)
507 1.1 gdamore return err;
508 1.1 gdamore
509 1.16 plunky err = l2cap_setopt(sc->sc_ctl_l, &sc->sc_mode);
510 1.8 plunky if (err)
511 1.8 plunky return err;
512 1.8 plunky
513 1.1 gdamore sa.bt_psm = sc->sc_ctlpsm;
514 1.26 rtr err = l2cap_bind_pcb(sc->sc_ctl_l, &sa);
515 1.1 gdamore if (err)
516 1.1 gdamore return err;
517 1.1 gdamore
518 1.26 rtr err = l2cap_listen_pcb(sc->sc_ctl_l);
519 1.1 gdamore if (err)
520 1.1 gdamore return err;
521 1.1 gdamore
522 1.1 gdamore /*
523 1.1 gdamore * Listen on interrupt PSM
524 1.1 gdamore */
525 1.25 rmind err = l2cap_attach_pcb(&sc->sc_int_l, &bthidev_int_proto, sc);
526 1.1 gdamore if (err)
527 1.1 gdamore return err;
528 1.1 gdamore
529 1.16 plunky err = l2cap_setopt(sc->sc_int_l, &sc->sc_mode);
530 1.8 plunky if (err)
531 1.8 plunky return err;
532 1.8 plunky
533 1.1 gdamore sa.bt_psm = sc->sc_intpsm;
534 1.26 rtr err = l2cap_bind_pcb(sc->sc_int_l, &sa);
535 1.1 gdamore if (err)
536 1.1 gdamore return err;
537 1.1 gdamore
538 1.26 rtr err = l2cap_listen_pcb(sc->sc_int_l);
539 1.1 gdamore if (err)
540 1.1 gdamore return err;
541 1.1 gdamore
542 1.1 gdamore sc->sc_state = BTHID_WAIT_CTL;
543 1.1 gdamore return 0;
544 1.1 gdamore }
545 1.1 gdamore
546 1.1 gdamore /*
547 1.1 gdamore * start connecting to our device
548 1.1 gdamore */
549 1.1 gdamore static int
550 1.1 gdamore bthidev_connect(struct bthidev_softc *sc)
551 1.1 gdamore {
552 1.1 gdamore struct sockaddr_bt sa;
553 1.1 gdamore int err;
554 1.1 gdamore
555 1.1 gdamore if (sc->sc_attempts++ > 0)
556 1.12 plunky aprint_verbose_dev(sc->sc_dev, "connect (#%d)\n", sc->sc_attempts);
557 1.1 gdamore
558 1.1 gdamore memset(&sa, 0, sizeof(sa));
559 1.1 gdamore sa.bt_len = sizeof(sa);
560 1.1 gdamore sa.bt_family = AF_BLUETOOTH;
561 1.1 gdamore
562 1.25 rmind err = l2cap_attach_pcb(&sc->sc_ctl, &bthidev_ctl_proto, sc);
563 1.1 gdamore if (err) {
564 1.12 plunky aprint_error_dev(sc->sc_dev, "l2cap_attach failed (%d)\n", err);
565 1.1 gdamore return err;
566 1.1 gdamore }
567 1.1 gdamore
568 1.16 plunky err = l2cap_setopt(sc->sc_ctl, &sc->sc_mode);
569 1.24 plunky if (err) {
570 1.24 plunky aprint_error_dev(sc->sc_dev, "l2cap_setopt failed (%d)\n", err);
571 1.8 plunky return err;
572 1.24 plunky }
573 1.8 plunky
574 1.1 gdamore bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
575 1.26 rtr err = l2cap_bind_pcb(sc->sc_ctl, &sa);
576 1.1 gdamore if (err) {
577 1.26 rtr aprint_error_dev(sc->sc_dev, "l2cap_bind_pcb failed (%d)\n", err);
578 1.1 gdamore return err;
579 1.1 gdamore }
580 1.1 gdamore
581 1.1 gdamore sa.bt_psm = sc->sc_ctlpsm;
582 1.1 gdamore bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
583 1.27 rtr err = l2cap_connect_pcb(sc->sc_ctl, &sa);
584 1.1 gdamore if (err) {
585 1.27 rtr aprint_error_dev(sc->sc_dev, "l2cap_connect_pcb failed (%d)\n", err);
586 1.1 gdamore return err;
587 1.1 gdamore }
588 1.1 gdamore
589 1.1 gdamore sc->sc_state = BTHID_WAIT_CTL;
590 1.1 gdamore return 0;
591 1.1 gdamore }
592 1.1 gdamore
593 1.21 plunky /*
594 1.21 plunky * The LWP which processes input reports, forwarding to child devices.
595 1.21 plunky * We are always either processing input reports, holding the lock, or
596 1.21 plunky * waiting for a signal on condvar.
597 1.21 plunky */
598 1.21 plunky static void
599 1.21 plunky bthidev_process(void *arg)
600 1.21 plunky {
601 1.21 plunky struct bthidev_softc *sc = arg;
602 1.21 plunky struct mbuf *m;
603 1.21 plunky
604 1.21 plunky mutex_enter(&sc->sc_lock);
605 1.21 plunky while (sc->sc_detach == 0) {
606 1.21 plunky MBUFQ_DEQUEUE(&sc->sc_inq, m);
607 1.21 plunky if (m == NULL) {
608 1.21 plunky cv_wait(&sc->sc_cv, &sc->sc_lock);
609 1.21 plunky continue;
610 1.21 plunky }
611 1.21 plunky
612 1.21 plunky mutex_exit(&sc->sc_lock);
613 1.21 plunky bthidev_process_one(sc, m);
614 1.21 plunky m_freem(m);
615 1.21 plunky mutex_enter(&sc->sc_lock);
616 1.21 plunky }
617 1.21 plunky mutex_exit(&sc->sc_lock);
618 1.21 plunky kthread_exit(0);
619 1.21 plunky }
620 1.21 plunky
621 1.21 plunky static void
622 1.21 plunky bthidev_process_one(struct bthidev_softc *sc, struct mbuf *m)
623 1.21 plunky {
624 1.21 plunky struct bthidev *hidev;
625 1.21 plunky uint8_t *data;
626 1.21 plunky int len;
627 1.21 plunky
628 1.21 plunky if (sc->sc_state != BTHID_OPEN)
629 1.21 plunky return;
630 1.21 plunky
631 1.21 plunky if (m->m_pkthdr.len > m->m_len)
632 1.21 plunky aprint_error_dev(sc->sc_dev, "truncating HID report\n");
633 1.21 plunky
634 1.21 plunky len = m->m_len;
635 1.21 plunky data = mtod(m, uint8_t *);
636 1.21 plunky
637 1.21 plunky switch (BTHID_TYPE(data[0])) {
638 1.21 plunky case BTHID_DATA:
639 1.21 plunky /*
640 1.21 plunky * data[0] == type / parameter
641 1.21 plunky * data[1] == id
642 1.21 plunky * data[2..len] == report
643 1.21 plunky */
644 1.21 plunky if (len < 3)
645 1.21 plunky break;
646 1.21 plunky
647 1.21 plunky LIST_FOREACH(hidev, &sc->sc_list, sc_next)
648 1.21 plunky if (data[1] == hidev->sc_id)
649 1.21 plunky break;
650 1.21 plunky
651 1.21 plunky if (hidev == NULL) {
652 1.21 plunky aprint_error_dev(sc->sc_dev,
653 1.21 plunky "report id %d, len = %d ignored\n", data[1], len - 2);
654 1.21 plunky
655 1.21 plunky break;
656 1.21 plunky }
657 1.21 plunky
658 1.21 plunky switch (BTHID_DATA_PARAM(data[0])) {
659 1.21 plunky case BTHID_DATA_INPUT:
660 1.21 plunky (*hidev->sc_input)(hidev, data + 2, len - 2);
661 1.21 plunky break;
662 1.21 plunky
663 1.21 plunky case BTHID_DATA_FEATURE:
664 1.21 plunky (*hidev->sc_feature)(hidev, data + 2, len - 2);
665 1.21 plunky break;
666 1.21 plunky
667 1.21 plunky default:
668 1.21 plunky break;
669 1.21 plunky }
670 1.21 plunky
671 1.21 plunky break;
672 1.21 plunky
673 1.21 plunky case BTHID_CONTROL:
674 1.21 plunky if (len < 1)
675 1.21 plunky break;
676 1.21 plunky
677 1.21 plunky switch (BTHID_DATA_PARAM(data[0])) {
678 1.21 plunky case BTHID_CONTROL_UNPLUG:
679 1.21 plunky aprint_normal_dev(sc->sc_dev, "unplugged\n");
680 1.21 plunky
681 1.21 plunky mutex_enter(bt_lock);
682 1.21 plunky /* close interrupt channel */
683 1.21 plunky if (sc->sc_int != NULL) {
684 1.28 rtr l2cap_disconnect_pcb(sc->sc_int, 0);
685 1.25 rmind l2cap_detach_pcb(&sc->sc_int);
686 1.21 plunky sc->sc_int = NULL;
687 1.21 plunky }
688 1.21 plunky
689 1.21 plunky /* close control channel */
690 1.21 plunky if (sc->sc_ctl != NULL) {
691 1.28 rtr l2cap_disconnect_pcb(sc->sc_ctl, 0);
692 1.25 rmind l2cap_detach_pcb(&sc->sc_ctl);
693 1.21 plunky sc->sc_ctl = NULL;
694 1.21 plunky }
695 1.21 plunky mutex_exit(bt_lock);
696 1.21 plunky
697 1.21 plunky break;
698 1.21 plunky
699 1.21 plunky default:
700 1.21 plunky break;
701 1.21 plunky }
702 1.21 plunky
703 1.21 plunky break;
704 1.21 plunky
705 1.21 plunky default:
706 1.21 plunky break;
707 1.21 plunky }
708 1.21 plunky }
709 1.21 plunky
710 1.1 gdamore /*****************************************************************************
711 1.1 gdamore *
712 1.1 gdamore * bluetooth(9) callback methods for L2CAP
713 1.1 gdamore *
714 1.1 gdamore * All these are called from Bluetooth Protocol code, in a soft
715 1.1 gdamore * interrupt context at IPL_SOFTNET.
716 1.1 gdamore */
717 1.1 gdamore
718 1.1 gdamore static void
719 1.7 christos bthidev_connecting(void *arg)
720 1.1 gdamore {
721 1.1 gdamore
722 1.1 gdamore /* dont care */
723 1.1 gdamore }
724 1.1 gdamore
725 1.1 gdamore static void
726 1.1 gdamore bthidev_ctl_connected(void *arg)
727 1.1 gdamore {
728 1.1 gdamore struct sockaddr_bt sa;
729 1.1 gdamore struct bthidev_softc *sc = arg;
730 1.1 gdamore int err;
731 1.1 gdamore
732 1.1 gdamore if (sc->sc_state != BTHID_WAIT_CTL)
733 1.1 gdamore return;
734 1.1 gdamore
735 1.1 gdamore KASSERT(sc->sc_ctl != NULL);
736 1.1 gdamore KASSERT(sc->sc_int == NULL);
737 1.1 gdamore
738 1.2 tron if (sc->sc_flags & BTHID_CONNECTING) {
739 1.1 gdamore /* initiate connect on interrupt PSM */
740 1.25 rmind err = l2cap_attach_pcb(&sc->sc_int, &bthidev_int_proto, sc);
741 1.1 gdamore if (err)
742 1.1 gdamore goto fail;
743 1.1 gdamore
744 1.16 plunky err = l2cap_setopt(sc->sc_int, &sc->sc_mode);
745 1.8 plunky if (err)
746 1.8 plunky goto fail;
747 1.8 plunky
748 1.1 gdamore memset(&sa, 0, sizeof(sa));
749 1.1 gdamore sa.bt_len = sizeof(sa);
750 1.1 gdamore sa.bt_family = AF_BLUETOOTH;
751 1.1 gdamore bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
752 1.1 gdamore
753 1.26 rtr err = l2cap_bind_pcb(sc->sc_int, &sa);
754 1.1 gdamore if (err)
755 1.1 gdamore goto fail;
756 1.1 gdamore
757 1.1 gdamore sa.bt_psm = sc->sc_intpsm;
758 1.1 gdamore bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
759 1.27 rtr err = l2cap_connect_pcb(sc->sc_int, &sa);
760 1.1 gdamore if (err)
761 1.1 gdamore goto fail;
762 1.1 gdamore }
763 1.1 gdamore
764 1.1 gdamore sc->sc_state = BTHID_WAIT_INT;
765 1.1 gdamore return;
766 1.1 gdamore
767 1.1 gdamore fail:
768 1.25 rmind l2cap_detach_pcb(&sc->sc_ctl);
769 1.1 gdamore sc->sc_ctl = NULL;
770 1.3 plunky
771 1.12 plunky aprint_error_dev(sc->sc_dev, "connect failed (%d)\n", err);
772 1.1 gdamore }
773 1.1 gdamore
774 1.1 gdamore static void
775 1.1 gdamore bthidev_int_connected(void *arg)
776 1.1 gdamore {
777 1.1 gdamore struct bthidev_softc *sc = arg;
778 1.1 gdamore
779 1.1 gdamore if (sc->sc_state != BTHID_WAIT_INT)
780 1.1 gdamore return;
781 1.1 gdamore
782 1.1 gdamore KASSERT(sc->sc_ctl != NULL);
783 1.1 gdamore KASSERT(sc->sc_int != NULL);
784 1.1 gdamore
785 1.1 gdamore sc->sc_attempts = 0;
786 1.2 tron sc->sc_flags &= ~BTHID_CONNECTING;
787 1.1 gdamore sc->sc_state = BTHID_OPEN;
788 1.1 gdamore
789 1.13 plunky aprint_normal_dev(sc->sc_dev, "connected\n");
790 1.1 gdamore }
791 1.1 gdamore
792 1.1 gdamore /*
793 1.1 gdamore * Disconnected
794 1.1 gdamore *
795 1.1 gdamore * Depending on our state, this could mean several things, but essentially
796 1.2 tron * we are lost. If both channels are closed, and we are marked to reconnect,
797 1.2 tron * schedule another try otherwise just give up. They will contact us.
798 1.1 gdamore */
799 1.1 gdamore static void
800 1.7 christos bthidev_ctl_disconnected(void *arg, int err)
801 1.1 gdamore {
802 1.1 gdamore struct bthidev_softc *sc = arg;
803 1.1 gdamore
804 1.1 gdamore if (sc->sc_ctl != NULL) {
805 1.25 rmind l2cap_detach_pcb(&sc->sc_ctl);
806 1.1 gdamore sc->sc_ctl = NULL;
807 1.1 gdamore }
808 1.1 gdamore
809 1.1 gdamore sc->sc_state = BTHID_CLOSED;
810 1.1 gdamore
811 1.1 gdamore if (sc->sc_int == NULL) {
812 1.24 plunky aprint_normal_dev(sc->sc_dev, "disconnected (%d)\n", err);
813 1.2 tron sc->sc_flags &= ~BTHID_CONNECTING;
814 1.1 gdamore
815 1.2 tron if (sc->sc_flags & BTHID_RECONNECT)
816 1.1 gdamore callout_schedule(&sc->sc_reconnect,
817 1.1 gdamore BTHID_RETRY_INTERVAL * hz);
818 1.1 gdamore else
819 1.1 gdamore sc->sc_state = BTHID_WAIT_CTL;
820 1.2 tron } else {
821 1.2 tron /*
822 1.2 tron * The interrupt channel should have been closed first,
823 1.4 plunky * but its potentially unsafe to detach that from here.
824 1.4 plunky * Give them a second to do the right thing or let the
825 1.4 plunky * callout handle it.
826 1.2 tron */
827 1.4 plunky callout_schedule(&sc->sc_reconnect, hz);
828 1.1 gdamore }
829 1.1 gdamore }
830 1.1 gdamore
831 1.1 gdamore static void
832 1.7 christos bthidev_int_disconnected(void *arg, int err)
833 1.1 gdamore {
834 1.1 gdamore struct bthidev_softc *sc = arg;
835 1.1 gdamore
836 1.1 gdamore if (sc->sc_int != NULL) {
837 1.25 rmind l2cap_detach_pcb(&sc->sc_int);
838 1.1 gdamore sc->sc_int = NULL;
839 1.1 gdamore }
840 1.1 gdamore
841 1.1 gdamore sc->sc_state = BTHID_CLOSED;
842 1.1 gdamore
843 1.1 gdamore if (sc->sc_ctl == NULL) {
844 1.24 plunky aprint_normal_dev(sc->sc_dev, "disconnected (%d)\n", err);
845 1.2 tron sc->sc_flags &= ~BTHID_CONNECTING;
846 1.1 gdamore
847 1.2 tron if (sc->sc_flags & BTHID_RECONNECT)
848 1.1 gdamore callout_schedule(&sc->sc_reconnect,
849 1.1 gdamore BTHID_RETRY_INTERVAL * hz);
850 1.1 gdamore else
851 1.1 gdamore sc->sc_state = BTHID_WAIT_CTL;
852 1.4 plunky } else {
853 1.4 plunky /*
854 1.4 plunky * The control channel should be closing also, allow
855 1.4 plunky * them a chance to do that before we force it.
856 1.4 plunky */
857 1.4 plunky callout_schedule(&sc->sc_reconnect, hz);
858 1.1 gdamore }
859 1.1 gdamore }
860 1.1 gdamore
861 1.1 gdamore /*
862 1.1 gdamore * New Connections
863 1.1 gdamore *
864 1.1 gdamore * We give a new L2CAP handle back if this matches the BDADDR we are
865 1.1 gdamore * listening for and we are in the right state. bthidev_connected will
866 1.1 gdamore * be called when the connection is open, so nothing else to do here
867 1.1 gdamore */
868 1.1 gdamore static void *
869 1.7 christos bthidev_ctl_newconn(void *arg, struct sockaddr_bt *laddr,
870 1.6 christos struct sockaddr_bt *raddr)
871 1.1 gdamore {
872 1.1 gdamore struct bthidev_softc *sc = arg;
873 1.1 gdamore
874 1.18 plunky if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0)
875 1.18 plunky return NULL;
876 1.18 plunky
877 1.18 plunky if ((sc->sc_flags & BTHID_CONNECTING)
878 1.1 gdamore || sc->sc_state != BTHID_WAIT_CTL
879 1.1 gdamore || sc->sc_ctl != NULL
880 1.18 plunky || sc->sc_int != NULL) {
881 1.18 plunky aprint_verbose_dev(sc->sc_dev, "reject ctl newconn %s%s%s%s\n",
882 1.18 plunky (sc->sc_flags & BTHID_CONNECTING) ? " (CONNECTING)" : "",
883 1.18 plunky (sc->sc_state == BTHID_WAIT_CTL) ? " (WAITING)": "",
884 1.18 plunky (sc->sc_ctl != NULL) ? " (GOT CONTROL)" : "",
885 1.18 plunky (sc->sc_int != NULL) ? " (GOT INTERRUPT)" : "");
886 1.18 plunky
887 1.1 gdamore return NULL;
888 1.18 plunky }
889 1.1 gdamore
890 1.25 rmind l2cap_attach_pcb(&sc->sc_ctl, &bthidev_ctl_proto, sc);
891 1.1 gdamore return sc->sc_ctl;
892 1.1 gdamore }
893 1.1 gdamore
894 1.1 gdamore static void *
895 1.7 christos bthidev_int_newconn(void *arg, struct sockaddr_bt *laddr,
896 1.6 christos struct sockaddr_bt *raddr)
897 1.1 gdamore {
898 1.1 gdamore struct bthidev_softc *sc = arg;
899 1.1 gdamore
900 1.18 plunky if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0)
901 1.18 plunky return NULL;
902 1.18 plunky
903 1.18 plunky if ((sc->sc_flags & BTHID_CONNECTING)
904 1.1 gdamore || sc->sc_state != BTHID_WAIT_INT
905 1.1 gdamore || sc->sc_ctl == NULL
906 1.18 plunky || sc->sc_int != NULL) {
907 1.18 plunky aprint_verbose_dev(sc->sc_dev, "reject int newconn %s%s%s%s\n",
908 1.18 plunky (sc->sc_flags & BTHID_CONNECTING) ? " (CONNECTING)" : "",
909 1.18 plunky (sc->sc_state == BTHID_WAIT_INT) ? " (WAITING)": "",
910 1.18 plunky (sc->sc_ctl == NULL) ? " (NO CONTROL)" : "",
911 1.18 plunky (sc->sc_int != NULL) ? " (GOT INTERRUPT)" : "");
912 1.18 plunky
913 1.1 gdamore return NULL;
914 1.18 plunky }
915 1.1 gdamore
916 1.25 rmind l2cap_attach_pcb(&sc->sc_int, &bthidev_int_proto, sc);
917 1.1 gdamore return sc->sc_int;
918 1.1 gdamore }
919 1.1 gdamore
920 1.1 gdamore static void
921 1.7 christos bthidev_complete(void *arg, int count)
922 1.1 gdamore {
923 1.1 gdamore
924 1.1 gdamore /* dont care */
925 1.1 gdamore }
926 1.1 gdamore
927 1.8 plunky static void
928 1.8 plunky bthidev_linkmode(void *arg, int new)
929 1.8 plunky {
930 1.8 plunky struct bthidev_softc *sc = arg;
931 1.16 plunky int mode;
932 1.16 plunky
933 1.16 plunky (void)sockopt_getint(&sc->sc_mode, &mode);
934 1.8 plunky
935 1.16 plunky if ((mode & L2CAP_LM_AUTH) && !(new & L2CAP_LM_AUTH))
936 1.12 plunky aprint_error_dev(sc->sc_dev, "auth failed\n");
937 1.16 plunky else if ((mode & L2CAP_LM_ENCRYPT) && !(new & L2CAP_LM_ENCRYPT))
938 1.12 plunky aprint_error_dev(sc->sc_dev, "encrypt off\n");
939 1.16 plunky else if ((mode & L2CAP_LM_SECURE) && !(new & L2CAP_LM_SECURE))
940 1.12 plunky aprint_error_dev(sc->sc_dev, "insecure\n");
941 1.8 plunky else
942 1.8 plunky return;
943 1.8 plunky
944 1.8 plunky if (sc->sc_int != NULL)
945 1.28 rtr l2cap_disconnect_pcb(sc->sc_int, 0);
946 1.8 plunky
947 1.8 plunky if (sc->sc_ctl != NULL)
948 1.28 rtr l2cap_disconnect_pcb(sc->sc_ctl, 0);
949 1.8 plunky }
950 1.8 plunky
951 1.1 gdamore /*
952 1.21 plunky * Receive reports from the protocol stack. Because this will be called
953 1.21 plunky * with bt_lock held, we queue the mbuf and process it with a kernel thread
954 1.1 gdamore */
955 1.1 gdamore static void
956 1.1 gdamore bthidev_input(void *arg, struct mbuf *m)
957 1.1 gdamore {
958 1.1 gdamore struct bthidev_softc *sc = arg;
959 1.1 gdamore
960 1.21 plunky if (sc->sc_state != BTHID_OPEN) {
961 1.21 plunky m_freem(m);
962 1.21 plunky return;
963 1.1 gdamore }
964 1.1 gdamore
965 1.21 plunky mutex_enter(&sc->sc_lock);
966 1.21 plunky MBUFQ_ENQUEUE(&sc->sc_inq, m);
967 1.21 plunky cv_signal(&sc->sc_cv);
968 1.21 plunky mutex_exit(&sc->sc_lock);
969 1.1 gdamore }
970 1.1 gdamore
971 1.1 gdamore /*****************************************************************************
972 1.1 gdamore *
973 1.1 gdamore * IO routines
974 1.1 gdamore */
975 1.1 gdamore
976 1.1 gdamore static void
977 1.10 plunky bthidev_null(struct bthidev *hidev, uint8_t *report, int len)
978 1.1 gdamore {
979 1.1 gdamore
980 1.1 gdamore /*
981 1.1 gdamore * empty routine just in case the device
982 1.1 gdamore * provided no method to handle this report
983 1.1 gdamore */
984 1.1 gdamore }
985 1.1 gdamore
986 1.1 gdamore static int
987 1.10 plunky bthidev_output(struct bthidev *hidev, uint8_t *report, int rlen)
988 1.1 gdamore {
989 1.10 plunky struct bthidev_softc *sc = device_private(hidev->sc_parent);
990 1.1 gdamore struct mbuf *m;
991 1.15 ad int err;
992 1.1 gdamore
993 1.1 gdamore if (sc == NULL || sc->sc_state != BTHID_OPEN)
994 1.1 gdamore return ENOTCONN;
995 1.1 gdamore
996 1.1 gdamore KASSERT(sc->sc_ctl != NULL);
997 1.1 gdamore KASSERT(sc->sc_int != NULL);
998 1.1 gdamore
999 1.1 gdamore if (rlen == 0 || report == NULL)
1000 1.1 gdamore return 0;
1001 1.1 gdamore
1002 1.1 gdamore if (rlen > MHLEN - 2) {
1003 1.12 plunky aprint_error_dev(sc->sc_dev,
1004 1.12 plunky "output report too long (%d)!\n", rlen);
1005 1.1 gdamore return EMSGSIZE;
1006 1.1 gdamore }
1007 1.1 gdamore
1008 1.1 gdamore m = m_gethdr(M_DONTWAIT, MT_DATA);
1009 1.1 gdamore if (m == NULL)
1010 1.1 gdamore return ENOMEM;
1011 1.1 gdamore
1012 1.1 gdamore /*
1013 1.1 gdamore * data[0] = type / parameter
1014 1.1 gdamore * data[1] = id
1015 1.1 gdamore * data[2..N] = report
1016 1.1 gdamore */
1017 1.1 gdamore mtod(m, uint8_t *)[0] = (uint8_t)((BTHID_DATA << 4) | BTHID_DATA_OUTPUT);
1018 1.10 plunky mtod(m, uint8_t *)[1] = hidev->sc_id;
1019 1.1 gdamore memcpy(mtod(m, uint8_t *) + 2, report, rlen);
1020 1.1 gdamore m->m_pkthdr.len = m->m_len = rlen + 2;
1021 1.1 gdamore
1022 1.21 plunky mutex_enter(bt_lock);
1023 1.29 rtr err = l2cap_send_pcb(sc->sc_int, m);
1024 1.21 plunky mutex_exit(bt_lock);
1025 1.1 gdamore
1026 1.1 gdamore return err;
1027 1.1 gdamore }
1028