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