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 #define A2MP_FEAT_EXT 0x8000
24 /* Global AMP Manager list */
25 static LIST_HEAD(amp_mgr_list
);
26 static DEFINE_MUTEX(amp_mgr_list_lock
);
28 /* A2MP build & send command helper functions */
29 static struct a2mp_cmd
*__a2mp_build(u8 code
, u8 ident
, u16 len
, void *data
)
34 plen
= sizeof(*cmd
) + len
;
35 cmd
= kzalloc(plen
, GFP_KERNEL
);
41 cmd
->len
= cpu_to_le16(len
);
43 memcpy(cmd
->data
, data
, len
);
48 static void a2mp_send(struct amp_mgr
*mgr
, u8 code
, u8 ident
, u16 len
, void *data
)
50 struct l2cap_chan
*chan
= mgr
->a2mp_chan
;
52 u16 total_len
= len
+ sizeof(*cmd
);
56 cmd
= __a2mp_build(code
, ident
, len
, data
);
61 iv
.iov_len
= total_len
;
63 memset(&msg
, 0, sizeof(msg
));
65 iov_iter_kvec(&msg
.msg_iter
, WRITE
| ITER_KVEC
, &iv
, 1, total_len
);
67 l2cap_chan_send(chan
, &msg
, total_len
);
72 static u8
__next_ident(struct amp_mgr
*mgr
)
74 if (++mgr
->ident
== 0)
80 static struct amp_mgr
*amp_mgr_lookup_by_state(u8 state
)
84 mutex_lock(&_mgr_list_lock
);
85 list_for_each_entry(mgr
, &_mgr_list
, list
) {
86 if (test_and_clear_bit(state
, &mgr
->state
)) {
88 mutex_unlock(&_mgr_list_lock
);
92 mutex_unlock(&_mgr_list_lock
);
97 /* hci_dev_list shall be locked */
98 static void __a2mp_add_cl(struct amp_mgr
*mgr
, struct a2mp_cl
*cl
)
100 struct hci_dev
*hdev
;
103 cl
[0].id
= AMP_ID_BREDR
;
104 cl
[0].type
= AMP_TYPE_BREDR
;
105 cl
[0].status
= AMP_STATUS_BLUETOOTH_ONLY
;
107 list_for_each_entry(hdev
, &hci_dev_list
, list
) {
108 if (hdev
->dev_type
== HCI_AMP
) {
110 cl
[i
].type
= hdev
->amp_type
;
111 if (test_bit(HCI_UP
, &hdev
->flags
))
112 cl
[i
].status
= hdev
->amp_status
;
114 cl
[i
].status
= AMP_STATUS_POWERED_DOWN
;
120 /* Processing A2MP messages */
121 static int a2mp_command_rej(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
122 struct a2mp_cmd
*hdr
)
124 struct a2mp_cmd_rej
*rej
= (void *) skb
->data
;
126 if (le16_to_cpu(hdr
->len
) < sizeof(*rej
))
129 BT_DBG("ident %d reason %d", hdr
->ident
, le16_to_cpu(rej
->reason
));
131 skb_pull(skb
, sizeof(*rej
));
136 static int a2mp_discover_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
137 struct a2mp_cmd
*hdr
)
139 struct a2mp_discov_req
*req
= (void *) skb
->data
;
140 u16 len
= le16_to_cpu(hdr
->len
);
141 struct a2mp_discov_rsp
*rsp
;
144 struct hci_dev
*hdev
;
146 if (len
< sizeof(*req
))
149 skb_pull(skb
, sizeof(*req
));
151 ext_feat
= le16_to_cpu(req
->ext_feat
);
153 BT_DBG("mtu %d efm 0x%4.4x", le16_to_cpu(req
->mtu
), ext_feat
);
155 /* check that packet is not broken for now */
156 while (ext_feat
& A2MP_FEAT_EXT
) {
157 if (len
< sizeof(ext_feat
))
160 ext_feat
= get_unaligned_le16(skb
->data
);
161 BT_DBG("efm 0x%4.4x", ext_feat
);
162 len
-= sizeof(ext_feat
);
163 skb_pull(skb
, sizeof(ext_feat
));
166 read_lock(&hci_dev_list_lock
);
168 /* at minimum the BR/EDR needs to be listed */
171 list_for_each_entry(hdev
, &hci_dev_list
, list
) {
172 if (hdev
->dev_type
== HCI_AMP
)
176 len
= num_ctrl
* sizeof(struct a2mp_cl
) + sizeof(*rsp
);
177 rsp
= kmalloc(len
, GFP_ATOMIC
);
179 read_unlock(&hci_dev_list_lock
);
183 rsp
->mtu
= cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU
);
186 __a2mp_add_cl(mgr
, rsp
->cl
);
188 read_unlock(&hci_dev_list_lock
);
190 a2mp_send(mgr
, A2MP_DISCOVER_RSP
, hdr
->ident
, len
, rsp
);
196 static int a2mp_discover_rsp(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
197 struct a2mp_cmd
*hdr
)
199 struct a2mp_discov_rsp
*rsp
= (void *) skb
->data
;
200 u16 len
= le16_to_cpu(hdr
->len
);
205 if (len
< sizeof(*rsp
))
209 skb_pull(skb
, sizeof(*rsp
));
211 ext_feat
= le16_to_cpu(rsp
->ext_feat
);
213 BT_DBG("mtu %d efm 0x%4.4x", le16_to_cpu(rsp
->mtu
), ext_feat
);
215 /* check that packet is not broken for now */
216 while (ext_feat
& A2MP_FEAT_EXT
) {
217 if (len
< sizeof(ext_feat
))
220 ext_feat
= get_unaligned_le16(skb
->data
);
221 BT_DBG("efm 0x%4.4x", ext_feat
);
222 len
-= sizeof(ext_feat
);
223 skb_pull(skb
, sizeof(ext_feat
));
226 cl
= (void *) skb
->data
;
227 while (len
>= sizeof(*cl
)) {
228 BT_DBG("Remote AMP id %d type %d status %d", cl
->id
, cl
->type
,
231 if (cl
->id
!= AMP_ID_BREDR
&& cl
->type
!= AMP_TYPE_BREDR
) {
232 struct a2mp_info_req req
;
236 a2mp_send(mgr
, A2MP_GETINFO_REQ
, __next_ident(mgr
),
241 cl
= (void *) skb_pull(skb
, sizeof(*cl
));
244 /* Fall back to L2CAP init sequence */
246 struct l2cap_conn
*conn
= mgr
->l2cap_conn
;
247 struct l2cap_chan
*chan
;
249 mutex_lock(&conn
->chan_lock
);
251 list_for_each_entry(chan
, &conn
->chan_l
, list
) {
253 BT_DBG("chan %p state %s", chan
,
254 state_to_string(chan
->state
));
256 if (chan
->scid
== L2CAP_CID_A2MP
)
259 l2cap_chan_lock(chan
);
261 if (chan
->state
== BT_CONNECT
)
262 l2cap_send_conn_req(chan
);
264 l2cap_chan_unlock(chan
);
267 mutex_unlock(&conn
->chan_lock
);
273 static int a2mp_change_notify(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
274 struct a2mp_cmd
*hdr
)
276 struct a2mp_cl
*cl
= (void *) skb
->data
;
278 while (skb
->len
>= sizeof(*cl
)) {
279 BT_DBG("Controller id %d type %d status %d", cl
->id
, cl
->type
,
281 cl
= (struct a2mp_cl
*) skb_pull(skb
, sizeof(*cl
));
284 /* TODO send A2MP_CHANGE_RSP */
289 static int a2mp_getinfo_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
290 struct a2mp_cmd
*hdr
)
292 struct a2mp_info_req
*req
= (void *) skb
->data
;
293 struct hci_dev
*hdev
;
295 if (le16_to_cpu(hdr
->len
) < sizeof(*req
))
298 BT_DBG("id %d", req
->id
);
300 hdev
= hci_dev_get(req
->id
);
301 if (!hdev
|| hdev
->dev_type
!= HCI_AMP
) {
302 struct a2mp_info_rsp rsp
;
305 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
307 a2mp_send(mgr
, A2MP_GETINFO_RSP
, hdr
->ident
, sizeof(rsp
),
313 set_bit(READ_LOC_AMP_INFO
, &mgr
->state
);
314 hci_send_cmd(hdev
, HCI_OP_READ_LOCAL_AMP_INFO
, 0, NULL
);
320 skb_pull(skb
, sizeof(*req
));
324 static int a2mp_getinfo_rsp(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
325 struct a2mp_cmd
*hdr
)
327 struct a2mp_info_rsp
*rsp
= (struct a2mp_info_rsp
*) skb
->data
;
328 struct a2mp_amp_assoc_req req
;
329 struct amp_ctrl
*ctrl
;
331 if (le16_to_cpu(hdr
->len
) < sizeof(*rsp
))
334 BT_DBG("id %d status 0x%2.2x", rsp
->id
, rsp
->status
);
339 ctrl
= amp_ctrl_add(mgr
, rsp
->id
);
344 a2mp_send(mgr
, A2MP_GETAMPASSOC_REQ
, __next_ident(mgr
), sizeof(req
),
347 skb_pull(skb
, sizeof(*rsp
));
351 static int a2mp_getampassoc_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
352 struct a2mp_cmd
*hdr
)
354 struct a2mp_amp_assoc_req
*req
= (void *) skb
->data
;
355 struct hci_dev
*hdev
;
358 if (le16_to_cpu(hdr
->len
) < sizeof(*req
))
361 BT_DBG("id %d", req
->id
);
363 /* Make sure that other request is not processed */
364 tmp
= amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC
);
366 hdev
= hci_dev_get(req
->id
);
367 if (!hdev
|| hdev
->amp_type
== AMP_TYPE_BREDR
|| tmp
) {
368 struct a2mp_amp_assoc_rsp rsp
;
372 rsp
.status
= A2MP_STATUS_COLLISION_OCCURED
;
375 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
378 a2mp_send(mgr
, A2MP_GETAMPASSOC_RSP
, hdr
->ident
, sizeof(rsp
),
384 amp_read_loc_assoc(hdev
, mgr
);
390 skb_pull(skb
, sizeof(*req
));
394 static int a2mp_getampassoc_rsp(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
395 struct a2mp_cmd
*hdr
)
397 struct a2mp_amp_assoc_rsp
*rsp
= (void *) skb
->data
;
398 u16 len
= le16_to_cpu(hdr
->len
);
399 struct hci_dev
*hdev
;
400 struct amp_ctrl
*ctrl
;
401 struct hci_conn
*hcon
;
404 if (len
< sizeof(*rsp
))
407 assoc_len
= len
- sizeof(*rsp
);
409 BT_DBG("id %d status 0x%2.2x assoc len %zu", rsp
->id
, rsp
->status
,
415 /* Save remote ASSOC data */
416 ctrl
= amp_ctrl_lookup(mgr
, rsp
->id
);
420 assoc
= kmemdup(rsp
->amp_assoc
, assoc_len
, GFP_KERNEL
);
427 ctrl
->assoc_len
= assoc_len
;
428 ctrl
->assoc_rem_len
= assoc_len
;
429 ctrl
->assoc_len_so_far
= 0;
434 /* Create Phys Link */
435 hdev
= hci_dev_get(rsp
->id
);
439 hcon
= phylink_add(hdev
, mgr
, rsp
->id
, true);
443 BT_DBG("Created hcon %p: loc:%d -> rem:%d", hcon
, hdev
->id
, rsp
->id
);
445 mgr
->bredr_chan
->remote_amp_id
= rsp
->id
;
447 amp_create_phylink(hdev
, mgr
, hcon
);
455 static int a2mp_createphyslink_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
456 struct a2mp_cmd
*hdr
)
458 struct a2mp_physlink_req
*req
= (void *) skb
->data
;
460 struct a2mp_physlink_rsp rsp
;
461 struct hci_dev
*hdev
;
462 struct hci_conn
*hcon
;
463 struct amp_ctrl
*ctrl
;
465 if (le16_to_cpu(hdr
->len
) < sizeof(*req
))
468 BT_DBG("local_id %d, remote_id %d", req
->local_id
, req
->remote_id
);
470 rsp
.local_id
= req
->remote_id
;
471 rsp
.remote_id
= req
->local_id
;
473 hdev
= hci_dev_get(req
->remote_id
);
474 if (!hdev
|| hdev
->amp_type
== AMP_TYPE_BREDR
) {
475 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
479 ctrl
= amp_ctrl_lookup(mgr
, rsp
.remote_id
);
481 ctrl
= amp_ctrl_add(mgr
, rsp
.remote_id
);
485 rsp
.status
= A2MP_STATUS_UNABLE_START_LINK_CREATION
;
491 size_t assoc_len
= le16_to_cpu(hdr
->len
) - sizeof(*req
);
494 assoc
= kmemdup(req
->amp_assoc
, assoc_len
, GFP_KERNEL
);
501 ctrl
->assoc_len
= assoc_len
;
502 ctrl
->assoc_rem_len
= assoc_len
;
503 ctrl
->assoc_len_so_far
= 0;
508 hcon
= phylink_add(hdev
, mgr
, req
->local_id
, false);
510 amp_accept_phylink(hdev
, mgr
, hcon
);
511 rsp
.status
= A2MP_STATUS_SUCCESS
;
513 rsp
.status
= A2MP_STATUS_UNABLE_START_LINK_CREATION
;
520 /* Reply error now and success after HCI Write Remote AMP Assoc
521 command complete with success status
523 if (rsp
.status
!= A2MP_STATUS_SUCCESS
) {
524 a2mp_send(mgr
, A2MP_CREATEPHYSLINK_RSP
, hdr
->ident
,
527 set_bit(WRITE_REMOTE_AMP_ASSOC
, &mgr
->state
);
528 mgr
->ident
= hdr
->ident
;
531 skb_pull(skb
, le16_to_cpu(hdr
->len
));
535 static int a2mp_discphyslink_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
536 struct a2mp_cmd
*hdr
)
538 struct a2mp_physlink_req
*req
= (void *) skb
->data
;
539 struct a2mp_physlink_rsp rsp
;
540 struct hci_dev
*hdev
;
541 struct hci_conn
*hcon
;
543 if (le16_to_cpu(hdr
->len
) < sizeof(*req
))
546 BT_DBG("local_id %d remote_id %d", req
->local_id
, req
->remote_id
);
548 rsp
.local_id
= req
->remote_id
;
549 rsp
.remote_id
= req
->local_id
;
550 rsp
.status
= A2MP_STATUS_SUCCESS
;
552 hdev
= hci_dev_get(req
->remote_id
);
554 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
558 hcon
= hci_conn_hash_lookup_ba(hdev
, AMP_LINK
,
559 &mgr
->l2cap_conn
->hcon
->dst
);
561 BT_ERR("No phys link exist");
562 rsp
.status
= A2MP_STATUS_NO_PHYSICAL_LINK_EXISTS
;
566 /* TODO Disconnect Phys Link here */
572 a2mp_send(mgr
, A2MP_DISCONNPHYSLINK_RSP
, hdr
->ident
, sizeof(rsp
), &rsp
);
574 skb_pull(skb
, sizeof(*req
));
578 static inline int a2mp_cmd_rsp(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
579 struct a2mp_cmd
*hdr
)
581 BT_DBG("ident %d code 0x%2.2x", hdr
->ident
, hdr
->code
);
583 skb_pull(skb
, le16_to_cpu(hdr
->len
));
587 /* Handle A2MP signalling */
588 static int a2mp_chan_recv_cb(struct l2cap_chan
*chan
, struct sk_buff
*skb
)
590 struct a2mp_cmd
*hdr
;
591 struct amp_mgr
*mgr
= chan
->data
;
596 while (skb
->len
>= sizeof(*hdr
)) {
599 hdr
= (void *) skb
->data
;
600 len
= le16_to_cpu(hdr
->len
);
602 BT_DBG("code 0x%2.2x id %d len %u", hdr
->code
, hdr
->ident
, len
);
604 skb_pull(skb
, sizeof(*hdr
));
606 if (len
> skb
->len
|| !hdr
->ident
) {
611 mgr
->ident
= hdr
->ident
;
614 case A2MP_COMMAND_REJ
:
615 a2mp_command_rej(mgr
, skb
, hdr
);
618 case A2MP_DISCOVER_REQ
:
619 err
= a2mp_discover_req(mgr
, skb
, hdr
);
622 case A2MP_CHANGE_NOTIFY
:
623 err
= a2mp_change_notify(mgr
, skb
, hdr
);
626 case A2MP_GETINFO_REQ
:
627 err
= a2mp_getinfo_req(mgr
, skb
, hdr
);
630 case A2MP_GETAMPASSOC_REQ
:
631 err
= a2mp_getampassoc_req(mgr
, skb
, hdr
);
634 case A2MP_CREATEPHYSLINK_REQ
:
635 err
= a2mp_createphyslink_req(mgr
, skb
, hdr
);
638 case A2MP_DISCONNPHYSLINK_REQ
:
639 err
= a2mp_discphyslink_req(mgr
, skb
, hdr
);
642 case A2MP_DISCOVER_RSP
:
643 err
= a2mp_discover_rsp(mgr
, skb
, hdr
);
646 case A2MP_GETINFO_RSP
:
647 err
= a2mp_getinfo_rsp(mgr
, skb
, hdr
);
650 case A2MP_GETAMPASSOC_RSP
:
651 err
= a2mp_getampassoc_rsp(mgr
, skb
, hdr
);
654 case A2MP_CHANGE_RSP
:
655 case A2MP_CREATEPHYSLINK_RSP
:
656 case A2MP_DISCONNPHYSLINK_RSP
:
657 err
= a2mp_cmd_rsp(mgr
, skb
, hdr
);
661 BT_ERR("Unknown A2MP sig cmd 0x%2.2x", hdr
->code
);
668 struct a2mp_cmd_rej rej
;
670 rej
.reason
= cpu_to_le16(0);
671 hdr
= (void *) skb
->data
;
673 BT_DBG("Send A2MP Rej: cmd 0x%2.2x err %d", hdr
->code
, err
);
675 a2mp_send(mgr
, A2MP_COMMAND_REJ
, hdr
->ident
, sizeof(rej
),
679 /* Always free skb and return success error code to prevent
680 from sending L2CAP Disconnect over A2MP channel */
688 static void a2mp_chan_close_cb(struct l2cap_chan
*chan
)
690 l2cap_chan_put(chan
);
693 static void a2mp_chan_state_change_cb(struct l2cap_chan
*chan
, int state
,
696 struct amp_mgr
*mgr
= chan
->data
;
701 BT_DBG("chan %p state %s", chan
, state_to_string(state
));
713 static struct sk_buff
*a2mp_chan_alloc_skb_cb(struct l2cap_chan
*chan
,
714 unsigned long hdr_len
,
715 unsigned long len
, int nb
)
719 skb
= bt_skb_alloc(hdr_len
+ len
, GFP_KERNEL
);
721 return ERR_PTR(-ENOMEM
);
726 static const struct l2cap_ops a2mp_chan_ops
= {
727 .name
= "L2CAP A2MP channel",
728 .recv
= a2mp_chan_recv_cb
,
729 .close
= a2mp_chan_close_cb
,
730 .state_change
= a2mp_chan_state_change_cb
,
731 .alloc_skb
= a2mp_chan_alloc_skb_cb
,
733 /* Not implemented for A2MP */
734 .new_connection
= l2cap_chan_no_new_connection
,
735 .teardown
= l2cap_chan_no_teardown
,
736 .ready
= l2cap_chan_no_ready
,
737 .defer
= l2cap_chan_no_defer
,
738 .resume
= l2cap_chan_no_resume
,
739 .set_shutdown
= l2cap_chan_no_set_shutdown
,
740 .get_sndtimeo
= l2cap_chan_no_get_sndtimeo
,
743 static struct l2cap_chan
*a2mp_chan_open(struct l2cap_conn
*conn
, bool locked
)
745 struct l2cap_chan
*chan
;
748 chan
= l2cap_chan_create();
752 BT_DBG("chan %p", chan
);
754 chan
->chan_type
= L2CAP_CHAN_FIXED
;
755 chan
->scid
= L2CAP_CID_A2MP
;
756 chan
->dcid
= L2CAP_CID_A2MP
;
757 chan
->omtu
= L2CAP_A2MP_DEFAULT_MTU
;
758 chan
->imtu
= L2CAP_A2MP_DEFAULT_MTU
;
759 chan
->flush_to
= L2CAP_DEFAULT_FLUSH_TO
;
761 chan
->ops
= &a2mp_chan_ops
;
763 l2cap_chan_set_defaults(chan
);
764 chan
->remote_max_tx
= chan
->max_tx
;
765 chan
->remote_tx_win
= chan
->tx_win
;
767 chan
->retrans_timeout
= L2CAP_DEFAULT_RETRANS_TO
;
768 chan
->monitor_timeout
= L2CAP_DEFAULT_MONITOR_TO
;
770 skb_queue_head_init(&chan
->tx_q
);
772 chan
->mode
= L2CAP_MODE_ERTM
;
774 err
= l2cap_ertm_init(chan
);
776 l2cap_chan_del(chan
, 0);
780 chan
->conf_state
= 0;
783 __l2cap_chan_add(conn
, chan
);
785 l2cap_chan_add(conn
, chan
);
787 chan
->remote_mps
= chan
->omtu
;
788 chan
->mps
= chan
->omtu
;
790 chan
->state
= BT_CONNECTED
;
795 /* AMP Manager functions */
796 struct amp_mgr
*amp_mgr_get(struct amp_mgr
*mgr
)
798 BT_DBG("mgr %p orig refcnt %d", mgr
, atomic_read(&mgr
->kref
.refcount
));
800 kref_get(&mgr
->kref
);
805 static void amp_mgr_destroy(struct kref
*kref
)
807 struct amp_mgr
*mgr
= container_of(kref
, struct amp_mgr
, kref
);
809 BT_DBG("mgr %p", mgr
);
811 mutex_lock(&_mgr_list_lock
);
812 list_del(&mgr
->list
);
813 mutex_unlock(&_mgr_list_lock
);
815 amp_ctrl_list_flush(mgr
);
819 int amp_mgr_put(struct amp_mgr
*mgr
)
821 BT_DBG("mgr %p orig refcnt %d", mgr
, atomic_read(&mgr
->kref
.refcount
));
823 return kref_put(&mgr
->kref
, &_mgr_destroy
);
826 static struct amp_mgr
*amp_mgr_create(struct l2cap_conn
*conn
, bool locked
)
829 struct l2cap_chan
*chan
;
831 mgr
= kzalloc(sizeof(*mgr
), GFP_KERNEL
);
835 BT_DBG("conn %p mgr %p", conn
, mgr
);
837 mgr
->l2cap_conn
= conn
;
839 chan
= a2mp_chan_open(conn
, locked
);
845 mgr
->a2mp_chan
= chan
;
848 conn
->hcon
->amp_mgr
= mgr
;
850 kref_init(&mgr
->kref
);
852 /* Remote AMP ctrl list initialization */
853 INIT_LIST_HEAD(&mgr
->amp_ctrls
);
854 mutex_init(&mgr
->amp_ctrls_lock
);
856 mutex_lock(&_mgr_list_lock
);
857 list_add(&mgr
->list
, &_mgr_list
);
858 mutex_unlock(&_mgr_list_lock
);
863 struct l2cap_chan
*a2mp_channel_create(struct l2cap_conn
*conn
,
868 if (conn
->hcon
->type
!= ACL_LINK
)
871 mgr
= amp_mgr_create(conn
, false);
873 BT_ERR("Could not create AMP manager");
877 BT_DBG("mgr: %p chan %p", mgr
, mgr
->a2mp_chan
);
879 return mgr
->a2mp_chan
;
882 void a2mp_send_getinfo_rsp(struct hci_dev
*hdev
)
885 struct a2mp_info_rsp rsp
;
887 mgr
= amp_mgr_lookup_by_state(READ_LOC_AMP_INFO
);
891 BT_DBG("%s mgr %p", hdev
->name
, mgr
);
894 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
896 if (hdev
->amp_type
!= AMP_TYPE_BREDR
) {
898 rsp
.total_bw
= cpu_to_le32(hdev
->amp_total_bw
);
899 rsp
.max_bw
= cpu_to_le32(hdev
->amp_max_bw
);
900 rsp
.min_latency
= cpu_to_le32(hdev
->amp_min_latency
);
901 rsp
.pal_cap
= cpu_to_le16(hdev
->amp_pal_cap
);
902 rsp
.assoc_size
= cpu_to_le16(hdev
->amp_assoc_size
);
905 a2mp_send(mgr
, A2MP_GETINFO_RSP
, mgr
->ident
, sizeof(rsp
), &rsp
);
909 void a2mp_send_getampassoc_rsp(struct hci_dev
*hdev
, u8 status
)
912 struct amp_assoc
*loc_assoc
= &hdev
->loc_assoc
;
913 struct a2mp_amp_assoc_rsp
*rsp
;
916 mgr
= amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC
);
920 BT_DBG("%s mgr %p", hdev
->name
, mgr
);
922 len
= sizeof(struct a2mp_amp_assoc_rsp
) + loc_assoc
->len
;
923 rsp
= kzalloc(len
, GFP_KERNEL
);
932 rsp
->status
= A2MP_STATUS_INVALID_CTRL_ID
;
934 rsp
->status
= A2MP_STATUS_SUCCESS
;
935 memcpy(rsp
->amp_assoc
, loc_assoc
->data
, loc_assoc
->len
);
938 a2mp_send(mgr
, A2MP_GETAMPASSOC_RSP
, mgr
->ident
, len
, rsp
);
943 void a2mp_send_create_phy_link_req(struct hci_dev
*hdev
, u8 status
)
946 struct amp_assoc
*loc_assoc
= &hdev
->loc_assoc
;
947 struct a2mp_physlink_req
*req
;
948 struct l2cap_chan
*bredr_chan
;
951 mgr
= amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC_FINAL
);
955 len
= sizeof(*req
) + loc_assoc
->len
;
957 BT_DBG("%s mgr %p assoc_len %zu", hdev
->name
, mgr
, len
);
959 req
= kzalloc(len
, GFP_KERNEL
);
965 bredr_chan
= mgr
->bredr_chan
;
969 req
->local_id
= hdev
->id
;
970 req
->remote_id
= bredr_chan
->remote_amp_id
;
971 memcpy(req
->amp_assoc
, loc_assoc
->data
, loc_assoc
->len
);
973 a2mp_send(mgr
, A2MP_CREATEPHYSLINK_REQ
, __next_ident(mgr
), len
, req
);
980 void a2mp_send_create_phy_link_rsp(struct hci_dev
*hdev
, u8 status
)
983 struct a2mp_physlink_rsp rsp
;
984 struct hci_conn
*hs_hcon
;
986 mgr
= amp_mgr_lookup_by_state(WRITE_REMOTE_AMP_ASSOC
);
990 hs_hcon
= hci_conn_hash_lookup_state(hdev
, AMP_LINK
, BT_CONNECT
);
992 rsp
.status
= A2MP_STATUS_UNABLE_START_LINK_CREATION
;
994 rsp
.remote_id
= hs_hcon
->remote_id
;
995 rsp
.status
= A2MP_STATUS_SUCCESS
;
998 BT_DBG("%s mgr %p hs_hcon %p status %u", hdev
->name
, mgr
, hs_hcon
,
1001 rsp
.local_id
= hdev
->id
;
1002 a2mp_send(mgr
, A2MP_CREATEPHYSLINK_RSP
, mgr
->ident
, sizeof(rsp
), &rsp
);
1006 void a2mp_discover_amp(struct l2cap_chan
*chan
)
1008 struct l2cap_conn
*conn
= chan
->conn
;
1009 struct amp_mgr
*mgr
= conn
->hcon
->amp_mgr
;
1010 struct a2mp_discov_req req
;
1012 BT_DBG("chan %p conn %p mgr %p", chan
, conn
, mgr
);
1015 mgr
= amp_mgr_create(conn
, true);
1020 mgr
->bredr_chan
= chan
;
1022 req
.mtu
= cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU
);
1024 a2mp_send(mgr
, A2MP_DISCOVER_REQ
, 1, sizeof(req
), &req
);