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