Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 1 | /* |
| 2 | Copyright (c) 2010,2011 Code Aurora Forum. All rights reserved. |
| 3 | Copyright (c) 2011,2012 Intel Corp. |
| 4 | |
| 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. |
| 8 | |
| 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. |
| 13 | */ |
| 14 | |
| 15 | #include <net/bluetooth/bluetooth.h> |
| 16 | #include <net/bluetooth/hci_core.h> |
| 17 | #include <net/bluetooth/l2cap.h> |
Marcel Holtmann | 7ef9fbf | 2013-10-10 14:54:14 -0700 | [diff] [blame] | 18 | |
Marcel Holtmann | 7024728 | 2013-10-10 14:54:15 -0700 | [diff] [blame] | 19 | #include "a2mp.h" |
Marcel Holtmann | 7ef9fbf | 2013-10-10 14:54:14 -0700 | [diff] [blame] | 20 | #include "amp.h" |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 21 | |
Andrei Emeltchenko | f97268f | 2012-09-27 17:26:07 +0300 | [diff] [blame] | 22 | /* Global AMP Manager list */ |
| 23 | LIST_HEAD(amp_mgr_list); |
| 24 | DEFINE_MUTEX(amp_mgr_list_lock); |
| 25 | |
Andrei Emeltchenko | f6d3c6e | 2012-05-29 13:59:03 +0300 | [diff] [blame] | 26 | /* A2MP build & send command helper functions */ |
| 27 | static struct a2mp_cmd *__a2mp_build(u8 code, u8 ident, u16 len, void *data) |
| 28 | { |
| 29 | struct a2mp_cmd *cmd; |
| 30 | int plen; |
| 31 | |
| 32 | plen = sizeof(*cmd) + len; |
| 33 | cmd = kzalloc(plen, GFP_KERNEL); |
| 34 | if (!cmd) |
| 35 | return NULL; |
| 36 | |
| 37 | cmd->code = code; |
| 38 | cmd->ident = ident; |
| 39 | cmd->len = cpu_to_le16(len); |
| 40 | |
| 41 | memcpy(cmd->data, data, len); |
| 42 | |
| 43 | return cmd; |
| 44 | } |
| 45 | |
Andrei Emeltchenko | 8e2a0d9 | 2012-09-27 17:26:08 +0300 | [diff] [blame] | 46 | void a2mp_send(struct amp_mgr *mgr, u8 code, u8 ident, u16 len, void *data) |
Andrei Emeltchenko | f6d3c6e | 2012-05-29 13:59:03 +0300 | [diff] [blame] | 47 | { |
| 48 | struct l2cap_chan *chan = mgr->a2mp_chan; |
| 49 | struct a2mp_cmd *cmd; |
| 50 | u16 total_len = len + sizeof(*cmd); |
| 51 | struct kvec iv; |
| 52 | struct msghdr msg; |
| 53 | |
| 54 | cmd = __a2mp_build(code, ident, len, data); |
| 55 | if (!cmd) |
| 56 | return; |
| 57 | |
| 58 | iv.iov_base = cmd; |
| 59 | iv.iov_len = total_len; |
| 60 | |
| 61 | memset(&msg, 0, sizeof(msg)); |
| 62 | |
Al Viro | 1783639 | 2014-11-24 17:07:38 -0500 | [diff] [blame] | 63 | iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, &iv, 1, total_len); |
Andrei Emeltchenko | f6d3c6e | 2012-05-29 13:59:03 +0300 | [diff] [blame] | 64 | |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 65 | l2cap_chan_send(chan, &msg, total_len); |
Andrei Emeltchenko | f6d3c6e | 2012-05-29 13:59:03 +0300 | [diff] [blame] | 66 | |
| 67 | kfree(cmd); |
| 68 | } |
| 69 | |
Andrei Emeltchenko | 9495b2e | 2012-09-27 17:26:22 +0300 | [diff] [blame] | 70 | u8 __next_ident(struct amp_mgr *mgr) |
Andrei Emeltchenko | aa09537 | 2012-09-27 17:26:10 +0300 | [diff] [blame] | 71 | { |
| 72 | if (++mgr->ident == 0) |
| 73 | mgr->ident = 1; |
| 74 | |
| 75 | return mgr->ident; |
| 76 | } |
| 77 | |
Andrei Emeltchenko | 8598d06 | 2012-05-29 13:59:09 +0300 | [diff] [blame] | 78 | /* hci_dev_list shall be locked */ |
Marcel Holtmann | 23f0cb4 | 2013-10-05 11:47:47 -0700 | [diff] [blame] | 79 | static void __a2mp_add_cl(struct amp_mgr *mgr, struct a2mp_cl *cl) |
Andrei Emeltchenko | 8598d06 | 2012-05-29 13:59:09 +0300 | [diff] [blame] | 80 | { |
Andrei Emeltchenko | 8598d06 | 2012-05-29 13:59:09 +0300 | [diff] [blame] | 81 | struct hci_dev *hdev; |
Marcel Holtmann | e880353 | 2013-10-06 02:08:35 -0700 | [diff] [blame] | 82 | int i = 1; |
Andrei Emeltchenko | 8598d06 | 2012-05-29 13:59:09 +0300 | [diff] [blame] | 83 | |
Marcel Holtmann | 346e709 | 2013-10-05 11:47:46 -0700 | [diff] [blame] | 84 | cl[0].id = AMP_ID_BREDR; |
| 85 | cl[0].type = AMP_TYPE_BREDR; |
| 86 | cl[0].status = AMP_STATUS_BLUETOOTH_ONLY; |
Andrei Emeltchenko | 8598d06 | 2012-05-29 13:59:09 +0300 | [diff] [blame] | 87 | |
| 88 | list_for_each_entry(hdev, &hci_dev_list, list) { |
Marcel Holtmann | e880353 | 2013-10-06 02:08:35 -0700 | [diff] [blame] | 89 | if (hdev->dev_type == HCI_AMP) { |
| 90 | cl[i].id = hdev->id; |
| 91 | cl[i].type = hdev->amp_type; |
Marcel Holtmann | cd0a85c | 2013-10-07 00:58:34 -0700 | [diff] [blame] | 92 | if (test_bit(HCI_UP, &hdev->flags)) |
| 93 | cl[i].status = hdev->amp_status; |
| 94 | else |
| 95 | cl[i].status = AMP_STATUS_POWERED_DOWN; |
Marcel Holtmann | e880353 | 2013-10-06 02:08:35 -0700 | [diff] [blame] | 96 | i++; |
| 97 | } |
Andrei Emeltchenko | 8598d06 | 2012-05-29 13:59:09 +0300 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Andrei Emeltchenko | 21dbd2c | 2012-05-29 13:59:08 +0300 | [diff] [blame] | 101 | /* Processing A2MP messages */ |
| 102 | static int a2mp_command_rej(struct amp_mgr *mgr, struct sk_buff *skb, |
| 103 | struct a2mp_cmd *hdr) |
| 104 | { |
| 105 | struct a2mp_cmd_rej *rej = (void *) skb->data; |
| 106 | |
| 107 | if (le16_to_cpu(hdr->len) < sizeof(*rej)) |
| 108 | return -EINVAL; |
| 109 | |
| 110 | BT_DBG("ident %d reason %d", hdr->ident, le16_to_cpu(rej->reason)); |
| 111 | |
| 112 | skb_pull(skb, sizeof(*rej)); |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
Andrei Emeltchenko | 8598d06 | 2012-05-29 13:59:09 +0300 | [diff] [blame] | 117 | static int a2mp_discover_req(struct amp_mgr *mgr, struct sk_buff *skb, |
| 118 | struct a2mp_cmd *hdr) |
| 119 | { |
| 120 | struct a2mp_discov_req *req = (void *) skb->data; |
| 121 | u16 len = le16_to_cpu(hdr->len); |
| 122 | struct a2mp_discov_rsp *rsp; |
| 123 | u16 ext_feat; |
| 124 | u8 num_ctrl; |
Marcel Holtmann | f822c41 | 2013-10-05 11:47:41 -0700 | [diff] [blame] | 125 | struct hci_dev *hdev; |
Andrei Emeltchenko | 8598d06 | 2012-05-29 13:59:09 +0300 | [diff] [blame] | 126 | |
| 127 | if (len < sizeof(*req)) |
| 128 | return -EINVAL; |
| 129 | |
| 130 | skb_pull(skb, sizeof(*req)); |
| 131 | |
| 132 | ext_feat = le16_to_cpu(req->ext_feat); |
| 133 | |
| 134 | BT_DBG("mtu %d efm 0x%4.4x", le16_to_cpu(req->mtu), ext_feat); |
| 135 | |
| 136 | /* check that packet is not broken for now */ |
| 137 | while (ext_feat & A2MP_FEAT_EXT) { |
| 138 | if (len < sizeof(ext_feat)) |
| 139 | return -EINVAL; |
| 140 | |
| 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)); |
| 145 | } |
| 146 | |
| 147 | read_lock(&hci_dev_list_lock); |
| 148 | |
Marcel Holtmann | f822c41 | 2013-10-05 11:47:41 -0700 | [diff] [blame] | 149 | /* at minimum the BR/EDR needs to be listed */ |
| 150 | num_ctrl = 1; |
| 151 | |
| 152 | list_for_each_entry(hdev, &hci_dev_list, list) { |
| 153 | if (hdev->dev_type == HCI_AMP) |
| 154 | num_ctrl++; |
| 155 | } |
| 156 | |
Andrei Emeltchenko | 8598d06 | 2012-05-29 13:59:09 +0300 | [diff] [blame] | 157 | len = num_ctrl * sizeof(struct a2mp_cl) + sizeof(*rsp); |
| 158 | rsp = kmalloc(len, GFP_ATOMIC); |
| 159 | if (!rsp) { |
| 160 | read_unlock(&hci_dev_list_lock); |
| 161 | return -ENOMEM; |
| 162 | } |
| 163 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 164 | rsp->mtu = cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU); |
Andrei Emeltchenko | 8598d06 | 2012-05-29 13:59:09 +0300 | [diff] [blame] | 165 | rsp->ext_feat = 0; |
| 166 | |
Marcel Holtmann | 23f0cb4 | 2013-10-05 11:47:47 -0700 | [diff] [blame] | 167 | __a2mp_add_cl(mgr, rsp->cl); |
Andrei Emeltchenko | 8598d06 | 2012-05-29 13:59:09 +0300 | [diff] [blame] | 168 | |
| 169 | read_unlock(&hci_dev_list_lock); |
| 170 | |
| 171 | a2mp_send(mgr, A2MP_DISCOVER_RSP, hdr->ident, len, rsp); |
| 172 | |
| 173 | kfree(rsp); |
| 174 | return 0; |
| 175 | } |
| 176 | |
Andrei Emeltchenko | aa09537 | 2012-09-27 17:26:10 +0300 | [diff] [blame] | 177 | static int a2mp_discover_rsp(struct amp_mgr *mgr, struct sk_buff *skb, |
| 178 | struct a2mp_cmd *hdr) |
| 179 | { |
| 180 | struct a2mp_discov_rsp *rsp = (void *) skb->data; |
| 181 | u16 len = le16_to_cpu(hdr->len); |
| 182 | struct a2mp_cl *cl; |
| 183 | u16 ext_feat; |
Andrei Emeltchenko | 2766be4 | 2012-09-27 17:26:21 +0300 | [diff] [blame] | 184 | bool found = false; |
Andrei Emeltchenko | aa09537 | 2012-09-27 17:26:10 +0300 | [diff] [blame] | 185 | |
| 186 | if (len < sizeof(*rsp)) |
| 187 | return -EINVAL; |
| 188 | |
| 189 | len -= sizeof(*rsp); |
| 190 | skb_pull(skb, sizeof(*rsp)); |
| 191 | |
| 192 | ext_feat = le16_to_cpu(rsp->ext_feat); |
| 193 | |
| 194 | BT_DBG("mtu %d efm 0x%4.4x", le16_to_cpu(rsp->mtu), ext_feat); |
| 195 | |
| 196 | /* check that packet is not broken for now */ |
| 197 | while (ext_feat & A2MP_FEAT_EXT) { |
| 198 | if (len < sizeof(ext_feat)) |
| 199 | return -EINVAL; |
| 200 | |
| 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)); |
| 205 | } |
| 206 | |
| 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, |
| 210 | cl->status); |
| 211 | |
Marcel Holtmann | a646bd8 | 2013-10-05 13:57:53 -0700 | [diff] [blame] | 212 | if (cl->id != AMP_ID_BREDR && cl->type != AMP_TYPE_BREDR) { |
Andrei Emeltchenko | aa09537 | 2012-09-27 17:26:10 +0300 | [diff] [blame] | 213 | struct a2mp_info_req req; |
| 214 | |
Andrei Emeltchenko | 2766be4 | 2012-09-27 17:26:21 +0300 | [diff] [blame] | 215 | found = true; |
Andrei Emeltchenko | aa09537 | 2012-09-27 17:26:10 +0300 | [diff] [blame] | 216 | req.id = cl->id; |
| 217 | a2mp_send(mgr, A2MP_GETINFO_REQ, __next_ident(mgr), |
| 218 | sizeof(req), &req); |
| 219 | } |
| 220 | |
| 221 | len -= sizeof(*cl); |
| 222 | cl = (void *) skb_pull(skb, sizeof(*cl)); |
| 223 | } |
| 224 | |
Andrei Emeltchenko | 2766be4 | 2012-09-27 17:26:21 +0300 | [diff] [blame] | 225 | /* Fall back to L2CAP init sequence */ |
| 226 | if (!found) { |
| 227 | struct l2cap_conn *conn = mgr->l2cap_conn; |
| 228 | struct l2cap_chan *chan; |
| 229 | |
| 230 | mutex_lock(&conn->chan_lock); |
| 231 | |
| 232 | list_for_each_entry(chan, &conn->chan_l, list) { |
| 233 | |
| 234 | BT_DBG("chan %p state %s", chan, |
| 235 | state_to_string(chan->state)); |
| 236 | |
Johan Hedberg | 2338a7e | 2014-01-24 10:35:40 +0200 | [diff] [blame] | 237 | if (chan->scid == L2CAP_CID_A2MP) |
Andrei Emeltchenko | 2766be4 | 2012-09-27 17:26:21 +0300 | [diff] [blame] | 238 | continue; |
| 239 | |
| 240 | l2cap_chan_lock(chan); |
| 241 | |
| 242 | if (chan->state == BT_CONNECT) |
| 243 | l2cap_send_conn_req(chan); |
| 244 | |
| 245 | l2cap_chan_unlock(chan); |
| 246 | } |
| 247 | |
| 248 | mutex_unlock(&conn->chan_lock); |
| 249 | } |
| 250 | |
Andrei Emeltchenko | aa09537 | 2012-09-27 17:26:10 +0300 | [diff] [blame] | 251 | return 0; |
| 252 | } |
| 253 | |
Andrei Emeltchenko | 329d81af | 2012-05-29 13:59:10 +0300 | [diff] [blame] | 254 | static int a2mp_change_notify(struct amp_mgr *mgr, struct sk_buff *skb, |
| 255 | struct a2mp_cmd *hdr) |
| 256 | { |
| 257 | struct a2mp_cl *cl = (void *) skb->data; |
| 258 | |
| 259 | while (skb->len >= sizeof(*cl)) { |
| 260 | BT_DBG("Controller id %d type %d status %d", cl->id, cl->type, |
| 261 | cl->status); |
| 262 | cl = (struct a2mp_cl *) skb_pull(skb, sizeof(*cl)); |
| 263 | } |
| 264 | |
| 265 | /* TODO send A2MP_CHANGE_RSP */ |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
Andrei Emeltchenko | 47f2d97 | 2012-05-29 13:59:11 +0300 | [diff] [blame] | 270 | static int a2mp_getinfo_req(struct amp_mgr *mgr, struct sk_buff *skb, |
| 271 | struct a2mp_cmd *hdr) |
| 272 | { |
| 273 | struct a2mp_info_req *req = (void *) skb->data; |
Andrei Emeltchenko | 47f2d97 | 2012-05-29 13:59:11 +0300 | [diff] [blame] | 274 | struct hci_dev *hdev; |
| 275 | |
| 276 | if (le16_to_cpu(hdr->len) < sizeof(*req)) |
| 277 | return -EINVAL; |
| 278 | |
| 279 | BT_DBG("id %d", req->id); |
| 280 | |
Andrei Emeltchenko | 47f2d97 | 2012-05-29 13:59:11 +0300 | [diff] [blame] | 281 | hdev = hci_dev_get(req->id); |
Andrei Emeltchenko | bc8dce4 | 2012-09-28 14:28:50 +0300 | [diff] [blame] | 282 | if (!hdev || hdev->dev_type != HCI_AMP) { |
Andrei Emeltchenko | 8e2a0d9 | 2012-09-27 17:26:08 +0300 | [diff] [blame] | 283 | struct a2mp_info_rsp rsp; |
| 284 | |
| 285 | rsp.id = req->id; |
| 286 | rsp.status = A2MP_STATUS_INVALID_CTRL_ID; |
| 287 | |
| 288 | a2mp_send(mgr, A2MP_GETINFO_RSP, hdr->ident, sizeof(rsp), |
| 289 | &rsp); |
Andrei Emeltchenko | bc8dce4 | 2012-09-28 14:28:50 +0300 | [diff] [blame] | 290 | |
| 291 | goto done; |
Andrei Emeltchenko | 47f2d97 | 2012-05-29 13:59:11 +0300 | [diff] [blame] | 292 | } |
| 293 | |
Andrei Emeltchenko | cb6801c6 | 2012-12-07 14:59:08 +0200 | [diff] [blame] | 294 | set_bit(READ_LOC_AMP_INFO, &mgr->state); |
Andrei Emeltchenko | bc8dce4 | 2012-09-28 14:28:50 +0300 | [diff] [blame] | 295 | hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL); |
Andrei Emeltchenko | 47f2d97 | 2012-05-29 13:59:11 +0300 | [diff] [blame] | 296 | |
Andrei Emeltchenko | bc8dce4 | 2012-09-28 14:28:50 +0300 | [diff] [blame] | 297 | done: |
| 298 | if (hdev) |
| 299 | hci_dev_put(hdev); |
Andrei Emeltchenko | 47f2d97 | 2012-05-29 13:59:11 +0300 | [diff] [blame] | 300 | |
| 301 | skb_pull(skb, sizeof(*req)); |
| 302 | return 0; |
| 303 | } |
| 304 | |
Andrei Emeltchenko | 0d868de | 2012-09-27 17:26:14 +0300 | [diff] [blame] | 305 | static int a2mp_getinfo_rsp(struct amp_mgr *mgr, struct sk_buff *skb, |
| 306 | struct a2mp_cmd *hdr) |
| 307 | { |
| 308 | struct a2mp_info_rsp *rsp = (struct a2mp_info_rsp *) skb->data; |
| 309 | struct a2mp_amp_assoc_req req; |
| 310 | struct amp_ctrl *ctrl; |
| 311 | |
| 312 | if (le16_to_cpu(hdr->len) < sizeof(*rsp)) |
| 313 | return -EINVAL; |
| 314 | |
| 315 | BT_DBG("id %d status 0x%2.2x", rsp->id, rsp->status); |
| 316 | |
| 317 | if (rsp->status) |
| 318 | return -EINVAL; |
| 319 | |
Andrei Emeltchenko | fa4ebc6 | 2012-10-05 16:56:55 +0300 | [diff] [blame] | 320 | ctrl = amp_ctrl_add(mgr, rsp->id); |
Andrei Emeltchenko | 0d868de | 2012-09-27 17:26:14 +0300 | [diff] [blame] | 321 | if (!ctrl) |
| 322 | return -ENOMEM; |
| 323 | |
Andrei Emeltchenko | 0d868de | 2012-09-27 17:26:14 +0300 | [diff] [blame] | 324 | req.id = rsp->id; |
| 325 | a2mp_send(mgr, A2MP_GETAMPASSOC_REQ, __next_ident(mgr), sizeof(req), |
| 326 | &req); |
| 327 | |
| 328 | skb_pull(skb, sizeof(*rsp)); |
| 329 | return 0; |
| 330 | } |
| 331 | |
Andrei Emeltchenko | a28381dc | 2012-05-29 13:59:12 +0300 | [diff] [blame] | 332 | static int a2mp_getampassoc_req(struct amp_mgr *mgr, struct sk_buff *skb, |
| 333 | struct a2mp_cmd *hdr) |
| 334 | { |
| 335 | struct a2mp_amp_assoc_req *req = (void *) skb->data; |
| 336 | struct hci_dev *hdev; |
Andrei Emeltchenko | 903e454 | 2012-09-27 17:26:09 +0300 | [diff] [blame] | 337 | struct amp_mgr *tmp; |
Andrei Emeltchenko | a28381dc | 2012-05-29 13:59:12 +0300 | [diff] [blame] | 338 | |
| 339 | if (le16_to_cpu(hdr->len) < sizeof(*req)) |
| 340 | return -EINVAL; |
| 341 | |
| 342 | BT_DBG("id %d", req->id); |
| 343 | |
Andrei Emeltchenko | 903e454 | 2012-09-27 17:26:09 +0300 | [diff] [blame] | 344 | /* Make sure that other request is not processed */ |
| 345 | tmp = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC); |
| 346 | |
Andrei Emeltchenko | a28381dc | 2012-05-29 13:59:12 +0300 | [diff] [blame] | 347 | hdev = hci_dev_get(req->id); |
Marcel Holtmann | ece6912 | 2013-10-05 11:47:43 -0700 | [diff] [blame] | 348 | if (!hdev || hdev->amp_type == AMP_TYPE_BREDR || tmp) { |
Andrei Emeltchenko | a28381dc | 2012-05-29 13:59:12 +0300 | [diff] [blame] | 349 | struct a2mp_amp_assoc_rsp rsp; |
| 350 | rsp.id = req->id; |
Andrei Emeltchenko | 903e454 | 2012-09-27 17:26:09 +0300 | [diff] [blame] | 351 | |
| 352 | if (tmp) { |
| 353 | rsp.status = A2MP_STATUS_COLLISION_OCCURED; |
| 354 | amp_mgr_put(tmp); |
| 355 | } else { |
| 356 | rsp.status = A2MP_STATUS_INVALID_CTRL_ID; |
| 357 | } |
Andrei Emeltchenko | a28381dc | 2012-05-29 13:59:12 +0300 | [diff] [blame] | 358 | |
| 359 | a2mp_send(mgr, A2MP_GETAMPASSOC_RSP, hdr->ident, sizeof(rsp), |
| 360 | &rsp); |
Andrei Emeltchenko | 903e454 | 2012-09-27 17:26:09 +0300 | [diff] [blame] | 361 | |
| 362 | goto done; |
Andrei Emeltchenko | a28381dc | 2012-05-29 13:59:12 +0300 | [diff] [blame] | 363 | } |
| 364 | |
Andrei Emeltchenko | 903e454 | 2012-09-27 17:26:09 +0300 | [diff] [blame] | 365 | amp_read_loc_assoc(hdev, mgr); |
Andrei Emeltchenko | a28381dc | 2012-05-29 13:59:12 +0300 | [diff] [blame] | 366 | |
Andrei Emeltchenko | 903e454 | 2012-09-27 17:26:09 +0300 | [diff] [blame] | 367 | done: |
Andrei Emeltchenko | a28381dc | 2012-05-29 13:59:12 +0300 | [diff] [blame] | 368 | if (hdev) |
| 369 | hci_dev_put(hdev); |
| 370 | |
| 371 | skb_pull(skb, sizeof(*req)); |
| 372 | return 0; |
| 373 | } |
| 374 | |
Andrei Emeltchenko | 9a5e94d | 2012-09-27 17:26:15 +0300 | [diff] [blame] | 375 | static int a2mp_getampassoc_rsp(struct amp_mgr *mgr, struct sk_buff *skb, |
| 376 | struct a2mp_cmd *hdr) |
| 377 | { |
| 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; |
Andrei Emeltchenko | 13465c0 | 2012-09-28 16:55:00 +0300 | [diff] [blame] | 383 | size_t assoc_len; |
Andrei Emeltchenko | 9a5e94d | 2012-09-27 17:26:15 +0300 | [diff] [blame] | 384 | |
| 385 | if (len < sizeof(*rsp)) |
| 386 | return -EINVAL; |
| 387 | |
Andrei Emeltchenko | 13465c0 | 2012-09-28 16:55:00 +0300 | [diff] [blame] | 388 | assoc_len = len - sizeof(*rsp); |
| 389 | |
| 390 | BT_DBG("id %d status 0x%2.2x assoc len %zu", rsp->id, rsp->status, |
| 391 | assoc_len); |
Andrei Emeltchenko | 9a5e94d | 2012-09-27 17:26:15 +0300 | [diff] [blame] | 392 | |
| 393 | if (rsp->status) |
| 394 | return -EINVAL; |
| 395 | |
| 396 | /* Save remote ASSOC data */ |
| 397 | ctrl = amp_ctrl_lookup(mgr, rsp->id); |
| 398 | if (ctrl) { |
Andrei Emeltchenko | 13465c0 | 2012-09-28 16:55:00 +0300 | [diff] [blame] | 399 | u8 *assoc; |
Andrei Emeltchenko | 9a5e94d | 2012-09-27 17:26:15 +0300 | [diff] [blame] | 400 | |
Alexandru Gheorghiu | 5ae327f | 2013-03-17 07:16:50 +0200 | [diff] [blame] | 401 | assoc = kmemdup(rsp->amp_assoc, assoc_len, GFP_KERNEL); |
Andrei Emeltchenko | 9a5e94d | 2012-09-27 17:26:15 +0300 | [diff] [blame] | 402 | if (!assoc) { |
| 403 | amp_ctrl_put(ctrl); |
| 404 | return -ENOMEM; |
| 405 | } |
| 406 | |
Andrei Emeltchenko | 9a5e94d | 2012-09-27 17:26:15 +0300 | [diff] [blame] | 407 | ctrl->assoc = assoc; |
| 408 | ctrl->assoc_len = assoc_len; |
| 409 | ctrl->assoc_rem_len = assoc_len; |
| 410 | ctrl->assoc_len_so_far = 0; |
| 411 | |
| 412 | amp_ctrl_put(ctrl); |
| 413 | } |
| 414 | |
| 415 | /* Create Phys Link */ |
| 416 | hdev = hci_dev_get(rsp->id); |
| 417 | if (!hdev) |
| 418 | return -EINVAL; |
| 419 | |
Andrei Emeltchenko | a0c234f | 2012-10-05 16:56:56 +0300 | [diff] [blame] | 420 | hcon = phylink_add(hdev, mgr, rsp->id, true); |
Andrei Emeltchenko | 9a5e94d | 2012-09-27 17:26:15 +0300 | [diff] [blame] | 421 | if (!hcon) |
| 422 | goto done; |
| 423 | |
| 424 | BT_DBG("Created hcon %p: loc:%d -> rem:%d", hcon, hdev->id, rsp->id); |
| 425 | |
Andrei Emeltchenko | fffadc0 | 2012-11-01 15:37:03 +0200 | [diff] [blame] | 426 | mgr->bredr_chan->remote_amp_id = rsp->id; |
Andrei Emeltchenko | 9495b2e | 2012-09-27 17:26:22 +0300 | [diff] [blame] | 427 | |
Andrei Emeltchenko | a02226d | 2012-09-27 17:26:19 +0300 | [diff] [blame] | 428 | amp_create_phylink(hdev, mgr, hcon); |
| 429 | |
Andrei Emeltchenko | 9a5e94d | 2012-09-27 17:26:15 +0300 | [diff] [blame] | 430 | done: |
| 431 | hci_dev_put(hdev); |
| 432 | skb_pull(skb, len); |
| 433 | return 0; |
| 434 | } |
| 435 | |
Andrei Emeltchenko | e072f5d | 2012-05-29 13:59:13 +0300 | [diff] [blame] | 436 | static int a2mp_createphyslink_req(struct amp_mgr *mgr, struct sk_buff *skb, |
| 437 | struct a2mp_cmd *hdr) |
| 438 | { |
| 439 | struct a2mp_physlink_req *req = (void *) skb->data; |
| 440 | |
| 441 | struct a2mp_physlink_rsp rsp; |
| 442 | struct hci_dev *hdev; |
Andrei Emeltchenko | cb8488c | 2012-09-27 17:26:13 +0300 | [diff] [blame] | 443 | struct hci_conn *hcon; |
Andrei Emeltchenko | 0b26ab9 | 2012-09-27 17:26:24 +0300 | [diff] [blame] | 444 | struct amp_ctrl *ctrl; |
Andrei Emeltchenko | e072f5d | 2012-05-29 13:59:13 +0300 | [diff] [blame] | 445 | |
| 446 | if (le16_to_cpu(hdr->len) < sizeof(*req)) |
| 447 | return -EINVAL; |
| 448 | |
| 449 | BT_DBG("local_id %d, remote_id %d", req->local_id, req->remote_id); |
| 450 | |
| 451 | rsp.local_id = req->remote_id; |
| 452 | rsp.remote_id = req->local_id; |
| 453 | |
| 454 | hdev = hci_dev_get(req->remote_id); |
Marcel Holtmann | ece6912 | 2013-10-05 11:47:43 -0700 | [diff] [blame] | 455 | if (!hdev || hdev->amp_type == AMP_TYPE_BREDR) { |
Andrei Emeltchenko | e072f5d | 2012-05-29 13:59:13 +0300 | [diff] [blame] | 456 | rsp.status = A2MP_STATUS_INVALID_CTRL_ID; |
| 457 | goto send_rsp; |
| 458 | } |
| 459 | |
Andrei Emeltchenko | 0b26ab9 | 2012-09-27 17:26:24 +0300 | [diff] [blame] | 460 | ctrl = amp_ctrl_lookup(mgr, rsp.remote_id); |
| 461 | if (!ctrl) { |
Andrei Emeltchenko | fa4ebc6 | 2012-10-05 16:56:55 +0300 | [diff] [blame] | 462 | ctrl = amp_ctrl_add(mgr, rsp.remote_id); |
Andrei Emeltchenko | 0b26ab9 | 2012-09-27 17:26:24 +0300 | [diff] [blame] | 463 | if (ctrl) { |
| 464 | amp_ctrl_get(ctrl); |
| 465 | } else { |
| 466 | rsp.status = A2MP_STATUS_UNABLE_START_LINK_CREATION; |
| 467 | goto send_rsp; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | if (ctrl) { |
Andrei Emeltchenko | 13465c0 | 2012-09-28 16:55:00 +0300 | [diff] [blame] | 472 | size_t assoc_len = le16_to_cpu(hdr->len) - sizeof(*req); |
| 473 | u8 *assoc; |
Andrei Emeltchenko | 0b26ab9 | 2012-09-27 17:26:24 +0300 | [diff] [blame] | 474 | |
Alexandru Gheorghiu | 5ae327f | 2013-03-17 07:16:50 +0200 | [diff] [blame] | 475 | assoc = kmemdup(req->amp_assoc, assoc_len, GFP_KERNEL); |
Andrei Emeltchenko | 0b26ab9 | 2012-09-27 17:26:24 +0300 | [diff] [blame] | 476 | if (!assoc) { |
| 477 | amp_ctrl_put(ctrl); |
| 478 | return -ENOMEM; |
| 479 | } |
| 480 | |
Andrei Emeltchenko | 0b26ab9 | 2012-09-27 17:26:24 +0300 | [diff] [blame] | 481 | ctrl->assoc = assoc; |
| 482 | ctrl->assoc_len = assoc_len; |
| 483 | ctrl->assoc_rem_len = assoc_len; |
| 484 | ctrl->assoc_len_so_far = 0; |
| 485 | |
| 486 | amp_ctrl_put(ctrl); |
| 487 | } |
| 488 | |
Andrei Emeltchenko | a0c234f | 2012-10-05 16:56:56 +0300 | [diff] [blame] | 489 | hcon = phylink_add(hdev, mgr, req->local_id, false); |
Andrei Emeltchenko | cb8488c | 2012-09-27 17:26:13 +0300 | [diff] [blame] | 490 | if (hcon) { |
Andrei Emeltchenko | dffa387 | 2012-09-27 17:26:23 +0300 | [diff] [blame] | 491 | amp_accept_phylink(hdev, mgr, hcon); |
Andrei Emeltchenko | cb8488c | 2012-09-27 17:26:13 +0300 | [diff] [blame] | 492 | rsp.status = A2MP_STATUS_SUCCESS; |
| 493 | } else { |
| 494 | rsp.status = A2MP_STATUS_UNABLE_START_LINK_CREATION; |
| 495 | } |
Andrei Emeltchenko | e072f5d | 2012-05-29 13:59:13 +0300 | [diff] [blame] | 496 | |
| 497 | send_rsp: |
| 498 | if (hdev) |
| 499 | hci_dev_put(hdev); |
| 500 | |
Andrei Emeltchenko | 8e05e3b | 2012-12-07 14:59:05 +0200 | [diff] [blame] | 501 | /* Reply error now and success after HCI Write Remote AMP Assoc |
| 502 | command complete with success status |
| 503 | */ |
| 504 | if (rsp.status != A2MP_STATUS_SUCCESS) { |
| 505 | a2mp_send(mgr, A2MP_CREATEPHYSLINK_RSP, hdr->ident, |
| 506 | sizeof(rsp), &rsp); |
| 507 | } else { |
Andrei Emeltchenko | cb6801c6 | 2012-12-07 14:59:08 +0200 | [diff] [blame] | 508 | set_bit(WRITE_REMOTE_AMP_ASSOC, &mgr->state); |
Andrei Emeltchenko | 8e05e3b | 2012-12-07 14:59:05 +0200 | [diff] [blame] | 509 | mgr->ident = hdr->ident; |
| 510 | } |
Andrei Emeltchenko | e072f5d | 2012-05-29 13:59:13 +0300 | [diff] [blame] | 511 | |
| 512 | skb_pull(skb, le16_to_cpu(hdr->len)); |
| 513 | return 0; |
| 514 | } |
| 515 | |
Andrei Emeltchenko | 6113f84 | 2012-05-29 13:59:14 +0300 | [diff] [blame] | 516 | static int a2mp_discphyslink_req(struct amp_mgr *mgr, struct sk_buff *skb, |
| 517 | struct a2mp_cmd *hdr) |
| 518 | { |
| 519 | struct a2mp_physlink_req *req = (void *) skb->data; |
| 520 | struct a2mp_physlink_rsp rsp; |
| 521 | struct hci_dev *hdev; |
Andrei Emeltchenko | cb8488c | 2012-09-27 17:26:13 +0300 | [diff] [blame] | 522 | struct hci_conn *hcon; |
Andrei Emeltchenko | 6113f84 | 2012-05-29 13:59:14 +0300 | [diff] [blame] | 523 | |
| 524 | if (le16_to_cpu(hdr->len) < sizeof(*req)) |
| 525 | return -EINVAL; |
| 526 | |
| 527 | BT_DBG("local_id %d remote_id %d", req->local_id, req->remote_id); |
| 528 | |
| 529 | rsp.local_id = req->remote_id; |
| 530 | rsp.remote_id = req->local_id; |
| 531 | rsp.status = A2MP_STATUS_SUCCESS; |
| 532 | |
Andrei Emeltchenko | cb8488c | 2012-09-27 17:26:13 +0300 | [diff] [blame] | 533 | hdev = hci_dev_get(req->remote_id); |
Andrei Emeltchenko | 6113f84 | 2012-05-29 13:59:14 +0300 | [diff] [blame] | 534 | if (!hdev) { |
| 535 | rsp.status = A2MP_STATUS_INVALID_CTRL_ID; |
| 536 | goto send_rsp; |
| 537 | } |
| 538 | |
Marcel Holtmann | bdc8ead2 | 2013-10-13 02:23:38 -0700 | [diff] [blame] | 539 | hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK, |
| 540 | &mgr->l2cap_conn->hcon->dst); |
Andrei Emeltchenko | cb8488c | 2012-09-27 17:26:13 +0300 | [diff] [blame] | 541 | if (!hcon) { |
| 542 | BT_ERR("No phys link exist"); |
| 543 | rsp.status = A2MP_STATUS_NO_PHYSICAL_LINK_EXISTS; |
| 544 | goto clean; |
| 545 | } |
| 546 | |
Andrei Emeltchenko | 6113f84 | 2012-05-29 13:59:14 +0300 | [diff] [blame] | 547 | /* TODO Disconnect Phys Link here */ |
| 548 | |
Andrei Emeltchenko | cb8488c | 2012-09-27 17:26:13 +0300 | [diff] [blame] | 549 | clean: |
Andrei Emeltchenko | 6113f84 | 2012-05-29 13:59:14 +0300 | [diff] [blame] | 550 | hci_dev_put(hdev); |
| 551 | |
| 552 | send_rsp: |
| 553 | a2mp_send(mgr, A2MP_DISCONNPHYSLINK_RSP, hdr->ident, sizeof(rsp), &rsp); |
| 554 | |
| 555 | skb_pull(skb, sizeof(*req)); |
| 556 | return 0; |
| 557 | } |
| 558 | |
Andrei Emeltchenko | f6410a8 | 2012-05-29 13:59:15 +0300 | [diff] [blame] | 559 | static inline int a2mp_cmd_rsp(struct amp_mgr *mgr, struct sk_buff *skb, |
| 560 | struct a2mp_cmd *hdr) |
| 561 | { |
Andrei Emeltchenko | 8e8c7e3 | 2012-07-19 17:03:41 +0300 | [diff] [blame] | 562 | BT_DBG("ident %d code 0x%2.2x", hdr->ident, hdr->code); |
Andrei Emeltchenko | f6410a8 | 2012-05-29 13:59:15 +0300 | [diff] [blame] | 563 | |
| 564 | skb_pull(skb, le16_to_cpu(hdr->len)); |
| 565 | return 0; |
| 566 | } |
| 567 | |
Andrei Emeltchenko | 6b44d9b | 2012-05-29 13:59:07 +0300 | [diff] [blame] | 568 | /* Handle A2MP signalling */ |
| 569 | static int a2mp_chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb) |
| 570 | { |
Andrei Emeltchenko | d9fc1d5 | 2012-07-19 17:03:46 +0300 | [diff] [blame] | 571 | struct a2mp_cmd *hdr; |
Andrei Emeltchenko | 6b44d9b | 2012-05-29 13:59:07 +0300 | [diff] [blame] | 572 | struct amp_mgr *mgr = chan->data; |
| 573 | int err = 0; |
| 574 | |
| 575 | amp_mgr_get(mgr); |
| 576 | |
| 577 | while (skb->len >= sizeof(*hdr)) { |
Andrei Emeltchenko | d9fc1d5 | 2012-07-19 17:03:46 +0300 | [diff] [blame] | 578 | u16 len; |
| 579 | |
| 580 | hdr = (void *) skb->data; |
| 581 | len = le16_to_cpu(hdr->len); |
Andrei Emeltchenko | 6b44d9b | 2012-05-29 13:59:07 +0300 | [diff] [blame] | 582 | |
Andrei Emeltchenko | 8e8c7e3 | 2012-07-19 17:03:41 +0300 | [diff] [blame] | 583 | BT_DBG("code 0x%2.2x id %d len %u", hdr->code, hdr->ident, len); |
Andrei Emeltchenko | 6b44d9b | 2012-05-29 13:59:07 +0300 | [diff] [blame] | 584 | |
| 585 | skb_pull(skb, sizeof(*hdr)); |
| 586 | |
| 587 | if (len > skb->len || !hdr->ident) { |
| 588 | err = -EINVAL; |
| 589 | break; |
| 590 | } |
| 591 | |
| 592 | mgr->ident = hdr->ident; |
| 593 | |
| 594 | switch (hdr->code) { |
| 595 | case A2MP_COMMAND_REJ: |
Andrei Emeltchenko | 21dbd2c | 2012-05-29 13:59:08 +0300 | [diff] [blame] | 596 | a2mp_command_rej(mgr, skb, hdr); |
| 597 | break; |
| 598 | |
Andrei Emeltchenko | 6b44d9b | 2012-05-29 13:59:07 +0300 | [diff] [blame] | 599 | case A2MP_DISCOVER_REQ: |
Andrei Emeltchenko | 8598d06 | 2012-05-29 13:59:09 +0300 | [diff] [blame] | 600 | err = a2mp_discover_req(mgr, skb, hdr); |
| 601 | break; |
| 602 | |
Andrei Emeltchenko | 6b44d9b | 2012-05-29 13:59:07 +0300 | [diff] [blame] | 603 | case A2MP_CHANGE_NOTIFY: |
Andrei Emeltchenko | 329d81af | 2012-05-29 13:59:10 +0300 | [diff] [blame] | 604 | err = a2mp_change_notify(mgr, skb, hdr); |
| 605 | break; |
| 606 | |
Andrei Emeltchenko | 6b44d9b | 2012-05-29 13:59:07 +0300 | [diff] [blame] | 607 | case A2MP_GETINFO_REQ: |
Andrei Emeltchenko | 47f2d97 | 2012-05-29 13:59:11 +0300 | [diff] [blame] | 608 | err = a2mp_getinfo_req(mgr, skb, hdr); |
| 609 | break; |
| 610 | |
Andrei Emeltchenko | 6b44d9b | 2012-05-29 13:59:07 +0300 | [diff] [blame] | 611 | case A2MP_GETAMPASSOC_REQ: |
Andrei Emeltchenko | a28381dc | 2012-05-29 13:59:12 +0300 | [diff] [blame] | 612 | err = a2mp_getampassoc_req(mgr, skb, hdr); |
| 613 | break; |
| 614 | |
Andrei Emeltchenko | 6b44d9b | 2012-05-29 13:59:07 +0300 | [diff] [blame] | 615 | case A2MP_CREATEPHYSLINK_REQ: |
Andrei Emeltchenko | e072f5d | 2012-05-29 13:59:13 +0300 | [diff] [blame] | 616 | err = a2mp_createphyslink_req(mgr, skb, hdr); |
| 617 | break; |
| 618 | |
Andrei Emeltchenko | 6b44d9b | 2012-05-29 13:59:07 +0300 | [diff] [blame] | 619 | case A2MP_DISCONNPHYSLINK_REQ: |
Andrei Emeltchenko | 6113f84 | 2012-05-29 13:59:14 +0300 | [diff] [blame] | 620 | err = a2mp_discphyslink_req(mgr, skb, hdr); |
| 621 | break; |
| 622 | |
Andrei Emeltchenko | 6b44d9b | 2012-05-29 13:59:07 +0300 | [diff] [blame] | 623 | case A2MP_DISCOVER_RSP: |
Andrei Emeltchenko | aa09537 | 2012-09-27 17:26:10 +0300 | [diff] [blame] | 624 | err = a2mp_discover_rsp(mgr, skb, hdr); |
| 625 | break; |
| 626 | |
Andrei Emeltchenko | 6b44d9b | 2012-05-29 13:59:07 +0300 | [diff] [blame] | 627 | case A2MP_GETINFO_RSP: |
Andrei Emeltchenko | 0d868de | 2012-09-27 17:26:14 +0300 | [diff] [blame] | 628 | err = a2mp_getinfo_rsp(mgr, skb, hdr); |
| 629 | break; |
| 630 | |
Andrei Emeltchenko | 6b44d9b | 2012-05-29 13:59:07 +0300 | [diff] [blame] | 631 | case A2MP_GETAMPASSOC_RSP: |
Andrei Emeltchenko | 9a5e94d | 2012-09-27 17:26:15 +0300 | [diff] [blame] | 632 | err = a2mp_getampassoc_rsp(mgr, skb, hdr); |
| 633 | break; |
| 634 | |
| 635 | case A2MP_CHANGE_RSP: |
Andrei Emeltchenko | 6b44d9b | 2012-05-29 13:59:07 +0300 | [diff] [blame] | 636 | case A2MP_CREATEPHYSLINK_RSP: |
| 637 | case A2MP_DISCONNPHYSLINK_RSP: |
Andrei Emeltchenko | f6410a8 | 2012-05-29 13:59:15 +0300 | [diff] [blame] | 638 | err = a2mp_cmd_rsp(mgr, skb, hdr); |
| 639 | break; |
| 640 | |
Andrei Emeltchenko | 6b44d9b | 2012-05-29 13:59:07 +0300 | [diff] [blame] | 641 | default: |
| 642 | BT_ERR("Unknown A2MP sig cmd 0x%2.2x", hdr->code); |
| 643 | err = -EINVAL; |
| 644 | break; |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | if (err) { |
| 649 | struct a2mp_cmd_rej rej; |
Andrei Emeltchenko | d9fc1d5 | 2012-07-19 17:03:46 +0300 | [diff] [blame] | 650 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 651 | rej.reason = cpu_to_le16(0); |
Andrei Emeltchenko | d9fc1d5 | 2012-07-19 17:03:46 +0300 | [diff] [blame] | 652 | hdr = (void *) skb->data; |
Andrei Emeltchenko | 6b44d9b | 2012-05-29 13:59:07 +0300 | [diff] [blame] | 653 | |
| 654 | BT_DBG("Send A2MP Rej: cmd 0x%2.2x err %d", hdr->code, err); |
| 655 | |
| 656 | a2mp_send(mgr, A2MP_COMMAND_REJ, hdr->ident, sizeof(rej), |
| 657 | &rej); |
| 658 | } |
| 659 | |
| 660 | /* Always free skb and return success error code to prevent |
| 661 | from sending L2CAP Disconnect over A2MP channel */ |
| 662 | kfree_skb(skb); |
| 663 | |
| 664 | amp_mgr_put(mgr); |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
Andrei Emeltchenko | 46d5c90 | 2012-05-29 13:59:04 +0300 | [diff] [blame] | 669 | static void a2mp_chan_close_cb(struct l2cap_chan *chan) |
| 670 | { |
Jaganath Kanakkassery | 4af66c6 | 2012-07-13 18:17:55 +0530 | [diff] [blame] | 671 | l2cap_chan_put(chan); |
Andrei Emeltchenko | 46d5c90 | 2012-05-29 13:59:04 +0300 | [diff] [blame] | 672 | } |
| 673 | |
Gustavo Padovan | 53f5212 | 2013-10-15 19:24:45 -0300 | [diff] [blame] | 674 | static void a2mp_chan_state_change_cb(struct l2cap_chan *chan, int state, |
| 675 | int err) |
Andrei Emeltchenko | 46d5c90 | 2012-05-29 13:59:04 +0300 | [diff] [blame] | 676 | { |
| 677 | struct amp_mgr *mgr = chan->data; |
| 678 | |
| 679 | if (!mgr) |
| 680 | return; |
| 681 | |
| 682 | BT_DBG("chan %p state %s", chan, state_to_string(state)); |
| 683 | |
| 684 | chan->state = state; |
| 685 | |
| 686 | switch (state) { |
| 687 | case BT_CLOSED: |
| 688 | if (mgr) |
| 689 | amp_mgr_put(mgr); |
| 690 | break; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | static struct sk_buff *a2mp_chan_alloc_skb_cb(struct l2cap_chan *chan, |
Marcel Holtmann | d9fbd02 | 2014-06-08 11:22:28 +0200 | [diff] [blame] | 695 | unsigned long hdr_len, |
Andrei Emeltchenko | 46d5c90 | 2012-05-29 13:59:04 +0300 | [diff] [blame] | 696 | unsigned long len, int nb) |
| 697 | { |
Gustavo Padovan | 0753c18 | 2012-12-22 01:22:53 -0200 | [diff] [blame] | 698 | struct sk_buff *skb; |
| 699 | |
Marcel Holtmann | d9fbd02 | 2014-06-08 11:22:28 +0200 | [diff] [blame] | 700 | skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL); |
Gustavo Padovan | 0753c18 | 2012-12-22 01:22:53 -0200 | [diff] [blame] | 701 | if (!skb) |
| 702 | return ERR_PTR(-ENOMEM); |
| 703 | |
| 704 | return skb; |
Andrei Emeltchenko | 46d5c90 | 2012-05-29 13:59:04 +0300 | [diff] [blame] | 705 | } |
| 706 | |
Marcel Holtmann | 67f86a4 | 2014-06-08 10:05:31 +0200 | [diff] [blame] | 707 | static const struct l2cap_ops a2mp_chan_ops = { |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 708 | .name = "L2CAP A2MP channel", |
Andrei Emeltchenko | 6b44d9b | 2012-05-29 13:59:07 +0300 | [diff] [blame] | 709 | .recv = a2mp_chan_recv_cb, |
Andrei Emeltchenko | 46d5c90 | 2012-05-29 13:59:04 +0300 | [diff] [blame] | 710 | .close = a2mp_chan_close_cb, |
| 711 | .state_change = a2mp_chan_state_change_cb, |
| 712 | .alloc_skb = a2mp_chan_alloc_skb_cb, |
| 713 | |
| 714 | /* Not implemented for A2MP */ |
Gustavo Padovan | 7e1af8a | 2012-05-29 13:19:26 -0300 | [diff] [blame] | 715 | .new_connection = l2cap_chan_no_new_connection, |
| 716 | .teardown = l2cap_chan_no_teardown, |
| 717 | .ready = l2cap_chan_no_ready, |
Gustavo Padovan | 2dc4e51 | 2012-10-12 19:35:24 +0800 | [diff] [blame] | 718 | .defer = l2cap_chan_no_defer, |
Marcel Holtmann | 2ce5fb5 | 2013-10-15 16:47:11 -0700 | [diff] [blame] | 719 | .resume = l2cap_chan_no_resume, |
Gustavo Padovan | 5ec1bbe | 2013-10-15 19:24:48 -0300 | [diff] [blame] | 720 | .set_shutdown = l2cap_chan_no_set_shutdown, |
Gustavo Padovan | 8d836d7 | 2013-10-15 19:24:47 -0300 | [diff] [blame] | 721 | .get_sndtimeo = l2cap_chan_no_get_sndtimeo, |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 722 | }; |
| 723 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 724 | static struct l2cap_chan *a2mp_chan_open(struct l2cap_conn *conn, bool locked) |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 725 | { |
| 726 | struct l2cap_chan *chan; |
| 727 | int err; |
| 728 | |
| 729 | chan = l2cap_chan_create(); |
| 730 | if (!chan) |
| 731 | return NULL; |
| 732 | |
| 733 | BT_DBG("chan %p", chan); |
| 734 | |
Johan Hedberg | 2338a7e | 2014-01-24 10:35:40 +0200 | [diff] [blame] | 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; |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 740 | chan->flush_to = L2CAP_DEFAULT_FLUSH_TO; |
| 741 | |
| 742 | chan->ops = &a2mp_chan_ops; |
| 743 | |
| 744 | l2cap_chan_set_defaults(chan); |
| 745 | chan->remote_max_tx = chan->max_tx; |
| 746 | chan->remote_tx_win = chan->tx_win; |
| 747 | |
| 748 | chan->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO; |
| 749 | chan->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO; |
| 750 | |
| 751 | skb_queue_head_init(&chan->tx_q); |
| 752 | |
| 753 | chan->mode = L2CAP_MODE_ERTM; |
| 754 | |
| 755 | err = l2cap_ertm_init(chan); |
| 756 | if (err < 0) { |
| 757 | l2cap_chan_del(chan, 0); |
| 758 | return NULL; |
| 759 | } |
| 760 | |
| 761 | chan->conf_state = 0; |
| 762 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 763 | if (locked) |
| 764 | __l2cap_chan_add(conn, chan); |
| 765 | else |
| 766 | l2cap_chan_add(conn, chan); |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 767 | |
| 768 | chan->remote_mps = chan->omtu; |
| 769 | chan->mps = chan->omtu; |
| 770 | |
| 771 | chan->state = BT_CONNECTED; |
| 772 | |
| 773 | return chan; |
| 774 | } |
Andrei Emeltchenko | 9740e49 | 2012-05-29 13:59:02 +0300 | [diff] [blame] | 775 | |
| 776 | /* AMP Manager functions */ |
Andrei Emeltchenko | f706adf | 2012-10-18 13:16:19 +0300 | [diff] [blame] | 777 | struct amp_mgr *amp_mgr_get(struct amp_mgr *mgr) |
Andrei Emeltchenko | 9740e49 | 2012-05-29 13:59:02 +0300 | [diff] [blame] | 778 | { |
Andrei Emeltchenko | a0dfe0a | 2012-07-11 14:43:35 +0300 | [diff] [blame] | 779 | BT_DBG("mgr %p orig refcnt %d", mgr, atomic_read(&mgr->kref.refcount)); |
Andrei Emeltchenko | 9740e49 | 2012-05-29 13:59:02 +0300 | [diff] [blame] | 780 | |
| 781 | kref_get(&mgr->kref); |
Andrei Emeltchenko | f706adf | 2012-10-18 13:16:19 +0300 | [diff] [blame] | 782 | |
| 783 | return mgr; |
Andrei Emeltchenko | 9740e49 | 2012-05-29 13:59:02 +0300 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | static void amp_mgr_destroy(struct kref *kref) |
| 787 | { |
| 788 | struct amp_mgr *mgr = container_of(kref, struct amp_mgr, kref); |
| 789 | |
| 790 | BT_DBG("mgr %p", mgr); |
| 791 | |
Andrei Emeltchenko | f97268f | 2012-09-27 17:26:07 +0300 | [diff] [blame] | 792 | mutex_lock(&_mgr_list_lock); |
| 793 | list_del(&mgr->list); |
| 794 | mutex_unlock(&_mgr_list_lock); |
| 795 | |
Andrei Emeltchenko | 52c0d6e | 2012-09-27 17:26:12 +0300 | [diff] [blame] | 796 | amp_ctrl_list_flush(mgr); |
Andrei Emeltchenko | 9740e49 | 2012-05-29 13:59:02 +0300 | [diff] [blame] | 797 | kfree(mgr); |
| 798 | } |
| 799 | |
| 800 | int amp_mgr_put(struct amp_mgr *mgr) |
| 801 | { |
Andrei Emeltchenko | a0dfe0a | 2012-07-11 14:43:35 +0300 | [diff] [blame] | 802 | BT_DBG("mgr %p orig refcnt %d", mgr, atomic_read(&mgr->kref.refcount)); |
Andrei Emeltchenko | 9740e49 | 2012-05-29 13:59:02 +0300 | [diff] [blame] | 803 | |
| 804 | return kref_put(&mgr->kref, &_mgr_destroy); |
| 805 | } |
| 806 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 807 | static struct amp_mgr *amp_mgr_create(struct l2cap_conn *conn, bool locked) |
Andrei Emeltchenko | 9740e49 | 2012-05-29 13:59:02 +0300 | [diff] [blame] | 808 | { |
| 809 | struct amp_mgr *mgr; |
| 810 | struct l2cap_chan *chan; |
| 811 | |
| 812 | mgr = kzalloc(sizeof(*mgr), GFP_KERNEL); |
| 813 | if (!mgr) |
| 814 | return NULL; |
| 815 | |
| 816 | BT_DBG("conn %p mgr %p", conn, mgr); |
| 817 | |
| 818 | mgr->l2cap_conn = conn; |
| 819 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 820 | chan = a2mp_chan_open(conn, locked); |
Andrei Emeltchenko | 9740e49 | 2012-05-29 13:59:02 +0300 | [diff] [blame] | 821 | if (!chan) { |
| 822 | kfree(mgr); |
| 823 | return NULL; |
| 824 | } |
| 825 | |
| 826 | mgr->a2mp_chan = chan; |
| 827 | chan->data = mgr; |
| 828 | |
| 829 | conn->hcon->amp_mgr = mgr; |
| 830 | |
| 831 | kref_init(&mgr->kref); |
| 832 | |
Andrei Emeltchenko | 52c0d6e | 2012-09-27 17:26:12 +0300 | [diff] [blame] | 833 | /* Remote AMP ctrl list initialization */ |
| 834 | INIT_LIST_HEAD(&mgr->amp_ctrls); |
| 835 | mutex_init(&mgr->amp_ctrls_lock); |
| 836 | |
Andrei Emeltchenko | f97268f | 2012-09-27 17:26:07 +0300 | [diff] [blame] | 837 | mutex_lock(&_mgr_list_lock); |
| 838 | list_add(&mgr->list, &_mgr_list); |
| 839 | mutex_unlock(&_mgr_list_lock); |
| 840 | |
Andrei Emeltchenko | 9740e49 | 2012-05-29 13:59:02 +0300 | [diff] [blame] | 841 | return mgr; |
| 842 | } |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 843 | |
| 844 | struct l2cap_chan *a2mp_channel_create(struct l2cap_conn *conn, |
| 845 | struct sk_buff *skb) |
| 846 | { |
| 847 | struct amp_mgr *mgr; |
| 848 | |
Johan Hedberg | 07e307f | 2013-10-16 11:37:00 +0300 | [diff] [blame] | 849 | if (conn->hcon->type != ACL_LINK) |
| 850 | return NULL; |
| 851 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 852 | mgr = amp_mgr_create(conn, false); |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 853 | if (!mgr) { |
| 854 | BT_ERR("Could not create AMP manager"); |
| 855 | return NULL; |
| 856 | } |
| 857 | |
| 858 | BT_DBG("mgr: %p chan %p", mgr, mgr->a2mp_chan); |
| 859 | |
| 860 | return mgr->a2mp_chan; |
| 861 | } |
Andrei Emeltchenko | f97268f | 2012-09-27 17:26:07 +0300 | [diff] [blame] | 862 | |
| 863 | struct amp_mgr *amp_mgr_lookup_by_state(u8 state) |
| 864 | { |
| 865 | struct amp_mgr *mgr; |
| 866 | |
| 867 | mutex_lock(&_mgr_list_lock); |
| 868 | list_for_each_entry(mgr, &_mgr_list, list) { |
Andrei Emeltchenko | cb6801c6 | 2012-12-07 14:59:08 +0200 | [diff] [blame] | 869 | if (test_and_clear_bit(state, &mgr->state)) { |
Andrei Emeltchenko | f97268f | 2012-09-27 17:26:07 +0300 | [diff] [blame] | 870 | amp_mgr_get(mgr); |
| 871 | mutex_unlock(&_mgr_list_lock); |
| 872 | return mgr; |
| 873 | } |
| 874 | } |
| 875 | mutex_unlock(&_mgr_list_lock); |
| 876 | |
| 877 | return NULL; |
| 878 | } |
Andrei Emeltchenko | 8e2a0d9 | 2012-09-27 17:26:08 +0300 | [diff] [blame] | 879 | |
| 880 | void a2mp_send_getinfo_rsp(struct hci_dev *hdev) |
| 881 | { |
| 882 | struct amp_mgr *mgr; |
| 883 | struct a2mp_info_rsp rsp; |
| 884 | |
| 885 | mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_INFO); |
| 886 | if (!mgr) |
| 887 | return; |
| 888 | |
| 889 | BT_DBG("%s mgr %p", hdev->name, mgr); |
| 890 | |
| 891 | rsp.id = hdev->id; |
| 892 | rsp.status = A2MP_STATUS_INVALID_CTRL_ID; |
| 893 | |
Marcel Holtmann | ece6912 | 2013-10-05 11:47:43 -0700 | [diff] [blame] | 894 | if (hdev->amp_type != AMP_TYPE_BREDR) { |
Andrei Emeltchenko | 8e2a0d9 | 2012-09-27 17:26:08 +0300 | [diff] [blame] | 895 | rsp.status = 0; |
| 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); |
| 901 | } |
| 902 | |
| 903 | a2mp_send(mgr, A2MP_GETINFO_RSP, mgr->ident, sizeof(rsp), &rsp); |
| 904 | amp_mgr_put(mgr); |
| 905 | } |
Andrei Emeltchenko | 903e454 | 2012-09-27 17:26:09 +0300 | [diff] [blame] | 906 | |
| 907 | void a2mp_send_getampassoc_rsp(struct hci_dev *hdev, u8 status) |
| 908 | { |
| 909 | struct amp_mgr *mgr; |
| 910 | struct amp_assoc *loc_assoc = &hdev->loc_assoc; |
| 911 | struct a2mp_amp_assoc_rsp *rsp; |
| 912 | size_t len; |
| 913 | |
| 914 | mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC); |
| 915 | if (!mgr) |
| 916 | return; |
| 917 | |
| 918 | BT_DBG("%s mgr %p", hdev->name, mgr); |
| 919 | |
| 920 | len = sizeof(struct a2mp_amp_assoc_rsp) + loc_assoc->len; |
| 921 | rsp = kzalloc(len, GFP_KERNEL); |
| 922 | if (!rsp) { |
| 923 | amp_mgr_put(mgr); |
| 924 | return; |
| 925 | } |
| 926 | |
| 927 | rsp->id = hdev->id; |
| 928 | |
| 929 | if (status) { |
| 930 | rsp->status = A2MP_STATUS_INVALID_CTRL_ID; |
| 931 | } else { |
| 932 | rsp->status = A2MP_STATUS_SUCCESS; |
| 933 | memcpy(rsp->amp_assoc, loc_assoc->data, loc_assoc->len); |
| 934 | } |
| 935 | |
| 936 | a2mp_send(mgr, A2MP_GETAMPASSOC_RSP, mgr->ident, len, rsp); |
| 937 | amp_mgr_put(mgr); |
| 938 | kfree(rsp); |
| 939 | } |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 940 | |
Andrei Emeltchenko | 9495b2e | 2012-09-27 17:26:22 +0300 | [diff] [blame] | 941 | void a2mp_send_create_phy_link_req(struct hci_dev *hdev, u8 status) |
| 942 | { |
| 943 | struct amp_mgr *mgr; |
| 944 | struct amp_assoc *loc_assoc = &hdev->loc_assoc; |
| 945 | struct a2mp_physlink_req *req; |
| 946 | struct l2cap_chan *bredr_chan; |
| 947 | size_t len; |
| 948 | |
| 949 | mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC_FINAL); |
| 950 | if (!mgr) |
| 951 | return; |
| 952 | |
| 953 | len = sizeof(*req) + loc_assoc->len; |
| 954 | |
| 955 | BT_DBG("%s mgr %p assoc_len %zu", hdev->name, mgr, len); |
| 956 | |
| 957 | req = kzalloc(len, GFP_KERNEL); |
| 958 | if (!req) { |
| 959 | amp_mgr_put(mgr); |
| 960 | return; |
| 961 | } |
| 962 | |
| 963 | bredr_chan = mgr->bredr_chan; |
| 964 | if (!bredr_chan) |
| 965 | goto clean; |
| 966 | |
| 967 | req->local_id = hdev->id; |
Andrei Emeltchenko | fffadc0 | 2012-11-01 15:37:03 +0200 | [diff] [blame] | 968 | req->remote_id = bredr_chan->remote_amp_id; |
Andrei Emeltchenko | 9495b2e | 2012-09-27 17:26:22 +0300 | [diff] [blame] | 969 | memcpy(req->amp_assoc, loc_assoc->data, loc_assoc->len); |
| 970 | |
| 971 | a2mp_send(mgr, A2MP_CREATEPHYSLINK_REQ, __next_ident(mgr), len, req); |
| 972 | |
| 973 | clean: |
| 974 | amp_mgr_put(mgr); |
| 975 | kfree(req); |
| 976 | } |
| 977 | |
Andrei Emeltchenko | 8e05e3b | 2012-12-07 14:59:05 +0200 | [diff] [blame] | 978 | void a2mp_send_create_phy_link_rsp(struct hci_dev *hdev, u8 status) |
| 979 | { |
| 980 | struct amp_mgr *mgr; |
| 981 | struct a2mp_physlink_rsp rsp; |
| 982 | struct hci_conn *hs_hcon; |
| 983 | |
| 984 | mgr = amp_mgr_lookup_by_state(WRITE_REMOTE_AMP_ASSOC); |
| 985 | if (!mgr) |
| 986 | return; |
| 987 | |
| 988 | hs_hcon = hci_conn_hash_lookup_state(hdev, AMP_LINK, BT_CONNECT); |
| 989 | if (!hs_hcon) { |
| 990 | rsp.status = A2MP_STATUS_UNABLE_START_LINK_CREATION; |
| 991 | } else { |
| 992 | rsp.remote_id = hs_hcon->remote_id; |
| 993 | rsp.status = A2MP_STATUS_SUCCESS; |
| 994 | } |
| 995 | |
| 996 | BT_DBG("%s mgr %p hs_hcon %p status %u", hdev->name, mgr, hs_hcon, |
| 997 | status); |
| 998 | |
| 999 | rsp.local_id = hdev->id; |
| 1000 | a2mp_send(mgr, A2MP_CREATEPHYSLINK_RSP, mgr->ident, sizeof(rsp), &rsp); |
| 1001 | amp_mgr_put(mgr); |
| 1002 | } |
| 1003 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1004 | void a2mp_discover_amp(struct l2cap_chan *chan) |
| 1005 | { |
| 1006 | struct l2cap_conn *conn = chan->conn; |
| 1007 | struct amp_mgr *mgr = conn->hcon->amp_mgr; |
| 1008 | struct a2mp_discov_req req; |
| 1009 | |
| 1010 | BT_DBG("chan %p conn %p mgr %p", chan, conn, mgr); |
| 1011 | |
| 1012 | if (!mgr) { |
| 1013 | mgr = amp_mgr_create(conn, true); |
| 1014 | if (!mgr) |
| 1015 | return; |
| 1016 | } |
| 1017 | |
| 1018 | mgr->bredr_chan = chan; |
| 1019 | |
| 1020 | req.mtu = cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU); |
| 1021 | req.ext_feat = 0; |
| 1022 | a2mp_send(mgr, A2MP_DISCOVER_REQ, 1, sizeof(req), &req); |
| 1023 | } |