]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/bluetooth/a2mp.c
Merge branch 'rework/printk_safe-removal' into for-linus
[mirror_ubuntu-jammy-kernel.git] / net / bluetooth / a2mp.c
CommitLineData
97fb5e8d 1// SPDX-License-Identifier: GPL-2.0-only
466f8004
AE
2/*
3 Copyright (c) 2010,2011 Code Aurora Forum. All rights reserved.
4 Copyright (c) 2011,2012 Intel Corp.
5
466f8004
AE
6*/
7
8#include <net/bluetooth/bluetooth.h>
9#include <net/bluetooth/hci_core.h>
10#include <net/bluetooth/l2cap.h>
7ef9fbf0 11
83927882 12#include "hci_request.h"
7024728e 13#include "a2mp.h"
7ef9fbf0 14#include "amp.h"
466f8004 15
055540a1
MH
16#define A2MP_FEAT_EXT 0x8000
17
f97268fc 18/* Global AMP Manager list */
59d4d086
MH
19static LIST_HEAD(amp_mgr_list);
20static DEFINE_MUTEX(amp_mgr_list_lock);
f97268fc 21
f6d3c6e7
AE
22/* 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
bc333cc4 42static void a2mp_send(struct amp_mgr *mgr, u8 code, u8 ident, u16 len, void *data)
f6d3c6e7
AE
43{
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
aa563d7b 59 iov_iter_kvec(&msg.msg_iter, WRITE, &iv, 1, total_len);
f6d3c6e7 60
8d46321c 61 l2cap_chan_send(chan, &msg, total_len);
f6d3c6e7
AE
62
63 kfree(cmd);
64}
65
87e2a020 66static u8 __next_ident(struct amp_mgr *mgr)
aa09537d
AE
67{
68 if (++mgr->ident == 0)
69 mgr->ident = 1;
70
71 return mgr->ident;
72}
73
469cd4c5
MH
74static 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
8598d064 91/* hci_dev_list shall be locked */
23f0cb41 92static void __a2mp_add_cl(struct amp_mgr *mgr, struct a2mp_cl *cl)
8598d064 93{
8598d064 94 struct hci_dev *hdev;
e8803534 95 int i = 1;
8598d064 96
346e7099
MH
97 cl[0].id = AMP_ID_BREDR;
98 cl[0].type = AMP_TYPE_BREDR;
99 cl[0].status = AMP_STATUS_BLUETOOTH_ONLY;
8598d064
AE
100
101 list_for_each_entry(hdev, &hci_dev_list, list) {
e8803534
MH
102 if (hdev->dev_type == HCI_AMP) {
103 cl[i].id = hdev->id;
104 cl[i].type = hdev->amp_type;
cd0a85c2
MH
105 if (test_bit(HCI_UP, &hdev->flags))
106 cl[i].status = hdev->amp_status;
107 else
108 cl[i].status = AMP_STATUS_POWERED_DOWN;
e8803534
MH
109 i++;
110 }
8598d064
AE
111 }
112}
113
21dbd2ce
AE
114/* 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
fad48d84 123 BT_DBG("ident %u reason %d", hdr->ident, le16_to_cpu(rej->reason));
21dbd2ce
AE
124
125 skb_pull(skb, sizeof(*rej));
126
127 return 0;
128}
129
8598d064
AE
130static 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;
f822c411 138 struct hci_dev *hdev;
8598d064
AE
139
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
f822c411
MH
162 /* 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
3c97ce1f 170 len = struct_size(rsp, cl, num_ctrl);
8598d064
AE
171 rsp = kmalloc(len, GFP_ATOMIC);
172 if (!rsp) {
173 read_unlock(&hci_dev_list_lock);
174 return -ENOMEM;
175 }
176
dcf4adbf 177 rsp->mtu = cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU);
8598d064
AE
178 rsp->ext_feat = 0;
179
23f0cb41 180 __a2mp_add_cl(mgr, rsp->cl);
8598d064
AE
181
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
aa09537d
AE
190static 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;
2766be48 197 bool found = false;
aa09537d
AE
198
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)) {
fad48d84 222 BT_DBG("Remote AMP id %u type %u status %u", cl->id, cl->type,
aa09537d
AE
223 cl->status);
224
a646bd81 225 if (cl->id != AMP_ID_BREDR && cl->type != AMP_TYPE_BREDR) {
aa09537d
AE
226 struct a2mp_info_req req;
227
2766be48 228 found = true;
eddb7732
LAD
229
230 memset(&req, 0, sizeof(req));
231
aa09537d
AE
232 req.id = cl->id;
233 a2mp_send(mgr, A2MP_GETINFO_REQ, __next_ident(mgr),
234 sizeof(req), &req);
235 }
236
237 len -= sizeof(*cl);
af72868b 238 cl = skb_pull(skb, sizeof(*cl));
aa09537d
AE
239 }
240
2766be48
AE
241 /* 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
2338a7e0 253 if (chan->scid == L2CAP_CID_A2MP)
2766be48
AE
254 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
aa09537d
AE
267 return 0;
268}
269
329d81af
AE
270static 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)) {
fad48d84 276 BT_DBG("Controller id %u type %u status %u", cl->id, cl->type,
329d81af 277 cl->status);
af72868b 278 cl = skb_pull(skb, sizeof(*cl));
329d81af
AE
279 }
280
281 /* TODO send A2MP_CHANGE_RSP */
282
283 return 0;
284}
285
83927882
AW
286static 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
47f2d97d
AE
294static 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;
47f2d97d 298 struct hci_dev *hdev;
83927882
AW
299 struct hci_request hreq;
300 int err = 0;
47f2d97d
AE
301
302 if (le16_to_cpu(hdr->len) < sizeof(*req))
303 return -EINVAL;
304
fad48d84 305 BT_DBG("id %u", req->id);
47f2d97d 306
47f2d97d 307 hdev = hci_dev_get(req->id);
bc8dce4f 308 if (!hdev || hdev->dev_type != HCI_AMP) {
8e2a0d92
AE
309 struct a2mp_info_rsp rsp;
310
eddb7732
LAD
311 memset(&rsp, 0, sizeof(rsp));
312
8e2a0d92
AE
313 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);
47f2d97d 318
bc8dce4f 319 goto done;
8e2a0d92 320 }
47f2d97d 321
cb6801c6 322 set_bit(READ_LOC_AMP_INFO, &mgr->state);
83927882
AW
323 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);
bc8dce4f
AE
328
329done:
330 if (hdev)
331 hci_dev_put(hdev);
47f2d97d
AE
332
333 skb_pull(skb, sizeof(*req));
334 return 0;
335}
336
0d868de9
AE
337static 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
fad48d84 347 BT_DBG("id %u status 0x%2.2x", rsp->id, rsp->status);
0d868de9
AE
348
349 if (rsp->status)
350 return -EINVAL;
351
fa4ebc66 352 ctrl = amp_ctrl_add(mgr, rsp->id);
0d868de9
AE
353 if (!ctrl)
354 return -ENOMEM;
355
eddb7732
LAD
356 memset(&req, 0, sizeof(req));
357
0d868de9
AE
358 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
a28381dc
AE
366static 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;
903e4541 371 struct amp_mgr *tmp;
a28381dc
AE
372
373 if (le16_to_cpu(hdr->len) < sizeof(*req))
374 return -EINVAL;
375
fad48d84 376 BT_DBG("id %u", req->id);
a28381dc 377
903e4541
AE
378 /* Make sure that other request is not processed */
379 tmp = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC);
380
a28381dc 381 hdev = hci_dev_get(req->id);
ece69126 382 if (!hdev || hdev->amp_type == AMP_TYPE_BREDR || tmp) {
a28381dc 383 struct a2mp_amp_assoc_rsp rsp;
903e4541 384
eddb7732 385 memset(&rsp, 0, sizeof(rsp));
a5687c64 386 rsp.id = req->id;
eddb7732 387
903e4541
AE
388 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 }
a28381dc
AE
394
395 a2mp_send(mgr, A2MP_GETAMPASSOC_RSP, hdr->ident, sizeof(rsp),
396 &rsp);
903e4541
AE
397
398 goto done;
a28381dc
AE
399 }
400
903e4541 401 amp_read_loc_assoc(hdev, mgr);
a28381dc 402
903e4541 403done:
a28381dc
AE
404 if (hdev)
405 hci_dev_put(hdev);
406
407 skb_pull(skb, sizeof(*req));
408 return 0;
409}
410
9a5e94db
AE
411static 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;
13465c0a 419 size_t assoc_len;
9a5e94db
AE
420
421 if (len < sizeof(*rsp))
422 return -EINVAL;
423
13465c0a
AE
424 assoc_len = len - sizeof(*rsp);
425
fad48d84 426 BT_DBG("id %u status 0x%2.2x assoc len %zu", rsp->id, rsp->status,
13465c0a 427 assoc_len);
9a5e94db
AE
428
429 if (rsp->status)
430 return -EINVAL;
431
432 /* Save remote ASSOC data */
433 ctrl = amp_ctrl_lookup(mgr, rsp->id);
434 if (ctrl) {
13465c0a 435 u8 *assoc;
9a5e94db 436
5ae327f0 437 assoc = kmemdup(rsp->amp_assoc, assoc_len, GFP_KERNEL);
9a5e94db
AE
438 if (!assoc) {
439 amp_ctrl_put(ctrl);
440 return -ENOMEM;
441 }
442
9a5e94db
AE
443 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
a0c234fe 456 hcon = phylink_add(hdev, mgr, rsp->id, true);
9a5e94db
AE
457 if (!hcon)
458 goto done;
459
fad48d84 460 BT_DBG("Created hcon %p: loc:%u -> rem:%u", hcon, hdev->id, rsp->id);
9a5e94db 461
fffadc08 462 mgr->bredr_chan->remote_amp_id = rsp->id;
9495b2ee 463
a02226d6
AE
464 amp_create_phylink(hdev, mgr, hcon);
465
9a5e94db
AE
466done:
467 hci_dev_put(hdev);
468 skb_pull(skb, len);
469 return 0;
470}
471
e072f5da
AE
472static 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;
e072f5da
AE
476 struct a2mp_physlink_rsp rsp;
477 struct hci_dev *hdev;
cb8488c0 478 struct hci_conn *hcon;
0b26ab9d 479 struct amp_ctrl *ctrl;
e072f5da
AE
480
481 if (le16_to_cpu(hdr->len) < sizeof(*req))
482 return -EINVAL;
483
fad48d84 484 BT_DBG("local_id %u, remote_id %u", req->local_id, req->remote_id);
e072f5da 485
eddb7732
LAD
486 memset(&rsp, 0, sizeof(rsp));
487
e072f5da
AE
488 rsp.local_id = req->remote_id;
489 rsp.remote_id = req->local_id;
490
491 hdev = hci_dev_get(req->remote_id);
ece69126 492 if (!hdev || hdev->amp_type == AMP_TYPE_BREDR) {
e072f5da
AE
493 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
494 goto send_rsp;
495 }
496
0b26ab9d
AE
497 ctrl = amp_ctrl_lookup(mgr, rsp.remote_id);
498 if (!ctrl) {
fa4ebc66 499 ctrl = amp_ctrl_add(mgr, rsp.remote_id);
0b26ab9d
AE
500 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) {
13465c0a
AE
509 size_t assoc_len = le16_to_cpu(hdr->len) - sizeof(*req);
510 u8 *assoc;
0b26ab9d 511
5ae327f0 512 assoc = kmemdup(req->amp_assoc, assoc_len, GFP_KERNEL);
0b26ab9d
AE
513 if (!assoc) {
514 amp_ctrl_put(ctrl);
5a3ef03a 515 hci_dev_put(hdev);
0b26ab9d
AE
516 return -ENOMEM;
517 }
518
0b26ab9d
AE
519 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
a0c234fe 527 hcon = phylink_add(hdev, mgr, req->local_id, false);
cb8488c0 528 if (hcon) {
dffa3871 529 amp_accept_phylink(hdev, mgr, hcon);
cb8488c0
AE
530 rsp.status = A2MP_STATUS_SUCCESS;
531 } else {
532 rsp.status = A2MP_STATUS_UNABLE_START_LINK_CREATION;
533 }
e072f5da
AE
534
535send_rsp:
536 if (hdev)
537 hci_dev_put(hdev);
538
8e05e3ba
AE
539 /* 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 {
cb6801c6 546 set_bit(WRITE_REMOTE_AMP_ASSOC, &mgr->state);
8e05e3ba
AE
547 mgr->ident = hdr->ident;
548 }
e072f5da
AE
549
550 skb_pull(skb, le16_to_cpu(hdr->len));
551 return 0;
552}
553
6113f84f
AE
554static 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;
cb8488c0 560 struct hci_conn *hcon;
6113f84f
AE
561
562 if (le16_to_cpu(hdr->len) < sizeof(*req))
563 return -EINVAL;
564
fad48d84 565 BT_DBG("local_id %u remote_id %u", req->local_id, req->remote_id);
6113f84f 566
eddb7732
LAD
567 memset(&rsp, 0, sizeof(rsp));
568
6113f84f
AE
569 rsp.local_id = req->remote_id;
570 rsp.remote_id = req->local_id;
571 rsp.status = A2MP_STATUS_SUCCESS;
572
cb8488c0 573 hdev = hci_dev_get(req->remote_id);
6113f84f
AE
574 if (!hdev) {
575 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
576 goto send_rsp;
577 }
578
bdc8ead2
MH
579 hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK,
580 &mgr->l2cap_conn->hcon->dst);
cb8488c0 581 if (!hcon) {
2064ee33 582 bt_dev_err(hdev, "no phys link exist");
cb8488c0
AE
583 rsp.status = A2MP_STATUS_NO_PHYSICAL_LINK_EXISTS;
584 goto clean;
585 }
586
6113f84f
AE
587 /* TODO Disconnect Phys Link here */
588
cb8488c0 589clean:
6113f84f
AE
590 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
f6410a84
AE
599static inline int a2mp_cmd_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
600 struct a2mp_cmd *hdr)
601{
fad48d84 602 BT_DBG("ident %u code 0x%2.2x", hdr->ident, hdr->code);
f6410a84
AE
603
604 skb_pull(skb, le16_to_cpu(hdr->len));
605 return 0;
606}
607
6b44d9b8
AE
608/* Handle A2MP signalling */
609static int a2mp_chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
610{
d9fc1d54 611 struct a2mp_cmd *hdr;
6b44d9b8
AE
612 struct amp_mgr *mgr = chan->data;
613 int err = 0;
614
615 amp_mgr_get(mgr);
616
617 while (skb->len >= sizeof(*hdr)) {
d9fc1d54
AE
618 u16 len;
619
620 hdr = (void *) skb->data;
621 len = le16_to_cpu(hdr->len);
6b44d9b8 622
fad48d84 623 BT_DBG("code 0x%2.2x id %u len %u", hdr->code, hdr->ident, len);
6b44d9b8
AE
624
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:
21dbd2ce
AE
636 a2mp_command_rej(mgr, skb, hdr);
637 break;
638
6b44d9b8 639 case A2MP_DISCOVER_REQ:
8598d064
AE
640 err = a2mp_discover_req(mgr, skb, hdr);
641 break;
642
6b44d9b8 643 case A2MP_CHANGE_NOTIFY:
329d81af
AE
644 err = a2mp_change_notify(mgr, skb, hdr);
645 break;
646
6b44d9b8 647 case A2MP_GETINFO_REQ:
47f2d97d
AE
648 err = a2mp_getinfo_req(mgr, skb, hdr);
649 break;
650
6b44d9b8 651 case A2MP_GETAMPASSOC_REQ:
a28381dc
AE
652 err = a2mp_getampassoc_req(mgr, skb, hdr);
653 break;
654
6b44d9b8 655 case A2MP_CREATEPHYSLINK_REQ:
e072f5da
AE
656 err = a2mp_createphyslink_req(mgr, skb, hdr);
657 break;
658
6b44d9b8 659 case A2MP_DISCONNPHYSLINK_REQ:
6113f84f
AE
660 err = a2mp_discphyslink_req(mgr, skb, hdr);
661 break;
662
6b44d9b8 663 case A2MP_DISCOVER_RSP:
aa09537d
AE
664 err = a2mp_discover_rsp(mgr, skb, hdr);
665 break;
666
6b44d9b8 667 case A2MP_GETINFO_RSP:
0d868de9
AE
668 err = a2mp_getinfo_rsp(mgr, skb, hdr);
669 break;
670
6b44d9b8 671 case A2MP_GETAMPASSOC_RSP:
9a5e94db
AE
672 err = a2mp_getampassoc_rsp(mgr, skb, hdr);
673 break;
674
675 case A2MP_CHANGE_RSP:
6b44d9b8
AE
676 case A2MP_CREATEPHYSLINK_RSP:
677 case A2MP_DISCONNPHYSLINK_RSP:
f6410a84
AE
678 err = a2mp_cmd_rsp(mgr, skb, hdr);
679 break;
680
6b44d9b8
AE
681 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;
d9fc1d54 690
eddb7732
LAD
691 memset(&rej, 0, sizeof(rej));
692
dcf4adbf 693 rej.reason = cpu_to_le16(0);
d9fc1d54 694 hdr = (void *) skb->data;
6b44d9b8
AE
695
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
46d5c908
AE
711static void a2mp_chan_close_cb(struct l2cap_chan *chan)
712{
4af66c69 713 l2cap_chan_put(chan);
46d5c908
AE
714}
715
53f52121
GP
716static void a2mp_chan_state_change_cb(struct l2cap_chan *chan, int state,
717 int err)
46d5c908
AE
718{
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,
d9fbd02b 737 unsigned long hdr_len,
46d5c908
AE
738 unsigned long len, int nb)
739{
0753c182
GP
740 struct sk_buff *skb;
741
d9fbd02b 742 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
0753c182
GP
743 if (!skb)
744 return ERR_PTR(-ENOMEM);
745
746 return skb;
46d5c908
AE
747}
748
67f86a45 749static const struct l2cap_ops a2mp_chan_ops = {
466f8004 750 .name = "L2CAP A2MP channel",
6b44d9b8 751 .recv = a2mp_chan_recv_cb,
46d5c908
AE
752 .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 */
7e1af8a3
GP
757 .new_connection = l2cap_chan_no_new_connection,
758 .teardown = l2cap_chan_no_teardown,
759 .ready = l2cap_chan_no_ready,
2dc4e510 760 .defer = l2cap_chan_no_defer,
2ce5fb51 761 .resume = l2cap_chan_no_resume,
5ec1bbe5 762 .set_shutdown = l2cap_chan_no_set_shutdown,
8d836d71 763 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
466f8004
AE
764};
765
93c3e8f5 766static struct l2cap_chan *a2mp_chan_open(struct l2cap_conn *conn, bool locked)
466f8004
AE
767{
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
2338a7e0
JH
777 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;
466f8004
AE
782 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
93c3e8f5
AE
805 if (locked)
806 __l2cap_chan_add(conn, chan);
807 else
808 l2cap_chan_add(conn, chan);
466f8004
AE
809
810 chan->remote_mps = chan->omtu;
811 chan->mps = chan->omtu;
812
813 chan->state = BT_CONNECTED;
814
815 return chan;
816}
9740e49d
AE
817
818/* AMP Manager functions */
f706adfe 819struct amp_mgr *amp_mgr_get(struct amp_mgr *mgr)
9740e49d 820{
2c935bc5 821 BT_DBG("mgr %p orig refcnt %d", mgr, kref_read(&mgr->kref));
9740e49d
AE
822
823 kref_get(&mgr->kref);
f706adfe
AE
824
825 return mgr;
9740e49d
AE
826}
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
f97268fc
AE
834 mutex_lock(&amp_mgr_list_lock);
835 list_del(&mgr->list);
836 mutex_unlock(&amp_mgr_list_lock);
837
52c0d6e5 838 amp_ctrl_list_flush(mgr);
9740e49d
AE
839 kfree(mgr);
840}
841
842int amp_mgr_put(struct amp_mgr *mgr)
843{
2c935bc5 844 BT_DBG("mgr %p orig refcnt %d", mgr, kref_read(&mgr->kref));
9740e49d
AE
845
846 return kref_put(&mgr->kref, &amp_mgr_destroy);
847}
848
93c3e8f5 849static struct amp_mgr *amp_mgr_create(struct l2cap_conn *conn, bool locked)
9740e49d
AE
850{
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
93c3e8f5 862 chan = a2mp_chan_open(conn, locked);
9740e49d
AE
863 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
52c0d6e5
AE
875 /* Remote AMP ctrl list initialization */
876 INIT_LIST_HEAD(&mgr->amp_ctrls);
877 mutex_init(&mgr->amp_ctrls_lock);
878
f97268fc
AE
879 mutex_lock(&amp_mgr_list_lock);
880 list_add(&mgr->list, &amp_mgr_list);
881 mutex_unlock(&amp_mgr_list_lock);
882
9740e49d
AE
883 return mgr;
884}
97e8e89d
AE
885
886struct l2cap_chan *a2mp_channel_create(struct l2cap_conn *conn,
887 struct sk_buff *skb)
888{
889 struct amp_mgr *mgr;
890
07e307f8
JH
891 if (conn->hcon->type != ACL_LINK)
892 return NULL;
893
93c3e8f5 894 mgr = amp_mgr_create(conn, false);
97e8e89d
AE
895 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}
f97268fc 904
8e2a0d92
AE
905void 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
eddb7732
LAD
916 memset(&rsp, 0, sizeof(rsp));
917
8e2a0d92
AE
918 rsp.id = hdev->id;
919 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
920
ece69126 921 if (hdev->amp_type != AMP_TYPE_BREDR) {
8e2a0d92
AE
922 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}
903e4541
AE
933
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}
93c3e8f5 967
9495b2ee
AE
968void 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;
fffadc08 995 req->remote_id = bredr_chan->remote_amp_id;
9495b2ee
AE
996 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
8e05e3ba
AE
1005void 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
eddb7732
LAD
1015 memset(&rsp, 0, sizeof(rsp));
1016
8e05e3ba
AE
1017 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
93c3e8f5
AE
1033void 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
eddb7732
LAD
1049 memset(&req, 0, sizeof(req));
1050
93c3e8f5
AE
1051 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}