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