blob: 1fcc482397c364052a5b82d842576f60be48c485 [file] [log] [blame]
Thomas Gleixner97fb5e82019-05-29 07:17:58 -07001// SPDX-License-Identifier: GPL-2.0-only
Andrei Emeltchenko466f8002012-05-29 13:59:01 +03002/*
3 Copyright (c) 2010,2011 Code Aurora Forum. All rights reserved.
4 Copyright (c) 2011,2012 Intel Corp.
5
Andrei Emeltchenko466f8002012-05-29 13:59:01 +03006*/
7
8#include <net/bluetooth/bluetooth.h>
9#include <net/bluetooth/hci_core.h>
10#include <net/bluetooth/l2cap.h>
Marcel Holtmann7ef9fbf2013-10-10 14:54:14 -070011
Arron Wang83927882015-07-24 17:10:16 +080012#include "hci_request.h"
Marcel Holtmann70247282013-10-10 14:54:15 -070013#include "a2mp.h"
Marcel Holtmann7ef9fbf2013-10-10 14:54:14 -070014#include "amp.h"
Andrei Emeltchenko466f8002012-05-29 13:59:01 +030015
Marcel Holtmann055540a2015-02-14 13:36:03 -080016#define A2MP_FEAT_EXT 0x8000
17
Andrei Emeltchenkof97268f2012-09-27 17:26:07 +030018/* Global AMP Manager list */
Marcel Holtmann59d4d082015-02-14 13:36:04 -080019static LIST_HEAD(amp_mgr_list);
20static DEFINE_MUTEX(amp_mgr_list_lock);
Andrei Emeltchenkof97268f2012-09-27 17:26:07 +030021
Andrei Emeltchenkof6d3c6e2012-05-29 13:59:03 +030022/* A2MP build & send command helper functions */
23static struct a2mp_cmd *__a2mp_build(u8 code, u8 ident, u16 len, void *data)
24{
25 struct a2mp_cmd *cmd;
26 int plen;
27
28 plen = sizeof(*cmd) + len;
29 cmd = kzalloc(plen, GFP_KERNEL);
30 if (!cmd)
31 return NULL;
32
33 cmd->code = code;
34 cmd->ident = ident;
35 cmd->len = cpu_to_le16(len);
36
37 memcpy(cmd->data, data, len);
38
39 return cmd;
40}
41
Marcel Holtmannbc333cc2015-02-14 13:36:06 -080042static void a2mp_send(struct amp_mgr *mgr, u8 code, u8 ident, u16 len, void *data)
Andrei Emeltchenkof6d3c6e2012-05-29 13:59:03 +030043{
44 struct l2cap_chan *chan = mgr->a2mp_chan;
45 struct a2mp_cmd *cmd;
46 u16 total_len = len + sizeof(*cmd);
47 struct kvec iv;
48 struct msghdr msg;
49
50 cmd = __a2mp_build(code, ident, len, data);
51 if (!cmd)
52 return;
53
54 iv.iov_base = cmd;
55 iv.iov_len = total_len;
56
57 memset(&msg, 0, sizeof(msg));
58
David Howellsaa563d72018-10-20 00:57:56 +010059 iov_iter_kvec(&msg.msg_iter, WRITE, &iv, 1, total_len);
Andrei Emeltchenkof6d3c6e2012-05-29 13:59:03 +030060
Marcel Holtmann8d463212014-06-05 15:22:51 +020061 l2cap_chan_send(chan, &msg, total_len);
Andrei Emeltchenkof6d3c6e2012-05-29 13:59:03 +030062
63 kfree(cmd);
64}
65
Marcel Holtmann87e2a022015-02-14 13:36:07 -080066static u8 __next_ident(struct amp_mgr *mgr)
Andrei Emeltchenkoaa095372012-09-27 17:26:10 +030067{
68 if (++mgr->ident == 0)
69 mgr->ident = 1;
70
71 return mgr->ident;
72}
73
Marcel Holtmann469cd4c2015-02-14 13:36:05 -080074static struct amp_mgr *amp_mgr_lookup_by_state(u8 state)
75{
76 struct amp_mgr *mgr;
77
78 mutex_lock(&amp_mgr_list_lock);
79 list_for_each_entry(mgr, &amp_mgr_list, list) {
80 if (test_and_clear_bit(state, &mgr->state)) {
81 amp_mgr_get(mgr);
82 mutex_unlock(&amp_mgr_list_lock);
83 return mgr;
84 }
85 }
86 mutex_unlock(&amp_mgr_list_lock);
87
88 return NULL;
89}
90
Andrei Emeltchenko8598d062012-05-29 13:59:09 +030091/* hci_dev_list shall be locked */
Marcel Holtmann23f0cb42013-10-05 11:47:47 -070092static void __a2mp_add_cl(struct amp_mgr *mgr, struct a2mp_cl *cl)
Andrei Emeltchenko8598d062012-05-29 13:59:09 +030093{
Andrei Emeltchenko8598d062012-05-29 13:59:09 +030094 struct hci_dev *hdev;
Marcel Holtmanne8803532013-10-06 02:08:35 -070095 int i = 1;
Andrei Emeltchenko8598d062012-05-29 13:59:09 +030096
Marcel Holtmann346e7092013-10-05 11:47:46 -070097 cl[0].id = AMP_ID_BREDR;
98 cl[0].type = AMP_TYPE_BREDR;
99 cl[0].status = AMP_STATUS_BLUETOOTH_ONLY;
Andrei Emeltchenko8598d062012-05-29 13:59:09 +0300100
101 list_for_each_entry(hdev, &hci_dev_list, list) {
Marcel Holtmanne8803532013-10-06 02:08:35 -0700102 if (hdev->dev_type == HCI_AMP) {
103 cl[i].id = hdev->id;
104 cl[i].type = hdev->amp_type;
Marcel Holtmanncd0a85c2013-10-07 00:58:34 -0700105 if (test_bit(HCI_UP, &hdev->flags))
106 cl[i].status = hdev->amp_status;
107 else
108 cl[i].status = AMP_STATUS_POWERED_DOWN;
Marcel Holtmanne8803532013-10-06 02:08:35 -0700109 i++;
110 }
Andrei Emeltchenko8598d062012-05-29 13:59:09 +0300111 }
112}
113
Andrei Emeltchenko21dbd2c2012-05-29 13:59:08 +0300114/* Processing A2MP messages */
115static int a2mp_command_rej(struct amp_mgr *mgr, struct sk_buff *skb,
116 struct a2mp_cmd *hdr)
117{
118 struct a2mp_cmd_rej *rej = (void *) skb->data;
119
120 if (le16_to_cpu(hdr->len) < sizeof(*rej))
121 return -EINVAL;
122
Kai Yefad48d82021-06-03 15:40:59 +0800123 BT_DBG("ident %u reason %d", hdr->ident, le16_to_cpu(rej->reason));
Andrei Emeltchenko21dbd2c2012-05-29 13:59:08 +0300124
125 skb_pull(skb, sizeof(*rej));
126
127 return 0;
128}
129
Andrei Emeltchenko8598d062012-05-29 13:59:09 +0300130static int a2mp_discover_req(struct amp_mgr *mgr, struct sk_buff *skb,
131 struct a2mp_cmd *hdr)
132{
133 struct a2mp_discov_req *req = (void *) skb->data;
134 u16 len = le16_to_cpu(hdr->len);
135 struct a2mp_discov_rsp *rsp;
136 u16 ext_feat;
137 u8 num_ctrl;
Marcel Holtmannf822c412013-10-05 11:47:41 -0700138 struct hci_dev *hdev;
Andrei Emeltchenko8598d062012-05-29 13:59:09 +0300139
140 if (len < sizeof(*req))
141 return -EINVAL;
142
143 skb_pull(skb, sizeof(*req));
144
145 ext_feat = le16_to_cpu(req->ext_feat);
146
147 BT_DBG("mtu %d efm 0x%4.4x", le16_to_cpu(req->mtu), ext_feat);
148
149 /* check that packet is not broken for now */
150 while (ext_feat & A2MP_FEAT_EXT) {
151 if (len < sizeof(ext_feat))
152 return -EINVAL;
153
154 ext_feat = get_unaligned_le16(skb->data);
155 BT_DBG("efm 0x%4.4x", ext_feat);
156 len -= sizeof(ext_feat);
157 skb_pull(skb, sizeof(ext_feat));
158 }
159
160 read_lock(&hci_dev_list_lock);
161
Marcel Holtmannf822c412013-10-05 11:47:41 -0700162 /* at minimum the BR/EDR needs to be listed */
163 num_ctrl = 1;
164
165 list_for_each_entry(hdev, &hci_dev_list, list) {
166 if (hdev->dev_type == HCI_AMP)
167 num_ctrl++;
168 }
169
Gustavo A. R. Silva3c97ce12019-02-07 18:28:17 -0600170 len = struct_size(rsp, cl, num_ctrl);
Andrei Emeltchenko8598d062012-05-29 13:59:09 +0300171 rsp = kmalloc(len, GFP_ATOMIC);
172 if (!rsp) {
173 read_unlock(&hci_dev_list_lock);
174 return -ENOMEM;
175 }
176
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700177 rsp->mtu = cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU);
Andrei Emeltchenko8598d062012-05-29 13:59:09 +0300178 rsp->ext_feat = 0;
179
Marcel Holtmann23f0cb42013-10-05 11:47:47 -0700180 __a2mp_add_cl(mgr, rsp->cl);
Andrei Emeltchenko8598d062012-05-29 13:59:09 +0300181
182 read_unlock(&hci_dev_list_lock);
183
184 a2mp_send(mgr, A2MP_DISCOVER_RSP, hdr->ident, len, rsp);
185
186 kfree(rsp);
187 return 0;
188}
189
Andrei Emeltchenkoaa095372012-09-27 17:26:10 +0300190static int a2mp_discover_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
191 struct a2mp_cmd *hdr)
192{
193 struct a2mp_discov_rsp *rsp = (void *) skb->data;
194 u16 len = le16_to_cpu(hdr->len);
195 struct a2mp_cl *cl;
196 u16 ext_feat;
Andrei Emeltchenko2766be42012-09-27 17:26:21 +0300197 bool found = false;
Andrei Emeltchenkoaa095372012-09-27 17:26:10 +0300198
199 if (len < sizeof(*rsp))
200 return -EINVAL;
201
202 len -= sizeof(*rsp);
203 skb_pull(skb, sizeof(*rsp));
204
205 ext_feat = le16_to_cpu(rsp->ext_feat);
206
207 BT_DBG("mtu %d efm 0x%4.4x", le16_to_cpu(rsp->mtu), ext_feat);
208
209 /* check that packet is not broken for now */
210 while (ext_feat & A2MP_FEAT_EXT) {
211 if (len < sizeof(ext_feat))
212 return -EINVAL;
213
214 ext_feat = get_unaligned_le16(skb->data);
215 BT_DBG("efm 0x%4.4x", ext_feat);
216 len -= sizeof(ext_feat);
217 skb_pull(skb, sizeof(ext_feat));
218 }
219
220 cl = (void *) skb->data;
221 while (len >= sizeof(*cl)) {
Kai Yefad48d82021-06-03 15:40:59 +0800222 BT_DBG("Remote AMP id %u type %u status %u", cl->id, cl->type,
Andrei Emeltchenkoaa095372012-09-27 17:26:10 +0300223 cl->status);
224
Marcel Holtmanna646bd82013-10-05 13:57:53 -0700225 if (cl->id != AMP_ID_BREDR && cl->type != AMP_TYPE_BREDR) {
Andrei Emeltchenkoaa095372012-09-27 17:26:10 +0300226 struct a2mp_info_req req;
227
Andrei Emeltchenko2766be42012-09-27 17:26:21 +0300228 found = true;
Luiz Augusto von Dentzeddb7732020-08-06 11:17:11 -0700229
230 memset(&req, 0, sizeof(req));
231
Andrei Emeltchenkoaa095372012-09-27 17:26:10 +0300232 req.id = cl->id;
233 a2mp_send(mgr, A2MP_GETINFO_REQ, __next_ident(mgr),
234 sizeof(req), &req);
235 }
236
237 len -= sizeof(*cl);
Johannes Bergaf728682017-06-16 14:29:22 +0200238 cl = skb_pull(skb, sizeof(*cl));
Andrei Emeltchenkoaa095372012-09-27 17:26:10 +0300239 }
240
Andrei Emeltchenko2766be42012-09-27 17:26:21 +0300241 /* Fall back to L2CAP init sequence */
242 if (!found) {
243 struct l2cap_conn *conn = mgr->l2cap_conn;
244 struct l2cap_chan *chan;
245
246 mutex_lock(&conn->chan_lock);
247
248 list_for_each_entry(chan, &conn->chan_l, list) {
249
250 BT_DBG("chan %p state %s", chan,
251 state_to_string(chan->state));
252
Johan Hedberg2338a7e2014-01-24 10:35:40 +0200253 if (chan->scid == L2CAP_CID_A2MP)
Andrei Emeltchenko2766be42012-09-27 17:26:21 +0300254 continue;
255
256 l2cap_chan_lock(chan);
257
258 if (chan->state == BT_CONNECT)
259 l2cap_send_conn_req(chan);
260
261 l2cap_chan_unlock(chan);
262 }
263
264 mutex_unlock(&conn->chan_lock);
265 }
266
Andrei Emeltchenkoaa095372012-09-27 17:26:10 +0300267 return 0;
268}
269
Andrei Emeltchenko329d81af2012-05-29 13:59:10 +0300270static int a2mp_change_notify(struct amp_mgr *mgr, struct sk_buff *skb,
271 struct a2mp_cmd *hdr)
272{
273 struct a2mp_cl *cl = (void *) skb->data;
274
275 while (skb->len >= sizeof(*cl)) {
Kai Yefad48d82021-06-03 15:40:59 +0800276 BT_DBG("Controller id %u type %u status %u", cl->id, cl->type,
Andrei Emeltchenko329d81af2012-05-29 13:59:10 +0300277 cl->status);
Johannes Bergaf728682017-06-16 14:29:22 +0200278 cl = skb_pull(skb, sizeof(*cl));
Andrei Emeltchenko329d81af2012-05-29 13:59:10 +0300279 }
280
281 /* TODO send A2MP_CHANGE_RSP */
282
283 return 0;
284}
285
Arron Wang83927882015-07-24 17:10:16 +0800286static void read_local_amp_info_complete(struct hci_dev *hdev, u8 status,
287 u16 opcode)
288{
289 BT_DBG("%s status 0x%2.2x", hdev->name, status);
290
291 a2mp_send_getinfo_rsp(hdev);
292}
293
Andrei Emeltchenko47f2d972012-05-29 13:59:11 +0300294static int a2mp_getinfo_req(struct amp_mgr *mgr, struct sk_buff *skb,
295 struct a2mp_cmd *hdr)
296{
297 struct a2mp_info_req *req = (void *) skb->data;
Andrei Emeltchenko47f2d972012-05-29 13:59:11 +0300298 struct hci_dev *hdev;
Arron Wang83927882015-07-24 17:10:16 +0800299 struct hci_request hreq;
300 int err = 0;
Andrei Emeltchenko47f2d972012-05-29 13:59:11 +0300301
302 if (le16_to_cpu(hdr->len) < sizeof(*req))
303 return -EINVAL;
304
Kai Yefad48d82021-06-03 15:40:59 +0800305 BT_DBG("id %u", req->id);
Andrei Emeltchenko47f2d972012-05-29 13:59:11 +0300306
Andrei Emeltchenko47f2d972012-05-29 13:59:11 +0300307 hdev = hci_dev_get(req->id);
Andrei Emeltchenkobc8dce42012-09-28 14:28:50 +0300308 if (!hdev || hdev->dev_type != HCI_AMP) {
Andrei Emeltchenko8e2a0d92012-09-27 17:26:08 +0300309 struct a2mp_info_rsp rsp;
310
Luiz Augusto von Dentzeddb7732020-08-06 11:17:11 -0700311 memset(&rsp, 0, sizeof(rsp));
312
Andrei Emeltchenko8e2a0d92012-09-27 17:26:08 +0300313 rsp.id = req->id;
314 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
315
316 a2mp_send(mgr, A2MP_GETINFO_RSP, hdr->ident, sizeof(rsp),
317 &rsp);
Andrei Emeltchenkobc8dce42012-09-28 14:28:50 +0300318
319 goto done;
Andrei Emeltchenko47f2d972012-05-29 13:59:11 +0300320 }
321
Andrei Emeltchenkocb6801c62012-12-07 14:59:08 +0200322 set_bit(READ_LOC_AMP_INFO, &mgr->state);
Arron Wang83927882015-07-24 17:10:16 +0800323 hci_req_init(&hreq, hdev);
324 hci_req_add(&hreq, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL);
325 err = hci_req_run(&hreq, read_local_amp_info_complete);
326 if (err < 0)
327 a2mp_send_getinfo_rsp(hdev);
Andrei Emeltchenko47f2d972012-05-29 13:59:11 +0300328
Andrei Emeltchenkobc8dce42012-09-28 14:28:50 +0300329done:
330 if (hdev)
331 hci_dev_put(hdev);
Andrei Emeltchenko47f2d972012-05-29 13:59:11 +0300332
333 skb_pull(skb, sizeof(*req));
334 return 0;
335}
336
Andrei Emeltchenko0d868de2012-09-27 17:26:14 +0300337static int a2mp_getinfo_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
338 struct a2mp_cmd *hdr)
339{
340 struct a2mp_info_rsp *rsp = (struct a2mp_info_rsp *) skb->data;
341 struct a2mp_amp_assoc_req req;
342 struct amp_ctrl *ctrl;
343
344 if (le16_to_cpu(hdr->len) < sizeof(*rsp))
345 return -EINVAL;
346
Kai Yefad48d82021-06-03 15:40:59 +0800347 BT_DBG("id %u status 0x%2.2x", rsp->id, rsp->status);
Andrei Emeltchenko0d868de2012-09-27 17:26:14 +0300348
349 if (rsp->status)
350 return -EINVAL;
351
Andrei Emeltchenkofa4ebc62012-10-05 16:56:55 +0300352 ctrl = amp_ctrl_add(mgr, rsp->id);
Andrei Emeltchenko0d868de2012-09-27 17:26:14 +0300353 if (!ctrl)
354 return -ENOMEM;
355
Luiz Augusto von Dentzeddb7732020-08-06 11:17:11 -0700356 memset(&req, 0, sizeof(req));
357
Andrei Emeltchenko0d868de2012-09-27 17:26:14 +0300358 req.id = rsp->id;
359 a2mp_send(mgr, A2MP_GETAMPASSOC_REQ, __next_ident(mgr), sizeof(req),
360 &req);
361
362 skb_pull(skb, sizeof(*rsp));
363 return 0;
364}
365
Andrei Emeltchenkoa28381dc2012-05-29 13:59:12 +0300366static int a2mp_getampassoc_req(struct amp_mgr *mgr, struct sk_buff *skb,
367 struct a2mp_cmd *hdr)
368{
369 struct a2mp_amp_assoc_req *req = (void *) skb->data;
370 struct hci_dev *hdev;
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300371 struct amp_mgr *tmp;
Andrei Emeltchenkoa28381dc2012-05-29 13:59:12 +0300372
373 if (le16_to_cpu(hdr->len) < sizeof(*req))
374 return -EINVAL;
375
Kai Yefad48d82021-06-03 15:40:59 +0800376 BT_DBG("id %u", req->id);
Andrei Emeltchenkoa28381dc2012-05-29 13:59:12 +0300377
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300378 /* Make sure that other request is not processed */
379 tmp = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC);
380
Andrei Emeltchenkoa28381dc2012-05-29 13:59:12 +0300381 hdev = hci_dev_get(req->id);
Marcel Holtmannece69122013-10-05 11:47:43 -0700382 if (!hdev || hdev->amp_type == AMP_TYPE_BREDR || tmp) {
Andrei Emeltchenkoa28381dc2012-05-29 13:59:12 +0300383 struct a2mp_amp_assoc_rsp rsp;
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300384
Luiz Augusto von Dentzeddb7732020-08-06 11:17:11 -0700385 memset(&rsp, 0, sizeof(rsp));
Christopher William Snowhilla5687c62020-12-26 19:12:32 -0800386 rsp.id = req->id;
Luiz Augusto von Dentzeddb7732020-08-06 11:17:11 -0700387
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300388 if (tmp) {
389 rsp.status = A2MP_STATUS_COLLISION_OCCURED;
390 amp_mgr_put(tmp);
391 } else {
392 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
393 }
Andrei Emeltchenkoa28381dc2012-05-29 13:59:12 +0300394
395 a2mp_send(mgr, A2MP_GETAMPASSOC_RSP, hdr->ident, sizeof(rsp),
396 &rsp);
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300397
398 goto done;
Andrei Emeltchenkoa28381dc2012-05-29 13:59:12 +0300399 }
400
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300401 amp_read_loc_assoc(hdev, mgr);
Andrei Emeltchenkoa28381dc2012-05-29 13:59:12 +0300402
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300403done:
Andrei Emeltchenkoa28381dc2012-05-29 13:59:12 +0300404 if (hdev)
405 hci_dev_put(hdev);
406
407 skb_pull(skb, sizeof(*req));
408 return 0;
409}
410
Andrei Emeltchenko9a5e94d2012-09-27 17:26:15 +0300411static int a2mp_getampassoc_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
412 struct a2mp_cmd *hdr)
413{
414 struct a2mp_amp_assoc_rsp *rsp = (void *) skb->data;
415 u16 len = le16_to_cpu(hdr->len);
416 struct hci_dev *hdev;
417 struct amp_ctrl *ctrl;
418 struct hci_conn *hcon;
Andrei Emeltchenko13465c02012-09-28 16:55:00 +0300419 size_t assoc_len;
Andrei Emeltchenko9a5e94d2012-09-27 17:26:15 +0300420
421 if (len < sizeof(*rsp))
422 return -EINVAL;
423
Andrei Emeltchenko13465c02012-09-28 16:55:00 +0300424 assoc_len = len - sizeof(*rsp);
425
Kai Yefad48d82021-06-03 15:40:59 +0800426 BT_DBG("id %u status 0x%2.2x assoc len %zu", rsp->id, rsp->status,
Andrei Emeltchenko13465c02012-09-28 16:55:00 +0300427 assoc_len);
Andrei Emeltchenko9a5e94d2012-09-27 17:26:15 +0300428
429 if (rsp->status)
430 return -EINVAL;
431
432 /* Save remote ASSOC data */
433 ctrl = amp_ctrl_lookup(mgr, rsp->id);
434 if (ctrl) {
Andrei Emeltchenko13465c02012-09-28 16:55:00 +0300435 u8 *assoc;
Andrei Emeltchenko9a5e94d2012-09-27 17:26:15 +0300436
Alexandru Gheorghiu5ae327f2013-03-17 07:16:50 +0200437 assoc = kmemdup(rsp->amp_assoc, assoc_len, GFP_KERNEL);
Andrei Emeltchenko9a5e94d2012-09-27 17:26:15 +0300438 if (!assoc) {
439 amp_ctrl_put(ctrl);
440 return -ENOMEM;
441 }
442
Andrei Emeltchenko9a5e94d2012-09-27 17:26:15 +0300443 ctrl->assoc = assoc;
444 ctrl->assoc_len = assoc_len;
445 ctrl->assoc_rem_len = assoc_len;
446 ctrl->assoc_len_so_far = 0;
447
448 amp_ctrl_put(ctrl);
449 }
450
451 /* Create Phys Link */
452 hdev = hci_dev_get(rsp->id);
453 if (!hdev)
454 return -EINVAL;
455
Andrei Emeltchenkoa0c234f2012-10-05 16:56:56 +0300456 hcon = phylink_add(hdev, mgr, rsp->id, true);
Andrei Emeltchenko9a5e94d2012-09-27 17:26:15 +0300457 if (!hcon)
458 goto done;
459
Kai Yefad48d82021-06-03 15:40:59 +0800460 BT_DBG("Created hcon %p: loc:%u -> rem:%u", hcon, hdev->id, rsp->id);
Andrei Emeltchenko9a5e94d2012-09-27 17:26:15 +0300461
Andrei Emeltchenkofffadc02012-11-01 15:37:03 +0200462 mgr->bredr_chan->remote_amp_id = rsp->id;
Andrei Emeltchenko9495b2e2012-09-27 17:26:22 +0300463
Andrei Emeltchenkoa02226d2012-09-27 17:26:19 +0300464 amp_create_phylink(hdev, mgr, hcon);
465
Andrei Emeltchenko9a5e94d2012-09-27 17:26:15 +0300466done:
467 hci_dev_put(hdev);
468 skb_pull(skb, len);
469 return 0;
470}
471
Andrei Emeltchenkoe072f5d2012-05-29 13:59:13 +0300472static int a2mp_createphyslink_req(struct amp_mgr *mgr, struct sk_buff *skb,
473 struct a2mp_cmd *hdr)
474{
475 struct a2mp_physlink_req *req = (void *) skb->data;
Andrei Emeltchenkoe072f5d2012-05-29 13:59:13 +0300476 struct a2mp_physlink_rsp rsp;
477 struct hci_dev *hdev;
Andrei Emeltchenkocb8488c2012-09-27 17:26:13 +0300478 struct hci_conn *hcon;
Andrei Emeltchenko0b26ab92012-09-27 17:26:24 +0300479 struct amp_ctrl *ctrl;
Andrei Emeltchenkoe072f5d2012-05-29 13:59:13 +0300480
481 if (le16_to_cpu(hdr->len) < sizeof(*req))
482 return -EINVAL;
483
Kai Yefad48d82021-06-03 15:40:59 +0800484 BT_DBG("local_id %u, remote_id %u", req->local_id, req->remote_id);
Andrei Emeltchenkoe072f5d2012-05-29 13:59:13 +0300485
Luiz Augusto von Dentzeddb7732020-08-06 11:17:11 -0700486 memset(&rsp, 0, sizeof(rsp));
487
Andrei Emeltchenkoe072f5d2012-05-29 13:59:13 +0300488 rsp.local_id = req->remote_id;
489 rsp.remote_id = req->local_id;
490
491 hdev = hci_dev_get(req->remote_id);
Marcel Holtmannece69122013-10-05 11:47:43 -0700492 if (!hdev || hdev->amp_type == AMP_TYPE_BREDR) {
Andrei Emeltchenkoe072f5d2012-05-29 13:59:13 +0300493 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
494 goto send_rsp;
495 }
496
Andrei Emeltchenko0b26ab92012-09-27 17:26:24 +0300497 ctrl = amp_ctrl_lookup(mgr, rsp.remote_id);
498 if (!ctrl) {
Andrei Emeltchenkofa4ebc62012-10-05 16:56:55 +0300499 ctrl = amp_ctrl_add(mgr, rsp.remote_id);
Andrei Emeltchenko0b26ab92012-09-27 17:26:24 +0300500 if (ctrl) {
501 amp_ctrl_get(ctrl);
502 } else {
503 rsp.status = A2MP_STATUS_UNABLE_START_LINK_CREATION;
504 goto send_rsp;
505 }
506 }
507
508 if (ctrl) {
Andrei Emeltchenko13465c02012-09-28 16:55:00 +0300509 size_t assoc_len = le16_to_cpu(hdr->len) - sizeof(*req);
510 u8 *assoc;
Andrei Emeltchenko0b26ab92012-09-27 17:26:24 +0300511
Alexandru Gheorghiu5ae327f2013-03-17 07:16:50 +0200512 assoc = kmemdup(req->amp_assoc, assoc_len, GFP_KERNEL);
Andrei Emeltchenko0b26ab92012-09-27 17:26:24 +0300513 if (!assoc) {
514 amp_ctrl_put(ctrl);
Pan Bian5a3ef032021-01-20 23:34:19 -0800515 hci_dev_put(hdev);
Andrei Emeltchenko0b26ab92012-09-27 17:26:24 +0300516 return -ENOMEM;
517 }
518
Andrei Emeltchenko0b26ab92012-09-27 17:26:24 +0300519 ctrl->assoc = assoc;
520 ctrl->assoc_len = assoc_len;
521 ctrl->assoc_rem_len = assoc_len;
522 ctrl->assoc_len_so_far = 0;
523
524 amp_ctrl_put(ctrl);
525 }
526
Andrei Emeltchenkoa0c234f2012-10-05 16:56:56 +0300527 hcon = phylink_add(hdev, mgr, req->local_id, false);
Andrei Emeltchenkocb8488c2012-09-27 17:26:13 +0300528 if (hcon) {
Andrei Emeltchenkodffa3872012-09-27 17:26:23 +0300529 amp_accept_phylink(hdev, mgr, hcon);
Andrei Emeltchenkocb8488c2012-09-27 17:26:13 +0300530 rsp.status = A2MP_STATUS_SUCCESS;
531 } else {
532 rsp.status = A2MP_STATUS_UNABLE_START_LINK_CREATION;
533 }
Andrei Emeltchenkoe072f5d2012-05-29 13:59:13 +0300534
535send_rsp:
536 if (hdev)
537 hci_dev_put(hdev);
538
Andrei Emeltchenko8e05e3b2012-12-07 14:59:05 +0200539 /* Reply error now and success after HCI Write Remote AMP Assoc
540 command complete with success status
541 */
542 if (rsp.status != A2MP_STATUS_SUCCESS) {
543 a2mp_send(mgr, A2MP_CREATEPHYSLINK_RSP, hdr->ident,
544 sizeof(rsp), &rsp);
545 } else {
Andrei Emeltchenkocb6801c62012-12-07 14:59:08 +0200546 set_bit(WRITE_REMOTE_AMP_ASSOC, &mgr->state);
Andrei Emeltchenko8e05e3b2012-12-07 14:59:05 +0200547 mgr->ident = hdr->ident;
548 }
Andrei Emeltchenkoe072f5d2012-05-29 13:59:13 +0300549
550 skb_pull(skb, le16_to_cpu(hdr->len));
551 return 0;
552}
553
Andrei Emeltchenko6113f842012-05-29 13:59:14 +0300554static int a2mp_discphyslink_req(struct amp_mgr *mgr, struct sk_buff *skb,
555 struct a2mp_cmd *hdr)
556{
557 struct a2mp_physlink_req *req = (void *) skb->data;
558 struct a2mp_physlink_rsp rsp;
559 struct hci_dev *hdev;
Andrei Emeltchenkocb8488c2012-09-27 17:26:13 +0300560 struct hci_conn *hcon;
Andrei Emeltchenko6113f842012-05-29 13:59:14 +0300561
562 if (le16_to_cpu(hdr->len) < sizeof(*req))
563 return -EINVAL;
564
Kai Yefad48d82021-06-03 15:40:59 +0800565 BT_DBG("local_id %u remote_id %u", req->local_id, req->remote_id);
Andrei Emeltchenko6113f842012-05-29 13:59:14 +0300566
Luiz Augusto von Dentzeddb7732020-08-06 11:17:11 -0700567 memset(&rsp, 0, sizeof(rsp));
568
Andrei Emeltchenko6113f842012-05-29 13:59:14 +0300569 rsp.local_id = req->remote_id;
570 rsp.remote_id = req->local_id;
571 rsp.status = A2MP_STATUS_SUCCESS;
572
Andrei Emeltchenkocb8488c2012-09-27 17:26:13 +0300573 hdev = hci_dev_get(req->remote_id);
Andrei Emeltchenko6113f842012-05-29 13:59:14 +0300574 if (!hdev) {
575 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
576 goto send_rsp;
577 }
578
Marcel Holtmannbdc8ead22013-10-13 02:23:38 -0700579 hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK,
580 &mgr->l2cap_conn->hcon->dst);
Andrei Emeltchenkocb8488c2012-09-27 17:26:13 +0300581 if (!hcon) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100582 bt_dev_err(hdev, "no phys link exist");
Andrei Emeltchenkocb8488c2012-09-27 17:26:13 +0300583 rsp.status = A2MP_STATUS_NO_PHYSICAL_LINK_EXISTS;
584 goto clean;
585 }
586
Andrei Emeltchenko6113f842012-05-29 13:59:14 +0300587 /* TODO Disconnect Phys Link here */
588
Andrei Emeltchenkocb8488c2012-09-27 17:26:13 +0300589clean:
Andrei Emeltchenko6113f842012-05-29 13:59:14 +0300590 hci_dev_put(hdev);
591
592send_rsp:
593 a2mp_send(mgr, A2MP_DISCONNPHYSLINK_RSP, hdr->ident, sizeof(rsp), &rsp);
594
595 skb_pull(skb, sizeof(*req));
596 return 0;
597}
598
Andrei Emeltchenkof6410a82012-05-29 13:59:15 +0300599static inline int a2mp_cmd_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
600 struct a2mp_cmd *hdr)
601{
Kai Yefad48d82021-06-03 15:40:59 +0800602 BT_DBG("ident %u code 0x%2.2x", hdr->ident, hdr->code);
Andrei Emeltchenkof6410a82012-05-29 13:59:15 +0300603
604 skb_pull(skb, le16_to_cpu(hdr->len));
605 return 0;
606}
607
Andrei Emeltchenko6b44d9b2012-05-29 13:59:07 +0300608/* Handle A2MP signalling */
609static int a2mp_chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
610{
Andrei Emeltchenkod9fc1d52012-07-19 17:03:46 +0300611 struct a2mp_cmd *hdr;
Andrei Emeltchenko6b44d9b2012-05-29 13:59:07 +0300612 struct amp_mgr *mgr = chan->data;
613 int err = 0;
614
615 amp_mgr_get(mgr);
616
617 while (skb->len >= sizeof(*hdr)) {
Andrei Emeltchenkod9fc1d52012-07-19 17:03:46 +0300618 u16 len;
619
620 hdr = (void *) skb->data;
621 len = le16_to_cpu(hdr->len);
Andrei Emeltchenko6b44d9b2012-05-29 13:59:07 +0300622
Kai Yefad48d82021-06-03 15:40:59 +0800623 BT_DBG("code 0x%2.2x id %u len %u", hdr->code, hdr->ident, len);
Andrei Emeltchenko6b44d9b2012-05-29 13:59:07 +0300624
625 skb_pull(skb, sizeof(*hdr));
626
627 if (len > skb->len || !hdr->ident) {
628 err = -EINVAL;
629 break;
630 }
631
632 mgr->ident = hdr->ident;
633
634 switch (hdr->code) {
635 case A2MP_COMMAND_REJ:
Andrei Emeltchenko21dbd2c2012-05-29 13:59:08 +0300636 a2mp_command_rej(mgr, skb, hdr);
637 break;
638
Andrei Emeltchenko6b44d9b2012-05-29 13:59:07 +0300639 case A2MP_DISCOVER_REQ:
Andrei Emeltchenko8598d062012-05-29 13:59:09 +0300640 err = a2mp_discover_req(mgr, skb, hdr);
641 break;
642
Andrei Emeltchenko6b44d9b2012-05-29 13:59:07 +0300643 case A2MP_CHANGE_NOTIFY:
Andrei Emeltchenko329d81af2012-05-29 13:59:10 +0300644 err = a2mp_change_notify(mgr, skb, hdr);
645 break;
646
Andrei Emeltchenko6b44d9b2012-05-29 13:59:07 +0300647 case A2MP_GETINFO_REQ:
Andrei Emeltchenko47f2d972012-05-29 13:59:11 +0300648 err = a2mp_getinfo_req(mgr, skb, hdr);
649 break;
650
Andrei Emeltchenko6b44d9b2012-05-29 13:59:07 +0300651 case A2MP_GETAMPASSOC_REQ:
Andrei Emeltchenkoa28381dc2012-05-29 13:59:12 +0300652 err = a2mp_getampassoc_req(mgr, skb, hdr);
653 break;
654
Andrei Emeltchenko6b44d9b2012-05-29 13:59:07 +0300655 case A2MP_CREATEPHYSLINK_REQ:
Andrei Emeltchenkoe072f5d2012-05-29 13:59:13 +0300656 err = a2mp_createphyslink_req(mgr, skb, hdr);
657 break;
658
Andrei Emeltchenko6b44d9b2012-05-29 13:59:07 +0300659 case A2MP_DISCONNPHYSLINK_REQ:
Andrei Emeltchenko6113f842012-05-29 13:59:14 +0300660 err = a2mp_discphyslink_req(mgr, skb, hdr);
661 break;
662
Andrei Emeltchenko6b44d9b2012-05-29 13:59:07 +0300663 case A2MP_DISCOVER_RSP:
Andrei Emeltchenkoaa095372012-09-27 17:26:10 +0300664 err = a2mp_discover_rsp(mgr, skb, hdr);
665 break;
666
Andrei Emeltchenko6b44d9b2012-05-29 13:59:07 +0300667 case A2MP_GETINFO_RSP:
Andrei Emeltchenko0d868de2012-09-27 17:26:14 +0300668 err = a2mp_getinfo_rsp(mgr, skb, hdr);
669 break;
670
Andrei Emeltchenko6b44d9b2012-05-29 13:59:07 +0300671 case A2MP_GETAMPASSOC_RSP:
Andrei Emeltchenko9a5e94d2012-09-27 17:26:15 +0300672 err = a2mp_getampassoc_rsp(mgr, skb, hdr);
673 break;
674
675 case A2MP_CHANGE_RSP:
Andrei Emeltchenko6b44d9b2012-05-29 13:59:07 +0300676 case A2MP_CREATEPHYSLINK_RSP:
677 case A2MP_DISCONNPHYSLINK_RSP:
Andrei Emeltchenkof6410a82012-05-29 13:59:15 +0300678 err = a2mp_cmd_rsp(mgr, skb, hdr);
679 break;
680
Andrei Emeltchenko6b44d9b2012-05-29 13:59:07 +0300681 default:
682 BT_ERR("Unknown A2MP sig cmd 0x%2.2x", hdr->code);
683 err = -EINVAL;
684 break;
685 }
686 }
687
688 if (err) {
689 struct a2mp_cmd_rej rej;
Andrei Emeltchenkod9fc1d52012-07-19 17:03:46 +0300690
Luiz Augusto von Dentzeddb7732020-08-06 11:17:11 -0700691 memset(&rej, 0, sizeof(rej));
692
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700693 rej.reason = cpu_to_le16(0);
Andrei Emeltchenkod9fc1d52012-07-19 17:03:46 +0300694 hdr = (void *) skb->data;
Andrei Emeltchenko6b44d9b2012-05-29 13:59:07 +0300695
696 BT_DBG("Send A2MP Rej: cmd 0x%2.2x err %d", hdr->code, err);
697
698 a2mp_send(mgr, A2MP_COMMAND_REJ, hdr->ident, sizeof(rej),
699 &rej);
700 }
701
702 /* Always free skb and return success error code to prevent
703 from sending L2CAP Disconnect over A2MP channel */
704 kfree_skb(skb);
705
706 amp_mgr_put(mgr);
707
708 return 0;
709}
710
Andrei Emeltchenko46d5c902012-05-29 13:59:04 +0300711static void a2mp_chan_close_cb(struct l2cap_chan *chan)
712{
Jaganath Kanakkassery4af66c62012-07-13 18:17:55 +0530713 l2cap_chan_put(chan);
Andrei Emeltchenko46d5c902012-05-29 13:59:04 +0300714}
715
Gustavo Padovan53f52122013-10-15 19:24:45 -0300716static void a2mp_chan_state_change_cb(struct l2cap_chan *chan, int state,
717 int err)
Andrei Emeltchenko46d5c902012-05-29 13:59:04 +0300718{
719 struct amp_mgr *mgr = chan->data;
720
721 if (!mgr)
722 return;
723
724 BT_DBG("chan %p state %s", chan, state_to_string(state));
725
726 chan->state = state;
727
728 switch (state) {
729 case BT_CLOSED:
730 if (mgr)
731 amp_mgr_put(mgr);
732 break;
733 }
734}
735
736static struct sk_buff *a2mp_chan_alloc_skb_cb(struct l2cap_chan *chan,
Marcel Holtmannd9fbd022014-06-08 11:22:28 +0200737 unsigned long hdr_len,
Andrei Emeltchenko46d5c902012-05-29 13:59:04 +0300738 unsigned long len, int nb)
739{
Gustavo Padovan0753c182012-12-22 01:22:53 -0200740 struct sk_buff *skb;
741
Marcel Holtmannd9fbd022014-06-08 11:22:28 +0200742 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
Gustavo Padovan0753c182012-12-22 01:22:53 -0200743 if (!skb)
744 return ERR_PTR(-ENOMEM);
745
746 return skb;
Andrei Emeltchenko46d5c902012-05-29 13:59:04 +0300747}
748
Marcel Holtmann67f86a42014-06-08 10:05:31 +0200749static const struct l2cap_ops a2mp_chan_ops = {
Andrei Emeltchenko466f8002012-05-29 13:59:01 +0300750 .name = "L2CAP A2MP channel",
Andrei Emeltchenko6b44d9b2012-05-29 13:59:07 +0300751 .recv = a2mp_chan_recv_cb,
Andrei Emeltchenko46d5c902012-05-29 13:59:04 +0300752 .close = a2mp_chan_close_cb,
753 .state_change = a2mp_chan_state_change_cb,
754 .alloc_skb = a2mp_chan_alloc_skb_cb,
755
756 /* Not implemented for A2MP */
Gustavo Padovan7e1af8a2012-05-29 13:19:26 -0300757 .new_connection = l2cap_chan_no_new_connection,
758 .teardown = l2cap_chan_no_teardown,
759 .ready = l2cap_chan_no_ready,
Gustavo Padovan2dc4e512012-10-12 19:35:24 +0800760 .defer = l2cap_chan_no_defer,
Marcel Holtmann2ce5fb52013-10-15 16:47:11 -0700761 .resume = l2cap_chan_no_resume,
Gustavo Padovan5ec1bbe2013-10-15 19:24:48 -0300762 .set_shutdown = l2cap_chan_no_set_shutdown,
Gustavo Padovan8d836d72013-10-15 19:24:47 -0300763 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Andrei Emeltchenko466f8002012-05-29 13:59:01 +0300764};
765
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +0300766static struct l2cap_chan *a2mp_chan_open(struct l2cap_conn *conn, bool locked)
Andrei Emeltchenko466f8002012-05-29 13:59:01 +0300767{
768 struct l2cap_chan *chan;
769 int err;
770
771 chan = l2cap_chan_create();
772 if (!chan)
773 return NULL;
774
775 BT_DBG("chan %p", chan);
776
Johan Hedberg2338a7e2014-01-24 10:35:40 +0200777 chan->chan_type = L2CAP_CHAN_FIXED;
778 chan->scid = L2CAP_CID_A2MP;
779 chan->dcid = L2CAP_CID_A2MP;
780 chan->omtu = L2CAP_A2MP_DEFAULT_MTU;
781 chan->imtu = L2CAP_A2MP_DEFAULT_MTU;
Andrei Emeltchenko466f8002012-05-29 13:59:01 +0300782 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
783
784 chan->ops = &a2mp_chan_ops;
785
786 l2cap_chan_set_defaults(chan);
787 chan->remote_max_tx = chan->max_tx;
788 chan->remote_tx_win = chan->tx_win;
789
790 chan->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO;
791 chan->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO;
792
793 skb_queue_head_init(&chan->tx_q);
794
795 chan->mode = L2CAP_MODE_ERTM;
796
797 err = l2cap_ertm_init(chan);
798 if (err < 0) {
799 l2cap_chan_del(chan, 0);
800 return NULL;
801 }
802
803 chan->conf_state = 0;
804
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +0300805 if (locked)
806 __l2cap_chan_add(conn, chan);
807 else
808 l2cap_chan_add(conn, chan);
Andrei Emeltchenko466f8002012-05-29 13:59:01 +0300809
810 chan->remote_mps = chan->omtu;
811 chan->mps = chan->omtu;
812
813 chan->state = BT_CONNECTED;
814
815 return chan;
816}
Andrei Emeltchenko9740e492012-05-29 13:59:02 +0300817
818/* AMP Manager functions */
Andrei Emeltchenkof706adf2012-10-18 13:16:19 +0300819struct amp_mgr *amp_mgr_get(struct amp_mgr *mgr)
Andrei Emeltchenko9740e492012-05-29 13:59:02 +0300820{
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100821 BT_DBG("mgr %p orig refcnt %d", mgr, kref_read(&mgr->kref));
Andrei Emeltchenko9740e492012-05-29 13:59:02 +0300822
823 kref_get(&mgr->kref);
Andrei Emeltchenkof706adf2012-10-18 13:16:19 +0300824
825 return mgr;
Andrei Emeltchenko9740e492012-05-29 13:59:02 +0300826}
827
828static void amp_mgr_destroy(struct kref *kref)
829{
830 struct amp_mgr *mgr = container_of(kref, struct amp_mgr, kref);
831
832 BT_DBG("mgr %p", mgr);
833
Andrei Emeltchenkof97268f2012-09-27 17:26:07 +0300834 mutex_lock(&amp_mgr_list_lock);
835 list_del(&mgr->list);
836 mutex_unlock(&amp_mgr_list_lock);
837
Andrei Emeltchenko52c0d6e2012-09-27 17:26:12 +0300838 amp_ctrl_list_flush(mgr);
Andrei Emeltchenko9740e492012-05-29 13:59:02 +0300839 kfree(mgr);
840}
841
842int amp_mgr_put(struct amp_mgr *mgr)
843{
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100844 BT_DBG("mgr %p orig refcnt %d", mgr, kref_read(&mgr->kref));
Andrei Emeltchenko9740e492012-05-29 13:59:02 +0300845
846 return kref_put(&mgr->kref, &amp_mgr_destroy);
847}
848
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +0300849static struct amp_mgr *amp_mgr_create(struct l2cap_conn *conn, bool locked)
Andrei Emeltchenko9740e492012-05-29 13:59:02 +0300850{
851 struct amp_mgr *mgr;
852 struct l2cap_chan *chan;
853
854 mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
855 if (!mgr)
856 return NULL;
857
858 BT_DBG("conn %p mgr %p", conn, mgr);
859
860 mgr->l2cap_conn = conn;
861
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +0300862 chan = a2mp_chan_open(conn, locked);
Andrei Emeltchenko9740e492012-05-29 13:59:02 +0300863 if (!chan) {
864 kfree(mgr);
865 return NULL;
866 }
867
868 mgr->a2mp_chan = chan;
869 chan->data = mgr;
870
871 conn->hcon->amp_mgr = mgr;
872
873 kref_init(&mgr->kref);
874
Andrei Emeltchenko52c0d6e2012-09-27 17:26:12 +0300875 /* Remote AMP ctrl list initialization */
876 INIT_LIST_HEAD(&mgr->amp_ctrls);
877 mutex_init(&mgr->amp_ctrls_lock);
878
Andrei Emeltchenkof97268f2012-09-27 17:26:07 +0300879 mutex_lock(&amp_mgr_list_lock);
880 list_add(&mgr->list, &amp_mgr_list);
881 mutex_unlock(&amp_mgr_list_lock);
882
Andrei Emeltchenko9740e492012-05-29 13:59:02 +0300883 return mgr;
884}
Andrei Emeltchenko97e8e892012-05-29 13:59:17 +0300885
886struct l2cap_chan *a2mp_channel_create(struct l2cap_conn *conn,
887 struct sk_buff *skb)
888{
889 struct amp_mgr *mgr;
890
Johan Hedberg07e307f2013-10-16 11:37:00 +0300891 if (conn->hcon->type != ACL_LINK)
892 return NULL;
893
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +0300894 mgr = amp_mgr_create(conn, false);
Andrei Emeltchenko97e8e892012-05-29 13:59:17 +0300895 if (!mgr) {
896 BT_ERR("Could not create AMP manager");
897 return NULL;
898 }
899
900 BT_DBG("mgr: %p chan %p", mgr, mgr->a2mp_chan);
901
902 return mgr->a2mp_chan;
903}
Andrei Emeltchenkof97268f2012-09-27 17:26:07 +0300904
Andrei Emeltchenko8e2a0d92012-09-27 17:26:08 +0300905void a2mp_send_getinfo_rsp(struct hci_dev *hdev)
906{
907 struct amp_mgr *mgr;
908 struct a2mp_info_rsp rsp;
909
910 mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_INFO);
911 if (!mgr)
912 return;
913
914 BT_DBG("%s mgr %p", hdev->name, mgr);
915
Luiz Augusto von Dentzeddb7732020-08-06 11:17:11 -0700916 memset(&rsp, 0, sizeof(rsp));
917
Andrei Emeltchenko8e2a0d92012-09-27 17:26:08 +0300918 rsp.id = hdev->id;
919 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
920
Marcel Holtmannece69122013-10-05 11:47:43 -0700921 if (hdev->amp_type != AMP_TYPE_BREDR) {
Andrei Emeltchenko8e2a0d92012-09-27 17:26:08 +0300922 rsp.status = 0;
923 rsp.total_bw = cpu_to_le32(hdev->amp_total_bw);
924 rsp.max_bw = cpu_to_le32(hdev->amp_max_bw);
925 rsp.min_latency = cpu_to_le32(hdev->amp_min_latency);
926 rsp.pal_cap = cpu_to_le16(hdev->amp_pal_cap);
927 rsp.assoc_size = cpu_to_le16(hdev->amp_assoc_size);
928 }
929
930 a2mp_send(mgr, A2MP_GETINFO_RSP, mgr->ident, sizeof(rsp), &rsp);
931 amp_mgr_put(mgr);
932}
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300933
934void a2mp_send_getampassoc_rsp(struct hci_dev *hdev, u8 status)
935{
936 struct amp_mgr *mgr;
937 struct amp_assoc *loc_assoc = &hdev->loc_assoc;
938 struct a2mp_amp_assoc_rsp *rsp;
939 size_t len;
940
941 mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC);
942 if (!mgr)
943 return;
944
945 BT_DBG("%s mgr %p", hdev->name, mgr);
946
947 len = sizeof(struct a2mp_amp_assoc_rsp) + loc_assoc->len;
948 rsp = kzalloc(len, GFP_KERNEL);
949 if (!rsp) {
950 amp_mgr_put(mgr);
951 return;
952 }
953
954 rsp->id = hdev->id;
955
956 if (status) {
957 rsp->status = A2MP_STATUS_INVALID_CTRL_ID;
958 } else {
959 rsp->status = A2MP_STATUS_SUCCESS;
960 memcpy(rsp->amp_assoc, loc_assoc->data, loc_assoc->len);
961 }
962
963 a2mp_send(mgr, A2MP_GETAMPASSOC_RSP, mgr->ident, len, rsp);
964 amp_mgr_put(mgr);
965 kfree(rsp);
966}
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +0300967
Andrei Emeltchenko9495b2e2012-09-27 17:26:22 +0300968void a2mp_send_create_phy_link_req(struct hci_dev *hdev, u8 status)
969{
970 struct amp_mgr *mgr;
971 struct amp_assoc *loc_assoc = &hdev->loc_assoc;
972 struct a2mp_physlink_req *req;
973 struct l2cap_chan *bredr_chan;
974 size_t len;
975
976 mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC_FINAL);
977 if (!mgr)
978 return;
979
980 len = sizeof(*req) + loc_assoc->len;
981
982 BT_DBG("%s mgr %p assoc_len %zu", hdev->name, mgr, len);
983
984 req = kzalloc(len, GFP_KERNEL);
985 if (!req) {
986 amp_mgr_put(mgr);
987 return;
988 }
989
990 bredr_chan = mgr->bredr_chan;
991 if (!bredr_chan)
992 goto clean;
993
994 req->local_id = hdev->id;
Andrei Emeltchenkofffadc02012-11-01 15:37:03 +0200995 req->remote_id = bredr_chan->remote_amp_id;
Andrei Emeltchenko9495b2e2012-09-27 17:26:22 +0300996 memcpy(req->amp_assoc, loc_assoc->data, loc_assoc->len);
997
998 a2mp_send(mgr, A2MP_CREATEPHYSLINK_REQ, __next_ident(mgr), len, req);
999
1000clean:
1001 amp_mgr_put(mgr);
1002 kfree(req);
1003}
1004
Andrei Emeltchenko8e05e3b2012-12-07 14:59:05 +02001005void a2mp_send_create_phy_link_rsp(struct hci_dev *hdev, u8 status)
1006{
1007 struct amp_mgr *mgr;
1008 struct a2mp_physlink_rsp rsp;
1009 struct hci_conn *hs_hcon;
1010
1011 mgr = amp_mgr_lookup_by_state(WRITE_REMOTE_AMP_ASSOC);
1012 if (!mgr)
1013 return;
1014
Luiz Augusto von Dentzeddb7732020-08-06 11:17:11 -07001015 memset(&rsp, 0, sizeof(rsp));
1016
Andrei Emeltchenko8e05e3b2012-12-07 14:59:05 +02001017 hs_hcon = hci_conn_hash_lookup_state(hdev, AMP_LINK, BT_CONNECT);
1018 if (!hs_hcon) {
1019 rsp.status = A2MP_STATUS_UNABLE_START_LINK_CREATION;
1020 } else {
1021 rsp.remote_id = hs_hcon->remote_id;
1022 rsp.status = A2MP_STATUS_SUCCESS;
1023 }
1024
1025 BT_DBG("%s mgr %p hs_hcon %p status %u", hdev->name, mgr, hs_hcon,
1026 status);
1027
1028 rsp.local_id = hdev->id;
1029 a2mp_send(mgr, A2MP_CREATEPHYSLINK_RSP, mgr->ident, sizeof(rsp), &rsp);
1030 amp_mgr_put(mgr);
1031}
1032
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001033void a2mp_discover_amp(struct l2cap_chan *chan)
1034{
1035 struct l2cap_conn *conn = chan->conn;
1036 struct amp_mgr *mgr = conn->hcon->amp_mgr;
1037 struct a2mp_discov_req req;
1038
1039 BT_DBG("chan %p conn %p mgr %p", chan, conn, mgr);
1040
1041 if (!mgr) {
1042 mgr = amp_mgr_create(conn, true);
1043 if (!mgr)
1044 return;
1045 }
1046
1047 mgr->bredr_chan = chan;
1048
Luiz Augusto von Dentzeddb7732020-08-06 11:17:11 -07001049 memset(&req, 0, sizeof(req));
1050
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001051 req.mtu = cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU);
1052 req.ext_feat = 0;
1053 a2mp_send(mgr, A2MP_DISCOVER_REQ, 1, sizeof(req), &req);
1054}