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