bthidev.c revision 1.4 1 1.4 plunky /* $NetBSD: bthidev.c,v 1.4 2006/09/12 18:18:01 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.4 plunky __KERNEL_RCSID(0, "$NetBSD: bthidev.c,v 1.4 2006/09/12 18:18:01 plunky Exp $");
36 1.1 gdamore
37 1.1 gdamore #include <sys/param.h>
38 1.1 gdamore #include <sys/conf.h>
39 1.1 gdamore #include <sys/device.h>
40 1.1 gdamore #include <sys/fcntl.h>
41 1.1 gdamore #include <sys/kernel.h>
42 1.1 gdamore #include <sys/queue.h>
43 1.1 gdamore #include <sys/malloc.h>
44 1.1 gdamore #include <sys/mbuf.h>
45 1.1 gdamore #include <sys/proc.h>
46 1.1 gdamore #include <sys/systm.h>
47 1.1 gdamore
48 1.2 tron #include <prop/proplib.h>
49 1.2 tron
50 1.1 gdamore #include <netbt/bluetooth.h>
51 1.1 gdamore #include <netbt/l2cap.h>
52 1.1 gdamore
53 1.1 gdamore #include <dev/usb/hid.h>
54 1.1 gdamore #include <dev/bluetooth/btdev.h>
55 1.1 gdamore #include <dev/bluetooth/bthid.h>
56 1.1 gdamore #include <dev/bluetooth/bthidev.h>
57 1.1 gdamore
58 1.1 gdamore #include "locators.h"
59 1.1 gdamore
60 1.1 gdamore /*****************************************************************************
61 1.1 gdamore *
62 1.1 gdamore * Bluetooth HID device
63 1.1 gdamore */
64 1.1 gdamore
65 1.1 gdamore #define MAX_DESCRIPTOR_LEN 1024 /* sanity check */
66 1.1 gdamore
67 1.1 gdamore /* bthidev softc */
68 1.1 gdamore struct bthidev_softc {
69 1.3 plunky struct btdev sc_btdev;
70 1.1 gdamore uint16_t sc_state;
71 1.1 gdamore uint16_t sc_flags;
72 1.1 gdamore
73 1.1 gdamore bdaddr_t sc_laddr; /* local address */
74 1.1 gdamore bdaddr_t sc_raddr; /* remote address */
75 1.1 gdamore
76 1.1 gdamore uint16_t sc_ctlpsm; /* control PSM */
77 1.1 gdamore struct l2cap_channel *sc_ctl; /* control channel */
78 1.1 gdamore struct l2cap_channel *sc_ctl_l; /* control listen */
79 1.1 gdamore
80 1.1 gdamore uint16_t sc_intpsm; /* interrupt PSM */
81 1.1 gdamore struct l2cap_channel *sc_int; /* interrupt channel */
82 1.1 gdamore struct l2cap_channel *sc_int_l; /* interrupt listen */
83 1.1 gdamore
84 1.1 gdamore LIST_HEAD(,bthidev) sc_list; /* child list */
85 1.1 gdamore
86 1.1 gdamore struct callout sc_reconnect;
87 1.1 gdamore int sc_attempts; /* connection attempts */
88 1.1 gdamore };
89 1.1 gdamore
90 1.2 tron /* sc_flags */
91 1.2 tron #define BTHID_RECONNECT (1 << 0) /* reconnect on link loss */
92 1.2 tron #define BTHID_CONNECTING (1 << 1) /* we are connecting */
93 1.2 tron
94 1.1 gdamore /* device state */
95 1.1 gdamore #define BTHID_CLOSED 0
96 1.1 gdamore #define BTHID_WAIT_CTL 1
97 1.1 gdamore #define BTHID_WAIT_INT 2
98 1.1 gdamore #define BTHID_OPEN 3
99 1.1 gdamore #define BTHID_DETACHING 4
100 1.1 gdamore
101 1.1 gdamore #define BTHID_RETRY_INTERVAL 5 /* seconds between connection attempts */
102 1.1 gdamore
103 1.1 gdamore /* bthidev internals */
104 1.1 gdamore static void bthidev_timeout(void *);
105 1.1 gdamore static int bthidev_listen(struct bthidev_softc *);
106 1.1 gdamore static int bthidev_connect(struct bthidev_softc *);
107 1.1 gdamore static int bthidev_output(struct bthidev *, uint8_t *, int);
108 1.1 gdamore static void bthidev_null(struct bthidev *, uint8_t *, int);
109 1.1 gdamore
110 1.1 gdamore /* autoconf(9) glue */
111 1.1 gdamore static int bthidev_match(struct device *, struct cfdata *, void *);
112 1.1 gdamore static void bthidev_attach(struct device *, struct device *, void *);
113 1.1 gdamore static int bthidev_detach(struct device *, int);
114 1.1 gdamore static int bthidev_print(void *, const char *);
115 1.1 gdamore
116 1.1 gdamore CFATTACH_DECL(bthidev, sizeof(struct bthidev_softc),
117 1.1 gdamore bthidev_match, bthidev_attach, bthidev_detach, NULL);
118 1.1 gdamore
119 1.3 plunky /* btdev identity matcher */
120 1.3 plunky static int bthidev_identify(struct btdev *, prop_dictionary_t);
121 1.3 plunky
122 1.1 gdamore /* bluetooth(9) protocol methods for L2CAP */
123 1.1 gdamore static void bthidev_connecting(void *);
124 1.1 gdamore static void bthidev_ctl_connected(void *);
125 1.1 gdamore static void bthidev_int_connected(void *);
126 1.1 gdamore static void bthidev_ctl_disconnected(void *, int);
127 1.1 gdamore static void bthidev_int_disconnected(void *, int);
128 1.1 gdamore static void *bthidev_ctl_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
129 1.1 gdamore static void *bthidev_int_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
130 1.1 gdamore static void bthidev_complete(void *, int);
131 1.1 gdamore static void bthidev_input(void *, struct mbuf *);
132 1.1 gdamore
133 1.1 gdamore static const struct btproto bthidev_ctl_proto = {
134 1.1 gdamore bthidev_connecting,
135 1.1 gdamore bthidev_ctl_connected,
136 1.1 gdamore bthidev_ctl_disconnected,
137 1.1 gdamore bthidev_ctl_newconn,
138 1.1 gdamore bthidev_complete,
139 1.1 gdamore bthidev_input,
140 1.1 gdamore };
141 1.1 gdamore
142 1.1 gdamore static const struct btproto bthidev_int_proto = {
143 1.1 gdamore bthidev_connecting,
144 1.1 gdamore bthidev_int_connected,
145 1.1 gdamore bthidev_int_disconnected,
146 1.1 gdamore bthidev_int_newconn,
147 1.1 gdamore bthidev_complete,
148 1.1 gdamore bthidev_input,
149 1.1 gdamore };
150 1.1 gdamore
151 1.1 gdamore /*****************************************************************************
152 1.1 gdamore *
153 1.1 gdamore * bthidev autoconf(9) routines
154 1.1 gdamore */
155 1.1 gdamore
156 1.1 gdamore static int
157 1.1 gdamore bthidev_match(struct device *self, struct cfdata *cfdata, void *aux)
158 1.1 gdamore {
159 1.2 tron prop_dictionary_t dict = aux;
160 1.2 tron prop_object_t obj;
161 1.1 gdamore
162 1.3 plunky obj = prop_dictionary_get(dict, BTDEVtype);
163 1.2 tron return prop_string_equals_cstring(obj, "bthidev");
164 1.1 gdamore }
165 1.1 gdamore
166 1.1 gdamore static void
167 1.1 gdamore bthidev_attach(struct device *parent, struct device *self, void *aux)
168 1.1 gdamore {
169 1.1 gdamore struct bthidev_softc *sc = (struct bthidev_softc *)self;
170 1.2 tron prop_dictionary_t dict = aux;
171 1.2 tron prop_object_t obj;
172 1.1 gdamore struct bthidev_attach_args bha;
173 1.1 gdamore struct bthidev *dev;
174 1.1 gdamore struct hid_data *d;
175 1.1 gdamore struct hid_item h;
176 1.2 tron const void *desc;
177 1.1 gdamore int locs[BTHIDBUSCF_NLOCS];
178 1.2 tron int maxid, rep, s, dlen;
179 1.1 gdamore
180 1.1 gdamore /*
181 1.1 gdamore * Init softc
182 1.1 gdamore */
183 1.1 gdamore LIST_INIT(&sc->sc_list);
184 1.1 gdamore callout_init(&sc->sc_reconnect);
185 1.1 gdamore callout_setfunc(&sc->sc_reconnect, bthidev_timeout, sc);
186 1.1 gdamore sc->sc_state = BTHID_CLOSED;
187 1.2 tron sc->sc_flags = BTHID_CONNECTING;
188 1.2 tron sc->sc_ctlpsm = L2CAP_PSM_HID_CNTL;
189 1.2 tron sc->sc_intpsm = L2CAP_PSM_HID_INTR;
190 1.1 gdamore
191 1.3 plunky sc->sc_btdev.sc_identify = bthidev_identify;
192 1.3 plunky
193 1.1 gdamore /*
194 1.2 tron * extract config from proplist
195 1.1 gdamore */
196 1.3 plunky obj = prop_dictionary_get(dict, BTDEVladdr);
197 1.2 tron bdaddr_copy(&sc->sc_laddr, prop_data_data_nocopy(obj));
198 1.2 tron
199 1.3 plunky obj = prop_dictionary_get(dict, BTDEVraddr);
200 1.2 tron bdaddr_copy(&sc->sc_raddr, prop_data_data_nocopy(obj));
201 1.1 gdamore
202 1.3 plunky obj = prop_dictionary_get(dict, BTHIDEVcontrolpsm);
203 1.3 plunky if (prop_object_type(obj) == PROP_TYPE_NUMBER) {
204 1.2 tron sc->sc_ctlpsm = prop_number_integer_value(obj);
205 1.2 tron if (L2CAP_PSM_INVALID(sc->sc_ctlpsm)) {
206 1.3 plunky aprint_error(" invalid %s\n", BTHIDEVcontrolpsm);
207 1.2 tron return;
208 1.2 tron }
209 1.2 tron }
210 1.1 gdamore
211 1.3 plunky obj = prop_dictionary_get(dict, BTHIDEVinterruptpsm);
212 1.3 plunky if (prop_object_type(obj) == PROP_TYPE_NUMBER) {
213 1.2 tron sc->sc_intpsm = prop_number_integer_value(obj);
214 1.2 tron if (L2CAP_PSM_INVALID(sc->sc_intpsm)) {
215 1.3 plunky aprint_error(" invalid %s\n", BTHIDEVinterruptpsm);
216 1.2 tron return;
217 1.2 tron }
218 1.2 tron }
219 1.2 tron
220 1.3 plunky obj = prop_dictionary_get(dict, BTHIDEVdescriptor);
221 1.3 plunky if (prop_object_type(obj) == PROP_TYPE_DATA) {
222 1.2 tron dlen = prop_data_size(obj);
223 1.2 tron desc = prop_data_data_nocopy(obj);
224 1.2 tron } else {
225 1.3 plunky aprint_error(" no %s\n", BTHIDEVdescriptor);
226 1.1 gdamore return;
227 1.1 gdamore }
228 1.2 tron
229 1.3 plunky obj = prop_dictionary_get(dict, BTHIDEVreconnect);
230 1.3 plunky if (prop_object_type(obj) == PROP_TYPE_BOOL
231 1.2 tron && !prop_bool_true(obj))
232 1.2 tron sc->sc_flags |= BTHID_RECONNECT;
233 1.1 gdamore
234 1.1 gdamore /*
235 1.1 gdamore * Parse the descriptor and attach child devices, one per report.
236 1.1 gdamore */
237 1.1 gdamore maxid = -1;
238 1.1 gdamore h.report_ID = 0;
239 1.2 tron d = hid_start_parse(desc, dlen, hid_none);
240 1.1 gdamore while (hid_get_item(d, &h)) {
241 1.1 gdamore if (h.report_ID > maxid)
242 1.1 gdamore maxid = h.report_ID;
243 1.1 gdamore }
244 1.1 gdamore hid_end_parse(d);
245 1.1 gdamore
246 1.1 gdamore if (maxid < 0) {
247 1.1 gdamore aprint_error(" no reports found\n");
248 1.1 gdamore return;
249 1.1 gdamore }
250 1.1 gdamore
251 1.1 gdamore aprint_normal("\n");
252 1.1 gdamore
253 1.1 gdamore for (rep = 0 ; rep <= maxid ; rep++) {
254 1.2 tron if (hid_report_size(desc, dlen, hid_feature, rep) == 0
255 1.2 tron && hid_report_size(desc, dlen, hid_input, rep) == 0
256 1.2 tron && hid_report_size(desc, dlen, hid_output, rep) == 0)
257 1.1 gdamore continue;
258 1.1 gdamore
259 1.2 tron bha.ba_desc = desc;
260 1.2 tron bha.ba_dlen = dlen;
261 1.1 gdamore bha.ba_input = bthidev_null;
262 1.1 gdamore bha.ba_feature = bthidev_null;
263 1.1 gdamore bha.ba_output = bthidev_output;
264 1.1 gdamore bha.ba_id = rep;
265 1.1 gdamore
266 1.1 gdamore locs[BTHIDBUSCF_REPORTID] = rep;
267 1.1 gdamore
268 1.1 gdamore dev = (struct bthidev *)config_found_sm_loc((struct device *)sc, "bthidbus",
269 1.1 gdamore locs, &bha, bthidev_print, config_stdsubmatch);
270 1.1 gdamore if (dev != NULL) {
271 1.3 plunky dev->sc_parent = (struct device *)sc;
272 1.1 gdamore dev->sc_id = rep;
273 1.1 gdamore dev->sc_input = bha.ba_input;
274 1.1 gdamore dev->sc_feature = bha.ba_feature;
275 1.1 gdamore LIST_INSERT_HEAD(&sc->sc_list, dev, sc_next);
276 1.1 gdamore }
277 1.1 gdamore }
278 1.1 gdamore
279 1.1 gdamore /*
280 1.1 gdamore * start bluetooth connections
281 1.1 gdamore */
282 1.1 gdamore s = splsoftnet();
283 1.2 tron if ((sc->sc_flags & BTHID_RECONNECT) == 0)
284 1.1 gdamore bthidev_listen(sc);
285 1.1 gdamore
286 1.2 tron if (sc->sc_flags & BTHID_CONNECTING)
287 1.1 gdamore bthidev_connect(sc);
288 1.1 gdamore splx(s);
289 1.1 gdamore }
290 1.1 gdamore
291 1.1 gdamore static int
292 1.1 gdamore bthidev_detach(struct device *self, int flags)
293 1.1 gdamore {
294 1.1 gdamore struct bthidev_softc *sc = (struct bthidev_softc *)self;
295 1.1 gdamore struct bthidev *dev;
296 1.1 gdamore int s;
297 1.1 gdamore
298 1.1 gdamore s = splsoftnet();
299 1.1 gdamore sc->sc_flags = 0; /* disable reconnecting */
300 1.1 gdamore
301 1.1 gdamore /* release interrupt listen */
302 1.1 gdamore if (sc->sc_int_l != NULL) {
303 1.1 gdamore l2cap_detach(&sc->sc_int_l);
304 1.1 gdamore sc->sc_int_l = NULL;
305 1.1 gdamore }
306 1.1 gdamore
307 1.1 gdamore /* release control listen */
308 1.1 gdamore if (sc->sc_ctl_l != NULL) {
309 1.1 gdamore l2cap_detach(&sc->sc_ctl_l);
310 1.1 gdamore sc->sc_ctl_l = NULL;
311 1.1 gdamore }
312 1.1 gdamore
313 1.1 gdamore /* close interrupt channel */
314 1.1 gdamore if (sc->sc_int != NULL) {
315 1.1 gdamore l2cap_disconnect(sc->sc_int, 0);
316 1.1 gdamore l2cap_detach(&sc->sc_int);
317 1.1 gdamore sc->sc_int = NULL;
318 1.1 gdamore }
319 1.1 gdamore
320 1.1 gdamore /* close control channel */
321 1.1 gdamore if (sc->sc_ctl != NULL) {
322 1.1 gdamore l2cap_disconnect(sc->sc_ctl, 0);
323 1.1 gdamore l2cap_detach(&sc->sc_ctl);
324 1.1 gdamore sc->sc_ctl = NULL;
325 1.1 gdamore }
326 1.1 gdamore
327 1.1 gdamore /* remove callout */
328 1.1 gdamore sc->sc_state = BTHID_DETACHING;
329 1.1 gdamore callout_stop(&sc->sc_reconnect);
330 1.1 gdamore if (callout_invoking(&sc->sc_reconnect))
331 1.1 gdamore tsleep(sc, PWAIT, "bthidetach", 0);
332 1.1 gdamore
333 1.1 gdamore splx(s);
334 1.1 gdamore
335 1.1 gdamore /* detach children */
336 1.1 gdamore while ((dev = LIST_FIRST(&sc->sc_list)) != NULL) {
337 1.1 gdamore LIST_REMOVE(dev, sc_next);
338 1.3 plunky config_detach((struct device *)dev, flags);
339 1.1 gdamore }
340 1.1 gdamore
341 1.1 gdamore return 0;
342 1.1 gdamore }
343 1.1 gdamore
344 1.1 gdamore /*
345 1.1 gdamore * bthidev config print
346 1.1 gdamore */
347 1.1 gdamore static int
348 1.1 gdamore bthidev_print(void *aux, const char *pnp)
349 1.1 gdamore {
350 1.1 gdamore struct bthidev_attach_args *ba = aux;
351 1.1 gdamore
352 1.1 gdamore if (pnp != NULL)
353 1.1 gdamore aprint_normal("%s:", pnp);
354 1.1 gdamore
355 1.1 gdamore if (ba->ba_id > 0)
356 1.1 gdamore aprint_normal(" reportid %d", ba->ba_id);
357 1.1 gdamore
358 1.1 gdamore return UNCONF;
359 1.1 gdamore }
360 1.1 gdamore
361 1.3 plunky /*
362 1.3 plunky * btdev identity matcher
363 1.3 plunky */
364 1.3 plunky static int
365 1.3 plunky bthidev_identify(struct btdev *dev, prop_dictionary_t dict)
366 1.3 plunky {
367 1.3 plunky struct bthidev_softc *sc = (struct bthidev_softc *)dev;
368 1.3 plunky prop_object_t obj;
369 1.3 plunky
370 1.3 plunky obj = prop_dictionary_get(dict, BTDEVtype);
371 1.3 plunky if (!prop_string_equals_cstring(obj, "bthidev"))
372 1.3 plunky return 0;
373 1.3 plunky
374 1.3 plunky obj = prop_dictionary_get(dict, BTDEVladdr);
375 1.3 plunky if (!prop_data_equals_data(obj, &sc->sc_laddr, sizeof(bdaddr_t)))
376 1.3 plunky return 0;
377 1.3 plunky
378 1.3 plunky obj = prop_dictionary_get(dict, BTDEVraddr);
379 1.3 plunky if (!prop_data_equals_data(obj, &sc->sc_raddr, sizeof(bdaddr_t)))
380 1.3 plunky return 0;
381 1.3 plunky
382 1.3 plunky obj = prop_dictionary_get(dict, BTHIDEVcontrolpsm);
383 1.3 plunky if (!prop_number_equals_integer(obj, sc->sc_ctlpsm))
384 1.3 plunky return 0;
385 1.3 plunky
386 1.3 plunky obj = prop_dictionary_get(dict, BTHIDEVinterruptpsm);
387 1.3 plunky if (!prop_number_equals_integer(obj, sc->sc_intpsm))
388 1.3 plunky return 0;
389 1.3 plunky
390 1.3 plunky return 1;
391 1.3 plunky }
392 1.1 gdamore
393 1.1 gdamore /*****************************************************************************
394 1.1 gdamore *
395 1.1 gdamore * bluetooth(4) HID attach/detach routines
396 1.1 gdamore */
397 1.1 gdamore
398 1.1 gdamore /*
399 1.4 plunky * callouts are scheduled after connections have been lost, in order
400 1.4 plunky * to clean up and reconnect.
401 1.1 gdamore */
402 1.1 gdamore static void
403 1.1 gdamore bthidev_timeout(void *arg)
404 1.1 gdamore {
405 1.1 gdamore struct bthidev_softc *sc = arg;
406 1.4 plunky int s;
407 1.1 gdamore
408 1.1 gdamore s = splsoftnet();
409 1.1 gdamore callout_ack(&sc->sc_reconnect);
410 1.1 gdamore
411 1.1 gdamore switch (sc->sc_state) {
412 1.1 gdamore case BTHID_CLOSED:
413 1.4 plunky if (sc->sc_int != NULL) {
414 1.4 plunky l2cap_disconnect(sc->sc_int, 0);
415 1.4 plunky break;
416 1.4 plunky }
417 1.4 plunky
418 1.4 plunky if (sc->sc_ctl != NULL) {
419 1.4 plunky l2cap_disconnect(sc->sc_ctl, 0);
420 1.4 plunky break;
421 1.4 plunky }
422 1.4 plunky
423 1.4 plunky if (sc->sc_flags & BTHID_RECONNECT) {
424 1.4 plunky sc->sc_flags |= BTHID_CONNECTING;
425 1.4 plunky bthidev_connect(sc);
426 1.4 plunky break;
427 1.4 plunky }
428 1.4 plunky
429 1.1 gdamore break;
430 1.1 gdamore
431 1.1 gdamore case BTHID_WAIT_CTL:
432 1.1 gdamore break;
433 1.1 gdamore
434 1.1 gdamore case BTHID_WAIT_INT:
435 1.1 gdamore break;
436 1.1 gdamore
437 1.1 gdamore case BTHID_OPEN:
438 1.1 gdamore break;
439 1.1 gdamore
440 1.1 gdamore case BTHID_DETACHING:
441 1.1 gdamore wakeup(sc);
442 1.1 gdamore break;
443 1.1 gdamore
444 1.1 gdamore default:
445 1.1 gdamore break;
446 1.1 gdamore }
447 1.1 gdamore splx(s);
448 1.1 gdamore }
449 1.1 gdamore
450 1.1 gdamore /*
451 1.1 gdamore * listen for our device
452 1.1 gdamore */
453 1.1 gdamore static int
454 1.1 gdamore bthidev_listen(struct bthidev_softc *sc)
455 1.1 gdamore {
456 1.1 gdamore struct sockaddr_bt sa;
457 1.1 gdamore int err;
458 1.1 gdamore
459 1.1 gdamore memset(&sa, 0, sizeof(sa));
460 1.1 gdamore sa.bt_len = sizeof(sa);
461 1.1 gdamore sa.bt_family = AF_BLUETOOTH;
462 1.1 gdamore bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
463 1.1 gdamore
464 1.1 gdamore /*
465 1.1 gdamore * Listen on control PSM
466 1.1 gdamore */
467 1.1 gdamore err = l2cap_attach(&sc->sc_ctl_l, &bthidev_ctl_proto, sc);
468 1.1 gdamore if (err)
469 1.1 gdamore return err;
470 1.1 gdamore
471 1.1 gdamore sa.bt_psm = sc->sc_ctlpsm;
472 1.1 gdamore err = l2cap_bind(sc->sc_ctl_l, &sa);
473 1.1 gdamore if (err)
474 1.1 gdamore return err;
475 1.1 gdamore
476 1.1 gdamore err = l2cap_listen(sc->sc_ctl_l);
477 1.1 gdamore if (err)
478 1.1 gdamore return err;
479 1.1 gdamore
480 1.1 gdamore /*
481 1.1 gdamore * Listen on interrupt PSM
482 1.1 gdamore */
483 1.1 gdamore err = l2cap_attach(&sc->sc_int_l, &bthidev_int_proto, sc);
484 1.1 gdamore if (err)
485 1.1 gdamore return err;
486 1.1 gdamore
487 1.1 gdamore sa.bt_psm = sc->sc_intpsm;
488 1.1 gdamore err = l2cap_bind(sc->sc_int_l, &sa);
489 1.1 gdamore if (err)
490 1.1 gdamore return err;
491 1.1 gdamore
492 1.1 gdamore err = l2cap_listen(sc->sc_int_l);
493 1.1 gdamore if (err)
494 1.1 gdamore return err;
495 1.1 gdamore
496 1.1 gdamore sc->sc_state = BTHID_WAIT_CTL;
497 1.1 gdamore return 0;
498 1.1 gdamore }
499 1.1 gdamore
500 1.1 gdamore /*
501 1.1 gdamore * start connecting to our device
502 1.1 gdamore */
503 1.1 gdamore static int
504 1.1 gdamore bthidev_connect(struct bthidev_softc *sc)
505 1.1 gdamore {
506 1.1 gdamore struct sockaddr_bt sa;
507 1.1 gdamore int err;
508 1.1 gdamore
509 1.1 gdamore if (sc->sc_attempts++ > 0)
510 1.3 plunky printf("%s: connect (#%d)\n",
511 1.3 plunky device_xname((struct device *)sc), sc->sc_attempts);
512 1.1 gdamore
513 1.1 gdamore memset(&sa, 0, sizeof(sa));
514 1.1 gdamore sa.bt_len = sizeof(sa);
515 1.1 gdamore sa.bt_family = AF_BLUETOOTH;
516 1.1 gdamore
517 1.1 gdamore err = l2cap_attach(&sc->sc_ctl, &bthidev_ctl_proto, sc);
518 1.1 gdamore if (err) {
519 1.3 plunky printf("%s: l2cap_attach failed (%d)\n",
520 1.3 plunky device_xname((struct device *)sc), err);
521 1.1 gdamore return err;
522 1.1 gdamore }
523 1.1 gdamore
524 1.1 gdamore bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
525 1.1 gdamore err = l2cap_bind(sc->sc_ctl, &sa);
526 1.1 gdamore if (err) {
527 1.3 plunky printf("%s: l2cap_bind failed (%d)\n",
528 1.3 plunky device_xname((struct device *)sc), err);
529 1.1 gdamore return err;
530 1.1 gdamore }
531 1.1 gdamore
532 1.1 gdamore sa.bt_psm = sc->sc_ctlpsm;
533 1.1 gdamore bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
534 1.1 gdamore err = l2cap_connect(sc->sc_ctl, &sa);
535 1.1 gdamore if (err) {
536 1.3 plunky printf("%s: l2cap_connect failed (%d)\n",
537 1.3 plunky device_xname((struct device *)sc), err);
538 1.1 gdamore return err;
539 1.1 gdamore }
540 1.1 gdamore
541 1.1 gdamore sc->sc_state = BTHID_WAIT_CTL;
542 1.1 gdamore return 0;
543 1.1 gdamore }
544 1.1 gdamore
545 1.1 gdamore /*****************************************************************************
546 1.1 gdamore *
547 1.1 gdamore * bluetooth(9) callback methods for L2CAP
548 1.1 gdamore *
549 1.1 gdamore * All these are called from Bluetooth Protocol code, in a soft
550 1.1 gdamore * interrupt context at IPL_SOFTNET.
551 1.1 gdamore */
552 1.1 gdamore
553 1.1 gdamore static void
554 1.1 gdamore bthidev_connecting(void *arg)
555 1.1 gdamore {
556 1.1 gdamore
557 1.1 gdamore /* dont care */
558 1.1 gdamore }
559 1.1 gdamore
560 1.1 gdamore static void
561 1.1 gdamore bthidev_ctl_connected(void *arg)
562 1.1 gdamore {
563 1.1 gdamore struct sockaddr_bt sa;
564 1.1 gdamore struct bthidev_softc *sc = arg;
565 1.1 gdamore int err;
566 1.1 gdamore
567 1.1 gdamore if (sc->sc_state != BTHID_WAIT_CTL)
568 1.1 gdamore return;
569 1.1 gdamore
570 1.1 gdamore KASSERT(sc->sc_ctl != NULL);
571 1.1 gdamore KASSERT(sc->sc_int == NULL);
572 1.1 gdamore
573 1.2 tron if (sc->sc_flags & BTHID_CONNECTING) {
574 1.1 gdamore /* initiate connect on interrupt PSM */
575 1.1 gdamore err = l2cap_attach(&sc->sc_int, &bthidev_int_proto, sc);
576 1.1 gdamore if (err)
577 1.1 gdamore goto fail;
578 1.1 gdamore
579 1.1 gdamore memset(&sa, 0, sizeof(sa));
580 1.1 gdamore sa.bt_len = sizeof(sa);
581 1.1 gdamore sa.bt_family = AF_BLUETOOTH;
582 1.1 gdamore bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
583 1.1 gdamore
584 1.1 gdamore err = l2cap_bind(sc->sc_int, &sa);
585 1.1 gdamore if (err)
586 1.1 gdamore goto fail;
587 1.1 gdamore
588 1.1 gdamore sa.bt_psm = sc->sc_intpsm;
589 1.1 gdamore bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
590 1.1 gdamore err = l2cap_connect(sc->sc_int, &sa);
591 1.1 gdamore if (err)
592 1.1 gdamore goto fail;
593 1.1 gdamore }
594 1.1 gdamore
595 1.1 gdamore sc->sc_state = BTHID_WAIT_INT;
596 1.1 gdamore return;
597 1.1 gdamore
598 1.1 gdamore fail:
599 1.1 gdamore l2cap_detach(&sc->sc_ctl);
600 1.1 gdamore sc->sc_ctl = NULL;
601 1.3 plunky
602 1.3 plunky printf("%s: connect failed (%d)\n",
603 1.3 plunky device_xname((struct device *)sc), err);
604 1.1 gdamore }
605 1.1 gdamore
606 1.1 gdamore static void
607 1.1 gdamore bthidev_int_connected(void *arg)
608 1.1 gdamore {
609 1.1 gdamore struct bthidev_softc *sc = arg;
610 1.1 gdamore
611 1.1 gdamore if (sc->sc_state != BTHID_WAIT_INT)
612 1.1 gdamore return;
613 1.1 gdamore
614 1.1 gdamore KASSERT(sc->sc_ctl != NULL);
615 1.1 gdamore KASSERT(sc->sc_int != NULL);
616 1.1 gdamore
617 1.1 gdamore sc->sc_attempts = 0;
618 1.2 tron sc->sc_flags &= ~BTHID_CONNECTING;
619 1.1 gdamore sc->sc_state = BTHID_OPEN;
620 1.1 gdamore
621 1.3 plunky printf("%s: connected\n", device_xname((struct device *)sc));
622 1.1 gdamore }
623 1.1 gdamore
624 1.1 gdamore /*
625 1.1 gdamore * Disconnected
626 1.1 gdamore *
627 1.1 gdamore * Depending on our state, this could mean several things, but essentially
628 1.2 tron * we are lost. If both channels are closed, and we are marked to reconnect,
629 1.2 tron * schedule another try otherwise just give up. They will contact us.
630 1.1 gdamore */
631 1.1 gdamore static void
632 1.1 gdamore bthidev_ctl_disconnected(void *arg, int err)
633 1.1 gdamore {
634 1.1 gdamore struct bthidev_softc *sc = arg;
635 1.1 gdamore
636 1.1 gdamore if (sc->sc_ctl != NULL) {
637 1.1 gdamore l2cap_detach(&sc->sc_ctl);
638 1.1 gdamore sc->sc_ctl = NULL;
639 1.1 gdamore }
640 1.1 gdamore
641 1.1 gdamore sc->sc_state = BTHID_CLOSED;
642 1.1 gdamore
643 1.1 gdamore if (sc->sc_int == NULL) {
644 1.3 plunky printf("%s: disconnected\n",
645 1.3 plunky device_xname((struct device *)sc));
646 1.2 tron sc->sc_flags &= ~BTHID_CONNECTING;
647 1.1 gdamore
648 1.2 tron if (sc->sc_flags & BTHID_RECONNECT)
649 1.1 gdamore callout_schedule(&sc->sc_reconnect,
650 1.1 gdamore BTHID_RETRY_INTERVAL * hz);
651 1.1 gdamore else
652 1.1 gdamore sc->sc_state = BTHID_WAIT_CTL;
653 1.2 tron } else {
654 1.2 tron /*
655 1.2 tron * The interrupt channel should have been closed first,
656 1.4 plunky * but its potentially unsafe to detach that from here.
657 1.4 plunky * Give them a second to do the right thing or let the
658 1.4 plunky * callout handle it.
659 1.2 tron */
660 1.4 plunky callout_schedule(&sc->sc_reconnect, hz);
661 1.1 gdamore }
662 1.1 gdamore }
663 1.1 gdamore
664 1.1 gdamore static void
665 1.1 gdamore bthidev_int_disconnected(void *arg, int err)
666 1.1 gdamore {
667 1.1 gdamore struct bthidev_softc *sc = arg;
668 1.1 gdamore
669 1.1 gdamore if (sc->sc_int != NULL) {
670 1.1 gdamore l2cap_detach(&sc->sc_int);
671 1.1 gdamore sc->sc_int = NULL;
672 1.1 gdamore }
673 1.1 gdamore
674 1.1 gdamore sc->sc_state = BTHID_CLOSED;
675 1.1 gdamore
676 1.1 gdamore if (sc->sc_ctl == NULL) {
677 1.3 plunky printf("%s: disconnected\n",
678 1.3 plunky device_xname((struct device *)sc));
679 1.2 tron sc->sc_flags &= ~BTHID_CONNECTING;
680 1.1 gdamore
681 1.2 tron if (sc->sc_flags & BTHID_RECONNECT)
682 1.1 gdamore callout_schedule(&sc->sc_reconnect,
683 1.1 gdamore BTHID_RETRY_INTERVAL * hz);
684 1.1 gdamore else
685 1.1 gdamore sc->sc_state = BTHID_WAIT_CTL;
686 1.4 plunky } else {
687 1.4 plunky /*
688 1.4 plunky * The control channel should be closing also, allow
689 1.4 plunky * them a chance to do that before we force it.
690 1.4 plunky */
691 1.4 plunky callout_schedule(&sc->sc_reconnect, hz);
692 1.1 gdamore }
693 1.1 gdamore }
694 1.1 gdamore
695 1.1 gdamore /*
696 1.1 gdamore * New Connections
697 1.1 gdamore *
698 1.1 gdamore * We give a new L2CAP handle back if this matches the BDADDR we are
699 1.1 gdamore * listening for and we are in the right state. bthidev_connected will
700 1.1 gdamore * be called when the connection is open, so nothing else to do here
701 1.1 gdamore */
702 1.1 gdamore static void *
703 1.1 gdamore bthidev_ctl_newconn(void *arg, struct sockaddr_bt *laddr, struct sockaddr_bt *raddr)
704 1.1 gdamore {
705 1.1 gdamore struct bthidev_softc *sc = arg;
706 1.1 gdamore
707 1.1 gdamore if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0
708 1.2 tron || (sc->sc_flags & BTHID_CONNECTING)
709 1.1 gdamore || sc->sc_state != BTHID_WAIT_CTL
710 1.1 gdamore || sc->sc_ctl != NULL
711 1.1 gdamore || sc->sc_int != NULL)
712 1.1 gdamore return NULL;
713 1.1 gdamore
714 1.1 gdamore l2cap_attach(&sc->sc_ctl, &bthidev_ctl_proto, sc);
715 1.1 gdamore return sc->sc_ctl;
716 1.1 gdamore }
717 1.1 gdamore
718 1.1 gdamore static void *
719 1.1 gdamore bthidev_int_newconn(void *arg, struct sockaddr_bt *laddr, struct sockaddr_bt *raddr)
720 1.1 gdamore {
721 1.1 gdamore struct bthidev_softc *sc = arg;
722 1.1 gdamore
723 1.1 gdamore if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0
724 1.2 tron || (sc->sc_flags & BTHID_CONNECTING)
725 1.1 gdamore || sc->sc_state != BTHID_WAIT_INT
726 1.1 gdamore || sc->sc_ctl == NULL
727 1.1 gdamore || sc->sc_int != NULL)
728 1.1 gdamore return NULL;
729 1.1 gdamore
730 1.1 gdamore l2cap_attach(&sc->sc_int, &bthidev_int_proto, sc);
731 1.1 gdamore return sc->sc_int;
732 1.1 gdamore }
733 1.1 gdamore
734 1.1 gdamore static void
735 1.1 gdamore bthidev_complete(void *arg, int count)
736 1.1 gdamore {
737 1.1 gdamore
738 1.1 gdamore /* dont care */
739 1.1 gdamore }
740 1.1 gdamore
741 1.1 gdamore /*
742 1.1 gdamore * Receive reports from the protocol stack.
743 1.1 gdamore */
744 1.1 gdamore static void
745 1.1 gdamore bthidev_input(void *arg, struct mbuf *m)
746 1.1 gdamore {
747 1.1 gdamore struct bthidev_softc *sc = arg;
748 1.1 gdamore struct bthidev *dev;
749 1.1 gdamore uint8_t *data;
750 1.1 gdamore int len;
751 1.1 gdamore
752 1.1 gdamore if (sc->sc_state != BTHID_OPEN)
753 1.1 gdamore goto release;
754 1.1 gdamore
755 1.1 gdamore if (m->m_pkthdr.len > m->m_len)
756 1.3 plunky printf("%s: truncating HID report\n",
757 1.3 plunky device_xname((struct device *)sc));
758 1.1 gdamore
759 1.1 gdamore len = m->m_len;
760 1.1 gdamore data = mtod(m, uint8_t *);
761 1.1 gdamore
762 1.1 gdamore if (BTHID_TYPE(data[0]) == BTHID_DATA) {
763 1.1 gdamore /*
764 1.1 gdamore * data[0] == type / parameter
765 1.1 gdamore * data[1] == id
766 1.1 gdamore * data[2..len] == report
767 1.1 gdamore */
768 1.1 gdamore if (len < 3)
769 1.1 gdamore goto release;
770 1.1 gdamore
771 1.1 gdamore LIST_FOREACH(dev, &sc->sc_list, sc_next) {
772 1.1 gdamore if (data[1] == dev->sc_id) {
773 1.1 gdamore switch (BTHID_DATA_PARAM(data[0])) {
774 1.1 gdamore case BTHID_DATA_INPUT:
775 1.1 gdamore (*dev->sc_input)(dev, data + 2, len - 2);
776 1.1 gdamore break;
777 1.1 gdamore
778 1.1 gdamore case BTHID_DATA_FEATURE:
779 1.1 gdamore (*dev->sc_feature)(dev, data + 2, len - 2);
780 1.1 gdamore break;
781 1.1 gdamore
782 1.1 gdamore default:
783 1.1 gdamore break;
784 1.1 gdamore }
785 1.1 gdamore
786 1.1 gdamore goto release;
787 1.1 gdamore }
788 1.1 gdamore }
789 1.1 gdamore printf("%s: report id %d, len = %d ignored\n",
790 1.3 plunky device_xname((struct device *)sc), data[1], len - 2);
791 1.1 gdamore
792 1.1 gdamore goto release;
793 1.1 gdamore }
794 1.1 gdamore
795 1.1 gdamore if (BTHID_TYPE(data[0]) == BTHID_CONTROL) {
796 1.1 gdamore if (len < 1)
797 1.1 gdamore goto release;
798 1.1 gdamore
799 1.1 gdamore if (BTHID_DATA_PARAM(data[0]) == BTHID_CONTROL_UNPLUG) {
800 1.3 plunky printf("%s: unplugged\n",
801 1.3 plunky device_xname((struct device *)sc));
802 1.1 gdamore
803 1.1 gdamore /* close interrupt channel */
804 1.1 gdamore if (sc->sc_int != NULL) {
805 1.1 gdamore l2cap_disconnect(sc->sc_int, 0);
806 1.1 gdamore l2cap_detach(&sc->sc_int);
807 1.1 gdamore sc->sc_int = NULL;
808 1.1 gdamore }
809 1.1 gdamore
810 1.1 gdamore /* close control channel */
811 1.1 gdamore if (sc->sc_ctl != NULL) {
812 1.1 gdamore l2cap_disconnect(sc->sc_ctl, 0);
813 1.1 gdamore l2cap_detach(&sc->sc_ctl);
814 1.1 gdamore sc->sc_ctl = NULL;
815 1.1 gdamore }
816 1.1 gdamore }
817 1.1 gdamore
818 1.1 gdamore goto release;
819 1.1 gdamore }
820 1.1 gdamore
821 1.1 gdamore release:
822 1.1 gdamore m_freem(m);
823 1.1 gdamore }
824 1.1 gdamore
825 1.1 gdamore /*****************************************************************************
826 1.1 gdamore *
827 1.1 gdamore * IO routines
828 1.1 gdamore */
829 1.1 gdamore
830 1.1 gdamore static void
831 1.1 gdamore bthidev_null(struct bthidev *dev, uint8_t *report, int len)
832 1.1 gdamore {
833 1.1 gdamore
834 1.1 gdamore /*
835 1.1 gdamore * empty routine just in case the device
836 1.1 gdamore * provided no method to handle this report
837 1.1 gdamore */
838 1.1 gdamore }
839 1.1 gdamore
840 1.1 gdamore static int
841 1.1 gdamore bthidev_output(struct bthidev *dev, uint8_t *report, int rlen)
842 1.1 gdamore {
843 1.1 gdamore struct bthidev_softc *sc = (struct bthidev_softc *)dev->sc_parent;
844 1.1 gdamore struct mbuf *m;
845 1.1 gdamore int s, err;
846 1.1 gdamore
847 1.1 gdamore if (sc == NULL || sc->sc_state != BTHID_OPEN)
848 1.1 gdamore return ENOTCONN;
849 1.1 gdamore
850 1.1 gdamore KASSERT(sc->sc_ctl != NULL);
851 1.1 gdamore KASSERT(sc->sc_int != NULL);
852 1.1 gdamore
853 1.1 gdamore if (rlen == 0 || report == NULL)
854 1.1 gdamore return 0;
855 1.1 gdamore
856 1.1 gdamore if (rlen > MHLEN - 2) {
857 1.1 gdamore printf("%s: output report too long (%d)!\n",
858 1.3 plunky device_xname((struct device *)sc), rlen);
859 1.1 gdamore
860 1.1 gdamore return EMSGSIZE;
861 1.1 gdamore }
862 1.1 gdamore
863 1.1 gdamore m = m_gethdr(M_DONTWAIT, MT_DATA);
864 1.1 gdamore if (m == NULL)
865 1.1 gdamore return ENOMEM;
866 1.1 gdamore
867 1.1 gdamore /*
868 1.1 gdamore * data[0] = type / parameter
869 1.1 gdamore * data[1] = id
870 1.1 gdamore * data[2..N] = report
871 1.1 gdamore */
872 1.1 gdamore mtod(m, uint8_t *)[0] = (uint8_t)((BTHID_DATA << 4) | BTHID_DATA_OUTPUT);
873 1.1 gdamore mtod(m, uint8_t *)[1] = dev->sc_id;
874 1.1 gdamore memcpy(mtod(m, uint8_t *) + 2, report, rlen);
875 1.1 gdamore m->m_pkthdr.len = m->m_len = rlen + 2;
876 1.1 gdamore
877 1.1 gdamore s = splsoftnet();
878 1.1 gdamore err = l2cap_send(sc->sc_int, m);
879 1.1 gdamore splx(s);
880 1.1 gdamore
881 1.1 gdamore return err;
882 1.1 gdamore }
883