msft.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2020 Google Corporation
  4. */
  5. #include <net/bluetooth/bluetooth.h>
  6. #include <net/bluetooth/hci_core.h>
  7. #include <net/bluetooth/mgmt.h>
  8. #include "hci_request.h"
  9. #include "mgmt_util.h"
  10. #include "msft.h"
  11. #define MSFT_RSSI_THRESHOLD_VALUE_MIN -127
  12. #define MSFT_RSSI_THRESHOLD_VALUE_MAX 20
  13. #define MSFT_RSSI_LOW_TIMEOUT_MAX 0x3C
  14. #define MSFT_OP_READ_SUPPORTED_FEATURES 0x00
  15. struct msft_cp_read_supported_features {
  16. __u8 sub_opcode;
  17. } __packed;
  18. struct msft_rp_read_supported_features {
  19. __u8 status;
  20. __u8 sub_opcode;
  21. __le64 features;
  22. __u8 evt_prefix_len;
  23. __u8 evt_prefix[];
  24. } __packed;
  25. #define MSFT_OP_LE_MONITOR_ADVERTISEMENT 0x03
  26. #define MSFT_MONITOR_ADVERTISEMENT_TYPE_PATTERN 0x01
  27. struct msft_le_monitor_advertisement_pattern {
  28. __u8 length;
  29. __u8 data_type;
  30. __u8 start_byte;
  31. __u8 pattern[];
  32. };
  33. struct msft_le_monitor_advertisement_pattern_data {
  34. __u8 count;
  35. __u8 data[];
  36. };
  37. struct msft_cp_le_monitor_advertisement {
  38. __u8 sub_opcode;
  39. __s8 rssi_high;
  40. __s8 rssi_low;
  41. __u8 rssi_low_interval;
  42. __u8 rssi_sampling_period;
  43. __u8 cond_type;
  44. __u8 data[];
  45. } __packed;
  46. struct msft_rp_le_monitor_advertisement {
  47. __u8 status;
  48. __u8 sub_opcode;
  49. __u8 handle;
  50. } __packed;
  51. #define MSFT_OP_LE_CANCEL_MONITOR_ADVERTISEMENT 0x04
  52. struct msft_cp_le_cancel_monitor_advertisement {
  53. __u8 sub_opcode;
  54. __u8 handle;
  55. } __packed;
  56. struct msft_rp_le_cancel_monitor_advertisement {
  57. __u8 status;
  58. __u8 sub_opcode;
  59. } __packed;
  60. #define MSFT_OP_LE_SET_ADVERTISEMENT_FILTER_ENABLE 0x05
  61. struct msft_cp_le_set_advertisement_filter_enable {
  62. __u8 sub_opcode;
  63. __u8 enable;
  64. } __packed;
  65. struct msft_rp_le_set_advertisement_filter_enable {
  66. __u8 status;
  67. __u8 sub_opcode;
  68. } __packed;
  69. #define MSFT_EV_LE_MONITOR_DEVICE 0x02
  70. struct msft_ev_le_monitor_device {
  71. __u8 addr_type;
  72. bdaddr_t bdaddr;
  73. __u8 monitor_handle;
  74. __u8 monitor_state;
  75. } __packed;
  76. struct msft_monitor_advertisement_handle_data {
  77. __u8 msft_handle;
  78. __u16 mgmt_handle;
  79. struct list_head list;
  80. };
  81. struct msft_data {
  82. __u64 features;
  83. __u8 evt_prefix_len;
  84. __u8 *evt_prefix;
  85. struct list_head handle_map;
  86. __u8 resuming;
  87. __u8 suspending;
  88. __u8 filter_enabled;
  89. };
  90. bool msft_monitor_supported(struct hci_dev *hdev)
  91. {
  92. return !!(msft_get_features(hdev) & MSFT_FEATURE_MASK_LE_ADV_MONITOR);
  93. }
  94. static bool read_supported_features(struct hci_dev *hdev,
  95. struct msft_data *msft)
  96. {
  97. struct msft_cp_read_supported_features cp;
  98. struct msft_rp_read_supported_features *rp;
  99. struct sk_buff *skb;
  100. cp.sub_opcode = MSFT_OP_READ_SUPPORTED_FEATURES;
  101. skb = __hci_cmd_sync(hdev, hdev->msft_opcode, sizeof(cp), &cp,
  102. HCI_CMD_TIMEOUT);
  103. if (IS_ERR_OR_NULL(skb)) {
  104. if (!skb)
  105. skb = ERR_PTR(-EIO);
  106. bt_dev_err(hdev, "Failed to read MSFT supported features (%ld)",
  107. PTR_ERR(skb));
  108. return false;
  109. }
  110. if (skb->len < sizeof(*rp)) {
  111. bt_dev_err(hdev, "MSFT supported features length mismatch");
  112. goto failed;
  113. }
  114. rp = (struct msft_rp_read_supported_features *)skb->data;
  115. if (rp->sub_opcode != MSFT_OP_READ_SUPPORTED_FEATURES)
  116. goto failed;
  117. if (rp->evt_prefix_len > 0) {
  118. msft->evt_prefix = kmemdup(rp->evt_prefix, rp->evt_prefix_len,
  119. GFP_KERNEL);
  120. if (!msft->evt_prefix)
  121. goto failed;
  122. }
  123. msft->evt_prefix_len = rp->evt_prefix_len;
  124. msft->features = __le64_to_cpu(rp->features);
  125. if (msft->features & MSFT_FEATURE_MASK_CURVE_VALIDITY)
  126. hdev->msft_curve_validity = true;
  127. kfree_skb(skb);
  128. return true;
  129. failed:
  130. kfree_skb(skb);
  131. return false;
  132. }
  133. /* is_mgmt = true matches the handle exposed to userspace via mgmt.
  134. * is_mgmt = false matches the handle used by the msft controller.
  135. * This function requires the caller holds hdev->lock
  136. */
  137. static struct msft_monitor_advertisement_handle_data *msft_find_handle_data
  138. (struct hci_dev *hdev, u16 handle, bool is_mgmt)
  139. {
  140. struct msft_monitor_advertisement_handle_data *entry;
  141. struct msft_data *msft = hdev->msft_data;
  142. list_for_each_entry(entry, &msft->handle_map, list) {
  143. if (is_mgmt && entry->mgmt_handle == handle)
  144. return entry;
  145. if (!is_mgmt && entry->msft_handle == handle)
  146. return entry;
  147. }
  148. return NULL;
  149. }
  150. /* This function requires the caller holds hdev->lock */
  151. static int msft_monitor_device_del(struct hci_dev *hdev, __u16 mgmt_handle,
  152. bdaddr_t *bdaddr, __u8 addr_type,
  153. bool notify)
  154. {
  155. struct monitored_device *dev, *tmp;
  156. int count = 0;
  157. list_for_each_entry_safe(dev, tmp, &hdev->monitored_devices, list) {
  158. /* mgmt_handle == 0 indicates remove all devices, whereas,
  159. * bdaddr == NULL indicates remove all devices matching the
  160. * mgmt_handle.
  161. */
  162. if ((!mgmt_handle || dev->handle == mgmt_handle) &&
  163. (!bdaddr || (!bacmp(bdaddr, &dev->bdaddr) &&
  164. addr_type == dev->addr_type))) {
  165. if (notify && dev->notified) {
  166. mgmt_adv_monitor_device_lost(hdev, dev->handle,
  167. &dev->bdaddr,
  168. dev->addr_type);
  169. }
  170. list_del(&dev->list);
  171. kfree(dev);
  172. count++;
  173. }
  174. }
  175. return count;
  176. }
  177. static int msft_le_monitor_advertisement_cb(struct hci_dev *hdev, u16 opcode,
  178. struct adv_monitor *monitor,
  179. struct sk_buff *skb)
  180. {
  181. struct msft_rp_le_monitor_advertisement *rp;
  182. struct msft_monitor_advertisement_handle_data *handle_data;
  183. struct msft_data *msft = hdev->msft_data;
  184. int status = 0;
  185. hci_dev_lock(hdev);
  186. rp = (struct msft_rp_le_monitor_advertisement *)skb->data;
  187. if (skb->len < sizeof(*rp)) {
  188. status = HCI_ERROR_UNSPECIFIED;
  189. goto unlock;
  190. }
  191. status = rp->status;
  192. if (status)
  193. goto unlock;
  194. handle_data = kmalloc(sizeof(*handle_data), GFP_KERNEL);
  195. if (!handle_data) {
  196. status = HCI_ERROR_UNSPECIFIED;
  197. goto unlock;
  198. }
  199. handle_data->mgmt_handle = monitor->handle;
  200. handle_data->msft_handle = rp->handle;
  201. INIT_LIST_HEAD(&handle_data->list);
  202. list_add(&handle_data->list, &msft->handle_map);
  203. monitor->state = ADV_MONITOR_STATE_OFFLOADED;
  204. unlock:
  205. if (status)
  206. hci_free_adv_monitor(hdev, monitor);
  207. hci_dev_unlock(hdev);
  208. return status;
  209. }
  210. static int msft_le_cancel_monitor_advertisement_cb(struct hci_dev *hdev,
  211. u16 opcode,
  212. struct adv_monitor *monitor,
  213. struct sk_buff *skb)
  214. {
  215. struct msft_rp_le_cancel_monitor_advertisement *rp;
  216. struct msft_monitor_advertisement_handle_data *handle_data;
  217. struct msft_data *msft = hdev->msft_data;
  218. int status = 0;
  219. rp = (struct msft_rp_le_cancel_monitor_advertisement *)skb->data;
  220. if (skb->len < sizeof(*rp)) {
  221. status = HCI_ERROR_UNSPECIFIED;
  222. goto done;
  223. }
  224. status = rp->status;
  225. if (status)
  226. goto done;
  227. hci_dev_lock(hdev);
  228. handle_data = msft_find_handle_data(hdev, monitor->handle, true);
  229. if (handle_data) {
  230. if (monitor->state == ADV_MONITOR_STATE_OFFLOADED)
  231. monitor->state = ADV_MONITOR_STATE_REGISTERED;
  232. /* Do not free the monitor if it is being removed due to
  233. * suspend. It will be re-monitored on resume.
  234. */
  235. if (!msft->suspending) {
  236. hci_free_adv_monitor(hdev, monitor);
  237. /* Clear any monitored devices by this Adv Monitor */
  238. msft_monitor_device_del(hdev, handle_data->mgmt_handle,
  239. NULL, 0, false);
  240. }
  241. list_del(&handle_data->list);
  242. kfree(handle_data);
  243. }
  244. hci_dev_unlock(hdev);
  245. done:
  246. return status;
  247. }
  248. /* This function requires the caller holds hci_req_sync_lock */
  249. static int msft_remove_monitor_sync(struct hci_dev *hdev,
  250. struct adv_monitor *monitor)
  251. {
  252. struct msft_cp_le_cancel_monitor_advertisement cp;
  253. struct msft_monitor_advertisement_handle_data *handle_data;
  254. struct sk_buff *skb;
  255. handle_data = msft_find_handle_data(hdev, monitor->handle, true);
  256. /* If no matched handle, just remove without telling controller */
  257. if (!handle_data)
  258. return -ENOENT;
  259. cp.sub_opcode = MSFT_OP_LE_CANCEL_MONITOR_ADVERTISEMENT;
  260. cp.handle = handle_data->msft_handle;
  261. skb = __hci_cmd_sync(hdev, hdev->msft_opcode, sizeof(cp), &cp,
  262. HCI_CMD_TIMEOUT);
  263. if (IS_ERR_OR_NULL(skb)) {
  264. if (!skb)
  265. return -EIO;
  266. return PTR_ERR(skb);
  267. }
  268. return msft_le_cancel_monitor_advertisement_cb(hdev, hdev->msft_opcode,
  269. monitor, skb);
  270. }
  271. /* This function requires the caller holds hci_req_sync_lock */
  272. int msft_suspend_sync(struct hci_dev *hdev)
  273. {
  274. struct msft_data *msft = hdev->msft_data;
  275. struct adv_monitor *monitor;
  276. int handle = 0;
  277. if (!msft || !msft_monitor_supported(hdev))
  278. return 0;
  279. msft->suspending = true;
  280. while (1) {
  281. monitor = idr_get_next(&hdev->adv_monitors_idr, &handle);
  282. if (!monitor)
  283. break;
  284. msft_remove_monitor_sync(hdev, monitor);
  285. handle++;
  286. }
  287. /* All monitors have been removed */
  288. msft->suspending = false;
  289. return 0;
  290. }
  291. static bool msft_monitor_rssi_valid(struct adv_monitor *monitor)
  292. {
  293. struct adv_rssi_thresholds *r = &monitor->rssi;
  294. if (r->high_threshold < MSFT_RSSI_THRESHOLD_VALUE_MIN ||
  295. r->high_threshold > MSFT_RSSI_THRESHOLD_VALUE_MAX ||
  296. r->low_threshold < MSFT_RSSI_THRESHOLD_VALUE_MIN ||
  297. r->low_threshold > MSFT_RSSI_THRESHOLD_VALUE_MAX)
  298. return false;
  299. /* High_threshold_timeout is not supported,
  300. * once high_threshold is reached, events are immediately reported.
  301. */
  302. if (r->high_threshold_timeout != 0)
  303. return false;
  304. if (r->low_threshold_timeout > MSFT_RSSI_LOW_TIMEOUT_MAX)
  305. return false;
  306. /* Sampling period from 0x00 to 0xFF are all allowed */
  307. return true;
  308. }
  309. static bool msft_monitor_pattern_valid(struct adv_monitor *monitor)
  310. {
  311. return msft_monitor_rssi_valid(monitor);
  312. /* No additional check needed for pattern-based monitor */
  313. }
  314. static int msft_add_monitor_sync(struct hci_dev *hdev,
  315. struct adv_monitor *monitor)
  316. {
  317. struct msft_cp_le_monitor_advertisement *cp;
  318. struct msft_le_monitor_advertisement_pattern_data *pattern_data;
  319. struct msft_le_monitor_advertisement_pattern *pattern;
  320. struct adv_pattern *entry;
  321. size_t total_size = sizeof(*cp) + sizeof(*pattern_data);
  322. ptrdiff_t offset = 0;
  323. u8 pattern_count = 0;
  324. struct sk_buff *skb;
  325. if (!msft_monitor_pattern_valid(monitor))
  326. return -EINVAL;
  327. list_for_each_entry(entry, &monitor->patterns, list) {
  328. pattern_count++;
  329. total_size += sizeof(*pattern) + entry->length;
  330. }
  331. cp = kmalloc(total_size, GFP_KERNEL);
  332. if (!cp)
  333. return -ENOMEM;
  334. cp->sub_opcode = MSFT_OP_LE_MONITOR_ADVERTISEMENT;
  335. cp->rssi_high = monitor->rssi.high_threshold;
  336. cp->rssi_low = monitor->rssi.low_threshold;
  337. cp->rssi_low_interval = (u8)monitor->rssi.low_threshold_timeout;
  338. cp->rssi_sampling_period = monitor->rssi.sampling_period;
  339. cp->cond_type = MSFT_MONITOR_ADVERTISEMENT_TYPE_PATTERN;
  340. pattern_data = (void *)cp->data;
  341. pattern_data->count = pattern_count;
  342. list_for_each_entry(entry, &monitor->patterns, list) {
  343. pattern = (void *)(pattern_data->data + offset);
  344. /* the length also includes data_type and offset */
  345. pattern->length = entry->length + 2;
  346. pattern->data_type = entry->ad_type;
  347. pattern->start_byte = entry->offset;
  348. memcpy(pattern->pattern, entry->value, entry->length);
  349. offset += sizeof(*pattern) + entry->length;
  350. }
  351. skb = __hci_cmd_sync(hdev, hdev->msft_opcode, total_size, cp,
  352. HCI_CMD_TIMEOUT);
  353. kfree(cp);
  354. if (IS_ERR_OR_NULL(skb)) {
  355. if (!skb)
  356. return -EIO;
  357. return PTR_ERR(skb);
  358. }
  359. return msft_le_monitor_advertisement_cb(hdev, hdev->msft_opcode,
  360. monitor, skb);
  361. }
  362. /* This function requires the caller holds hci_req_sync_lock */
  363. static void reregister_monitor(struct hci_dev *hdev)
  364. {
  365. struct adv_monitor *monitor;
  366. struct msft_data *msft = hdev->msft_data;
  367. int handle = 0;
  368. if (!msft)
  369. return;
  370. msft->resuming = true;
  371. while (1) {
  372. monitor = idr_get_next(&hdev->adv_monitors_idr, &handle);
  373. if (!monitor)
  374. break;
  375. msft_add_monitor_sync(hdev, monitor);
  376. handle++;
  377. }
  378. /* All monitors have been reregistered */
  379. msft->resuming = false;
  380. }
  381. /* This function requires the caller holds hci_req_sync_lock */
  382. int msft_resume_sync(struct hci_dev *hdev)
  383. {
  384. struct msft_data *msft = hdev->msft_data;
  385. if (!msft || !msft_monitor_supported(hdev))
  386. return 0;
  387. hci_dev_lock(hdev);
  388. /* Clear already tracked devices on resume. Once the monitors are
  389. * reregistered, devices in range will be found again after resume.
  390. */
  391. hdev->advmon_pend_notify = false;
  392. msft_monitor_device_del(hdev, 0, NULL, 0, true);
  393. hci_dev_unlock(hdev);
  394. reregister_monitor(hdev);
  395. return 0;
  396. }
  397. /* This function requires the caller holds hci_req_sync_lock */
  398. void msft_do_open(struct hci_dev *hdev)
  399. {
  400. struct msft_data *msft = hdev->msft_data;
  401. if (hdev->msft_opcode == HCI_OP_NOP)
  402. return;
  403. if (!msft) {
  404. bt_dev_err(hdev, "MSFT extension not registered");
  405. return;
  406. }
  407. bt_dev_dbg(hdev, "Initialize MSFT extension");
  408. /* Reset existing MSFT data before re-reading */
  409. kfree(msft->evt_prefix);
  410. msft->evt_prefix = NULL;
  411. msft->evt_prefix_len = 0;
  412. msft->features = 0;
  413. if (!read_supported_features(hdev, msft)) {
  414. hdev->msft_data = NULL;
  415. kfree(msft);
  416. return;
  417. }
  418. if (msft_monitor_supported(hdev)) {
  419. msft->resuming = true;
  420. msft_set_filter_enable(hdev, true);
  421. /* Monitors get removed on power off, so we need to explicitly
  422. * tell the controller to re-monitor.
  423. */
  424. reregister_monitor(hdev);
  425. }
  426. }
  427. void msft_do_close(struct hci_dev *hdev)
  428. {
  429. struct msft_data *msft = hdev->msft_data;
  430. struct msft_monitor_advertisement_handle_data *handle_data, *tmp;
  431. struct adv_monitor *monitor;
  432. if (!msft)
  433. return;
  434. bt_dev_dbg(hdev, "Cleanup of MSFT extension");
  435. /* The controller will silently remove all monitors on power off.
  436. * Therefore, remove handle_data mapping and reset monitor state.
  437. */
  438. list_for_each_entry_safe(handle_data, tmp, &msft->handle_map, list) {
  439. monitor = idr_find(&hdev->adv_monitors_idr,
  440. handle_data->mgmt_handle);
  441. if (monitor && monitor->state == ADV_MONITOR_STATE_OFFLOADED)
  442. monitor->state = ADV_MONITOR_STATE_REGISTERED;
  443. list_del(&handle_data->list);
  444. kfree(handle_data);
  445. }
  446. hci_dev_lock(hdev);
  447. /* Clear any devices that are being monitored and notify device lost */
  448. hdev->advmon_pend_notify = false;
  449. msft_monitor_device_del(hdev, 0, NULL, 0, true);
  450. hci_dev_unlock(hdev);
  451. }
  452. void msft_register(struct hci_dev *hdev)
  453. {
  454. struct msft_data *msft = NULL;
  455. bt_dev_dbg(hdev, "Register MSFT extension");
  456. msft = kzalloc(sizeof(*msft), GFP_KERNEL);
  457. if (!msft) {
  458. bt_dev_err(hdev, "Failed to register MSFT extension");
  459. return;
  460. }
  461. INIT_LIST_HEAD(&msft->handle_map);
  462. hdev->msft_data = msft;
  463. }
  464. void msft_unregister(struct hci_dev *hdev)
  465. {
  466. struct msft_data *msft = hdev->msft_data;
  467. if (!msft)
  468. return;
  469. bt_dev_dbg(hdev, "Unregister MSFT extension");
  470. hdev->msft_data = NULL;
  471. kfree(msft->evt_prefix);
  472. kfree(msft);
  473. }
  474. /* This function requires the caller holds hdev->lock */
  475. static void msft_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr,
  476. __u8 addr_type, __u16 mgmt_handle)
  477. {
  478. struct monitored_device *dev;
  479. dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  480. if (!dev) {
  481. bt_dev_err(hdev, "MSFT vendor event %u: no memory",
  482. MSFT_EV_LE_MONITOR_DEVICE);
  483. return;
  484. }
  485. bacpy(&dev->bdaddr, bdaddr);
  486. dev->addr_type = addr_type;
  487. dev->handle = mgmt_handle;
  488. dev->notified = false;
  489. INIT_LIST_HEAD(&dev->list);
  490. list_add(&dev->list, &hdev->monitored_devices);
  491. hdev->advmon_pend_notify = true;
  492. }
  493. /* This function requires the caller holds hdev->lock */
  494. static void msft_device_lost(struct hci_dev *hdev, bdaddr_t *bdaddr,
  495. __u8 addr_type, __u16 mgmt_handle)
  496. {
  497. if (!msft_monitor_device_del(hdev, mgmt_handle, bdaddr, addr_type,
  498. true)) {
  499. bt_dev_err(hdev, "MSFT vendor event %u: dev %pMR not in list",
  500. MSFT_EV_LE_MONITOR_DEVICE, bdaddr);
  501. }
  502. }
  503. static void *msft_skb_pull(struct hci_dev *hdev, struct sk_buff *skb,
  504. u8 ev, size_t len)
  505. {
  506. void *data;
  507. data = skb_pull_data(skb, len);
  508. if (!data)
  509. bt_dev_err(hdev, "Malformed MSFT vendor event: 0x%02x", ev);
  510. return data;
  511. }
  512. /* This function requires the caller holds hdev->lock */
  513. static void msft_monitor_device_evt(struct hci_dev *hdev, struct sk_buff *skb)
  514. {
  515. struct msft_ev_le_monitor_device *ev;
  516. struct msft_monitor_advertisement_handle_data *handle_data;
  517. u8 addr_type;
  518. ev = msft_skb_pull(hdev, skb, MSFT_EV_LE_MONITOR_DEVICE, sizeof(*ev));
  519. if (!ev)
  520. return;
  521. bt_dev_dbg(hdev,
  522. "MSFT vendor event 0x%02x: handle 0x%04x state %d addr %pMR",
  523. MSFT_EV_LE_MONITOR_DEVICE, ev->monitor_handle,
  524. ev->monitor_state, &ev->bdaddr);
  525. handle_data = msft_find_handle_data(hdev, ev->monitor_handle, false);
  526. if (!handle_data)
  527. return;
  528. switch (ev->addr_type) {
  529. case ADDR_LE_DEV_PUBLIC:
  530. addr_type = BDADDR_LE_PUBLIC;
  531. break;
  532. case ADDR_LE_DEV_RANDOM:
  533. addr_type = BDADDR_LE_RANDOM;
  534. break;
  535. default:
  536. bt_dev_err(hdev,
  537. "MSFT vendor event 0x%02x: unknown addr type 0x%02x",
  538. MSFT_EV_LE_MONITOR_DEVICE, ev->addr_type);
  539. return;
  540. }
  541. if (ev->monitor_state)
  542. msft_device_found(hdev, &ev->bdaddr, addr_type,
  543. handle_data->mgmt_handle);
  544. else
  545. msft_device_lost(hdev, &ev->bdaddr, addr_type,
  546. handle_data->mgmt_handle);
  547. }
  548. void msft_vendor_evt(struct hci_dev *hdev, void *data, struct sk_buff *skb)
  549. {
  550. struct msft_data *msft = hdev->msft_data;
  551. u8 *evt_prefix;
  552. u8 *evt;
  553. if (!msft)
  554. return;
  555. /* When the extension has defined an event prefix, check that it
  556. * matches, and otherwise just return.
  557. */
  558. if (msft->evt_prefix_len > 0) {
  559. evt_prefix = msft_skb_pull(hdev, skb, 0, msft->evt_prefix_len);
  560. if (!evt_prefix)
  561. return;
  562. if (memcmp(evt_prefix, msft->evt_prefix, msft->evt_prefix_len))
  563. return;
  564. }
  565. /* Every event starts at least with an event code and the rest of
  566. * the data is variable and depends on the event code.
  567. */
  568. if (skb->len < 1)
  569. return;
  570. evt = msft_skb_pull(hdev, skb, 0, sizeof(*evt));
  571. if (!evt)
  572. return;
  573. hci_dev_lock(hdev);
  574. switch (*evt) {
  575. case MSFT_EV_LE_MONITOR_DEVICE:
  576. msft_monitor_device_evt(hdev, skb);
  577. break;
  578. default:
  579. bt_dev_dbg(hdev, "MSFT vendor event 0x%02x", *evt);
  580. break;
  581. }
  582. hci_dev_unlock(hdev);
  583. }
  584. __u64 msft_get_features(struct hci_dev *hdev)
  585. {
  586. struct msft_data *msft = hdev->msft_data;
  587. return msft ? msft->features : 0;
  588. }
  589. static void msft_le_set_advertisement_filter_enable_cb(struct hci_dev *hdev,
  590. u8 status, u16 opcode,
  591. struct sk_buff *skb)
  592. {
  593. struct msft_cp_le_set_advertisement_filter_enable *cp;
  594. struct msft_rp_le_set_advertisement_filter_enable *rp;
  595. struct msft_data *msft = hdev->msft_data;
  596. rp = (struct msft_rp_le_set_advertisement_filter_enable *)skb->data;
  597. if (skb->len < sizeof(*rp))
  598. return;
  599. /* Error 0x0C would be returned if the filter enabled status is
  600. * already set to whatever we were trying to set.
  601. * Although the default state should be disabled, some controller set
  602. * the initial value to enabled. Because there is no way to know the
  603. * actual initial value before sending this command, here we also treat
  604. * error 0x0C as success.
  605. */
  606. if (status != 0x00 && status != 0x0C)
  607. return;
  608. hci_dev_lock(hdev);
  609. cp = hci_sent_cmd_data(hdev, hdev->msft_opcode);
  610. msft->filter_enabled = cp->enable;
  611. if (status == 0x0C)
  612. bt_dev_warn(hdev, "MSFT filter_enable is already %s",
  613. cp->enable ? "on" : "off");
  614. hci_dev_unlock(hdev);
  615. }
  616. /* This function requires the caller holds hci_req_sync_lock */
  617. int msft_add_monitor_pattern(struct hci_dev *hdev, struct adv_monitor *monitor)
  618. {
  619. struct msft_data *msft = hdev->msft_data;
  620. if (!msft)
  621. return -EOPNOTSUPP;
  622. if (msft->resuming || msft->suspending)
  623. return -EBUSY;
  624. return msft_add_monitor_sync(hdev, monitor);
  625. }
  626. /* This function requires the caller holds hci_req_sync_lock */
  627. int msft_remove_monitor(struct hci_dev *hdev, struct adv_monitor *monitor)
  628. {
  629. struct msft_data *msft = hdev->msft_data;
  630. if (!msft)
  631. return -EOPNOTSUPP;
  632. if (msft->resuming || msft->suspending)
  633. return -EBUSY;
  634. return msft_remove_monitor_sync(hdev, monitor);
  635. }
  636. void msft_req_add_set_filter_enable(struct hci_request *req, bool enable)
  637. {
  638. struct hci_dev *hdev = req->hdev;
  639. struct msft_cp_le_set_advertisement_filter_enable cp;
  640. cp.sub_opcode = MSFT_OP_LE_SET_ADVERTISEMENT_FILTER_ENABLE;
  641. cp.enable = enable;
  642. hci_req_add(req, hdev->msft_opcode, sizeof(cp), &cp);
  643. }
  644. int msft_set_filter_enable(struct hci_dev *hdev, bool enable)
  645. {
  646. struct hci_request req;
  647. struct msft_data *msft = hdev->msft_data;
  648. int err;
  649. if (!msft)
  650. return -EOPNOTSUPP;
  651. hci_req_init(&req, hdev);
  652. msft_req_add_set_filter_enable(&req, enable);
  653. err = hci_req_run_skb(&req, msft_le_set_advertisement_filter_enable_cb);
  654. return err;
  655. }
  656. bool msft_curve_validity(struct hci_dev *hdev)
  657. {
  658. return hdev->msft_curve_validity;
  659. }