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