xref: /linux/net/bluetooth/msft.c (revision 4a8f9b7fb4f503f48e2ccfc67caa8b07d303f8a8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2020 Google Corporation
4  */
5 
6 #include <net/bluetooth/bluetooth.h>
7 #include <net/bluetooth/hci_core.h>
8 #include <net/bluetooth/mgmt.h>
9 
10 #include "mgmt_util.h"
11 #include "msft.h"
12 
13 #define MSFT_RSSI_THRESHOLD_VALUE_MIN		-127
14 #define MSFT_RSSI_THRESHOLD_VALUE_MAX		20
15 #define MSFT_RSSI_LOW_TIMEOUT_MAX		0x3C
16 
17 #define MSFT_OP_READ_SUPPORTED_FEATURES		0x00
18 struct msft_cp_read_supported_features {
19 	__u8   sub_opcode;
20 } __packed;
21 
22 struct msft_rp_read_supported_features {
23 	__u8   status;
24 	__u8   sub_opcode;
25 	__le64 features;
26 	__u8   evt_prefix_len;
27 	__u8   evt_prefix[];
28 } __packed;
29 
30 #define MSFT_OP_LE_MONITOR_ADVERTISEMENT	0x03
31 #define MSFT_MONITOR_ADVERTISEMENT_TYPE_PATTERN	0x01
32 struct msft_le_monitor_advertisement_pattern {
33 	__u8 length;
34 	__u8 data_type;
35 	__u8 start_byte;
36 	__u8 pattern[];
37 };
38 
39 struct msft_le_monitor_advertisement_pattern_data {
40 	__u8 count;
41 	__u8 data[];
42 };
43 
44 struct msft_cp_le_monitor_advertisement {
45 	__u8 sub_opcode;
46 	__s8 rssi_high;
47 	__s8 rssi_low;
48 	__u8 rssi_low_interval;
49 	__u8 rssi_sampling_period;
50 	__u8 cond_type;
51 	__u8 data[];
52 } __packed;
53 
54 struct msft_rp_le_monitor_advertisement {
55 	__u8 status;
56 	__u8 sub_opcode;
57 	__u8 handle;
58 } __packed;
59 
60 #define MSFT_OP_LE_CANCEL_MONITOR_ADVERTISEMENT	0x04
61 struct msft_cp_le_cancel_monitor_advertisement {
62 	__u8 sub_opcode;
63 	__u8 handle;
64 } __packed;
65 
66 struct msft_rp_le_cancel_monitor_advertisement {
67 	__u8 status;
68 	__u8 sub_opcode;
69 } __packed;
70 
71 #define MSFT_OP_LE_SET_ADVERTISEMENT_FILTER_ENABLE	0x05
72 struct msft_cp_le_set_advertisement_filter_enable {
73 	__u8 sub_opcode;
74 	__u8 enable;
75 } __packed;
76 
77 struct msft_rp_le_set_advertisement_filter_enable {
78 	__u8 status;
79 	__u8 sub_opcode;
80 } __packed;
81 
82 #define MSFT_EV_LE_MONITOR_DEVICE	0x02
83 struct msft_ev_le_monitor_device {
84 	__u8     addr_type;
85 	bdaddr_t bdaddr;
86 	__u8     monitor_handle;
87 	__u8     monitor_state;
88 } __packed;
89 
90 struct msft_monitor_advertisement_handle_data {
91 	__u8  msft_handle;
92 	__u16 mgmt_handle;
93 	__s8 rssi_high;
94 	__s8 rssi_low;
95 	__u8 rssi_low_interval;
96 	__u8 rssi_sampling_period;
97 	__u8 cond_type;
98 	struct list_head list;
99 };
100 
101 enum monitor_addr_filter_state {
102 	AF_STATE_IDLE,
103 	AF_STATE_ADDING,
104 	AF_STATE_ADDED,
105 	AF_STATE_REMOVING,
106 };
107 
108 #define MSFT_MONITOR_ADVERTISEMENT_TYPE_ADDR	0x04
109 struct msft_monitor_addr_filter_data {
110 	__u8     msft_handle;
111 	__u8     pattern_handle; /* address filters pertain to */
112 	__u16    mgmt_handle;
113 	int      state;
114 	__s8     rssi_high;
115 	__s8     rssi_low;
116 	__u8     rssi_low_interval;
117 	__u8     rssi_sampling_period;
118 	__u8     addr_type;
119 	bdaddr_t bdaddr;
120 	struct list_head list;
121 };
122 
123 struct msft_data {
124 	__u64 features;
125 	__u8  evt_prefix_len;
126 	__u8  *evt_prefix;
127 	struct list_head handle_map;
128 	struct list_head address_filters;
129 	__u8 resuming;
130 	__u8 suspending;
131 	__u8 filter_enabled;
132 	/* To synchronize add/remove address filter and monitor device event.*/
133 	struct mutex filter_lock;
134 };
135 
136 bool msft_monitor_supported(struct hci_dev *hdev)
137 {
138 	return !!(msft_get_features(hdev) & MSFT_FEATURE_MASK_LE_ADV_MONITOR);
139 }
140 
141 static bool read_supported_features(struct hci_dev *hdev,
142 				    struct msft_data *msft)
143 {
144 	struct msft_cp_read_supported_features cp;
145 	struct msft_rp_read_supported_features *rp;
146 	struct sk_buff *skb;
147 
148 	cp.sub_opcode = MSFT_OP_READ_SUPPORTED_FEATURES;
149 
150 	skb = __hci_cmd_sync(hdev, hdev->msft_opcode, sizeof(cp), &cp,
151 			     HCI_CMD_TIMEOUT);
152 	if (IS_ERR(skb)) {
153 		bt_dev_err(hdev, "Failed to read MSFT supported features (%ld)",
154 			   PTR_ERR(skb));
155 		return false;
156 	}
157 
158 	if (skb->len < sizeof(*rp)) {
159 		bt_dev_err(hdev, "MSFT supported features length mismatch");
160 		goto failed;
161 	}
162 
163 	rp = (struct msft_rp_read_supported_features *)skb->data;
164 
165 	if (rp->sub_opcode != MSFT_OP_READ_SUPPORTED_FEATURES)
166 		goto failed;
167 
168 	if (skb->len < sizeof(*rp) + rp->evt_prefix_len) {
169 		bt_dev_err(hdev, "MSFT event prefix length mismatch");
170 		goto failed;
171 	}
172 
173 	/* The event prefix is used by msft_vendor_evt() with hdev->lock held */
174 	hci_dev_lock(hdev);
175 
176 	if (rp->evt_prefix_len > 0) {
177 		msft->evt_prefix = kmemdup(rp->evt_prefix, rp->evt_prefix_len,
178 					   GFP_KERNEL);
179 		if (!msft->evt_prefix) {
180 			hci_dev_unlock(hdev);
181 			goto failed;
182 		}
183 	}
184 
185 	msft->evt_prefix_len = rp->evt_prefix_len;
186 	msft->features = __le64_to_cpu(rp->features);
187 
188 	if (msft->features & MSFT_FEATURE_MASK_CURVE_VALIDITY)
189 		hdev->msft_curve_validity = true;
190 
191 	hci_dev_unlock(hdev);
192 
193 	kfree_skb(skb);
194 	return true;
195 
196 failed:
197 	kfree_skb(skb);
198 	return false;
199 }
200 
201 /* is_mgmt = true matches the handle exposed to userspace via mgmt.
202  * is_mgmt = false matches the handle used by the msft controller.
203  * This function requires the caller holds hdev->lock
204  */
205 static struct msft_monitor_advertisement_handle_data *msft_find_handle_data
206 				(struct hci_dev *hdev, u16 handle, bool is_mgmt)
207 {
208 	struct msft_monitor_advertisement_handle_data *entry;
209 	struct msft_data *msft = hdev->msft_data;
210 
211 	list_for_each_entry(entry, &msft->handle_map, list) {
212 		if (is_mgmt && entry->mgmt_handle == handle)
213 			return entry;
214 		if (!is_mgmt && entry->msft_handle == handle)
215 			return entry;
216 	}
217 
218 	return NULL;
219 }
220 
221 /* This function requires the caller holds msft->filter_lock */
222 static struct msft_monitor_addr_filter_data *msft_find_address_data
223 			(struct hci_dev *hdev, u8 addr_type, bdaddr_t *addr,
224 			 u8 pattern_handle)
225 {
226 	struct msft_monitor_addr_filter_data *entry;
227 	struct msft_data *msft = hdev->msft_data;
228 
229 	list_for_each_entry(entry, &msft->address_filters, list) {
230 		if (entry->pattern_handle == pattern_handle &&
231 		    addr_type == entry->addr_type &&
232 		    !bacmp(addr, &entry->bdaddr))
233 			return entry;
234 	}
235 
236 	return NULL;
237 }
238 
239 /* This function requires the caller holds hdev->lock */
240 static int msft_monitor_device_del(struct hci_dev *hdev, __u16 mgmt_handle,
241 				   bdaddr_t *bdaddr, __u8 addr_type,
242 				   bool notify)
243 {
244 	struct monitored_device *dev, *tmp;
245 	int count = 0;
246 
247 	list_for_each_entry_safe(dev, tmp, &hdev->monitored_devices, list) {
248 		/* mgmt_handle == 0 indicates remove all devices, whereas,
249 		 * bdaddr == NULL indicates remove all devices matching the
250 		 * mgmt_handle.
251 		 */
252 		if ((!mgmt_handle || dev->handle == mgmt_handle) &&
253 		    (!bdaddr || (!bacmp(bdaddr, &dev->bdaddr) &&
254 				 addr_type == dev->addr_type))) {
255 			if (notify && dev->notified) {
256 				mgmt_adv_monitor_device_lost(hdev, dev->handle,
257 							     &dev->bdaddr,
258 							     dev->addr_type);
259 			}
260 
261 			list_del(&dev->list);
262 			kfree(dev);
263 			count++;
264 		}
265 	}
266 
267 	return count;
268 }
269 
270 static int msft_le_monitor_advertisement_cb(struct hci_dev *hdev, u16 opcode,
271 					    struct adv_monitor *monitor,
272 					    struct sk_buff *skb)
273 {
274 	struct msft_rp_le_monitor_advertisement *rp;
275 	struct msft_monitor_advertisement_handle_data *handle_data;
276 	struct msft_data *msft = hdev->msft_data;
277 	int status = 0;
278 
279 	hci_dev_lock(hdev);
280 
281 	rp = (struct msft_rp_le_monitor_advertisement *)skb->data;
282 	if (skb->len < sizeof(*rp)) {
283 		status = HCI_ERROR_UNSPECIFIED;
284 		goto unlock;
285 	}
286 
287 	status = rp->status;
288 	if (status)
289 		goto unlock;
290 
291 	handle_data = kmalloc_obj(*handle_data);
292 	if (!handle_data) {
293 		status = HCI_ERROR_UNSPECIFIED;
294 		goto unlock;
295 	}
296 
297 	handle_data->mgmt_handle = monitor->handle;
298 	handle_data->msft_handle = rp->handle;
299 	handle_data->cond_type   = MSFT_MONITOR_ADVERTISEMENT_TYPE_PATTERN;
300 	handle_data->rssi_high	= monitor->rssi.high_threshold;
301 	handle_data->rssi_low	= monitor->rssi.low_threshold;
302 	handle_data->rssi_low_interval	  = (u8)monitor->rssi.low_threshold_timeout;
303 	handle_data->rssi_sampling_period = monitor->rssi.sampling_period;
304 	INIT_LIST_HEAD(&handle_data->list);
305 	list_add(&handle_data->list, &msft->handle_map);
306 
307 	monitor->state = ADV_MONITOR_STATE_OFFLOADED;
308 
309 unlock:
310 	if (status && msft->resuming)
311 		hci_free_adv_monitor(hdev, monitor);
312 
313 	hci_dev_unlock(hdev);
314 
315 	return status;
316 }
317 
318 /* This function requires the caller holds hci_req_sync_lock */
319 static void msft_remove_addr_filters_sync(struct hci_dev *hdev, u8 handle)
320 {
321 	struct msft_monitor_addr_filter_data *address_filter, *n;
322 	struct msft_cp_le_cancel_monitor_advertisement cp;
323 	struct msft_data *msft = hdev->msft_data;
324 	struct list_head head;
325 	struct sk_buff *skb;
326 
327 	INIT_LIST_HEAD(&head);
328 
329 	/* Cancel all corresponding address monitors */
330 	mutex_lock(&msft->filter_lock);
331 
332 	list_for_each_entry_safe(address_filter, n, &msft->address_filters,
333 				 list) {
334 		if (address_filter->pattern_handle != handle)
335 			continue;
336 
337 		list_del(&address_filter->list);
338 
339 		/* Keep the address filter and let
340 		 * msft_add_address_filter_sync() remove and free the address
341 		 * filter.
342 		 */
343 		if (address_filter->state == AF_STATE_ADDING) {
344 			address_filter->state = AF_STATE_REMOVING;
345 			continue;
346 		}
347 
348 		/* Keep the address filter and let
349 		 * msft_cancel_address_filter_sync() remove and free the address
350 		 * filter
351 		 */
352 		if (address_filter->state == AF_STATE_REMOVING)
353 			continue;
354 
355 		list_add_tail(&address_filter->list, &head);
356 	}
357 
358 	mutex_unlock(&msft->filter_lock);
359 
360 	list_for_each_entry_safe(address_filter, n, &head, list) {
361 		list_del(&address_filter->list);
362 
363 		cp.sub_opcode = MSFT_OP_LE_CANCEL_MONITOR_ADVERTISEMENT;
364 		cp.handle = address_filter->msft_handle;
365 
366 		skb = __hci_cmd_sync(hdev, hdev->msft_opcode, sizeof(cp), &cp,
367 				     HCI_CMD_TIMEOUT);
368 		if (IS_ERR(skb)) {
369 			kfree(address_filter);
370 			continue;
371 		}
372 
373 		kfree_skb(skb);
374 
375 		bt_dev_dbg(hdev, "MSFT: Canceled device %pMR address filter",
376 			   &address_filter->bdaddr);
377 
378 		kfree(address_filter);
379 	}
380 }
381 
382 static int msft_le_cancel_monitor_advertisement_cb(struct hci_dev *hdev,
383 						   u16 opcode,
384 						   struct adv_monitor *monitor,
385 						   struct sk_buff *skb)
386 {
387 	struct msft_rp_le_cancel_monitor_advertisement *rp;
388 	struct msft_monitor_advertisement_handle_data *handle_data;
389 	struct msft_data *msft = hdev->msft_data;
390 	int status = 0;
391 	u8 msft_handle;
392 
393 	rp = (struct msft_rp_le_cancel_monitor_advertisement *)skb->data;
394 	if (skb->len < sizeof(*rp)) {
395 		status = HCI_ERROR_UNSPECIFIED;
396 		goto done;
397 	}
398 
399 	status = rp->status;
400 	if (status)
401 		goto done;
402 
403 	hci_dev_lock(hdev);
404 
405 	handle_data = msft_find_handle_data(hdev, monitor->handle, true);
406 
407 	if (handle_data) {
408 		if (monitor->state == ADV_MONITOR_STATE_OFFLOADED)
409 			monitor->state = ADV_MONITOR_STATE_REGISTERED;
410 
411 		/* Do not free the monitor if it is being removed due to
412 		 * suspend. It will be re-monitored on resume.
413 		 */
414 		if (!msft->suspending) {
415 			hci_free_adv_monitor(hdev, monitor);
416 
417 			/* Clear any monitored devices by this Adv Monitor */
418 			msft_monitor_device_del(hdev, handle_data->mgmt_handle,
419 						NULL, 0, false);
420 		}
421 
422 		msft_handle = handle_data->msft_handle;
423 
424 		list_del(&handle_data->list);
425 		kfree(handle_data);
426 
427 		hci_dev_unlock(hdev);
428 
429 		msft_remove_addr_filters_sync(hdev, msft_handle);
430 	} else {
431 		hci_dev_unlock(hdev);
432 	}
433 
434 done:
435 	return status;
436 }
437 
438 /* This function requires the caller holds hci_req_sync_lock */
439 static int msft_remove_monitor_sync(struct hci_dev *hdev,
440 				    struct adv_monitor *monitor)
441 {
442 	struct msft_cp_le_cancel_monitor_advertisement cp;
443 	struct msft_monitor_advertisement_handle_data *handle_data;
444 	struct sk_buff *skb;
445 	int err;
446 
447 	hci_dev_lock(hdev);
448 	handle_data = msft_find_handle_data(hdev, monitor->handle, true);
449 	if (handle_data)
450 		cp.handle = handle_data->msft_handle;
451 	hci_dev_unlock(hdev);
452 
453 	/* If no matched handle, just remove without telling controller */
454 	if (!handle_data)
455 		return -ENOENT;
456 
457 	cp.sub_opcode = MSFT_OP_LE_CANCEL_MONITOR_ADVERTISEMENT;
458 
459 	skb = __hci_cmd_sync(hdev, hdev->msft_opcode, sizeof(cp), &cp,
460 			     HCI_CMD_TIMEOUT);
461 	if (IS_ERR(skb))
462 		return PTR_ERR(skb);
463 
464 	err = msft_le_cancel_monitor_advertisement_cb(hdev, hdev->msft_opcode,
465 						      monitor, skb);
466 	kfree_skb(skb);
467 
468 	return err;
469 }
470 
471 /* Keep the advertisement filter enabled only while at least one monitor is
472  * offloaded to the controller. With the filter enabled and no monitors, some
473  * controllers (e.g. MediaTek MT7922) drop every advertisement, which breaks
474  * LE discovery and passive scanning entirely.
475  *
476  * This function requires the caller holds hci_req_sync_lock
477  */
478 static void msft_update_filter_enable(struct hci_dev *hdev)
479 {
480 	struct msft_data *msft = hdev->msft_data;
481 	bool enable, update;
482 
483 	if (!msft)
484 		return;
485 
486 	hci_dev_lock(hdev);
487 	enable = !list_empty(&msft->handle_map);
488 	update = enable != msft->filter_enabled;
489 	hci_dev_unlock(hdev);
490 
491 	if (update)
492 		msft_set_filter_enable(hdev, enable);
493 }
494 
495 /* This function requires the caller holds hci_req_sync_lock */
496 int msft_suspend_sync(struct hci_dev *hdev)
497 {
498 	struct msft_data *msft = hdev->msft_data;
499 	struct adv_monitor *monitor;
500 	int handle = 0;
501 
502 	if (!msft || !msft_monitor_supported(hdev))
503 		return 0;
504 
505 	msft->suspending = true;
506 
507 	while (1) {
508 		monitor = idr_get_next(&hdev->adv_monitors_idr, &handle);
509 		if (!monitor)
510 			break;
511 
512 		msft_remove_monitor_sync(hdev, monitor);
513 
514 		handle++;
515 	}
516 
517 	/* All monitors have been removed */
518 	msft->suspending = false;
519 
520 	msft_update_filter_enable(hdev);
521 
522 	return 0;
523 }
524 
525 static bool msft_monitor_rssi_valid(struct adv_monitor *monitor)
526 {
527 	struct adv_rssi_thresholds *r = &monitor->rssi;
528 
529 	if (r->high_threshold < MSFT_RSSI_THRESHOLD_VALUE_MIN ||
530 	    r->high_threshold > MSFT_RSSI_THRESHOLD_VALUE_MAX ||
531 	    r->low_threshold < MSFT_RSSI_THRESHOLD_VALUE_MIN ||
532 	    r->low_threshold > MSFT_RSSI_THRESHOLD_VALUE_MAX)
533 		return false;
534 
535 	/* High_threshold_timeout is not supported,
536 	 * once high_threshold is reached, events are immediately reported.
537 	 */
538 	if (r->high_threshold_timeout != 0)
539 		return false;
540 
541 	if (r->low_threshold_timeout > MSFT_RSSI_LOW_TIMEOUT_MAX)
542 		return false;
543 
544 	/* Sampling period from 0x00 to 0xFF are all allowed */
545 	return true;
546 }
547 
548 static bool msft_monitor_pattern_valid(struct adv_monitor *monitor)
549 {
550 	return msft_monitor_rssi_valid(monitor);
551 	/* No additional check needed for pattern-based monitor */
552 }
553 
554 static int msft_add_monitor_sync(struct hci_dev *hdev,
555 				 struct adv_monitor *monitor)
556 {
557 	struct msft_cp_le_monitor_advertisement *cp;
558 	struct msft_le_monitor_advertisement_pattern_data *pattern_data;
559 	struct msft_le_monitor_advertisement_pattern *pattern;
560 	struct adv_pattern *entry;
561 	size_t total_size = sizeof(*cp) + sizeof(*pattern_data);
562 	ptrdiff_t offset = 0;
563 	u8 pattern_count = 0;
564 	struct sk_buff *skb;
565 	int err;
566 
567 	if (!msft_monitor_pattern_valid(monitor))
568 		return -EINVAL;
569 
570 	list_for_each_entry(entry, &monitor->patterns, list) {
571 		pattern_count++;
572 		total_size += sizeof(*pattern) + entry->length;
573 	}
574 
575 	cp = kmalloc(total_size, GFP_KERNEL);
576 	if (!cp)
577 		return -ENOMEM;
578 
579 	cp->sub_opcode = MSFT_OP_LE_MONITOR_ADVERTISEMENT;
580 	cp->rssi_high = monitor->rssi.high_threshold;
581 	cp->rssi_low = monitor->rssi.low_threshold;
582 	cp->rssi_low_interval = (u8)monitor->rssi.low_threshold_timeout;
583 	cp->rssi_sampling_period = monitor->rssi.sampling_period;
584 
585 	cp->cond_type = MSFT_MONITOR_ADVERTISEMENT_TYPE_PATTERN;
586 
587 	pattern_data = (void *)cp->data;
588 	pattern_data->count = pattern_count;
589 
590 	list_for_each_entry(entry, &monitor->patterns, list) {
591 		pattern = (void *)(pattern_data->data + offset);
592 		/* the length also includes data_type and offset */
593 		pattern->length = entry->length + 2;
594 		pattern->data_type = entry->ad_type;
595 		pattern->start_byte = entry->offset;
596 		memcpy(pattern->pattern, entry->value, entry->length);
597 		offset += sizeof(*pattern) + entry->length;
598 	}
599 
600 	skb = __hci_cmd_sync(hdev, hdev->msft_opcode, total_size, cp,
601 			     HCI_CMD_TIMEOUT);
602 
603 	if (IS_ERR(skb)) {
604 		err = PTR_ERR(skb);
605 		goto out_free;
606 	}
607 
608 	err = msft_le_monitor_advertisement_cb(hdev, hdev->msft_opcode,
609 					       monitor, skb);
610 	kfree_skb(skb);
611 
612 out_free:
613 	kfree(cp);
614 	return err;
615 }
616 
617 /* This function requires the caller holds hci_req_sync_lock */
618 static void reregister_monitor(struct hci_dev *hdev)
619 {
620 	struct adv_monitor *monitor;
621 	struct msft_data *msft = hdev->msft_data;
622 	int handle = 0;
623 
624 	if (!msft)
625 		return;
626 
627 	msft->resuming = true;
628 
629 	while (1) {
630 		monitor = idr_get_next(&hdev->adv_monitors_idr, &handle);
631 		if (!monitor)
632 			break;
633 
634 		msft_add_monitor_sync(hdev, monitor);
635 
636 		handle++;
637 	}
638 
639 	/* All monitors have been reregistered */
640 	msft->resuming = false;
641 }
642 
643 /* This function requires the caller holds hci_req_sync_lock */
644 int msft_resume_sync(struct hci_dev *hdev)
645 {
646 	struct msft_data *msft = hdev->msft_data;
647 
648 	if (!msft || !msft_monitor_supported(hdev))
649 		return 0;
650 
651 	hci_dev_lock(hdev);
652 
653 	/* Clear already tracked devices on resume. Once the monitors are
654 	 * reregistered, devices in range will be found again after resume.
655 	 */
656 	hdev->advmon_pend_notify = false;
657 	msft_monitor_device_del(hdev, 0, NULL, 0, true);
658 
659 	hci_dev_unlock(hdev);
660 
661 	reregister_monitor(hdev);
662 	msft_update_filter_enable(hdev);
663 
664 	return 0;
665 }
666 
667 /* This function requires the caller holds hci_req_sync_lock */
668 void msft_do_open(struct hci_dev *hdev)
669 {
670 	struct msft_data *msft = hdev->msft_data;
671 
672 	if (hdev->msft_opcode == HCI_OP_NOP)
673 		return;
674 
675 	if (!msft) {
676 		bt_dev_err(hdev, "MSFT extension not registered");
677 		return;
678 	}
679 
680 	bt_dev_dbg(hdev, "Initialize MSFT extension");
681 
682 	/* Reset existing MSFT data before re-reading */
683 	hci_dev_lock(hdev);
684 	kfree(msft->evt_prefix);
685 	msft->evt_prefix = NULL;
686 	msft->evt_prefix_len = 0;
687 	msft->features = 0;
688 	hci_dev_unlock(hdev);
689 
690 	if (!read_supported_features(hdev, msft)) {
691 		hci_dev_lock(hdev);
692 		hdev->msft_data = NULL;
693 		kfree(msft);
694 		hci_dev_unlock(hdev);
695 		return;
696 	}
697 
698 	if (msft_monitor_supported(hdev)) {
699 		msft->resuming = true;
700 		/* Monitors get removed on power off, so we need to explicitly
701 		 * tell the controller to re-monitor.
702 		 */
703 		reregister_monitor(hdev);
704 		msft_update_filter_enable(hdev);
705 	}
706 }
707 
708 void msft_do_close(struct hci_dev *hdev)
709 {
710 	struct msft_data *msft = hdev->msft_data;
711 	struct msft_monitor_advertisement_handle_data *handle_data, *tmp;
712 	struct msft_monitor_addr_filter_data *address_filter, *n;
713 	struct adv_monitor *monitor;
714 
715 	if (!msft)
716 		return;
717 
718 	bt_dev_dbg(hdev, "Cleanup of MSFT extension");
719 
720 	hci_dev_lock(hdev);
721 
722 	/* The controller will silently remove all monitors on power off.
723 	 * Therefore, remove handle_data mapping and reset monitor state.
724 	 */
725 	list_for_each_entry_safe(handle_data, tmp, &msft->handle_map, list) {
726 		monitor = idr_find(&hdev->adv_monitors_idr,
727 				   handle_data->mgmt_handle);
728 
729 		if (monitor && monitor->state == ADV_MONITOR_STATE_OFFLOADED)
730 			monitor->state = ADV_MONITOR_STATE_REGISTERED;
731 
732 		list_del(&handle_data->list);
733 		kfree(handle_data);
734 	}
735 
736 	/* The advertisement filter is back to its default, disabled state
737 	 * after power off as well.
738 	 */
739 	msft->filter_enabled = false;
740 
741 	hci_dev_unlock(hdev);
742 
743 	mutex_lock(&msft->filter_lock);
744 	list_for_each_entry_safe(address_filter, n, &msft->address_filters,
745 				 list) {
746 		list_del(&address_filter->list);
747 		kfree(address_filter);
748 	}
749 	mutex_unlock(&msft->filter_lock);
750 
751 	hci_dev_lock(hdev);
752 
753 	/* Clear any devices that are being monitored and notify device lost */
754 	hdev->advmon_pend_notify = false;
755 	msft_monitor_device_del(hdev, 0, NULL, 0, true);
756 
757 	hci_dev_unlock(hdev);
758 }
759 
760 static int msft_cancel_address_filter_sync(struct hci_dev *hdev, void *data)
761 {
762 	struct msft_monitor_addr_filter_data *address_filter = data;
763 	struct msft_cp_le_cancel_monitor_advertisement cp;
764 	struct msft_data *msft = hdev->msft_data;
765 	struct sk_buff *skb;
766 	int err = 0;
767 
768 	if (!msft) {
769 		bt_dev_err(hdev, "MSFT: msft data is freed");
770 		return -EINVAL;
771 	}
772 
773 	/* The address filter has been removed by hci dev close */
774 	if (!test_bit(HCI_UP, &hdev->flags))
775 		return 0;
776 
777 	mutex_lock(&msft->filter_lock);
778 	list_del(&address_filter->list);
779 	mutex_unlock(&msft->filter_lock);
780 
781 	cp.sub_opcode = MSFT_OP_LE_CANCEL_MONITOR_ADVERTISEMENT;
782 	cp.handle = address_filter->msft_handle;
783 
784 	skb = __hci_cmd_sync(hdev, hdev->msft_opcode, sizeof(cp), &cp,
785 			     HCI_CMD_TIMEOUT);
786 	if (IS_ERR(skb)) {
787 		bt_dev_err(hdev, "MSFT: Failed to cancel address (%pMR) filter",
788 			   &address_filter->bdaddr);
789 		err = PTR_ERR(skb);
790 		goto done;
791 	}
792 	kfree_skb(skb);
793 
794 	bt_dev_dbg(hdev, "MSFT: Canceled device %pMR address filter",
795 		   &address_filter->bdaddr);
796 
797 done:
798 	kfree(address_filter);
799 
800 	return err;
801 }
802 
803 void msft_register(struct hci_dev *hdev)
804 {
805 	struct msft_data *msft = NULL;
806 
807 	bt_dev_dbg(hdev, "Register MSFT extension");
808 
809 	msft = kzalloc_obj(*msft);
810 	if (!msft) {
811 		bt_dev_err(hdev, "Failed to register MSFT extension");
812 		return;
813 	}
814 
815 	INIT_LIST_HEAD(&msft->handle_map);
816 	INIT_LIST_HEAD(&msft->address_filters);
817 	mutex_init(&msft->filter_lock);
818 	hdev->msft_data = msft;
819 }
820 
821 void msft_release(struct hci_dev *hdev)
822 {
823 	struct msft_data *msft = hdev->msft_data;
824 
825 	if (!msft)
826 		return;
827 
828 	bt_dev_dbg(hdev, "Unregister MSFT extension");
829 
830 	hdev->msft_data = NULL;
831 
832 	kfree(msft->evt_prefix);
833 	mutex_destroy(&msft->filter_lock);
834 	kfree(msft);
835 }
836 
837 /* This function requires the caller holds hdev->lock */
838 static void msft_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr,
839 			      __u8 addr_type, __u16 mgmt_handle)
840 {
841 	struct monitored_device *dev;
842 
843 	dev = kmalloc_obj(*dev);
844 	if (!dev) {
845 		bt_dev_err(hdev, "MSFT vendor event %u: no memory",
846 			   MSFT_EV_LE_MONITOR_DEVICE);
847 		return;
848 	}
849 
850 	bacpy(&dev->bdaddr, bdaddr);
851 	dev->addr_type = addr_type;
852 	dev->handle = mgmt_handle;
853 	dev->notified = false;
854 
855 	INIT_LIST_HEAD(&dev->list);
856 	list_add(&dev->list, &hdev->monitored_devices);
857 	hdev->advmon_pend_notify = true;
858 }
859 
860 /* This function requires the caller holds hdev->lock */
861 static void msft_device_lost(struct hci_dev *hdev, bdaddr_t *bdaddr,
862 			     __u8 addr_type, __u16 mgmt_handle)
863 {
864 	if (!msft_monitor_device_del(hdev, mgmt_handle, bdaddr, addr_type,
865 				     true)) {
866 		bt_dev_err(hdev, "MSFT vendor event %u: dev %pMR not in list",
867 			   MSFT_EV_LE_MONITOR_DEVICE, bdaddr);
868 	}
869 }
870 
871 static void *msft_skb_pull(struct hci_dev *hdev, struct sk_buff *skb,
872 			   u8 ev, size_t len)
873 {
874 	void *data;
875 
876 	data = skb_pull_data(skb, len);
877 	if (!data)
878 		bt_dev_err(hdev, "Malformed MSFT vendor event: 0x%02x", ev);
879 
880 	return data;
881 }
882 
883 static int msft_add_address_filter_sync(struct hci_dev *hdev, void *data)
884 {
885 	struct msft_monitor_addr_filter_data *address_filter = data;
886 	struct msft_rp_le_monitor_advertisement *rp;
887 	struct msft_cp_le_monitor_advertisement *cp;
888 	struct msft_data *msft = hdev->msft_data;
889 	struct sk_buff *skb = NULL;
890 	bool remove = false;
891 	size_t size;
892 
893 	if (!msft) {
894 		bt_dev_err(hdev, "MSFT: msft data is freed");
895 		return -EINVAL;
896 	}
897 
898 	/* The address filter has been removed by hci dev close */
899 	if (!test_bit(HCI_UP, &hdev->flags))
900 		return -ENODEV;
901 
902 	/* We are safe to use the address filter from now on.
903 	 * msft_monitor_device_evt() wouldn't delete this filter because it's
904 	 * not been added by now.
905 	 * And all other functions that requiring hci_req_sync_lock wouldn't
906 	 * touch this filter before this func completes because it's protected
907 	 * by hci_req_sync_lock.
908 	 */
909 
910 	if (address_filter->state == AF_STATE_REMOVING) {
911 		mutex_lock(&msft->filter_lock);
912 		list_del(&address_filter->list);
913 		mutex_unlock(&msft->filter_lock);
914 		kfree(address_filter);
915 		return 0;
916 	}
917 
918 	size = sizeof(*cp) +
919 	       sizeof(address_filter->addr_type) +
920 	       sizeof(address_filter->bdaddr);
921 	cp = kzalloc(size, GFP_KERNEL);
922 	if (!cp) {
923 		bt_dev_err(hdev, "MSFT: Alloc cmd param err");
924 		remove = true;
925 		goto done;
926 	}
927 
928 	cp->sub_opcode           = MSFT_OP_LE_MONITOR_ADVERTISEMENT;
929 	cp->rssi_high		 = address_filter->rssi_high;
930 	cp->rssi_low		 = address_filter->rssi_low;
931 	cp->rssi_low_interval    = address_filter->rssi_low_interval;
932 	cp->rssi_sampling_period = address_filter->rssi_sampling_period;
933 	cp->cond_type            = MSFT_MONITOR_ADVERTISEMENT_TYPE_ADDR;
934 	cp->data[0]              = address_filter->addr_type;
935 	memcpy(&cp->data[1], &address_filter->bdaddr,
936 	       sizeof(address_filter->bdaddr));
937 
938 	skb = __hci_cmd_sync(hdev, hdev->msft_opcode, size, cp,
939 			     HCI_CMD_TIMEOUT);
940 	kfree(cp);
941 
942 	if (IS_ERR(skb)) {
943 		bt_dev_err(hdev, "Failed to enable address %pMR filter",
944 			   &address_filter->bdaddr);
945 		skb = NULL;
946 		remove = true;
947 		goto done;
948 	}
949 
950 	rp = skb_pull_data(skb, sizeof(*rp));
951 	if (!rp || rp->sub_opcode != MSFT_OP_LE_MONITOR_ADVERTISEMENT ||
952 	    rp->status)
953 		remove = true;
954 
955 done:
956 	mutex_lock(&msft->filter_lock);
957 
958 	if (remove) {
959 		bt_dev_warn(hdev, "MSFT: Remove address (%pMR) filter",
960 			    &address_filter->bdaddr);
961 		list_del(&address_filter->list);
962 		kfree(address_filter);
963 	} else {
964 		address_filter->state = AF_STATE_ADDED;
965 		address_filter->msft_handle = rp->handle;
966 		bt_dev_dbg(hdev, "MSFT: Address %pMR filter enabled",
967 			   &address_filter->bdaddr);
968 	}
969 	mutex_unlock(&msft->filter_lock);
970 
971 	kfree_skb(skb);
972 
973 	return 0;
974 }
975 
976 /* This function requires the caller holds msft->filter_lock */
977 static struct msft_monitor_addr_filter_data *msft_add_address_filter
978 		(struct hci_dev *hdev, u8 addr_type, bdaddr_t *bdaddr,
979 		 struct msft_monitor_advertisement_handle_data *handle_data)
980 {
981 	struct msft_monitor_addr_filter_data *address_filter = NULL;
982 	struct msft_data *msft = hdev->msft_data;
983 	int err;
984 
985 	address_filter = kzalloc_obj(*address_filter);
986 	if (!address_filter)
987 		return NULL;
988 
989 	address_filter->state             = AF_STATE_ADDING;
990 	address_filter->msft_handle       = 0xff;
991 	address_filter->pattern_handle    = handle_data->msft_handle;
992 	address_filter->mgmt_handle       = handle_data->mgmt_handle;
993 	address_filter->rssi_high         = handle_data->rssi_high;
994 	address_filter->rssi_low          = handle_data->rssi_low;
995 	address_filter->rssi_low_interval = handle_data->rssi_low_interval;
996 	address_filter->rssi_sampling_period = handle_data->rssi_sampling_period;
997 	address_filter->addr_type            = addr_type;
998 	bacpy(&address_filter->bdaddr, bdaddr);
999 
1000 	/* With the above AF_STATE_ADDING, duplicated address filter can be
1001 	 * avoided when receiving monitor device event (found/lost) frequently
1002 	 * for the same device.
1003 	 */
1004 	list_add_tail(&address_filter->list, &msft->address_filters);
1005 
1006 	err = hci_cmd_sync_queue(hdev, msft_add_address_filter_sync,
1007 				 address_filter, NULL);
1008 	if (err < 0) {
1009 		bt_dev_err(hdev, "MSFT: Add address %pMR filter err", bdaddr);
1010 		list_del(&address_filter->list);
1011 		kfree(address_filter);
1012 		return NULL;
1013 	}
1014 
1015 	bt_dev_dbg(hdev, "MSFT: Add device %pMR address filter",
1016 		   &address_filter->bdaddr);
1017 
1018 	return address_filter;
1019 }
1020 
1021 /* This function requires the caller holds hdev->lock */
1022 static void msft_monitor_device_evt(struct hci_dev *hdev, struct sk_buff *skb)
1023 {
1024 	struct msft_monitor_addr_filter_data *n, *address_filter = NULL;
1025 	struct msft_ev_le_monitor_device *ev;
1026 	struct msft_monitor_advertisement_handle_data *handle_data;
1027 	struct msft_data *msft = hdev->msft_data;
1028 	u16 mgmt_handle = 0xffff;
1029 	u8 addr_type;
1030 
1031 	ev = msft_skb_pull(hdev, skb, MSFT_EV_LE_MONITOR_DEVICE, sizeof(*ev));
1032 	if (!ev)
1033 		return;
1034 
1035 	bt_dev_dbg(hdev,
1036 		   "MSFT vendor event 0x%02x: handle 0x%04x state %d addr %pMR",
1037 		   MSFT_EV_LE_MONITOR_DEVICE, ev->monitor_handle,
1038 		   ev->monitor_state, &ev->bdaddr);
1039 
1040 	handle_data = msft_find_handle_data(hdev, ev->monitor_handle, false);
1041 
1042 	if (!hci_test_quirk(hdev, HCI_QUIRK_USE_MSFT_EXT_ADDRESS_FILTER)) {
1043 		if (!handle_data)
1044 			return;
1045 		mgmt_handle = handle_data->mgmt_handle;
1046 		goto report_state;
1047 	}
1048 
1049 	if (handle_data) {
1050 		/* Don't report any device found/lost event from pattern
1051 		 * monitors. Pattern monitor always has its address filters for
1052 		 * tracking devices.
1053 		 */
1054 
1055 		address_filter = msft_find_address_data(hdev, ev->addr_type,
1056 							&ev->bdaddr,
1057 							handle_data->msft_handle);
1058 		if (address_filter)
1059 			return;
1060 
1061 		if (ev->monitor_state && handle_data->cond_type ==
1062 				MSFT_MONITOR_ADVERTISEMENT_TYPE_PATTERN)
1063 			msft_add_address_filter(hdev, ev->addr_type,
1064 						&ev->bdaddr, handle_data);
1065 
1066 		return;
1067 	}
1068 
1069 	/* This device event is not from pattern monitor.
1070 	 * Report it if there is a corresponding address_filter for it.
1071 	 */
1072 	list_for_each_entry(n, &msft->address_filters, list) {
1073 		if (n->state == AF_STATE_ADDED &&
1074 		    n->msft_handle == ev->monitor_handle) {
1075 			mgmt_handle = n->mgmt_handle;
1076 			address_filter = n;
1077 			break;
1078 		}
1079 	}
1080 
1081 	if (!address_filter) {
1082 		bt_dev_warn(hdev, "MSFT: Unexpected device event %pMR, %u, %u",
1083 			    &ev->bdaddr, ev->monitor_handle, ev->monitor_state);
1084 		return;
1085 	}
1086 
1087 report_state:
1088 	switch (ev->addr_type) {
1089 	case ADDR_LE_DEV_PUBLIC:
1090 		addr_type = BDADDR_LE_PUBLIC;
1091 		break;
1092 
1093 	case ADDR_LE_DEV_RANDOM:
1094 		addr_type = BDADDR_LE_RANDOM;
1095 		break;
1096 
1097 	default:
1098 		bt_dev_err(hdev,
1099 			   "MSFT vendor event 0x%02x: unknown addr type 0x%02x",
1100 			   MSFT_EV_LE_MONITOR_DEVICE, ev->addr_type);
1101 		return;
1102 	}
1103 
1104 	if (ev->monitor_state) {
1105 		msft_device_found(hdev, &ev->bdaddr, addr_type, mgmt_handle);
1106 	} else {
1107 		if (address_filter && address_filter->state == AF_STATE_ADDED) {
1108 			address_filter->state = AF_STATE_REMOVING;
1109 			hci_cmd_sync_queue(hdev,
1110 					   msft_cancel_address_filter_sync,
1111 					   address_filter,
1112 					   NULL);
1113 		}
1114 		msft_device_lost(hdev, &ev->bdaddr, addr_type, mgmt_handle);
1115 	}
1116 }
1117 
1118 void msft_vendor_evt(struct hci_dev *hdev, void *data, struct sk_buff *skb)
1119 {
1120 	struct msft_data *msft;
1121 	u8 *evt_prefix;
1122 	u8 *evt;
1123 
1124 	hci_dev_lock(hdev);
1125 
1126 	msft = hdev->msft_data;
1127 	if (!msft)
1128 		goto unlock;
1129 
1130 	/* When the extension has defined an event prefix, check that it
1131 	 * matches, and otherwise just return.
1132 	 */
1133 	if (msft->evt_prefix_len > 0) {
1134 		evt_prefix = msft_skb_pull(hdev, skb, 0, msft->evt_prefix_len);
1135 		if (!evt_prefix)
1136 			goto unlock;
1137 
1138 		if (memcmp(evt_prefix, msft->evt_prefix, msft->evt_prefix_len))
1139 			goto unlock;
1140 	}
1141 
1142 	/* Every event starts at least with an event code and the rest of
1143 	 * the data is variable and depends on the event code.
1144 	 */
1145 	if (skb->len < 1)
1146 		goto unlock;
1147 
1148 	evt = msft_skb_pull(hdev, skb, 0, sizeof(*evt));
1149 	if (!evt)
1150 		goto unlock;
1151 
1152 	switch (*evt) {
1153 	case MSFT_EV_LE_MONITOR_DEVICE:
1154 		mutex_lock(&msft->filter_lock);
1155 		msft_monitor_device_evt(hdev, skb);
1156 		mutex_unlock(&msft->filter_lock);
1157 		break;
1158 
1159 	default:
1160 		bt_dev_dbg(hdev, "MSFT vendor event 0x%02x", *evt);
1161 		break;
1162 	}
1163 
1164 unlock:
1165 	hci_dev_unlock(hdev);
1166 }
1167 
1168 __u64 msft_get_features(struct hci_dev *hdev)
1169 {
1170 	struct msft_data *msft = hdev->msft_data;
1171 
1172 	return msft ? msft->features : 0;
1173 }
1174 
1175 static void msft_le_set_advertisement_filter_enable_cb(struct hci_dev *hdev,
1176 						       void *user_data,
1177 						       int err)
1178 {
1179 	struct msft_cp_le_set_advertisement_filter_enable *cp = user_data;
1180 	struct msft_data *msft = hdev->msft_data;
1181 
1182 	/* Error 0x0C (Command Disallowed), which __hci_cmd_sync_status()
1183 	 * reports as -EBUSY, would be returned if the filter enabled status is
1184 	 * already set to whatever we were trying to set.
1185 	 * Although the default state should be disabled, some controller set
1186 	 * the initial value to enabled. Because there is no way to know the
1187 	 * actual initial value before sending this command, here we also treat
1188 	 * error 0x0C as success.
1189 	 */
1190 	if (err && err != -EBUSY)
1191 		return;
1192 
1193 	hci_dev_lock(hdev);
1194 
1195 	msft->filter_enabled = cp->enable;
1196 
1197 	if (err == -EBUSY)
1198 		bt_dev_warn(hdev, "MSFT filter_enable is already %s",
1199 			    cp->enable ? "on" : "off");
1200 
1201 	hci_dev_unlock(hdev);
1202 }
1203 
1204 /* This function requires the caller holds hci_req_sync_lock */
1205 int msft_add_monitor_pattern(struct hci_dev *hdev, struct adv_monitor *monitor)
1206 {
1207 	struct msft_data *msft = hdev->msft_data;
1208 	int err;
1209 
1210 	if (!msft)
1211 		return -EOPNOTSUPP;
1212 
1213 	if (msft->resuming || msft->suspending)
1214 		return -EBUSY;
1215 
1216 	err = msft_add_monitor_sync(hdev, monitor);
1217 	msft_update_filter_enable(hdev);
1218 
1219 	return err;
1220 }
1221 
1222 /* This function requires the caller holds hci_req_sync_lock */
1223 int msft_remove_monitor(struct hci_dev *hdev, struct adv_monitor *monitor)
1224 {
1225 	struct msft_data *msft = hdev->msft_data;
1226 	int err;
1227 
1228 	if (!msft)
1229 		return -EOPNOTSUPP;
1230 
1231 	if (msft->resuming || msft->suspending)
1232 		return -EBUSY;
1233 
1234 	err = msft_remove_monitor_sync(hdev, monitor);
1235 	msft_update_filter_enable(hdev);
1236 
1237 	return err;
1238 }
1239 
1240 int msft_set_filter_enable(struct hci_dev *hdev, bool enable)
1241 {
1242 	struct msft_cp_le_set_advertisement_filter_enable cp;
1243 	struct msft_data *msft = hdev->msft_data;
1244 	int err;
1245 
1246 	if (!msft)
1247 		return -EOPNOTSUPP;
1248 
1249 	cp.sub_opcode = MSFT_OP_LE_SET_ADVERTISEMENT_FILTER_ENABLE;
1250 	cp.enable = enable;
1251 	err = __hci_cmd_sync_status(hdev, hdev->msft_opcode, sizeof(cp), &cp,
1252 				    HCI_CMD_TIMEOUT);
1253 
1254 	msft_le_set_advertisement_filter_enable_cb(hdev, &cp, err);
1255 
1256 	return err == -EBUSY ? 0 : err;
1257 }
1258 
1259 bool msft_curve_validity(struct hci_dev *hdev)
1260 {
1261 	return hdev->msft_curve_validity;
1262 }
1263