1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020 Google Corporation
6 #include <net/bluetooth/bluetooth.h>
7 #include <net/bluetooth/hci_core.h>
8 #include <net/bluetooth/mgmt.h>
10 #include "hci_request.h"
11 #include "mgmt_util.h"
14 #define MSFT_RSSI_THRESHOLD_VALUE_MIN -127
15 #define MSFT_RSSI_THRESHOLD_VALUE_MAX 20
16 #define MSFT_RSSI_LOW_TIMEOUT_MAX 0x3C
18 #define MSFT_OP_READ_SUPPORTED_FEATURES 0x00
19 struct msft_cp_read_supported_features {
23 struct msft_rp_read_supported_features {
31 #define MSFT_OP_LE_MONITOR_ADVERTISEMENT 0x03
32 #define MSFT_MONITOR_ADVERTISEMENT_TYPE_PATTERN 0x01
33 struct msft_le_monitor_advertisement_pattern {
40 struct msft_le_monitor_advertisement_pattern_data {
45 struct msft_cp_le_monitor_advertisement {
49 __u8 rssi_low_interval;
50 __u8 rssi_sampling_period;
55 struct msft_rp_le_monitor_advertisement {
61 #define MSFT_OP_LE_CANCEL_MONITOR_ADVERTISEMENT 0x04
62 struct msft_cp_le_cancel_monitor_advertisement {
67 struct msft_rp_le_cancel_monitor_advertisement {
72 #define MSFT_OP_LE_SET_ADVERTISEMENT_FILTER_ENABLE 0x05
73 struct msft_cp_le_set_advertisement_filter_enable {
78 struct msft_rp_le_set_advertisement_filter_enable {
83 #define MSFT_EV_LE_MONITOR_DEVICE 0x02
84 struct msft_ev_le_monitor_device {
91 struct msft_monitor_advertisement_handle_data {
96 __u8 rssi_low_interval;
97 __u8 rssi_sampling_period;
99 struct list_head list;
102 enum monitor_addr_filter_state {
109 #define MSFT_MONITOR_ADVERTISEMENT_TYPE_ADDR 0x04
110 struct msft_monitor_addr_filter_data {
112 __u8 pattern_handle; /* address filters pertain to */
117 __u8 rssi_low_interval;
118 __u8 rssi_sampling_period;
121 struct list_head list;
128 struct list_head handle_map;
129 struct list_head address_filters;
133 /* To synchronize add/remove address filter and monitor device event.*/
134 struct mutex filter_lock;
137 bool msft_monitor_supported(struct hci_dev *hdev)
139 return !!(msft_get_features(hdev) & MSFT_FEATURE_MASK_LE_ADV_MONITOR);
142 static bool read_supported_features(struct hci_dev *hdev,
143 struct msft_data *msft)
145 struct msft_cp_read_supported_features cp;
146 struct msft_rp_read_supported_features *rp;
149 cp.sub_opcode = MSFT_OP_READ_SUPPORTED_FEATURES;
151 skb = __hci_cmd_sync(hdev, hdev->msft_opcode, sizeof(cp), &cp,
153 if (IS_ERR_OR_NULL(skb)) {
157 bt_dev_err(hdev, "Failed to read MSFT supported features (%ld)",
162 if (skb->len < sizeof(*rp)) {
163 bt_dev_err(hdev, "MSFT supported features length mismatch");
167 rp = (struct msft_rp_read_supported_features *)skb->data;
169 if (rp->sub_opcode != MSFT_OP_READ_SUPPORTED_FEATURES)
172 if (rp->evt_prefix_len > 0) {
173 msft->evt_prefix = kmemdup(rp->evt_prefix, rp->evt_prefix_len,
175 if (!msft->evt_prefix)
179 msft->evt_prefix_len = rp->evt_prefix_len;
180 msft->features = __le64_to_cpu(rp->features);
182 if (msft->features & MSFT_FEATURE_MASK_CURVE_VALIDITY)
183 hdev->msft_curve_validity = true;
193 /* is_mgmt = true matches the handle exposed to userspace via mgmt.
194 * is_mgmt = false matches the handle used by the msft controller.
195 * This function requires the caller holds hdev->lock
197 static struct msft_monitor_advertisement_handle_data *msft_find_handle_data
198 (struct hci_dev *hdev, u16 handle, bool is_mgmt)
200 struct msft_monitor_advertisement_handle_data *entry;
201 struct msft_data *msft = hdev->msft_data;
203 list_for_each_entry(entry, &msft->handle_map, list) {
204 if (is_mgmt && entry->mgmt_handle == handle)
206 if (!is_mgmt && entry->msft_handle == handle)
213 /* This function requires the caller holds msft->filter_lock */
214 static struct msft_monitor_addr_filter_data *msft_find_address_data
215 (struct hci_dev *hdev, u8 addr_type, bdaddr_t *addr,
218 struct msft_monitor_addr_filter_data *entry;
219 struct msft_data *msft = hdev->msft_data;
221 list_for_each_entry(entry, &msft->address_filters, list) {
222 if (entry->pattern_handle == pattern_handle &&
223 addr_type == entry->addr_type &&
224 !bacmp(addr, &entry->bdaddr))
231 /* This function requires the caller holds hdev->lock */
232 static int msft_monitor_device_del(struct hci_dev *hdev, __u16 mgmt_handle,
233 bdaddr_t *bdaddr, __u8 addr_type,
236 struct monitored_device *dev, *tmp;
239 list_for_each_entry_safe(dev, tmp, &hdev->monitored_devices, list) {
240 /* mgmt_handle == 0 indicates remove all devices, whereas,
241 * bdaddr == NULL indicates remove all devices matching the
244 if ((!mgmt_handle || dev->handle == mgmt_handle) &&
245 (!bdaddr || (!bacmp(bdaddr, &dev->bdaddr) &&
246 addr_type == dev->addr_type))) {
247 if (notify && dev->notified) {
248 mgmt_adv_monitor_device_lost(hdev, dev->handle,
253 list_del(&dev->list);
262 static int msft_le_monitor_advertisement_cb(struct hci_dev *hdev, u16 opcode,
263 struct adv_monitor *monitor,
266 struct msft_rp_le_monitor_advertisement *rp;
267 struct msft_monitor_advertisement_handle_data *handle_data;
268 struct msft_data *msft = hdev->msft_data;
273 rp = (struct msft_rp_le_monitor_advertisement *)skb->data;
274 if (skb->len < sizeof(*rp)) {
275 status = HCI_ERROR_UNSPECIFIED;
283 handle_data = kmalloc(sizeof(*handle_data), GFP_KERNEL);
285 status = HCI_ERROR_UNSPECIFIED;
289 handle_data->mgmt_handle = monitor->handle;
290 handle_data->msft_handle = rp->handle;
291 handle_data->cond_type = MSFT_MONITOR_ADVERTISEMENT_TYPE_PATTERN;
292 INIT_LIST_HEAD(&handle_data->list);
293 list_add(&handle_data->list, &msft->handle_map);
295 monitor->state = ADV_MONITOR_STATE_OFFLOADED;
299 hci_free_adv_monitor(hdev, monitor);
301 hci_dev_unlock(hdev);
306 /* This function requires the caller holds hci_req_sync_lock */
307 static void msft_remove_addr_filters_sync(struct hci_dev *hdev, u8 handle)
309 struct msft_monitor_addr_filter_data *address_filter, *n;
310 struct msft_cp_le_cancel_monitor_advertisement cp;
311 struct msft_data *msft = hdev->msft_data;
312 struct list_head head;
315 INIT_LIST_HEAD(&head);
317 /* Cancel all corresponding address monitors */
318 mutex_lock(&msft->filter_lock);
320 list_for_each_entry_safe(address_filter, n, &msft->address_filters,
322 if (address_filter->pattern_handle != handle)
325 list_del(&address_filter->list);
327 /* Keep the address filter and let
328 * msft_add_address_filter_sync() remove and free the address
331 if (address_filter->state == AF_STATE_ADDING) {
332 address_filter->state = AF_STATE_REMOVING;
336 /* Keep the address filter and let
337 * msft_cancel_address_filter_sync() remove and free the address
340 if (address_filter->state == AF_STATE_REMOVING)
343 list_add_tail(&address_filter->list, &head);
346 mutex_unlock(&msft->filter_lock);
348 list_for_each_entry_safe(address_filter, n, &head, list) {
349 list_del(&address_filter->list);
351 cp.sub_opcode = MSFT_OP_LE_CANCEL_MONITOR_ADVERTISEMENT;
352 cp.handle = address_filter->msft_handle;
354 skb = __hci_cmd_sync(hdev, hdev->msft_opcode, sizeof(cp), &cp,
356 if (IS_ERR_OR_NULL(skb)) {
357 kfree(address_filter);
363 bt_dev_dbg(hdev, "MSFT: Canceled device %pMR address filter",
364 &address_filter->bdaddr);
366 kfree(address_filter);
370 static int msft_le_cancel_monitor_advertisement_cb(struct hci_dev *hdev,
372 struct adv_monitor *monitor,
375 struct msft_rp_le_cancel_monitor_advertisement *rp;
376 struct msft_monitor_advertisement_handle_data *handle_data;
377 struct msft_data *msft = hdev->msft_data;
381 rp = (struct msft_rp_le_cancel_monitor_advertisement *)skb->data;
382 if (skb->len < sizeof(*rp)) {
383 status = HCI_ERROR_UNSPECIFIED;
393 handle_data = msft_find_handle_data(hdev, monitor->handle, true);
396 if (monitor->state == ADV_MONITOR_STATE_OFFLOADED)
397 monitor->state = ADV_MONITOR_STATE_REGISTERED;
399 /* Do not free the monitor if it is being removed due to
400 * suspend. It will be re-monitored on resume.
402 if (!msft->suspending) {
403 hci_free_adv_monitor(hdev, monitor);
405 /* Clear any monitored devices by this Adv Monitor */
406 msft_monitor_device_del(hdev, handle_data->mgmt_handle,
410 msft_handle = handle_data->msft_handle;
412 list_del(&handle_data->list);
415 hci_dev_unlock(hdev);
417 msft_remove_addr_filters_sync(hdev, msft_handle);
419 hci_dev_unlock(hdev);
426 /* This function requires the caller holds hci_req_sync_lock */
427 static int msft_remove_monitor_sync(struct hci_dev *hdev,
428 struct adv_monitor *monitor)
430 struct msft_cp_le_cancel_monitor_advertisement cp;
431 struct msft_monitor_advertisement_handle_data *handle_data;
434 handle_data = msft_find_handle_data(hdev, monitor->handle, true);
436 /* If no matched handle, just remove without telling controller */
440 cp.sub_opcode = MSFT_OP_LE_CANCEL_MONITOR_ADVERTISEMENT;
441 cp.handle = handle_data->msft_handle;
443 skb = __hci_cmd_sync(hdev, hdev->msft_opcode, sizeof(cp), &cp,
445 if (IS_ERR_OR_NULL(skb)) {
451 return msft_le_cancel_monitor_advertisement_cb(hdev, hdev->msft_opcode,
455 /* This function requires the caller holds hci_req_sync_lock */
456 int msft_suspend_sync(struct hci_dev *hdev)
458 struct msft_data *msft = hdev->msft_data;
459 struct adv_monitor *monitor;
462 if (!msft || !msft_monitor_supported(hdev))
465 msft->suspending = true;
468 monitor = idr_get_next(&hdev->adv_monitors_idr, &handle);
472 msft_remove_monitor_sync(hdev, monitor);
477 /* All monitors have been removed */
478 msft->suspending = false;
483 static bool msft_monitor_rssi_valid(struct adv_monitor *monitor)
485 struct adv_rssi_thresholds *r = &monitor->rssi;
487 if (r->high_threshold < MSFT_RSSI_THRESHOLD_VALUE_MIN ||
488 r->high_threshold > MSFT_RSSI_THRESHOLD_VALUE_MAX ||
489 r->low_threshold < MSFT_RSSI_THRESHOLD_VALUE_MIN ||
490 r->low_threshold > MSFT_RSSI_THRESHOLD_VALUE_MAX)
493 /* High_threshold_timeout is not supported,
494 * once high_threshold is reached, events are immediately reported.
496 if (r->high_threshold_timeout != 0)
499 if (r->low_threshold_timeout > MSFT_RSSI_LOW_TIMEOUT_MAX)
502 /* Sampling period from 0x00 to 0xFF are all allowed */
506 static bool msft_monitor_pattern_valid(struct adv_monitor *monitor)
508 return msft_monitor_rssi_valid(monitor);
509 /* No additional check needed for pattern-based monitor */
512 static int msft_add_monitor_sync(struct hci_dev *hdev,
513 struct adv_monitor *monitor)
515 struct msft_cp_le_monitor_advertisement *cp;
516 struct msft_le_monitor_advertisement_pattern_data *pattern_data;
517 struct msft_monitor_advertisement_handle_data *handle_data;
518 struct msft_le_monitor_advertisement_pattern *pattern;
519 struct adv_pattern *entry;
520 size_t total_size = sizeof(*cp) + sizeof(*pattern_data);
521 ptrdiff_t offset = 0;
522 u8 pattern_count = 0;
526 if (!msft_monitor_pattern_valid(monitor))
529 list_for_each_entry(entry, &monitor->patterns, list) {
531 total_size += sizeof(*pattern) + entry->length;
534 cp = kmalloc(total_size, GFP_KERNEL);
538 cp->sub_opcode = MSFT_OP_LE_MONITOR_ADVERTISEMENT;
539 cp->rssi_high = monitor->rssi.high_threshold;
540 cp->rssi_low = monitor->rssi.low_threshold;
541 cp->rssi_low_interval = (u8)monitor->rssi.low_threshold_timeout;
542 cp->rssi_sampling_period = monitor->rssi.sampling_period;
544 cp->cond_type = MSFT_MONITOR_ADVERTISEMENT_TYPE_PATTERN;
546 pattern_data = (void *)cp->data;
547 pattern_data->count = pattern_count;
549 list_for_each_entry(entry, &monitor->patterns, list) {
550 pattern = (void *)(pattern_data->data + offset);
551 /* the length also includes data_type and offset */
552 pattern->length = entry->length + 2;
553 pattern->data_type = entry->ad_type;
554 pattern->start_byte = entry->offset;
555 memcpy(pattern->pattern, entry->value, entry->length);
556 offset += sizeof(*pattern) + entry->length;
559 skb = __hci_cmd_sync(hdev, hdev->msft_opcode, total_size, cp,
562 if (IS_ERR_OR_NULL(skb)) {
567 err = msft_le_monitor_advertisement_cb(hdev, hdev->msft_opcode,
572 handle_data = msft_find_handle_data(hdev, monitor->handle, true);
578 handle_data->rssi_high = cp->rssi_high;
579 handle_data->rssi_low = cp->rssi_low;
580 handle_data->rssi_low_interval = cp->rssi_low_interval;
581 handle_data->rssi_sampling_period = cp->rssi_sampling_period;
588 /* This function requires the caller holds hci_req_sync_lock */
589 static void reregister_monitor(struct hci_dev *hdev)
591 struct adv_monitor *monitor;
592 struct msft_data *msft = hdev->msft_data;
598 msft->resuming = true;
601 monitor = idr_get_next(&hdev->adv_monitors_idr, &handle);
605 msft_add_monitor_sync(hdev, monitor);
610 /* All monitors have been reregistered */
611 msft->resuming = false;
614 /* This function requires the caller holds hci_req_sync_lock */
615 int msft_resume_sync(struct hci_dev *hdev)
617 struct msft_data *msft = hdev->msft_data;
619 if (!msft || !msft_monitor_supported(hdev))
624 /* Clear already tracked devices on resume. Once the monitors are
625 * reregistered, devices in range will be found again after resume.
627 hdev->advmon_pend_notify = false;
628 msft_monitor_device_del(hdev, 0, NULL, 0, true);
630 hci_dev_unlock(hdev);
632 reregister_monitor(hdev);
637 /* This function requires the caller holds hci_req_sync_lock */
638 void msft_do_open(struct hci_dev *hdev)
640 struct msft_data *msft = hdev->msft_data;
642 if (hdev->msft_opcode == HCI_OP_NOP)
646 bt_dev_err(hdev, "MSFT extension not registered");
650 bt_dev_dbg(hdev, "Initialize MSFT extension");
652 /* Reset existing MSFT data before re-reading */
653 kfree(msft->evt_prefix);
654 msft->evt_prefix = NULL;
655 msft->evt_prefix_len = 0;
658 if (!read_supported_features(hdev, msft)) {
659 hdev->msft_data = NULL;
664 if (msft_monitor_supported(hdev)) {
665 msft->resuming = true;
666 msft_set_filter_enable(hdev, true);
667 /* Monitors get removed on power off, so we need to explicitly
668 * tell the controller to re-monitor.
670 reregister_monitor(hdev);
674 void msft_do_close(struct hci_dev *hdev)
676 struct msft_data *msft = hdev->msft_data;
677 struct msft_monitor_advertisement_handle_data *handle_data, *tmp;
678 struct msft_monitor_addr_filter_data *address_filter, *n;
679 struct adv_monitor *monitor;
684 bt_dev_dbg(hdev, "Cleanup of MSFT extension");
686 /* The controller will silently remove all monitors on power off.
687 * Therefore, remove handle_data mapping and reset monitor state.
689 list_for_each_entry_safe(handle_data, tmp, &msft->handle_map, list) {
690 monitor = idr_find(&hdev->adv_monitors_idr,
691 handle_data->mgmt_handle);
693 if (monitor && monitor->state == ADV_MONITOR_STATE_OFFLOADED)
694 monitor->state = ADV_MONITOR_STATE_REGISTERED;
696 list_del(&handle_data->list);
700 mutex_lock(&msft->filter_lock);
701 list_for_each_entry_safe(address_filter, n, &msft->address_filters,
703 list_del(&address_filter->list);
704 kfree(address_filter);
706 mutex_unlock(&msft->filter_lock);
710 /* Clear any devices that are being monitored and notify device lost */
711 hdev->advmon_pend_notify = false;
712 msft_monitor_device_del(hdev, 0, NULL, 0, true);
714 hci_dev_unlock(hdev);
717 static int msft_cancel_address_filter_sync(struct hci_dev *hdev, void *data)
719 struct msft_monitor_addr_filter_data *address_filter = data;
720 struct msft_cp_le_cancel_monitor_advertisement cp;
721 struct msft_data *msft = hdev->msft_data;
726 bt_dev_err(hdev, "MSFT: msft data is freed");
730 /* The address filter has been removed by hci dev close */
731 if (!test_bit(HCI_UP, &hdev->flags))
734 mutex_lock(&msft->filter_lock);
735 list_del(&address_filter->list);
736 mutex_unlock(&msft->filter_lock);
738 cp.sub_opcode = MSFT_OP_LE_CANCEL_MONITOR_ADVERTISEMENT;
739 cp.handle = address_filter->msft_handle;
741 skb = __hci_cmd_sync(hdev, hdev->msft_opcode, sizeof(cp), &cp,
743 if (IS_ERR_OR_NULL(skb)) {
744 bt_dev_err(hdev, "MSFT: Failed to cancel address (%pMR) filter",
745 &address_filter->bdaddr);
751 bt_dev_dbg(hdev, "MSFT: Canceled device %pMR address filter",
752 &address_filter->bdaddr);
755 kfree(address_filter);
760 void msft_register(struct hci_dev *hdev)
762 struct msft_data *msft = NULL;
764 bt_dev_dbg(hdev, "Register MSFT extension");
766 msft = kzalloc(sizeof(*msft), GFP_KERNEL);
768 bt_dev_err(hdev, "Failed to register MSFT extension");
772 INIT_LIST_HEAD(&msft->handle_map);
773 INIT_LIST_HEAD(&msft->address_filters);
774 hdev->msft_data = msft;
775 mutex_init(&msft->filter_lock);
778 void msft_unregister(struct hci_dev *hdev)
780 struct msft_data *msft = hdev->msft_data;
785 bt_dev_dbg(hdev, "Unregister MSFT extension");
787 hdev->msft_data = NULL;
789 kfree(msft->evt_prefix);
790 mutex_destroy(&msft->filter_lock);
794 /* This function requires the caller holds hdev->lock */
795 static void msft_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr,
796 __u8 addr_type, __u16 mgmt_handle)
798 struct monitored_device *dev;
800 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
802 bt_dev_err(hdev, "MSFT vendor event %u: no memory",
803 MSFT_EV_LE_MONITOR_DEVICE);
807 bacpy(&dev->bdaddr, bdaddr);
808 dev->addr_type = addr_type;
809 dev->handle = mgmt_handle;
810 dev->notified = false;
812 INIT_LIST_HEAD(&dev->list);
813 list_add(&dev->list, &hdev->monitored_devices);
814 hdev->advmon_pend_notify = true;
817 /* This function requires the caller holds hdev->lock */
818 static void msft_device_lost(struct hci_dev *hdev, bdaddr_t *bdaddr,
819 __u8 addr_type, __u16 mgmt_handle)
821 if (!msft_monitor_device_del(hdev, mgmt_handle, bdaddr, addr_type,
823 bt_dev_err(hdev, "MSFT vendor event %u: dev %pMR not in list",
824 MSFT_EV_LE_MONITOR_DEVICE, bdaddr);
828 static void *msft_skb_pull(struct hci_dev *hdev, struct sk_buff *skb,
833 data = skb_pull_data(skb, len);
835 bt_dev_err(hdev, "Malformed MSFT vendor event: 0x%02x", ev);
840 static int msft_add_address_filter_sync(struct hci_dev *hdev, void *data)
842 struct msft_monitor_addr_filter_data *address_filter = data;
843 struct msft_rp_le_monitor_advertisement *rp;
844 struct msft_cp_le_monitor_advertisement *cp;
845 struct msft_data *msft = hdev->msft_data;
846 struct sk_buff *skb = NULL;
851 bt_dev_err(hdev, "MSFT: msft data is freed");
855 /* The address filter has been removed by hci dev close */
856 if (!test_bit(HCI_UP, &hdev->flags))
859 /* We are safe to use the address filter from now on.
860 * msft_monitor_device_evt() wouldn't delete this filter because it's
861 * not been added by now.
862 * And all other functions that requiring hci_req_sync_lock wouldn't
863 * touch this filter before this func completes because it's protected
864 * by hci_req_sync_lock.
867 if (address_filter->state == AF_STATE_REMOVING) {
868 mutex_lock(&msft->filter_lock);
869 list_del(&address_filter->list);
870 mutex_unlock(&msft->filter_lock);
871 kfree(address_filter);
876 sizeof(address_filter->addr_type) +
877 sizeof(address_filter->bdaddr);
878 cp = kzalloc(size, GFP_KERNEL);
880 bt_dev_err(hdev, "MSFT: Alloc cmd param err");
884 cp->sub_opcode = MSFT_OP_LE_MONITOR_ADVERTISEMENT;
885 cp->rssi_high = address_filter->rssi_high;
886 cp->rssi_low = address_filter->rssi_low;
887 cp->rssi_low_interval = address_filter->rssi_low_interval;
888 cp->rssi_sampling_period = address_filter->rssi_sampling_period;
889 cp->cond_type = MSFT_MONITOR_ADVERTISEMENT_TYPE_ADDR;
890 cp->data[0] = address_filter->addr_type;
891 memcpy(&cp->data[1], &address_filter->bdaddr,
892 sizeof(address_filter->bdaddr));
894 skb = __hci_cmd_sync(hdev, hdev->msft_opcode, size, cp,
896 if (IS_ERR_OR_NULL(skb)) {
897 bt_dev_err(hdev, "Failed to enable address %pMR filter",
898 &address_filter->bdaddr);
904 rp = skb_pull_data(skb, sizeof(*rp));
905 if (!rp || rp->sub_opcode != MSFT_OP_LE_MONITOR_ADVERTISEMENT ||
910 mutex_lock(&msft->filter_lock);
913 bt_dev_warn(hdev, "MSFT: Remove address (%pMR) filter",
914 &address_filter->bdaddr);
915 list_del(&address_filter->list);
916 kfree(address_filter);
918 address_filter->state = AF_STATE_ADDED;
919 address_filter->msft_handle = rp->handle;
920 bt_dev_dbg(hdev, "MSFT: Address %pMR filter enabled",
921 &address_filter->bdaddr);
923 mutex_unlock(&msft->filter_lock);
930 /* This function requires the caller holds msft->filter_lock */
931 static struct msft_monitor_addr_filter_data *msft_add_address_filter
932 (struct hci_dev *hdev, u8 addr_type, bdaddr_t *bdaddr,
933 struct msft_monitor_advertisement_handle_data *handle_data)
935 struct msft_monitor_addr_filter_data *address_filter = NULL;
936 struct msft_data *msft = hdev->msft_data;
939 address_filter = kzalloc(sizeof(*address_filter), GFP_KERNEL);
943 address_filter->state = AF_STATE_ADDING;
944 address_filter->msft_handle = 0xff;
945 address_filter->pattern_handle = handle_data->msft_handle;
946 address_filter->mgmt_handle = handle_data->mgmt_handle;
947 address_filter->rssi_high = handle_data->rssi_high;
948 address_filter->rssi_low = handle_data->rssi_low;
949 address_filter->rssi_low_interval = handle_data->rssi_low_interval;
950 address_filter->rssi_sampling_period = handle_data->rssi_sampling_period;
951 address_filter->addr_type = addr_type;
952 bacpy(&address_filter->bdaddr, bdaddr);
954 /* With the above AF_STATE_ADDING, duplicated address filter can be
955 * avoided when receiving monitor device event (found/lost) frequently
956 * for the same device.
958 list_add_tail(&address_filter->list, &msft->address_filters);
960 err = hci_cmd_sync_queue(hdev, msft_add_address_filter_sync,
961 address_filter, NULL);
963 bt_dev_err(hdev, "MSFT: Add address %pMR filter err", bdaddr);
964 list_del(&address_filter->list);
965 kfree(address_filter);
969 bt_dev_dbg(hdev, "MSFT: Add device %pMR address filter",
970 &address_filter->bdaddr);
972 return address_filter;
975 /* This function requires the caller holds hdev->lock */
976 static void msft_monitor_device_evt(struct hci_dev *hdev, struct sk_buff *skb)
978 struct msft_monitor_addr_filter_data *n, *address_filter = NULL;
979 struct msft_ev_le_monitor_device *ev;
980 struct msft_monitor_advertisement_handle_data *handle_data;
981 struct msft_data *msft = hdev->msft_data;
982 u16 mgmt_handle = 0xffff;
985 ev = msft_skb_pull(hdev, skb, MSFT_EV_LE_MONITOR_DEVICE, sizeof(*ev));
990 "MSFT vendor event 0x%02x: handle 0x%04x state %d addr %pMR",
991 MSFT_EV_LE_MONITOR_DEVICE, ev->monitor_handle,
992 ev->monitor_state, &ev->bdaddr);
994 handle_data = msft_find_handle_data(hdev, ev->monitor_handle, false);
996 if (!test_bit(HCI_QUIRK_USE_MSFT_EXT_ADDRESS_FILTER, &hdev->quirks)) {
999 mgmt_handle = handle_data->mgmt_handle;
1004 /* Don't report any device found/lost event from pattern
1005 * monitors. Pattern monitor always has its address filters for
1009 address_filter = msft_find_address_data(hdev, ev->addr_type,
1011 handle_data->msft_handle);
1015 if (ev->monitor_state && handle_data->cond_type ==
1016 MSFT_MONITOR_ADVERTISEMENT_TYPE_PATTERN)
1017 msft_add_address_filter(hdev, ev->addr_type,
1018 &ev->bdaddr, handle_data);
1023 /* This device event is not from pattern monitor.
1024 * Report it if there is a corresponding address_filter for it.
1026 list_for_each_entry(n, &msft->address_filters, list) {
1027 if (n->state == AF_STATE_ADDED &&
1028 n->msft_handle == ev->monitor_handle) {
1029 mgmt_handle = n->mgmt_handle;
1035 if (!address_filter) {
1036 bt_dev_warn(hdev, "MSFT: Unexpected device event %pMR, %u, %u",
1037 &ev->bdaddr, ev->monitor_handle, ev->monitor_state);
1042 switch (ev->addr_type) {
1043 case ADDR_LE_DEV_PUBLIC:
1044 addr_type = BDADDR_LE_PUBLIC;
1047 case ADDR_LE_DEV_RANDOM:
1048 addr_type = BDADDR_LE_RANDOM;
1053 "MSFT vendor event 0x%02x: unknown addr type 0x%02x",
1054 MSFT_EV_LE_MONITOR_DEVICE, ev->addr_type);
1058 if (ev->monitor_state) {
1059 msft_device_found(hdev, &ev->bdaddr, addr_type, mgmt_handle);
1061 if (address_filter && address_filter->state == AF_STATE_ADDED) {
1062 address_filter->state = AF_STATE_REMOVING;
1063 hci_cmd_sync_queue(hdev,
1064 msft_cancel_address_filter_sync,
1068 msft_device_lost(hdev, &ev->bdaddr, addr_type, mgmt_handle);
1072 void msft_vendor_evt(struct hci_dev *hdev, void *data, struct sk_buff *skb)
1074 struct msft_data *msft = hdev->msft_data;
1081 /* When the extension has defined an event prefix, check that it
1082 * matches, and otherwise just return.
1084 if (msft->evt_prefix_len > 0) {
1085 evt_prefix = msft_skb_pull(hdev, skb, 0, msft->evt_prefix_len);
1089 if (memcmp(evt_prefix, msft->evt_prefix, msft->evt_prefix_len))
1093 /* Every event starts at least with an event code and the rest of
1094 * the data is variable and depends on the event code.
1099 evt = msft_skb_pull(hdev, skb, 0, sizeof(*evt));
1106 case MSFT_EV_LE_MONITOR_DEVICE:
1107 mutex_lock(&msft->filter_lock);
1108 msft_monitor_device_evt(hdev, skb);
1109 mutex_unlock(&msft->filter_lock);
1113 bt_dev_dbg(hdev, "MSFT vendor event 0x%02x", *evt);
1117 hci_dev_unlock(hdev);
1120 __u64 msft_get_features(struct hci_dev *hdev)
1122 struct msft_data *msft = hdev->msft_data;
1124 return msft ? msft->features : 0;
1127 static void msft_le_set_advertisement_filter_enable_cb(struct hci_dev *hdev,
1131 struct msft_cp_le_set_advertisement_filter_enable *cp = user_data;
1132 struct msft_data *msft = hdev->msft_data;
1134 /* Error 0x0C would be returned if the filter enabled status is
1135 * already set to whatever we were trying to set.
1136 * Although the default state should be disabled, some controller set
1137 * the initial value to enabled. Because there is no way to know the
1138 * actual initial value before sending this command, here we also treat
1139 * error 0x0C as success.
1141 if (status != 0x00 && status != 0x0C)
1146 msft->filter_enabled = cp->enable;
1149 bt_dev_warn(hdev, "MSFT filter_enable is already %s",
1150 cp->enable ? "on" : "off");
1152 hci_dev_unlock(hdev);
1155 /* This function requires the caller holds hci_req_sync_lock */
1156 int msft_add_monitor_pattern(struct hci_dev *hdev, struct adv_monitor *monitor)
1158 struct msft_data *msft = hdev->msft_data;
1163 if (msft->resuming || msft->suspending)
1166 return msft_add_monitor_sync(hdev, monitor);
1169 /* This function requires the caller holds hci_req_sync_lock */
1170 int msft_remove_monitor(struct hci_dev *hdev, struct adv_monitor *monitor)
1172 struct msft_data *msft = hdev->msft_data;
1177 if (msft->resuming || msft->suspending)
1180 return msft_remove_monitor_sync(hdev, monitor);
1183 int msft_set_filter_enable(struct hci_dev *hdev, bool enable)
1185 struct msft_cp_le_set_advertisement_filter_enable cp;
1186 struct msft_data *msft = hdev->msft_data;
1192 cp.sub_opcode = MSFT_OP_LE_SET_ADVERTISEMENT_FILTER_ENABLE;
1194 err = __hci_cmd_sync_status(hdev, hdev->msft_opcode, sizeof(cp), &cp,
1197 msft_le_set_advertisement_filter_enable_cb(hdev, &cp, err);
1202 bool msft_curve_validity(struct hci_dev *hdev)
1204 return hdev->msft_curve_validity;