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