diff options
Diffstat (limited to 'net/bluetooth/a2mp.c')
-rw-r--r-- | net/bluetooth/a2mp.c | 459 |
1 files changed, 429 insertions, 30 deletions
diff --git a/net/bluetooth/a2mp.c b/net/bluetooth/a2mp.c index 0760d1fed6f0..d5136cfb57e2 100644 --- a/net/bluetooth/a2mp.c +++ b/net/bluetooth/a2mp.c | |||
@@ -16,6 +16,11 @@ | |||
16 | #include <net/bluetooth/hci_core.h> | 16 | #include <net/bluetooth/hci_core.h> |
17 | #include <net/bluetooth/l2cap.h> | 17 | #include <net/bluetooth/l2cap.h> |
18 | #include <net/bluetooth/a2mp.h> | 18 | #include <net/bluetooth/a2mp.h> |
19 | #include <net/bluetooth/amp.h> | ||
20 | |||
21 | /* Global AMP Manager list */ | ||
22 | LIST_HEAD(amp_mgr_list); | ||
23 | DEFINE_MUTEX(amp_mgr_list_lock); | ||
19 | 24 | ||
20 | /* A2MP build & send command helper functions */ | 25 | /* A2MP build & send command helper functions */ |
21 | static struct a2mp_cmd *__a2mp_build(u8 code, u8 ident, u16 len, void *data) | 26 | static struct a2mp_cmd *__a2mp_build(u8 code, u8 ident, u16 len, void *data) |
@@ -37,8 +42,7 @@ static struct a2mp_cmd *__a2mp_build(u8 code, u8 ident, u16 len, void *data) | |||
37 | return cmd; | 42 | return cmd; |
38 | } | 43 | } |
39 | 44 | ||
40 | static void a2mp_send(struct amp_mgr *mgr, u8 code, u8 ident, u16 len, | 45 | void a2mp_send(struct amp_mgr *mgr, u8 code, u8 ident, u16 len, void *data) |
41 | void *data) | ||
42 | { | 46 | { |
43 | struct l2cap_chan *chan = mgr->a2mp_chan; | 47 | struct l2cap_chan *chan = mgr->a2mp_chan; |
44 | struct a2mp_cmd *cmd; | 48 | struct a2mp_cmd *cmd; |
@@ -63,6 +67,14 @@ static void a2mp_send(struct amp_mgr *mgr, u8 code, u8 ident, u16 len, | |||
63 | kfree(cmd); | 67 | kfree(cmd); |
64 | } | 68 | } |
65 | 69 | ||
70 | u8 __next_ident(struct amp_mgr *mgr) | ||
71 | { | ||
72 | if (++mgr->ident == 0) | ||
73 | mgr->ident = 1; | ||
74 | |||
75 | return mgr->ident; | ||
76 | } | ||
77 | |||
66 | static inline void __a2mp_cl_bredr(struct a2mp_cl *cl) | 78 | static inline void __a2mp_cl_bredr(struct a2mp_cl *cl) |
67 | { | 79 | { |
68 | cl->id = 0; | 80 | cl->id = 0; |
@@ -161,6 +173,83 @@ static int a2mp_discover_req(struct amp_mgr *mgr, struct sk_buff *skb, | |||
161 | return 0; | 173 | return 0; |
162 | } | 174 | } |
163 | 175 | ||
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; | ||
183 | bool found = false; | ||
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 | |||
214 | found = true; | ||
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 | |||
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 | |||
250 | return 0; | ||
251 | } | ||
252 | |||
164 | static int a2mp_change_notify(struct amp_mgr *mgr, struct sk_buff *skb, | 253 | static int a2mp_change_notify(struct amp_mgr *mgr, struct sk_buff *skb, |
165 | struct a2mp_cmd *hdr) | 254 | struct a2mp_cmd *hdr) |
166 | { | 255 | { |
@@ -181,7 +270,6 @@ static int a2mp_getinfo_req(struct amp_mgr *mgr, struct sk_buff *skb, | |||
181 | struct a2mp_cmd *hdr) | 270 | struct a2mp_cmd *hdr) |
182 | { | 271 | { |
183 | struct a2mp_info_req *req = (void *) skb->data; | 272 | struct a2mp_info_req *req = (void *) skb->data; |
184 | struct a2mp_info_rsp rsp; | ||
185 | struct hci_dev *hdev; | 273 | struct hci_dev *hdev; |
186 | 274 | ||
187 | if (le16_to_cpu(hdr->len) < sizeof(*req)) | 275 | if (le16_to_cpu(hdr->len) < sizeof(*req)) |
@@ -189,53 +277,93 @@ static int a2mp_getinfo_req(struct amp_mgr *mgr, struct sk_buff *skb, | |||
189 | 277 | ||
190 | BT_DBG("id %d", req->id); | 278 | BT_DBG("id %d", req->id); |
191 | 279 | ||
192 | rsp.id = req->id; | ||
193 | rsp.status = A2MP_STATUS_INVALID_CTRL_ID; | ||
194 | |||
195 | hdev = hci_dev_get(req->id); | 280 | hdev = hci_dev_get(req->id); |
196 | if (hdev && hdev->amp_type != HCI_BREDR) { | 281 | if (!hdev || hdev->dev_type != HCI_AMP) { |
197 | rsp.status = 0; | 282 | struct a2mp_info_rsp rsp; |
198 | rsp.total_bw = cpu_to_le32(hdev->amp_total_bw); | 283 | |
199 | rsp.max_bw = cpu_to_le32(hdev->amp_max_bw); | 284 | rsp.id = req->id; |
200 | rsp.min_latency = cpu_to_le32(hdev->amp_min_latency); | 285 | rsp.status = A2MP_STATUS_INVALID_CTRL_ID; |
201 | rsp.pal_cap = cpu_to_le16(hdev->amp_pal_cap); | 286 | |
202 | rsp.assoc_size = cpu_to_le16(hdev->amp_assoc_size); | 287 | a2mp_send(mgr, A2MP_GETINFO_RSP, hdr->ident, sizeof(rsp), |
288 | &rsp); | ||
289 | |||
290 | goto done; | ||
203 | } | 291 | } |
204 | 292 | ||
293 | mgr->state = READ_LOC_AMP_INFO; | ||
294 | hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL); | ||
295 | |||
296 | done: | ||
205 | if (hdev) | 297 | if (hdev) |
206 | hci_dev_put(hdev); | 298 | hci_dev_put(hdev); |
207 | 299 | ||
208 | a2mp_send(mgr, A2MP_GETINFO_RSP, hdr->ident, sizeof(rsp), &rsp); | ||
209 | |||
210 | skb_pull(skb, sizeof(*req)); | 300 | skb_pull(skb, sizeof(*req)); |
211 | return 0; | 301 | return 0; |
212 | } | 302 | } |
213 | 303 | ||
304 | static int a2mp_getinfo_rsp(struct amp_mgr *mgr, struct sk_buff *skb, | ||
305 | struct a2mp_cmd *hdr) | ||
306 | { | ||
307 | struct a2mp_info_rsp *rsp = (struct a2mp_info_rsp *) skb->data; | ||
308 | struct a2mp_amp_assoc_req req; | ||
309 | struct amp_ctrl *ctrl; | ||
310 | |||
311 | if (le16_to_cpu(hdr->len) < sizeof(*rsp)) | ||
312 | return -EINVAL; | ||
313 | |||
314 | BT_DBG("id %d status 0x%2.2x", rsp->id, rsp->status); | ||
315 | |||
316 | if (rsp->status) | ||
317 | return -EINVAL; | ||
318 | |||
319 | ctrl = amp_ctrl_add(mgr, rsp->id); | ||
320 | if (!ctrl) | ||
321 | return -ENOMEM; | ||
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 | |||
214 | static int a2mp_getampassoc_req(struct amp_mgr *mgr, struct sk_buff *skb, | 331 | static int a2mp_getampassoc_req(struct amp_mgr *mgr, struct sk_buff *skb, |
215 | struct a2mp_cmd *hdr) | 332 | struct a2mp_cmd *hdr) |
216 | { | 333 | { |
217 | struct a2mp_amp_assoc_req *req = (void *) skb->data; | 334 | struct a2mp_amp_assoc_req *req = (void *) skb->data; |
218 | struct hci_dev *hdev; | 335 | struct hci_dev *hdev; |
336 | struct amp_mgr *tmp; | ||
219 | 337 | ||
220 | if (le16_to_cpu(hdr->len) < sizeof(*req)) | 338 | if (le16_to_cpu(hdr->len) < sizeof(*req)) |
221 | return -EINVAL; | 339 | return -EINVAL; |
222 | 340 | ||
223 | BT_DBG("id %d", req->id); | 341 | BT_DBG("id %d", req->id); |
224 | 342 | ||
343 | /* Make sure that other request is not processed */ | ||
344 | tmp = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC); | ||
345 | |||
225 | hdev = hci_dev_get(req->id); | 346 | hdev = hci_dev_get(req->id); |
226 | if (!hdev || hdev->amp_type == HCI_BREDR) { | 347 | if (!hdev || hdev->amp_type == HCI_BREDR || tmp) { |
227 | struct a2mp_amp_assoc_rsp rsp; | 348 | struct a2mp_amp_assoc_rsp rsp; |
228 | rsp.id = req->id; | 349 | rsp.id = req->id; |
229 | rsp.status = A2MP_STATUS_INVALID_CTRL_ID; | 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 | } | ||
230 | 357 | ||
231 | a2mp_send(mgr, A2MP_GETAMPASSOC_RSP, hdr->ident, sizeof(rsp), | 358 | a2mp_send(mgr, A2MP_GETAMPASSOC_RSP, hdr->ident, sizeof(rsp), |
232 | &rsp); | 359 | &rsp); |
233 | goto clean; | 360 | |
361 | goto done; | ||
234 | } | 362 | } |
235 | 363 | ||
236 | /* Placeholder for HCI Read AMP Assoc */ | 364 | amp_read_loc_assoc(hdev, mgr); |
237 | 365 | ||
238 | clean: | 366 | done: |
239 | if (hdev) | 367 | if (hdev) |
240 | hci_dev_put(hdev); | 368 | hci_dev_put(hdev); |
241 | 369 | ||
@@ -243,6 +371,68 @@ clean: | |||
243 | return 0; | 371 | return 0; |
244 | } | 372 | } |
245 | 373 | ||
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 | size_t assoc_len; | ||
383 | |||
384 | if (len < sizeof(*rsp)) | ||
385 | return -EINVAL; | ||
386 | |||
387 | assoc_len = len - sizeof(*rsp); | ||
388 | |||
389 | BT_DBG("id %d status 0x%2.2x assoc len %zu", rsp->id, rsp->status, | ||
390 | assoc_len); | ||
391 | |||
392 | if (rsp->status) | ||
393 | return -EINVAL; | ||
394 | |||
395 | /* Save remote ASSOC data */ | ||
396 | ctrl = amp_ctrl_lookup(mgr, rsp->id); | ||
397 | if (ctrl) { | ||
398 | u8 *assoc; | ||
399 | |||
400 | assoc = kzalloc(assoc_len, GFP_KERNEL); | ||
401 | if (!assoc) { | ||
402 | amp_ctrl_put(ctrl); | ||
403 | return -ENOMEM; | ||
404 | } | ||
405 | |||
406 | memcpy(assoc, rsp->amp_assoc, assoc_len); | ||
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 | |||
420 | hcon = phylink_add(hdev, mgr, rsp->id, true); | ||
421 | if (!hcon) | ||
422 | goto done; | ||
423 | |||
424 | BT_DBG("Created hcon %p: loc:%d -> rem:%d", hcon, hdev->id, rsp->id); | ||
425 | |||
426 | mgr->bredr_chan->ctrl_id = rsp->id; | ||
427 | |||
428 | amp_create_phylink(hdev, mgr, hcon); | ||
429 | |||
430 | done: | ||
431 | hci_dev_put(hdev); | ||
432 | skb_pull(skb, len); | ||
433 | return 0; | ||
434 | } | ||
435 | |||
246 | static int a2mp_createphyslink_req(struct amp_mgr *mgr, struct sk_buff *skb, | 436 | static int a2mp_createphyslink_req(struct amp_mgr *mgr, struct sk_buff *skb, |
247 | struct a2mp_cmd *hdr) | 437 | struct a2mp_cmd *hdr) |
248 | { | 438 | { |
@@ -250,6 +440,8 @@ static int a2mp_createphyslink_req(struct amp_mgr *mgr, struct sk_buff *skb, | |||
250 | 440 | ||
251 | struct a2mp_physlink_rsp rsp; | 441 | struct a2mp_physlink_rsp rsp; |
252 | struct hci_dev *hdev; | 442 | struct hci_dev *hdev; |
443 | struct hci_conn *hcon; | ||
444 | struct amp_ctrl *ctrl; | ||
253 | 445 | ||
254 | if (le16_to_cpu(hdr->len) < sizeof(*req)) | 446 | if (le16_to_cpu(hdr->len) < sizeof(*req)) |
255 | return -EINVAL; | 447 | return -EINVAL; |
@@ -265,9 +457,43 @@ static int a2mp_createphyslink_req(struct amp_mgr *mgr, struct sk_buff *skb, | |||
265 | goto send_rsp; | 457 | goto send_rsp; |
266 | } | 458 | } |
267 | 459 | ||
268 | /* TODO process physlink create */ | 460 | ctrl = amp_ctrl_lookup(mgr, rsp.remote_id); |
461 | if (!ctrl) { | ||
462 | ctrl = amp_ctrl_add(mgr, rsp.remote_id); | ||
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 | } | ||
269 | 470 | ||
270 | rsp.status = A2MP_STATUS_SUCCESS; | 471 | if (ctrl) { |
472 | size_t assoc_len = le16_to_cpu(hdr->len) - sizeof(*req); | ||
473 | u8 *assoc; | ||
474 | |||
475 | assoc = kzalloc(assoc_len, GFP_KERNEL); | ||
476 | if (!assoc) { | ||
477 | amp_ctrl_put(ctrl); | ||
478 | return -ENOMEM; | ||
479 | } | ||
480 | |||
481 | memcpy(assoc, req->amp_assoc, assoc_len); | ||
482 | ctrl->assoc = assoc; | ||
483 | ctrl->assoc_len = assoc_len; | ||
484 | ctrl->assoc_rem_len = assoc_len; | ||
485 | ctrl->assoc_len_so_far = 0; | ||
486 | |||
487 | amp_ctrl_put(ctrl); | ||
488 | } | ||
489 | |||
490 | hcon = phylink_add(hdev, mgr, req->local_id, false); | ||
491 | if (hcon) { | ||
492 | amp_accept_phylink(hdev, mgr, hcon); | ||
493 | rsp.status = A2MP_STATUS_SUCCESS; | ||
494 | } else { | ||
495 | rsp.status = A2MP_STATUS_UNABLE_START_LINK_CREATION; | ||
496 | } | ||
271 | 497 | ||
272 | send_rsp: | 498 | send_rsp: |
273 | if (hdev) | 499 | if (hdev) |
@@ -286,6 +512,7 @@ static int a2mp_discphyslink_req(struct amp_mgr *mgr, struct sk_buff *skb, | |||
286 | struct a2mp_physlink_req *req = (void *) skb->data; | 512 | struct a2mp_physlink_req *req = (void *) skb->data; |
287 | struct a2mp_physlink_rsp rsp; | 513 | struct a2mp_physlink_rsp rsp; |
288 | struct hci_dev *hdev; | 514 | struct hci_dev *hdev; |
515 | struct hci_conn *hcon; | ||
289 | 516 | ||
290 | if (le16_to_cpu(hdr->len) < sizeof(*req)) | 517 | if (le16_to_cpu(hdr->len) < sizeof(*req)) |
291 | return -EINVAL; | 518 | return -EINVAL; |
@@ -296,14 +523,22 @@ static int a2mp_discphyslink_req(struct amp_mgr *mgr, struct sk_buff *skb, | |||
296 | rsp.remote_id = req->local_id; | 523 | rsp.remote_id = req->local_id; |
297 | rsp.status = A2MP_STATUS_SUCCESS; | 524 | rsp.status = A2MP_STATUS_SUCCESS; |
298 | 525 | ||
299 | hdev = hci_dev_get(req->local_id); | 526 | hdev = hci_dev_get(req->remote_id); |
300 | if (!hdev) { | 527 | if (!hdev) { |
301 | rsp.status = A2MP_STATUS_INVALID_CTRL_ID; | 528 | rsp.status = A2MP_STATUS_INVALID_CTRL_ID; |
302 | goto send_rsp; | 529 | goto send_rsp; |
303 | } | 530 | } |
304 | 531 | ||
532 | hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK, mgr->l2cap_conn->dst); | ||
533 | if (!hcon) { | ||
534 | BT_ERR("No phys link exist"); | ||
535 | rsp.status = A2MP_STATUS_NO_PHYSICAL_LINK_EXISTS; | ||
536 | goto clean; | ||
537 | } | ||
538 | |||
305 | /* TODO Disconnect Phys Link here */ | 539 | /* TODO Disconnect Phys Link here */ |
306 | 540 | ||
541 | clean: | ||
307 | hci_dev_put(hdev); | 542 | hci_dev_put(hdev); |
308 | 543 | ||
309 | send_rsp: | 544 | send_rsp: |
@@ -377,10 +612,19 @@ static int a2mp_chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb) | |||
377 | err = a2mp_discphyslink_req(mgr, skb, hdr); | 612 | err = a2mp_discphyslink_req(mgr, skb, hdr); |
378 | break; | 613 | break; |
379 | 614 | ||
380 | case A2MP_CHANGE_RSP: | ||
381 | case A2MP_DISCOVER_RSP: | 615 | case A2MP_DISCOVER_RSP: |
616 | err = a2mp_discover_rsp(mgr, skb, hdr); | ||
617 | break; | ||
618 | |||
382 | case A2MP_GETINFO_RSP: | 619 | case A2MP_GETINFO_RSP: |
620 | err = a2mp_getinfo_rsp(mgr, skb, hdr); | ||
621 | break; | ||
622 | |||
383 | case A2MP_GETAMPASSOC_RSP: | 623 | case A2MP_GETAMPASSOC_RSP: |
624 | err = a2mp_getampassoc_rsp(mgr, skb, hdr); | ||
625 | break; | ||
626 | |||
627 | case A2MP_CHANGE_RSP: | ||
384 | case A2MP_CREATEPHYSLINK_RSP: | 628 | case A2MP_CREATEPHYSLINK_RSP: |
385 | case A2MP_DISCONNPHYSLINK_RSP: | 629 | case A2MP_DISCONNPHYSLINK_RSP: |
386 | err = a2mp_cmd_rsp(mgr, skb, hdr); | 630 | err = a2mp_cmd_rsp(mgr, skb, hdr); |
@@ -455,9 +699,10 @@ static struct l2cap_ops a2mp_chan_ops = { | |||
455 | .new_connection = l2cap_chan_no_new_connection, | 699 | .new_connection = l2cap_chan_no_new_connection, |
456 | .teardown = l2cap_chan_no_teardown, | 700 | .teardown = l2cap_chan_no_teardown, |
457 | .ready = l2cap_chan_no_ready, | 701 | .ready = l2cap_chan_no_ready, |
702 | .defer = l2cap_chan_no_defer, | ||
458 | }; | 703 | }; |
459 | 704 | ||
460 | static struct l2cap_chan *a2mp_chan_open(struct l2cap_conn *conn) | 705 | static struct l2cap_chan *a2mp_chan_open(struct l2cap_conn *conn, bool locked) |
461 | { | 706 | { |
462 | struct l2cap_chan *chan; | 707 | struct l2cap_chan *chan; |
463 | int err; | 708 | int err; |
@@ -492,7 +737,10 @@ static struct l2cap_chan *a2mp_chan_open(struct l2cap_conn *conn) | |||
492 | 737 | ||
493 | chan->conf_state = 0; | 738 | chan->conf_state = 0; |
494 | 739 | ||
495 | l2cap_chan_add(conn, chan); | 740 | if (locked) |
741 | __l2cap_chan_add(conn, chan); | ||
742 | else | ||
743 | l2cap_chan_add(conn, chan); | ||
496 | 744 | ||
497 | chan->remote_mps = chan->omtu; | 745 | chan->remote_mps = chan->omtu; |
498 | chan->mps = chan->omtu; | 746 | chan->mps = chan->omtu; |
@@ -503,11 +751,13 @@ static struct l2cap_chan *a2mp_chan_open(struct l2cap_conn *conn) | |||
503 | } | 751 | } |
504 | 752 | ||
505 | /* AMP Manager functions */ | 753 | /* AMP Manager functions */ |
506 | void amp_mgr_get(struct amp_mgr *mgr) | 754 | struct amp_mgr *amp_mgr_get(struct amp_mgr *mgr) |
507 | { | 755 | { |
508 | BT_DBG("mgr %p orig refcnt %d", mgr, atomic_read(&mgr->kref.refcount)); | 756 | BT_DBG("mgr %p orig refcnt %d", mgr, atomic_read(&mgr->kref.refcount)); |
509 | 757 | ||
510 | kref_get(&mgr->kref); | 758 | kref_get(&mgr->kref); |
759 | |||
760 | return mgr; | ||
511 | } | 761 | } |
512 | 762 | ||
513 | static void amp_mgr_destroy(struct kref *kref) | 763 | static void amp_mgr_destroy(struct kref *kref) |
@@ -516,6 +766,11 @@ static void amp_mgr_destroy(struct kref *kref) | |||
516 | 766 | ||
517 | BT_DBG("mgr %p", mgr); | 767 | BT_DBG("mgr %p", mgr); |
518 | 768 | ||
769 | mutex_lock(&_mgr_list_lock); | ||
770 | list_del(&mgr->list); | ||
771 | mutex_unlock(&_mgr_list_lock); | ||
772 | |||
773 | amp_ctrl_list_flush(mgr); | ||
519 | kfree(mgr); | 774 | kfree(mgr); |
520 | } | 775 | } |
521 | 776 | ||
@@ -526,7 +781,7 @@ int amp_mgr_put(struct amp_mgr *mgr) | |||
526 | return kref_put(&mgr->kref, &_mgr_destroy); | 781 | return kref_put(&mgr->kref, &_mgr_destroy); |
527 | } | 782 | } |
528 | 783 | ||
529 | static struct amp_mgr *amp_mgr_create(struct l2cap_conn *conn) | 784 | static struct amp_mgr *amp_mgr_create(struct l2cap_conn *conn, bool locked) |
530 | { | 785 | { |
531 | struct amp_mgr *mgr; | 786 | struct amp_mgr *mgr; |
532 | struct l2cap_chan *chan; | 787 | struct l2cap_chan *chan; |
@@ -539,7 +794,7 @@ static struct amp_mgr *amp_mgr_create(struct l2cap_conn *conn) | |||
539 | 794 | ||
540 | mgr->l2cap_conn = conn; | 795 | mgr->l2cap_conn = conn; |
541 | 796 | ||
542 | chan = a2mp_chan_open(conn); | 797 | chan = a2mp_chan_open(conn, locked); |
543 | if (!chan) { | 798 | if (!chan) { |
544 | kfree(mgr); | 799 | kfree(mgr); |
545 | return NULL; | 800 | return NULL; |
@@ -552,6 +807,14 @@ static struct amp_mgr *amp_mgr_create(struct l2cap_conn *conn) | |||
552 | 807 | ||
553 | kref_init(&mgr->kref); | 808 | kref_init(&mgr->kref); |
554 | 809 | ||
810 | /* Remote AMP ctrl list initialization */ | ||
811 | INIT_LIST_HEAD(&mgr->amp_ctrls); | ||
812 | mutex_init(&mgr->amp_ctrls_lock); | ||
813 | |||
814 | mutex_lock(&_mgr_list_lock); | ||
815 | list_add(&mgr->list, &_mgr_list); | ||
816 | mutex_unlock(&_mgr_list_lock); | ||
817 | |||
555 | return mgr; | 818 | return mgr; |
556 | } | 819 | } |
557 | 820 | ||
@@ -560,7 +823,7 @@ struct l2cap_chan *a2mp_channel_create(struct l2cap_conn *conn, | |||
560 | { | 823 | { |
561 | struct amp_mgr *mgr; | 824 | struct amp_mgr *mgr; |
562 | 825 | ||
563 | mgr = amp_mgr_create(conn); | 826 | mgr = amp_mgr_create(conn, false); |
564 | if (!mgr) { | 827 | if (!mgr) { |
565 | BT_ERR("Could not create AMP manager"); | 828 | BT_ERR("Could not create AMP manager"); |
566 | return NULL; | 829 | return NULL; |
@@ -570,3 +833,139 @@ struct l2cap_chan *a2mp_channel_create(struct l2cap_conn *conn, | |||
570 | 833 | ||
571 | return mgr->a2mp_chan; | 834 | return mgr->a2mp_chan; |
572 | } | 835 | } |
836 | |||
837 | struct amp_mgr *amp_mgr_lookup_by_state(u8 state) | ||
838 | { | ||
839 | struct amp_mgr *mgr; | ||
840 | |||
841 | mutex_lock(&_mgr_list_lock); | ||
842 | list_for_each_entry(mgr, &_mgr_list, list) { | ||
843 | if (mgr->state == state) { | ||
844 | amp_mgr_get(mgr); | ||
845 | mutex_unlock(&_mgr_list_lock); | ||
846 | return mgr; | ||
847 | } | ||
848 | } | ||
849 | mutex_unlock(&_mgr_list_lock); | ||
850 | |||
851 | return NULL; | ||
852 | } | ||
853 | |||
854 | void a2mp_send_getinfo_rsp(struct hci_dev *hdev) | ||
855 | { | ||
856 | struct amp_mgr *mgr; | ||
857 | struct a2mp_info_rsp rsp; | ||
858 | |||
859 | mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_INFO); | ||
860 | if (!mgr) | ||
861 | return; | ||
862 | |||
863 | BT_DBG("%s mgr %p", hdev->name, mgr); | ||
864 | |||
865 | rsp.id = hdev->id; | ||
866 | rsp.status = A2MP_STATUS_INVALID_CTRL_ID; | ||
867 | |||
868 | if (hdev->amp_type != HCI_BREDR) { | ||
869 | rsp.status = 0; | ||
870 | rsp.total_bw = cpu_to_le32(hdev->amp_total_bw); | ||
871 | rsp.max_bw = cpu_to_le32(hdev->amp_max_bw); | ||
872 | rsp.min_latency = cpu_to_le32(hdev->amp_min_latency); | ||
873 | rsp.pal_cap = cpu_to_le16(hdev->amp_pal_cap); | ||
874 | rsp.assoc_size = cpu_to_le16(hdev->amp_assoc_size); | ||
875 | } | ||
876 | |||
877 | a2mp_send(mgr, A2MP_GETINFO_RSP, mgr->ident, sizeof(rsp), &rsp); | ||
878 | amp_mgr_put(mgr); | ||
879 | } | ||
880 | |||
881 | void a2mp_send_getampassoc_rsp(struct hci_dev *hdev, u8 status) | ||
882 | { | ||
883 | struct amp_mgr *mgr; | ||
884 | struct amp_assoc *loc_assoc = &hdev->loc_assoc; | ||
885 | struct a2mp_amp_assoc_rsp *rsp; | ||
886 | size_t len; | ||
887 | |||
888 | mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC); | ||
889 | if (!mgr) | ||
890 | return; | ||
891 | |||
892 | BT_DBG("%s mgr %p", hdev->name, mgr); | ||
893 | |||
894 | len = sizeof(struct a2mp_amp_assoc_rsp) + loc_assoc->len; | ||
895 | rsp = kzalloc(len, GFP_KERNEL); | ||
896 | if (!rsp) { | ||
897 | amp_mgr_put(mgr); | ||
898 | return; | ||
899 | } | ||
900 | |||
901 | rsp->id = hdev->id; | ||
902 | |||
903 | if (status) { | ||
904 | rsp->status = A2MP_STATUS_INVALID_CTRL_ID; | ||
905 | } else { | ||
906 | rsp->status = A2MP_STATUS_SUCCESS; | ||
907 | memcpy(rsp->amp_assoc, loc_assoc->data, loc_assoc->len); | ||
908 | } | ||
909 | |||
910 | a2mp_send(mgr, A2MP_GETAMPASSOC_RSP, mgr->ident, len, rsp); | ||
911 | amp_mgr_put(mgr); | ||
912 | kfree(rsp); | ||
913 | } | ||
914 | |||
915 | void a2mp_send_create_phy_link_req(struct hci_dev *hdev, u8 status) | ||
916 | { | ||
917 | struct amp_mgr *mgr; | ||
918 | struct amp_assoc *loc_assoc = &hdev->loc_assoc; | ||
919 | struct a2mp_physlink_req *req; | ||
920 | struct l2cap_chan *bredr_chan; | ||
921 | size_t len; | ||
922 | |||
923 | mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC_FINAL); | ||
924 | if (!mgr) | ||
925 | return; | ||
926 | |||
927 | len = sizeof(*req) + loc_assoc->len; | ||
928 | |||
929 | BT_DBG("%s mgr %p assoc_len %zu", hdev->name, mgr, len); | ||
930 | |||
931 | req = kzalloc(len, GFP_KERNEL); | ||
932 | if (!req) { | ||
933 | amp_mgr_put(mgr); | ||
934 | return; | ||
935 | } | ||
936 | |||
937 | bredr_chan = mgr->bredr_chan; | ||
938 | if (!bredr_chan) | ||
939 | goto clean; | ||
940 | |||
941 | req->local_id = hdev->id; | ||
942 | req->remote_id = bredr_chan->ctrl_id; | ||
943 | memcpy(req->amp_assoc, loc_assoc->data, loc_assoc->len); | ||
944 | |||
945 | a2mp_send(mgr, A2MP_CREATEPHYSLINK_REQ, __next_ident(mgr), len, req); | ||
946 | |||
947 | clean: | ||
948 | amp_mgr_put(mgr); | ||
949 | kfree(req); | ||
950 | } | ||
951 | |||
952 | void a2mp_discover_amp(struct l2cap_chan *chan) | ||
953 | { | ||
954 | struct l2cap_conn *conn = chan->conn; | ||
955 | struct amp_mgr *mgr = conn->hcon->amp_mgr; | ||
956 | struct a2mp_discov_req req; | ||
957 | |||
958 | BT_DBG("chan %p conn %p mgr %p", chan, conn, mgr); | ||
959 | |||
960 | if (!mgr) { | ||
961 | mgr = amp_mgr_create(conn, true); | ||
962 | if (!mgr) | ||
963 | return; | ||
964 | } | ||
965 | |||
966 | mgr->bredr_chan = chan; | ||
967 | |||
968 | req.mtu = cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU); | ||
969 | req.ext_feat = 0; | ||
970 | a2mp_send(mgr, A2MP_DISCOVER_REQ, 1, sizeof(req), &req); | ||
971 | } | ||