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