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>
19 #include "hci_request.h"
23 #define A2MP_FEAT_EXT 0x8000
25 /* Global AMP Manager list */
26 static LIST_HEAD(amp_mgr_list
);
27 static DEFINE_MUTEX(amp_mgr_list_lock
);
29 /* A2MP build & send command helper functions */
30 static struct a2mp_cmd
*__a2mp_build(u8 code
, u8 ident
, u16 len
, void *data
)
35 plen
= sizeof(*cmd
) + len
;
36 cmd
= kzalloc(plen
, GFP_KERNEL
);
42 cmd
->len
= cpu_to_le16(len
);
44 memcpy(cmd
->data
, data
, len
);
49 static void a2mp_send(struct amp_mgr
*mgr
, u8 code
, u8 ident
, u16 len
, void *data
)
51 struct l2cap_chan
*chan
= mgr
->a2mp_chan
;
53 u16 total_len
= len
+ sizeof(*cmd
);
57 cmd
= __a2mp_build(code
, ident
, len
, data
);
62 iv
.iov_len
= total_len
;
64 memset(&msg
, 0, sizeof(msg
));
66 iov_iter_kvec(&msg
.msg_iter
, WRITE
| ITER_KVEC
, &iv
, 1, total_len
);
68 l2cap_chan_send(chan
, &msg
, total_len
);
73 static u8
__next_ident(struct amp_mgr
*mgr
)
75 if (++mgr
->ident
== 0)
81 static struct amp_mgr
*amp_mgr_lookup_by_state(u8 state
)
85 mutex_lock(&_mgr_list_lock
);
86 list_for_each_entry(mgr
, &_mgr_list
, list
) {
87 if (test_and_clear_bit(state
, &mgr
->state
)) {
89 mutex_unlock(&_mgr_list_lock
);
93 mutex_unlock(&_mgr_list_lock
);
98 /* hci_dev_list shall be locked */
99 static void __a2mp_add_cl(struct amp_mgr
*mgr
, struct a2mp_cl
*cl
)
101 struct hci_dev
*hdev
;
104 cl
[0].id
= AMP_ID_BREDR
;
105 cl
[0].type
= AMP_TYPE_BREDR
;
106 cl
[0].status
= AMP_STATUS_BLUETOOTH_ONLY
;
108 list_for_each_entry(hdev
, &hci_dev_list
, list
) {
109 if (hdev
->dev_type
== HCI_AMP
) {
111 cl
[i
].type
= hdev
->amp_type
;
112 if (test_bit(HCI_UP
, &hdev
->flags
))
113 cl
[i
].status
= hdev
->amp_status
;
115 cl
[i
].status
= AMP_STATUS_POWERED_DOWN
;
121 /* Processing A2MP messages */
122 static int a2mp_command_rej(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
123 struct a2mp_cmd
*hdr
)
125 struct a2mp_cmd_rej
*rej
= (void *) skb
->data
;
127 if (le16_to_cpu(hdr
->len
) < sizeof(*rej
))
130 BT_DBG("ident %d reason %d", hdr
->ident
, le16_to_cpu(rej
->reason
));
132 skb_pull(skb
, sizeof(*rej
));
137 static int a2mp_discover_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
138 struct a2mp_cmd
*hdr
)
140 struct a2mp_discov_req
*req
= (void *) skb
->data
;
141 u16 len
= le16_to_cpu(hdr
->len
);
142 struct a2mp_discov_rsp
*rsp
;
145 struct hci_dev
*hdev
;
147 if (len
< sizeof(*req
))
150 skb_pull(skb
, sizeof(*req
));
152 ext_feat
= le16_to_cpu(req
->ext_feat
);
154 BT_DBG("mtu %d efm 0x%4.4x", le16_to_cpu(req
->mtu
), ext_feat
);
156 /* check that packet is not broken for now */
157 while (ext_feat
& A2MP_FEAT_EXT
) {
158 if (len
< sizeof(ext_feat
))
161 ext_feat
= get_unaligned_le16(skb
->data
);
162 BT_DBG("efm 0x%4.4x", ext_feat
);
163 len
-= sizeof(ext_feat
);
164 skb_pull(skb
, sizeof(ext_feat
));
167 read_lock(&hci_dev_list_lock
);
169 /* at minimum the BR/EDR needs to be listed */
172 list_for_each_entry(hdev
, &hci_dev_list
, list
) {
173 if (hdev
->dev_type
== HCI_AMP
)
177 len
= num_ctrl
* sizeof(struct a2mp_cl
) + sizeof(*rsp
);
178 rsp
= kmalloc(len
, GFP_ATOMIC
);
180 read_unlock(&hci_dev_list_lock
);
184 rsp
->mtu
= cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU
);
187 __a2mp_add_cl(mgr
, rsp
->cl
);
189 read_unlock(&hci_dev_list_lock
);
191 a2mp_send(mgr
, A2MP_DISCOVER_RSP
, hdr
->ident
, len
, rsp
);
197 static int a2mp_discover_rsp(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
198 struct a2mp_cmd
*hdr
)
200 struct a2mp_discov_rsp
*rsp
= (void *) skb
->data
;
201 u16 len
= le16_to_cpu(hdr
->len
);
206 if (len
< sizeof(*rsp
))
210 skb_pull(skb
, sizeof(*rsp
));
212 ext_feat
= le16_to_cpu(rsp
->ext_feat
);
214 BT_DBG("mtu %d efm 0x%4.4x", le16_to_cpu(rsp
->mtu
), ext_feat
);
216 /* check that packet is not broken for now */
217 while (ext_feat
& A2MP_FEAT_EXT
) {
218 if (len
< sizeof(ext_feat
))
221 ext_feat
= get_unaligned_le16(skb
->data
);
222 BT_DBG("efm 0x%4.4x", ext_feat
);
223 len
-= sizeof(ext_feat
);
224 skb_pull(skb
, sizeof(ext_feat
));
227 cl
= (void *) skb
->data
;
228 while (len
>= sizeof(*cl
)) {
229 BT_DBG("Remote AMP id %d type %d status %d", cl
->id
, cl
->type
,
232 if (cl
->id
!= AMP_ID_BREDR
&& cl
->type
!= AMP_TYPE_BREDR
) {
233 struct a2mp_info_req req
;
237 memset(&req
, 0, sizeof(req
));
240 a2mp_send(mgr
, A2MP_GETINFO_REQ
, __next_ident(mgr
),
245 cl
= skb_pull(skb
, sizeof(*cl
));
248 /* Fall back to L2CAP init sequence */
250 struct l2cap_conn
*conn
= mgr
->l2cap_conn
;
251 struct l2cap_chan
*chan
;
253 mutex_lock(&conn
->chan_lock
);
255 list_for_each_entry(chan
, &conn
->chan_l
, list
) {
257 BT_DBG("chan %p state %s", chan
,
258 state_to_string(chan
->state
));
260 if (chan
->scid
== L2CAP_CID_A2MP
)
263 l2cap_chan_lock(chan
);
265 if (chan
->state
== BT_CONNECT
)
266 l2cap_send_conn_req(chan
);
268 l2cap_chan_unlock(chan
);
271 mutex_unlock(&conn
->chan_lock
);
277 static int a2mp_change_notify(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
278 struct a2mp_cmd
*hdr
)
280 struct a2mp_cl
*cl
= (void *) skb
->data
;
282 while (skb
->len
>= sizeof(*cl
)) {
283 BT_DBG("Controller id %d type %d status %d", cl
->id
, cl
->type
,
285 cl
= skb_pull(skb
, sizeof(*cl
));
288 /* TODO send A2MP_CHANGE_RSP */
293 static void read_local_amp_info_complete(struct hci_dev
*hdev
, u8 status
,
296 BT_DBG("%s status 0x%2.2x", hdev
->name
, status
);
298 a2mp_send_getinfo_rsp(hdev
);
301 static int a2mp_getinfo_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
302 struct a2mp_cmd
*hdr
)
304 struct a2mp_info_req
*req
= (void *) skb
->data
;
305 struct hci_dev
*hdev
;
306 struct hci_request hreq
;
309 if (le16_to_cpu(hdr
->len
) < sizeof(*req
))
312 BT_DBG("id %d", req
->id
);
314 hdev
= hci_dev_get(req
->id
);
315 if (!hdev
|| hdev
->dev_type
!= HCI_AMP
) {
316 struct a2mp_info_rsp rsp
;
318 memset(&rsp
, 0, sizeof(rsp
));
321 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
323 a2mp_send(mgr
, A2MP_GETINFO_RSP
, hdr
->ident
, sizeof(rsp
),
329 set_bit(READ_LOC_AMP_INFO
, &mgr
->state
);
330 hci_req_init(&hreq
, hdev
);
331 hci_req_add(&hreq
, HCI_OP_READ_LOCAL_AMP_INFO
, 0, NULL
);
332 err
= hci_req_run(&hreq
, read_local_amp_info_complete
);
334 a2mp_send_getinfo_rsp(hdev
);
340 skb_pull(skb
, sizeof(*req
));
344 static int a2mp_getinfo_rsp(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
345 struct a2mp_cmd
*hdr
)
347 struct a2mp_info_rsp
*rsp
= (struct a2mp_info_rsp
*) skb
->data
;
348 struct a2mp_amp_assoc_req req
;
349 struct amp_ctrl
*ctrl
;
351 if (le16_to_cpu(hdr
->len
) < sizeof(*rsp
))
354 BT_DBG("id %d status 0x%2.2x", rsp
->id
, rsp
->status
);
359 ctrl
= amp_ctrl_add(mgr
, rsp
->id
);
363 memset(&req
, 0, sizeof(req
));
366 a2mp_send(mgr
, A2MP_GETAMPASSOC_REQ
, __next_ident(mgr
), sizeof(req
),
369 skb_pull(skb
, sizeof(*rsp
));
373 static int a2mp_getampassoc_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
374 struct a2mp_cmd
*hdr
)
376 struct a2mp_amp_assoc_req
*req
= (void *) skb
->data
;
377 struct hci_dev
*hdev
;
380 if (le16_to_cpu(hdr
->len
) < sizeof(*req
))
383 BT_DBG("id %d", req
->id
);
385 /* Make sure that other request is not processed */
386 tmp
= amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC
);
388 hdev
= hci_dev_get(req
->id
);
389 if (!hdev
|| hdev
->amp_type
== AMP_TYPE_BREDR
|| tmp
) {
390 struct a2mp_amp_assoc_rsp rsp
;
393 memset(&rsp
, 0, sizeof(rsp
));
396 rsp
.status
= A2MP_STATUS_COLLISION_OCCURED
;
399 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
402 a2mp_send(mgr
, A2MP_GETAMPASSOC_RSP
, hdr
->ident
, sizeof(rsp
),
408 amp_read_loc_assoc(hdev
, mgr
);
414 skb_pull(skb
, sizeof(*req
));
418 static int a2mp_getampassoc_rsp(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
419 struct a2mp_cmd
*hdr
)
421 struct a2mp_amp_assoc_rsp
*rsp
= (void *) skb
->data
;
422 u16 len
= le16_to_cpu(hdr
->len
);
423 struct hci_dev
*hdev
;
424 struct amp_ctrl
*ctrl
;
425 struct hci_conn
*hcon
;
428 if (len
< sizeof(*rsp
))
431 assoc_len
= len
- sizeof(*rsp
);
433 BT_DBG("id %d status 0x%2.2x assoc len %zu", rsp
->id
, rsp
->status
,
439 /* Save remote ASSOC data */
440 ctrl
= amp_ctrl_lookup(mgr
, rsp
->id
);
444 assoc
= kmemdup(rsp
->amp_assoc
, assoc_len
, GFP_KERNEL
);
451 ctrl
->assoc_len
= assoc_len
;
452 ctrl
->assoc_rem_len
= assoc_len
;
453 ctrl
->assoc_len_so_far
= 0;
458 /* Create Phys Link */
459 hdev
= hci_dev_get(rsp
->id
);
463 hcon
= phylink_add(hdev
, mgr
, rsp
->id
, true);
467 BT_DBG("Created hcon %p: loc:%d -> rem:%d", hcon
, hdev
->id
, rsp
->id
);
469 mgr
->bredr_chan
->remote_amp_id
= rsp
->id
;
471 amp_create_phylink(hdev
, mgr
, hcon
);
479 static int a2mp_createphyslink_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
480 struct a2mp_cmd
*hdr
)
482 struct a2mp_physlink_req
*req
= (void *) skb
->data
;
483 struct a2mp_physlink_rsp rsp
;
484 struct hci_dev
*hdev
;
485 struct hci_conn
*hcon
;
486 struct amp_ctrl
*ctrl
;
488 if (le16_to_cpu(hdr
->len
) < sizeof(*req
))
491 BT_DBG("local_id %d, remote_id %d", req
->local_id
, req
->remote_id
);
493 memset(&rsp
, 0, sizeof(rsp
));
495 rsp
.local_id
= req
->remote_id
;
496 rsp
.remote_id
= req
->local_id
;
498 hdev
= hci_dev_get(req
->remote_id
);
499 if (!hdev
|| hdev
->amp_type
== AMP_TYPE_BREDR
) {
500 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
504 ctrl
= amp_ctrl_lookup(mgr
, rsp
.remote_id
);
506 ctrl
= amp_ctrl_add(mgr
, rsp
.remote_id
);
510 rsp
.status
= A2MP_STATUS_UNABLE_START_LINK_CREATION
;
516 size_t assoc_len
= le16_to_cpu(hdr
->len
) - sizeof(*req
);
519 assoc
= kmemdup(req
->amp_assoc
, assoc_len
, GFP_KERNEL
);
526 ctrl
->assoc_len
= assoc_len
;
527 ctrl
->assoc_rem_len
= assoc_len
;
528 ctrl
->assoc_len_so_far
= 0;
533 hcon
= phylink_add(hdev
, mgr
, req
->local_id
, false);
535 amp_accept_phylink(hdev
, mgr
, hcon
);
536 rsp
.status
= A2MP_STATUS_SUCCESS
;
538 rsp
.status
= A2MP_STATUS_UNABLE_START_LINK_CREATION
;
545 /* Reply error now and success after HCI Write Remote AMP Assoc
546 command complete with success status
548 if (rsp
.status
!= A2MP_STATUS_SUCCESS
) {
549 a2mp_send(mgr
, A2MP_CREATEPHYSLINK_RSP
, hdr
->ident
,
552 set_bit(WRITE_REMOTE_AMP_ASSOC
, &mgr
->state
);
553 mgr
->ident
= hdr
->ident
;
556 skb_pull(skb
, le16_to_cpu(hdr
->len
));
560 static int a2mp_discphyslink_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
561 struct a2mp_cmd
*hdr
)
563 struct a2mp_physlink_req
*req
= (void *) skb
->data
;
564 struct a2mp_physlink_rsp rsp
;
565 struct hci_dev
*hdev
;
566 struct hci_conn
*hcon
;
568 if (le16_to_cpu(hdr
->len
) < sizeof(*req
))
571 BT_DBG("local_id %d remote_id %d", req
->local_id
, req
->remote_id
);
573 memset(&rsp
, 0, sizeof(rsp
));
575 rsp
.local_id
= req
->remote_id
;
576 rsp
.remote_id
= req
->local_id
;
577 rsp
.status
= A2MP_STATUS_SUCCESS
;
579 hdev
= hci_dev_get(req
->remote_id
);
581 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
585 hcon
= hci_conn_hash_lookup_ba(hdev
, AMP_LINK
,
586 &mgr
->l2cap_conn
->hcon
->dst
);
588 bt_dev_err(hdev
, "no phys link exist");
589 rsp
.status
= A2MP_STATUS_NO_PHYSICAL_LINK_EXISTS
;
593 /* TODO Disconnect Phys Link here */
599 a2mp_send(mgr
, A2MP_DISCONNPHYSLINK_RSP
, hdr
->ident
, sizeof(rsp
), &rsp
);
601 skb_pull(skb
, sizeof(*req
));
605 static inline int a2mp_cmd_rsp(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
606 struct a2mp_cmd
*hdr
)
608 BT_DBG("ident %d code 0x%2.2x", hdr
->ident
, hdr
->code
);
610 skb_pull(skb
, le16_to_cpu(hdr
->len
));
614 /* Handle A2MP signalling */
615 static int a2mp_chan_recv_cb(struct l2cap_chan
*chan
, struct sk_buff
*skb
)
617 struct a2mp_cmd
*hdr
;
618 struct amp_mgr
*mgr
= chan
->data
;
623 while (skb
->len
>= sizeof(*hdr
)) {
626 hdr
= (void *) skb
->data
;
627 len
= le16_to_cpu(hdr
->len
);
629 BT_DBG("code 0x%2.2x id %d len %u", hdr
->code
, hdr
->ident
, len
);
631 skb_pull(skb
, sizeof(*hdr
));
633 if (len
> skb
->len
|| !hdr
->ident
) {
638 mgr
->ident
= hdr
->ident
;
641 case A2MP_COMMAND_REJ
:
642 a2mp_command_rej(mgr
, skb
, hdr
);
645 case A2MP_DISCOVER_REQ
:
646 err
= a2mp_discover_req(mgr
, skb
, hdr
);
649 case A2MP_CHANGE_NOTIFY
:
650 err
= a2mp_change_notify(mgr
, skb
, hdr
);
653 case A2MP_GETINFO_REQ
:
654 err
= a2mp_getinfo_req(mgr
, skb
, hdr
);
657 case A2MP_GETAMPASSOC_REQ
:
658 err
= a2mp_getampassoc_req(mgr
, skb
, hdr
);
661 case A2MP_CREATEPHYSLINK_REQ
:
662 err
= a2mp_createphyslink_req(mgr
, skb
, hdr
);
665 case A2MP_DISCONNPHYSLINK_REQ
:
666 err
= a2mp_discphyslink_req(mgr
, skb
, hdr
);
669 case A2MP_DISCOVER_RSP
:
670 err
= a2mp_discover_rsp(mgr
, skb
, hdr
);
673 case A2MP_GETINFO_RSP
:
674 err
= a2mp_getinfo_rsp(mgr
, skb
, hdr
);
677 case A2MP_GETAMPASSOC_RSP
:
678 err
= a2mp_getampassoc_rsp(mgr
, skb
, hdr
);
681 case A2MP_CHANGE_RSP
:
682 case A2MP_CREATEPHYSLINK_RSP
:
683 case A2MP_DISCONNPHYSLINK_RSP
:
684 err
= a2mp_cmd_rsp(mgr
, skb
, hdr
);
688 BT_ERR("Unknown A2MP sig cmd 0x%2.2x", hdr
->code
);
695 struct a2mp_cmd_rej rej
;
697 memset(&rej
, 0, sizeof(rej
));
699 rej
.reason
= cpu_to_le16(0);
700 hdr
= (void *) skb
->data
;
702 BT_DBG("Send A2MP Rej: cmd 0x%2.2x err %d", hdr
->code
, err
);
704 a2mp_send(mgr
, A2MP_COMMAND_REJ
, hdr
->ident
, sizeof(rej
),
708 /* Always free skb and return success error code to prevent
709 from sending L2CAP Disconnect over A2MP channel */
717 static void a2mp_chan_close_cb(struct l2cap_chan
*chan
)
719 l2cap_chan_put(chan
);
722 static void a2mp_chan_state_change_cb(struct l2cap_chan
*chan
, int state
,
725 struct amp_mgr
*mgr
= chan
->data
;
730 BT_DBG("chan %p state %s", chan
, state_to_string(state
));
742 static struct sk_buff
*a2mp_chan_alloc_skb_cb(struct l2cap_chan
*chan
,
743 unsigned long hdr_len
,
744 unsigned long len
, int nb
)
748 skb
= bt_skb_alloc(hdr_len
+ len
, GFP_KERNEL
);
750 return ERR_PTR(-ENOMEM
);
755 static const struct l2cap_ops a2mp_chan_ops
= {
756 .name
= "L2CAP A2MP channel",
757 .recv
= a2mp_chan_recv_cb
,
758 .close
= a2mp_chan_close_cb
,
759 .state_change
= a2mp_chan_state_change_cb
,
760 .alloc_skb
= a2mp_chan_alloc_skb_cb
,
762 /* Not implemented for A2MP */
763 .new_connection
= l2cap_chan_no_new_connection
,
764 .teardown
= l2cap_chan_no_teardown
,
765 .ready
= l2cap_chan_no_ready
,
766 .defer
= l2cap_chan_no_defer
,
767 .resume
= l2cap_chan_no_resume
,
768 .set_shutdown
= l2cap_chan_no_set_shutdown
,
769 .get_sndtimeo
= l2cap_chan_no_get_sndtimeo
,
772 static struct l2cap_chan
*a2mp_chan_open(struct l2cap_conn
*conn
, bool locked
)
774 struct l2cap_chan
*chan
;
777 chan
= l2cap_chan_create();
781 BT_DBG("chan %p", chan
);
783 chan
->chan_type
= L2CAP_CHAN_FIXED
;
784 chan
->scid
= L2CAP_CID_A2MP
;
785 chan
->dcid
= L2CAP_CID_A2MP
;
786 chan
->omtu
= L2CAP_A2MP_DEFAULT_MTU
;
787 chan
->imtu
= L2CAP_A2MP_DEFAULT_MTU
;
788 chan
->flush_to
= L2CAP_DEFAULT_FLUSH_TO
;
790 chan
->ops
= &a2mp_chan_ops
;
792 l2cap_chan_set_defaults(chan
);
793 chan
->remote_max_tx
= chan
->max_tx
;
794 chan
->remote_tx_win
= chan
->tx_win
;
796 chan
->retrans_timeout
= L2CAP_DEFAULT_RETRANS_TO
;
797 chan
->monitor_timeout
= L2CAP_DEFAULT_MONITOR_TO
;
799 skb_queue_head_init(&chan
->tx_q
);
801 chan
->mode
= L2CAP_MODE_ERTM
;
803 err
= l2cap_ertm_init(chan
);
805 l2cap_chan_del(chan
, 0);
809 chan
->conf_state
= 0;
812 __l2cap_chan_add(conn
, chan
);
814 l2cap_chan_add(conn
, chan
);
816 chan
->remote_mps
= chan
->omtu
;
817 chan
->mps
= chan
->omtu
;
819 chan
->state
= BT_CONNECTED
;
824 /* AMP Manager functions */
825 struct amp_mgr
*amp_mgr_get(struct amp_mgr
*mgr
)
827 BT_DBG("mgr %p orig refcnt %d", mgr
, kref_read(&mgr
->kref
));
829 kref_get(&mgr
->kref
);
834 static void amp_mgr_destroy(struct kref
*kref
)
836 struct amp_mgr
*mgr
= container_of(kref
, struct amp_mgr
, kref
);
838 BT_DBG("mgr %p", mgr
);
840 mutex_lock(&_mgr_list_lock
);
841 list_del(&mgr
->list
);
842 mutex_unlock(&_mgr_list_lock
);
844 amp_ctrl_list_flush(mgr
);
848 int amp_mgr_put(struct amp_mgr
*mgr
)
850 BT_DBG("mgr %p orig refcnt %d", mgr
, kref_read(&mgr
->kref
));
852 return kref_put(&mgr
->kref
, &_mgr_destroy
);
855 static struct amp_mgr
*amp_mgr_create(struct l2cap_conn
*conn
, bool locked
)
858 struct l2cap_chan
*chan
;
860 mgr
= kzalloc(sizeof(*mgr
), GFP_KERNEL
);
864 BT_DBG("conn %p mgr %p", conn
, mgr
);
866 mgr
->l2cap_conn
= conn
;
868 chan
= a2mp_chan_open(conn
, locked
);
874 mgr
->a2mp_chan
= chan
;
877 conn
->hcon
->amp_mgr
= mgr
;
879 kref_init(&mgr
->kref
);
881 /* Remote AMP ctrl list initialization */
882 INIT_LIST_HEAD(&mgr
->amp_ctrls
);
883 mutex_init(&mgr
->amp_ctrls_lock
);
885 mutex_lock(&_mgr_list_lock
);
886 list_add(&mgr
->list
, &_mgr_list
);
887 mutex_unlock(&_mgr_list_lock
);
892 struct l2cap_chan
*a2mp_channel_create(struct l2cap_conn
*conn
,
897 if (conn
->hcon
->type
!= ACL_LINK
)
900 mgr
= amp_mgr_create(conn
, false);
902 BT_ERR("Could not create AMP manager");
906 BT_DBG("mgr: %p chan %p", mgr
, mgr
->a2mp_chan
);
908 return mgr
->a2mp_chan
;
911 void a2mp_send_getinfo_rsp(struct hci_dev
*hdev
)
914 struct a2mp_info_rsp rsp
;
916 mgr
= amp_mgr_lookup_by_state(READ_LOC_AMP_INFO
);
920 BT_DBG("%s mgr %p", hdev
->name
, mgr
);
922 memset(&rsp
, 0, sizeof(rsp
));
925 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
927 if (hdev
->amp_type
!= AMP_TYPE_BREDR
) {
929 rsp
.total_bw
= cpu_to_le32(hdev
->amp_total_bw
);
930 rsp
.max_bw
= cpu_to_le32(hdev
->amp_max_bw
);
931 rsp
.min_latency
= cpu_to_le32(hdev
->amp_min_latency
);
932 rsp
.pal_cap
= cpu_to_le16(hdev
->amp_pal_cap
);
933 rsp
.assoc_size
= cpu_to_le16(hdev
->amp_assoc_size
);
936 a2mp_send(mgr
, A2MP_GETINFO_RSP
, mgr
->ident
, sizeof(rsp
), &rsp
);
940 void a2mp_send_getampassoc_rsp(struct hci_dev
*hdev
, u8 status
)
943 struct amp_assoc
*loc_assoc
= &hdev
->loc_assoc
;
944 struct a2mp_amp_assoc_rsp
*rsp
;
947 mgr
= amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC
);
951 BT_DBG("%s mgr %p", hdev
->name
, mgr
);
953 len
= sizeof(struct a2mp_amp_assoc_rsp
) + loc_assoc
->len
;
954 rsp
= kzalloc(len
, GFP_KERNEL
);
963 rsp
->status
= A2MP_STATUS_INVALID_CTRL_ID
;
965 rsp
->status
= A2MP_STATUS_SUCCESS
;
966 memcpy(rsp
->amp_assoc
, loc_assoc
->data
, loc_assoc
->len
);
969 a2mp_send(mgr
, A2MP_GETAMPASSOC_RSP
, mgr
->ident
, len
, rsp
);
974 void a2mp_send_create_phy_link_req(struct hci_dev
*hdev
, u8 status
)
977 struct amp_assoc
*loc_assoc
= &hdev
->loc_assoc
;
978 struct a2mp_physlink_req
*req
;
979 struct l2cap_chan
*bredr_chan
;
982 mgr
= amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC_FINAL
);
986 len
= sizeof(*req
) + loc_assoc
->len
;
988 BT_DBG("%s mgr %p assoc_len %zu", hdev
->name
, mgr
, len
);
990 req
= kzalloc(len
, GFP_KERNEL
);
996 bredr_chan
= mgr
->bredr_chan
;
1000 req
->local_id
= hdev
->id
;
1001 req
->remote_id
= bredr_chan
->remote_amp_id
;
1002 memcpy(req
->amp_assoc
, loc_assoc
->data
, loc_assoc
->len
);
1004 a2mp_send(mgr
, A2MP_CREATEPHYSLINK_REQ
, __next_ident(mgr
), len
, req
);
1011 void a2mp_send_create_phy_link_rsp(struct hci_dev
*hdev
, u8 status
)
1013 struct amp_mgr
*mgr
;
1014 struct a2mp_physlink_rsp rsp
;
1015 struct hci_conn
*hs_hcon
;
1017 mgr
= amp_mgr_lookup_by_state(WRITE_REMOTE_AMP_ASSOC
);
1021 memset(&rsp
, 0, sizeof(rsp
));
1023 hs_hcon
= hci_conn_hash_lookup_state(hdev
, AMP_LINK
, BT_CONNECT
);
1025 rsp
.status
= A2MP_STATUS_UNABLE_START_LINK_CREATION
;
1027 rsp
.remote_id
= hs_hcon
->remote_id
;
1028 rsp
.status
= A2MP_STATUS_SUCCESS
;
1031 BT_DBG("%s mgr %p hs_hcon %p status %u", hdev
->name
, mgr
, hs_hcon
,
1034 rsp
.local_id
= hdev
->id
;
1035 a2mp_send(mgr
, A2MP_CREATEPHYSLINK_RSP
, mgr
->ident
, sizeof(rsp
), &rsp
);
1039 void a2mp_discover_amp(struct l2cap_chan
*chan
)
1041 struct l2cap_conn
*conn
= chan
->conn
;
1042 struct amp_mgr
*mgr
= conn
->hcon
->amp_mgr
;
1043 struct a2mp_discov_req req
;
1045 BT_DBG("chan %p conn %p mgr %p", chan
, conn
, mgr
);
1048 mgr
= amp_mgr_create(conn
, true);
1053 mgr
->bredr_chan
= chan
;
1055 memset(&req
, 0, sizeof(req
));
1057 req
.mtu
= cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU
);
1059 a2mp_send(mgr
, A2MP_DISCOVER_REQ
, 1, sizeof(req
), &req
);