amp.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. Copyright (c) 2011,2012 Intel Corp.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License version 2 and
  5. only version 2 as published by the Free Software Foundation.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. */
  11. #include <net/bluetooth/bluetooth.h>
  12. #include <net/bluetooth/hci.h>
  13. #include <net/bluetooth/hci_core.h>
  14. #include <net/bluetooth/a2mp.h>
  15. #include <net/bluetooth/amp.h>
  16. /* Remote AMP Controllers interface */
  17. static void amp_ctrl_get(struct amp_ctrl *ctrl)
  18. {
  19. BT_DBG("ctrl %p orig refcnt %d", ctrl,
  20. atomic_read(&ctrl->kref.refcount));
  21. kref_get(&ctrl->kref);
  22. }
  23. static void amp_ctrl_destroy(struct kref *kref)
  24. {
  25. struct amp_ctrl *ctrl = container_of(kref, struct amp_ctrl, kref);
  26. BT_DBG("ctrl %p", ctrl);
  27. kfree(ctrl->assoc);
  28. kfree(ctrl);
  29. }
  30. int amp_ctrl_put(struct amp_ctrl *ctrl)
  31. {
  32. BT_DBG("ctrl %p orig refcnt %d", ctrl,
  33. atomic_read(&ctrl->kref.refcount));
  34. return kref_put(&ctrl->kref, &amp_ctrl_destroy);
  35. }
  36. struct amp_ctrl *amp_ctrl_add(struct amp_mgr *mgr)
  37. {
  38. struct amp_ctrl *ctrl;
  39. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  40. if (!ctrl)
  41. return NULL;
  42. mutex_lock(&mgr->amp_ctrls_lock);
  43. list_add(&ctrl->list, &mgr->amp_ctrls);
  44. mutex_unlock(&mgr->amp_ctrls_lock);
  45. kref_init(&ctrl->kref);
  46. BT_DBG("mgr %p ctrl %p", mgr, ctrl);
  47. return ctrl;
  48. }
  49. void amp_ctrl_list_flush(struct amp_mgr *mgr)
  50. {
  51. struct amp_ctrl *ctrl, *n;
  52. BT_DBG("mgr %p", mgr);
  53. mutex_lock(&mgr->amp_ctrls_lock);
  54. list_for_each_entry_safe(ctrl, n, &mgr->amp_ctrls, list) {
  55. list_del(&ctrl->list);
  56. amp_ctrl_put(ctrl);
  57. }
  58. mutex_unlock(&mgr->amp_ctrls_lock);
  59. }
  60. struct amp_ctrl *amp_ctrl_lookup(struct amp_mgr *mgr, u8 id)
  61. {
  62. struct amp_ctrl *ctrl;
  63. BT_DBG("mgr %p id %d", mgr, id);
  64. mutex_lock(&mgr->amp_ctrls_lock);
  65. list_for_each_entry(ctrl, &mgr->amp_ctrls, list) {
  66. if (ctrl->id == id) {
  67. amp_ctrl_get(ctrl);
  68. mutex_unlock(&mgr->amp_ctrls_lock);
  69. return ctrl;
  70. }
  71. }
  72. mutex_unlock(&mgr->amp_ctrls_lock);
  73. return NULL;
  74. }
  75. /* Physical Link interface */
  76. static u8 __next_handle(struct amp_mgr *mgr)
  77. {
  78. if (++mgr->handle == 0)
  79. mgr->handle = 1;
  80. return mgr->handle;
  81. }
  82. struct hci_conn *phylink_add(struct hci_dev *hdev, struct amp_mgr *mgr,
  83. u8 remote_id)
  84. {
  85. bdaddr_t *dst = mgr->l2cap_conn->dst;
  86. struct hci_conn *hcon;
  87. hcon = hci_conn_add(hdev, AMP_LINK, dst);
  88. if (!hcon)
  89. return NULL;
  90. hcon->state = BT_CONNECT;
  91. hcon->out = true;
  92. hcon->attempt++;
  93. hcon->handle = __next_handle(mgr);
  94. hcon->remote_id = remote_id;
  95. hcon->amp_mgr = mgr;
  96. return hcon;
  97. }
  98. void amp_read_loc_assoc_frag(struct hci_dev *hdev, u8 phy_handle)
  99. {
  100. struct hci_cp_read_local_amp_assoc cp;
  101. struct amp_assoc *loc_assoc = &hdev->loc_assoc;
  102. BT_DBG("%s handle %d", hdev->name, phy_handle);
  103. cp.phy_handle = phy_handle;
  104. cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
  105. cp.len_so_far = cpu_to_le16(loc_assoc->offset);
  106. hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
  107. }
  108. void amp_read_loc_assoc(struct hci_dev *hdev, struct amp_mgr *mgr)
  109. {
  110. struct hci_cp_read_local_amp_assoc cp;
  111. memset(&hdev->loc_assoc, 0, sizeof(struct amp_assoc));
  112. memset(&cp, 0, sizeof(cp));
  113. cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
  114. mgr->state = READ_LOC_AMP_ASSOC;
  115. hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
  116. }