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>
18 #include <net/bluetooth/a2mp.h>
19 #include <net/bluetooth/amp.h>
21 /* Global AMP Manager list */
22 LIST_HEAD(amp_mgr_list
);
23 DEFINE_MUTEX(amp_mgr_list_lock
);
25 /* A2MP build & send command helper functions */
26 static struct a2mp_cmd
*__a2mp_build(u8 code
, u8 ident
, u16 len
, void *data
)
31 plen
= sizeof(*cmd
) + len
;
32 cmd
= kzalloc(plen
, GFP_KERNEL
);
38 cmd
->len
= cpu_to_le16(len
);
40 memcpy(cmd
->data
, data
, len
);
45 void a2mp_send(struct amp_mgr
*mgr
, u8 code
, u8 ident
, u16 len
, void *data
)
47 struct l2cap_chan
*chan
= mgr
->a2mp_chan
;
49 u16 total_len
= len
+ sizeof(*cmd
);
53 cmd
= __a2mp_build(code
, ident
, len
, data
);
58 iv
.iov_len
= total_len
;
60 memset(&msg
, 0, sizeof(msg
));
62 msg
.msg_iov
= (struct iovec
*) &iv
;
65 l2cap_chan_send(chan
, &msg
, total_len
, 0);
70 static inline void __a2mp_cl_bredr(struct a2mp_cl
*cl
)
77 /* hci_dev_list shall be locked */
78 static void __a2mp_add_cl(struct amp_mgr
*mgr
, struct a2mp_cl
*cl
, u8 num_ctrl
)
85 list_for_each_entry(hdev
, &hci_dev_list
, list
) {
86 /* Iterate through AMP controllers */
87 if (hdev
->id
== HCI_BREDR_ID
)
90 /* Starting from second entry */
95 cl
[i
].type
= hdev
->amp_type
;
96 cl
[i
].status
= hdev
->amp_status
;
100 /* Processing A2MP messages */
101 static int a2mp_command_rej(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
102 struct a2mp_cmd
*hdr
)
104 struct a2mp_cmd_rej
*rej
= (void *) skb
->data
;
106 if (le16_to_cpu(hdr
->len
) < sizeof(*rej
))
109 BT_DBG("ident %d reason %d", hdr
->ident
, le16_to_cpu(rej
->reason
));
111 skb_pull(skb
, sizeof(*rej
));
116 static int a2mp_discover_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
117 struct a2mp_cmd
*hdr
)
119 struct a2mp_discov_req
*req
= (void *) skb
->data
;
120 u16 len
= le16_to_cpu(hdr
->len
);
121 struct a2mp_discov_rsp
*rsp
;
125 if (len
< sizeof(*req
))
128 skb_pull(skb
, sizeof(*req
));
130 ext_feat
= le16_to_cpu(req
->ext_feat
);
132 BT_DBG("mtu %d efm 0x%4.4x", le16_to_cpu(req
->mtu
), ext_feat
);
134 /* check that packet is not broken for now */
135 while (ext_feat
& A2MP_FEAT_EXT
) {
136 if (len
< sizeof(ext_feat
))
139 ext_feat
= get_unaligned_le16(skb
->data
);
140 BT_DBG("efm 0x%4.4x", ext_feat
);
141 len
-= sizeof(ext_feat
);
142 skb_pull(skb
, sizeof(ext_feat
));
145 read_lock(&hci_dev_list_lock
);
147 num_ctrl
= __hci_num_ctrl();
148 len
= num_ctrl
* sizeof(struct a2mp_cl
) + sizeof(*rsp
);
149 rsp
= kmalloc(len
, GFP_ATOMIC
);
151 read_unlock(&hci_dev_list_lock
);
155 rsp
->mtu
= __constant_cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU
);
158 __a2mp_add_cl(mgr
, rsp
->cl
, num_ctrl
);
160 read_unlock(&hci_dev_list_lock
);
162 a2mp_send(mgr
, A2MP_DISCOVER_RSP
, hdr
->ident
, len
, rsp
);
168 static int a2mp_change_notify(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
169 struct a2mp_cmd
*hdr
)
171 struct a2mp_cl
*cl
= (void *) skb
->data
;
173 while (skb
->len
>= sizeof(*cl
)) {
174 BT_DBG("Controller id %d type %d status %d", cl
->id
, cl
->type
,
176 cl
= (struct a2mp_cl
*) skb_pull(skb
, sizeof(*cl
));
179 /* TODO send A2MP_CHANGE_RSP */
184 static int a2mp_getinfo_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
185 struct a2mp_cmd
*hdr
)
187 struct a2mp_info_req
*req
= (void *) skb
->data
;
188 struct hci_dev
*hdev
;
190 if (le16_to_cpu(hdr
->len
) < sizeof(*req
))
193 BT_DBG("id %d", req
->id
);
195 hdev
= hci_dev_get(req
->id
);
197 struct a2mp_info_rsp rsp
;
200 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
202 a2mp_send(mgr
, A2MP_GETINFO_RSP
, hdr
->ident
, sizeof(rsp
),
206 if (hdev
->dev_type
!= HCI_BREDR
) {
207 mgr
->state
= READ_LOC_AMP_INFO
;
208 hci_send_cmd(hdev
, HCI_OP_READ_LOCAL_AMP_INFO
, 0, NULL
);
213 skb_pull(skb
, sizeof(*req
));
217 static int a2mp_getampassoc_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
218 struct a2mp_cmd
*hdr
)
220 struct a2mp_amp_assoc_req
*req
= (void *) skb
->data
;
221 struct hci_dev
*hdev
;
224 if (le16_to_cpu(hdr
->len
) < sizeof(*req
))
227 BT_DBG("id %d", req
->id
);
229 /* Make sure that other request is not processed */
230 tmp
= amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC
);
232 hdev
= hci_dev_get(req
->id
);
233 if (!hdev
|| hdev
->amp_type
== HCI_BREDR
|| tmp
) {
234 struct a2mp_amp_assoc_rsp rsp
;
238 rsp
.status
= A2MP_STATUS_COLLISION_OCCURED
;
241 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
244 a2mp_send(mgr
, A2MP_GETAMPASSOC_RSP
, hdr
->ident
, sizeof(rsp
),
250 amp_read_loc_assoc(hdev
, mgr
);
256 skb_pull(skb
, sizeof(*req
));
260 static int a2mp_createphyslink_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
261 struct a2mp_cmd
*hdr
)
263 struct a2mp_physlink_req
*req
= (void *) skb
->data
;
265 struct a2mp_physlink_rsp rsp
;
266 struct hci_dev
*hdev
;
268 if (le16_to_cpu(hdr
->len
) < sizeof(*req
))
271 BT_DBG("local_id %d, remote_id %d", req
->local_id
, req
->remote_id
);
273 rsp
.local_id
= req
->remote_id
;
274 rsp
.remote_id
= req
->local_id
;
276 hdev
= hci_dev_get(req
->remote_id
);
277 if (!hdev
|| hdev
->amp_type
!= HCI_AMP
) {
278 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
282 /* TODO process physlink create */
284 rsp
.status
= A2MP_STATUS_SUCCESS
;
290 a2mp_send(mgr
, A2MP_CREATEPHYSLINK_RSP
, hdr
->ident
, sizeof(rsp
),
293 skb_pull(skb
, le16_to_cpu(hdr
->len
));
297 static int a2mp_discphyslink_req(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
298 struct a2mp_cmd
*hdr
)
300 struct a2mp_physlink_req
*req
= (void *) skb
->data
;
301 struct a2mp_physlink_rsp rsp
;
302 struct hci_dev
*hdev
;
304 if (le16_to_cpu(hdr
->len
) < sizeof(*req
))
307 BT_DBG("local_id %d remote_id %d", req
->local_id
, req
->remote_id
);
309 rsp
.local_id
= req
->remote_id
;
310 rsp
.remote_id
= req
->local_id
;
311 rsp
.status
= A2MP_STATUS_SUCCESS
;
313 hdev
= hci_dev_get(req
->local_id
);
315 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
319 /* TODO Disconnect Phys Link here */
324 a2mp_send(mgr
, A2MP_DISCONNPHYSLINK_RSP
, hdr
->ident
, sizeof(rsp
), &rsp
);
326 skb_pull(skb
, sizeof(*req
));
330 static inline int a2mp_cmd_rsp(struct amp_mgr
*mgr
, struct sk_buff
*skb
,
331 struct a2mp_cmd
*hdr
)
333 BT_DBG("ident %d code 0x%2.2x", hdr
->ident
, hdr
->code
);
335 skb_pull(skb
, le16_to_cpu(hdr
->len
));
339 /* Handle A2MP signalling */
340 static int a2mp_chan_recv_cb(struct l2cap_chan
*chan
, struct sk_buff
*skb
)
342 struct a2mp_cmd
*hdr
;
343 struct amp_mgr
*mgr
= chan
->data
;
348 while (skb
->len
>= sizeof(*hdr
)) {
351 hdr
= (void *) skb
->data
;
352 len
= le16_to_cpu(hdr
->len
);
354 BT_DBG("code 0x%2.2x id %d len %u", hdr
->code
, hdr
->ident
, len
);
356 skb_pull(skb
, sizeof(*hdr
));
358 if (len
> skb
->len
|| !hdr
->ident
) {
363 mgr
->ident
= hdr
->ident
;
366 case A2MP_COMMAND_REJ
:
367 a2mp_command_rej(mgr
, skb
, hdr
);
370 case A2MP_DISCOVER_REQ
:
371 err
= a2mp_discover_req(mgr
, skb
, hdr
);
374 case A2MP_CHANGE_NOTIFY
:
375 err
= a2mp_change_notify(mgr
, skb
, hdr
);
378 case A2MP_GETINFO_REQ
:
379 err
= a2mp_getinfo_req(mgr
, skb
, hdr
);
382 case A2MP_GETAMPASSOC_REQ
:
383 err
= a2mp_getampassoc_req(mgr
, skb
, hdr
);
386 case A2MP_CREATEPHYSLINK_REQ
:
387 err
= a2mp_createphyslink_req(mgr
, skb
, hdr
);
390 case A2MP_DISCONNPHYSLINK_REQ
:
391 err
= a2mp_discphyslink_req(mgr
, skb
, hdr
);
394 case A2MP_CHANGE_RSP
:
395 case A2MP_DISCOVER_RSP
:
396 case A2MP_GETINFO_RSP
:
397 case A2MP_GETAMPASSOC_RSP
:
398 case A2MP_CREATEPHYSLINK_RSP
:
399 case A2MP_DISCONNPHYSLINK_RSP
:
400 err
= a2mp_cmd_rsp(mgr
, skb
, hdr
);
404 BT_ERR("Unknown A2MP sig cmd 0x%2.2x", hdr
->code
);
411 struct a2mp_cmd_rej rej
;
413 rej
.reason
= __constant_cpu_to_le16(0);
414 hdr
= (void *) skb
->data
;
416 BT_DBG("Send A2MP Rej: cmd 0x%2.2x err %d", hdr
->code
, err
);
418 a2mp_send(mgr
, A2MP_COMMAND_REJ
, hdr
->ident
, sizeof(rej
),
422 /* Always free skb and return success error code to prevent
423 from sending L2CAP Disconnect over A2MP channel */
431 static void a2mp_chan_close_cb(struct l2cap_chan
*chan
)
433 l2cap_chan_put(chan
);
436 static void a2mp_chan_state_change_cb(struct l2cap_chan
*chan
, int state
)
438 struct amp_mgr
*mgr
= chan
->data
;
443 BT_DBG("chan %p state %s", chan
, state_to_string(state
));
455 static struct sk_buff
*a2mp_chan_alloc_skb_cb(struct l2cap_chan
*chan
,
456 unsigned long len
, int nb
)
458 return bt_skb_alloc(len
, GFP_KERNEL
);
461 static struct l2cap_ops a2mp_chan_ops
= {
462 .name
= "L2CAP A2MP channel",
463 .recv
= a2mp_chan_recv_cb
,
464 .close
= a2mp_chan_close_cb
,
465 .state_change
= a2mp_chan_state_change_cb
,
466 .alloc_skb
= a2mp_chan_alloc_skb_cb
,
468 /* Not implemented for A2MP */
469 .new_connection
= l2cap_chan_no_new_connection
,
470 .teardown
= l2cap_chan_no_teardown
,
471 .ready
= l2cap_chan_no_ready
,
474 static struct l2cap_chan
*a2mp_chan_open(struct l2cap_conn
*conn
)
476 struct l2cap_chan
*chan
;
479 chan
= l2cap_chan_create();
483 BT_DBG("chan %p", chan
);
485 chan
->chan_type
= L2CAP_CHAN_CONN_FIX_A2MP
;
486 chan
->flush_to
= L2CAP_DEFAULT_FLUSH_TO
;
488 chan
->ops
= &a2mp_chan_ops
;
490 l2cap_chan_set_defaults(chan
);
491 chan
->remote_max_tx
= chan
->max_tx
;
492 chan
->remote_tx_win
= chan
->tx_win
;
494 chan
->retrans_timeout
= L2CAP_DEFAULT_RETRANS_TO
;
495 chan
->monitor_timeout
= L2CAP_DEFAULT_MONITOR_TO
;
497 skb_queue_head_init(&chan
->tx_q
);
499 chan
->mode
= L2CAP_MODE_ERTM
;
501 err
= l2cap_ertm_init(chan
);
503 l2cap_chan_del(chan
, 0);
507 chan
->conf_state
= 0;
509 l2cap_chan_add(conn
, chan
);
511 chan
->remote_mps
= chan
->omtu
;
512 chan
->mps
= chan
->omtu
;
514 chan
->state
= BT_CONNECTED
;
519 /* AMP Manager functions */
520 void amp_mgr_get(struct amp_mgr
*mgr
)
522 BT_DBG("mgr %p orig refcnt %d", mgr
, atomic_read(&mgr
->kref
.refcount
));
524 kref_get(&mgr
->kref
);
527 static void amp_mgr_destroy(struct kref
*kref
)
529 struct amp_mgr
*mgr
= container_of(kref
, struct amp_mgr
, kref
);
531 BT_DBG("mgr %p", mgr
);
533 mutex_lock(&_mgr_list_lock
);
534 list_del(&mgr
->list
);
535 mutex_unlock(&_mgr_list_lock
);
540 int amp_mgr_put(struct amp_mgr
*mgr
)
542 BT_DBG("mgr %p orig refcnt %d", mgr
, atomic_read(&mgr
->kref
.refcount
));
544 return kref_put(&mgr
->kref
, &_mgr_destroy
);
547 static struct amp_mgr
*amp_mgr_create(struct l2cap_conn
*conn
)
550 struct l2cap_chan
*chan
;
552 mgr
= kzalloc(sizeof(*mgr
), GFP_KERNEL
);
556 BT_DBG("conn %p mgr %p", conn
, mgr
);
558 mgr
->l2cap_conn
= conn
;
560 chan
= a2mp_chan_open(conn
);
566 mgr
->a2mp_chan
= chan
;
569 conn
->hcon
->amp_mgr
= mgr
;
571 kref_init(&mgr
->kref
);
573 mutex_lock(&_mgr_list_lock
);
574 list_add(&mgr
->list
, &_mgr_list
);
575 mutex_unlock(&_mgr_list_lock
);
580 struct l2cap_chan
*a2mp_channel_create(struct l2cap_conn
*conn
,
585 mgr
= amp_mgr_create(conn
);
587 BT_ERR("Could not create AMP manager");
591 BT_DBG("mgr: %p chan %p", mgr
, mgr
->a2mp_chan
);
593 return mgr
->a2mp_chan
;
596 struct amp_mgr
*amp_mgr_lookup_by_state(u8 state
)
600 mutex_lock(&_mgr_list_lock
);
601 list_for_each_entry(mgr
, &_mgr_list
, list
) {
602 if (mgr
->state
== state
) {
604 mutex_unlock(&_mgr_list_lock
);
608 mutex_unlock(&_mgr_list_lock
);
613 void a2mp_send_getinfo_rsp(struct hci_dev
*hdev
)
616 struct a2mp_info_rsp rsp
;
618 mgr
= amp_mgr_lookup_by_state(READ_LOC_AMP_INFO
);
622 BT_DBG("%s mgr %p", hdev
->name
, mgr
);
625 rsp
.status
= A2MP_STATUS_INVALID_CTRL_ID
;
627 if (hdev
->amp_type
!= HCI_BREDR
) {
629 rsp
.total_bw
= cpu_to_le32(hdev
->amp_total_bw
);
630 rsp
.max_bw
= cpu_to_le32(hdev
->amp_max_bw
);
631 rsp
.min_latency
= cpu_to_le32(hdev
->amp_min_latency
);
632 rsp
.pal_cap
= cpu_to_le16(hdev
->amp_pal_cap
);
633 rsp
.assoc_size
= cpu_to_le16(hdev
->amp_assoc_size
);
636 a2mp_send(mgr
, A2MP_GETINFO_RSP
, mgr
->ident
, sizeof(rsp
), &rsp
);
640 void a2mp_send_getampassoc_rsp(struct hci_dev
*hdev
, u8 status
)
643 struct amp_assoc
*loc_assoc
= &hdev
->loc_assoc
;
644 struct a2mp_amp_assoc_rsp
*rsp
;
647 mgr
= amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC
);
651 BT_DBG("%s mgr %p", hdev
->name
, mgr
);
653 len
= sizeof(struct a2mp_amp_assoc_rsp
) + loc_assoc
->len
;
654 rsp
= kzalloc(len
, GFP_KERNEL
);
663 rsp
->status
= A2MP_STATUS_INVALID_CTRL_ID
;
665 rsp
->status
= A2MP_STATUS_SUCCESS
;
666 memcpy(rsp
->amp_assoc
, loc_assoc
->data
, loc_assoc
->len
);
669 a2mp_send(mgr
, A2MP_GETAMPASSOC_RSP
, mgr
->ident
, len
, rsp
);