1 // SPDX-License-Identifier: GPL-2.0-only
3 Copyright (c) 2010,2011 Code Aurora Forum. All rights reserved.
4 Copyright (c) 2011,2012 Intel Corp.
8 #include <net/bluetooth/bluetooth.h>
9 #include <net/bluetooth/hci_core.h>
10 #include <net/bluetooth/l2cap.h>
12 #include "hci_request.h"
16 #define A2MP_FEAT_EXT 0x8000
18 /* Global AMP Manager list */
19 static LIST_HEAD(amp_mgr_list
);
20 static DEFINE_MUTEX(amp_mgr_list_lock
);
22 /* A2MP build & send command helper functions */
23 static struct a2mp_cmd
*__a2mp_build(u8 code
, u8 ident
, u16 len
, void *data
)
28 plen
= sizeof(*cmd
) + len
;
29 cmd
= kzalloc(plen
, GFP_KERNEL
);
35 cmd
->len
= cpu_to_le16(len
);
37 memcpy(cmd
->data
, data
, len
);
42 static void a2mp_send(struct amp_mgr
*mgr
, u8 code
, u8 ident
, u16 len
, void *data
)
44 struct l2cap_chan
*chan
= mgr
->a2mp_chan
;
46 u16 total_len
= len
+ sizeof(*cmd
);
50 cmd
= __a2mp_build(code
, ident
, len
, data
);
55 iv
.iov_len
= total_len
;
57 memset(&msg
, 0, sizeof(msg
));
59 iov_iter_kvec(&msg
.msg_iter
, WRITE
, &iv
, 1, total_len
);
61 l2cap_chan_send(chan
, &msg
, total_len
);
66 static u8
__next_ident(struct amp_mgr
*mgr
)
68 if (++mgr
->ident
== 0)
74 static struct amp_mgr
*amp_mgr_lookup_by_state(u8 state
)
78 mutex_lock(&_mgr_list_lock
);
79 list_for_each_entry(mgr
, &_mgr_list
, list
) {
80 if (test_and_clear_bit(state
, &mgr
->state
)) {
82 mutex_unlock(&_mgr_list_lock
);
86 mutex_unlock(&_mgr_list_lock
);
91 /* hci_dev_list shall be locked */
92 static void __a2mp_add_cl(struct amp_mgr
*mgr
, struct a2mp_cl
*cl
)
97 cl
[0].id
= AMP_ID_BREDR
;
98 cl
[0].type
= AMP_TYPE_BREDR
;
99 cl
[0].status
= AMP_STATUS_BLUETOOTH_ONLY
;
101 list_for_each_entry(hdev
, &hci_dev_list
, list
) {
102 if (hdev
->dev_type
== HCI_AMP
) {
104 cl
[i
].type
= hdev
->amp_type
;
105 if (test_bit(HCI_UP
, &hdev
->flags
))
106 cl
[i
].status
= hdev
->amp_status
;
108 cl
[i
].status
= AMP_STATUS_POWERED_DOWN
;
114 /* Processing A2MP messages */
115 static int a2mp_command_rej(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
116 struct a2mp_cmd
*hdr
)
118 struct a2mp_cmd_rej
*rej
= (void *) skb
->data
;
120 if (le16_to_cpu(hdr
->len
) < sizeof(*rej
))
123 BT_DBG("ident %d reason %d", hdr
->ident
, le16_to_cpu(rej
->reason
));
125 skb_pull(skb
, sizeof(*rej
));
130 static int a2mp_discover_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
131 struct a2mp_cmd
*hdr
)
133 struct a2mp_discov_req
*req
= (void *) skb
->data
;
134 u16 len
= le16_to_cpu(hdr
->len
);
135 struct a2mp_discov_rsp
*rsp
;
138 struct hci_dev
*hdev
;
140 if (len
< sizeof(*req
))
143 skb_pull(skb
, sizeof(*req
));
145 ext_feat
= le16_to_cpu(req
->ext_feat
);
147 BT_DBG("mtu %d efm 0x%4.4x", le16_to_cpu(req
->mtu
), ext_feat
);
149 /* check that packet is not broken for now */
150 while (ext_feat
& A2MP_FEAT_EXT
) {
151 if (len
< sizeof(ext_feat
))
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
));
160 read_lock(&hci_dev_list_lock
);
162 /* at minimum the BR/EDR needs to be listed */
165 list_for_each_entry(hdev
, &hci_dev_list
, list
) {
166 if (hdev
->dev_type
== HCI_AMP
)
170 len
= struct_size(rsp
, cl
, num_ctrl
);
171 rsp
= kmalloc(len
, GFP_ATOMIC
);
173 read_unlock(&hci_dev_list_lock
);
177 rsp
->mtu
= cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU
);
180 __a2mp_add_cl(mgr
, rsp
->cl
);
182 read_unlock(&hci_dev_list_lock
);
184 a2mp_send(mgr
, A2MP_DISCOVER_RSP
, hdr
->ident
, len
, rsp
);
190 static int a2mp_discover_rsp(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
191 struct a2mp_cmd
*hdr
)
193 struct a2mp_discov_rsp
*rsp
= (void *) skb
->data
;
194 u16 len
= le16_to_cpu(hdr
->len
);
199 if (len
< sizeof(*rsp
))
203 skb_pull(skb
, sizeof(*rsp
));
205 ext_feat
= le16_to_cpu(rsp
->ext_feat
);
207 BT_DBG("mtu %d efm 0x%4.4x", le16_to_cpu(rsp
->mtu
), ext_feat
);
209 /* check that packet is not broken for now */
210 while (ext_feat
& A2MP_FEAT_EXT
) {
211 if (len
< sizeof(ext_feat
))
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
));
220 cl
= (void *) skb
->data
;
221 while (len
>= sizeof(*cl
)) {
222 BT_DBG("Remote AMP id %d type %d status %d", cl
->id
, cl
->type
,
225 if (cl
->id
!= AMP_ID_BREDR
&& cl
->type
!= AMP_TYPE_BREDR
) {
226 struct a2mp_info_req req
;
230 memset(&req
, 0, sizeof(req
));
233 a2mp_send(mgr
, A2MP_GETINFO_REQ
, __next_ident(mgr
),
238 cl
= skb_pull(skb
, sizeof(*cl
));
241 /* Fall back to L2CAP init sequence */
243 struct l2cap_conn
*conn
= mgr
->l2cap_conn
;
244 struct l2cap_chan
*chan
;
246 mutex_lock(&conn
->chan_lock
);
248 list_for_each_entry(chan
, &conn
->chan_l
, list
) {
250 BT_DBG("chan %p state %s", chan
,
251 state_to_string(chan
->state
));
253 if (chan
->scid
== L2CAP_CID_A2MP
)
256 l2cap_chan_lock(chan
);
258 if (chan
->state
== BT_CONNECT
)
259 l2cap_send_conn_req(chan
);
261 l2cap_chan_unlock(chan
);
264 mutex_unlock(&conn
->chan_lock
);
270 static int a2mp_change_notify(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
271 struct a2mp_cmd
*hdr
)
273 struct a2mp_cl
*cl
= (void *) skb
->data
;
275 while (skb
->len
>= sizeof(*cl
)) {
276 BT_DBG("Controller id %d type %d status %d", cl
->id
, cl
->type
,
278 cl
= skb_pull(skb
, sizeof(*cl
));
281 /* TODO send A2MP_CHANGE_RSP */
286 static void read_local_amp_info_complete(struct hci_dev
*hdev
, u8 status
,
289 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
291 a2mp_send_getinfo_rsp(hdev
);
294 static int a2mp_getinfo_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
295 struct a2mp_cmd
*hdr
)
297 struct a2mp_info_req
*req
= (void *) skb
->data
;
298 struct hci_dev
*hdev
;
299 struct hci_request hreq
;
302 if (le16_to_cpu(hdr
->len
) < sizeof(*req
))
305 BT_DBG("id %d", req
->id
);
307 hdev
= hci_dev_get(req
->id
);
308 if (!hdev
|| hdev
->dev_type
!= HCI_AMP
) {
309 struct a2mp_info_rsp rsp
;
311 memset(&rsp
, 0, sizeof(rsp
));
314 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
316 a2mp_send(mgr
, A2MP_GETINFO_RSP
, hdr
->ident
, sizeof(rsp
),
322 set_bit(READ_LOC_AMP_INFO
, &mgr
->state
);
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
);
327 a2mp_send_getinfo_rsp(hdev
);
333 skb_pull(skb
, sizeof(*req
));
337 static int a2mp_getinfo_rsp(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
338 struct a2mp_cmd
*hdr
)
340 struct a2mp_info_rsp
*rsp
= (struct a2mp_info_rsp
*) skb
->data
;
341 struct a2mp_amp_assoc_req req
;
342 struct amp_ctrl
*ctrl
;
344 if (le16_to_cpu(hdr
->len
) < sizeof(*rsp
))
347 BT_DBG("id %d status 0x%2.2x", rsp
->id
, rsp
->status
);
352 ctrl
= amp_ctrl_add(mgr
, rsp
->id
);
356 memset(&req
, 0, sizeof(req
));
359 a2mp_send(mgr
, A2MP_GETAMPASSOC_REQ
, __next_ident(mgr
), sizeof(req
),
362 skb_pull(skb
, sizeof(*rsp
));
366 static int a2mp_getampassoc_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
367 struct a2mp_cmd
*hdr
)
369 struct a2mp_amp_assoc_req
*req
= (void *) skb
->data
;
370 struct hci_dev
*hdev
;
373 if (le16_to_cpu(hdr
->len
) < sizeof(*req
))
376 BT_DBG("id %d", req
->id
);
378 /* Make sure that other request is not processed */
379 tmp
= amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC
);
381 hdev
= hci_dev_get(req
->id
);
382 if (!hdev
|| hdev
->amp_type
== AMP_TYPE_BREDR
|| tmp
) {
383 struct a2mp_amp_assoc_rsp rsp
;
386 memset(&rsp
, 0, sizeof(rsp
));
389 rsp
.status
= A2MP_STATUS_COLLISION_OCCURED
;
392 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
395 a2mp_send(mgr
, A2MP_GETAMPASSOC_RSP
, hdr
->ident
, sizeof(rsp
),
401 amp_read_loc_assoc(hdev
, mgr
);
407 skb_pull(skb
, sizeof(*req
));
411 static int a2mp_getampassoc_rsp(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
412 struct a2mp_cmd
*hdr
)
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
;
421 if (len
< sizeof(*rsp
))
424 assoc_len
= len
- sizeof(*rsp
);
426 BT_DBG("id %d status 0x%2.2x assoc len %zu", rsp
->id
, rsp
->status
,
432 /* Save remote ASSOC data */
433 ctrl
= amp_ctrl_lookup(mgr
, rsp
->id
);
437 assoc
= kmemdup(rsp
->amp_assoc
, assoc_len
, GFP_KERNEL
);
444 ctrl
->assoc_len
= assoc_len
;
445 ctrl
->assoc_rem_len
= assoc_len
;
446 ctrl
->assoc_len_so_far
= 0;
451 /* Create Phys Link */
452 hdev
= hci_dev_get(rsp
->id
);
456 hcon
= phylink_add(hdev
, mgr
, rsp
->id
, true);
460 BT_DBG("Created hcon %p: loc:%d -> rem:%d", hcon
, hdev
->id
, rsp
->id
);
462 mgr
->bredr_chan
->remote_amp_id
= rsp
->id
;
464 amp_create_phylink(hdev
, mgr
, hcon
);
472 static int a2mp_createphyslink_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
473 struct a2mp_cmd
*hdr
)
475 struct a2mp_physlink_req
*req
= (void *) skb
->data
;
476 struct a2mp_physlink_rsp rsp
;
477 struct hci_dev
*hdev
;
478 struct hci_conn
*hcon
;
479 struct amp_ctrl
*ctrl
;
481 if (le16_to_cpu(hdr
->len
) < sizeof(*req
))
484 BT_DBG("local_id %d, remote_id %d", req
->local_id
, req
->remote_id
);
486 memset(&rsp
, 0, sizeof(rsp
));
488 rsp
.local_id
= req
->remote_id
;
489 rsp
.remote_id
= req
->local_id
;
491 hdev
= hci_dev_get(req
->remote_id
);
492 if (!hdev
|| hdev
->amp_type
== AMP_TYPE_BREDR
) {
493 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
497 ctrl
= amp_ctrl_lookup(mgr
, rsp
.remote_id
);
499 ctrl
= amp_ctrl_add(mgr
, rsp
.remote_id
);
503 rsp
.status
= A2MP_STATUS_UNABLE_START_LINK_CREATION
;
509 size_t assoc_len
= le16_to_cpu(hdr
->len
) - sizeof(*req
);
512 assoc
= kmemdup(req
->amp_assoc
, assoc_len
, GFP_KERNEL
);
519 ctrl
->assoc_len
= assoc_len
;
520 ctrl
->assoc_rem_len
= assoc_len
;
521 ctrl
->assoc_len_so_far
= 0;
526 hcon
= phylink_add(hdev
, mgr
, req
->local_id
, false);
528 amp_accept_phylink(hdev
, mgr
, hcon
);
529 rsp
.status
= A2MP_STATUS_SUCCESS
;
531 rsp
.status
= A2MP_STATUS_UNABLE_START_LINK_CREATION
;
538 /* Reply error now and success after HCI Write Remote AMP Assoc
539 command complete with success status
541 if (rsp
.status
!= A2MP_STATUS_SUCCESS
) {
542 a2mp_send(mgr
, A2MP_CREATEPHYSLINK_RSP
, hdr
->ident
,
545 set_bit(WRITE_REMOTE_AMP_ASSOC
, &mgr
->state
);
546 mgr
->ident
= hdr
->ident
;
549 skb_pull(skb
, le16_to_cpu(hdr
->len
));
553 static int a2mp_discphyslink_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
554 struct a2mp_cmd
*hdr
)
556 struct a2mp_physlink_req
*req
= (void *) skb
->data
;
557 struct a2mp_physlink_rsp rsp
;
558 struct hci_dev
*hdev
;
559 struct hci_conn
*hcon
;
561 if (le16_to_cpu(hdr
->len
) < sizeof(*req
))
564 BT_DBG("local_id %d remote_id %d", req
->local_id
, req
->remote_id
);
566 memset(&rsp
, 0, sizeof(rsp
));
568 rsp
.local_id
= req
->remote_id
;
569 rsp
.remote_id
= req
->local_id
;
570 rsp
.status
= A2MP_STATUS_SUCCESS
;
572 hdev
= hci_dev_get(req
->remote_id
);
574 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
578 hcon
= hci_conn_hash_lookup_ba(hdev
, AMP_LINK
,
579 &mgr
->l2cap_conn
->hcon
->dst
);
581 bt_dev_err(hdev
, "no phys link exist");
582 rsp
.status
= A2MP_STATUS_NO_PHYSICAL_LINK_EXISTS
;
586 /* TODO Disconnect Phys Link here */
592 a2mp_send(mgr
, A2MP_DISCONNPHYSLINK_RSP
, hdr
->ident
, sizeof(rsp
), &rsp
);
594 skb_pull(skb
, sizeof(*req
));
598 static inline int a2mp_cmd_rsp(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
599 struct a2mp_cmd
*hdr
)
601 BT_DBG("ident %d code 0x%2.2x", hdr
->ident
, hdr
->code
);
603 skb_pull(skb
, le16_to_cpu(hdr
->len
));
607 /* Handle A2MP signalling */
608 static int a2mp_chan_recv_cb(struct l2cap_chan
*chan
, struct sk_buff
*skb
)
610 struct a2mp_cmd
*hdr
;
611 struct amp_mgr
*mgr
= chan
->data
;
616 while (skb
->len
>= sizeof(*hdr
)) {
619 hdr
= (void *) skb
->data
;
620 len
= le16_to_cpu(hdr
->len
);
622 BT_DBG("code 0x%2.2x id %d len %u", hdr
->code
, hdr
->ident
, len
);
624 skb_pull(skb
, sizeof(*hdr
));
626 if (len
> skb
->len
|| !hdr
->ident
) {
631 mgr
->ident
= hdr
->ident
;
634 case A2MP_COMMAND_REJ
:
635 a2mp_command_rej(mgr
, skb
, hdr
);
638 case A2MP_DISCOVER_REQ
:
639 err
= a2mp_discover_req(mgr
, skb
, hdr
);
642 case A2MP_CHANGE_NOTIFY
:
643 err
= a2mp_change_notify(mgr
, skb
, hdr
);
646 case A2MP_GETINFO_REQ
:
647 err
= a2mp_getinfo_req(mgr
, skb
, hdr
);
650 case A2MP_GETAMPASSOC_REQ
:
651 err
= a2mp_getampassoc_req(mgr
, skb
, hdr
);
654 case A2MP_CREATEPHYSLINK_REQ
:
655 err
= a2mp_createphyslink_req(mgr
, skb
, hdr
);
658 case A2MP_DISCONNPHYSLINK_REQ
:
659 err
= a2mp_discphyslink_req(mgr
, skb
, hdr
);
662 case A2MP_DISCOVER_RSP
:
663 err
= a2mp_discover_rsp(mgr
, skb
, hdr
);
666 case A2MP_GETINFO_RSP
:
667 err
= a2mp_getinfo_rsp(mgr
, skb
, hdr
);
670 case A2MP_GETAMPASSOC_RSP
:
671 err
= a2mp_getampassoc_rsp(mgr
, skb
, hdr
);
674 case A2MP_CHANGE_RSP
:
675 case A2MP_CREATEPHYSLINK_RSP
:
676 case A2MP_DISCONNPHYSLINK_RSP
:
677 err
= a2mp_cmd_rsp(mgr
, skb
, hdr
);
681 BT_ERR("Unknown A2MP sig cmd 0x%2.2x", hdr
->code
);
688 struct a2mp_cmd_rej rej
;
690 memset(&rej
, 0, sizeof(rej
));
692 rej
.reason
= cpu_to_le16(0);
693 hdr
= (void *) skb
->data
;
695 BT_DBG("Send A2MP Rej: cmd 0x%2.2x err %d", hdr
->code
, err
);
697 a2mp_send(mgr
, A2MP_COMMAND_REJ
, hdr
->ident
, sizeof(rej
),
701 /* Always free skb and return success error code to prevent
702 from sending L2CAP Disconnect over A2MP channel */
710 static void a2mp_chan_close_cb(struct l2cap_chan
*chan
)
712 l2cap_chan_put(chan
);
715 static void a2mp_chan_state_change_cb(struct l2cap_chan
*chan
, int state
,
718 struct amp_mgr
*mgr
= chan
->data
;
723 BT_DBG("chan %p state %s", chan
, state_to_string(state
));
735 static struct sk_buff
*a2mp_chan_alloc_skb_cb(struct l2cap_chan
*chan
,
736 unsigned long hdr_len
,
737 unsigned long len
, int nb
)
741 skb
= bt_skb_alloc(hdr_len
+ len
, GFP_KERNEL
);
743 return ERR_PTR(-ENOMEM
);
748 static const struct l2cap_ops a2mp_chan_ops
= {
749 .name
= "L2CAP A2MP channel",
750 .recv
= a2mp_chan_recv_cb
,
751 .close
= a2mp_chan_close_cb
,
752 .state_change
= a2mp_chan_state_change_cb
,
753 .alloc_skb
= a2mp_chan_alloc_skb_cb
,
755 /* Not implemented for A2MP */
756 .new_connection
= l2cap_chan_no_new_connection
,
757 .teardown
= l2cap_chan_no_teardown
,
758 .ready
= l2cap_chan_no_ready
,
759 .defer
= l2cap_chan_no_defer
,
760 .resume
= l2cap_chan_no_resume
,
761 .set_shutdown
= l2cap_chan_no_set_shutdown
,
762 .get_sndtimeo
= l2cap_chan_no_get_sndtimeo
,
765 static struct l2cap_chan
*a2mp_chan_open(struct l2cap_conn
*conn
, bool locked
)
767 struct l2cap_chan
*chan
;
770 chan
= l2cap_chan_create();
774 BT_DBG("chan %p", chan
);
776 chan
->chan_type
= L2CAP_CHAN_FIXED
;
777 chan
->scid
= L2CAP_CID_A2MP
;
778 chan
->dcid
= L2CAP_CID_A2MP
;
779 chan
->omtu
= L2CAP_A2MP_DEFAULT_MTU
;
780 chan
->imtu
= L2CAP_A2MP_DEFAULT_MTU
;
781 chan
->flush_to
= L2CAP_DEFAULT_FLUSH_TO
;
783 chan
->ops
= &a2mp_chan_ops
;
785 l2cap_chan_set_defaults(chan
);
786 chan
->remote_max_tx
= chan
->max_tx
;
787 chan
->remote_tx_win
= chan
->tx_win
;
789 chan
->retrans_timeout
= L2CAP_DEFAULT_RETRANS_TO
;
790 chan
->monitor_timeout
= L2CAP_DEFAULT_MONITOR_TO
;
792 skb_queue_head_init(&chan
->tx_q
);
794 chan
->mode
= L2CAP_MODE_ERTM
;
796 err
= l2cap_ertm_init(chan
);
798 l2cap_chan_del(chan
, 0);
802 chan
->conf_state
= 0;
805 __l2cap_chan_add(conn
, chan
);
807 l2cap_chan_add(conn
, chan
);
809 chan
->remote_mps
= chan
->omtu
;
810 chan
->mps
= chan
->omtu
;
812 chan
->state
= BT_CONNECTED
;
817 /* AMP Manager functions */
818 struct amp_mgr
*amp_mgr_get(struct amp_mgr
*mgr
)
820 BT_DBG("mgr %p orig refcnt %d", mgr
, kref_read(&mgr
->kref
));
822 kref_get(&mgr
->kref
);
827 static void amp_mgr_destroy(struct kref
*kref
)
829 struct amp_mgr
*mgr
= container_of(kref
, struct amp_mgr
, kref
);
831 BT_DBG("mgr %p", mgr
);
833 mutex_lock(&_mgr_list_lock
);
834 list_del(&mgr
->list
);
835 mutex_unlock(&_mgr_list_lock
);
837 amp_ctrl_list_flush(mgr
);
841 int amp_mgr_put(struct amp_mgr
*mgr
)
843 BT_DBG("mgr %p orig refcnt %d", mgr
, kref_read(&mgr
->kref
));
845 return kref_put(&mgr
->kref
, &_mgr_destroy
);
848 static struct amp_mgr
*amp_mgr_create(struct l2cap_conn
*conn
, bool locked
)
851 struct l2cap_chan
*chan
;
853 mgr
= kzalloc(sizeof(*mgr
), GFP_KERNEL
);
857 BT_DBG("conn %p mgr %p", conn
, mgr
);
859 mgr
->l2cap_conn
= conn
;
861 chan
= a2mp_chan_open(conn
, locked
);
867 mgr
->a2mp_chan
= chan
;
870 conn
->hcon
->amp_mgr
= mgr
;
872 kref_init(&mgr
->kref
);
874 /* Remote AMP ctrl list initialization */
875 INIT_LIST_HEAD(&mgr
->amp_ctrls
);
876 mutex_init(&mgr
->amp_ctrls_lock
);
878 mutex_lock(&_mgr_list_lock
);
879 list_add(&mgr
->list
, &_mgr_list
);
880 mutex_unlock(&_mgr_list_lock
);
885 struct l2cap_chan
*a2mp_channel_create(struct l2cap_conn
*conn
,
890 if (conn
->hcon
->type
!= ACL_LINK
)
893 mgr
= amp_mgr_create(conn
, false);
895 BT_ERR("Could not create AMP manager");
899 BT_DBG("mgr: %p chan %p", mgr
, mgr
->a2mp_chan
);
901 return mgr
->a2mp_chan
;
904 void a2mp_send_getinfo_rsp(struct hci_dev
*hdev
)
907 struct a2mp_info_rsp rsp
;
909 mgr
= amp_mgr_lookup_by_state(READ_LOC_AMP_INFO
);
913 BT_DBG("%s mgr %p", hdev
->name
, mgr
);
915 memset(&rsp
, 0, sizeof(rsp
));
918 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
920 if (hdev
->amp_type
!= AMP_TYPE_BREDR
) {
922 rsp
.total_bw
= cpu_to_le32(hdev
->amp_total_bw
);
923 rsp
.max_bw
= cpu_to_le32(hdev
->amp_max_bw
);
924 rsp
.min_latency
= cpu_to_le32(hdev
->amp_min_latency
);
925 rsp
.pal_cap
= cpu_to_le16(hdev
->amp_pal_cap
);
926 rsp
.assoc_size
= cpu_to_le16(hdev
->amp_assoc_size
);
929 a2mp_send(mgr
, A2MP_GETINFO_RSP
, mgr
->ident
, sizeof(rsp
), &rsp
);
933 void a2mp_send_getampassoc_rsp(struct hci_dev
*hdev
, u8 status
)
936 struct amp_assoc
*loc_assoc
= &hdev
->loc_assoc
;
937 struct a2mp_amp_assoc_rsp
*rsp
;
940 mgr
= amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC
);
944 BT_DBG("%s mgr %p", hdev
->name
, mgr
);
946 len
= sizeof(struct a2mp_amp_assoc_rsp
) + loc_assoc
->len
;
947 rsp
= kzalloc(len
, GFP_KERNEL
);
956 rsp
->status
= A2MP_STATUS_INVALID_CTRL_ID
;
958 rsp
->status
= A2MP_STATUS_SUCCESS
;
959 memcpy(rsp
->amp_assoc
, loc_assoc
->data
, loc_assoc
->len
);
962 a2mp_send(mgr
, A2MP_GETAMPASSOC_RSP
, mgr
->ident
, len
, rsp
);
967 void a2mp_send_create_phy_link_req(struct hci_dev
*hdev
, u8 status
)
970 struct amp_assoc
*loc_assoc
= &hdev
->loc_assoc
;
971 struct a2mp_physlink_req
*req
;
972 struct l2cap_chan
*bredr_chan
;
975 mgr
= amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC_FINAL
);
979 len
= sizeof(*req
) + loc_assoc
->len
;
981 BT_DBG("%s mgr %p assoc_len %zu", hdev
->name
, mgr
, len
);
983 req
= kzalloc(len
, GFP_KERNEL
);
989 bredr_chan
= mgr
->bredr_chan
;
993 req
->local_id
= hdev
->id
;
994 req
->remote_id
= bredr_chan
->remote_amp_id
;
995 memcpy(req
->amp_assoc
, loc_assoc
->data
, loc_assoc
->len
);
997 a2mp_send(mgr
, A2MP_CREATEPHYSLINK_REQ
, __next_ident(mgr
), len
, req
);
1004 void a2mp_send_create_phy_link_rsp(struct hci_dev
*hdev
, u8 status
)
1006 struct amp_mgr
*mgr
;
1007 struct a2mp_physlink_rsp rsp
;
1008 struct hci_conn
*hs_hcon
;
1010 mgr
= amp_mgr_lookup_by_state(WRITE_REMOTE_AMP_ASSOC
);
1014 memset(&rsp
, 0, sizeof(rsp
));
1016 hs_hcon
= hci_conn_hash_lookup_state(hdev
, AMP_LINK
, BT_CONNECT
);
1018 rsp
.status
= A2MP_STATUS_UNABLE_START_LINK_CREATION
;
1020 rsp
.remote_id
= hs_hcon
->remote_id
;
1021 rsp
.status
= A2MP_STATUS_SUCCESS
;
1024 BT_DBG("%s mgr %p hs_hcon %p status %u", hdev
->name
, mgr
, hs_hcon
,
1027 rsp
.local_id
= hdev
->id
;
1028 a2mp_send(mgr
, A2MP_CREATEPHYSLINK_RSP
, mgr
->ident
, sizeof(rsp
), &rsp
);
1032 void a2mp_discover_amp(struct l2cap_chan
*chan
)
1034 struct l2cap_conn
*conn
= chan
->conn
;
1035 struct amp_mgr
*mgr
= conn
->hcon
->amp_mgr
;
1036 struct a2mp_discov_req req
;
1038 BT_DBG("chan %p conn %p mgr %p", chan
, conn
, mgr
);
1041 mgr
= amp_mgr_create(conn
, true);
1046 mgr
->bredr_chan
= chan
;
1048 memset(&req
, 0, sizeof(req
));
1050 req
.mtu
= cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU
);
1052 a2mp_send(mgr
, A2MP_DISCOVER_REQ
, 1, sizeof(req
), &req
);