Bluetooth: Make mgmt power down notification for BR/EDR explicit
[deliverable/linux.git] / net / bluetooth / a2mp.c
CommitLineData
466f8004
AE
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>
9740e49d 18#include <net/bluetooth/a2mp.h>
903e4541 19#include <net/bluetooth/amp.h>
466f8004 20
f97268fc
AE
21/* Global AMP Manager list */
22LIST_HEAD(amp_mgr_list);
23DEFINE_MUTEX(amp_mgr_list_lock);
24
f6d3c6e7
AE
25/* A2MP build & send command helper functions */
26static 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
8e2a0d92 45void a2mp_send(struct amp_mgr *mgr, u8 code, u8 ident, u16 len, void *data)
f6d3c6e7
AE
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
9495b2ee 70u8 __next_ident(struct amp_mgr *mgr)
aa09537d
AE
71{
72 if (++mgr->ident == 0)
73 mgr->ident = 1;
74
75 return mgr->ident;
76}
77
8598d064 78/* hci_dev_list shall be locked */
23f0cb41 79static void __a2mp_add_cl(struct amp_mgr *mgr, struct a2mp_cl *cl)
8598d064 80{
8598d064 81 struct hci_dev *hdev;
e8803534 82 int i = 1;
8598d064 83
346e7099
MH
84 cl[0].id = AMP_ID_BREDR;
85 cl[0].type = AMP_TYPE_BREDR;
86 cl[0].status = AMP_STATUS_BLUETOOTH_ONLY;
8598d064
AE
87
88 list_for_each_entry(hdev, &hci_dev_list, list) {
e8803534
MH
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 }
8598d064
AE
95 }
96}
97
21dbd2ce
AE
98/* Processing A2MP messages */
99static 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
8598d064
AE
114static 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;
f822c411 122 struct hci_dev *hdev;
8598d064
AE
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
f822c411
MH
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
8598d064
AE
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
23f0cb41 164 __a2mp_add_cl(mgr, rsp->cl);
8598d064
AE
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
aa09537d
AE
174static 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;
2766be48 181 bool found = false;
aa09537d
AE
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
a646bd81 209 if (cl->id != AMP_ID_BREDR && cl->type != AMP_TYPE_BREDR) {
aa09537d
AE
210 struct a2mp_info_req req;
211
2766be48 212 found = true;
aa09537d
AE
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
2766be48
AE
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
aa09537d
AE
248 return 0;
249}
250
329d81af
AE
251static 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
47f2d97d
AE
267static 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;
47f2d97d
AE
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
47f2d97d 278 hdev = hci_dev_get(req->id);
bc8dce4f 279 if (!hdev || hdev->dev_type != HCI_AMP) {
8e2a0d92
AE
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);
47f2d97d 287
bc8dce4f 288 goto done;
8e2a0d92 289 }
47f2d97d 290
cb6801c6 291 set_bit(READ_LOC_AMP_INFO, &mgr->state);
bc8dce4f
AE
292 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL);
293
294done:
295 if (hdev)
296 hci_dev_put(hdev);
47f2d97d
AE
297
298 skb_pull(skb, sizeof(*req));
299 return 0;
300}
301
0d868de9
AE
302static 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
fa4ebc66 317 ctrl = amp_ctrl_add(mgr, rsp->id);
0d868de9
AE
318 if (!ctrl)
319 return -ENOMEM;
320
0d868de9
AE
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
a28381dc
AE
329static 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;
903e4541 334 struct amp_mgr *tmp;
a28381dc
AE
335
336 if (le16_to_cpu(hdr->len) < sizeof(*req))
337 return -EINVAL;
338
339 BT_DBG("id %d", req->id);
340
903e4541
AE
341 /* Make sure that other request is not processed */
342 tmp = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC);
343
a28381dc 344 hdev = hci_dev_get(req->id);
ece69126 345 if (!hdev || hdev->amp_type == AMP_TYPE_BREDR || tmp) {
a28381dc
AE
346 struct a2mp_amp_assoc_rsp rsp;
347 rsp.id = req->id;
903e4541
AE
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 }
a28381dc
AE
355
356 a2mp_send(mgr, A2MP_GETAMPASSOC_RSP, hdr->ident, sizeof(rsp),
357 &rsp);
903e4541
AE
358
359 goto done;
a28381dc
AE
360 }
361
903e4541 362 amp_read_loc_assoc(hdev, mgr);
a28381dc 363
903e4541 364done:
a28381dc
AE
365 if (hdev)
366 hci_dev_put(hdev);
367
368 skb_pull(skb, sizeof(*req));
369 return 0;
370}
371
9a5e94db
AE
372static 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;
13465c0a 380 size_t assoc_len;
9a5e94db
AE
381
382 if (len < sizeof(*rsp))
383 return -EINVAL;
384
13465c0a
AE
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);
9a5e94db
AE
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) {
13465c0a 396 u8 *assoc;
9a5e94db 397
5ae327f0 398 assoc = kmemdup(rsp->amp_assoc, assoc_len, GFP_KERNEL);
9a5e94db
AE
399 if (!assoc) {
400 amp_ctrl_put(ctrl);
401 return -ENOMEM;
402 }
403
9a5e94db
AE
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
a0c234fe 417 hcon = phylink_add(hdev, mgr, rsp->id, true);
9a5e94db
AE
418 if (!hcon)
419 goto done;
420
421 BT_DBG("Created hcon %p: loc:%d -> rem:%d", hcon, hdev->id, rsp->id);
422
fffadc08 423 mgr->bredr_chan->remote_amp_id = rsp->id;
9495b2ee 424
a02226d6
AE
425 amp_create_phylink(hdev, mgr, hcon);
426
9a5e94db
AE
427done:
428 hci_dev_put(hdev);
429 skb_pull(skb, len);
430 return 0;
431}
432
e072f5da
AE
433static 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;
cb8488c0 440 struct hci_conn *hcon;
0b26ab9d 441 struct amp_ctrl *ctrl;
e072f5da
AE
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);
ece69126 452 if (!hdev || hdev->amp_type == AMP_TYPE_BREDR) {
e072f5da
AE
453 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
454 goto send_rsp;
455 }
456
0b26ab9d
AE
457 ctrl = amp_ctrl_lookup(mgr, rsp.remote_id);
458 if (!ctrl) {
fa4ebc66 459 ctrl = amp_ctrl_add(mgr, rsp.remote_id);
0b26ab9d
AE
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) {
13465c0a
AE
469 size_t assoc_len = le16_to_cpu(hdr->len) - sizeof(*req);
470 u8 *assoc;
0b26ab9d 471
5ae327f0 472 assoc = kmemdup(req->amp_assoc, assoc_len, GFP_KERNEL);
0b26ab9d
AE
473 if (!assoc) {
474 amp_ctrl_put(ctrl);
475 return -ENOMEM;
476 }
477
0b26ab9d
AE
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
a0c234fe 486 hcon = phylink_add(hdev, mgr, req->local_id, false);
cb8488c0 487 if (hcon) {
dffa3871 488 amp_accept_phylink(hdev, mgr, hcon);
cb8488c0
AE
489 rsp.status = A2MP_STATUS_SUCCESS;
490 } else {
491 rsp.status = A2MP_STATUS_UNABLE_START_LINK_CREATION;
492 }
e072f5da
AE
493
494send_rsp:
495 if (hdev)
496 hci_dev_put(hdev);
497
8e05e3ba
AE
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 {
cb6801c6 505 set_bit(WRITE_REMOTE_AMP_ASSOC, &mgr->state);
8e05e3ba
AE
506 mgr->ident = hdr->ident;
507 }
e072f5da
AE
508
509 skb_pull(skb, le16_to_cpu(hdr->len));
510 return 0;
511}
512
6113f84f
AE
513static 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;
cb8488c0 519 struct hci_conn *hcon;
6113f84f
AE
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
cb8488c0 530 hdev = hci_dev_get(req->remote_id);
6113f84f
AE
531 if (!hdev) {
532 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
533 goto send_rsp;
534 }
535
cb8488c0
AE
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
6113f84f
AE
543 /* TODO Disconnect Phys Link here */
544
cb8488c0 545clean:
6113f84f
AE
546 hci_dev_put(hdev);
547
548send_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
f6410a84
AE
555static inline int a2mp_cmd_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
556 struct a2mp_cmd *hdr)
557{
8e8c7e36 558 BT_DBG("ident %d code 0x%2.2x", hdr->ident, hdr->code);
f6410a84
AE
559
560 skb_pull(skb, le16_to_cpu(hdr->len));
561 return 0;
562}
563
6b44d9b8
AE
564/* Handle A2MP signalling */
565static int a2mp_chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
566{
d9fc1d54 567 struct a2mp_cmd *hdr;
6b44d9b8
AE
568 struct amp_mgr *mgr = chan->data;
569 int err = 0;
570
571 amp_mgr_get(mgr);
572
573 while (skb->len >= sizeof(*hdr)) {
d9fc1d54
AE
574 u16 len;
575
576 hdr = (void *) skb->data;
577 len = le16_to_cpu(hdr->len);
6b44d9b8 578
8e8c7e36 579 BT_DBG("code 0x%2.2x id %d len %u", hdr->code, hdr->ident, len);
6b44d9b8
AE
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:
21dbd2ce
AE
592 a2mp_command_rej(mgr, skb, hdr);
593 break;
594
6b44d9b8 595 case A2MP_DISCOVER_REQ:
8598d064
AE
596 err = a2mp_discover_req(mgr, skb, hdr);
597 break;
598
6b44d9b8 599 case A2MP_CHANGE_NOTIFY:
329d81af
AE
600 err = a2mp_change_notify(mgr, skb, hdr);
601 break;
602
6b44d9b8 603 case A2MP_GETINFO_REQ:
47f2d97d
AE
604 err = a2mp_getinfo_req(mgr, skb, hdr);
605 break;
606
6b44d9b8 607 case A2MP_GETAMPASSOC_REQ:
a28381dc
AE
608 err = a2mp_getampassoc_req(mgr, skb, hdr);
609 break;
610
6b44d9b8 611 case A2MP_CREATEPHYSLINK_REQ:
e072f5da
AE
612 err = a2mp_createphyslink_req(mgr, skb, hdr);
613 break;
614
6b44d9b8 615 case A2MP_DISCONNPHYSLINK_REQ:
6113f84f
AE
616 err = a2mp_discphyslink_req(mgr, skb, hdr);
617 break;
618
6b44d9b8 619 case A2MP_DISCOVER_RSP:
aa09537d
AE
620 err = a2mp_discover_rsp(mgr, skb, hdr);
621 break;
622
6b44d9b8 623 case A2MP_GETINFO_RSP:
0d868de9
AE
624 err = a2mp_getinfo_rsp(mgr, skb, hdr);
625 break;
626
6b44d9b8 627 case A2MP_GETAMPASSOC_RSP:
9a5e94db
AE
628 err = a2mp_getampassoc_rsp(mgr, skb, hdr);
629 break;
630
631 case A2MP_CHANGE_RSP:
6b44d9b8
AE
632 case A2MP_CREATEPHYSLINK_RSP:
633 case A2MP_DISCONNPHYSLINK_RSP:
f6410a84
AE
634 err = a2mp_cmd_rsp(mgr, skb, hdr);
635 break;
636
6b44d9b8
AE
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;
d9fc1d54 646
6b44d9b8 647 rej.reason = __constant_cpu_to_le16(0);
d9fc1d54 648 hdr = (void *) skb->data;
6b44d9b8
AE
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
46d5c908
AE
665static void a2mp_chan_close_cb(struct l2cap_chan *chan)
666{
4af66c69 667 l2cap_chan_put(chan);
46d5c908
AE
668}
669
670static 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
689static 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
466f8004
AE
695static struct l2cap_ops a2mp_chan_ops = {
696 .name = "L2CAP A2MP channel",
6b44d9b8 697 .recv = a2mp_chan_recv_cb,
46d5c908
AE
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 */
7e1af8a3
GP
703 .new_connection = l2cap_chan_no_new_connection,
704 .teardown = l2cap_chan_no_teardown,
705 .ready = l2cap_chan_no_ready,
2dc4e510 706 .defer = l2cap_chan_no_defer,
466f8004
AE
707};
708
93c3e8f5 709static struct l2cap_chan *a2mp_chan_open(struct l2cap_conn *conn, bool locked)
466f8004
AE
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
416fa752 720 chan->chan_type = L2CAP_CHAN_CONN_FIX_A2MP;
466f8004
AE
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
93c3e8f5
AE
744 if (locked)
745 __l2cap_chan_add(conn, chan);
746 else
747 l2cap_chan_add(conn, chan);
466f8004
AE
748
749 chan->remote_mps = chan->omtu;
750 chan->mps = chan->omtu;
751
752 chan->state = BT_CONNECTED;
753
754 return chan;
755}
9740e49d
AE
756
757/* AMP Manager functions */
f706adfe 758struct amp_mgr *amp_mgr_get(struct amp_mgr *mgr)
9740e49d 759{
a0dfe0ab 760 BT_DBG("mgr %p orig refcnt %d", mgr, atomic_read(&mgr->kref.refcount));
9740e49d
AE
761
762 kref_get(&mgr->kref);
f706adfe
AE
763
764 return mgr;
9740e49d
AE
765}
766
767static 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
f97268fc
AE
773 mutex_lock(&amp_mgr_list_lock);
774 list_del(&mgr->list);
775 mutex_unlock(&amp_mgr_list_lock);
776
52c0d6e5 777 amp_ctrl_list_flush(mgr);
9740e49d
AE
778 kfree(mgr);
779}
780
781int amp_mgr_put(struct amp_mgr *mgr)
782{
a0dfe0ab 783 BT_DBG("mgr %p orig refcnt %d", mgr, atomic_read(&mgr->kref.refcount));
9740e49d
AE
784
785 return kref_put(&mgr->kref, &amp_mgr_destroy);
786}
787
93c3e8f5 788static struct amp_mgr *amp_mgr_create(struct l2cap_conn *conn, bool locked)
9740e49d
AE
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
93c3e8f5 801 chan = a2mp_chan_open(conn, locked);
9740e49d
AE
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
52c0d6e5
AE
814 /* Remote AMP ctrl list initialization */
815 INIT_LIST_HEAD(&mgr->amp_ctrls);
816 mutex_init(&mgr->amp_ctrls_lock);
817
f97268fc
AE
818 mutex_lock(&amp_mgr_list_lock);
819 list_add(&mgr->list, &amp_mgr_list);
820 mutex_unlock(&amp_mgr_list_lock);
821
9740e49d
AE
822 return mgr;
823}
97e8e89d
AE
824
825struct l2cap_chan *a2mp_channel_create(struct l2cap_conn *conn,
826 struct sk_buff *skb)
827{
828 struct amp_mgr *mgr;
829
93c3e8f5 830 mgr = amp_mgr_create(conn, false);
97e8e89d
AE
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}
f97268fc
AE
840
841struct amp_mgr *amp_mgr_lookup_by_state(u8 state)
842{
843 struct amp_mgr *mgr;
844
845 mutex_lock(&amp_mgr_list_lock);
846 list_for_each_entry(mgr, &amp_mgr_list, list) {
cb6801c6 847 if (test_and_clear_bit(state, &mgr->state)) {
f97268fc
AE
848 amp_mgr_get(mgr);
849 mutex_unlock(&amp_mgr_list_lock);
850 return mgr;
851 }
852 }
853 mutex_unlock(&amp_mgr_list_lock);
854
855 return NULL;
856}
8e2a0d92
AE
857
858void 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
ece69126 872 if (hdev->amp_type != AMP_TYPE_BREDR) {
8e2a0d92
AE
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}
903e4541
AE
884
885void 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}
93c3e8f5 918
9495b2ee
AE
919void 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;
fffadc08 946 req->remote_id = bredr_chan->remote_amp_id;
9495b2ee
AE
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
951clean:
952 amp_mgr_put(mgr);
953 kfree(req);
954}
955
8e05e3ba
AE
956void 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
93c3e8f5
AE
982void 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}
This page took 0.141549 seconds and 5 git commands to generate.