hci_event.c revision 6ac59344ef25d5f0ebadb5663cf700d25d2a3886
1/* 2 BlueZ - Bluetooth protocol stack for Linux 3 Copyright (C) 2000-2001 Qualcomm Incorporated 4 5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License version 2 as 9 published by the Free Software Foundation; 10 11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. 14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY 15 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 20 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 22 SOFTWARE IS DISCLAIMED. 23*/ 24 25/* Bluetooth HCI event handling. */ 26 27#include <linux/module.h> 28 29#include <linux/types.h> 30#include <linux/errno.h> 31#include <linux/kernel.h> 32#include <linux/sched.h> 33#include <linux/slab.h> 34#include <linux/poll.h> 35#include <linux/fcntl.h> 36#include <linux/init.h> 37#include <linux/skbuff.h> 38#include <linux/interrupt.h> 39#include <linux/notifier.h> 40#include <net/sock.h> 41 42#include <asm/system.h> 43#include <asm/uaccess.h> 44#include <asm/unaligned.h> 45 46#include <net/bluetooth/bluetooth.h> 47#include <net/bluetooth/hci_core.h> 48 49#ifndef CONFIG_BT_HCI_CORE_DEBUG 50#undef BT_DBG 51#define BT_DBG(D...) 52#endif 53 54/* Handle HCI Event packets */ 55 56/* Command Complete OGF LINK_CTL */ 57static void hci_cc_link_ctl(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb) 58{ 59 __u8 status; 60 61 BT_DBG("%s ocf 0x%x", hdev->name, ocf); 62 63 switch (ocf) { 64 case OCF_INQUIRY_CANCEL: 65 case OCF_EXIT_PERIODIC_INQ: 66 status = *((__u8 *) skb->data); 67 68 if (status) { 69 BT_DBG("%s Inquiry cancel error: status 0x%x", hdev->name, status); 70 } else { 71 clear_bit(HCI_INQUIRY, &hdev->flags); 72 hci_req_complete(hdev, status); 73 } 74 break; 75 76 default: 77 BT_DBG("%s Command complete: ogf LINK_CTL ocf %x", hdev->name, ocf); 78 break; 79 } 80} 81 82/* Command Complete OGF LINK_POLICY */ 83static void hci_cc_link_policy(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb) 84{ 85 struct hci_conn *conn; 86 struct hci_rp_role_discovery *rd; 87 struct hci_rp_write_link_policy *lp; 88 void *sent; 89 90 BT_DBG("%s ocf 0x%x", hdev->name, ocf); 91 92 switch (ocf) { 93 case OCF_ROLE_DISCOVERY: 94 rd = (void *) skb->data; 95 96 if (rd->status) 97 break; 98 99 hci_dev_lock(hdev); 100 101 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rd->handle)); 102 if (conn) { 103 if (rd->role) 104 conn->link_mode &= ~HCI_LM_MASTER; 105 else 106 conn->link_mode |= HCI_LM_MASTER; 107 } 108 109 hci_dev_unlock(hdev); 110 break; 111 112 case OCF_WRITE_LINK_POLICY: 113 sent = hci_sent_cmd_data(hdev, OGF_LINK_POLICY, OCF_WRITE_LINK_POLICY); 114 if (!sent) 115 break; 116 117 lp = (struct hci_rp_write_link_policy *) skb->data; 118 119 if (lp->status) 120 break; 121 122 hci_dev_lock(hdev); 123 124 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(lp->handle)); 125 if (conn) { 126 __le16 policy = get_unaligned((__le16 *) (sent + 2)); 127 conn->link_policy = __le16_to_cpu(policy); 128 } 129 130 hci_dev_unlock(hdev); 131 break; 132 133 default: 134 BT_DBG("%s: Command complete: ogf LINK_POLICY ocf %x", 135 hdev->name, ocf); 136 break; 137 } 138} 139 140/* Command Complete OGF HOST_CTL */ 141static void hci_cc_host_ctl(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb) 142{ 143 __u8 status, param; 144 __u16 setting; 145 struct hci_rp_read_voice_setting *vs; 146 void *sent; 147 148 BT_DBG("%s ocf 0x%x", hdev->name, ocf); 149 150 switch (ocf) { 151 case OCF_RESET: 152 status = *((__u8 *) skb->data); 153 hci_req_complete(hdev, status); 154 break; 155 156 case OCF_SET_EVENT_FLT: 157 status = *((__u8 *) skb->data); 158 if (status) { 159 BT_DBG("%s SET_EVENT_FLT failed %d", hdev->name, status); 160 } else { 161 BT_DBG("%s SET_EVENT_FLT succeseful", hdev->name); 162 } 163 break; 164 165 case OCF_WRITE_AUTH_ENABLE: 166 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_AUTH_ENABLE); 167 if (!sent) 168 break; 169 170 status = *((__u8 *) skb->data); 171 param = *((__u8 *) sent); 172 173 if (!status) { 174 if (param == AUTH_ENABLED) 175 set_bit(HCI_AUTH, &hdev->flags); 176 else 177 clear_bit(HCI_AUTH, &hdev->flags); 178 } 179 hci_req_complete(hdev, status); 180 break; 181 182 case OCF_WRITE_ENCRYPT_MODE: 183 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_ENCRYPT_MODE); 184 if (!sent) 185 break; 186 187 status = *((__u8 *) skb->data); 188 param = *((__u8 *) sent); 189 190 if (!status) { 191 if (param) 192 set_bit(HCI_ENCRYPT, &hdev->flags); 193 else 194 clear_bit(HCI_ENCRYPT, &hdev->flags); 195 } 196 hci_req_complete(hdev, status); 197 break; 198 199 case OCF_WRITE_CA_TIMEOUT: 200 status = *((__u8 *) skb->data); 201 if (status) { 202 BT_DBG("%s OCF_WRITE_CA_TIMEOUT failed %d", hdev->name, status); 203 } else { 204 BT_DBG("%s OCF_WRITE_CA_TIMEOUT succeseful", hdev->name); 205 } 206 break; 207 208 case OCF_WRITE_PG_TIMEOUT: 209 status = *((__u8 *) skb->data); 210 if (status) { 211 BT_DBG("%s OCF_WRITE_PG_TIMEOUT failed %d", hdev->name, status); 212 } else { 213 BT_DBG("%s: OCF_WRITE_PG_TIMEOUT succeseful", hdev->name); 214 } 215 break; 216 217 case OCF_WRITE_SCAN_ENABLE: 218 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_SCAN_ENABLE); 219 if (!sent) 220 break; 221 222 status = *((__u8 *) skb->data); 223 param = *((__u8 *) sent); 224 225 BT_DBG("param 0x%x", param); 226 227 if (!status) { 228 clear_bit(HCI_PSCAN, &hdev->flags); 229 clear_bit(HCI_ISCAN, &hdev->flags); 230 if (param & SCAN_INQUIRY) 231 set_bit(HCI_ISCAN, &hdev->flags); 232 233 if (param & SCAN_PAGE) 234 set_bit(HCI_PSCAN, &hdev->flags); 235 } 236 hci_req_complete(hdev, status); 237 break; 238 239 case OCF_READ_VOICE_SETTING: 240 vs = (struct hci_rp_read_voice_setting *) skb->data; 241 242 if (vs->status) { 243 BT_DBG("%s READ_VOICE_SETTING failed %d", hdev->name, vs->status); 244 break; 245 } 246 247 setting = __le16_to_cpu(vs->voice_setting); 248 249 if (hdev->voice_setting != setting ) { 250 hdev->voice_setting = setting; 251 252 BT_DBG("%s: voice setting 0x%04x", hdev->name, setting); 253 254 if (hdev->notify) { 255 tasklet_disable(&hdev->tx_task); 256 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING); 257 tasklet_enable(&hdev->tx_task); 258 } 259 } 260 break; 261 262 case OCF_WRITE_VOICE_SETTING: 263 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_VOICE_SETTING); 264 if (!sent) 265 break; 266 267 status = *((__u8 *) skb->data); 268 setting = __le16_to_cpu(get_unaligned((__le16 *) sent)); 269 270 if (!status && hdev->voice_setting != setting) { 271 hdev->voice_setting = setting; 272 273 BT_DBG("%s: voice setting 0x%04x", hdev->name, setting); 274 275 if (hdev->notify) { 276 tasklet_disable(&hdev->tx_task); 277 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING); 278 tasklet_enable(&hdev->tx_task); 279 } 280 } 281 hci_req_complete(hdev, status); 282 break; 283 284 case OCF_HOST_BUFFER_SIZE: 285 status = *((__u8 *) skb->data); 286 if (status) { 287 BT_DBG("%s OCF_BUFFER_SIZE failed %d", hdev->name, status); 288 hci_req_complete(hdev, status); 289 } 290 break; 291 292 default: 293 BT_DBG("%s Command complete: ogf HOST_CTL ocf %x", hdev->name, ocf); 294 break; 295 } 296} 297 298/* Command Complete OGF INFO_PARAM */ 299static void hci_cc_info_param(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb) 300{ 301 struct hci_rp_read_loc_version *lv; 302 struct hci_rp_read_local_features *lf; 303 struct hci_rp_read_buffer_size *bs; 304 struct hci_rp_read_bd_addr *ba; 305 306 BT_DBG("%s ocf 0x%x", hdev->name, ocf); 307 308 switch (ocf) { 309 case OCF_READ_LOCAL_VERSION: 310 lv = (struct hci_rp_read_loc_version *) skb->data; 311 312 if (lv->status) { 313 BT_DBG("%s READ_LOCAL_VERSION failed %d", hdev->name, lf->status); 314 break; 315 } 316 317 hdev->hci_ver = lv->hci_ver; 318 hdev->hci_rev = btohs(lv->hci_rev); 319 hdev->manufacturer = btohs(lv->manufacturer); 320 321 BT_DBG("%s: manufacturer %d hci_ver %d hci_rev %d", hdev->name, 322 hdev->manufacturer, hdev->hci_ver, hdev->hci_rev); 323 324 break; 325 326 case OCF_READ_LOCAL_FEATURES: 327 lf = (struct hci_rp_read_local_features *) skb->data; 328 329 if (lf->status) { 330 BT_DBG("%s READ_LOCAL_FEATURES failed %d", hdev->name, lf->status); 331 break; 332 } 333 334 memcpy(hdev->features, lf->features, sizeof(hdev->features)); 335 336 /* Adjust default settings according to features 337 * supported by device. */ 338 if (hdev->features[0] & LMP_3SLOT) 339 hdev->pkt_type |= (HCI_DM3 | HCI_DH3); 340 341 if (hdev->features[0] & LMP_5SLOT) 342 hdev->pkt_type |= (HCI_DM5 | HCI_DH5); 343 344 if (hdev->features[1] & LMP_HV2) 345 hdev->pkt_type |= (HCI_HV2); 346 347 if (hdev->features[1] & LMP_HV3) 348 hdev->pkt_type |= (HCI_HV3); 349 350 BT_DBG("%s: features 0x%x 0x%x 0x%x", hdev->name, 351 lf->features[0], lf->features[1], lf->features[2]); 352 353 break; 354 355 case OCF_READ_BUFFER_SIZE: 356 bs = (struct hci_rp_read_buffer_size *) skb->data; 357 358 if (bs->status) { 359 BT_DBG("%s READ_BUFFER_SIZE failed %d", hdev->name, bs->status); 360 hci_req_complete(hdev, bs->status); 361 break; 362 } 363 364 hdev->acl_mtu = __le16_to_cpu(bs->acl_mtu); 365 hdev->sco_mtu = bs->sco_mtu; 366 hdev->acl_pkts = __le16_to_cpu(bs->acl_max_pkt); 367 hdev->sco_pkts = __le16_to_cpu(bs->sco_max_pkt); 368 369 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) { 370 hdev->sco_mtu = 64; 371 hdev->sco_pkts = 8; 372 } 373 374 hdev->acl_cnt = hdev->acl_pkts; 375 hdev->sco_cnt = hdev->sco_pkts; 376 377 BT_DBG("%s mtu: acl %d, sco %d max_pkt: acl %d, sco %d", hdev->name, 378 hdev->acl_mtu, hdev->sco_mtu, hdev->acl_pkts, hdev->sco_pkts); 379 break; 380 381 case OCF_READ_BD_ADDR: 382 ba = (struct hci_rp_read_bd_addr *) skb->data; 383 384 if (!ba->status) { 385 bacpy(&hdev->bdaddr, &ba->bdaddr); 386 } else { 387 BT_DBG("%s: READ_BD_ADDR failed %d", hdev->name, ba->status); 388 } 389 390 hci_req_complete(hdev, ba->status); 391 break; 392 393 default: 394 BT_DBG("%s Command complete: ogf INFO_PARAM ocf %x", hdev->name, ocf); 395 break; 396 } 397} 398 399/* Command Status OGF LINK_CTL */ 400static inline void hci_cs_create_conn(struct hci_dev *hdev, __u8 status) 401{ 402 struct hci_conn *conn; 403 struct hci_cp_create_conn *cp = hci_sent_cmd_data(hdev, OGF_LINK_CTL, OCF_CREATE_CONN); 404 405 if (!cp) 406 return; 407 408 hci_dev_lock(hdev); 409 410 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr); 411 412 BT_DBG("%s status 0x%x bdaddr %s conn %p", hdev->name, 413 status, batostr(&cp->bdaddr), conn); 414 415 if (status) { 416 if (conn && conn->state == BT_CONNECT) { 417 conn->state = BT_CLOSED; 418 hci_proto_connect_cfm(conn, status); 419 hci_conn_del(conn); 420 } 421 } else { 422 if (!conn) { 423 conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr); 424 if (conn) { 425 conn->out = 1; 426 conn->link_mode |= HCI_LM_MASTER; 427 } else 428 BT_ERR("No memmory for new connection"); 429 } 430 } 431 432 hci_dev_unlock(hdev); 433} 434 435static void hci_cs_link_ctl(struct hci_dev *hdev, __u16 ocf, __u8 status) 436{ 437 BT_DBG("%s ocf 0x%x", hdev->name, ocf); 438 439 switch (ocf) { 440 case OCF_CREATE_CONN: 441 hci_cs_create_conn(hdev, status); 442 break; 443 444 case OCF_ADD_SCO: 445 if (status) { 446 struct hci_conn *acl, *sco; 447 struct hci_cp_add_sco *cp = hci_sent_cmd_data(hdev, OGF_LINK_CTL, OCF_ADD_SCO); 448 __u16 handle; 449 450 if (!cp) 451 break; 452 453 handle = __le16_to_cpu(cp->handle); 454 455 BT_DBG("%s Add SCO error: handle %d status 0x%x", hdev->name, handle, status); 456 457 hci_dev_lock(hdev); 458 459 acl = hci_conn_hash_lookup_handle(hdev, handle); 460 if (acl && (sco = acl->link)) { 461 sco->state = BT_CLOSED; 462 463 hci_proto_connect_cfm(sco, status); 464 hci_conn_del(sco); 465 } 466 467 hci_dev_unlock(hdev); 468 } 469 break; 470 471 case OCF_INQUIRY: 472 if (status) { 473 BT_DBG("%s Inquiry error: status 0x%x", hdev->name, status); 474 hci_req_complete(hdev, status); 475 } else { 476 set_bit(HCI_INQUIRY, &hdev->flags); 477 } 478 break; 479 480 default: 481 BT_DBG("%s Command status: ogf LINK_CTL ocf %x status %d", 482 hdev->name, ocf, status); 483 break; 484 } 485} 486 487/* Command Status OGF LINK_POLICY */ 488static void hci_cs_link_policy(struct hci_dev *hdev, __u16 ocf, __u8 status) 489{ 490 BT_DBG("%s ocf 0x%x", hdev->name, ocf); 491 492 switch (ocf) { 493 case OCF_SNIFF_MODE: 494 if (status) { 495 struct hci_conn *conn; 496 struct hci_cp_sniff_mode *cp = hci_sent_cmd_data(hdev, OGF_LINK_POLICY, OCF_SNIFF_MODE); 497 498 if (!cp) 499 break; 500 501 hci_dev_lock(hdev); 502 503 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle)); 504 if (conn) { 505 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend); 506 } 507 508 hci_dev_unlock(hdev); 509 } 510 break; 511 512 case OCF_EXIT_SNIFF_MODE: 513 if (status) { 514 struct hci_conn *conn; 515 struct hci_cp_exit_sniff_mode *cp = hci_sent_cmd_data(hdev, OGF_LINK_POLICY, OCF_EXIT_SNIFF_MODE); 516 517 if (!cp) 518 break; 519 520 hci_dev_lock(hdev); 521 522 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle)); 523 if (conn) { 524 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend); 525 } 526 527 hci_dev_unlock(hdev); 528 } 529 break; 530 531 default: 532 BT_DBG("%s Command status: ogf LINK_POLICY ocf %x", hdev->name, ocf); 533 break; 534 } 535} 536 537/* Command Status OGF HOST_CTL */ 538static void hci_cs_host_ctl(struct hci_dev *hdev, __u16 ocf, __u8 status) 539{ 540 BT_DBG("%s ocf 0x%x", hdev->name, ocf); 541 542 switch (ocf) { 543 default: 544 BT_DBG("%s Command status: ogf HOST_CTL ocf %x", hdev->name, ocf); 545 break; 546 } 547} 548 549/* Command Status OGF INFO_PARAM */ 550static void hci_cs_info_param(struct hci_dev *hdev, __u16 ocf, __u8 status) 551{ 552 BT_DBG("%s: hci_cs_info_param: ocf 0x%x", hdev->name, ocf); 553 554 switch (ocf) { 555 default: 556 BT_DBG("%s Command status: ogf INFO_PARAM ocf %x", hdev->name, ocf); 557 break; 558 } 559} 560 561/* Inquiry Complete */ 562static inline void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb) 563{ 564 __u8 status = *((__u8 *) skb->data); 565 566 BT_DBG("%s status %d", hdev->name, status); 567 568 clear_bit(HCI_INQUIRY, &hdev->flags); 569 hci_req_complete(hdev, status); 570} 571 572/* Inquiry Result */ 573static inline void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb) 574{ 575 struct inquiry_data data; 576 struct inquiry_info *info = (struct inquiry_info *) (skb->data + 1); 577 int num_rsp = *((__u8 *) skb->data); 578 579 BT_DBG("%s num_rsp %d", hdev->name, num_rsp); 580 581 if (!num_rsp) 582 return; 583 584 hci_dev_lock(hdev); 585 586 for (; num_rsp; num_rsp--) { 587 bacpy(&data.bdaddr, &info->bdaddr); 588 data.pscan_rep_mode = info->pscan_rep_mode; 589 data.pscan_period_mode = info->pscan_period_mode; 590 data.pscan_mode = info->pscan_mode; 591 memcpy(data.dev_class, info->dev_class, 3); 592 data.clock_offset = info->clock_offset; 593 data.rssi = 0x00; 594 info++; 595 hci_inquiry_cache_update(hdev, &data); 596 } 597 598 hci_dev_unlock(hdev); 599} 600 601/* Inquiry Result With RSSI */ 602static inline void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, struct sk_buff *skb) 603{ 604 struct inquiry_data data; 605 int num_rsp = *((__u8 *) skb->data); 606 607 BT_DBG("%s num_rsp %d", hdev->name, num_rsp); 608 609 if (!num_rsp) 610 return; 611 612 hci_dev_lock(hdev); 613 614 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) { 615 struct inquiry_info_with_rssi_and_pscan_mode *info = 616 (struct inquiry_info_with_rssi_and_pscan_mode *) (skb->data + 1); 617 618 for (; num_rsp; num_rsp--) { 619 bacpy(&data.bdaddr, &info->bdaddr); 620 data.pscan_rep_mode = info->pscan_rep_mode; 621 data.pscan_period_mode = info->pscan_period_mode; 622 data.pscan_mode = info->pscan_mode; 623 memcpy(data.dev_class, info->dev_class, 3); 624 data.clock_offset = info->clock_offset; 625 data.rssi = info->rssi; 626 info++; 627 hci_inquiry_cache_update(hdev, &data); 628 } 629 } else { 630 struct inquiry_info_with_rssi *info = 631 (struct inquiry_info_with_rssi *) (skb->data + 1); 632 633 for (; num_rsp; num_rsp--) { 634 bacpy(&data.bdaddr, &info->bdaddr); 635 data.pscan_rep_mode = info->pscan_rep_mode; 636 data.pscan_period_mode = info->pscan_period_mode; 637 data.pscan_mode = 0x00; 638 memcpy(data.dev_class, info->dev_class, 3); 639 data.clock_offset = info->clock_offset; 640 data.rssi = info->rssi; 641 info++; 642 hci_inquiry_cache_update(hdev, &data); 643 } 644 } 645 646 hci_dev_unlock(hdev); 647} 648 649/* Extended Inquiry Result */ 650static inline void hci_extended_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb) 651{ 652 struct inquiry_data data; 653 struct extended_inquiry_info *info = (struct extended_inquiry_info *) (skb->data + 1); 654 int num_rsp = *((__u8 *) skb->data); 655 656 BT_DBG("%s num_rsp %d", hdev->name, num_rsp); 657 658 if (!num_rsp) 659 return; 660 661 hci_dev_lock(hdev); 662 663 for (; num_rsp; num_rsp--) { 664 bacpy(&data.bdaddr, &info->bdaddr); 665 data.pscan_rep_mode = info->pscan_rep_mode; 666 data.pscan_period_mode = info->pscan_period_mode; 667 data.pscan_mode = 0x00; 668 memcpy(data.dev_class, info->dev_class, 3); 669 data.clock_offset = info->clock_offset; 670 data.rssi = info->rssi; 671 info++; 672 hci_inquiry_cache_update(hdev, &data); 673 } 674 675 hci_dev_unlock(hdev); 676} 677 678/* Connect Request */ 679static inline void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb) 680{ 681 struct hci_ev_conn_request *ev = (struct hci_ev_conn_request *) skb->data; 682 int mask = hdev->link_mode; 683 684 BT_DBG("%s Connection request: %s type 0x%x", hdev->name, 685 batostr(&ev->bdaddr), ev->link_type); 686 687 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type); 688 689 if (mask & HCI_LM_ACCEPT) { 690 /* Connection accepted */ 691 struct hci_conn *conn; 692 struct hci_cp_accept_conn_req cp; 693 694 hci_dev_lock(hdev); 695 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr); 696 if (!conn) { 697 if (!(conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr))) { 698 BT_ERR("No memmory for new connection"); 699 hci_dev_unlock(hdev); 700 return; 701 } 702 } 703 memcpy(conn->dev_class, ev->dev_class, 3); 704 conn->state = BT_CONNECT; 705 hci_dev_unlock(hdev); 706 707 bacpy(&cp.bdaddr, &ev->bdaddr); 708 709 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER)) 710 cp.role = 0x00; /* Become master */ 711 else 712 cp.role = 0x01; /* Remain slave */ 713 714 hci_send_cmd(hdev, OGF_LINK_CTL, 715 OCF_ACCEPT_CONN_REQ, sizeof(cp), &cp); 716 } else { 717 /* Connection rejected */ 718 struct hci_cp_reject_conn_req cp; 719 720 bacpy(&cp.bdaddr, &ev->bdaddr); 721 cp.reason = 0x0f; 722 hci_send_cmd(hdev, OGF_LINK_CTL, 723 OCF_REJECT_CONN_REQ, sizeof(cp), &cp); 724 } 725} 726 727/* Connect Complete */ 728static inline void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb) 729{ 730 struct hci_ev_conn_complete *ev = (struct hci_ev_conn_complete *) skb->data; 731 struct hci_conn *conn; 732 733 BT_DBG("%s", hdev->name); 734 735 hci_dev_lock(hdev); 736 737 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr); 738 if (!conn) { 739 hci_dev_unlock(hdev); 740 return; 741 } 742 743 if (!ev->status) { 744 conn->handle = __le16_to_cpu(ev->handle); 745 conn->state = BT_CONNECTED; 746 747 if (test_bit(HCI_AUTH, &hdev->flags)) 748 conn->link_mode |= HCI_LM_AUTH; 749 750 if (test_bit(HCI_ENCRYPT, &hdev->flags)) 751 conn->link_mode |= HCI_LM_ENCRYPT; 752 753 hci_conn_hold(conn); 754 755 /* Get remote features */ 756 if (conn->type == ACL_LINK) { 757 struct hci_cp_read_remote_features cp; 758 cp.handle = ev->handle; 759 hci_send_cmd(hdev, OGF_LINK_CTL, 760 OCF_READ_REMOTE_FEATURES, sizeof(cp), &cp); 761 } 762 763 /* Set link policy */ 764 if (conn->type == ACL_LINK && hdev->link_policy) { 765 struct hci_cp_write_link_policy cp; 766 cp.handle = ev->handle; 767 cp.policy = __cpu_to_le16(hdev->link_policy); 768 hci_send_cmd(hdev, OGF_LINK_POLICY, 769 OCF_WRITE_LINK_POLICY, sizeof(cp), &cp); 770 } 771 772 /* Set packet type for incoming connection */ 773 if (!conn->out) { 774 struct hci_cp_change_conn_ptype cp; 775 cp.handle = ev->handle; 776 cp.pkt_type = (conn->type == ACL_LINK) ? 777 __cpu_to_le16(hdev->pkt_type & ACL_PTYPE_MASK): 778 __cpu_to_le16(hdev->pkt_type & SCO_PTYPE_MASK); 779 780 hci_send_cmd(hdev, OGF_LINK_CTL, 781 OCF_CHANGE_CONN_PTYPE, sizeof(cp), &cp); 782 } 783 784 hci_conn_put(conn); 785 } else 786 conn->state = BT_CLOSED; 787 788 if (conn->type == ACL_LINK) { 789 struct hci_conn *sco = conn->link; 790 if (sco) { 791 if (!ev->status) 792 hci_add_sco(sco, conn->handle); 793 else { 794 hci_proto_connect_cfm(sco, ev->status); 795 hci_conn_del(sco); 796 } 797 } 798 } 799 800 hci_proto_connect_cfm(conn, ev->status); 801 if (ev->status) 802 hci_conn_del(conn); 803 804 hci_dev_unlock(hdev); 805} 806 807/* Disconnect Complete */ 808static inline void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb) 809{ 810 struct hci_ev_disconn_complete *ev = (struct hci_ev_disconn_complete *) skb->data; 811 struct hci_conn *conn; 812 813 BT_DBG("%s status %d", hdev->name, ev->status); 814 815 if (ev->status) 816 return; 817 818 hci_dev_lock(hdev); 819 820 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle)); 821 if (conn) { 822 conn->state = BT_CLOSED; 823 hci_proto_disconn_ind(conn, ev->reason); 824 hci_conn_del(conn); 825 } 826 827 hci_dev_unlock(hdev); 828} 829 830/* Number of completed packets */ 831static inline void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb) 832{ 833 struct hci_ev_num_comp_pkts *ev = (struct hci_ev_num_comp_pkts *) skb->data; 834 __le16 *ptr; 835 int i; 836 837 skb_pull(skb, sizeof(*ev)); 838 839 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl); 840 841 if (skb->len < ev->num_hndl * 4) { 842 BT_DBG("%s bad parameters", hdev->name); 843 return; 844 } 845 846 tasklet_disable(&hdev->tx_task); 847 848 for (i = 0, ptr = (__le16 *) skb->data; i < ev->num_hndl; i++) { 849 struct hci_conn *conn; 850 __u16 handle, count; 851 852 handle = __le16_to_cpu(get_unaligned(ptr++)); 853 count = __le16_to_cpu(get_unaligned(ptr++)); 854 855 conn = hci_conn_hash_lookup_handle(hdev, handle); 856 if (conn) { 857 conn->sent -= count; 858 859 if (conn->type == SCO_LINK) { 860 if ((hdev->sco_cnt += count) > hdev->sco_pkts) 861 hdev->sco_cnt = hdev->sco_pkts; 862 } else { 863 if ((hdev->acl_cnt += count) > hdev->acl_pkts) 864 hdev->acl_cnt = hdev->acl_pkts; 865 } 866 } 867 } 868 hci_sched_tx(hdev); 869 870 tasklet_enable(&hdev->tx_task); 871} 872 873/* Role Change */ 874static inline void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb) 875{ 876 struct hci_ev_role_change *ev = (struct hci_ev_role_change *) skb->data; 877 struct hci_conn *conn; 878 879 BT_DBG("%s status %d", hdev->name, ev->status); 880 881 hci_dev_lock(hdev); 882 883 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr); 884 if (conn) { 885 if (!ev->status) { 886 if (ev->role) 887 conn->link_mode &= ~HCI_LM_MASTER; 888 else 889 conn->link_mode |= HCI_LM_MASTER; 890 } 891 892 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->pend); 893 894 hci_role_switch_cfm(conn, ev->status, ev->role); 895 } 896 897 hci_dev_unlock(hdev); 898} 899 900/* Mode Change */ 901static inline void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb) 902{ 903 struct hci_ev_mode_change *ev = (struct hci_ev_mode_change *) skb->data; 904 struct hci_conn *conn; 905 906 BT_DBG("%s status %d", hdev->name, ev->status); 907 908 hci_dev_lock(hdev); 909 910 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle)); 911 if (conn) { 912 conn->mode = ev->mode; 913 conn->interval = __le16_to_cpu(ev->interval); 914 915 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) { 916 if (conn->mode == HCI_CM_ACTIVE) 917 conn->power_save = 1; 918 else 919 conn->power_save = 0; 920 } 921 } 922 923 hci_dev_unlock(hdev); 924} 925 926/* Authentication Complete */ 927static inline void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb) 928{ 929 struct hci_ev_auth_complete *ev = (struct hci_ev_auth_complete *) skb->data; 930 struct hci_conn *conn; 931 932 BT_DBG("%s status %d", hdev->name, ev->status); 933 934 hci_dev_lock(hdev); 935 936 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle)); 937 if (conn) { 938 if (!ev->status) 939 conn->link_mode |= HCI_LM_AUTH; 940 941 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend); 942 943 hci_auth_cfm(conn, ev->status); 944 945 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) { 946 if (!ev->status) { 947 struct hci_cp_set_conn_encrypt cp; 948 cp.handle = __cpu_to_le16(conn->handle); 949 cp.encrypt = 1; 950 hci_send_cmd(conn->hdev, OGF_LINK_CTL, 951 OCF_SET_CONN_ENCRYPT, sizeof(cp), &cp); 952 } else { 953 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend); 954 hci_encrypt_cfm(conn, ev->status, 0x00); 955 } 956 } 957 } 958 959 hci_dev_unlock(hdev); 960} 961 962/* Encryption Change */ 963static inline void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb) 964{ 965 struct hci_ev_encrypt_change *ev = (struct hci_ev_encrypt_change *) skb->data; 966 struct hci_conn *conn; 967 968 BT_DBG("%s status %d", hdev->name, ev->status); 969 970 hci_dev_lock(hdev); 971 972 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle)); 973 if (conn) { 974 if (!ev->status) { 975 if (ev->encrypt) 976 conn->link_mode |= HCI_LM_ENCRYPT; 977 else 978 conn->link_mode &= ~HCI_LM_ENCRYPT; 979 } 980 981 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend); 982 983 hci_encrypt_cfm(conn, ev->status, ev->encrypt); 984 } 985 986 hci_dev_unlock(hdev); 987} 988 989/* Change Connection Link Key Complete */ 990static inline void hci_change_conn_link_key_complete_evt(struct hci_dev *hdev, struct sk_buff *skb) 991{ 992 struct hci_ev_change_conn_link_key_complete *ev = (struct hci_ev_change_conn_link_key_complete *) skb->data; 993 struct hci_conn *conn; 994 995 BT_DBG("%s status %d", hdev->name, ev->status); 996 997 hci_dev_lock(hdev); 998 999 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle)); 1000 if (conn) { 1001 if (!ev->status) 1002 conn->link_mode |= HCI_LM_SECURE; 1003 1004 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend); 1005 1006 hci_key_change_cfm(conn, ev->status); 1007 } 1008 1009 hci_dev_unlock(hdev); 1010} 1011 1012/* Pin Code Request*/ 1013static inline void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb) 1014{ 1015} 1016 1017/* Link Key Request */ 1018static inline void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb) 1019{ 1020} 1021 1022/* Link Key Notification */ 1023static inline void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb) 1024{ 1025} 1026 1027/* Remote Features */ 1028static inline void hci_remote_features_evt(struct hci_dev *hdev, struct sk_buff *skb) 1029{ 1030 struct hci_ev_remote_features *ev = (struct hci_ev_remote_features *) skb->data; 1031 struct hci_conn *conn; 1032 1033 BT_DBG("%s status %d", hdev->name, ev->status); 1034 1035 hci_dev_lock(hdev); 1036 1037 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle)); 1038 if (conn && !ev->status) { 1039 memcpy(conn->features, ev->features, sizeof(conn->features)); 1040 } 1041 1042 hci_dev_unlock(hdev); 1043} 1044 1045/* Clock Offset */ 1046static inline void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb) 1047{ 1048 struct hci_ev_clock_offset *ev = (struct hci_ev_clock_offset *) skb->data; 1049 struct hci_conn *conn; 1050 1051 BT_DBG("%s status %d", hdev->name, ev->status); 1052 1053 hci_dev_lock(hdev); 1054 1055 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle)); 1056 if (conn && !ev->status) { 1057 struct inquiry_entry *ie; 1058 1059 if ((ie = hci_inquiry_cache_lookup(hdev, &conn->dst))) { 1060 ie->data.clock_offset = ev->clock_offset; 1061 ie->timestamp = jiffies; 1062 } 1063 } 1064 1065 hci_dev_unlock(hdev); 1066} 1067 1068/* Page Scan Repetition Mode */ 1069static inline void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb) 1070{ 1071 struct hci_ev_pscan_rep_mode *ev = (struct hci_ev_pscan_rep_mode *) skb->data; 1072 struct inquiry_entry *ie; 1073 1074 BT_DBG("%s", hdev->name); 1075 1076 hci_dev_lock(hdev); 1077 1078 if ((ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr))) { 1079 ie->data.pscan_rep_mode = ev->pscan_rep_mode; 1080 ie->timestamp = jiffies; 1081 } 1082 1083 hci_dev_unlock(hdev); 1084} 1085 1086/* Sniff Subrate */ 1087static inline void hci_sniff_subrate_evt(struct hci_dev *hdev, struct sk_buff *skb) 1088{ 1089 struct hci_ev_sniff_subrate *ev = (struct hci_ev_sniff_subrate *) skb->data; 1090 struct hci_conn *conn; 1091 1092 BT_DBG("%s status %d", hdev->name, ev->status); 1093 1094 hci_dev_lock(hdev); 1095 1096 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle)); 1097 if (conn) { 1098 } 1099 1100 hci_dev_unlock(hdev); 1101} 1102 1103void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb) 1104{ 1105 struct hci_event_hdr *hdr = (struct hci_event_hdr *) skb->data; 1106 struct hci_ev_cmd_complete *ec; 1107 struct hci_ev_cmd_status *cs; 1108 u16 opcode, ocf, ogf; 1109 1110 skb_pull(skb, HCI_EVENT_HDR_SIZE); 1111 1112 BT_DBG("%s evt 0x%x", hdev->name, hdr->evt); 1113 1114 switch (hdr->evt) { 1115 case HCI_EV_NUM_COMP_PKTS: 1116 hci_num_comp_pkts_evt(hdev, skb); 1117 break; 1118 1119 case HCI_EV_INQUIRY_COMPLETE: 1120 hci_inquiry_complete_evt(hdev, skb); 1121 break; 1122 1123 case HCI_EV_INQUIRY_RESULT: 1124 hci_inquiry_result_evt(hdev, skb); 1125 break; 1126 1127 case HCI_EV_INQUIRY_RESULT_WITH_RSSI: 1128 hci_inquiry_result_with_rssi_evt(hdev, skb); 1129 break; 1130 1131 case HCI_EV_EXTENDED_INQUIRY_RESULT: 1132 hci_extended_inquiry_result_evt(hdev, skb); 1133 break; 1134 1135 case HCI_EV_CONN_REQUEST: 1136 hci_conn_request_evt(hdev, skb); 1137 break; 1138 1139 case HCI_EV_CONN_COMPLETE: 1140 hci_conn_complete_evt(hdev, skb); 1141 break; 1142 1143 case HCI_EV_DISCONN_COMPLETE: 1144 hci_disconn_complete_evt(hdev, skb); 1145 break; 1146 1147 case HCI_EV_ROLE_CHANGE: 1148 hci_role_change_evt(hdev, skb); 1149 break; 1150 1151 case HCI_EV_MODE_CHANGE: 1152 hci_mode_change_evt(hdev, skb); 1153 break; 1154 1155 case HCI_EV_AUTH_COMPLETE: 1156 hci_auth_complete_evt(hdev, skb); 1157 break; 1158 1159 case HCI_EV_ENCRYPT_CHANGE: 1160 hci_encrypt_change_evt(hdev, skb); 1161 break; 1162 1163 case HCI_EV_CHANGE_CONN_LINK_KEY_COMPLETE: 1164 hci_change_conn_link_key_complete_evt(hdev, skb); 1165 break; 1166 1167 case HCI_EV_PIN_CODE_REQ: 1168 hci_pin_code_request_evt(hdev, skb); 1169 break; 1170 1171 case HCI_EV_LINK_KEY_REQ: 1172 hci_link_key_request_evt(hdev, skb); 1173 break; 1174 1175 case HCI_EV_LINK_KEY_NOTIFY: 1176 hci_link_key_notify_evt(hdev, skb); 1177 break; 1178 1179 case HCI_EV_REMOTE_FEATURES: 1180 hci_remote_features_evt(hdev, skb); 1181 break; 1182 1183 case HCI_EV_CLOCK_OFFSET: 1184 hci_clock_offset_evt(hdev, skb); 1185 break; 1186 1187 case HCI_EV_PSCAN_REP_MODE: 1188 hci_pscan_rep_mode_evt(hdev, skb); 1189 break; 1190 1191 case HCI_EV_SNIFF_SUBRATE: 1192 hci_sniff_subrate_evt(hdev, skb); 1193 break; 1194 1195 case HCI_EV_CMD_STATUS: 1196 cs = (struct hci_ev_cmd_status *) skb->data; 1197 skb_pull(skb, sizeof(cs)); 1198 1199 opcode = __le16_to_cpu(cs->opcode); 1200 ogf = hci_opcode_ogf(opcode); 1201 ocf = hci_opcode_ocf(opcode); 1202 1203 switch (ogf) { 1204 case OGF_INFO_PARAM: 1205 hci_cs_info_param(hdev, ocf, cs->status); 1206 break; 1207 1208 case OGF_HOST_CTL: 1209 hci_cs_host_ctl(hdev, ocf, cs->status); 1210 break; 1211 1212 case OGF_LINK_CTL: 1213 hci_cs_link_ctl(hdev, ocf, cs->status); 1214 break; 1215 1216 case OGF_LINK_POLICY: 1217 hci_cs_link_policy(hdev, ocf, cs->status); 1218 break; 1219 1220 default: 1221 BT_DBG("%s Command Status OGF %x", hdev->name, ogf); 1222 break; 1223 } 1224 1225 if (cs->ncmd) { 1226 atomic_set(&hdev->cmd_cnt, 1); 1227 if (!skb_queue_empty(&hdev->cmd_q)) 1228 hci_sched_cmd(hdev); 1229 } 1230 break; 1231 1232 case HCI_EV_CMD_COMPLETE: 1233 ec = (struct hci_ev_cmd_complete *) skb->data; 1234 skb_pull(skb, sizeof(*ec)); 1235 1236 opcode = __le16_to_cpu(ec->opcode); 1237 ogf = hci_opcode_ogf(opcode); 1238 ocf = hci_opcode_ocf(opcode); 1239 1240 switch (ogf) { 1241 case OGF_INFO_PARAM: 1242 hci_cc_info_param(hdev, ocf, skb); 1243 break; 1244 1245 case OGF_HOST_CTL: 1246 hci_cc_host_ctl(hdev, ocf, skb); 1247 break; 1248 1249 case OGF_LINK_CTL: 1250 hci_cc_link_ctl(hdev, ocf, skb); 1251 break; 1252 1253 case OGF_LINK_POLICY: 1254 hci_cc_link_policy(hdev, ocf, skb); 1255 break; 1256 1257 default: 1258 BT_DBG("%s Command Completed OGF %x", hdev->name, ogf); 1259 break; 1260 } 1261 1262 if (ec->ncmd) { 1263 atomic_set(&hdev->cmd_cnt, 1); 1264 if (!skb_queue_empty(&hdev->cmd_q)) 1265 hci_sched_cmd(hdev); 1266 } 1267 break; 1268 } 1269 1270 kfree_skb(skb); 1271 hdev->stat.evt_rx++; 1272} 1273 1274/* Generate internal stack event */ 1275void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data) 1276{ 1277 struct hci_event_hdr *hdr; 1278 struct hci_ev_stack_internal *ev; 1279 struct sk_buff *skb; 1280 1281 skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC); 1282 if (!skb) 1283 return; 1284 1285 hdr = (void *) skb_put(skb, HCI_EVENT_HDR_SIZE); 1286 hdr->evt = HCI_EV_STACK_INTERNAL; 1287 hdr->plen = sizeof(*ev) + dlen; 1288 1289 ev = (void *) skb_put(skb, sizeof(*ev) + dlen); 1290 ev->type = type; 1291 memcpy(ev->data, data, dlen); 1292 1293 bt_cb(skb)->incoming = 1; 1294 __net_timestamp(skb); 1295 1296 bt_cb(skb)->pkt_type = HCI_EVENT_PKT; 1297 skb->dev = (void *) hdev; 1298 hci_send_to_sock(hdev, skb); 1299 kfree_skb(skb); 1300} 1301