1 // SPDX-License-Identifier: GPL-2.0
3 /* Copyright (C) 2021 Intel Corporation */
5 #include <net/bluetooth/bluetooth.h>
6 #include <net/bluetooth/hci_core.h>
9 static int hci_codec_list_add(struct list_head *list,
10 struct hci_op_read_local_codec_caps *sent,
11 struct hci_rp_read_local_codec_caps *rp,
15 struct codec_list *entry;
17 entry = kzalloc(sizeof(*entry) + len, GFP_KERNEL);
22 if (sent->id == 0xFF) {
23 entry->cid = __le16_to_cpu(sent->cid);
24 entry->vid = __le16_to_cpu(sent->vid);
26 entry->transport = sent->transport;
30 entry->num_caps = rp->num_caps;
31 memcpy(entry->caps, caps, len);
33 list_add(&entry->list, list);
38 void hci_codec_list_clear(struct list_head *codec_list)
40 struct codec_list *c, *n;
42 list_for_each_entry_safe(c, n, codec_list, list) {
48 static void hci_read_codec_capabilities(struct hci_dev *hdev, __u8 transport,
49 struct hci_op_read_local_codec_caps
54 for (i = 0; i < TRANSPORT_TYPE_MAX; i++) {
55 if (transport & BIT(i)) {
56 struct hci_rp_read_local_codec_caps *rp;
57 struct hci_codec_caps *caps;
64 /* If Read_Codec_Capabilities command is not supported
65 * then just add codec to the list without caps
67 if (!(hdev->commands[45] & 0x08)) {
69 hci_codec_list_add(&hdev->local_codecs, cmd,
75 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_CODEC_CAPS,
79 bt_dev_err(hdev, "Failed to read codec capabilities (%ld)",
84 if (skb->len < sizeof(*rp))
87 rp = (void *)skb->data;
94 /* this codec doesn't have capabilities */
98 skb_pull(skb, sizeof(*rp));
100 for (j = 0, len = 0; j < rp->num_caps; j++) {
101 caps = (void *)skb->data;
102 if (skb->len < sizeof(*caps))
104 if (skb->len < caps->len)
106 len += sizeof(caps->len) + caps->len;
107 skb_pull(skb, sizeof(caps->len) + caps->len);
112 hci_codec_list_add(&hdev->local_codecs, cmd, rp,
113 (__u8 *)rp + sizeof(*rp), len);
114 hci_dev_unlock(hdev);
121 void hci_read_supported_codecs(struct hci_dev *hdev)
124 struct hci_rp_read_local_supported_codecs *rp;
125 struct hci_std_codecs *std_codecs;
126 struct hci_vnd_codecs *vnd_codecs;
127 struct hci_op_read_local_codec_caps caps;
130 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_CODECS, 0, NULL,
134 bt_dev_err(hdev, "Failed to read local supported codecs (%ld)",
139 if (skb->len < sizeof(*rp))
142 rp = (void *)skb->data;
147 skb_pull(skb, sizeof(rp->status));
149 std_codecs = (void *)skb->data;
151 /* validate codecs length before accessing */
152 if (skb->len < flex_array_size(std_codecs, codec, std_codecs->num)
153 + sizeof(std_codecs->num))
156 /* enumerate codec capabilities of standard codecs */
157 memset(&caps, 0, sizeof(caps));
158 for (i = 0; i < std_codecs->num; i++) {
159 caps.id = std_codecs->codec[i];
160 caps.direction = 0x00;
161 hci_read_codec_capabilities(hdev, LOCAL_CODEC_ACL_MASK, &caps);
164 skb_pull(skb, flex_array_size(std_codecs, codec, std_codecs->num)
165 + sizeof(std_codecs->num));
167 vnd_codecs = (void *)skb->data;
169 /* validate vendor codecs length before accessing */
171 flex_array_size(vnd_codecs, codec, vnd_codecs->num)
172 + sizeof(vnd_codecs->num))
175 /* enumerate vendor codec capabilities */
176 for (i = 0; i < vnd_codecs->num; i++) {
178 caps.cid = vnd_codecs->codec[i].cid;
179 caps.vid = vnd_codecs->codec[i].vid;
180 caps.direction = 0x00;
181 hci_read_codec_capabilities(hdev, LOCAL_CODEC_ACL_MASK, &caps);
188 void hci_read_supported_codecs_v2(struct hci_dev *hdev)
191 struct hci_rp_read_local_supported_codecs_v2 *rp;
192 struct hci_std_codecs_v2 *std_codecs;
193 struct hci_vnd_codecs_v2 *vnd_codecs;
194 struct hci_op_read_local_codec_caps caps;
197 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_CODECS_V2, 0, NULL,
201 bt_dev_err(hdev, "Failed to read local supported codecs (%ld)",
206 if (skb->len < sizeof(*rp))
209 rp = (void *)skb->data;
214 skb_pull(skb, sizeof(rp->status));
216 std_codecs = (void *)skb->data;
218 /* check for payload data length before accessing */
219 if (skb->len < flex_array_size(std_codecs, codec, std_codecs->num)
220 + sizeof(std_codecs->num))
223 memset(&caps, 0, sizeof(caps));
225 for (i = 0; i < std_codecs->num; i++) {
226 caps.id = std_codecs->codec[i].id;
227 hci_read_codec_capabilities(hdev, std_codecs->codec[i].transport,
231 skb_pull(skb, flex_array_size(std_codecs, codec, std_codecs->num)
232 + sizeof(std_codecs->num));
234 vnd_codecs = (void *)skb->data;
236 /* check for payload data length before accessing */
238 flex_array_size(vnd_codecs, codec, vnd_codecs->num)
239 + sizeof(vnd_codecs->num))
242 for (i = 0; i < vnd_codecs->num; i++) {
244 caps.cid = vnd_codecs->codec[i].cid;
245 caps.vid = vnd_codecs->codec[i].vid;
246 hci_read_codec_capabilities(hdev, vnd_codecs->codec[i].transport,