Bluetooth: Simplify check if L2CAP connection is AMP capable
[deliverable/linux.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>
9740e49d 18#include <net/bluetooth/a2mp.h>
903e4541 19#include <net/bluetooth/amp.h>
466f8004 20
f97268fc
AE
21/* Global AMP Manager list */
22LIST_HEAD(amp_mgr_list);
23DEFINE_MUTEX(amp_mgr_list_lock);
24
f6d3c6e7
AE
25/* A2MP build & send command helper functions */
26static struct a2mp_cmd *__a2mp_build(u8 code, u8 ident, u16 len, void *data)
27{
28 struct a2mp_cmd *cmd;
29 int plen;
30
31 plen = sizeof(*cmd) + len;
32 cmd = kzalloc(plen, GFP_KERNEL);
33 if (!cmd)
34 return NULL;
35
36 cmd->code = code;
37 cmd->ident = ident;
38 cmd->len = cpu_to_le16(len);
39
40 memcpy(cmd->data, data, len);
41
42 return cmd;
43}
44
8e2a0d92 45void a2mp_send(struct amp_mgr *mgr, u8 code, u8 ident, u16 len, void *data)
f6d3c6e7
AE
46{
47 struct l2cap_chan *chan = mgr->a2mp_chan;
48 struct a2mp_cmd *cmd;
49 u16 total_len = len + sizeof(*cmd);
50 struct kvec iv;
51 struct msghdr msg;
52
53 cmd = __a2mp_build(code, ident, len, data);
54 if (!cmd)
55 return;
56
57 iv.iov_base = cmd;
58 iv.iov_len = total_len;
59
60 memset(&msg, 0, sizeof(msg));
61
62 msg.msg_iov = (struct iovec *) &iv;
63 msg.msg_iovlen = 1;
64
65 l2cap_chan_send(chan, &msg, total_len, 0);
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
AE
80{
81 int i = 0;
82 struct hci_dev *hdev;
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) {
89 /* Iterate through AMP controllers */
23b9003b 90 if (hdev->dev_type != HCI_AMP)
8598d064
AE
91 continue;
92
93 /* Starting from second entry */
23f0cb41 94 ++i;
8598d064
AE
95
96 cl[i].id = hdev->id;
97 cl[i].type = hdev->amp_type;
98 cl[i].status = hdev->amp_status;
99 }
100}
101
21dbd2ce
AE
102/* Processing A2MP messages */
103static int a2mp_command_rej(struct amp_mgr *mgr, struct sk_buff *skb,
104 struct a2mp_cmd *hdr)
105{
106 struct a2mp_cmd_rej *rej = (void *) skb->data;
107
108 if (le16_to_cpu(hdr->len) < sizeof(*rej))
109 return -EINVAL;
110
111 BT_DBG("ident %d reason %d", hdr->ident, le16_to_cpu(rej->reason));
112
113 skb_pull(skb, sizeof(*rej));
114
115 return 0;
116}
117
8598d064
AE
118static int a2mp_discover_req(struct amp_mgr *mgr, struct sk_buff *skb,
119 struct a2mp_cmd *hdr)
120{
121 struct a2mp_discov_req *req = (void *) skb->data;
122 u16 len = le16_to_cpu(hdr->len);
123 struct a2mp_discov_rsp *rsp;
124 u16 ext_feat;
125 u8 num_ctrl;
f822c411 126 struct hci_dev *hdev;
8598d064
AE
127
128 if (len < sizeof(*req))
129 return -EINVAL;
130
131 skb_pull(skb, sizeof(*req));
132
133 ext_feat = le16_to_cpu(req->ext_feat);
134
135 BT_DBG("mtu %d efm 0x%4.4x", le16_to_cpu(req->mtu), ext_feat);
136
137 /* check that packet is not broken for now */
138 while (ext_feat & A2MP_FEAT_EXT) {
139 if (len < sizeof(ext_feat))
140 return -EINVAL;
141
142 ext_feat = get_unaligned_le16(skb->data);
143 BT_DBG("efm 0x%4.4x", ext_feat);
144 len -= sizeof(ext_feat);
145 skb_pull(skb, sizeof(ext_feat));
146 }
147
148 read_lock(&hci_dev_list_lock);
149
f822c411
MH
150 /* at minimum the BR/EDR needs to be listed */
151 num_ctrl = 1;
152
153 list_for_each_entry(hdev, &hci_dev_list, list) {
154 if (hdev->dev_type == HCI_AMP)
155 num_ctrl++;
156 }
157
8598d064
AE
158 len = num_ctrl * sizeof(struct a2mp_cl) + sizeof(*rsp);
159 rsp = kmalloc(len, GFP_ATOMIC);
160 if (!rsp) {
161 read_unlock(&hci_dev_list_lock);
162 return -ENOMEM;
163 }
164
165 rsp->mtu = __constant_cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU);
166 rsp->ext_feat = 0;
167
23f0cb41 168 __a2mp_add_cl(mgr, rsp->cl);
8598d064
AE
169
170 read_unlock(&hci_dev_list_lock);
171
172 a2mp_send(mgr, A2MP_DISCOVER_RSP, hdr->ident, len, rsp);
173
174 kfree(rsp);
175 return 0;
176}
177
aa09537d
AE
178static int a2mp_discover_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
179 struct a2mp_cmd *hdr)
180{
181 struct a2mp_discov_rsp *rsp = (void *) skb->data;
182 u16 len = le16_to_cpu(hdr->len);
183 struct a2mp_cl *cl;
184 u16 ext_feat;
2766be48 185 bool found = false;
aa09537d
AE
186
187 if (len < sizeof(*rsp))
188 return -EINVAL;
189
190 len -= sizeof(*rsp);
191 skb_pull(skb, sizeof(*rsp));
192
193 ext_feat = le16_to_cpu(rsp->ext_feat);
194
195 BT_DBG("mtu %d efm 0x%4.4x", le16_to_cpu(rsp->mtu), ext_feat);
196
197 /* check that packet is not broken for now */
198 while (ext_feat & A2MP_FEAT_EXT) {
199 if (len < sizeof(ext_feat))
200 return -EINVAL;
201
202 ext_feat = get_unaligned_le16(skb->data);
203 BT_DBG("efm 0x%4.4x", ext_feat);
204 len -= sizeof(ext_feat);
205 skb_pull(skb, sizeof(ext_feat));
206 }
207
208 cl = (void *) skb->data;
209 while (len >= sizeof(*cl)) {
210 BT_DBG("Remote AMP id %d type %d status %d", cl->id, cl->type,
211 cl->status);
212
6ed971ca 213 if (cl->id != AMP_ID_BREDR && cl->type == HCI_AMP) {
aa09537d
AE
214 struct a2mp_info_req req;
215
2766be48 216 found = true;
aa09537d
AE
217 req.id = cl->id;
218 a2mp_send(mgr, A2MP_GETINFO_REQ, __next_ident(mgr),
219 sizeof(req), &req);
220 }
221
222 len -= sizeof(*cl);
223 cl = (void *) skb_pull(skb, sizeof(*cl));
224 }
225
2766be48
AE
226 /* Fall back to L2CAP init sequence */
227 if (!found) {
228 struct l2cap_conn *conn = mgr->l2cap_conn;
229 struct l2cap_chan *chan;
230
231 mutex_lock(&conn->chan_lock);
232
233 list_for_each_entry(chan, &conn->chan_l, list) {
234
235 BT_DBG("chan %p state %s", chan,
236 state_to_string(chan->state));
237
238 if (chan->chan_type == L2CAP_CHAN_CONN_FIX_A2MP)
239 continue;
240
241 l2cap_chan_lock(chan);
242
243 if (chan->state == BT_CONNECT)
244 l2cap_send_conn_req(chan);
245
246 l2cap_chan_unlock(chan);
247 }
248
249 mutex_unlock(&conn->chan_lock);
250 }
251
aa09537d
AE
252 return 0;
253}
254
329d81af
AE
255static int a2mp_change_notify(struct amp_mgr *mgr, struct sk_buff *skb,
256 struct a2mp_cmd *hdr)
257{
258 struct a2mp_cl *cl = (void *) skb->data;
259
260 while (skb->len >= sizeof(*cl)) {
261 BT_DBG("Controller id %d type %d status %d", cl->id, cl->type,
262 cl->status);
263 cl = (struct a2mp_cl *) skb_pull(skb, sizeof(*cl));
264 }
265
266 /* TODO send A2MP_CHANGE_RSP */
267
268 return 0;
269}
270
47f2d97d
AE
271static int a2mp_getinfo_req(struct amp_mgr *mgr, struct sk_buff *skb,
272 struct a2mp_cmd *hdr)
273{
274 struct a2mp_info_req *req = (void *) skb->data;
47f2d97d
AE
275 struct hci_dev *hdev;
276
277 if (le16_to_cpu(hdr->len) < sizeof(*req))
278 return -EINVAL;
279
280 BT_DBG("id %d", req->id);
281
47f2d97d 282 hdev = hci_dev_get(req->id);
bc8dce4f 283 if (!hdev || hdev->dev_type != HCI_AMP) {
8e2a0d92
AE
284 struct a2mp_info_rsp rsp;
285
286 rsp.id = req->id;
287 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
288
289 a2mp_send(mgr, A2MP_GETINFO_RSP, hdr->ident, sizeof(rsp),
290 &rsp);
47f2d97d 291
bc8dce4f 292 goto done;
8e2a0d92 293 }
47f2d97d 294
cb6801c6 295 set_bit(READ_LOC_AMP_INFO, &mgr->state);
bc8dce4f
AE
296 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL);
297
298done:
299 if (hdev)
300 hci_dev_put(hdev);
47f2d97d
AE
301
302 skb_pull(skb, sizeof(*req));
303 return 0;
304}
305
0d868de9
AE
306static int a2mp_getinfo_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
307 struct a2mp_cmd *hdr)
308{
309 struct a2mp_info_rsp *rsp = (struct a2mp_info_rsp *) skb->data;
310 struct a2mp_amp_assoc_req req;
311 struct amp_ctrl *ctrl;
312
313 if (le16_to_cpu(hdr->len) < sizeof(*rsp))
314 return -EINVAL;
315
316 BT_DBG("id %d status 0x%2.2x", rsp->id, rsp->status);
317
318 if (rsp->status)
319 return -EINVAL;
320
fa4ebc66 321 ctrl = amp_ctrl_add(mgr, rsp->id);
0d868de9
AE
322 if (!ctrl)
323 return -ENOMEM;
324
0d868de9
AE
325 req.id = rsp->id;
326 a2mp_send(mgr, A2MP_GETAMPASSOC_REQ, __next_ident(mgr), sizeof(req),
327 &req);
328
329 skb_pull(skb, sizeof(*rsp));
330 return 0;
331}
332
a28381dc
AE
333static int a2mp_getampassoc_req(struct amp_mgr *mgr, struct sk_buff *skb,
334 struct a2mp_cmd *hdr)
335{
336 struct a2mp_amp_assoc_req *req = (void *) skb->data;
337 struct hci_dev *hdev;
903e4541 338 struct amp_mgr *tmp;
a28381dc
AE
339
340 if (le16_to_cpu(hdr->len) < sizeof(*req))
341 return -EINVAL;
342
343 BT_DBG("id %d", req->id);
344
903e4541
AE
345 /* Make sure that other request is not processed */
346 tmp = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC);
347
a28381dc 348 hdev = hci_dev_get(req->id);
ece69126 349 if (!hdev || hdev->amp_type == AMP_TYPE_BREDR || tmp) {
a28381dc
AE
350 struct a2mp_amp_assoc_rsp rsp;
351 rsp.id = req->id;
903e4541
AE
352
353 if (tmp) {
354 rsp.status = A2MP_STATUS_COLLISION_OCCURED;
355 amp_mgr_put(tmp);
356 } else {
357 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
358 }
a28381dc
AE
359
360 a2mp_send(mgr, A2MP_GETAMPASSOC_RSP, hdr->ident, sizeof(rsp),
361 &rsp);
903e4541
AE
362
363 goto done;
a28381dc
AE
364 }
365
903e4541 366 amp_read_loc_assoc(hdev, mgr);
a28381dc 367
903e4541 368done:
a28381dc
AE
369 if (hdev)
370 hci_dev_put(hdev);
371
372 skb_pull(skb, sizeof(*req));
373 return 0;
374}
375
9a5e94db
AE
376static int a2mp_getampassoc_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
377 struct a2mp_cmd *hdr)
378{
379 struct a2mp_amp_assoc_rsp *rsp = (void *) skb->data;
380 u16 len = le16_to_cpu(hdr->len);
381 struct hci_dev *hdev;
382 struct amp_ctrl *ctrl;
383 struct hci_conn *hcon;
13465c0a 384 size_t assoc_len;
9a5e94db
AE
385
386 if (len < sizeof(*rsp))
387 return -EINVAL;
388
13465c0a
AE
389 assoc_len = len - sizeof(*rsp);
390
391 BT_DBG("id %d status 0x%2.2x assoc len %zu", rsp->id, rsp->status,
392 assoc_len);
9a5e94db
AE
393
394 if (rsp->status)
395 return -EINVAL;
396
397 /* Save remote ASSOC data */
398 ctrl = amp_ctrl_lookup(mgr, rsp->id);
399 if (ctrl) {
13465c0a 400 u8 *assoc;
9a5e94db 401
5ae327f0 402 assoc = kmemdup(rsp->amp_assoc, assoc_len, GFP_KERNEL);
9a5e94db
AE
403 if (!assoc) {
404 amp_ctrl_put(ctrl);
405 return -ENOMEM;
406 }
407
9a5e94db
AE
408 ctrl->assoc = assoc;
409 ctrl->assoc_len = assoc_len;
410 ctrl->assoc_rem_len = assoc_len;
411 ctrl->assoc_len_so_far = 0;
412
413 amp_ctrl_put(ctrl);
414 }
415
416 /* Create Phys Link */
417 hdev = hci_dev_get(rsp->id);
418 if (!hdev)
419 return -EINVAL;
420
a0c234fe 421 hcon = phylink_add(hdev, mgr, rsp->id, true);
9a5e94db
AE
422 if (!hcon)
423 goto done;
424
425 BT_DBG("Created hcon %p: loc:%d -> rem:%d", hcon, hdev->id, rsp->id);
426
fffadc08 427 mgr->bredr_chan->remote_amp_id = rsp->id;
9495b2ee 428
a02226d6
AE
429 amp_create_phylink(hdev, mgr, hcon);
430
9a5e94db
AE
431done:
432 hci_dev_put(hdev);
433 skb_pull(skb, len);
434 return 0;
435}
436
e072f5da
AE
437static int a2mp_createphyslink_req(struct amp_mgr *mgr, struct sk_buff *skb,
438 struct a2mp_cmd *hdr)
439{
440 struct a2mp_physlink_req *req = (void *) skb->data;
441
442 struct a2mp_physlink_rsp rsp;
443 struct hci_dev *hdev;
cb8488c0 444 struct hci_conn *hcon;
0b26ab9d 445 struct amp_ctrl *ctrl;
e072f5da
AE
446
447 if (le16_to_cpu(hdr->len) < sizeof(*req))
448 return -EINVAL;
449
450 BT_DBG("local_id %d, remote_id %d", req->local_id, req->remote_id);
451
452 rsp.local_id = req->remote_id;
453 rsp.remote_id = req->local_id;
454
455 hdev = hci_dev_get(req->remote_id);
ece69126 456 if (!hdev || hdev->amp_type == AMP_TYPE_BREDR) {
e072f5da
AE
457 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
458 goto send_rsp;
459 }
460
0b26ab9d
AE
461 ctrl = amp_ctrl_lookup(mgr, rsp.remote_id);
462 if (!ctrl) {
fa4ebc66 463 ctrl = amp_ctrl_add(mgr, rsp.remote_id);
0b26ab9d
AE
464 if (ctrl) {
465 amp_ctrl_get(ctrl);
466 } else {
467 rsp.status = A2MP_STATUS_UNABLE_START_LINK_CREATION;
468 goto send_rsp;
469 }
470 }
471
472 if (ctrl) {
13465c0a
AE
473 size_t assoc_len = le16_to_cpu(hdr->len) - sizeof(*req);
474 u8 *assoc;
0b26ab9d 475
5ae327f0 476 assoc = kmemdup(req->amp_assoc, assoc_len, GFP_KERNEL);
0b26ab9d
AE
477 if (!assoc) {
478 amp_ctrl_put(ctrl);
479 return -ENOMEM;
480 }
481
0b26ab9d
AE
482 ctrl->assoc = assoc;
483 ctrl->assoc_len = assoc_len;
484 ctrl->assoc_rem_len = assoc_len;
485 ctrl->assoc_len_so_far = 0;
486
487 amp_ctrl_put(ctrl);
488 }
489
a0c234fe 490 hcon = phylink_add(hdev, mgr, req->local_id, false);
cb8488c0 491 if (hcon) {
dffa3871 492 amp_accept_phylink(hdev, mgr, hcon);
cb8488c0
AE
493 rsp.status = A2MP_STATUS_SUCCESS;
494 } else {
495 rsp.status = A2MP_STATUS_UNABLE_START_LINK_CREATION;
496 }
e072f5da
AE
497
498send_rsp:
499 if (hdev)
500 hci_dev_put(hdev);
501
8e05e3ba
AE
502 /* Reply error now and success after HCI Write Remote AMP Assoc
503 command complete with success status
504 */
505 if (rsp.status != A2MP_STATUS_SUCCESS) {
506 a2mp_send(mgr, A2MP_CREATEPHYSLINK_RSP, hdr->ident,
507 sizeof(rsp), &rsp);
508 } else {
cb6801c6 509 set_bit(WRITE_REMOTE_AMP_ASSOC, &mgr->state);
8e05e3ba
AE
510 mgr->ident = hdr->ident;
511 }
e072f5da
AE
512
513 skb_pull(skb, le16_to_cpu(hdr->len));
514 return 0;
515}
516
6113f84f
AE
517static int a2mp_discphyslink_req(struct amp_mgr *mgr, struct sk_buff *skb,
518 struct a2mp_cmd *hdr)
519{
520 struct a2mp_physlink_req *req = (void *) skb->data;
521 struct a2mp_physlink_rsp rsp;
522 struct hci_dev *hdev;
cb8488c0 523 struct hci_conn *hcon;
6113f84f
AE
524
525 if (le16_to_cpu(hdr->len) < sizeof(*req))
526 return -EINVAL;
527
528 BT_DBG("local_id %d remote_id %d", req->local_id, req->remote_id);
529
530 rsp.local_id = req->remote_id;
531 rsp.remote_id = req->local_id;
532 rsp.status = A2MP_STATUS_SUCCESS;
533
cb8488c0 534 hdev = hci_dev_get(req->remote_id);
6113f84f
AE
535 if (!hdev) {
536 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
537 goto send_rsp;
538 }
539
cb8488c0
AE
540 hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK, mgr->l2cap_conn->dst);
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
6b44d9b8 651 rej.reason = __constant_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
674static void a2mp_chan_state_change_cb(struct l2cap_chan *chan, int state)
675{
676 struct amp_mgr *mgr = chan->data;
677
678 if (!mgr)
679 return;
680
681 BT_DBG("chan %p state %s", chan, state_to_string(state));
682
683 chan->state = state;
684
685 switch (state) {
686 case BT_CLOSED:
687 if (mgr)
688 amp_mgr_put(mgr);
689 break;
690 }
691}
692
693static struct sk_buff *a2mp_chan_alloc_skb_cb(struct l2cap_chan *chan,
694 unsigned long len, int nb)
695{
696 return bt_skb_alloc(len, GFP_KERNEL);
697}
698
466f8004
AE
699static struct l2cap_ops a2mp_chan_ops = {
700 .name = "L2CAP A2MP channel",
6b44d9b8 701 .recv = a2mp_chan_recv_cb,
46d5c908
AE
702 .close = a2mp_chan_close_cb,
703 .state_change = a2mp_chan_state_change_cb,
704 .alloc_skb = a2mp_chan_alloc_skb_cb,
705
706 /* Not implemented for A2MP */
7e1af8a3
GP
707 .new_connection = l2cap_chan_no_new_connection,
708 .teardown = l2cap_chan_no_teardown,
709 .ready = l2cap_chan_no_ready,
2dc4e510 710 .defer = l2cap_chan_no_defer,
466f8004
AE
711};
712
93c3e8f5 713static struct l2cap_chan *a2mp_chan_open(struct l2cap_conn *conn, bool locked)
466f8004
AE
714{
715 struct l2cap_chan *chan;
716 int err;
717
718 chan = l2cap_chan_create();
719 if (!chan)
720 return NULL;
721
722 BT_DBG("chan %p", chan);
723
416fa752 724 chan->chan_type = L2CAP_CHAN_CONN_FIX_A2MP;
466f8004
AE
725 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
726
727 chan->ops = &a2mp_chan_ops;
728
729 l2cap_chan_set_defaults(chan);
730 chan->remote_max_tx = chan->max_tx;
731 chan->remote_tx_win = chan->tx_win;
732
733 chan->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO;
734 chan->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO;
735
736 skb_queue_head_init(&chan->tx_q);
737
738 chan->mode = L2CAP_MODE_ERTM;
739
740 err = l2cap_ertm_init(chan);
741 if (err < 0) {
742 l2cap_chan_del(chan, 0);
743 return NULL;
744 }
745
746 chan->conf_state = 0;
747
93c3e8f5
AE
748 if (locked)
749 __l2cap_chan_add(conn, chan);
750 else
751 l2cap_chan_add(conn, chan);
466f8004
AE
752
753 chan->remote_mps = chan->omtu;
754 chan->mps = chan->omtu;
755
756 chan->state = BT_CONNECTED;
757
758 return chan;
759}
9740e49d
AE
760
761/* AMP Manager functions */
f706adfe 762struct amp_mgr *amp_mgr_get(struct amp_mgr *mgr)
9740e49d 763{
a0dfe0ab 764 BT_DBG("mgr %p orig refcnt %d", mgr, atomic_read(&mgr->kref.refcount));
9740e49d
AE
765
766 kref_get(&mgr->kref);
f706adfe
AE
767
768 return mgr;
9740e49d
AE
769}
770
771static void amp_mgr_destroy(struct kref *kref)
772{
773 struct amp_mgr *mgr = container_of(kref, struct amp_mgr, kref);
774
775 BT_DBG("mgr %p", mgr);
776
f97268fc
AE
777 mutex_lock(&amp_mgr_list_lock);
778 list_del(&mgr->list);
779 mutex_unlock(&amp_mgr_list_lock);
780
52c0d6e5 781 amp_ctrl_list_flush(mgr);
9740e49d
AE
782 kfree(mgr);
783}
784
785int amp_mgr_put(struct amp_mgr *mgr)
786{
a0dfe0ab 787 BT_DBG("mgr %p orig refcnt %d", mgr, atomic_read(&mgr->kref.refcount));
9740e49d
AE
788
789 return kref_put(&mgr->kref, &amp_mgr_destroy);
790}
791
93c3e8f5 792static struct amp_mgr *amp_mgr_create(struct l2cap_conn *conn, bool locked)
9740e49d
AE
793{
794 struct amp_mgr *mgr;
795 struct l2cap_chan *chan;
796
797 mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
798 if (!mgr)
799 return NULL;
800
801 BT_DBG("conn %p mgr %p", conn, mgr);
802
803 mgr->l2cap_conn = conn;
804
93c3e8f5 805 chan = a2mp_chan_open(conn, locked);
9740e49d
AE
806 if (!chan) {
807 kfree(mgr);
808 return NULL;
809 }
810
811 mgr->a2mp_chan = chan;
812 chan->data = mgr;
813
814 conn->hcon->amp_mgr = mgr;
815
816 kref_init(&mgr->kref);
817
52c0d6e5
AE
818 /* Remote AMP ctrl list initialization */
819 INIT_LIST_HEAD(&mgr->amp_ctrls);
820 mutex_init(&mgr->amp_ctrls_lock);
821
f97268fc
AE
822 mutex_lock(&amp_mgr_list_lock);
823 list_add(&mgr->list, &amp_mgr_list);
824 mutex_unlock(&amp_mgr_list_lock);
825
9740e49d
AE
826 return mgr;
827}
97e8e89d
AE
828
829struct l2cap_chan *a2mp_channel_create(struct l2cap_conn *conn,
830 struct sk_buff *skb)
831{
832 struct amp_mgr *mgr;
833
93c3e8f5 834 mgr = amp_mgr_create(conn, false);
97e8e89d
AE
835 if (!mgr) {
836 BT_ERR("Could not create AMP manager");
837 return NULL;
838 }
839
840 BT_DBG("mgr: %p chan %p", mgr, mgr->a2mp_chan);
841
842 return mgr->a2mp_chan;
843}
f97268fc
AE
844
845struct amp_mgr *amp_mgr_lookup_by_state(u8 state)
846{
847 struct amp_mgr *mgr;
848
849 mutex_lock(&amp_mgr_list_lock);
850 list_for_each_entry(mgr, &amp_mgr_list, list) {
cb6801c6 851 if (test_and_clear_bit(state, &mgr->state)) {
f97268fc
AE
852 amp_mgr_get(mgr);
853 mutex_unlock(&amp_mgr_list_lock);
854 return mgr;
855 }
856 }
857 mutex_unlock(&amp_mgr_list_lock);
858
859 return NULL;
860}
8e2a0d92
AE
861
862void a2mp_send_getinfo_rsp(struct hci_dev *hdev)
863{
864 struct amp_mgr *mgr;
865 struct a2mp_info_rsp rsp;
866
867 mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_INFO);
868 if (!mgr)
869 return;
870
871 BT_DBG("%s mgr %p", hdev->name, mgr);
872
873 rsp.id = hdev->id;
874 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
875
ece69126 876 if (hdev->amp_type != AMP_TYPE_BREDR) {
8e2a0d92
AE
877 rsp.status = 0;
878 rsp.total_bw = cpu_to_le32(hdev->amp_total_bw);
879 rsp.max_bw = cpu_to_le32(hdev->amp_max_bw);
880 rsp.min_latency = cpu_to_le32(hdev->amp_min_latency);
881 rsp.pal_cap = cpu_to_le16(hdev->amp_pal_cap);
882 rsp.assoc_size = cpu_to_le16(hdev->amp_assoc_size);
883 }
884
885 a2mp_send(mgr, A2MP_GETINFO_RSP, mgr->ident, sizeof(rsp), &rsp);
886 amp_mgr_put(mgr);
887}
903e4541
AE
888
889void a2mp_send_getampassoc_rsp(struct hci_dev *hdev, u8 status)
890{
891 struct amp_mgr *mgr;
892 struct amp_assoc *loc_assoc = &hdev->loc_assoc;
893 struct a2mp_amp_assoc_rsp *rsp;
894 size_t len;
895
896 mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC);
897 if (!mgr)
898 return;
899
900 BT_DBG("%s mgr %p", hdev->name, mgr);
901
902 len = sizeof(struct a2mp_amp_assoc_rsp) + loc_assoc->len;
903 rsp = kzalloc(len, GFP_KERNEL);
904 if (!rsp) {
905 amp_mgr_put(mgr);
906 return;
907 }
908
909 rsp->id = hdev->id;
910
911 if (status) {
912 rsp->status = A2MP_STATUS_INVALID_CTRL_ID;
913 } else {
914 rsp->status = A2MP_STATUS_SUCCESS;
915 memcpy(rsp->amp_assoc, loc_assoc->data, loc_assoc->len);
916 }
917
918 a2mp_send(mgr, A2MP_GETAMPASSOC_RSP, mgr->ident, len, rsp);
919 amp_mgr_put(mgr);
920 kfree(rsp);
921}
93c3e8f5 922
9495b2ee
AE
923void a2mp_send_create_phy_link_req(struct hci_dev *hdev, u8 status)
924{
925 struct amp_mgr *mgr;
926 struct amp_assoc *loc_assoc = &hdev->loc_assoc;
927 struct a2mp_physlink_req *req;
928 struct l2cap_chan *bredr_chan;
929 size_t len;
930
931 mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC_FINAL);
932 if (!mgr)
933 return;
934
935 len = sizeof(*req) + loc_assoc->len;
936
937 BT_DBG("%s mgr %p assoc_len %zu", hdev->name, mgr, len);
938
939 req = kzalloc(len, GFP_KERNEL);
940 if (!req) {
941 amp_mgr_put(mgr);
942 return;
943 }
944
945 bredr_chan = mgr->bredr_chan;
946 if (!bredr_chan)
947 goto clean;
948
949 req->local_id = hdev->id;
fffadc08 950 req->remote_id = bredr_chan->remote_amp_id;
9495b2ee
AE
951 memcpy(req->amp_assoc, loc_assoc->data, loc_assoc->len);
952
953 a2mp_send(mgr, A2MP_CREATEPHYSLINK_REQ, __next_ident(mgr), len, req);
954
955clean:
956 amp_mgr_put(mgr);
957 kfree(req);
958}
959
8e05e3ba
AE
960void a2mp_send_create_phy_link_rsp(struct hci_dev *hdev, u8 status)
961{
962 struct amp_mgr *mgr;
963 struct a2mp_physlink_rsp rsp;
964 struct hci_conn *hs_hcon;
965
966 mgr = amp_mgr_lookup_by_state(WRITE_REMOTE_AMP_ASSOC);
967 if (!mgr)
968 return;
969
970 hs_hcon = hci_conn_hash_lookup_state(hdev, AMP_LINK, BT_CONNECT);
971 if (!hs_hcon) {
972 rsp.status = A2MP_STATUS_UNABLE_START_LINK_CREATION;
973 } else {
974 rsp.remote_id = hs_hcon->remote_id;
975 rsp.status = A2MP_STATUS_SUCCESS;
976 }
977
978 BT_DBG("%s mgr %p hs_hcon %p status %u", hdev->name, mgr, hs_hcon,
979 status);
980
981 rsp.local_id = hdev->id;
982 a2mp_send(mgr, A2MP_CREATEPHYSLINK_RSP, mgr->ident, sizeof(rsp), &rsp);
983 amp_mgr_put(mgr);
984}
985
93c3e8f5
AE
986void a2mp_discover_amp(struct l2cap_chan *chan)
987{
988 struct l2cap_conn *conn = chan->conn;
989 struct amp_mgr *mgr = conn->hcon->amp_mgr;
990 struct a2mp_discov_req req;
991
992 BT_DBG("chan %p conn %p mgr %p", chan, conn, mgr);
993
994 if (!mgr) {
995 mgr = amp_mgr_create(conn, true);
996 if (!mgr)
997 return;
998 }
999
1000 mgr->bredr_chan = chan;
1001
1002 req.mtu = cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU);
1003 req.ext_feat = 0;
1004 a2mp_send(mgr, A2MP_DISCOVER_REQ, 1, sizeof(req), &req);
1005}
This page took 0.124572 seconds and 5 git commands to generate.