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