Lines Matching defs:hdev

281 static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data,
284 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
288 static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data,
291 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
295 static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len,
298 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
318 static int read_version(struct sock *sk, struct hci_dev *hdev, void *data,
323 bt_dev_dbg(hdev, "sock %p", sk);
331 static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data,
339 bt_dev_dbg(hdev, "sock %p", sk);
383 static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
392 bt_dev_dbg(hdev, "sock %p", sk);
426 bt_dev_dbg(hdev, "Added hci%u", d->id);
443 static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev,
452 bt_dev_dbg(hdev, "sock %p", sk);
486 bt_dev_dbg(hdev, "Added hci%u", d->id);
503 static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev,
511 bt_dev_dbg(hdev, "sock %p", sk);
553 bt_dev_dbg(hdev, "Added hci%u", d->id);
577 static bool is_configured(struct hci_dev *hdev)
579 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
580 !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
583 if ((test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) ||
584 test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) &&
585 !bacmp(&hdev->public_addr, BDADDR_ANY))
591 static __le32 get_missing_options(struct hci_dev *hdev)
595 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
596 !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
599 if ((test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) ||
600 test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) &&
601 !bacmp(&hdev->public_addr, BDADDR_ANY))
607 static int new_options(struct hci_dev *hdev, struct sock *skip)
609 __le32 options = get_missing_options(hdev);
611 return mgmt_limited_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options,
615 static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
617 __le32 options = get_missing_options(hdev);
619 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options,
623 static int read_config_info(struct sock *sk, struct hci_dev *hdev,
629 bt_dev_dbg(hdev, "sock %p", sk);
631 hci_dev_lock(hdev);
634 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
636 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
639 if (hdev->set_bdaddr)
643 rp.missing_options = get_missing_options(hdev);
645 hci_dev_unlock(hdev);
647 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0,
651 static u32 get_supported_phys(struct hci_dev *hdev)
655 if (lmp_bredr_capable(hdev)) {
658 if (hdev->features[0][0] & LMP_3SLOT)
661 if (hdev->features[0][0] & LMP_5SLOT)
664 if (lmp_edr_2m_capable(hdev)) {
667 if (lmp_edr_3slot_capable(hdev))
670 if (lmp_edr_5slot_capable(hdev))
673 if (lmp_edr_3m_capable(hdev)) {
676 if (lmp_edr_3slot_capable(hdev))
679 if (lmp_edr_5slot_capable(hdev))
685 if (lmp_le_capable(hdev)) {
689 if (hdev->le_features[1] & HCI_LE_PHY_2M) {
694 if (hdev->le_features[1] & HCI_LE_PHY_CODED) {
703 static u32 get_selected_phys(struct hci_dev *hdev)
707 if (lmp_bredr_capable(hdev)) {
710 if (hdev->pkt_type & (HCI_DM3 | HCI_DH3))
713 if (hdev->pkt_type & (HCI_DM5 | HCI_DH5))
716 if (lmp_edr_2m_capable(hdev)) {
717 if (!(hdev->pkt_type & HCI_2DH1))
720 if (lmp_edr_3slot_capable(hdev) &&
721 !(hdev->pkt_type & HCI_2DH3))
724 if (lmp_edr_5slot_capable(hdev) &&
725 !(hdev->pkt_type & HCI_2DH5))
728 if (lmp_edr_3m_capable(hdev)) {
729 if (!(hdev->pkt_type & HCI_3DH1))
732 if (lmp_edr_3slot_capable(hdev) &&
733 !(hdev->pkt_type & HCI_3DH3))
736 if (lmp_edr_5slot_capable(hdev) &&
737 !(hdev->pkt_type & HCI_3DH5))
743 if (lmp_le_capable(hdev)) {
744 if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_1M)
747 if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_1M)
750 if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_2M)
753 if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_2M)
756 if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_CODED)
759 if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_CODED)
766 static u32 get_configurable_phys(struct hci_dev *hdev)
768 return (get_supported_phys(hdev) & ~MGMT_PHY_BR_1M_1SLOT &
772 static u32 get_supported_settings(struct hci_dev *hdev)
782 if (lmp_bredr_capable(hdev)) {
783 if (hdev->hci_ver >= BLUETOOTH_VER_1_2)
788 if (lmp_ssp_capable(hdev)) {
794 if (lmp_sc_capable(hdev))
798 &hdev->quirks))
802 if (lmp_le_capable(hdev)) {
811 if (!hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY))
815 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
816 hdev->set_bdaddr)
824 static u32 get_current_settings(struct hci_dev *hdev)
828 if (hdev_is_powered(hdev))
831 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE))
834 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
837 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
840 if (hci_dev_test_flag(hdev, HCI_BONDABLE))
843 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
846 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
849 if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY))
852 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
855 if (hci_dev_test_flag(hdev, HCI_HS_ENABLED))
858 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
861 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED))
864 if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS))
867 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
882 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
883 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
884 !bacmp(&hdev->bdaddr, BDADDR_ANY)) {
885 if (bacmp(&hdev->static_addr, BDADDR_ANY))
889 if (hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED))
895 static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev)
897 return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev);
901 struct hci_dev *hdev,
904 return mgmt_pending_find_data(HCI_CHANNEL_CONTROL, opcode, hdev, data);
907 u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev)
914 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
922 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
924 else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
931 bool mgmt_get_connectable(struct hci_dev *hdev)
938 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
945 return hci_dev_test_flag(hdev, HCI_CONNECTABLE);
950 struct hci_dev *hdev = container_of(work, struct hci_dev,
954 if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
957 hci_req_init(&req, hdev);
959 hci_dev_lock(hdev);
964 hci_dev_unlock(hdev);
971 struct hci_dev *hdev = container_of(work, struct hci_dev,
975 bt_dev_dbg(hdev, "");
977 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
979 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING))
986 hci_req_init(&req, hdev);
987 if (ext_adv_capable(hdev))
988 __hci_req_start_ext_adv(&req, hdev->cur_adv_instance);
994 static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev)
996 if (hci_dev_test_and_set_flag(hdev, HCI_MGMT))
999 INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off);
1000 INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired);
1007 hci_dev_clear_flag(hdev, HCI_BONDABLE);
1010 static int read_controller_info(struct sock *sk, struct hci_dev *hdev,
1015 bt_dev_dbg(hdev, "sock %p", sk);
1017 hci_dev_lock(hdev);
1021 bacpy(&rp.bdaddr, &hdev->bdaddr);
1023 rp.version = hdev->hci_ver;
1024 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
1026 rp.supported_settings = cpu_to_le32(get_supported_settings(hdev));
1027 rp.current_settings = cpu_to_le32(get_current_settings(hdev));
1029 memcpy(rp.dev_class, hdev->dev_class, 3);
1031 memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name));
1032 memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name));
1034 hci_dev_unlock(hdev);
1036 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp,
1040 static u16 append_eir_data_to_buf(struct hci_dev *hdev, u8 *eir)
1045 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1047 hdev->dev_class, 3);
1049 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1051 hdev->appearance);
1053 name_len = strlen(hdev->dev_name);
1055 hdev->dev_name, name_len);
1057 name_len = strlen(hdev->short_name);
1059 hdev->short_name, name_len);
1064 static int read_ext_controller_info(struct sock *sk, struct hci_dev *hdev,
1071 bt_dev_dbg(hdev, "sock %p", sk);
1075 hci_dev_lock(hdev);
1077 bacpy(&rp->bdaddr, &hdev->bdaddr);
1079 rp->version = hdev->hci_ver;
1080 rp->manufacturer = cpu_to_le16(hdev->manufacturer);
1082 rp->supported_settings = cpu_to_le32(get_supported_settings(hdev));
1083 rp->current_settings = cpu_to_le32(get_current_settings(hdev));
1086 eir_len = append_eir_data_to_buf(hdev, rp->eir);
1089 hci_dev_unlock(hdev);
1100 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_EXT_INFO, 0, rp,
1104 static int ext_info_changed(struct hci_dev *hdev, struct sock *skip)
1112 eir_len = append_eir_data_to_buf(hdev, ev->eir);
1115 return mgmt_limited_event(MGMT_EV_EXT_INFO_CHANGED, hdev, ev,
1120 static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
1122 __le32 settings = cpu_to_le32(get_current_settings(hdev));
1124 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings,
1128 static void clean_up_hci_complete(struct hci_dev *hdev, u8 status, u16 opcode)
1130 bt_dev_dbg(hdev, "status 0x%02x", status);
1132 if (hci_conn_count(hdev) == 0) {
1133 cancel_delayed_work(&hdev->power_off);
1134 queue_work(hdev->req_workqueue, &hdev->power_off.work);
1138 void mgmt_advertising_added(struct sock *sk, struct hci_dev *hdev, u8 instance)
1144 mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk);
1147 void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev,
1154 mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk);
1157 static void cancel_adv_timeout(struct hci_dev *hdev)
1159 if (hdev->adv_instance_timeout) {
1160 hdev->adv_instance_timeout = 0;
1161 cancel_delayed_work(&hdev->adv_instance_expire);
1165 static int clean_up_hci_state(struct hci_dev *hdev)
1172 hci_req_init(&req, hdev);
1174 if (test_bit(HCI_ISCAN, &hdev->flags) ||
1175 test_bit(HCI_PSCAN, &hdev->flags)) {
1180 hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, false);
1182 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
1187 list_for_each_entry(conn, &hdev->conn_hash.list, list) {
1194 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
1199 static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data,
1206 bt_dev_dbg(hdev, "sock %p", sk);
1209 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1212 hci_dev_lock(hdev);
1214 if (pending_find(MGMT_OP_SET_POWERED, hdev)) {
1215 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1220 if (!!cp->val == hdev_is_powered(hdev)) {
1221 err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev);
1225 cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len);
1232 queue_work(hdev->req_workqueue, &hdev->power_on);
1236 err = clean_up_hci_state(hdev);
1238 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
1243 cancel_delayed_work(&hdev->power_off);
1244 queue_work(hdev->req_workqueue, &hdev->power_off.work);
1250 hci_dev_unlock(hdev);
1254 static int new_settings(struct hci_dev *hdev, struct sock *skip)
1256 __le32 ev = cpu_to_le32(get_current_settings(hdev));
1258 return mgmt_limited_event(MGMT_EV_NEW_SETTINGS, hdev, &ev,
1262 int mgmt_new_settings(struct hci_dev *hdev)
1264 return new_settings(hdev, NULL);
1269 struct hci_dev *hdev;
1277 send_settings_rsp(cmd->sk, cmd->opcode, match->hdev);
1323 static u8 mgmt_bredr_support(struct hci_dev *hdev)
1325 if (!lmp_bredr_capable(hdev))
1327 else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1333 static u8 mgmt_le_support(struct hci_dev *hdev)
1335 if (!lmp_le_capable(hdev))
1337 else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1343 void mgmt_set_discoverable_complete(struct hci_dev *hdev, u8 status)
1347 bt_dev_dbg(hdev, "status 0x%02x", status);
1349 hci_dev_lock(hdev);
1351 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
1358 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1362 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1363 hdev->discov_timeout > 0) {
1364 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
1365 queue_delayed_work(hdev->req_workqueue, &hdev->discov_off, to);
1368 send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev);
1369 new_settings(hdev, cmd->sk);
1375 hci_dev_unlock(hdev);
1378 static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
1386 bt_dev_dbg(hdev, "sock %p", sk);
1388 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
1389 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1390 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1394 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1404 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1407 hci_dev_lock(hdev);
1409 if (!hdev_is_powered(hdev) && timeout > 0) {
1410 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1415 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
1416 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
1417 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1422 if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) {
1423 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1428 if (hdev->advertising_paused) {
1429 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1434 if (!hdev_is_powered(hdev)) {
1441 if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) {
1442 hci_dev_change_flag(hdev, HCI_DISCOVERABLE);
1446 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
1451 err = new_settings(hdev, sk);
1460 if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1461 (cp->val == 0x02) == hci_dev_test_flag(hdev,
1463 cancel_delayed_work(&hdev->discov_off);
1464 hdev->discov_timeout = timeout;
1466 if (cp->val && hdev->discov_timeout > 0) {
1467 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
1468 queue_delayed_work(hdev->req_workqueue,
1469 &hdev->discov_off, to);
1472 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
1476 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len);
1486 cancel_delayed_work(&hdev->discov_off);
1487 hdev->discov_timeout = timeout;
1490 hci_dev_set_flag(hdev, HCI_DISCOVERABLE);
1492 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1496 hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1498 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1500 queue_work(hdev->req_workqueue, &hdev->discoverable_update);
1504 hci_dev_unlock(hdev);
1508 void mgmt_set_connectable_complete(struct hci_dev *hdev, u8 status)
1512 bt_dev_dbg(hdev, "status 0x%02x", status);
1514 hci_dev_lock(hdev);
1516 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
1526 send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev);
1527 new_settings(hdev, cmd->sk);
1533 hci_dev_unlock(hdev);
1536 static int set_connectable_update_settings(struct hci_dev *hdev,
1542 if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE))
1546 hci_dev_set_flag(hdev, HCI_CONNECTABLE);
1548 hci_dev_clear_flag(hdev, HCI_CONNECTABLE);
1549 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1552 err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
1557 hci_req_update_scan(hdev);
1558 hci_update_background_scan(hdev);
1559 return new_settings(hdev, sk);
1565 static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
1572 bt_dev_dbg(hdev, "sock %p", sk);
1574 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
1575 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1576 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1580 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1583 hci_dev_lock(hdev);
1585 if (!hdev_is_powered(hdev)) {
1586 err = set_connectable_update_settings(hdev, sk, cp->val);
1590 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
1591 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
1592 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1597 cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len);
1604 hci_dev_set_flag(hdev, HCI_CONNECTABLE);
1606 if (hdev->discov_timeout > 0)
1607 cancel_delayed_work(&hdev->discov_off);
1609 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1610 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1611 hci_dev_clear_flag(hdev, HCI_CONNECTABLE);
1614 queue_work(hdev->req_workqueue, &hdev->connectable_update);
1618 hci_dev_unlock(hdev);
1622 static int set_bondable(struct sock *sk, struct hci_dev *hdev, void *data,
1629 bt_dev_dbg(hdev, "sock %p", sk);
1632 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE,
1635 hci_dev_lock(hdev);
1638 changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE);
1640 changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE);
1642 err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev);
1650 if (hdev_is_powered(hdev) &&
1651 hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
1652 hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1653 hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
1654 queue_work(hdev->req_workqueue,
1655 &hdev->discoverable_update);
1657 err = new_settings(hdev, sk);
1661 hci_dev_unlock(hdev);
1665 static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data,
1673 bt_dev_dbg(hdev, "sock %p", sk);
1675 status = mgmt_bredr_support(hdev);
1677 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1681 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1684 hci_dev_lock(hdev);
1686 if (!hdev_is_powered(hdev)) {
1689 if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) {
1690 hci_dev_change_flag(hdev, HCI_LINK_SECURITY);
1694 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
1699 err = new_settings(hdev, sk);
1704 if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) {
1705 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1712 if (test_bit(HCI_AUTH, &hdev->flags) == val) {
1713 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
1717 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len);
1723 err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val);
1730 hci_dev_unlock(hdev);
1734 static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
1741 bt_dev_dbg(hdev, "sock %p", sk);
1743 status = mgmt_bredr_support(hdev);
1745 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status);
1747 if (!lmp_ssp_capable(hdev))
1748 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
1752 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
1755 hci_dev_lock(hdev);
1757 if (!hdev_is_powered(hdev)) {
1761 changed = !hci_dev_test_and_set_flag(hdev,
1764 changed = hci_dev_test_and_clear_flag(hdev,
1767 changed = hci_dev_test_and_clear_flag(hdev,
1770 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
1773 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
1778 err = new_settings(hdev, sk);
1783 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
1784 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
1789 if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
1790 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
1794 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len);
1800 if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
1801 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
1804 err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &cp->val);
1811 hci_dev_unlock(hdev);
1815 static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
1822 bt_dev_dbg(hdev, "sock %p", sk);
1825 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1828 status = mgmt_bredr_support(hdev);
1830 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status);
1832 if (!lmp_ssp_capable(hdev))
1833 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1836 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
1837 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1841 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1844 hci_dev_lock(hdev);
1846 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
1847 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1853 changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED);
1855 if (hdev_is_powered(hdev)) {
1856 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1861 changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED);
1864 err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev);
1869 err = new_settings(hdev, sk);
1872 hci_dev_unlock(hdev);
1876 static void le_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
1878 struct cmd_lookup match = { NULL, hdev };
1880 hci_dev_lock(hdev);
1885 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp,
1890 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match);
1892 new_settings(hdev, match.sk);
1902 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
1904 hci_req_init(&req, hdev);
1905 if (ext_adv_capable(hdev)) {
1916 hci_update_background_scan(hdev);
1920 hci_dev_unlock(hdev);
1923 static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
1932 bt_dev_dbg(hdev, "sock %p", sk);
1934 if (!lmp_le_capable(hdev))
1935 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1939 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1951 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
1953 return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
1955 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1959 hci_dev_lock(hdev);
1962 enabled = lmp_host_le_capable(hdev);
1965 hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, true);
1967 if (!hdev_is_powered(hdev) || val == enabled) {
1970 if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
1971 hci_dev_change_flag(hdev, HCI_LE_ENABLED);
1975 if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
1976 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
1980 err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
1985 err = new_settings(hdev, sk);
1990 if (pending_find(MGMT_OP_SET_LE, hdev) ||
1991 pending_find(MGMT_OP_SET_ADVERTISING, hdev)) {
1992 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1997 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len);
2003 hci_req_init(&req, hdev);
2011 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
2014 if (ext_adv_capable(hdev))
2026 hci_dev_unlock(hdev);
2036 static bool pending_eir_or_class(struct hci_dev *hdev)
2040 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
2072 static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status)
2076 hci_dev_lock(hdev);
2078 cmd = pending_find(mgmt_op, hdev);
2083 mgmt_status(status), hdev->dev_class, 3);
2088 hci_dev_unlock(hdev);
2091 static void add_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
2093 bt_dev_dbg(hdev, "status 0x%02x", status);
2095 mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status);
2098 static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2106 bt_dev_dbg(hdev, "sock %p", sk);
2108 hci_dev_lock(hdev);
2110 if (pending_eir_or_class(hdev)) {
2111 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID,
2126 list_add_tail(&uuid->list, &hdev->uuids);
2128 hci_req_init(&req, hdev);
2138 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0,
2139 hdev->dev_class, 3);
2143 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len);
2152 hci_dev_unlock(hdev);
2156 static bool enable_service_cache(struct hci_dev *hdev)
2158 if (!hdev_is_powered(hdev))
2161 if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) {
2162 queue_delayed_work(hdev->workqueue, &hdev->service_cache,
2170 static void remove_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
2172 bt_dev_dbg(hdev, "status 0x%02x", status);
2174 mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status);
2177 static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
2187 bt_dev_dbg(hdev, "sock %p", sk);
2189 hci_dev_lock(hdev);
2191 if (pending_eir_or_class(hdev)) {
2192 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2198 hci_uuids_clear(hdev);
2200 if (enable_service_cache(hdev)) {
2201 err = mgmt_cmd_complete(sk, hdev->id,
2203 0, hdev->dev_class, 3);
2212 list_for_each_entry_safe(match, tmp, &hdev->uuids, list) {
2222 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2228 hci_req_init(&req, hdev);
2238 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0,
2239 hdev->dev_class, 3);
2243 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len);
2252 hci_dev_unlock(hdev);
2256 static void set_class_complete(struct hci_dev *hdev, u8 status, u16 opcode)
2258 bt_dev_dbg(hdev, "status 0x%02x", status);
2260 mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status);
2263 static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,
2271 bt_dev_dbg(hdev, "sock %p", sk);
2273 if (!lmp_bredr_capable(hdev))
2274 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2277 hci_dev_lock(hdev);
2279 if (pending_eir_or_class(hdev)) {
2280 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2286 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2291 hdev->major_class = cp->major;
2292 hdev->minor_class = cp->minor;
2294 if (!hdev_is_powered(hdev)) {
2295 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2296 hdev->dev_class, 3);
2300 hci_req_init(&req, hdev);
2302 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) {
2303 hci_dev_unlock(hdev);
2304 cancel_delayed_work_sync(&hdev->service_cache);
2305 hci_dev_lock(hdev);
2316 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2317 hdev->dev_class, 3);
2321 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len);
2330 hci_dev_unlock(hdev);
2334 static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
2344 bt_dev_dbg(hdev, "sock %p", sk);
2346 if (!lmp_bredr_capable(hdev))
2347 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2352 bt_dev_err(hdev, "load_link_keys: too big key_count value %u",
2354 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2360 bt_dev_err(hdev, "load_link_keys: expected %u bytes, got %u bytes",
2362 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2367 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2370 bt_dev_dbg(hdev, "debug_keys %u key_count %u", cp->debug_keys,
2378 return mgmt_cmd_status(sk, hdev->id,
2383 hci_dev_lock(hdev);
2385 hci_link_keys_clear(hdev);
2388 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
2390 changed = hci_dev_test_and_clear_flag(hdev,
2394 new_settings(hdev, NULL);
2399 if (hci_is_blocked_key(hdev,
2402 bt_dev_warn(hdev, "Skipping blocked link key for %pMR",
2413 hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val,
2417 mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0);
2419 hci_dev_unlock(hdev);
2424 static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr,
2432 return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev),
2436 static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,
2452 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2457 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2461 hci_dev_lock(hdev);
2463 if (!hdev_is_powered(hdev)) {
2464 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2479 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
2484 err = hci_remove_link_key(hdev, &cp->addr.bdaddr);
2486 err = mgmt_cmd_complete(sk, hdev->id,
2500 err = smp_cancel_and_remove_pairing(hdev, &cp->addr.bdaddr, addr_type);
2502 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2508 conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, addr_type);
2510 hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type);
2521 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, addr_type);
2540 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0,
2542 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk);
2546 cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp,
2560 hci_dev_unlock(hdev);
2564 static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,
2573 bt_dev_dbg(hdev, "sock %p", sk);
2580 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2584 hci_dev_lock(hdev);
2586 if (!test_bit(HCI_UP, &hdev->flags)) {
2587 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2593 if (pending_find(MGMT_OP_DISCONNECT, hdev)) {
2594 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2600 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
2603 conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr,
2607 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2613 cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len);
2626 hci_dev_unlock(hdev);
2649 static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data,
2657 bt_dev_dbg(hdev, "sock %p", sk);
2659 hci_dev_lock(hdev);
2661 if (!hdev_is_powered(hdev)) {
2662 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS,
2668 list_for_each_entry(c, &hdev->conn_hash.list, list) {
2680 list_for_each_entry(c, &hdev->conn_hash.list, list) {
2693 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp,
2699 hci_dev_unlock(hdev);
2703 static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
2709 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp,
2716 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
2724 static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data,
2733 bt_dev_dbg(hdev, "sock %p", sk);
2735 hci_dev_lock(hdev);
2737 if (!hdev_is_powered(hdev)) {
2738 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
2743 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr);
2745 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
2755 bt_dev_err(hdev, "PIN code is not 16 bytes long");
2757 err = send_pin_code_neg_reply(sk, hdev, &ncp);
2759 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
2765 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len);
2777 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply);
2782 hci_dev_unlock(hdev);
2786 static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data,
2791 bt_dev_dbg(hdev, "sock %p", sk);
2794 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY,
2797 hci_dev_lock(hdev);
2799 hdev->io_capability = cp->io_capability;
2801 bt_dev_dbg(hdev, "IO capability set to 0x%02x", hdev->io_capability);
2803 hci_dev_unlock(hdev);
2805 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0,
2811 struct hci_dev *hdev = conn->hdev;
2814 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
2903 static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
2913 bt_dev_dbg(hdev, "sock %p", sk);
2920 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2925 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2929 hci_dev_lock(hdev);
2931 if (!hdev_is_powered(hdev)) {
2932 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2938 if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) {
2939 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2949 conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level,
2964 p = hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type);
2973 conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr, addr_type,
2990 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
2997 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3002 cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len);
3034 hci_dev_unlock(hdev);
3038 static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
3046 bt_dev_dbg(hdev, "sock %p", sk);
3048 hci_dev_lock(hdev);
3050 if (!hdev_is_powered(hdev)) {
3051 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3056 cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev);
3058 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3066 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3074 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0,
3082 hci_remove_link_key(hdev, &addr->bdaddr);
3084 smp_cancel_and_remove_pairing(hdev, &addr->bdaddr,
3091 hci_dev_unlock(hdev);
3095 static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev,
3103 hci_dev_lock(hdev);
3105 if (!hdev_is_powered(hdev)) {
3106 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3113 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr);
3115 conn = hci_conn_hash_lookup_le(hdev, &addr->bdaddr,
3119 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3128 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3132 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3139 cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr));
3153 err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp);
3155 err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr),
3162 hci_dev_unlock(hdev);
3166 static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
3171 bt_dev_dbg(hdev, "sock %p", sk);
3173 return user_pairing_resp(sk, hdev, &cp->addr,
3178 static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3183 bt_dev_dbg(hdev, "sock %p", sk);
3186 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY,
3189 return user_pairing_resp(sk, hdev, &cp->addr,
3194 static int user_confirm_neg_reply(struct sock *sk, struct hci_dev *hdev,
3199 bt_dev_dbg(hdev, "sock %p", sk);
3201 return user_pairing_resp(sk, hdev, &cp->addr,
3206 static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3211 bt_dev_dbg(hdev, "sock %p", sk);
3213 return user_pairing_resp(sk, hdev, &cp->addr,
3218 static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev,
3223 bt_dev_dbg(hdev, "sock %p", sk);
3225 return user_pairing_resp(sk, hdev, &cp->addr,
3230 static void adv_expire(struct hci_dev *hdev, u32 flags)
3236 adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
3244 cancel_adv_timeout(hdev);
3246 adv_instance = hci_get_next_instance(hdev, adv_instance->instance);
3250 hci_req_init(&req, hdev);
3259 static void set_name_complete(struct hci_dev *hdev, u8 status, u16 opcode)
3264 bt_dev_dbg(hdev, "status 0x%02x", status);
3266 hci_dev_lock(hdev);
3268 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
3275 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
3278 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3281 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
3282 adv_expire(hdev, MGMT_ADV_FLAG_LOCAL_NAME);
3288 hci_dev_unlock(hdev);
3291 static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
3299 bt_dev_dbg(hdev, "sock %p", sk);
3301 hci_dev_lock(hdev);
3306 if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) &&
3307 !memcmp(hdev->short_name, cp->short_name,
3308 sizeof(hdev->short_name))) {
3309 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3314 memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name));
3316 if (!hdev_is_powered(hdev)) {
3317 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
3319 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3324 err = mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data,
3326 ext_info_changed(hdev, sk);
3331 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len);
3337 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
3339 hci_req_init(&req, hdev);
3341 if (lmp_bredr_capable(hdev)) {
3349 if (lmp_le_capable(hdev) && hci_dev_test_flag(hdev, HCI_ADVERTISING))
3350 __hci_req_update_scan_rsp_data(&req, hdev->cur_adv_instance);
3357 hci_dev_unlock(hdev);
3361 static int set_appearance(struct sock *sk, struct hci_dev *hdev, void *data,
3368 bt_dev_dbg(hdev, "sock %p", sk);
3370 if (!lmp_le_capable(hdev))
3371 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_APPEARANCE,
3376 hci_dev_lock(hdev);
3378 if (hdev->appearance != appearance) {
3379 hdev->appearance = appearance;
3381 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
3382 adv_expire(hdev, MGMT_ADV_FLAG_APPEARANCE);
3384 ext_info_changed(hdev, sk);
3387 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_APPEARANCE, 0, NULL,
3390 hci_dev_unlock(hdev);
3395 static int get_phy_configuration(struct sock *sk, struct hci_dev *hdev,
3400 bt_dev_dbg(hdev, "sock %p", sk);
3402 hci_dev_lock(hdev);
3406 rp.supported_phys = cpu_to_le32(get_supported_phys(hdev));
3407 rp.selected_phys = cpu_to_le32(get_selected_phys(hdev));
3408 rp.configurable_phys = cpu_to_le32(get_configurable_phys(hdev));
3410 hci_dev_unlock(hdev);
3412 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_PHY_CONFIGURATION, 0,
3416 int mgmt_phy_configuration_changed(struct hci_dev *hdev, struct sock *skip)
3422 ev.selected_phys = cpu_to_le32(get_selected_phys(hdev));
3424 return mgmt_event(MGMT_EV_PHY_CONFIGURATION_CHANGED, hdev, &ev,
3428 static void set_default_phy_complete(struct hci_dev *hdev, u8 status,
3433 bt_dev_dbg(hdev, "status 0x%02x", status);
3435 hci_dev_lock(hdev);
3437 cmd = pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev);
3442 mgmt_cmd_status(cmd->sk, hdev->id,
3446 mgmt_cmd_complete(cmd->sk, hdev->id,
3450 mgmt_phy_configuration_changed(hdev, cmd->sk);
3456 hci_dev_unlock(hdev);
3459 static int set_phy_configuration(struct sock *sk, struct hci_dev *hdev,
3471 bt_dev_dbg(hdev, "sock %p", sk);
3473 configurable_phys = get_configurable_phys(hdev);
3474 supported_phys = get_supported_phys(hdev);
3478 return mgmt_cmd_status(sk, hdev->id,
3485 return mgmt_cmd_status(sk, hdev->id,
3489 if (selected_phys == get_selected_phys(hdev))
3490 return mgmt_cmd_complete(sk, hdev->id,
3494 hci_dev_lock(hdev);
3496 if (!hdev_is_powered(hdev)) {
3497 err = mgmt_cmd_status(sk, hdev->id,
3503 if (pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev)) {
3504 err = mgmt_cmd_status(sk, hdev->id,
3550 if (pkt_type != hdev->pkt_type) {
3551 hdev->pkt_type = pkt_type;
3556 (get_selected_phys(hdev) & MGMT_PHY_LE_MASK)) {
3558 mgmt_phy_configuration_changed(hdev, sk);
3560 err = mgmt_cmd_complete(sk, hdev->id,
3567 cmd = mgmt_pending_add(sk, MGMT_OP_SET_PHY_CONFIGURATION, hdev, data,
3574 hci_req_init(&req, hdev);
3609 hci_dev_unlock(hdev);
3614 static int set_blocked_keys(struct sock *sk, struct hci_dev *hdev, void *data,
3624 bt_dev_dbg(hdev, "sock %p", sk);
3628 bt_dev_err(hdev, "too big key_count value %u", key_count);
3629 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS,
3635 bt_dev_err(hdev, "expected %u bytes, got %u bytes",
3637 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS,
3641 hci_dev_lock(hdev);
3643 hci_blocked_keys_clear(hdev);
3655 list_add_rcu(&b->list, &hdev->blocked_keys);
3657 hci_dev_unlock(hdev);
3659 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS,
3663 static int set_wideband_speech(struct sock *sk, struct hci_dev *hdev,
3670 bt_dev_dbg(hdev, "sock %p", sk);
3672 if (!test_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks))
3673 return mgmt_cmd_status(sk, hdev->id,
3678 return mgmt_cmd_status(sk, hdev->id,
3682 hci_dev_lock(hdev);
3684 if (pending_find(MGMT_OP_SET_WIDEBAND_SPEECH, hdev)) {
3685 err = mgmt_cmd_status(sk, hdev->id,
3691 if (hdev_is_powered(hdev) &&
3692 !!cp->val != hci_dev_test_flag(hdev,
3694 err = mgmt_cmd_status(sk, hdev->id,
3701 changed = !hci_dev_test_and_set_flag(hdev,
3704 changed = hci_dev_test_and_clear_flag(hdev,
3707 err = send_settings_rsp(sk, MGMT_OP_SET_WIDEBAND_SPEECH, hdev);
3712 err = new_settings(hdev, sk);
3715 hci_dev_unlock(hdev);
3719 static int read_security_info(struct sock *sk, struct hci_dev *hdev,
3727 bt_dev_dbg(hdev, "sock %p", sk);
3731 hci_dev_lock(hdev);
3736 if (hdev->commands[41] & 0x08)
3744 if (hdev->commands[20] & 0x10)
3754 if (hdev->commands[41] & 0x08)
3756 hdev->max_enc_key_size);
3762 hci_dev_unlock(hdev);
3764 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_SECURITY_INFO, 0,
3788 static int read_exp_features_info(struct sock *sk, struct hci_dev *hdev,
3796 bt_dev_dbg(hdev, "sock %p", sk);
3801 if (!hdev) {
3810 if (hdev) {
3811 if (test_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks) &&
3812 (hdev->le_states[4] & 0x08) && /* Central */
3813 (hdev->le_states[4] & 0x40) && /* Peripheral */
3814 (hdev->le_states[3] & 0x10)) /* Simultaneous */
3824 if (hdev && use_ll_privacy(hdev)) {
3825 if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY))
3842 return mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE,
3847 static int exp_ll_privacy_feature_changed(bool enabled, struct hci_dev *hdev,
3856 return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, hdev,
3877 static int set_exp_feature(struct sock *sk, struct hci_dev *hdev,
3883 bt_dev_dbg(hdev, "sock %p", sk);
3890 if (!hdev) {
3900 if (hdev && use_ll_privacy(hdev) && !hdev_is_powered(hdev)) {
3901 bool changed = hci_dev_test_flag(hdev,
3904 hci_dev_clear_flag(hdev, HCI_ENABLE_LL_PRIVACY);
3907 exp_ll_privacy_feature_changed(false, hdev, sk);
3912 return mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE,
3923 if (hdev)
3924 return mgmt_cmd_status(sk, hdev->id,
3966 if (!hdev)
3972 if (hdev_is_powered(hdev))
3973 return mgmt_cmd_status(sk, hdev->id,
3979 return mgmt_cmd_status(sk, hdev->id,
3985 return mgmt_cmd_status(sk, hdev->id,
3992 changed = !hci_dev_test_flag(hdev,
3994 hci_dev_set_flag(hdev, HCI_ENABLE_LL_PRIVACY);
3995 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
4000 changed = hci_dev_test_flag(hdev,
4002 hci_dev_clear_flag(hdev, HCI_ENABLE_LL_PRIVACY);
4013 err = mgmt_cmd_complete(sk, hdev->id,
4018 exp_ll_privacy_feature_changed(val, hdev, sk);
4023 return mgmt_cmd_status(sk, hdev ? hdev->id : MGMT_INDEX_NONE,
4030 static int get_device_flags(struct sock *sk, struct hci_dev *hdev, void *data,
4041 bt_dev_dbg(hdev, "Get device flags %pMR (type 0x%x)\n",
4044 hci_dev_lock(hdev);
4049 br_params = hci_bdaddr_list_lookup_with_flags(&hdev->accept_list,
4057 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
4074 hci_dev_unlock(hdev);
4076 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_DEVICE_FLAGS, status,
4080 static void device_flags_changed(struct sock *sk, struct hci_dev *hdev,
4091 mgmt_event(MGMT_EV_DEVICE_FLAGS_CHANGED, hdev, &ev, sizeof(ev), sk);
4094 static int set_device_flags(struct sock *sk, struct hci_dev *hdev, void *data,
4104 bt_dev_dbg(hdev, "Set device flags %pMR (type 0x%x) = 0x%x",
4109 bt_dev_warn(hdev, "Bad flag given (0x%x) vs supported (0x%0x)",
4114 hci_dev_lock(hdev);
4117 br_params = hci_bdaddr_list_lookup_with_flags(&hdev->accept_list,
4125 bt_dev_warn(hdev, "No such BR/EDR device %pMR (0x%x)",
4129 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
4135 bt_dev_warn(hdev, "No such LE device %pMR (0x%x)",
4142 hci_dev_unlock(hdev);
4145 device_flags_changed(sk, hdev, &cp->addr.bdaddr, cp->addr.type,
4148 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_FLAGS, status,
4152 static void mgmt_adv_monitor_added(struct sock *sk, struct hci_dev *hdev,
4159 mgmt_event(MGMT_EV_ADV_MONITOR_ADDED, hdev, &ev, sizeof(ev), sk);
4162 static void mgmt_adv_monitor_removed(struct sock *sk, struct hci_dev *hdev,
4169 mgmt_event(MGMT_EV_ADV_MONITOR_REMOVED, hdev, &ev, sizeof(ev), sk);
4172 static int read_adv_mon_features(struct sock *sk, struct hci_dev *hdev,
4183 BT_DBG("request for %s", hdev->name);
4185 hci_dev_lock(hdev);
4187 if (msft_get_features(hdev) & MSFT_FEATURE_MASK_LE_ADV_MONITOR)
4190 idr_for_each_entry(&hdev->adv_monitors_idr, monitor, handle) {
4194 hci_dev_unlock(hdev);
4212 err = mgmt_cmd_complete(sk, hdev->id,
4221 static int add_adv_patterns_monitor(struct sock *sk, struct hci_dev *hdev,
4232 BT_DBG("request for %s", hdev->name);
4235 err = mgmt_cmd_status(sk, hdev->id,
4252 err = mgmt_cmd_status(sk, hdev->id,
4263 err = mgmt_cmd_status(sk, hdev->id,
4285 err = mgmt_cmd_status(sk, hdev->id,
4291 hci_dev_lock(hdev);
4293 prev_adv_monitors_cnt = hdev->adv_monitors_cnt;
4295 err = hci_add_adv_monitor(hdev, m);
4298 mgmt_cmd_status(sk, hdev->id,
4305 if (hdev->adv_monitors_cnt > prev_adv_monitors_cnt)
4306 mgmt_adv_monitor_added(sk, hdev, m->handle);
4308 hci_dev_unlock(hdev);
4312 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADV_PATTERNS_MONITOR,
4316 hci_dev_unlock(hdev);
4323 static int remove_adv_monitor(struct sock *sk, struct hci_dev *hdev,
4332 BT_DBG("request for %s", hdev->name);
4334 hci_dev_lock(hdev);
4337 prev_adv_monitors_cnt = hdev->adv_monitors_cnt;
4339 err = hci_remove_adv_monitor(hdev, handle);
4341 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADV_MONITOR,
4346 if (hdev->adv_monitors_cnt < prev_adv_monitors_cnt)
4347 mgmt_adv_monitor_removed(sk, hdev, handle);
4349 hci_dev_unlock(hdev);
4353 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_ADV_MONITOR,
4357 hci_dev_unlock(hdev);
4361 static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status,
4368 bt_dev_dbg(hdev, "status %u", status);
4370 cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev);
4375 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
4386 mgmt_cmd_status(cmd->sk, hdev->id,
4400 mgmt_cmd_status(cmd->sk, hdev->id,
4413 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
4420 static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev,
4427 bt_dev_dbg(hdev, "sock %p", sk);
4429 hci_dev_lock(hdev);
4431 if (!hdev_is_powered(hdev)) {
4432 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
4437 if (!lmp_ssp_capable(hdev)) {
4438 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
4443 if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) {
4444 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
4449 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0);
4455 hci_req_init(&req, hdev);
4457 if (bredr_sc_enabled(hdev))
4467 hci_dev_unlock(hdev);
4471 static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
4477 bt_dev_dbg(hdev, "sock %p", sk);
4480 return mgmt_cmd_complete(sk, hdev->id,
4485 hci_dev_lock(hdev);
4492 err = mgmt_cmd_complete(sk, hdev->id,
4499 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
4507 err = mgmt_cmd_complete(sk, hdev->id,
4521 err = mgmt_cmd_complete(sk, hdev->id,
4556 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
4564 err = mgmt_cmd_complete(sk, hdev->id,
4568 bt_dev_err(hdev, "add_remote_oob_data: invalid len of %u bytes",
4570 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
4575 hci_dev_unlock(hdev);
4579 static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
4586 bt_dev_dbg(hdev, "sock %p", sk);
4589 return mgmt_cmd_complete(sk, hdev->id,
4594 hci_dev_lock(hdev);
4597 hci_remote_oob_data_clear(hdev);
4602 err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr, cp->addr.type);
4609 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA,
4612 hci_dev_unlock(hdev);
4616 void mgmt_start_discovery_complete(struct hci_dev *hdev, u8 status)
4620 bt_dev_dbg(hdev, "status %d", status);
4622 hci_dev_lock(hdev);
4624 cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev);
4626 cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev);
4629 cmd = pending_find(MGMT_OP_START_LIMITED_DISCOVERY, hdev);
4636 hci_dev_unlock(hdev);
4640 hdev->suspend_tasks)) {
4641 bt_dev_dbg(hdev, "Unpaused discovery");
4642 wake_up(&hdev->suspend_wait_q);
4646 static bool discovery_type_is_valid(struct hci_dev *hdev, uint8_t type,
4651 *mgmt_status = mgmt_le_support(hdev);
4656 *mgmt_status = mgmt_le_support(hdev);
4661 *mgmt_status = mgmt_bredr_support(hdev);
4673 static int start_discovery_internal(struct sock *sk, struct hci_dev *hdev,
4681 bt_dev_dbg(hdev, "sock %p", sk);
4683 hci_dev_lock(hdev);
4685 if (!hdev_is_powered(hdev)) {
4686 err = mgmt_cmd_complete(sk, hdev->id, op,
4692 if (hdev->discovery.state != DISCOVERY_STOPPED ||
4693 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
4694 err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY,
4699 if (!discovery_type_is_valid(hdev, cp->type, &status)) {
4700 err = mgmt_cmd_complete(sk, hdev->id, op, status,
4706 if (hdev->discovery_paused) {
4707 err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY,
4715 hci_discovery_filter_clear(hdev);
4717 hdev->discovery.type = cp->type;
4718 hdev->discovery.report_invalid_rssi = false;
4720 hdev->discovery.limited = true;
4722 hdev->discovery.limited = false;
4724 cmd = mgmt_pending_add(sk, op, hdev, data, len);
4732 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
4733 queue_work(hdev->req_workqueue, &hdev->discov_update);
4737 hci_dev_unlock(hdev);
4741 static int start_discovery(struct sock *sk, struct hci_dev *hdev,
4744 return start_discovery_internal(sk, hdev, MGMT_OP_START_DISCOVERY,
4748 static int start_limited_discovery(struct sock *sk, struct hci_dev *hdev,
4751 return start_discovery_internal(sk, hdev,
4763 static int start_service_discovery(struct sock *sk, struct hci_dev *hdev,
4773 bt_dev_dbg(hdev, "sock %p", sk);
4775 hci_dev_lock(hdev);
4777 if (!hdev_is_powered(hdev)) {
4778 err = mgmt_cmd_complete(sk, hdev->id,
4785 if (hdev->discovery.state != DISCOVERY_STOPPED ||
4786 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
4787 err = mgmt_cmd_complete(sk, hdev->id,
4796 bt_dev_err(hdev, "service_discovery: too big uuid_count value %u",
4798 err = mgmt_cmd_complete(sk, hdev->id,
4807 bt_dev_err(hdev, "service_discovery: expected %u bytes, got %u bytes",
4809 err = mgmt_cmd_complete(sk, hdev->id,
4816 if (!discovery_type_is_valid(hdev, cp->type, &status)) {
4817 err = mgmt_cmd_complete(sk, hdev->id,
4824 hdev, data, len);
4835 hci_discovery_filter_clear(hdev);
4837 hdev->discovery.result_filtering = true;
4838 hdev->discovery.type = cp->type;
4839 hdev->discovery.rssi = cp->rssi;
4840 hdev->discovery.uuid_count = uuid_count;
4843 hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16,
4845 if (!hdev->discovery.uuids) {
4846 err = mgmt_cmd_complete(sk, hdev->id,
4855 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
4856 queue_work(hdev->req_workqueue, &hdev->discov_update);
4860 hci_dev_unlock(hdev);
4864 void mgmt_stop_discovery_complete(struct hci_dev *hdev, u8 status)
4868 bt_dev_dbg(hdev, "status %d", status);
4870 hci_dev_lock(hdev);
4872 cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
4878 hci_dev_unlock(hdev);
4881 if (test_and_clear_bit(SUSPEND_PAUSE_DISCOVERY, hdev->suspend_tasks)) {
4882 bt_dev_dbg(hdev, "Paused discovery");
4883 wake_up(&hdev->suspend_wait_q);
4887 static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
4894 bt_dev_dbg(hdev, "sock %p", sk);
4896 hci_dev_lock(hdev);
4898 if (!hci_discovery_active(hdev)) {
4899 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
4905 if (hdev->discovery.type != mgmt_cp->type) {
4906 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
4912 cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len);
4920 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
4921 queue_work(hdev->req_workqueue, &hdev->discov_update);
4925 hci_dev_unlock(hdev);
4929 static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
4936 bt_dev_dbg(hdev, "sock %p", sk);
4938 hci_dev_lock(hdev);
4940 if (!hci_discovery_active(hdev)) {
4941 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
4947 e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr);
4949 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
4960 hci_inquiry_cache_update_resolve(hdev, e);
4963 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0,
4967 hci_dev_unlock(hdev);
4971 static int block_device(struct sock *sk, struct hci_dev *hdev, void *data,
4978 bt_dev_dbg(hdev, "sock %p", sk);
4981 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE,
4985 hci_dev_lock(hdev);
4987 err = hci_bdaddr_list_add(&hdev->reject_list, &cp->addr.bdaddr,
4994 mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr),
4999 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status,
5002 hci_dev_unlock(hdev);
5007 static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data,
5014 bt_dev_dbg(hdev, "sock %p", sk);
5017 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE,
5021 hci_dev_lock(hdev);
5023 err = hci_bdaddr_list_del(&hdev->reject_list, &cp->addr.bdaddr,
5030 mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr),
5035 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status,
5038 hci_dev_unlock(hdev);
5043 static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data,
5051 bt_dev_dbg(hdev, "sock %p", sk);
5056 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID,
5059 hci_dev_lock(hdev);
5061 hdev->devid_source = source;
5062 hdev->devid_vendor = __le16_to_cpu(cp->vendor);
5063 hdev->devid_product = __le16_to_cpu(cp->product);
5064 hdev->devid_version = __le16_to_cpu(cp->version);
5066 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0,
5069 hci_req_init(&req, hdev);
5073 hci_dev_unlock(hdev);
5078 static void enable_advertising_instance(struct hci_dev *hdev, u8 status,
5081 bt_dev_dbg(hdev, "status %d", status);
5084 static void set_advertising_complete(struct hci_dev *hdev, u8 status,
5087 struct cmd_lookup match = { NULL, hdev };
5093 hci_dev_lock(hdev);
5098 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev,
5103 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
5104 hci_dev_set_flag(hdev, HCI_ADVERTISING);
5106 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
5108 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp,
5111 new_settings(hdev, match.sk);
5118 hdev->suspend_tasks)) {
5119 bt_dev_dbg(hdev, "Paused advertising");
5120 wake_up(&hdev->suspend_wait_q);
5122 hdev->suspend_tasks)) {
5123 bt_dev_dbg(hdev, "Unpaused advertising");
5124 wake_up(&hdev->suspend_wait_q);
5130 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
5131 list_empty(&hdev->adv_instances))
5134 instance = hdev->cur_adv_instance;
5136 adv_instance = list_first_entry_or_null(&hdev->adv_instances,
5144 hci_req_init(&req, hdev);
5152 bt_dev_err(hdev, "failed to re-configure advertising");
5155 hci_dev_unlock(hdev);
5158 static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
5167 bt_dev_dbg(hdev, "sock %p", sk);
5169 status = mgmt_le_support(hdev);
5171 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
5177 if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY))
5178 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
5182 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
5185 if (hdev->advertising_paused)
5186 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
5189 hci_dev_lock(hdev);
5198 if (!hdev_is_powered(hdev) ||
5199 (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
5200 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) ||
5201 hci_conn_num(hdev, LE_LINK) > 0 ||
5202 (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
5203 hdev->le_scan_type == LE_SCAN_ACTIVE)) {
5207 hdev->cur_adv_instance = 0x00;
5208 changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING);
5210 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
5212 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
5214 changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING);
5215 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
5218 err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev);
5223 err = new_settings(hdev, sk);
5228 if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) ||
5229 pending_find(MGMT_OP_SET_LE, hdev)) {
5230 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
5235 cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len);
5241 hci_req_init(&req, hdev);
5244 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
5246 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
5248 cancel_adv_timeout(hdev);
5255 hdev->cur_adv_instance = 0x00;
5257 if (ext_adv_capable(hdev)) {
5273 hci_dev_unlock(hdev);
5277 static int set_static_address(struct sock *sk, struct hci_dev *hdev,
5283 bt_dev_dbg(hdev, "sock %p", sk);
5285 if (!lmp_le_capable(hdev))
5286 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
5289 if (hdev_is_powered(hdev))
5290 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
5295 return mgmt_cmd_status(sk, hdev->id,
5301 return mgmt_cmd_status(sk, hdev->id,
5306 hci_dev_lock(hdev);
5308 bacpy(&hdev->static_addr, &cp->bdaddr);
5310 err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev);
5314 err = new_settings(hdev, sk);
5317 hci_dev_unlock(hdev);
5321 static int set_scan_params(struct sock *sk, struct hci_dev *hdev,
5328 bt_dev_dbg(hdev, "sock %p", sk);
5330 if (!lmp_le_capable(hdev))
5331 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
5337 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
5343 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
5347 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
5350 hci_dev_lock(hdev);
5352 hdev->le_scan_interval = interval;
5353 hdev->le_scan_window = window;
5355 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0,
5361 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
5362 hdev->discovery.state == DISCOVERY_STOPPED) {
5365 hci_req_init(&req, hdev);
5373 hci_dev_unlock(hdev);
5378 static void fast_connectable_complete(struct hci_dev *hdev, u8 status,
5383 bt_dev_dbg(hdev, "status 0x%02x", status);
5385 hci_dev_lock(hdev);
5387 cmd = pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev);
5392 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
5398 hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE);
5400 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
5402 send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev);
5403 new_settings(hdev, cmd->sk);
5409 hci_dev_unlock(hdev);
5412 static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev,
5420 bt_dev_dbg(hdev, "sock %p", sk);
5422 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
5423 hdev->hci_ver < BLUETOOTH_VER_1_2)
5424 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
5428 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
5431 hci_dev_lock(hdev);
5433 if (pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) {
5434 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
5439 if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) {
5441 hdev);
5445 if (!hdev_is_powered(hdev)) {
5446 hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE);
5448 hdev);
5449 new_settings(hdev, sk);
5453 cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev,
5460 hci_req_init(&req, hdev);
5466 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
5472 hci_dev_unlock(hdev);
5477 static void set_bredr_complete(struct hci_dev *hdev, u8 status, u16 opcode)
5481 bt_dev_dbg(hdev, "status 0x%02x", status);
5483 hci_dev_lock(hdev);
5485 cmd = pending_find(MGMT_OP_SET_BREDR, hdev);
5495 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
5499 send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev);
5500 new_settings(hdev, cmd->sk);
5506 hci_dev_unlock(hdev);
5509 static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
5516 bt_dev_dbg(hdev, "sock %p", sk);
5518 if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev))
5519 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5522 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
5523 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5527 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5530 hci_dev_lock(hdev);
5532 if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
5533 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
5537 if (!hdev_is_powered(hdev)) {
5539 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
5540 hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
5541 hci_dev_clear_flag(hdev, HCI_LINK_SECURITY);
5542 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
5543 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
5546 hci_dev_change_flag(hdev, HCI_BREDR_ENABLED);
5548 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
5552 err = new_settings(hdev, sk);
5558 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5576 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
5577 (bacmp(&hdev->static_addr, BDADDR_ANY) ||
5578 hci_dev_test_flag(hdev, HCI_SC_ENABLED))) {
5579 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5585 if (pending_find(MGMT_OP_SET_BREDR, hdev)) {
5586 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5591 cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len);
5600 hci_dev_set_flag(hdev, HCI_BREDR_ENABLED);
5602 hci_req_init(&req, hdev);
5610 __hci_req_update_adv_data(&req, hdev->cur_adv_instance);
5617 hci_dev_unlock(hdev);
5621 static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
5626 bt_dev_dbg(hdev, "status %u", status);
5628 hci_dev_lock(hdev);
5630 cmd = pending_find(MGMT_OP_SET_SECURE_CONN, hdev);
5644 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
5645 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
5648 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
5649 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
5652 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
5653 hci_dev_set_flag(hdev, HCI_SC_ONLY);
5657 send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev);
5658 new_settings(hdev, cmd->sk);
5663 hci_dev_unlock(hdev);
5666 static int set_secure_conn(struct sock *sk, struct hci_dev *hdev,
5675 bt_dev_dbg(hdev, "sock %p", sk);
5677 if (!lmp_sc_capable(hdev) &&
5678 !hci_dev_test_flag(hdev, HCI_LE_ENABLED))
5679 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
5682 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
5683 lmp_sc_capable(hdev) &&
5684 !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
5685 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
5689 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
5692 hci_dev_lock(hdev);
5694 if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) ||
5695 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
5699 changed = !hci_dev_test_and_set_flag(hdev,
5702 hci_dev_set_flag(hdev, HCI_SC_ONLY);
5704 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
5706 changed = hci_dev_test_and_clear_flag(hdev,
5708 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
5711 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
5716 err = new_settings(hdev, sk);
5721 if (pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) {
5722 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
5729 if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
5730 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
5731 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
5735 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len);
5741 hci_req_init(&req, hdev);
5750 hci_dev_unlock(hdev);
5754 static int set_debug_keys(struct sock *sk, struct hci_dev *hdev,
5761 bt_dev_dbg(hdev, "sock %p", sk);
5764 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS,
5767 hci_dev_lock(hdev);
5770 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
5772 changed = hci_dev_test_and_clear_flag(hdev,
5776 use_changed = !hci_dev_test_and_set_flag(hdev,
5779 use_changed = hci_dev_test_and_clear_flag(hdev,
5782 if (hdev_is_powered(hdev) && use_changed &&
5783 hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
5785 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
5789 err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev);
5794 err = new_settings(hdev, sk);
5797 hci_dev_unlock(hdev);
5801 static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data,
5808 bt_dev_dbg(hdev, "sock %p", sk);
5810 if (!lmp_le_capable(hdev))
5811 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5815 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5818 if (hdev_is_powered(hdev))
5819 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5822 hci_dev_lock(hdev);
5827 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
5830 changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY);
5831 memcpy(hdev->irk, cp->irk, sizeof(hdev->irk));
5832 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
5833 hci_adv_instances_set_rpa_expired(hdev, true);
5835 hci_dev_set_flag(hdev, HCI_LIMITED_PRIVACY);
5837 hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY);
5839 changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY);
5840 memset(hdev->irk, 0, sizeof(hdev->irk));
5841 hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED);
5842 hci_adv_instances_set_rpa_expired(hdev, false);
5843 hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY);
5846 err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev);
5851 err = new_settings(hdev, sk);
5854 hci_dev_unlock(hdev);
5874 static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
5883 bt_dev_dbg(hdev, "sock %p", sk);
5885 if (!lmp_le_capable(hdev))
5886 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5891 bt_dev_err(hdev, "load_irks: too big irk_count value %u",
5893 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5899 bt_dev_err(hdev, "load_irks: expected %u bytes, got %u bytes",
5901 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5905 bt_dev_dbg(hdev, "irk_count %u", irk_count);
5911 return mgmt_cmd_status(sk, hdev->id,
5916 hci_dev_lock(hdev);
5918 hci_smp_irks_clear(hdev);
5924 if (hci_is_blocked_key(hdev,
5927 bt_dev_warn(hdev, "Skipping blocked IRK for %pMR",
5936 hci_add_irk(hdev, &irk->addr.bdaddr,
5941 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
5943 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0);
5945 hci_dev_unlock(hdev);
5969 static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
5978 bt_dev_dbg(hdev, "sock %p", sk);
5980 if (!lmp_le_capable(hdev))
5981 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5986 bt_dev_err(hdev, "load_ltks: too big key_count value %u",
5988 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5994 bt_dev_err(hdev, "load_keys: expected %u bytes, got %u bytes",
5996 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
6000 bt_dev_dbg(hdev, "key_count %u", key_count);
6006 return mgmt_cmd_status(sk, hdev->id,
6011 hci_dev_lock(hdev);
6013 hci_smp_ltks_clear(hdev);
6020 if (hci_is_blocked_key(hdev,
6023 bt_dev_warn(hdev, "Skipping blocked LTK for %pMR",
6057 hci_add_ltk(hdev, &key->addr.bdaddr,
6062 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0,
6065 hci_dev_unlock(hdev);
6097 static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status,
6106 bt_dev_dbg(hdev, "status 0x%02x", hci_status);
6108 hci_dev_lock(hdev);
6119 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI);
6121 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
6128 bt_dev_err(hdev, "invalid sent_cmd in conn_info response");
6133 conn = hci_conn_hash_lookup_handle(hdev, handle);
6135 bt_dev_err(hdev, "unknown handle (%d) in conn_info response",
6140 cmd = pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn);
6148 hci_dev_unlock(hdev);
6151 static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data,
6160 bt_dev_dbg(hdev, "sock %p", sk);
6167 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
6171 hci_dev_lock(hdev);
6173 if (!hdev_is_powered(hdev)) {
6174 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
6181 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
6184 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
6187 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
6193 if (pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) {
6194 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
6200 * calculate conn info age as random value between min/max set in hdev.
6202 conn_info_age = hdev->conn_info_min_age +
6203 prandom_u32_max(hdev->conn_info_max_age -
6204 hdev->conn_info_min_age);
6217 hci_req_init(&req, hdev);
6245 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev,
6263 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
6268 hci_dev_unlock(hdev);
6276 struct hci_dev *hdev;
6285 hdev = hci_dev_get(cmd->index);
6286 if (hdev) {
6287 rp.local_clock = cpu_to_le32(hdev->clock);
6288 hci_dev_put(hdev);
6308 static void get_clock_info_complete(struct hci_dev *hdev, u8 status, u16 opcode)
6314 bt_dev_dbg(hdev, "status %u", status);
6316 hci_dev_lock(hdev);
6318 hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
6324 conn = hci_conn_hash_lookup_handle(hdev, handle);
6329 cmd = pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn);
6337 hci_dev_unlock(hdev);
6340 static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data,
6351 bt_dev_dbg(hdev, "sock %p", sk);
6358 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
6362 hci_dev_lock(hdev);
6364 if (!hdev_is_powered(hdev)) {
6365 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
6372 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
6375 err = mgmt_cmd_complete(sk, hdev->id,
6385 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len);
6393 hci_req_init(&req, hdev);
6412 hci_dev_unlock(hdev);
6416 static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
6420 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
6433 /* This function requires the caller holds hdev->lock */
6434 static int hci_conn_params_set(struct hci_dev *hdev, bdaddr_t *addr,
6439 params = hci_conn_params_add(hdev, addr, addr_type);
6455 list_add(&params->action, &hdev->pend_le_conns);
6459 list_add(&params->action, &hdev->pend_le_conns);
6461 list_add(&params->action, &hdev->pend_le_reports);
6465 if (!is_connected(hdev, addr, addr_type))
6466 list_add(&params->action, &hdev->pend_le_conns);
6472 bt_dev_dbg(hdev, "addr %pMR (type %u) auto_connect %u",
6478 static void device_added(struct sock *sk, struct hci_dev *hdev,
6487 mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk);
6490 static int add_device(struct sock *sk, struct hci_dev *hdev,
6499 bt_dev_dbg(hdev, "sock %p", sk);
6503 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
6508 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
6512 hci_dev_lock(hdev);
6517 err = mgmt_cmd_complete(sk, hdev->id,
6524 err = hci_bdaddr_list_add_with_flags(&hdev->accept_list,
6530 hci_req_update_scan(hdev);
6550 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
6559 if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type,
6561 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
6566 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
6572 hci_update_background_scan(hdev);
6575 device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action);
6576 device_flags_changed(NULL, hdev, &cp->addr.bdaddr, cp->addr.type,
6579 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
6584 hci_dev_unlock(hdev);
6588 static void device_removed(struct sock *sk, struct hci_dev *hdev,
6596 mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk);
6599 static int remove_device(struct sock *sk, struct hci_dev *hdev,
6605 bt_dev_dbg(hdev, "sock %p", sk);
6607 hci_dev_lock(hdev);
6614 err = mgmt_cmd_complete(sk, hdev->id,
6622 err = hci_bdaddr_list_del(&hdev->accept_list,
6626 err = mgmt_cmd_complete(sk, hdev->id,
6634 hci_req_update_scan(hdev);
6636 device_removed(sk, hdev, &cp->addr.bdaddr,
6649 err = mgmt_cmd_complete(sk, hdev->id,
6656 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
6659 err = mgmt_cmd_complete(sk, hdev->id,
6668 err = mgmt_cmd_complete(sk, hdev->id,
6678 hci_update_background_scan(hdev);
6680 device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type);
6686 err = mgmt_cmd_complete(sk, hdev->id,
6693 list_for_each_entry_safe(b, btmp, &hdev->accept_list, list) {
6694 device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type);
6699 hci_req_update_scan(hdev);
6701 list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) {
6704 device_removed(sk, hdev, &p->addr, p->addr_type);
6714 bt_dev_dbg(hdev, "All LE connection parameters were removed");
6716 hci_update_background_scan(hdev);
6720 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE,
6724 hci_dev_unlock(hdev);
6728 static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data,
6737 if (!lmp_le_capable(hdev))
6738 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
6743 bt_dev_err(hdev, "load_conn_param: too big param_count value %u",
6745 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
6751 bt_dev_err(hdev, "load_conn_param: expected %u bytes, got %u bytes",
6753 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
6757 bt_dev_dbg(hdev, "param_count %u", param_count);
6759 hci_dev_lock(hdev);
6761 hci_conn_params_clear_disabled(hdev);
6769 bt_dev_dbg(hdev, "Adding %pMR (type %u)", &param->addr.bdaddr,
6777 bt_dev_err(hdev, "ignoring invalid connection parameters");
6786 bt_dev_dbg(hdev, "min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x",
6790 bt_dev_err(hdev, "ignoring invalid connection parameters");
6794 hci_param = hci_conn_params_add(hdev, &param->addr.bdaddr,
6797 bt_dev_err(hdev, "failed to add connection parameters");
6807 hci_dev_unlock(hdev);
6809 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0,
6813 static int set_external_config(struct sock *sk, struct hci_dev *hdev,
6820 bt_dev_dbg(hdev, "sock %p", sk);
6822 if (hdev_is_powered(hdev))
6823 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6827 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6830 if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
6831 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6834 hci_dev_lock(hdev);
6837 changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED);
6839 changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED);
6841 err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev);
6848 err = new_options(hdev, sk);
6850 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) {
6851 mgmt_index_removed(hdev);
6853 if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) {
6854 hci_dev_set_flag(hdev, HCI_CONFIG);
6855 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
6857 queue_work(hdev->req_workqueue, &hdev->power_on);
6859 set_bit(HCI_RAW, &hdev->flags);
6860 mgmt_index_added(hdev);
6865 hci_dev_unlock(hdev);
6869 static int set_public_address(struct sock *sk, struct hci_dev *hdev,
6876 bt_dev_dbg(hdev, "sock %p", sk);
6878 if (hdev_is_powered(hdev))
6879 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6883 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6886 if (!hdev->set_bdaddr)
6887 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6890 hci_dev_lock(hdev);
6892 changed = !!bacmp(&hdev->public_addr, &cp->bdaddr);
6893 bacpy(&hdev->public_addr, &cp->bdaddr);
6895 err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev);
6902 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
6903 err = new_options(hdev, sk);
6905 if (is_configured(hdev)) {
6906 mgmt_index_removed(hdev);
6908 hci_dev_clear_flag(hdev, HCI_UNCONFIGURED);
6910 hci_dev_set_flag(hdev, HCI_CONFIG);
6911 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
6913 queue_work(hdev->req_workqueue, &hdev->power_on);
6917 hci_dev_unlock(hdev);
6921 static void read_local_oob_ext_data_complete(struct hci_dev *hdev, u8 status,
6931 bt_dev_dbg(hdev, "status %u", status);
6933 cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev);
6973 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
6996 hdev->dev_class, 3);
7016 err = mgmt_cmd_complete(cmd->sk, hdev->id,
7024 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
7032 static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk,
7039 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev,
7044 hci_req_init(&req, hdev);
7046 if (bredr_sc_enabled(hdev))
7060 static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev,
7070 bt_dev_dbg(hdev, "sock %p", sk);
7072 if (hdev_is_powered(hdev)) {
7075 status = mgmt_bredr_support(hdev);
7082 status = mgmt_le_support(hdev);
7106 hci_dev_lock(hdev);
7111 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
7112 err = read_local_ssp_oob_req(hdev, sk, cp);
7113 hci_dev_unlock(hdev);
7122 hdev->dev_class, 3);
7126 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
7127 smp_generate_oob(hdev, hash, rand) < 0) {
7128 hci_dev_unlock(hdev);
7143 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
7144 hci_dev_unlock(hdev);
7149 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
7150 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
7151 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
7152 bacmp(&hdev->static_addr, BDADDR_ANY))) {
7153 memcpy(addr, &hdev->static_addr, 6);
7156 memcpy(addr, &hdev->bdaddr, 6);
7163 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
7171 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) {
7181 flags = mgmt_get_adv_discov_flags(hdev);
7183 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
7191 hci_dev_unlock(hdev);
7201 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
7206 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
7216 static u32 get_supported_adv_flags(struct hci_dev *hdev)
7230 if ((hdev->adv_tx_power != HCI_TX_POWER_INVALID) ||
7231 ext_adv_capable(hdev))
7234 if (ext_adv_capable(hdev)) {
7239 if (hdev->le_features[1] & HCI_LE_PHY_2M)
7242 if (hdev->le_features[1] & HCI_LE_PHY_CODED)
7249 static int read_adv_features(struct sock *sk, struct hci_dev *hdev,
7259 bt_dev_dbg(hdev, "sock %p", sk);
7261 if (!lmp_le_capable(hdev))
7262 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
7268 if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY))
7269 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
7272 hci_dev_lock(hdev);
7274 rp_len = sizeof(*rp) + hdev->adv_instance_cnt;
7277 hci_dev_unlock(hdev);
7281 supported_flags = get_supported_adv_flags(hdev);
7286 rp->max_instances = hdev->le_num_of_adv_sets;
7287 rp->num_instances = hdev->adv_instance_cnt;
7290 list_for_each_entry(adv_instance, &hdev->adv_instances, list) {
7295 hci_dev_unlock(hdev);
7297 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
7305 static u8 calculate_name_len(struct hci_dev *hdev)
7309 return append_local_name(hdev, buf, 0);
7312 static u8 tlv_data_max_len(struct hci_dev *hdev, u32 adv_flags,
7327 max_len -= calculate_name_len(hdev);
7358 static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data,
7364 max_len = tlv_data_max_len(hdev, adv_flags, is_adv_data);
7403 static void add_advertising_complete(struct hci_dev *hdev, u8 status,
7412 bt_dev_dbg(hdev, "status %d", status);
7414 hci_dev_lock(hdev);
7416 cmd = pending_find(MGMT_OP_ADD_ADVERTISING, hdev);
7418 list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, list) {
7429 if (hdev->cur_adv_instance == instance)
7430 cancel_adv_timeout(hdev);
7432 hci_remove_adv_instance(hdev, instance);
7433 mgmt_advertising_removed(cmd ? cmd->sk : NULL, hdev, instance);
7452 hci_dev_unlock(hdev);
7455 static int add_advertising(struct sock *sk, struct hci_dev *hdev,
7464 unsigned int prev_instance_cnt = hdev->adv_instance_cnt;
7471 bt_dev_dbg(hdev, "sock %p", sk);
7473 status = mgmt_le_support(hdev);
7475 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7481 if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY))
7482 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7485 if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets)
7486 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7490 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7500 supported_flags = get_supported_adv_flags(hdev);
7504 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7507 hci_dev_lock(hdev);
7509 if (timeout && !hdev_is_powered(hdev)) {
7510 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7515 if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
7516 pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) ||
7517 pending_find(MGMT_OP_SET_LE, hdev)) {
7518 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7523 if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) ||
7524 !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len,
7526 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7531 err = hci_add_adv_instance(hdev, cp->instance, flags,
7537 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7545 if (hdev->adv_instance_cnt > prev_instance_cnt)
7546 mgmt_advertising_added(sk, hdev, cp->instance);
7548 if (hdev->cur_adv_instance == cp->instance) {
7554 cancel_adv_timeout(hdev);
7556 next_instance = hci_get_next_instance(hdev, cp->instance);
7559 } else if (!hdev->adv_instance_timeout) {
7570 if (!hdev_is_powered(hdev) ||
7571 hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
7574 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7582 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_ADVERTISING, hdev, data,
7589 hci_req_init(&req, hdev);
7597 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7603 hci_dev_unlock(hdev);
7608 static void remove_advertising_complete(struct hci_dev *hdev, u8 status,
7615 bt_dev_dbg(hdev, "status %d", status);
7617 hci_dev_lock(hdev);
7623 cmd = pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev);
7635 hci_dev_unlock(hdev);
7638 static int remove_advertising(struct sock *sk, struct hci_dev *hdev,
7647 bt_dev_dbg(hdev, "sock %p", sk);
7652 if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY))
7653 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
7656 hci_dev_lock(hdev);
7658 if (cp->instance && !hci_find_adv_instance(hdev, cp->instance)) {
7659 err = mgmt_cmd_status(sk, hdev->id,
7665 if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
7666 pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) ||
7667 pending_find(MGMT_OP_SET_LE, hdev)) {
7668 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
7673 if (list_empty(&hdev->adv_instances)) {
7674 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
7679 hci_req_init(&req, hdev);
7682 if (ext_adv_capable(hdev)) {
7687 hci_req_clear_adv_instance(hdev, sk, &req, cp->instance, true);
7689 if (list_empty(&hdev->adv_instances))
7697 !hdev_is_powered(hdev) ||
7698 hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
7701 err = mgmt_cmd_complete(sk, hdev->id,
7707 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data,
7719 hci_dev_unlock(hdev);
7724 static int get_adv_size_info(struct sock *sk, struct hci_dev *hdev,
7732 bt_dev_dbg(hdev, "sock %p", sk);
7734 if (!lmp_le_capable(hdev))
7735 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
7738 if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets)
7739 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
7747 supported_flags = get_supported_adv_flags(hdev);
7749 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
7754 rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true);
7755 rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false);
7757 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
7884 void mgmt_index_added(struct hci_dev *hdev)
7888 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
7891 switch (hdev->dev_type) {
7893 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
7894 mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev,
7898 mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0,
7910 ev.bus = hdev->bus;
7912 mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev),
7916 void mgmt_index_removed(struct hci_dev *hdev)
7921 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
7924 switch (hdev->dev_type) {
7926 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
7928 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
7929 mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev,
7933 mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0,
7945 ev.bus = hdev->bus;
7947 mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev),
7951 /* This function requires the caller holds hdev->lock */
7952 static void restart_le_actions(struct hci_dev *hdev)
7956 list_for_each_entry(p, &hdev->le_conn_params, list) {
7965 list_add(&p->action, &hdev->pend_le_conns);
7968 list_add(&p->action, &hdev->pend_le_reports);
7976 void mgmt_power_on(struct hci_dev *hdev, int err)
7978 struct cmd_lookup match = { NULL, hdev };
7980 bt_dev_dbg(hdev, "err %d", err);
7982 hci_dev_lock(hdev);
7985 restart_le_actions(hdev);
7986 hci_update_background_scan(hdev);
7989 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
7991 new_settings(hdev, match.sk);
7996 hci_dev_unlock(hdev);
7999 void __mgmt_power_off(struct hci_dev *hdev)
8001 struct cmd_lookup match = { NULL, hdev };
8004 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
8006 /* If the power off is because of hdev unregistration let
8013 if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
8018 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
8020 if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) {
8021 mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
8024 ext_info_changed(hdev, NULL);
8027 new_settings(hdev, match.sk);
8033 void mgmt_set_powered_failed(struct hci_dev *hdev, int err)
8038 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
8047 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status);
8052 void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
8066 mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL);
8088 void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent)
8128 mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL);
8131 void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk, bool persistent)
8144 mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL);
8147 void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
8174 mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL);
8177 void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr,
8195 mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL);
8198 void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
8231 mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf,
8249 struct hci_dev *hdev = data;
8252 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk);
8258 bool mgmt_powering_down(struct hci_dev *hdev)
8263 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
8274 void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
8284 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
8285 cancel_delayed_work(&hdev->power_off);
8286 queue_work(hdev->req_workqueue, &hdev->power_off.work);
8295 mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk);
8302 if (hdev->suspended)
8305 mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk);
8310 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
8311 hdev);
8314 void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
8321 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
8322 hdev);
8324 cmd = pending_find(MGMT_OP_DISCONNECT, hdev);
8340 void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
8348 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
8349 cancel_delayed_work(&hdev->power_off);
8350 queue_work(hdev->req_workqueue, &hdev->power_off.work);
8357 mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL);
8360 void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure)
8368 mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL);
8371 void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
8376 cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev);
8384 void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
8389 cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev);
8397 int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
8403 bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr);
8410 return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev),
8414 int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
8419 bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr);
8424 return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev),
8428 static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
8434 cmd = pending_find(opcode, hdev);
8444 int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
8447 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
8451 int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
8454 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
8459 int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
8462 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
8466 int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
8469 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
8474 int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
8480 bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr);
8487 return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL);
8502 mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev),
8511 void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status)
8513 struct cmd_lookup match = { NULL, hdev };
8518 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev,
8523 if (test_bit(HCI_AUTH, &hdev->flags))
8524 changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY);
8526 changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY);
8528 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp,
8532 new_settings(hdev, match.sk);
8540 struct hci_dev *hdev = req->hdev;
8543 if (!lmp_ext_inq_capable(hdev))
8546 memset(hdev->eir, 0, sizeof(hdev->eir));
8553 void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
8555 struct cmd_lookup match = { NULL, hdev };
8562 if (enable && hci_dev_test_and_clear_flag(hdev,
8564 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
8565 new_settings(hdev, NULL);
8568 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp,
8574 changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED);
8576 changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED);
8578 changed = hci_dev_test_and_clear_flag(hdev,
8581 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
8584 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match);
8587 new_settings(hdev, match.sk);
8592 hci_req_init(&req, hdev);
8594 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
8595 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
8616 void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
8619 struct cmd_lookup match = { NULL, hdev, mgmt_status(status) };
8621 mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match);
8622 mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match);
8623 mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match);
8626 mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class,
8628 ext_info_changed(hdev, NULL);
8635 void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
8645 memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH);
8647 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
8649 memcpy(hdev->dev_name, name, sizeof(hdev->dev_name));
8654 if (pending_find(MGMT_OP_SET_POWERED, hdev))
8658 mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev),
8660 ext_info_changed(hdev, cmd ? cmd->sk : NULL);
8730 static void restart_le_scan(struct hci_dev *hdev)
8733 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
8737 hdev->discovery.scan_start +
8738 hdev->discovery.scan_duration))
8741 queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_restart,
8745 static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir,
8757 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
8759 (rssi < hdev->discovery.rssi &&
8760 !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks))))
8763 if (hdev->discovery.uuid_count != 0) {
8767 if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count,
8768 hdev->discovery.uuids) &&
8770 hdev->discovery.uuid_count,
8771 hdev->discovery.uuids))
8778 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) {
8779 restart_le_scan(hdev);
8782 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
8783 rssi < hdev->discovery.rssi)
8790 void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
8802 if (!hci_discovery_active(hdev)) {
8806 list_empty(&hdev->pend_le_reports) &&
8807 !hci_is_adv_monitoring(hdev)) {
8812 if (hdev->discovery.result_filtering) {
8814 if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp,
8819 if (hdev->discovery.limited) {
8847 if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi &&
8872 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL);
8875 void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
8895 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, sizeof(*ev) + eir_len, NULL);
8898 void mgmt_discovering(struct hci_dev *hdev, u8 discovering)
8902 bt_dev_dbg(hdev, "discovering %u", discovering);
8905 ev.type = hdev->discovery.type;
8908 mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL);
8911 void mgmt_suspending(struct hci_dev *hdev, u8 state)
8916 mgmt_event(MGMT_EV_CONTROLLER_SUSPEND, hdev, &ev, sizeof(ev), NULL);
8919 void mgmt_resuming(struct hci_dev *hdev, u8 reason, bdaddr_t *bdaddr,
8932 mgmt_event(MGMT_EV_CONTROLLER_RESUME, hdev, &ev, sizeof(ev), NULL);