]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/bluetooth/a2mp.c
put iov_iter into msghdr
[mirror_ubuntu-bionic-kernel.git] / net / bluetooth / a2mp.c
CommitLineData
466f8004
AE
1/*
2 Copyright (c) 2010,2011 Code Aurora Forum. All rights reserved.
3 Copyright (c) 2011,2012 Intel Corp.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 and
7 only version 2 as published by the Free Software Foundation.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13*/
14
15#include <net/bluetooth/bluetooth.h>
16#include <net/bluetooth/hci_core.h>
17#include <net/bluetooth/l2cap.h>
7ef9fbf0 18
7024728e 19#include "a2mp.h"
7ef9fbf0 20#include "amp.h"
466f8004 21
f97268fc
AE
22/* Global AMP Manager list */
23LIST_HEAD(amp_mgr_list);
24DEFINE_MUTEX(amp_mgr_list_lock);
25
f6d3c6e7
AE
26/* A2MP build & send command helper functions */
27static struct a2mp_cmd *__a2mp_build(u8 code, u8 ident, u16 len, void *data)
28{
29 struct a2mp_cmd *cmd;
30 int plen;
31
32 plen = sizeof(*cmd) + len;
33 cmd = kzalloc(plen, GFP_KERNEL);
34 if (!cmd)
35 return NULL;
36
37 cmd->code = code;
38 cmd->ident = ident;
39 cmd->len = cpu_to_le16(len);
40
41 memcpy(cmd->data, data, len);
42
43 return cmd;
44}
45
8e2a0d92 46void a2mp_send(struct amp_mgr *mgr, u8 code, u8 ident, u16 len, void *data)
f6d3c6e7
AE
47{
48 struct l2cap_chan *chan = mgr->a2mp_chan;
49 struct a2mp_cmd *cmd;
50 u16 total_len = len + sizeof(*cmd);
51 struct kvec iv;
52 struct msghdr msg;
53
54 cmd = __a2mp_build(code, ident, len, data);
55 if (!cmd)
56 return;
57
58 iv.iov_base = cmd;
59 iv.iov_len = total_len;
60
61 memset(&msg, 0, sizeof(msg));
62
c0371da6 63 iov_iter_init(&msg.msg_iter, WRITE, (struct iovec *)&iv, 1, total_len);
f6d3c6e7 64
8d46321c 65 l2cap_chan_send(chan, &msg, total_len);
f6d3c6e7
AE
66
67 kfree(cmd);
68}
69
9495b2ee 70u8 __next_ident(struct amp_mgr *mgr)
aa09537d
AE
71{
72 if (++mgr->ident == 0)
73 mgr->ident = 1;
74
75 return mgr->ident;
76}
77
8598d064 78/* hci_dev_list shall be locked */
23f0cb41 79static void __a2mp_add_cl(struct amp_mgr *mgr, struct a2mp_cl *cl)
8598d064 80{
8598d064 81 struct hci_dev *hdev;
e8803534 82 int i = 1;
8598d064 83
346e7099
MH
84 cl[0].id = AMP_ID_BREDR;
85 cl[0].type = AMP_TYPE_BREDR;
86 cl[0].status = AMP_STATUS_BLUETOOTH_ONLY;
8598d064
AE
87
88 list_for_each_entry(hdev, &hci_dev_list, list) {
e8803534
MH
89 if (hdev->dev_type == HCI_AMP) {
90 cl[i].id = hdev->id;
91 cl[i].type = hdev->amp_type;
cd0a85c2
MH
92 if (test_bit(HCI_UP, &hdev->flags))
93 cl[i].status = hdev->amp_status;
94 else
95 cl[i].status = AMP_STATUS_POWERED_DOWN;
e8803534
MH
96 i++;
97 }
8598d064
AE
98 }
99}
100
21dbd2ce
AE
101/* Processing A2MP messages */
102static int a2mp_command_rej(struct amp_mgr *mgr, struct sk_buff *skb,
103 struct a2mp_cmd *hdr)
104{
105 struct a2mp_cmd_rej *rej = (void *) skb->data;
106
107 if (le16_to_cpu(hdr->len) < sizeof(*rej))
108 return -EINVAL;
109
110 BT_DBG("ident %d reason %d", hdr->ident, le16_to_cpu(rej->reason));
111
112 skb_pull(skb, sizeof(*rej));
113
114 return 0;
115}
116
8598d064
AE
117static int a2mp_discover_req(struct amp_mgr *mgr, struct sk_buff *skb,
118 struct a2mp_cmd *hdr)
119{
120 struct a2mp_discov_req *req = (void *) skb->data;
121 u16 len = le16_to_cpu(hdr->len);
122 struct a2mp_discov_rsp *rsp;
123 u16 ext_feat;
124 u8 num_ctrl;
f822c411 125 struct hci_dev *hdev;
8598d064
AE
126
127 if (len < sizeof(*req))
128 return -EINVAL;
129
130 skb_pull(skb, sizeof(*req));
131
132 ext_feat = le16_to_cpu(req->ext_feat);
133
134 BT_DBG("mtu %d efm 0x%4.4x", le16_to_cpu(req->mtu), ext_feat);
135
136 /* check that packet is not broken for now */
137 while (ext_feat & A2MP_FEAT_EXT) {
138 if (len < sizeof(ext_feat))
139 return -EINVAL;
140
141 ext_feat = get_unaligned_le16(skb->data);
142 BT_DBG("efm 0x%4.4x", ext_feat);
143 len -= sizeof(ext_feat);
144 skb_pull(skb, sizeof(ext_feat));
145 }
146
147 read_lock(&hci_dev_list_lock);
148
f822c411
MH
149 /* at minimum the BR/EDR needs to be listed */
150 num_ctrl = 1;
151
152 list_for_each_entry(hdev, &hci_dev_list, list) {
153 if (hdev->dev_type == HCI_AMP)
154 num_ctrl++;
155 }
156
8598d064
AE
157 len = num_ctrl * sizeof(struct a2mp_cl) + sizeof(*rsp);
158 rsp = kmalloc(len, GFP_ATOMIC);
159 if (!rsp) {
160 read_unlock(&hci_dev_list_lock);
161 return -ENOMEM;
162 }
163
dcf4adbf 164 rsp->mtu = cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU);
8598d064
AE
165 rsp->ext_feat = 0;
166
23f0cb41 167 __a2mp_add_cl(mgr, rsp->cl);
8598d064
AE
168
169 read_unlock(&hci_dev_list_lock);
170
171 a2mp_send(mgr, A2MP_DISCOVER_RSP, hdr->ident, len, rsp);
172
173 kfree(rsp);
174 return 0;
175}
176
aa09537d
AE
177static int a2mp_discover_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
178 struct a2mp_cmd *hdr)
179{
180 struct a2mp_discov_rsp *rsp = (void *) skb->data;
181 u16 len = le16_to_cpu(hdr->len);
182 struct a2mp_cl *cl;
183 u16 ext_feat;
2766be48 184 bool found = false;
aa09537d
AE
185
186 if (len < sizeof(*rsp))
187 return -EINVAL;
188
189 len -= sizeof(*rsp);
190 skb_pull(skb, sizeof(*rsp));
191
192 ext_feat = le16_to_cpu(rsp->ext_feat);
193
194 BT_DBG("mtu %d efm 0x%4.4x", le16_to_cpu(rsp->mtu), ext_feat);
195
196 /* check that packet is not broken for now */
197 while (ext_feat & A2MP_FEAT_EXT) {
198 if (len < sizeof(ext_feat))
199 return -EINVAL;
200
201 ext_feat = get_unaligned_le16(skb->data);
202 BT_DBG("efm 0x%4.4x", ext_feat);
203 len -= sizeof(ext_feat);
204 skb_pull(skb, sizeof(ext_feat));
205 }
206
207 cl = (void *) skb->data;
208 while (len >= sizeof(*cl)) {
209 BT_DBG("Remote AMP id %d type %d status %d", cl->id, cl->type,
210 cl->status);
211
a646bd81 212 if (cl->id != AMP_ID_BREDR && cl->type != AMP_TYPE_BREDR) {
aa09537d
AE
213 struct a2mp_info_req req;
214
2766be48 215 found = true;
aa09537d
AE
216 req.id = cl->id;
217 a2mp_send(mgr, A2MP_GETINFO_REQ, __next_ident(mgr),
218 sizeof(req), &req);
219 }
220
221 len -= sizeof(*cl);
222 cl = (void *) skb_pull(skb, sizeof(*cl));
223 }
224
2766be48
AE
225 /* Fall back to L2CAP init sequence */
226 if (!found) {
227 struct l2cap_conn *conn = mgr->l2cap_conn;
228 struct l2cap_chan *chan;
229
230 mutex_lock(&conn->chan_lock);
231
232 list_for_each_entry(chan, &conn->chan_l, list) {
233
234 BT_DBG("chan %p state %s", chan,
235 state_to_string(chan->state));
236
2338a7e0 237 if (chan->scid == L2CAP_CID_A2MP)
2766be48
AE
238 continue;
239
240 l2cap_chan_lock(chan);
241
242 if (chan->state == BT_CONNECT)
243 l2cap_send_conn_req(chan);
244
245 l2cap_chan_unlock(chan);
246 }
247
248 mutex_unlock(&conn->chan_lock);
249 }
250
aa09537d
AE
251 return 0;
252}
253
329d81af
AE
254static int a2mp_change_notify(struct amp_mgr *mgr, struct sk_buff *skb,
255 struct a2mp_cmd *hdr)
256{
257 struct a2mp_cl *cl = (void *) skb->data;
258
259 while (skb->len >= sizeof(*cl)) {
260 BT_DBG("Controller id %d type %d status %d", cl->id, cl->type,
261 cl->status);
262 cl = (struct a2mp_cl *) skb_pull(skb, sizeof(*cl));
263 }
264
265 /* TODO send A2MP_CHANGE_RSP */
266
267 return 0;
268}
269
47f2d97d
AE
270static int a2mp_getinfo_req(struct amp_mgr *mgr, struct sk_buff *skb,
271 struct a2mp_cmd *hdr)
272{
273 struct a2mp_info_req *req = (void *) skb->data;
47f2d97d
AE
274 struct hci_dev *hdev;
275
276 if (le16_to_cpu(hdr->len) < sizeof(*req))
277 return -EINVAL;
278
279 BT_DBG("id %d", req->id);
280
47f2d97d 281 hdev = hci_dev_get(req->id);
bc8dce4f 282 if (!hdev || hdev->dev_type != HCI_AMP) {
8e2a0d92
AE
283 struct a2mp_info_rsp rsp;
284
285 rsp.id = req->id;
286 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
287
288 a2mp_send(mgr, A2MP_GETINFO_RSP, hdr->ident, sizeof(rsp),
289 &rsp);
47f2d97d 290
bc8dce4f 291 goto done;
8e2a0d92 292 }
47f2d97d 293
cb6801c6 294 set_bit(READ_LOC_AMP_INFO, &mgr->state);
bc8dce4f
AE
295 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL);
296
297done:
298 if (hdev)
299 hci_dev_put(hdev);
47f2d97d
AE
300
301 skb_pull(skb, sizeof(*req));
302 return 0;
303}
304
0d868de9
AE
305static int a2mp_getinfo_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
306 struct a2mp_cmd *hdr)
307{
308 struct a2mp_info_rsp *rsp = (struct a2mp_info_rsp *) skb->data;
309 struct a2mp_amp_assoc_req req;
310 struct amp_ctrl *ctrl;
311
312 if (le16_to_cpu(hdr->len) < sizeof(*rsp))
313 return -EINVAL;
314
315 BT_DBG("id %d status 0x%2.2x", rsp->id, rsp->status);
316
317 if (rsp->status)
318 return -EINVAL;
319
fa4ebc66 320 ctrl = amp_ctrl_add(mgr, rsp->id);
0d868de9
AE
321 if (!ctrl)
322 return -ENOMEM;
323
0d868de9
AE
324 req.id = rsp->id;
325 a2mp_send(mgr, A2MP_GETAMPASSOC_REQ, __next_ident(mgr), sizeof(req),
326 &req);
327
328 skb_pull(skb, sizeof(*rsp));
329 return 0;
330}
331
a28381dc
AE
332static int a2mp_getampassoc_req(struct amp_mgr *mgr, struct sk_buff *skb,
333 struct a2mp_cmd *hdr)
334{
335 struct a2mp_amp_assoc_req *req = (void *) skb->data;
336 struct hci_dev *hdev;
903e4541 337 struct amp_mgr *tmp;
a28381dc
AE
338
339 if (le16_to_cpu(hdr->len) < sizeof(*req))
340 return -EINVAL;
341
342 BT_DBG("id %d", req->id);
343
903e4541
AE
344 /* Make sure that other request is not processed */
345 tmp = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC);
346
a28381dc 347 hdev = hci_dev_get(req->id);
ece69126 348 if (!hdev || hdev->amp_type == AMP_TYPE_BREDR || tmp) {
a28381dc
AE
349 struct a2mp_amp_assoc_rsp rsp;
350 rsp.id = req->id;
903e4541
AE
351
352 if (tmp) {
353 rsp.status = A2MP_STATUS_COLLISION_OCCURED;
354 amp_mgr_put(tmp);
355 } else {
356 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
357 }
a28381dc
AE
358
359 a2mp_send(mgr, A2MP_GETAMPASSOC_RSP, hdr->ident, sizeof(rsp),
360 &rsp);
903e4541
AE
361
362 goto done;
a28381dc
AE
363 }
364
903e4541 365 amp_read_loc_assoc(hdev, mgr);
a28381dc 366
903e4541 367done:
a28381dc
AE
368 if (hdev)
369 hci_dev_put(hdev);
370
371 skb_pull(skb, sizeof(*req));
372 return 0;
373}
374
9a5e94db
AE
375static int a2mp_getampassoc_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
376 struct a2mp_cmd *hdr)
377{
378 struct a2mp_amp_assoc_rsp *rsp = (void *) skb->data;
379 u16 len = le16_to_cpu(hdr->len);
380 struct hci_dev *hdev;
381 struct amp_ctrl *ctrl;
382 struct hci_conn *hcon;
13465c0a 383 size_t assoc_len;
9a5e94db
AE
384
385 if (len < sizeof(*rsp))
386 return -EINVAL;
387
13465c0a
AE
388 assoc_len = len - sizeof(*rsp);
389
390 BT_DBG("id %d status 0x%2.2x assoc len %zu", rsp->id, rsp->status,
391 assoc_len);
9a5e94db
AE
392
393 if (rsp->status)
394 return -EINVAL;
395
396 /* Save remote ASSOC data */
397 ctrl = amp_ctrl_lookup(mgr, rsp->id);
398 if (ctrl) {
13465c0a 399 u8 *assoc;
9a5e94db 400
5ae327f0 401 assoc = kmemdup(rsp->amp_assoc, assoc_len, GFP_KERNEL);
9a5e94db
AE
402 if (!assoc) {
403 amp_ctrl_put(ctrl);
404 return -ENOMEM;
405 }
406
9a5e94db
AE
407 ctrl->assoc = assoc;
408 ctrl->assoc_len = assoc_len;
409 ctrl->assoc_rem_len = assoc_len;
410 ctrl->assoc_len_so_far = 0;
411
412 amp_ctrl_put(ctrl);
413 }
414
415 /* Create Phys Link */
416 hdev = hci_dev_get(rsp->id);
417 if (!hdev)
418 return -EINVAL;
419
a0c234fe 420 hcon = phylink_add(hdev, mgr, rsp->id, true);
9a5e94db
AE
421 if (!hcon)
422 goto done;
423
424 BT_DBG("Created hcon %p: loc:%d -> rem:%d", hcon, hdev->id, rsp->id);
425
fffadc08 426 mgr->bredr_chan->remote_amp_id = rsp->id;
9495b2ee 427
a02226d6
AE
428 amp_create_phylink(hdev, mgr, hcon);
429
9a5e94db
AE
430done:
431 hci_dev_put(hdev);
432 skb_pull(skb, len);
433 return 0;
434}
435
e072f5da
AE
436static int a2mp_createphyslink_req(struct amp_mgr *mgr, struct sk_buff *skb,
437 struct a2mp_cmd *hdr)
438{
439 struct a2mp_physlink_req *req = (void *) skb->data;
440
441 struct a2mp_physlink_rsp rsp;
442 struct hci_dev *hdev;
cb8488c0 443 struct hci_conn *hcon;
0b26ab9d 444 struct amp_ctrl *ctrl;
e072f5da
AE
445
446 if (le16_to_cpu(hdr->len) < sizeof(*req))
447 return -EINVAL;
448
449 BT_DBG("local_id %d, remote_id %d", req->local_id, req->remote_id);
450
451 rsp.local_id = req->remote_id;
452 rsp.remote_id = req->local_id;
453
454 hdev = hci_dev_get(req->remote_id);
ece69126 455 if (!hdev || hdev->amp_type == AMP_TYPE_BREDR) {
e072f5da
AE
456 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
457 goto send_rsp;
458 }
459
0b26ab9d
AE
460 ctrl = amp_ctrl_lookup(mgr, rsp.remote_id);
461 if (!ctrl) {
fa4ebc66 462 ctrl = amp_ctrl_add(mgr, rsp.remote_id);
0b26ab9d
AE
463 if (ctrl) {
464 amp_ctrl_get(ctrl);
465 } else {
466 rsp.status = A2MP_STATUS_UNABLE_START_LINK_CREATION;
467 goto send_rsp;
468 }
469 }
470
471 if (ctrl) {
13465c0a
AE
472 size_t assoc_len = le16_to_cpu(hdr->len) - sizeof(*req);
473 u8 *assoc;
0b26ab9d 474
5ae327f0 475 assoc = kmemdup(req->amp_assoc, assoc_len, GFP_KERNEL);
0b26ab9d
AE
476 if (!assoc) {
477 amp_ctrl_put(ctrl);
478 return -ENOMEM;
479 }
480
0b26ab9d
AE
481 ctrl->assoc = assoc;
482 ctrl->assoc_len = assoc_len;
483 ctrl->assoc_rem_len = assoc_len;
484 ctrl->assoc_len_so_far = 0;
485
486 amp_ctrl_put(ctrl);
487 }
488
a0c234fe 489 hcon = phylink_add(hdev, mgr, req->local_id, false);
cb8488c0 490 if (hcon) {
dffa3871 491 amp_accept_phylink(hdev, mgr, hcon);
cb8488c0
AE
492 rsp.status = A2MP_STATUS_SUCCESS;
493 } else {
494 rsp.status = A2MP_STATUS_UNABLE_START_LINK_CREATION;
495 }
e072f5da
AE
496
497send_rsp:
498 if (hdev)
499 hci_dev_put(hdev);
500
8e05e3ba
AE
501 /* Reply error now and success after HCI Write Remote AMP Assoc
502 command complete with success status
503 */
504 if (rsp.status != A2MP_STATUS_SUCCESS) {
505 a2mp_send(mgr, A2MP_CREATEPHYSLINK_RSP, hdr->ident,
506 sizeof(rsp), &rsp);
507 } else {
cb6801c6 508 set_bit(WRITE_REMOTE_AMP_ASSOC, &mgr->state);
8e05e3ba
AE
509 mgr->ident = hdr->ident;
510 }
e072f5da
AE
511
512 skb_pull(skb, le16_to_cpu(hdr->len));
513 return 0;
514}
515
6113f84f
AE
516static int a2mp_discphyslink_req(struct amp_mgr *mgr, struct sk_buff *skb,
517 struct a2mp_cmd *hdr)
518{
519 struct a2mp_physlink_req *req = (void *) skb->data;
520 struct a2mp_physlink_rsp rsp;
521 struct hci_dev *hdev;
cb8488c0 522 struct hci_conn *hcon;
6113f84f
AE
523
524 if (le16_to_cpu(hdr->len) < sizeof(*req))
525 return -EINVAL;
526
527 BT_DBG("local_id %d remote_id %d", req->local_id, req->remote_id);
528
529 rsp.local_id = req->remote_id;
530 rsp.remote_id = req->local_id;
531 rsp.status = A2MP_STATUS_SUCCESS;
532
cb8488c0 533 hdev = hci_dev_get(req->remote_id);
6113f84f
AE
534 if (!hdev) {
535 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
536 goto send_rsp;
537 }
538
bdc8ead2
MH
539 hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK,
540 &mgr->l2cap_conn->hcon->dst);
cb8488c0
AE
541 if (!hcon) {
542 BT_ERR("No phys link exist");
543 rsp.status = A2MP_STATUS_NO_PHYSICAL_LINK_EXISTS;
544 goto clean;
545 }
546
6113f84f
AE
547 /* TODO Disconnect Phys Link here */
548
cb8488c0 549clean:
6113f84f
AE
550 hci_dev_put(hdev);
551
552send_rsp:
553 a2mp_send(mgr, A2MP_DISCONNPHYSLINK_RSP, hdr->ident, sizeof(rsp), &rsp);
554
555 skb_pull(skb, sizeof(*req));
556 return 0;
557}
558
f6410a84
AE
559static inline int a2mp_cmd_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
560 struct a2mp_cmd *hdr)
561{
8e8c7e36 562 BT_DBG("ident %d code 0x%2.2x", hdr->ident, hdr->code);
f6410a84
AE
563
564 skb_pull(skb, le16_to_cpu(hdr->len));
565 return 0;
566}
567
6b44d9b8
AE
568/* Handle A2MP signalling */
569static int a2mp_chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
570{
d9fc1d54 571 struct a2mp_cmd *hdr;
6b44d9b8
AE
572 struct amp_mgr *mgr = chan->data;
573 int err = 0;
574
575 amp_mgr_get(mgr);
576
577 while (skb->len >= sizeof(*hdr)) {
d9fc1d54
AE
578 u16 len;
579
580 hdr = (void *) skb->data;
581 len = le16_to_cpu(hdr->len);
6b44d9b8 582
8e8c7e36 583 BT_DBG("code 0x%2.2x id %d len %u", hdr->code, hdr->ident, len);
6b44d9b8
AE
584
585 skb_pull(skb, sizeof(*hdr));
586
587 if (len > skb->len || !hdr->ident) {
588 err = -EINVAL;
589 break;
590 }
591
592 mgr->ident = hdr->ident;
593
594 switch (hdr->code) {
595 case A2MP_COMMAND_REJ:
21dbd2ce
AE
596 a2mp_command_rej(mgr, skb, hdr);
597 break;
598
6b44d9b8 599 case A2MP_DISCOVER_REQ:
8598d064
AE
600 err = a2mp_discover_req(mgr, skb, hdr);
601 break;
602
6b44d9b8 603 case A2MP_CHANGE_NOTIFY:
329d81af
AE
604 err = a2mp_change_notify(mgr, skb, hdr);
605 break;
606
6b44d9b8 607 case A2MP_GETINFO_REQ:
47f2d97d
AE
608 err = a2mp_getinfo_req(mgr, skb, hdr);
609 break;
610
6b44d9b8 611 case A2MP_GETAMPASSOC_REQ:
a28381dc
AE
612 err = a2mp_getampassoc_req(mgr, skb, hdr);
613 break;
614
6b44d9b8 615 case A2MP_CREATEPHYSLINK_REQ:
e072f5da
AE
616 err = a2mp_createphyslink_req(mgr, skb, hdr);
617 break;
618
6b44d9b8 619 case A2MP_DISCONNPHYSLINK_REQ:
6113f84f
AE
620 err = a2mp_discphyslink_req(mgr, skb, hdr);
621 break;
622
6b44d9b8 623 case A2MP_DISCOVER_RSP:
aa09537d
AE
624 err = a2mp_discover_rsp(mgr, skb, hdr);
625 break;
626
6b44d9b8 627 case A2MP_GETINFO_RSP:
0d868de9
AE
628 err = a2mp_getinfo_rsp(mgr, skb, hdr);
629 break;
630
6b44d9b8 631 case A2MP_GETAMPASSOC_RSP:
9a5e94db
AE
632 err = a2mp_getampassoc_rsp(mgr, skb, hdr);
633 break;
634
635 case A2MP_CHANGE_RSP:
6b44d9b8
AE
636 case A2MP_CREATEPHYSLINK_RSP:
637 case A2MP_DISCONNPHYSLINK_RSP:
f6410a84
AE
638 err = a2mp_cmd_rsp(mgr, skb, hdr);
639 break;
640
6b44d9b8
AE
641 default:
642 BT_ERR("Unknown A2MP sig cmd 0x%2.2x", hdr->code);
643 err = -EINVAL;
644 break;
645 }
646 }
647
648 if (err) {
649 struct a2mp_cmd_rej rej;
d9fc1d54 650
dcf4adbf 651 rej.reason = cpu_to_le16(0);
d9fc1d54 652 hdr = (void *) skb->data;
6b44d9b8
AE
653
654 BT_DBG("Send A2MP Rej: cmd 0x%2.2x err %d", hdr->code, err);
655
656 a2mp_send(mgr, A2MP_COMMAND_REJ, hdr->ident, sizeof(rej),
657 &rej);
658 }
659
660 /* Always free skb and return success error code to prevent
661 from sending L2CAP Disconnect over A2MP channel */
662 kfree_skb(skb);
663
664 amp_mgr_put(mgr);
665
666 return 0;
667}
668
46d5c908
AE
669static void a2mp_chan_close_cb(struct l2cap_chan *chan)
670{
4af66c69 671 l2cap_chan_put(chan);
46d5c908
AE
672}
673
53f52121
GP
674static void a2mp_chan_state_change_cb(struct l2cap_chan *chan, int state,
675 int err)
46d5c908
AE
676{
677 struct amp_mgr *mgr = chan->data;
678
679 if (!mgr)
680 return;
681
682 BT_DBG("chan %p state %s", chan, state_to_string(state));
683
684 chan->state = state;
685
686 switch (state) {
687 case BT_CLOSED:
688 if (mgr)
689 amp_mgr_put(mgr);
690 break;
691 }
692}
693
694static struct sk_buff *a2mp_chan_alloc_skb_cb(struct l2cap_chan *chan,
d9fbd02b 695 unsigned long hdr_len,
46d5c908
AE
696 unsigned long len, int nb)
697{
0753c182
GP
698 struct sk_buff *skb;
699
d9fbd02b 700 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
0753c182
GP
701 if (!skb)
702 return ERR_PTR(-ENOMEM);
703
704 return skb;
46d5c908
AE
705}
706
67f86a45 707static const struct l2cap_ops a2mp_chan_ops = {
466f8004 708 .name = "L2CAP A2MP channel",
6b44d9b8 709 .recv = a2mp_chan_recv_cb,
46d5c908
AE
710 .close = a2mp_chan_close_cb,
711 .state_change = a2mp_chan_state_change_cb,
712 .alloc_skb = a2mp_chan_alloc_skb_cb,
713
714 /* Not implemented for A2MP */
7e1af8a3
GP
715 .new_connection = l2cap_chan_no_new_connection,
716 .teardown = l2cap_chan_no_teardown,
717 .ready = l2cap_chan_no_ready,
2dc4e510 718 .defer = l2cap_chan_no_defer,
2ce5fb51 719 .resume = l2cap_chan_no_resume,
5ec1bbe5 720 .set_shutdown = l2cap_chan_no_set_shutdown,
8d836d71 721 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
0498878b 722 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
466f8004
AE
723};
724
93c3e8f5 725static struct l2cap_chan *a2mp_chan_open(struct l2cap_conn *conn, bool locked)
466f8004
AE
726{
727 struct l2cap_chan *chan;
728 int err;
729
730 chan = l2cap_chan_create();
731 if (!chan)
732 return NULL;
733
734 BT_DBG("chan %p", chan);
735
2338a7e0
JH
736 chan->chan_type = L2CAP_CHAN_FIXED;
737 chan->scid = L2CAP_CID_A2MP;
738 chan->dcid = L2CAP_CID_A2MP;
739 chan->omtu = L2CAP_A2MP_DEFAULT_MTU;
740 chan->imtu = L2CAP_A2MP_DEFAULT_MTU;
466f8004
AE
741 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
742
743 chan->ops = &a2mp_chan_ops;
744
745 l2cap_chan_set_defaults(chan);
746 chan->remote_max_tx = chan->max_tx;
747 chan->remote_tx_win = chan->tx_win;
748
749 chan->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO;
750 chan->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO;
751
752 skb_queue_head_init(&chan->tx_q);
753
754 chan->mode = L2CAP_MODE_ERTM;
755
756 err = l2cap_ertm_init(chan);
757 if (err < 0) {
758 l2cap_chan_del(chan, 0);
759 return NULL;
760 }
761
762 chan->conf_state = 0;
763
93c3e8f5
AE
764 if (locked)
765 __l2cap_chan_add(conn, chan);
766 else
767 l2cap_chan_add(conn, chan);
466f8004
AE
768
769 chan->remote_mps = chan->omtu;
770 chan->mps = chan->omtu;
771
772 chan->state = BT_CONNECTED;
773
774 return chan;
775}
9740e49d
AE
776
777/* AMP Manager functions */
f706adfe 778struct amp_mgr *amp_mgr_get(struct amp_mgr *mgr)
9740e49d 779{
a0dfe0ab 780 BT_DBG("mgr %p orig refcnt %d", mgr, atomic_read(&mgr->kref.refcount));
9740e49d
AE
781
782 kref_get(&mgr->kref);
f706adfe
AE
783
784 return mgr;
9740e49d
AE
785}
786
787static void amp_mgr_destroy(struct kref *kref)
788{
789 struct amp_mgr *mgr = container_of(kref, struct amp_mgr, kref);
790
791 BT_DBG("mgr %p", mgr);
792
f97268fc
AE
793 mutex_lock(&amp_mgr_list_lock);
794 list_del(&mgr->list);
795 mutex_unlock(&amp_mgr_list_lock);
796
52c0d6e5 797 amp_ctrl_list_flush(mgr);
9740e49d
AE
798 kfree(mgr);
799}
800
801int amp_mgr_put(struct amp_mgr *mgr)
802{
a0dfe0ab 803 BT_DBG("mgr %p orig refcnt %d", mgr, atomic_read(&mgr->kref.refcount));
9740e49d
AE
804
805 return kref_put(&mgr->kref, &amp_mgr_destroy);
806}
807
93c3e8f5 808static struct amp_mgr *amp_mgr_create(struct l2cap_conn *conn, bool locked)
9740e49d
AE
809{
810 struct amp_mgr *mgr;
811 struct l2cap_chan *chan;
812
813 mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
814 if (!mgr)
815 return NULL;
816
817 BT_DBG("conn %p mgr %p", conn, mgr);
818
819 mgr->l2cap_conn = conn;
820
93c3e8f5 821 chan = a2mp_chan_open(conn, locked);
9740e49d
AE
822 if (!chan) {
823 kfree(mgr);
824 return NULL;
825 }
826
827 mgr->a2mp_chan = chan;
828 chan->data = mgr;
829
830 conn->hcon->amp_mgr = mgr;
831
832 kref_init(&mgr->kref);
833
52c0d6e5
AE
834 /* Remote AMP ctrl list initialization */
835 INIT_LIST_HEAD(&mgr->amp_ctrls);
836 mutex_init(&mgr->amp_ctrls_lock);
837
f97268fc
AE
838 mutex_lock(&amp_mgr_list_lock);
839 list_add(&mgr->list, &amp_mgr_list);
840 mutex_unlock(&amp_mgr_list_lock);
841
9740e49d
AE
842 return mgr;
843}
97e8e89d
AE
844
845struct l2cap_chan *a2mp_channel_create(struct l2cap_conn *conn,
846 struct sk_buff *skb)
847{
848 struct amp_mgr *mgr;
849
07e307f8
JH
850 if (conn->hcon->type != ACL_LINK)
851 return NULL;
852
93c3e8f5 853 mgr = amp_mgr_create(conn, false);
97e8e89d
AE
854 if (!mgr) {
855 BT_ERR("Could not create AMP manager");
856 return NULL;
857 }
858
859 BT_DBG("mgr: %p chan %p", mgr, mgr->a2mp_chan);
860
861 return mgr->a2mp_chan;
862}
f97268fc
AE
863
864struct amp_mgr *amp_mgr_lookup_by_state(u8 state)
865{
866 struct amp_mgr *mgr;
867
868 mutex_lock(&amp_mgr_list_lock);
869 list_for_each_entry(mgr, &amp_mgr_list, list) {
cb6801c6 870 if (test_and_clear_bit(state, &mgr->state)) {
f97268fc
AE
871 amp_mgr_get(mgr);
872 mutex_unlock(&amp_mgr_list_lock);
873 return mgr;
874 }
875 }
876 mutex_unlock(&amp_mgr_list_lock);
877
878 return NULL;
879}
8e2a0d92
AE
880
881void a2mp_send_getinfo_rsp(struct hci_dev *hdev)
882{
883 struct amp_mgr *mgr;
884 struct a2mp_info_rsp rsp;
885
886 mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_INFO);
887 if (!mgr)
888 return;
889
890 BT_DBG("%s mgr %p", hdev->name, mgr);
891
892 rsp.id = hdev->id;
893 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
894
ece69126 895 if (hdev->amp_type != AMP_TYPE_BREDR) {
8e2a0d92
AE
896 rsp.status = 0;
897 rsp.total_bw = cpu_to_le32(hdev->amp_total_bw);
898 rsp.max_bw = cpu_to_le32(hdev->amp_max_bw);
899 rsp.min_latency = cpu_to_le32(hdev->amp_min_latency);
900 rsp.pal_cap = cpu_to_le16(hdev->amp_pal_cap);
901 rsp.assoc_size = cpu_to_le16(hdev->amp_assoc_size);
902 }
903
904 a2mp_send(mgr, A2MP_GETINFO_RSP, mgr->ident, sizeof(rsp), &rsp);
905 amp_mgr_put(mgr);
906}
903e4541
AE
907
908void a2mp_send_getampassoc_rsp(struct hci_dev *hdev, u8 status)
909{
910 struct amp_mgr *mgr;
911 struct amp_assoc *loc_assoc = &hdev->loc_assoc;
912 struct a2mp_amp_assoc_rsp *rsp;
913 size_t len;
914
915 mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC);
916 if (!mgr)
917 return;
918
919 BT_DBG("%s mgr %p", hdev->name, mgr);
920
921 len = sizeof(struct a2mp_amp_assoc_rsp) + loc_assoc->len;
922 rsp = kzalloc(len, GFP_KERNEL);
923 if (!rsp) {
924 amp_mgr_put(mgr);
925 return;
926 }
927
928 rsp->id = hdev->id;
929
930 if (status) {
931 rsp->status = A2MP_STATUS_INVALID_CTRL_ID;
932 } else {
933 rsp->status = A2MP_STATUS_SUCCESS;
934 memcpy(rsp->amp_assoc, loc_assoc->data, loc_assoc->len);
935 }
936
937 a2mp_send(mgr, A2MP_GETAMPASSOC_RSP, mgr->ident, len, rsp);
938 amp_mgr_put(mgr);
939 kfree(rsp);
940}
93c3e8f5 941
9495b2ee
AE
942void a2mp_send_create_phy_link_req(struct hci_dev *hdev, u8 status)
943{
944 struct amp_mgr *mgr;
945 struct amp_assoc *loc_assoc = &hdev->loc_assoc;
946 struct a2mp_physlink_req *req;
947 struct l2cap_chan *bredr_chan;
948 size_t len;
949
950 mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC_FINAL);
951 if (!mgr)
952 return;
953
954 len = sizeof(*req) + loc_assoc->len;
955
956 BT_DBG("%s mgr %p assoc_len %zu", hdev->name, mgr, len);
957
958 req = kzalloc(len, GFP_KERNEL);
959 if (!req) {
960 amp_mgr_put(mgr);
961 return;
962 }
963
964 bredr_chan = mgr->bredr_chan;
965 if (!bredr_chan)
966 goto clean;
967
968 req->local_id = hdev->id;
fffadc08 969 req->remote_id = bredr_chan->remote_amp_id;
9495b2ee
AE
970 memcpy(req->amp_assoc, loc_assoc->data, loc_assoc->len);
971
972 a2mp_send(mgr, A2MP_CREATEPHYSLINK_REQ, __next_ident(mgr), len, req);
973
974clean:
975 amp_mgr_put(mgr);
976 kfree(req);
977}
978
8e05e3ba
AE
979void a2mp_send_create_phy_link_rsp(struct hci_dev *hdev, u8 status)
980{
981 struct amp_mgr *mgr;
982 struct a2mp_physlink_rsp rsp;
983 struct hci_conn *hs_hcon;
984
985 mgr = amp_mgr_lookup_by_state(WRITE_REMOTE_AMP_ASSOC);
986 if (!mgr)
987 return;
988
989 hs_hcon = hci_conn_hash_lookup_state(hdev, AMP_LINK, BT_CONNECT);
990 if (!hs_hcon) {
991 rsp.status = A2MP_STATUS_UNABLE_START_LINK_CREATION;
992 } else {
993 rsp.remote_id = hs_hcon->remote_id;
994 rsp.status = A2MP_STATUS_SUCCESS;
995 }
996
997 BT_DBG("%s mgr %p hs_hcon %p status %u", hdev->name, mgr, hs_hcon,
998 status);
999
1000 rsp.local_id = hdev->id;
1001 a2mp_send(mgr, A2MP_CREATEPHYSLINK_RSP, mgr->ident, sizeof(rsp), &rsp);
1002 amp_mgr_put(mgr);
1003}
1004
93c3e8f5
AE
1005void a2mp_discover_amp(struct l2cap_chan *chan)
1006{
1007 struct l2cap_conn *conn = chan->conn;
1008 struct amp_mgr *mgr = conn->hcon->amp_mgr;
1009 struct a2mp_discov_req req;
1010
1011 BT_DBG("chan %p conn %p mgr %p", chan, conn, mgr);
1012
1013 if (!mgr) {
1014 mgr = amp_mgr_create(conn, true);
1015 if (!mgr)
1016 return;
1017 }
1018
1019 mgr->bredr_chan = chan;
1020
1021 req.mtu = cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU);
1022 req.ext_feat = 0;
1023 a2mp_send(mgr, A2MP_DISCOVER_REQ, 1, sizeof(req), &req);
1024}