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