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>
11 #define MSFT_OP_READ_SUPPORTED_FEATURES 0x00
12 struct msft_cp_read_supported_features {
15 struct msft_rp_read_supported_features {
29 static bool read_supported_features(struct hci_dev *hdev,
30 struct msft_data *msft)
32 struct msft_cp_read_supported_features cp;
33 struct msft_rp_read_supported_features *rp;
36 cp.sub_opcode = MSFT_OP_READ_SUPPORTED_FEATURES;
38 skb = __hci_cmd_sync(hdev, hdev->msft_opcode, sizeof(cp), &cp,
41 bt_dev_err(hdev, "Failed to read MSFT supported features (%ld)",
46 if (skb->len < sizeof(*rp)) {
47 bt_dev_err(hdev, "MSFT supported features length mismatch");
51 rp = (struct msft_rp_read_supported_features *)skb->data;
53 if (rp->sub_opcode != MSFT_OP_READ_SUPPORTED_FEATURES)
56 if (rp->evt_prefix_len > 0) {
57 msft->evt_prefix = kmemdup(rp->evt_prefix, rp->evt_prefix_len,
59 if (!msft->evt_prefix)
63 msft->evt_prefix_len = rp->evt_prefix_len;
64 msft->features = __le64_to_cpu(rp->features);
74 void msft_do_open(struct hci_dev *hdev)
76 struct msft_data *msft;
78 if (hdev->msft_opcode == HCI_OP_NOP)
81 bt_dev_dbg(hdev, "Initialize MSFT extension");
83 msft = kzalloc(sizeof(*msft), GFP_KERNEL);
87 if (!read_supported_features(hdev, msft)) {
92 hdev->msft_data = msft;
95 void msft_do_close(struct hci_dev *hdev)
97 struct msft_data *msft = hdev->msft_data;
102 bt_dev_dbg(hdev, "Cleanup of MSFT extension");
104 hdev->msft_data = NULL;
106 kfree(msft->evt_prefix);
110 void msft_vendor_evt(struct hci_dev *hdev, struct sk_buff *skb)
112 struct msft_data *msft = hdev->msft_data;
118 /* When the extension has defined an event prefix, check that it
119 * matches, and otherwise just return.
121 if (msft->evt_prefix_len > 0) {
122 if (skb->len < msft->evt_prefix_len)
125 if (memcmp(skb->data, msft->evt_prefix, msft->evt_prefix_len))
128 skb_pull(skb, msft->evt_prefix_len);
131 /* Every event starts at least with an event code and the rest of
132 * the data is variable and depends on the event code.
140 bt_dev_dbg(hdev, "MSFT vendor event %u", event);
143 __u64 msft_get_features(struct hci_dev *hdev)
145 struct msft_data *msft = hdev->msft_data;
147 return msft ? msft->features : 0;