2 Copyright (c) 2010,2011 Code Aurora Forum. All rights reserved.
3 Copyright (c) 2011,2012 Intel Corp.
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.
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.
15 #include <net/bluetooth/bluetooth.h>
16 #include <net/bluetooth/hci_core.h>
17 #include <net/bluetooth/l2cap.h>
22 /* Global AMP Manager list */
23 LIST_HEAD(amp_mgr_list
);
24 DEFINE_MUTEX(amp_mgr_list_lock
);
26 /* A2MP build & send command helper functions */
27 static struct a2mp_cmd
*__a2mp_build(u8 code
, u8 ident
, u16 len
, void *data
)
32 plen
= sizeof(*cmd
) + len
;
33 cmd
= kzalloc(plen
, GFP_KERNEL
);
39 cmd
->len
= cpu_to_le16(len
);
41 memcpy(cmd
->data
, data
, len
);
46 void a2mp_send(struct amp_mgr
*mgr
, u8 code
, u8 ident
, u16 len
, void *data
)
48 struct l2cap_chan
*chan
= mgr
->a2mp_chan
;
50 u16 total_len
= len
+ sizeof(*cmd
);
54 cmd
= __a2mp_build(code
, ident
, len
, data
);
59 iv
.iov_len
= total_len
;
61 memset(&msg
, 0, sizeof(msg
));
63 iov_iter_kvec(&msg
.msg_iter
, WRITE
| ITER_KVEC
, &iv
, 1, total_len
);
65 l2cap_chan_send(chan
, &msg
, total_len
);
70 u8
__next_ident(struct amp_mgr
*mgr
)
72 if (++mgr
->ident
== 0)
78 /* hci_dev_list shall be locked */
79 static void __a2mp_add_cl(struct amp_mgr
*mgr
, struct a2mp_cl
*cl
)
84 cl
[0].id
= AMP_ID_BREDR
;
85 cl
[0].type
= AMP_TYPE_BREDR
;
86 cl
[0].status
= AMP_STATUS_BLUETOOTH_ONLY
;
88 list_for_each_entry(hdev
, &hci_dev_list
, list
) {
89 if (hdev
->dev_type
== HCI_AMP
) {
91 cl
[i
].type
= hdev
->amp_type
;
92 if (test_bit(HCI_UP
, &hdev
->flags
))
93 cl
[i
].status
= hdev
->amp_status
;
95 cl
[i
].status
= AMP_STATUS_POWERED_DOWN
;
101 /* Processing A2MP messages */
102 static int a2mp_command_rej(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
103 struct a2mp_cmd
*hdr
)
105 struct a2mp_cmd_rej
*rej
= (void *) skb
->data
;
107 if (le16_to_cpu(hdr
->len
) < sizeof(*rej
))
110 BT_DBG("ident %d reason %d", hdr
->ident
, le16_to_cpu(rej
->reason
));
112 skb_pull(skb
, sizeof(*rej
));
117 static int a2mp_discover_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
118 struct a2mp_cmd
*hdr
)
120 struct a2mp_discov_req
*req
= (void *) skb
->data
;
121 u16 len
= le16_to_cpu(hdr
->len
);
122 struct a2mp_discov_rsp
*rsp
;
125 struct hci_dev
*hdev
;
127 if (len
< sizeof(*req
))
130 skb_pull(skb
, sizeof(*req
));
132 ext_feat
= le16_to_cpu(req
->ext_feat
);
134 BT_DBG("mtu %d efm 0x%4.4x", le16_to_cpu(req
->mtu
), ext_feat
);
136 /* check that packet is not broken for now */
137 while (ext_feat
& A2MP_FEAT_EXT
) {
138 if (len
< sizeof(ext_feat
))
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
));
147 read_lock(&hci_dev_list_lock
);
149 /* at minimum the BR/EDR needs to be listed */
152 list_for_each_entry(hdev
, &hci_dev_list
, list
) {
153 if (hdev
->dev_type
== HCI_AMP
)
157 len
= num_ctrl
* sizeof(struct a2mp_cl
) + sizeof(*rsp
);
158 rsp
= kmalloc(len
, GFP_ATOMIC
);
160 read_unlock(&hci_dev_list_lock
);
164 rsp
->mtu
= cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU
);
167 __a2mp_add_cl(mgr
, rsp
->cl
);
169 read_unlock(&hci_dev_list_lock
);
171 a2mp_send(mgr
, A2MP_DISCOVER_RSP
, hdr
->ident
, len
, rsp
);
177 static int a2mp_discover_rsp(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
178 struct a2mp_cmd
*hdr
)
180 struct a2mp_discov_rsp
*rsp
= (void *) skb
->data
;
181 u16 len
= le16_to_cpu(hdr
->len
);
186 if (len
< sizeof(*rsp
))
190 skb_pull(skb
, sizeof(*rsp
));
192 ext_feat
= le16_to_cpu(rsp
->ext_feat
);
194 BT_DBG("mtu %d efm 0x%4.4x", le16_to_cpu(rsp
->mtu
), ext_feat
);
196 /* check that packet is not broken for now */
197 while (ext_feat
& A2MP_FEAT_EXT
) {
198 if (len
< sizeof(ext_feat
))
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
));
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
,
212 if (cl
->id
!= AMP_ID_BREDR
&& cl
->type
!= AMP_TYPE_BREDR
) {
213 struct a2mp_info_req req
;
217 a2mp_send(mgr
, A2MP_GETINFO_REQ
, __next_ident(mgr
),
222 cl
= (void *) skb_pull(skb
, sizeof(*cl
));
225 /* Fall back to L2CAP init sequence */
227 struct l2cap_conn
*conn
= mgr
->l2cap_conn
;
228 struct l2cap_chan
*chan
;
230 mutex_lock(&conn
->chan_lock
);
232 list_for_each_entry(chan
, &conn
->chan_l
, list
) {
234 BT_DBG("chan %p state %s", chan
,
235 state_to_string(chan
->state
));
237 if (chan
->scid
== L2CAP_CID_A2MP
)
240 l2cap_chan_lock(chan
);
242 if (chan
->state
== BT_CONNECT
)
243 l2cap_send_conn_req(chan
);
245 l2cap_chan_unlock(chan
);
248 mutex_unlock(&conn
->chan_lock
);
254 static int a2mp_change_notify(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
255 struct a2mp_cmd
*hdr
)
257 struct a2mp_cl
*cl
= (void *) skb
->data
;
259 while (skb
->len
>= sizeof(*cl
)) {
260 BT_DBG("Controller id %d type %d status %d", cl
->id
, cl
->type
,
262 cl
= (struct a2mp_cl
*) skb_pull(skb
, sizeof(*cl
));
265 /* TODO send A2MP_CHANGE_RSP */
270 static int a2mp_getinfo_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
271 struct a2mp_cmd
*hdr
)
273 struct a2mp_info_req
*req
= (void *) skb
->data
;
274 struct hci_dev
*hdev
;
276 if (le16_to_cpu(hdr
->len
) < sizeof(*req
))
279 BT_DBG("id %d", req
->id
);
281 hdev
= hci_dev_get(req
->id
);
282 if (!hdev
|| hdev
->dev_type
!= HCI_AMP
) {
283 struct a2mp_info_rsp rsp
;
286 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
288 a2mp_send(mgr
, A2MP_GETINFO_RSP
, hdr
->ident
, sizeof(rsp
),
294 set_bit(READ_LOC_AMP_INFO
, &mgr
->state
);
295 hci_send_cmd(hdev
, HCI_OP_READ_LOCAL_AMP_INFO
, 0, NULL
);
301 skb_pull(skb
, sizeof(*req
));
305 static int a2mp_getinfo_rsp(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
306 struct a2mp_cmd
*hdr
)
308 struct a2mp_info_rsp
*rsp
= (struct a2mp_info_rsp
*) skb
->data
;
309 struct a2mp_amp_assoc_req req
;
310 struct amp_ctrl
*ctrl
;
312 if (le16_to_cpu(hdr
->len
) < sizeof(*rsp
))
315 BT_DBG("id %d status 0x%2.2x", rsp
->id
, rsp
->status
);
320 ctrl
= amp_ctrl_add(mgr
, rsp
->id
);
325 a2mp_send(mgr
, A2MP_GETAMPASSOC_REQ
, __next_ident(mgr
), sizeof(req
),
328 skb_pull(skb
, sizeof(*rsp
));
332 static int a2mp_getampassoc_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
333 struct a2mp_cmd
*hdr
)
335 struct a2mp_amp_assoc_req
*req
= (void *) skb
->data
;
336 struct hci_dev
*hdev
;
339 if (le16_to_cpu(hdr
->len
) < sizeof(*req
))
342 BT_DBG("id %d", req
->id
);
344 /* Make sure that other request is not processed */
345 tmp
= amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC
);
347 hdev
= hci_dev_get(req
->id
);
348 if (!hdev
|| hdev
->amp_type
== AMP_TYPE_BREDR
|| tmp
) {
349 struct a2mp_amp_assoc_rsp rsp
;
353 rsp
.status
= A2MP_STATUS_COLLISION_OCCURED
;
356 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
359 a2mp_send(mgr
, A2MP_GETAMPASSOC_RSP
, hdr
->ident
, sizeof(rsp
),
365 amp_read_loc_assoc(hdev
, mgr
);
371 skb_pull(skb
, sizeof(*req
));
375 static int a2mp_getampassoc_rsp(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
376 struct a2mp_cmd
*hdr
)
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
;
385 if (len
< sizeof(*rsp
))
388 assoc_len
= len
- sizeof(*rsp
);
390 BT_DBG("id %d status 0x%2.2x assoc len %zu", rsp
->id
, rsp
->status
,
396 /* Save remote ASSOC data */
397 ctrl
= amp_ctrl_lookup(mgr
, rsp
->id
);
401 assoc
= kmemdup(rsp
->amp_assoc
, assoc_len
, GFP_KERNEL
);
408 ctrl
->assoc_len
= assoc_len
;
409 ctrl
->assoc_rem_len
= assoc_len
;
410 ctrl
->assoc_len_so_far
= 0;
415 /* Create Phys Link */
416 hdev
= hci_dev_get(rsp
->id
);
420 hcon
= phylink_add(hdev
, mgr
, rsp
->id
, true);
424 BT_DBG("Created hcon %p: loc:%d -> rem:%d", hcon
, hdev
->id
, rsp
->id
);
426 mgr
->bredr_chan
->remote_amp_id
= rsp
->id
;
428 amp_create_phylink(hdev
, mgr
, hcon
);
436 static int a2mp_createphyslink_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
437 struct a2mp_cmd
*hdr
)
439 struct a2mp_physlink_req
*req
= (void *) skb
->data
;
441 struct a2mp_physlink_rsp rsp
;
442 struct hci_dev
*hdev
;
443 struct hci_conn
*hcon
;
444 struct amp_ctrl
*ctrl
;
446 if (le16_to_cpu(hdr
->len
) < sizeof(*req
))
449 BT_DBG("local_id %d, remote_id %d", req
->local_id
, req
->remote_id
);
451 rsp
.local_id
= req
->remote_id
;
452 rsp
.remote_id
= req
->local_id
;
454 hdev
= hci_dev_get(req
->remote_id
);
455 if (!hdev
|| hdev
->amp_type
== AMP_TYPE_BREDR
) {
456 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
460 ctrl
= amp_ctrl_lookup(mgr
, rsp
.remote_id
);
462 ctrl
= amp_ctrl_add(mgr
, rsp
.remote_id
);
466 rsp
.status
= A2MP_STATUS_UNABLE_START_LINK_CREATION
;
472 size_t assoc_len
= le16_to_cpu(hdr
->len
) - sizeof(*req
);
475 assoc
= kmemdup(req
->amp_assoc
, assoc_len
, GFP_KERNEL
);
482 ctrl
->assoc_len
= assoc_len
;
483 ctrl
->assoc_rem_len
= assoc_len
;
484 ctrl
->assoc_len_so_far
= 0;
489 hcon
= phylink_add(hdev
, mgr
, req
->local_id
, false);
491 amp_accept_phylink(hdev
, mgr
, hcon
);
492 rsp
.status
= A2MP_STATUS_SUCCESS
;
494 rsp
.status
= A2MP_STATUS_UNABLE_START_LINK_CREATION
;
501 /* Reply error now and success after HCI Write Remote AMP Assoc
502 command complete with success status
504 if (rsp
.status
!= A2MP_STATUS_SUCCESS
) {
505 a2mp_send(mgr
, A2MP_CREATEPHYSLINK_RSP
, hdr
->ident
,
508 set_bit(WRITE_REMOTE_AMP_ASSOC
, &mgr
->state
);
509 mgr
->ident
= hdr
->ident
;
512 skb_pull(skb
, le16_to_cpu(hdr
->len
));
516 static int a2mp_discphyslink_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
517 struct a2mp_cmd
*hdr
)
519 struct a2mp_physlink_req
*req
= (void *) skb
->data
;
520 struct a2mp_physlink_rsp rsp
;
521 struct hci_dev
*hdev
;
522 struct hci_conn
*hcon
;
524 if (le16_to_cpu(hdr
->len
) < sizeof(*req
))
527 BT_DBG("local_id %d remote_id %d", req
->local_id
, req
->remote_id
);
529 rsp
.local_id
= req
->remote_id
;
530 rsp
.remote_id
= req
->local_id
;
531 rsp
.status
= A2MP_STATUS_SUCCESS
;
533 hdev
= hci_dev_get(req
->remote_id
);
535 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
539 hcon
= hci_conn_hash_lookup_ba(hdev
, AMP_LINK
,
540 &mgr
->l2cap_conn
->hcon
->dst
);
542 BT_ERR("No phys link exist");
543 rsp
.status
= A2MP_STATUS_NO_PHYSICAL_LINK_EXISTS
;
547 /* TODO Disconnect Phys Link here */
553 a2mp_send(mgr
, A2MP_DISCONNPHYSLINK_RSP
, hdr
->ident
, sizeof(rsp
), &rsp
);
555 skb_pull(skb
, sizeof(*req
));
559 static inline int a2mp_cmd_rsp(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
560 struct a2mp_cmd
*hdr
)
562 BT_DBG("ident %d code 0x%2.2x", hdr
->ident
, hdr
->code
);
564 skb_pull(skb
, le16_to_cpu(hdr
->len
));
568 /* Handle A2MP signalling */
569 static int a2mp_chan_recv_cb(struct l2cap_chan
*chan
, struct sk_buff
*skb
)
571 struct a2mp_cmd
*hdr
;
572 struct amp_mgr
*mgr
= chan
->data
;
577 while (skb
->len
>= sizeof(*hdr
)) {
580 hdr
= (void *) skb
->data
;
581 len
= le16_to_cpu(hdr
->len
);
583 BT_DBG("code 0x%2.2x id %d len %u", hdr
->code
, hdr
->ident
, len
);
585 skb_pull(skb
, sizeof(*hdr
));
587 if (len
> skb
->len
|| !hdr
->ident
) {
592 mgr
->ident
= hdr
->ident
;
595 case A2MP_COMMAND_REJ
:
596 a2mp_command_rej(mgr
, skb
, hdr
);
599 case A2MP_DISCOVER_REQ
:
600 err
= a2mp_discover_req(mgr
, skb
, hdr
);
603 case A2MP_CHANGE_NOTIFY
:
604 err
= a2mp_change_notify(mgr
, skb
, hdr
);
607 case A2MP_GETINFO_REQ
:
608 err
= a2mp_getinfo_req(mgr
, skb
, hdr
);
611 case A2MP_GETAMPASSOC_REQ
:
612 err
= a2mp_getampassoc_req(mgr
, skb
, hdr
);
615 case A2MP_CREATEPHYSLINK_REQ
:
616 err
= a2mp_createphyslink_req(mgr
, skb
, hdr
);
619 case A2MP_DISCONNPHYSLINK_REQ
:
620 err
= a2mp_discphyslink_req(mgr
, skb
, hdr
);
623 case A2MP_DISCOVER_RSP
:
624 err
= a2mp_discover_rsp(mgr
, skb
, hdr
);
627 case A2MP_GETINFO_RSP
:
628 err
= a2mp_getinfo_rsp(mgr
, skb
, hdr
);
631 case A2MP_GETAMPASSOC_RSP
:
632 err
= a2mp_getampassoc_rsp(mgr
, skb
, hdr
);
635 case A2MP_CHANGE_RSP
:
636 case A2MP_CREATEPHYSLINK_RSP
:
637 case A2MP_DISCONNPHYSLINK_RSP
:
638 err
= a2mp_cmd_rsp(mgr
, skb
, hdr
);
642 BT_ERR("Unknown A2MP sig cmd 0x%2.2x", hdr
->code
);
649 struct a2mp_cmd_rej rej
;
651 rej
.reason
= cpu_to_le16(0);
652 hdr
= (void *) skb
->data
;
654 BT_DBG("Send A2MP Rej: cmd 0x%2.2x err %d", hdr
->code
, err
);
656 a2mp_send(mgr
, A2MP_COMMAND_REJ
, hdr
->ident
, sizeof(rej
),
660 /* Always free skb and return success error code to prevent
661 from sending L2CAP Disconnect over A2MP channel */
669 static void a2mp_chan_close_cb(struct l2cap_chan
*chan
)
671 l2cap_chan_put(chan
);
674 static void a2mp_chan_state_change_cb(struct l2cap_chan
*chan
, int state
,
677 struct amp_mgr
*mgr
= chan
->data
;
682 BT_DBG("chan %p state %s", chan
, state_to_string(state
));
694 static struct sk_buff
*a2mp_chan_alloc_skb_cb(struct l2cap_chan
*chan
,
695 unsigned long hdr_len
,
696 unsigned long len
, int nb
)
700 skb
= bt_skb_alloc(hdr_len
+ len
, GFP_KERNEL
);
702 return ERR_PTR(-ENOMEM
);
707 static const struct l2cap_ops a2mp_chan_ops
= {
708 .name
= "L2CAP A2MP channel",
709 .recv
= a2mp_chan_recv_cb
,
710 .close
= a2mp_chan_close_cb
,
711 .state_change
= a2mp_chan_state_change_cb
,
712 .alloc_skb
= a2mp_chan_alloc_skb_cb
,
714 /* Not implemented for A2MP */
715 .new_connection
= l2cap_chan_no_new_connection
,
716 .teardown
= l2cap_chan_no_teardown
,
717 .ready
= l2cap_chan_no_ready
,
718 .defer
= l2cap_chan_no_defer
,
719 .resume
= l2cap_chan_no_resume
,
720 .set_shutdown
= l2cap_chan_no_set_shutdown
,
721 .get_sndtimeo
= l2cap_chan_no_get_sndtimeo
,
724 static struct l2cap_chan
*a2mp_chan_open(struct l2cap_conn
*conn
, bool locked
)
726 struct l2cap_chan
*chan
;
729 chan
= l2cap_chan_create();
733 BT_DBG("chan %p", chan
);
735 chan
->chan_type
= L2CAP_CHAN_FIXED
;
736 chan
->scid
= L2CAP_CID_A2MP
;
737 chan
->dcid
= L2CAP_CID_A2MP
;
738 chan
->omtu
= L2CAP_A2MP_DEFAULT_MTU
;
739 chan
->imtu
= L2CAP_A2MP_DEFAULT_MTU
;
740 chan
->flush_to
= L2CAP_DEFAULT_FLUSH_TO
;
742 chan
->ops
= &a2mp_chan_ops
;
744 l2cap_chan_set_defaults(chan
);
745 chan
->remote_max_tx
= chan
->max_tx
;
746 chan
->remote_tx_win
= chan
->tx_win
;
748 chan
->retrans_timeout
= L2CAP_DEFAULT_RETRANS_TO
;
749 chan
->monitor_timeout
= L2CAP_DEFAULT_MONITOR_TO
;
751 skb_queue_head_init(&chan
->tx_q
);
753 chan
->mode
= L2CAP_MODE_ERTM
;
755 err
= l2cap_ertm_init(chan
);
757 l2cap_chan_del(chan
, 0);
761 chan
->conf_state
= 0;
764 __l2cap_chan_add(conn
, chan
);
766 l2cap_chan_add(conn
, chan
);
768 chan
->remote_mps
= chan
->omtu
;
769 chan
->mps
= chan
->omtu
;
771 chan
->state
= BT_CONNECTED
;
776 /* AMP Manager functions */
777 struct amp_mgr
*amp_mgr_get(struct amp_mgr
*mgr
)
779 BT_DBG("mgr %p orig refcnt %d", mgr
, atomic_read(&mgr
->kref
.refcount
));
781 kref_get(&mgr
->kref
);
786 static void amp_mgr_destroy(struct kref
*kref
)
788 struct amp_mgr
*mgr
= container_of(kref
, struct amp_mgr
, kref
);
790 BT_DBG("mgr %p", mgr
);
792 mutex_lock(&_mgr_list_lock
);
793 list_del(&mgr
->list
);
794 mutex_unlock(&_mgr_list_lock
);
796 amp_ctrl_list_flush(mgr
);
800 int amp_mgr_put(struct amp_mgr
*mgr
)
802 BT_DBG("mgr %p orig refcnt %d", mgr
, atomic_read(&mgr
->kref
.refcount
));
804 return kref_put(&mgr
->kref
, &_mgr_destroy
);
807 static struct amp_mgr
*amp_mgr_create(struct l2cap_conn
*conn
, bool locked
)
810 struct l2cap_chan
*chan
;
812 mgr
= kzalloc(sizeof(*mgr
), GFP_KERNEL
);
816 BT_DBG("conn %p mgr %p", conn
, mgr
);
818 mgr
->l2cap_conn
= conn
;
820 chan
= a2mp_chan_open(conn
, locked
);
826 mgr
->a2mp_chan
= chan
;
829 conn
->hcon
->amp_mgr
= mgr
;
831 kref_init(&mgr
->kref
);
833 /* Remote AMP ctrl list initialization */
834 INIT_LIST_HEAD(&mgr
->amp_ctrls
);
835 mutex_init(&mgr
->amp_ctrls_lock
);
837 mutex_lock(&_mgr_list_lock
);
838 list_add(&mgr
->list
, &_mgr_list
);
839 mutex_unlock(&_mgr_list_lock
);
844 struct l2cap_chan
*a2mp_channel_create(struct l2cap_conn
*conn
,
849 if (conn
->hcon
->type
!= ACL_LINK
)
852 mgr
= amp_mgr_create(conn
, false);
854 BT_ERR("Could not create AMP manager");
858 BT_DBG("mgr: %p chan %p", mgr
, mgr
->a2mp_chan
);
860 return mgr
->a2mp_chan
;
863 struct amp_mgr
*amp_mgr_lookup_by_state(u8 state
)
867 mutex_lock(&_mgr_list_lock
);
868 list_for_each_entry(mgr
, &_mgr_list
, list
) {
869 if (test_and_clear_bit(state
, &mgr
->state
)) {
871 mutex_unlock(&_mgr_list_lock
);
875 mutex_unlock(&_mgr_list_lock
);
880 void a2mp_send_getinfo_rsp(struct hci_dev
*hdev
)
883 struct a2mp_info_rsp rsp
;
885 mgr
= amp_mgr_lookup_by_state(READ_LOC_AMP_INFO
);
889 BT_DBG("%s mgr %p", hdev
->name
, mgr
);
892 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
894 if (hdev
->amp_type
!= AMP_TYPE_BREDR
) {
896 rsp
.total_bw
= cpu_to_le32(hdev
->amp_total_bw
);
897 rsp
.max_bw
= cpu_to_le32(hdev
->amp_max_bw
);
898 rsp
.min_latency
= cpu_to_le32(hdev
->amp_min_latency
);
899 rsp
.pal_cap
= cpu_to_le16(hdev
->amp_pal_cap
);
900 rsp
.assoc_size
= cpu_to_le16(hdev
->amp_assoc_size
);
903 a2mp_send(mgr
, A2MP_GETINFO_RSP
, mgr
->ident
, sizeof(rsp
), &rsp
);
907 void a2mp_send_getampassoc_rsp(struct hci_dev
*hdev
, u8 status
)
910 struct amp_assoc
*loc_assoc
= &hdev
->loc_assoc
;
911 struct a2mp_amp_assoc_rsp
*rsp
;
914 mgr
= amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC
);
918 BT_DBG("%s mgr %p", hdev
->name
, mgr
);
920 len
= sizeof(struct a2mp_amp_assoc_rsp
) + loc_assoc
->len
;
921 rsp
= kzalloc(len
, GFP_KERNEL
);
930 rsp
->status
= A2MP_STATUS_INVALID_CTRL_ID
;
932 rsp
->status
= A2MP_STATUS_SUCCESS
;
933 memcpy(rsp
->amp_assoc
, loc_assoc
->data
, loc_assoc
->len
);
936 a2mp_send(mgr
, A2MP_GETAMPASSOC_RSP
, mgr
->ident
, len
, rsp
);
941 void a2mp_send_create_phy_link_req(struct hci_dev
*hdev
, u8 status
)
944 struct amp_assoc
*loc_assoc
= &hdev
->loc_assoc
;
945 struct a2mp_physlink_req
*req
;
946 struct l2cap_chan
*bredr_chan
;
949 mgr
= amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC_FINAL
);
953 len
= sizeof(*req
) + loc_assoc
->len
;
955 BT_DBG("%s mgr %p assoc_len %zu", hdev
->name
, mgr
, len
);
957 req
= kzalloc(len
, GFP_KERNEL
);
963 bredr_chan
= mgr
->bredr_chan
;
967 req
->local_id
= hdev
->id
;
968 req
->remote_id
= bredr_chan
->remote_amp_id
;
969 memcpy(req
->amp_assoc
, loc_assoc
->data
, loc_assoc
->len
);
971 a2mp_send(mgr
, A2MP_CREATEPHYSLINK_REQ
, __next_ident(mgr
), len
, req
);
978 void a2mp_send_create_phy_link_rsp(struct hci_dev
*hdev
, u8 status
)
981 struct a2mp_physlink_rsp rsp
;
982 struct hci_conn
*hs_hcon
;
984 mgr
= amp_mgr_lookup_by_state(WRITE_REMOTE_AMP_ASSOC
);
988 hs_hcon
= hci_conn_hash_lookup_state(hdev
, AMP_LINK
, BT_CONNECT
);
990 rsp
.status
= A2MP_STATUS_UNABLE_START_LINK_CREATION
;
992 rsp
.remote_id
= hs_hcon
->remote_id
;
993 rsp
.status
= A2MP_STATUS_SUCCESS
;
996 BT_DBG("%s mgr %p hs_hcon %p status %u", hdev
->name
, mgr
, hs_hcon
,
999 rsp
.local_id
= hdev
->id
;
1000 a2mp_send(mgr
, A2MP_CREATEPHYSLINK_RSP
, mgr
->ident
, sizeof(rsp
), &rsp
);
1004 void a2mp_discover_amp(struct l2cap_chan
*chan
)
1006 struct l2cap_conn
*conn
= chan
->conn
;
1007 struct amp_mgr
*mgr
= conn
->hcon
->amp_mgr
;
1008 struct a2mp_discov_req req
;
1010 BT_DBG("chan %p conn %p mgr %p", chan
, conn
, mgr
);
1013 mgr
= amp_mgr_create(conn
, true);
1018 mgr
->bredr_chan
= chan
;
1020 req
.mtu
= cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU
);
1022 a2mp_send(mgr
, A2MP_DISCOVER_REQ
, 1, sizeof(req
), &req
);