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 u8 __next_ident(struct amp_mgr *mgr)
72 if (++mgr->ident == 0)
78 static inline void __a2mp_cl_bredr(struct a2mp_cl *cl)
85 /* hci_dev_list shall be locked */
86 static void __a2mp_add_cl(struct amp_mgr *mgr, struct a2mp_cl *cl, u8 num_ctrl)
93 list_for_each_entry(hdev, &hci_dev_list, list) {
94 /* Iterate through AMP controllers */
95 if (hdev->id == HCI_BREDR_ID)
98 /* Starting from second entry */
103 cl[i].type = hdev->amp_type;
104 cl[i].status = hdev->amp_status;
108 /* Processing A2MP messages */
109 static int a2mp_command_rej(struct amp_mgr *mgr, struct sk_buff *skb,
110 struct a2mp_cmd *hdr)
112 struct a2mp_cmd_rej *rej = (void *) skb->data;
114 if (le16_to_cpu(hdr->len) < sizeof(*rej))
117 BT_DBG("ident %d reason %d", hdr->ident, le16_to_cpu(rej->reason));
119 skb_pull(skb, sizeof(*rej));
124 static int a2mp_discover_req(struct amp_mgr *mgr, struct sk_buff *skb,
125 struct a2mp_cmd *hdr)
127 struct a2mp_discov_req *req = (void *) skb->data;
128 u16 len = le16_to_cpu(hdr->len);
129 struct a2mp_discov_rsp *rsp;
133 if (len < sizeof(*req))
136 skb_pull(skb, sizeof(*req));
138 ext_feat = le16_to_cpu(req->ext_feat);
140 BT_DBG("mtu %d efm 0x%4.4x", le16_to_cpu(req->mtu), ext_feat);
142 /* check that packet is not broken for now */
143 while (ext_feat & A2MP_FEAT_EXT) {
144 if (len < sizeof(ext_feat))
147 ext_feat = get_unaligned_le16(skb->data);
148 BT_DBG("efm 0x%4.4x", ext_feat);
149 len -= sizeof(ext_feat);
150 skb_pull(skb, sizeof(ext_feat));
153 read_lock(&hci_dev_list_lock);
155 num_ctrl = __hci_num_ctrl();
156 len = num_ctrl * sizeof(struct a2mp_cl) + sizeof(*rsp);
157 rsp = kmalloc(len, GFP_ATOMIC);
159 read_unlock(&hci_dev_list_lock);
163 rsp->mtu = __constant_cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU);
166 __a2mp_add_cl(mgr, rsp->cl, num_ctrl);
168 read_unlock(&hci_dev_list_lock);
170 a2mp_send(mgr, A2MP_DISCOVER_RSP, hdr->ident, len, rsp);
176 static int a2mp_discover_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
177 struct a2mp_cmd *hdr)
179 struct a2mp_discov_rsp *rsp = (void *) skb->data;
180 u16 len = le16_to_cpu(hdr->len);
184 if (len < sizeof(*rsp))
188 skb_pull(skb, sizeof(*rsp));
190 ext_feat = le16_to_cpu(rsp->ext_feat);
192 BT_DBG("mtu %d efm 0x%4.4x", le16_to_cpu(rsp->mtu), ext_feat);
194 /* check that packet is not broken for now */
195 while (ext_feat & A2MP_FEAT_EXT) {
196 if (len < sizeof(ext_feat))
199 ext_feat = get_unaligned_le16(skb->data);
200 BT_DBG("efm 0x%4.4x", ext_feat);
201 len -= sizeof(ext_feat);
202 skb_pull(skb, sizeof(ext_feat));
205 cl = (void *) skb->data;
206 while (len >= sizeof(*cl)) {
207 BT_DBG("Remote AMP id %d type %d status %d", cl->id, cl->type,
210 if (cl->id != HCI_BREDR_ID && cl->type == HCI_AMP) {
211 struct a2mp_info_req req;
214 a2mp_send(mgr, A2MP_GETINFO_REQ, __next_ident(mgr),
219 cl = (void *) skb_pull(skb, sizeof(*cl));
225 static int a2mp_change_notify(struct amp_mgr *mgr, struct sk_buff *skb,
226 struct a2mp_cmd *hdr)
228 struct a2mp_cl *cl = (void *) skb->data;
230 while (skb->len >= sizeof(*cl)) {
231 BT_DBG("Controller id %d type %d status %d", cl->id, cl->type,
233 cl = (struct a2mp_cl *) skb_pull(skb, sizeof(*cl));
236 /* TODO send A2MP_CHANGE_RSP */
241 static int a2mp_getinfo_req(struct amp_mgr *mgr, struct sk_buff *skb,
242 struct a2mp_cmd *hdr)
244 struct a2mp_info_req *req = (void *) skb->data;
245 struct hci_dev *hdev;
247 if (le16_to_cpu(hdr->len) < sizeof(*req))
250 BT_DBG("id %d", req->id);
252 hdev = hci_dev_get(req->id);
254 struct a2mp_info_rsp rsp;
257 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
259 a2mp_send(mgr, A2MP_GETINFO_RSP, hdr->ident, sizeof(rsp),
263 if (hdev->dev_type != HCI_BREDR) {
264 mgr->state = READ_LOC_AMP_INFO;
265 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL);
270 skb_pull(skb, sizeof(*req));
274 static int a2mp_getinfo_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
275 struct a2mp_cmd *hdr)
277 struct a2mp_info_rsp *rsp = (struct a2mp_info_rsp *) skb->data;
278 struct a2mp_amp_assoc_req req;
279 struct amp_ctrl *ctrl;
281 if (le16_to_cpu(hdr->len) < sizeof(*rsp))
284 BT_DBG("id %d status 0x%2.2x", rsp->id, rsp->status);
289 ctrl = amp_ctrl_add(mgr);
296 a2mp_send(mgr, A2MP_GETAMPASSOC_REQ, __next_ident(mgr), sizeof(req),
299 skb_pull(skb, sizeof(*rsp));
303 static int a2mp_getampassoc_req(struct amp_mgr *mgr, struct sk_buff *skb,
304 struct a2mp_cmd *hdr)
306 struct a2mp_amp_assoc_req *req = (void *) skb->data;
307 struct hci_dev *hdev;
310 if (le16_to_cpu(hdr->len) < sizeof(*req))
313 BT_DBG("id %d", req->id);
315 /* Make sure that other request is not processed */
316 tmp = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC);
318 hdev = hci_dev_get(req->id);
319 if (!hdev || hdev->amp_type == HCI_BREDR || tmp) {
320 struct a2mp_amp_assoc_rsp rsp;
324 rsp.status = A2MP_STATUS_COLLISION_OCCURED;
327 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
330 a2mp_send(mgr, A2MP_GETAMPASSOC_RSP, hdr->ident, sizeof(rsp),
336 amp_read_loc_assoc(hdev, mgr);
342 skb_pull(skb, sizeof(*req));
346 static int a2mp_getampassoc_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
347 struct a2mp_cmd *hdr)
349 struct a2mp_amp_assoc_rsp *rsp = (void *) skb->data;
350 u16 len = le16_to_cpu(hdr->len);
351 struct hci_dev *hdev;
352 struct amp_ctrl *ctrl;
353 struct hci_conn *hcon;
355 if (len < sizeof(*rsp))
358 BT_DBG("id %d status 0x%2.2x assoc len %u", rsp->id, rsp->status,
364 /* Save remote ASSOC data */
365 ctrl = amp_ctrl_lookup(mgr, rsp->id);
367 u8 *assoc, assoc_len = len - sizeof(*rsp);
369 assoc = kzalloc(assoc_len, GFP_KERNEL);
375 memcpy(assoc, rsp->amp_assoc, assoc_len);
377 ctrl->assoc_len = assoc_len;
378 ctrl->assoc_rem_len = assoc_len;
379 ctrl->assoc_len_so_far = 0;
384 /* Create Phys Link */
385 hdev = hci_dev_get(rsp->id);
389 hcon = phylink_add(hdev, mgr, rsp->id);
393 BT_DBG("Created hcon %p: loc:%d -> rem:%d", hcon, hdev->id, rsp->id);
401 static int a2mp_createphyslink_req(struct amp_mgr *mgr, struct sk_buff *skb,
402 struct a2mp_cmd *hdr)
404 struct a2mp_physlink_req *req = (void *) skb->data;
406 struct a2mp_physlink_rsp rsp;
407 struct hci_dev *hdev;
408 struct hci_conn *hcon;
410 if (le16_to_cpu(hdr->len) < sizeof(*req))
413 BT_DBG("local_id %d, remote_id %d", req->local_id, req->remote_id);
415 rsp.local_id = req->remote_id;
416 rsp.remote_id = req->local_id;
418 hdev = hci_dev_get(req->remote_id);
419 if (!hdev || hdev->amp_type != HCI_AMP) {
420 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
424 /* TODO process physlink create */
426 hcon = phylink_add(hdev, mgr, req->local_id);
428 BT_DBG("hcon %p", hcon);
430 rsp.status = A2MP_STATUS_SUCCESS;
432 rsp.status = A2MP_STATUS_UNABLE_START_LINK_CREATION;
439 a2mp_send(mgr, A2MP_CREATEPHYSLINK_RSP, hdr->ident, sizeof(rsp),
442 skb_pull(skb, le16_to_cpu(hdr->len));
446 static int a2mp_discphyslink_req(struct amp_mgr *mgr, struct sk_buff *skb,
447 struct a2mp_cmd *hdr)
449 struct a2mp_physlink_req *req = (void *) skb->data;
450 struct a2mp_physlink_rsp rsp;
451 struct hci_dev *hdev;
452 struct hci_conn *hcon;
454 if (le16_to_cpu(hdr->len) < sizeof(*req))
457 BT_DBG("local_id %d remote_id %d", req->local_id, req->remote_id);
459 rsp.local_id = req->remote_id;
460 rsp.remote_id = req->local_id;
461 rsp.status = A2MP_STATUS_SUCCESS;
463 hdev = hci_dev_get(req->remote_id);
465 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
469 hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK, mgr->l2cap_conn->dst);
471 BT_ERR("No phys link exist");
472 rsp.status = A2MP_STATUS_NO_PHYSICAL_LINK_EXISTS;
476 /* TODO Disconnect Phys Link here */
482 a2mp_send(mgr, A2MP_DISCONNPHYSLINK_RSP, hdr->ident, sizeof(rsp), &rsp);
484 skb_pull(skb, sizeof(*req));
488 static inline int a2mp_cmd_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
489 struct a2mp_cmd *hdr)
491 BT_DBG("ident %d code 0x%2.2x", hdr->ident, hdr->code);
493 skb_pull(skb, le16_to_cpu(hdr->len));
497 /* Handle A2MP signalling */
498 static int a2mp_chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
500 struct a2mp_cmd *hdr;
501 struct amp_mgr *mgr = chan->data;
506 while (skb->len >= sizeof(*hdr)) {
509 hdr = (void *) skb->data;
510 len = le16_to_cpu(hdr->len);
512 BT_DBG("code 0x%2.2x id %d len %u", hdr->code, hdr->ident, len);
514 skb_pull(skb, sizeof(*hdr));
516 if (len > skb->len || !hdr->ident) {
521 mgr->ident = hdr->ident;
524 case A2MP_COMMAND_REJ:
525 a2mp_command_rej(mgr, skb, hdr);
528 case A2MP_DISCOVER_REQ:
529 err = a2mp_discover_req(mgr, skb, hdr);
532 case A2MP_CHANGE_NOTIFY:
533 err = a2mp_change_notify(mgr, skb, hdr);
536 case A2MP_GETINFO_REQ:
537 err = a2mp_getinfo_req(mgr, skb, hdr);
540 case A2MP_GETAMPASSOC_REQ:
541 err = a2mp_getampassoc_req(mgr, skb, hdr);
544 case A2MP_CREATEPHYSLINK_REQ:
545 err = a2mp_createphyslink_req(mgr, skb, hdr);
548 case A2MP_DISCONNPHYSLINK_REQ:
549 err = a2mp_discphyslink_req(mgr, skb, hdr);
552 case A2MP_DISCOVER_RSP:
553 err = a2mp_discover_rsp(mgr, skb, hdr);
556 case A2MP_GETINFO_RSP:
557 err = a2mp_getinfo_rsp(mgr, skb, hdr);
560 case A2MP_GETAMPASSOC_RSP:
561 err = a2mp_getampassoc_rsp(mgr, skb, hdr);
564 case A2MP_CHANGE_RSP:
565 case A2MP_CREATEPHYSLINK_RSP:
566 case A2MP_DISCONNPHYSLINK_RSP:
567 err = a2mp_cmd_rsp(mgr, skb, hdr);
571 BT_ERR("Unknown A2MP sig cmd 0x%2.2x", hdr->code);
578 struct a2mp_cmd_rej rej;
580 rej.reason = __constant_cpu_to_le16(0);
581 hdr = (void *) skb->data;
583 BT_DBG("Send A2MP Rej: cmd 0x%2.2x err %d", hdr->code, err);
585 a2mp_send(mgr, A2MP_COMMAND_REJ, hdr->ident, sizeof(rej),
589 /* Always free skb and return success error code to prevent
590 from sending L2CAP Disconnect over A2MP channel */
598 static void a2mp_chan_close_cb(struct l2cap_chan *chan)
600 l2cap_chan_put(chan);
603 static void a2mp_chan_state_change_cb(struct l2cap_chan *chan, int state)
605 struct amp_mgr *mgr = chan->data;
610 BT_DBG("chan %p state %s", chan, state_to_string(state));
622 static struct sk_buff *a2mp_chan_alloc_skb_cb(struct l2cap_chan *chan,
623 unsigned long len, int nb)
625 return bt_skb_alloc(len, GFP_KERNEL);
628 static struct l2cap_ops a2mp_chan_ops = {
629 .name = "L2CAP A2MP channel",
630 .recv = a2mp_chan_recv_cb,
631 .close = a2mp_chan_close_cb,
632 .state_change = a2mp_chan_state_change_cb,
633 .alloc_skb = a2mp_chan_alloc_skb_cb,
635 /* Not implemented for A2MP */
636 .new_connection = l2cap_chan_no_new_connection,
637 .teardown = l2cap_chan_no_teardown,
638 .ready = l2cap_chan_no_ready,
641 static struct l2cap_chan *a2mp_chan_open(struct l2cap_conn *conn)
643 struct l2cap_chan *chan;
646 chan = l2cap_chan_create();
650 BT_DBG("chan %p", chan);
652 chan->chan_type = L2CAP_CHAN_CONN_FIX_A2MP;
653 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
655 chan->ops = &a2mp_chan_ops;
657 l2cap_chan_set_defaults(chan);
658 chan->remote_max_tx = chan->max_tx;
659 chan->remote_tx_win = chan->tx_win;
661 chan->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO;
662 chan->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO;
664 skb_queue_head_init(&chan->tx_q);
666 chan->mode = L2CAP_MODE_ERTM;
668 err = l2cap_ertm_init(chan);
670 l2cap_chan_del(chan, 0);
674 chan->conf_state = 0;
676 l2cap_chan_add(conn, chan);
678 chan->remote_mps = chan->omtu;
679 chan->mps = chan->omtu;
681 chan->state = BT_CONNECTED;
686 /* AMP Manager functions */
687 void amp_mgr_get(struct amp_mgr *mgr)
689 BT_DBG("mgr %p orig refcnt %d", mgr, atomic_read(&mgr->kref.refcount));
691 kref_get(&mgr->kref);
694 static void amp_mgr_destroy(struct kref *kref)
696 struct amp_mgr *mgr = container_of(kref, struct amp_mgr, kref);
698 BT_DBG("mgr %p", mgr);
700 mutex_lock(&_mgr_list_lock);
701 list_del(&mgr->list);
702 mutex_unlock(&_mgr_list_lock);
704 amp_ctrl_list_flush(mgr);
708 int amp_mgr_put(struct amp_mgr *mgr)
710 BT_DBG("mgr %p orig refcnt %d", mgr, atomic_read(&mgr->kref.refcount));
712 return kref_put(&mgr->kref, &_mgr_destroy);
715 static struct amp_mgr *amp_mgr_create(struct l2cap_conn *conn)
718 struct l2cap_chan *chan;
720 mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
724 BT_DBG("conn %p mgr %p", conn, mgr);
726 mgr->l2cap_conn = conn;
728 chan = a2mp_chan_open(conn);
734 mgr->a2mp_chan = chan;
737 conn->hcon->amp_mgr = mgr;
739 kref_init(&mgr->kref);
741 /* Remote AMP ctrl list initialization */
742 INIT_LIST_HEAD(&mgr->amp_ctrls);
743 mutex_init(&mgr->amp_ctrls_lock);
745 mutex_lock(&_mgr_list_lock);
746 list_add(&mgr->list, &_mgr_list);
747 mutex_unlock(&_mgr_list_lock);
752 struct l2cap_chan *a2mp_channel_create(struct l2cap_conn *conn,
757 mgr = amp_mgr_create(conn);
759 BT_ERR("Could not create AMP manager");
763 BT_DBG("mgr: %p chan %p", mgr, mgr->a2mp_chan);
765 return mgr->a2mp_chan;
768 struct amp_mgr *amp_mgr_lookup_by_state(u8 state)
772 mutex_lock(&_mgr_list_lock);
773 list_for_each_entry(mgr, &_mgr_list, list) {
774 if (mgr->state == state) {
776 mutex_unlock(&_mgr_list_lock);
780 mutex_unlock(&_mgr_list_lock);
785 void a2mp_send_getinfo_rsp(struct hci_dev *hdev)
788 struct a2mp_info_rsp rsp;
790 mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_INFO);
794 BT_DBG("%s mgr %p", hdev->name, mgr);
797 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
799 if (hdev->amp_type != HCI_BREDR) {
801 rsp.total_bw = cpu_to_le32(hdev->amp_total_bw);
802 rsp.max_bw = cpu_to_le32(hdev->amp_max_bw);
803 rsp.min_latency = cpu_to_le32(hdev->amp_min_latency);
804 rsp.pal_cap = cpu_to_le16(hdev->amp_pal_cap);
805 rsp.assoc_size = cpu_to_le16(hdev->amp_assoc_size);
808 a2mp_send(mgr, A2MP_GETINFO_RSP, mgr->ident, sizeof(rsp), &rsp);
812 void a2mp_send_getampassoc_rsp(struct hci_dev *hdev, u8 status)
815 struct amp_assoc *loc_assoc = &hdev->loc_assoc;
816 struct a2mp_amp_assoc_rsp *rsp;
819 mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC);
823 BT_DBG("%s mgr %p", hdev->name, mgr);
825 len = sizeof(struct a2mp_amp_assoc_rsp) + loc_assoc->len;
826 rsp = kzalloc(len, GFP_KERNEL);
835 rsp->status = A2MP_STATUS_INVALID_CTRL_ID;
837 rsp->status = A2MP_STATUS_SUCCESS;
838 memcpy(rsp->amp_assoc, loc_assoc->data, loc_assoc->len);
841 a2mp_send(mgr, A2MP_GETAMPASSOC_RSP, mgr->ident, len, rsp);