Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | BlueZ - Bluetooth protocol stack for Linux |
| 3 | Copyright (C) 2010 Nokia Corporation |
| 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 as |
| 7 | published by the Free Software Foundation; |
| 8 | |
| 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 10 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 11 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 12 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
| 13 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 14 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | |
| 18 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 19 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
| 20 | SOFTWARE IS DISCLAIMED. |
| 21 | */ |
| 22 | |
| 23 | /* Bluetooth HCI Management interface */ |
| 24 | |
Szymon Janc | 7235975 | 2011-02-17 14:16:32 +0100 | [diff] [blame] | 25 | #include <linux/uaccess.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 26 | #include <asm/unaligned.h> |
| 27 | |
| 28 | #include <net/bluetooth/bluetooth.h> |
| 29 | #include <net/bluetooth/hci_core.h> |
| 30 | #include <net/bluetooth/mgmt.h> |
| 31 | |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 32 | #define MGMT_VERSION 0 |
| 33 | #define MGMT_REVISION 1 |
| 34 | |
Andre Guedes | 2519a1f | 2011-11-07 11:45:24 -0300 | [diff] [blame] | 35 | #define INQUIRY_LEN_BREDR 0x08 /* TGAP(100) */ |
| 36 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 37 | struct pending_cmd { |
| 38 | struct list_head list; |
| 39 | __u16 opcode; |
| 40 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 41 | void *param; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 42 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 43 | void *user_data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 44 | }; |
| 45 | |
Johannes Berg | b5ad8b7 | 2011-06-01 08:54:45 +0200 | [diff] [blame] | 46 | static LIST_HEAD(cmd_list); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 47 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 48 | static int cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 49 | { |
| 50 | struct sk_buff *skb; |
| 51 | struct mgmt_hdr *hdr; |
| 52 | struct mgmt_ev_cmd_status *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 53 | int err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 54 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 55 | BT_DBG("sock %p, index %u, cmd %u, status %u", sk, index, cmd, status); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 56 | |
| 57 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_ATOMIC); |
| 58 | if (!skb) |
| 59 | return -ENOMEM; |
| 60 | |
| 61 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 62 | |
| 63 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 64 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 65 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 66 | |
| 67 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 68 | ev->status = status; |
| 69 | put_unaligned_le16(cmd, &ev->opcode); |
| 70 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 71 | err = sock_queue_rcv_skb(sk, skb); |
| 72 | if (err < 0) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 73 | kfree_skb(skb); |
| 74 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 75 | return err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 76 | } |
| 77 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 78 | static int cmd_complete(struct sock *sk, u16 index, u16 cmd, void *rp, |
| 79 | size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 80 | { |
| 81 | struct sk_buff *skb; |
| 82 | struct mgmt_hdr *hdr; |
| 83 | struct mgmt_ev_cmd_complete *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 84 | int err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 85 | |
| 86 | BT_DBG("sock %p", sk); |
| 87 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 88 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_ATOMIC); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 89 | if (!skb) |
| 90 | return -ENOMEM; |
| 91 | |
| 92 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 93 | |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 94 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 95 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 96 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 97 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 98 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
| 99 | put_unaligned_le16(cmd, &ev->opcode); |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 100 | |
| 101 | if (rp) |
| 102 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 103 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 104 | err = sock_queue_rcv_skb(sk, skb); |
| 105 | if (err < 0) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 106 | kfree_skb(skb); |
| 107 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 108 | return err;; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 111 | static int read_version(struct sock *sk) |
| 112 | { |
| 113 | struct mgmt_rp_read_version rp; |
| 114 | |
| 115 | BT_DBG("sock %p", sk); |
| 116 | |
| 117 | rp.version = MGMT_VERSION; |
| 118 | put_unaligned_le16(MGMT_REVISION, &rp.revision); |
| 119 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 120 | return cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, &rp, |
| 121 | sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 122 | } |
| 123 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 124 | static int read_index_list(struct sock *sk) |
| 125 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 126 | struct mgmt_rp_read_index_list *rp; |
| 127 | struct list_head *p; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 128 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 129 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 130 | u16 count; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 131 | int i, err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 132 | |
| 133 | BT_DBG("sock %p", sk); |
| 134 | |
| 135 | read_lock(&hci_dev_list_lock); |
| 136 | |
| 137 | count = 0; |
| 138 | list_for_each(p, &hci_dev_list) { |
| 139 | count++; |
| 140 | } |
| 141 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 142 | rp_len = sizeof(*rp) + (2 * count); |
| 143 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 144 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 145 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 146 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 147 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 148 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 149 | put_unaligned_le16(count, &rp->num_controllers); |
| 150 | |
| 151 | i = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 152 | list_for_each_entry(d, &hci_dev_list, list) { |
Johan Hedberg | 3243553 | 2011-11-07 22:16:04 +0200 | [diff] [blame] | 153 | if (test_and_clear_bit(HCI_AUTO_OFF, &d->flags)) |
| 154 | cancel_delayed_work_sync(&d->power_off); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 155 | |
| 156 | if (test_bit(HCI_SETUP, &d->flags)) |
| 157 | continue; |
| 158 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 159 | put_unaligned_le16(d->id, &rp->index[i++]); |
| 160 | BT_DBG("Added hci%u", d->id); |
| 161 | } |
| 162 | |
| 163 | read_unlock(&hci_dev_list_lock); |
| 164 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 165 | err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, rp, |
| 166 | rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 167 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 168 | kfree(rp); |
| 169 | |
| 170 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 171 | } |
| 172 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 173 | static int read_controller_info(struct sock *sk, u16 index) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 174 | { |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 175 | struct mgmt_rp_read_info rp; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 176 | struct hci_dev *hdev; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 177 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 178 | BT_DBG("sock %p hci%u", sk, index); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 179 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 180 | hdev = hci_dev_get(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 181 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 182 | return cmd_status(sk, index, MGMT_OP_READ_INFO, ENODEV); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 183 | |
Johan Hedberg | 3243553 | 2011-11-07 22:16:04 +0200 | [diff] [blame] | 184 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->flags)) |
| 185 | cancel_delayed_work_sync(&hdev->power_off); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 186 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 187 | hci_dev_lock_bh(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 188 | |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 189 | set_bit(HCI_MGMT, &hdev->flags); |
| 190 | |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 191 | memset(&rp, 0, sizeof(rp)); |
| 192 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 193 | rp.type = hdev->dev_type; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 194 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 195 | rp.powered = test_bit(HCI_UP, &hdev->flags); |
| 196 | rp.connectable = test_bit(HCI_PSCAN, &hdev->flags); |
| 197 | rp.discoverable = test_bit(HCI_ISCAN, &hdev->flags); |
| 198 | rp.pairable = test_bit(HCI_PSCAN, &hdev->flags); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 199 | |
| 200 | if (test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 201 | rp.sec_mode = 3; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 202 | else if (hdev->ssp_mode > 0) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 203 | rp.sec_mode = 4; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 204 | else |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 205 | rp.sec_mode = 2; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 206 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 207 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
| 208 | memcpy(rp.features, hdev->features, 8); |
| 209 | memcpy(rp.dev_class, hdev->dev_class, 3); |
| 210 | put_unaligned_le16(hdev->manufacturer, &rp.manufacturer); |
| 211 | rp.hci_ver = hdev->hci_ver; |
| 212 | put_unaligned_le16(hdev->hci_rev, &rp.hci_rev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 213 | |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 214 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
| 215 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 216 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 217 | hci_dev_put(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 218 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 219 | return cmd_complete(sk, index, MGMT_OP_READ_INFO, &rp, sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 220 | } |
| 221 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 222 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 223 | { |
| 224 | sock_put(cmd->sk); |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 225 | kfree(cmd->param); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 226 | kfree(cmd); |
| 227 | } |
| 228 | |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 229 | static struct pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode, |
| 230 | u16 index, void *data, u16 len) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 231 | { |
| 232 | struct pending_cmd *cmd; |
| 233 | |
| 234 | cmd = kmalloc(sizeof(*cmd), GFP_ATOMIC); |
| 235 | if (!cmd) |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 236 | return NULL; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 237 | |
| 238 | cmd->opcode = opcode; |
| 239 | cmd->index = index; |
| 240 | |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 241 | cmd->param = kmalloc(len, GFP_ATOMIC); |
| 242 | if (!cmd->param) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 243 | kfree(cmd); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 244 | return NULL; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Szymon Janc | 8fce635 | 2011-03-22 13:12:20 +0100 | [diff] [blame] | 247 | if (data) |
| 248 | memcpy(cmd->param, data, len); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 249 | |
| 250 | cmd->sk = sk; |
| 251 | sock_hold(sk); |
| 252 | |
| 253 | list_add(&cmd->list, &cmd_list); |
| 254 | |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 255 | return cmd; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 256 | } |
| 257 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 258 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 259 | void (*cb)(struct pending_cmd *cmd, void *data), |
| 260 | void *data) |
| 261 | { |
| 262 | struct list_head *p, *n; |
| 263 | |
| 264 | list_for_each_safe(p, n, &cmd_list) { |
| 265 | struct pending_cmd *cmd; |
| 266 | |
| 267 | cmd = list_entry(p, struct pending_cmd, list); |
| 268 | |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 269 | if (opcode > 0 && cmd->opcode != opcode) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 270 | continue; |
| 271 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 272 | if (hdev && cmd->index != hdev->id) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 273 | continue; |
| 274 | |
| 275 | cb(cmd, data); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | static struct pending_cmd *mgmt_pending_find(u16 opcode, int index) |
| 280 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 281 | struct pending_cmd *cmd; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 282 | |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 283 | list_for_each_entry(cmd, &cmd_list, list) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 284 | if (cmd->opcode != opcode) |
| 285 | continue; |
| 286 | |
| 287 | if (index >= 0 && cmd->index != index) |
| 288 | continue; |
| 289 | |
| 290 | return cmd; |
| 291 | } |
| 292 | |
| 293 | return NULL; |
| 294 | } |
| 295 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 296 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 297 | { |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 298 | list_del(&cmd->list); |
| 299 | mgmt_pending_free(cmd); |
| 300 | } |
| 301 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 302 | static int set_powered(struct sock *sk, u16 index, unsigned char *data, u16 len) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 303 | { |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 304 | struct mgmt_mode *cp; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 305 | struct hci_dev *hdev; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 306 | struct pending_cmd *cmd; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 307 | int err, up; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 308 | |
| 309 | cp = (void *) data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 310 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 311 | BT_DBG("request for hci%u", index); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 312 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 313 | if (len != sizeof(*cp)) |
| 314 | return cmd_status(sk, index, MGMT_OP_SET_POWERED, EINVAL); |
| 315 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 316 | hdev = hci_dev_get(index); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 317 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 318 | return cmd_status(sk, index, MGMT_OP_SET_POWERED, ENODEV); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 319 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 320 | hci_dev_lock_bh(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 321 | |
| 322 | up = test_bit(HCI_UP, &hdev->flags); |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 323 | if ((cp->val && up) || (!cp->val && !up)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 324 | err = cmd_status(sk, index, MGMT_OP_SET_POWERED, EALREADY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 325 | goto failed; |
| 326 | } |
| 327 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 328 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, index)) { |
| 329 | err = cmd_status(sk, index, MGMT_OP_SET_POWERED, EBUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 330 | goto failed; |
| 331 | } |
| 332 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 333 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, index, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 334 | if (!cmd) { |
| 335 | err = -ENOMEM; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 336 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 337 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 338 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 339 | if (cp->val) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 340 | queue_work(hdev->workqueue, &hdev->power_on); |
| 341 | else |
Johan Hedberg | 3243553 | 2011-11-07 22:16:04 +0200 | [diff] [blame] | 342 | queue_work(hdev->workqueue, &hdev->power_off.work); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 343 | |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 344 | err = 0; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 345 | |
| 346 | failed: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 347 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 348 | hci_dev_put(hdev); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 349 | return err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 350 | } |
| 351 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 352 | static int set_discoverable(struct sock *sk, u16 index, unsigned char *data, |
| 353 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 354 | { |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 355 | struct mgmt_cp_set_discoverable *cp; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 356 | struct hci_dev *hdev; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 357 | struct pending_cmd *cmd; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 358 | u8 scan; |
| 359 | int err; |
| 360 | |
| 361 | cp = (void *) data; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 362 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 363 | BT_DBG("request for hci%u", index); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 364 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 365 | if (len != sizeof(*cp)) |
| 366 | return cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, EINVAL); |
| 367 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 368 | hdev = hci_dev_get(index); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 369 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 370 | return cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, ENODEV); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 371 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 372 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 373 | |
| 374 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 375 | err = cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, ENETDOWN); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 376 | goto failed; |
| 377 | } |
| 378 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 379 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, index) || |
| 380 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, index)) { |
| 381 | err = cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, EBUSY); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 382 | goto failed; |
| 383 | } |
| 384 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 385 | if (cp->val == test_bit(HCI_ISCAN, &hdev->flags) && |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 386 | test_bit(HCI_PSCAN, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 387 | err = cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, EALREADY); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 388 | goto failed; |
| 389 | } |
| 390 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 391 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, index, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 392 | if (!cmd) { |
| 393 | err = -ENOMEM; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 394 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 395 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 396 | |
| 397 | scan = SCAN_PAGE; |
| 398 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 399 | if (cp->val) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 400 | scan |= SCAN_INQUIRY; |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 401 | else |
| 402 | cancel_delayed_work_sync(&hdev->discov_off); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 403 | |
| 404 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 405 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 406 | mgmt_pending_remove(cmd); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 407 | |
Johan Hedberg | 16ab91a | 2011-11-07 22:16:02 +0200 | [diff] [blame] | 408 | if (cp->val) |
| 409 | hdev->discov_timeout = get_unaligned_le16(&cp->timeout); |
| 410 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 411 | failed: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 412 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 413 | hci_dev_put(hdev); |
| 414 | |
| 415 | return err; |
| 416 | } |
| 417 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 418 | static int set_connectable(struct sock *sk, u16 index, unsigned char *data, |
| 419 | u16 len) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 420 | { |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 421 | struct mgmt_mode *cp; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 422 | struct hci_dev *hdev; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 423 | struct pending_cmd *cmd; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 424 | u8 scan; |
| 425 | int err; |
| 426 | |
| 427 | cp = (void *) data; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 428 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 429 | BT_DBG("request for hci%u", index); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 430 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 431 | if (len != sizeof(*cp)) |
| 432 | return cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, EINVAL); |
| 433 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 434 | hdev = hci_dev_get(index); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 435 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 436 | return cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, ENODEV); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 437 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 438 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 439 | |
| 440 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 441 | err = cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, ENETDOWN); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 442 | goto failed; |
| 443 | } |
| 444 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 445 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, index) || |
| 446 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, index)) { |
| 447 | err = cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, EBUSY); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 448 | goto failed; |
| 449 | } |
| 450 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 451 | if (cp->val == test_bit(HCI_PSCAN, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 452 | err = cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, EALREADY); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 453 | goto failed; |
| 454 | } |
| 455 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 456 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, index, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 457 | if (!cmd) { |
| 458 | err = -ENOMEM; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 459 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 460 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 461 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 462 | if (cp->val) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 463 | scan = SCAN_PAGE; |
| 464 | else |
| 465 | scan = 0; |
| 466 | |
| 467 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 468 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 469 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 470 | |
| 471 | failed: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 472 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 473 | hci_dev_put(hdev); |
| 474 | |
| 475 | return err; |
| 476 | } |
| 477 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 478 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, |
| 479 | u16 data_len, struct sock *skip_sk) |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 480 | { |
| 481 | struct sk_buff *skb; |
| 482 | struct mgmt_hdr *hdr; |
| 483 | |
| 484 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_ATOMIC); |
| 485 | if (!skb) |
| 486 | return -ENOMEM; |
| 487 | |
| 488 | bt_cb(skb)->channel = HCI_CHANNEL_CONTROL; |
| 489 | |
| 490 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 491 | hdr->opcode = cpu_to_le16(event); |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 492 | if (hdev) |
| 493 | hdr->index = cpu_to_le16(hdev->id); |
| 494 | else |
| 495 | hdr->index = cpu_to_le16(MGMT_INDEX_NONE); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 496 | hdr->len = cpu_to_le16(data_len); |
| 497 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 498 | if (data) |
| 499 | memcpy(skb_put(skb, data_len), data, data_len); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 500 | |
| 501 | hci_send_to_sock(NULL, skb, skip_sk); |
| 502 | kfree_skb(skb); |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 507 | static int send_mode_rsp(struct sock *sk, u16 opcode, u16 index, u8 val) |
| 508 | { |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 509 | struct mgmt_mode rp; |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 510 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 511 | rp.val = val; |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 512 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 513 | return cmd_complete(sk, index, opcode, &rp, sizeof(rp)); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 514 | } |
| 515 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 516 | static int set_pairable(struct sock *sk, u16 index, unsigned char *data, |
| 517 | u16 len) |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 518 | { |
| 519 | struct mgmt_mode *cp, ev; |
| 520 | struct hci_dev *hdev; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 521 | int err; |
| 522 | |
| 523 | cp = (void *) data; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 524 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 525 | BT_DBG("request for hci%u", index); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 526 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 527 | if (len != sizeof(*cp)) |
| 528 | return cmd_status(sk, index, MGMT_OP_SET_PAIRABLE, EINVAL); |
| 529 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 530 | hdev = hci_dev_get(index); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 531 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 532 | return cmd_status(sk, index, MGMT_OP_SET_PAIRABLE, ENODEV); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 533 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 534 | hci_dev_lock_bh(hdev); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 535 | |
| 536 | if (cp->val) |
| 537 | set_bit(HCI_PAIRABLE, &hdev->flags); |
| 538 | else |
| 539 | clear_bit(HCI_PAIRABLE, &hdev->flags); |
| 540 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 541 | err = send_mode_rsp(sk, MGMT_OP_SET_PAIRABLE, index, cp->val); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 542 | if (err < 0) |
| 543 | goto failed; |
| 544 | |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 545 | ev.val = cp->val; |
| 546 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 547 | err = mgmt_event(MGMT_EV_PAIRABLE, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 548 | |
| 549 | failed: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 550 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 551 | hci_dev_put(hdev); |
| 552 | |
| 553 | return err; |
| 554 | } |
| 555 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 556 | #define EIR_FLAGS 0x01 /* flags */ |
| 557 | #define EIR_UUID16_SOME 0x02 /* 16-bit UUID, more available */ |
| 558 | #define EIR_UUID16_ALL 0x03 /* 16-bit UUID, all listed */ |
| 559 | #define EIR_UUID32_SOME 0x04 /* 32-bit UUID, more available */ |
| 560 | #define EIR_UUID32_ALL 0x05 /* 32-bit UUID, all listed */ |
| 561 | #define EIR_UUID128_SOME 0x06 /* 128-bit UUID, more available */ |
| 562 | #define EIR_UUID128_ALL 0x07 /* 128-bit UUID, all listed */ |
| 563 | #define EIR_NAME_SHORT 0x08 /* shortened local name */ |
| 564 | #define EIR_NAME_COMPLETE 0x09 /* complete local name */ |
| 565 | #define EIR_TX_POWER 0x0A /* transmit power level */ |
| 566 | #define EIR_DEVICE_ID 0x10 /* device ID */ |
| 567 | |
| 568 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 569 | |
| 570 | static u8 bluetooth_base_uuid[] = { |
| 571 | 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, |
| 572 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 573 | }; |
| 574 | |
| 575 | static u16 get_uuid16(u8 *uuid128) |
| 576 | { |
| 577 | u32 val; |
| 578 | int i; |
| 579 | |
| 580 | for (i = 0; i < 12; i++) { |
| 581 | if (bluetooth_base_uuid[i] != uuid128[i]) |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | memcpy(&val, &uuid128[12], 4); |
| 586 | |
| 587 | val = le32_to_cpu(val); |
| 588 | if (val > 0xffff) |
| 589 | return 0; |
| 590 | |
| 591 | return (u16) val; |
| 592 | } |
| 593 | |
| 594 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 595 | { |
| 596 | u8 *ptr = data; |
| 597 | u16 eir_len = 0; |
| 598 | u16 uuid16_list[HCI_MAX_EIR_LENGTH / sizeof(u16)]; |
| 599 | int i, truncated = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 600 | struct bt_uuid *uuid; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 601 | size_t name_len; |
| 602 | |
| 603 | name_len = strlen(hdev->dev_name); |
| 604 | |
| 605 | if (name_len > 0) { |
| 606 | /* EIR Data type */ |
| 607 | if (name_len > 48) { |
| 608 | name_len = 48; |
| 609 | ptr[1] = EIR_NAME_SHORT; |
| 610 | } else |
| 611 | ptr[1] = EIR_NAME_COMPLETE; |
| 612 | |
| 613 | /* EIR Data length */ |
| 614 | ptr[0] = name_len + 1; |
| 615 | |
| 616 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 617 | |
| 618 | eir_len += (name_len + 2); |
| 619 | ptr += (name_len + 2); |
| 620 | } |
| 621 | |
| 622 | memset(uuid16_list, 0, sizeof(uuid16_list)); |
| 623 | |
| 624 | /* Group all UUID16 types */ |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 625 | list_for_each_entry(uuid, &hdev->uuids, list) { |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 626 | u16 uuid16; |
| 627 | |
| 628 | uuid16 = get_uuid16(uuid->uuid); |
| 629 | if (uuid16 == 0) |
| 630 | return; |
| 631 | |
| 632 | if (uuid16 < 0x1100) |
| 633 | continue; |
| 634 | |
| 635 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 636 | continue; |
| 637 | |
| 638 | /* Stop if not enough space to put next UUID */ |
| 639 | if (eir_len + 2 + sizeof(u16) > HCI_MAX_EIR_LENGTH) { |
| 640 | truncated = 1; |
| 641 | break; |
| 642 | } |
| 643 | |
| 644 | /* Check for duplicates */ |
| 645 | for (i = 0; uuid16_list[i] != 0; i++) |
| 646 | if (uuid16_list[i] == uuid16) |
| 647 | break; |
| 648 | |
| 649 | if (uuid16_list[i] == 0) { |
| 650 | uuid16_list[i] = uuid16; |
| 651 | eir_len += sizeof(u16); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | if (uuid16_list[0] != 0) { |
| 656 | u8 *length = ptr; |
| 657 | |
| 658 | /* EIR Data type */ |
| 659 | ptr[1] = truncated ? EIR_UUID16_SOME : EIR_UUID16_ALL; |
| 660 | |
| 661 | ptr += 2; |
| 662 | eir_len += 2; |
| 663 | |
| 664 | for (i = 0; uuid16_list[i] != 0; i++) { |
| 665 | *ptr++ = (uuid16_list[i] & 0x00ff); |
| 666 | *ptr++ = (uuid16_list[i] & 0xff00) >> 8; |
| 667 | } |
| 668 | |
| 669 | /* EIR Data length */ |
| 670 | *length = (i * sizeof(u16)) + 1; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | static int update_eir(struct hci_dev *hdev) |
| 675 | { |
| 676 | struct hci_cp_write_eir cp; |
| 677 | |
| 678 | if (!(hdev->features[6] & LMP_EXT_INQ)) |
| 679 | return 0; |
| 680 | |
| 681 | if (hdev->ssp_mode == 0) |
| 682 | return 0; |
| 683 | |
| 684 | if (test_bit(HCI_SERVICE_CACHE, &hdev->flags)) |
| 685 | return 0; |
| 686 | |
| 687 | memset(&cp, 0, sizeof(cp)); |
| 688 | |
| 689 | create_eir(hdev, cp.data); |
| 690 | |
| 691 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
| 692 | return 0; |
| 693 | |
| 694 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 695 | |
| 696 | return hci_send_cmd(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
| 697 | } |
| 698 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 699 | static u8 get_service_classes(struct hci_dev *hdev) |
| 700 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 701 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 702 | u8 val = 0; |
| 703 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 704 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 705 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 706 | |
| 707 | return val; |
| 708 | } |
| 709 | |
| 710 | static int update_class(struct hci_dev *hdev) |
| 711 | { |
| 712 | u8 cod[3]; |
| 713 | |
| 714 | BT_DBG("%s", hdev->name); |
| 715 | |
| 716 | if (test_bit(HCI_SERVICE_CACHE, &hdev->flags)) |
| 717 | return 0; |
| 718 | |
| 719 | cod[0] = hdev->minor_class; |
| 720 | cod[1] = hdev->major_class; |
| 721 | cod[2] = get_service_classes(hdev); |
| 722 | |
| 723 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
| 724 | return 0; |
| 725 | |
| 726 | return hci_send_cmd(hdev, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod); |
| 727 | } |
| 728 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 729 | static int add_uuid(struct sock *sk, u16 index, unsigned char *data, u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 730 | { |
| 731 | struct mgmt_cp_add_uuid *cp; |
| 732 | struct hci_dev *hdev; |
| 733 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 734 | int err; |
| 735 | |
| 736 | cp = (void *) data; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 737 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 738 | BT_DBG("request for hci%u", index); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 739 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 740 | if (len != sizeof(*cp)) |
| 741 | return cmd_status(sk, index, MGMT_OP_ADD_UUID, EINVAL); |
| 742 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 743 | hdev = hci_dev_get(index); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 744 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 745 | return cmd_status(sk, index, MGMT_OP_ADD_UUID, ENODEV); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 746 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 747 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 748 | |
| 749 | uuid = kmalloc(sizeof(*uuid), GFP_ATOMIC); |
| 750 | if (!uuid) { |
| 751 | err = -ENOMEM; |
| 752 | goto failed; |
| 753 | } |
| 754 | |
| 755 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 756 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 757 | |
| 758 | list_add(&uuid->list, &hdev->uuids); |
| 759 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 760 | err = update_class(hdev); |
| 761 | if (err < 0) |
| 762 | goto failed; |
| 763 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 764 | err = update_eir(hdev); |
| 765 | if (err < 0) |
| 766 | goto failed; |
| 767 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 768 | err = cmd_complete(sk, index, MGMT_OP_ADD_UUID, NULL, 0); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 769 | |
| 770 | failed: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 771 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 772 | hci_dev_put(hdev); |
| 773 | |
| 774 | return err; |
| 775 | } |
| 776 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 777 | static int remove_uuid(struct sock *sk, u16 index, unsigned char *data, u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 778 | { |
| 779 | struct list_head *p, *n; |
Szymon Janc | 779cb85 | 2011-02-25 19:05:47 +0100 | [diff] [blame] | 780 | struct mgmt_cp_remove_uuid *cp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 781 | struct hci_dev *hdev; |
| 782 | u8 bt_uuid_any[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 783 | int err, found; |
| 784 | |
| 785 | cp = (void *) data; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 786 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 787 | BT_DBG("request for hci%u", index); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 788 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 789 | if (len != sizeof(*cp)) |
| 790 | return cmd_status(sk, index, MGMT_OP_REMOVE_UUID, EINVAL); |
| 791 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 792 | hdev = hci_dev_get(index); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 793 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 794 | return cmd_status(sk, index, MGMT_OP_REMOVE_UUID, ENODEV); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 795 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 796 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 797 | |
| 798 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
| 799 | err = hci_uuids_clear(hdev); |
| 800 | goto unlock; |
| 801 | } |
| 802 | |
| 803 | found = 0; |
| 804 | |
| 805 | list_for_each_safe(p, n, &hdev->uuids) { |
| 806 | struct bt_uuid *match = list_entry(p, struct bt_uuid, list); |
| 807 | |
| 808 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 809 | continue; |
| 810 | |
| 811 | list_del(&match->list); |
| 812 | found++; |
| 813 | } |
| 814 | |
| 815 | if (found == 0) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 816 | err = cmd_status(sk, index, MGMT_OP_REMOVE_UUID, ENOENT); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 817 | goto unlock; |
| 818 | } |
| 819 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 820 | err = update_class(hdev); |
| 821 | if (err < 0) |
| 822 | goto unlock; |
| 823 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 824 | err = update_eir(hdev); |
| 825 | if (err < 0) |
| 826 | goto unlock; |
| 827 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 828 | err = cmd_complete(sk, index, MGMT_OP_REMOVE_UUID, NULL, 0); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 829 | |
| 830 | unlock: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 831 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 832 | hci_dev_put(hdev); |
| 833 | |
| 834 | return err; |
| 835 | } |
| 836 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 837 | static int set_dev_class(struct sock *sk, u16 index, unsigned char *data, |
| 838 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 839 | { |
| 840 | struct hci_dev *hdev; |
| 841 | struct mgmt_cp_set_dev_class *cp; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 842 | int err; |
| 843 | |
| 844 | cp = (void *) data; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 845 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 846 | BT_DBG("request for hci%u", index); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 847 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 848 | if (len != sizeof(*cp)) |
| 849 | return cmd_status(sk, index, MGMT_OP_SET_DEV_CLASS, EINVAL); |
| 850 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 851 | hdev = hci_dev_get(index); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 852 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 853 | return cmd_status(sk, index, MGMT_OP_SET_DEV_CLASS, ENODEV); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 854 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 855 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 856 | |
| 857 | hdev->major_class = cp->major; |
| 858 | hdev->minor_class = cp->minor; |
| 859 | |
| 860 | err = update_class(hdev); |
| 861 | |
| 862 | if (err == 0) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 863 | err = cmd_complete(sk, index, MGMT_OP_SET_DEV_CLASS, NULL, 0); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 864 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 865 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 866 | hci_dev_put(hdev); |
| 867 | |
| 868 | return err; |
| 869 | } |
| 870 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 871 | static int set_service_cache(struct sock *sk, u16 index, unsigned char *data, |
| 872 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 873 | { |
| 874 | struct hci_dev *hdev; |
| 875 | struct mgmt_cp_set_service_cache *cp; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 876 | int err; |
| 877 | |
| 878 | cp = (void *) data; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 879 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 880 | if (len != sizeof(*cp)) |
Szymon Janc | b8534e0 | 2011-03-01 16:55:34 +0100 | [diff] [blame] | 881 | return cmd_status(sk, index, MGMT_OP_SET_SERVICE_CACHE, EINVAL); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 882 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 883 | hdev = hci_dev_get(index); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 884 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 885 | return cmd_status(sk, index, MGMT_OP_SET_SERVICE_CACHE, ENODEV); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 886 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 887 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 888 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 889 | BT_DBG("hci%u enable %d", index, cp->enable); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 890 | |
| 891 | if (cp->enable) { |
| 892 | set_bit(HCI_SERVICE_CACHE, &hdev->flags); |
| 893 | err = 0; |
| 894 | } else { |
| 895 | clear_bit(HCI_SERVICE_CACHE, &hdev->flags); |
| 896 | err = update_class(hdev); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 897 | if (err == 0) |
| 898 | err = update_eir(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | if (err == 0) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 902 | err = cmd_complete(sk, index, MGMT_OP_SET_SERVICE_CACHE, NULL, |
| 903 | 0); |
Gustavo F. Padovan | e5b82e5 | 2011-10-15 18:03:15 -0300 | [diff] [blame] | 904 | else |
| 905 | cmd_status(sk, index, MGMT_OP_SET_SERVICE_CACHE, -err); |
| 906 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 907 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 908 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 909 | hci_dev_put(hdev); |
| 910 | |
| 911 | return err; |
| 912 | } |
| 913 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 914 | static int load_link_keys(struct sock *sk, u16 index, unsigned char *data, |
| 915 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 916 | { |
| 917 | struct hci_dev *hdev; |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 918 | struct mgmt_cp_load_link_keys *cp; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 919 | u16 key_count, expected_len; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 920 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 921 | |
| 922 | cp = (void *) data; |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 923 | |
| 924 | if (len < sizeof(*cp)) |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 925 | return cmd_status(sk, index, MGMT_OP_LOAD_LINK_KEYS, EINVAL); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 926 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 927 | key_count = get_unaligned_le16(&cp->key_count); |
| 928 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 929 | expected_len = sizeof(*cp) + key_count * |
| 930 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 931 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 932 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 933 | len, expected_len); |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 934 | return cmd_status(sk, index, MGMT_OP_LOAD_LINK_KEYS, EINVAL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 935 | } |
| 936 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 937 | hdev = hci_dev_get(index); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 938 | if (!hdev) |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 939 | return cmd_status(sk, index, MGMT_OP_LOAD_LINK_KEYS, ENODEV); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 940 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 941 | BT_DBG("hci%u debug_keys %u key_count %u", index, cp->debug_keys, |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 942 | key_count); |
| 943 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 944 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 945 | |
| 946 | hci_link_keys_clear(hdev); |
| 947 | |
| 948 | set_bit(HCI_LINK_KEYS, &hdev->flags); |
| 949 | |
| 950 | if (cp->debug_keys) |
| 951 | set_bit(HCI_DEBUG_KEYS, &hdev->flags); |
| 952 | else |
| 953 | clear_bit(HCI_DEBUG_KEYS, &hdev->flags); |
| 954 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 955 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 956 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 957 | |
Johan Hedberg | d25e28a | 2011-04-28 11:28:59 -0700 | [diff] [blame] | 958 | hci_add_link_key(hdev, NULL, 0, &key->bdaddr, key->val, key->type, |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 959 | key->pin_len); |
| 960 | } |
| 961 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 962 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 963 | hci_dev_put(hdev); |
| 964 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 965 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 966 | } |
| 967 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 968 | static int remove_keys(struct sock *sk, u16 index, unsigned char *data, |
| 969 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 970 | { |
| 971 | struct hci_dev *hdev; |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 972 | struct mgmt_cp_remove_keys *cp; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 973 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 974 | int err; |
| 975 | |
| 976 | cp = (void *) data; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 977 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 978 | if (len != sizeof(*cp)) |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 979 | return cmd_status(sk, index, MGMT_OP_REMOVE_KEYS, EINVAL); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 980 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 981 | hdev = hci_dev_get(index); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 982 | if (!hdev) |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 983 | return cmd_status(sk, index, MGMT_OP_REMOVE_KEYS, ENODEV); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 984 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 985 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 986 | |
| 987 | err = hci_remove_link_key(hdev, &cp->bdaddr); |
| 988 | if (err < 0) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 989 | err = cmd_status(sk, index, MGMT_OP_REMOVE_KEYS, -err); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 990 | goto unlock; |
| 991 | } |
| 992 | |
| 993 | err = 0; |
| 994 | |
| 995 | if (!test_bit(HCI_UP, &hdev->flags) || !cp->disconnect) |
| 996 | goto unlock; |
| 997 | |
| 998 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr); |
| 999 | if (conn) { |
| 1000 | struct hci_cp_disconnect dc; |
| 1001 | |
| 1002 | put_unaligned_le16(conn->handle, &dc.handle); |
| 1003 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
Anderson Lizardo | 94ac027 | 2011-06-13 15:42:03 -0400 | [diff] [blame] | 1004 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | unlock: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1008 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1009 | hci_dev_put(hdev); |
| 1010 | |
| 1011 | return err; |
| 1012 | } |
| 1013 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1014 | static int disconnect(struct sock *sk, u16 index, unsigned char *data, u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1015 | { |
| 1016 | struct hci_dev *hdev; |
| 1017 | struct mgmt_cp_disconnect *cp; |
| 1018 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1019 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1020 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1021 | int err; |
| 1022 | |
| 1023 | BT_DBG(""); |
| 1024 | |
| 1025 | cp = (void *) data; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1026 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1027 | if (len != sizeof(*cp)) |
| 1028 | return cmd_status(sk, index, MGMT_OP_DISCONNECT, EINVAL); |
| 1029 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1030 | hdev = hci_dev_get(index); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1031 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1032 | return cmd_status(sk, index, MGMT_OP_DISCONNECT, ENODEV); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1033 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1034 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1035 | |
| 1036 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1037 | err = cmd_status(sk, index, MGMT_OP_DISCONNECT, ENETDOWN); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1038 | goto failed; |
| 1039 | } |
| 1040 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1041 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, index)) { |
| 1042 | err = cmd_status(sk, index, MGMT_OP_DISCONNECT, EBUSY); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1043 | goto failed; |
| 1044 | } |
| 1045 | |
| 1046 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr); |
Vinicius Costa Gomes | 365227e | 2011-05-06 18:41:44 -0300 | [diff] [blame] | 1047 | if (!conn) |
| 1048 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->bdaddr); |
| 1049 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1050 | if (!conn) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1051 | err = cmd_status(sk, index, MGMT_OP_DISCONNECT, ENOTCONN); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1052 | goto failed; |
| 1053 | } |
| 1054 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1055 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, index, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1056 | if (!cmd) { |
| 1057 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1058 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1059 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1060 | |
| 1061 | put_unaligned_le16(conn->handle, &dc.handle); |
| 1062 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 1063 | |
| 1064 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1065 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1066 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1067 | |
| 1068 | failed: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1069 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1070 | hci_dev_put(hdev); |
| 1071 | |
| 1072 | return err; |
| 1073 | } |
| 1074 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1075 | static u8 link_to_mgmt(u8 link_type) |
| 1076 | { |
| 1077 | switch (link_type) { |
| 1078 | case LE_LINK: |
| 1079 | return MGMT_ADDR_LE; |
| 1080 | case ACL_LINK: |
| 1081 | return MGMT_ADDR_BREDR; |
| 1082 | default: |
| 1083 | return MGMT_ADDR_INVALID; |
| 1084 | } |
| 1085 | } |
| 1086 | |
Szymon Janc | 8ce6284 | 2011-03-01 16:55:32 +0100 | [diff] [blame] | 1087 | static int get_connections(struct sock *sk, u16 index) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1088 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1089 | struct mgmt_rp_get_connections *rp; |
| 1090 | struct hci_dev *hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1091 | struct hci_conn *c; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1092 | struct list_head *p; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 1093 | size_t rp_len; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1094 | u16 count; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1095 | int i, err; |
| 1096 | |
| 1097 | BT_DBG(""); |
| 1098 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1099 | hdev = hci_dev_get(index); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1100 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1101 | return cmd_status(sk, index, MGMT_OP_GET_CONNECTIONS, ENODEV); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1102 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1103 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1104 | |
| 1105 | count = 0; |
| 1106 | list_for_each(p, &hdev->conn_hash.list) { |
| 1107 | count++; |
| 1108 | } |
| 1109 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1110 | rp_len = sizeof(*rp) + (count * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 1111 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 1112 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1113 | err = -ENOMEM; |
| 1114 | goto unlock; |
| 1115 | } |
| 1116 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1117 | put_unaligned_le16(count, &rp->conn_count); |
| 1118 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1119 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1120 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 1121 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
| 1122 | rp->addr[i].type = link_to_mgmt(c->type); |
| 1123 | if (rp->addr[i].type == MGMT_ADDR_INVALID) |
| 1124 | continue; |
| 1125 | i++; |
| 1126 | } |
| 1127 | |
| 1128 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 1129 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1130 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1131 | err = cmd_complete(sk, index, MGMT_OP_GET_CONNECTIONS, rp, rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1132 | |
| 1133 | unlock: |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 1134 | kfree(rp); |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1135 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1136 | hci_dev_put(hdev); |
| 1137 | return err; |
| 1138 | } |
| 1139 | |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1140 | static int send_pin_code_neg_reply(struct sock *sk, u16 index, |
| 1141 | struct hci_dev *hdev, struct mgmt_cp_pin_code_neg_reply *cp) |
| 1142 | { |
| 1143 | struct pending_cmd *cmd; |
| 1144 | int err; |
| 1145 | |
| 1146 | cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, index, cp, |
| 1147 | sizeof(*cp)); |
| 1148 | if (!cmd) |
| 1149 | return -ENOMEM; |
| 1150 | |
| 1151 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, sizeof(cp->bdaddr), |
| 1152 | &cp->bdaddr); |
| 1153 | if (err < 0) |
| 1154 | mgmt_pending_remove(cmd); |
| 1155 | |
| 1156 | return err; |
| 1157 | } |
| 1158 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1159 | static int pin_code_reply(struct sock *sk, u16 index, unsigned char *data, |
| 1160 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1161 | { |
| 1162 | struct hci_dev *hdev; |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1163 | struct hci_conn *conn; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1164 | struct mgmt_cp_pin_code_reply *cp; |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1165 | struct mgmt_cp_pin_code_neg_reply ncp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1166 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1167 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1168 | int err; |
| 1169 | |
| 1170 | BT_DBG(""); |
| 1171 | |
| 1172 | cp = (void *) data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1173 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1174 | if (len != sizeof(*cp)) |
| 1175 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, EINVAL); |
| 1176 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1177 | hdev = hci_dev_get(index); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1178 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1179 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, ENODEV); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1180 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1181 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1182 | |
| 1183 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1184 | err = cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, ENETDOWN); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1185 | goto failed; |
| 1186 | } |
| 1187 | |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1188 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr); |
| 1189 | if (!conn) { |
| 1190 | err = cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, ENOTCONN); |
| 1191 | goto failed; |
| 1192 | } |
| 1193 | |
| 1194 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
| 1195 | bacpy(&ncp.bdaddr, &cp->bdaddr); |
| 1196 | |
| 1197 | BT_ERR("PIN code is not 16 bytes long"); |
| 1198 | |
| 1199 | err = send_pin_code_neg_reply(sk, index, hdev, &ncp); |
| 1200 | if (err >= 0) |
| 1201 | err = cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, |
| 1202 | EINVAL); |
| 1203 | |
| 1204 | goto failed; |
| 1205 | } |
| 1206 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1207 | cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, index, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1208 | if (!cmd) { |
| 1209 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1210 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1211 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1212 | |
| 1213 | bacpy(&reply.bdaddr, &cp->bdaddr); |
| 1214 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 1215 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1216 | |
| 1217 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 1218 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1219 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1220 | |
| 1221 | failed: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1222 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1223 | hci_dev_put(hdev); |
| 1224 | |
| 1225 | return err; |
| 1226 | } |
| 1227 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1228 | static int pin_code_neg_reply(struct sock *sk, u16 index, unsigned char *data, |
| 1229 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1230 | { |
| 1231 | struct hci_dev *hdev; |
| 1232 | struct mgmt_cp_pin_code_neg_reply *cp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1233 | int err; |
| 1234 | |
| 1235 | BT_DBG(""); |
| 1236 | |
| 1237 | cp = (void *) data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1238 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1239 | if (len != sizeof(*cp)) |
| 1240 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 1241 | EINVAL); |
| 1242 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1243 | hdev = hci_dev_get(index); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1244 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1245 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 1246 | ENODEV); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1247 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1248 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1249 | |
| 1250 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1251 | err = cmd_status(sk, index, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 1252 | ENETDOWN); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1253 | goto failed; |
| 1254 | } |
| 1255 | |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1256 | err = send_pin_code_neg_reply(sk, index, hdev, cp); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1257 | |
| 1258 | failed: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1259 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1260 | hci_dev_put(hdev); |
| 1261 | |
| 1262 | return err; |
| 1263 | } |
| 1264 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1265 | static int set_io_capability(struct sock *sk, u16 index, unsigned char *data, |
| 1266 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1267 | { |
| 1268 | struct hci_dev *hdev; |
| 1269 | struct mgmt_cp_set_io_capability *cp; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1270 | |
| 1271 | BT_DBG(""); |
| 1272 | |
| 1273 | cp = (void *) data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1274 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1275 | if (len != sizeof(*cp)) |
Szymon Janc | b8534e0 | 2011-03-01 16:55:34 +0100 | [diff] [blame] | 1276 | return cmd_status(sk, index, MGMT_OP_SET_IO_CAPABILITY, EINVAL); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1277 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1278 | hdev = hci_dev_get(index); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1279 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1280 | return cmd_status(sk, index, MGMT_OP_SET_IO_CAPABILITY, ENODEV); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1281 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1282 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1283 | |
| 1284 | hdev->io_capability = cp->io_capability; |
| 1285 | |
| 1286 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Szymon Janc | b8534e0 | 2011-03-01 16:55:34 +0100 | [diff] [blame] | 1287 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1288 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1289 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1290 | hci_dev_put(hdev); |
| 1291 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1292 | return cmd_complete(sk, index, MGMT_OP_SET_IO_CAPABILITY, NULL, 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1293 | } |
| 1294 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1295 | static inline struct pending_cmd *find_pairing(struct hci_conn *conn) |
| 1296 | { |
| 1297 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1298 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1299 | |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1300 | list_for_each_entry(cmd, &cmd_list, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1301 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 1302 | continue; |
| 1303 | |
| 1304 | if (cmd->index != hdev->id) |
| 1305 | continue; |
| 1306 | |
| 1307 | if (cmd->user_data != conn) |
| 1308 | continue; |
| 1309 | |
| 1310 | return cmd; |
| 1311 | } |
| 1312 | |
| 1313 | return NULL; |
| 1314 | } |
| 1315 | |
| 1316 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 1317 | { |
| 1318 | struct mgmt_rp_pair_device rp; |
| 1319 | struct hci_conn *conn = cmd->user_data; |
| 1320 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1321 | bacpy(&rp.bdaddr, &conn->dst); |
| 1322 | rp.status = status; |
| 1323 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1324 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1325 | |
| 1326 | /* So we don't get further callbacks for this connection */ |
| 1327 | conn->connect_cfm_cb = NULL; |
| 1328 | conn->security_cfm_cb = NULL; |
| 1329 | conn->disconn_cfm_cb = NULL; |
| 1330 | |
| 1331 | hci_conn_put(conn); |
| 1332 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1333 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 1337 | { |
| 1338 | struct pending_cmd *cmd; |
| 1339 | |
| 1340 | BT_DBG("status %u", status); |
| 1341 | |
| 1342 | cmd = find_pairing(conn); |
| 1343 | if (!cmd) { |
| 1344 | BT_DBG("Unable to find a pending command"); |
| 1345 | return; |
| 1346 | } |
| 1347 | |
| 1348 | pairing_complete(cmd, status); |
| 1349 | } |
| 1350 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1351 | static int pair_device(struct sock *sk, u16 index, unsigned char *data, u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1352 | { |
| 1353 | struct hci_dev *hdev; |
| 1354 | struct mgmt_cp_pair_device *cp; |
| 1355 | struct pending_cmd *cmd; |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 1356 | struct adv_entry *entry; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1357 | u8 sec_level, auth_type; |
| 1358 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1359 | int err; |
| 1360 | |
| 1361 | BT_DBG(""); |
| 1362 | |
| 1363 | cp = (void *) data; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1364 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1365 | if (len != sizeof(*cp)) |
| 1366 | return cmd_status(sk, index, MGMT_OP_PAIR_DEVICE, EINVAL); |
| 1367 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1368 | hdev = hci_dev_get(index); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1369 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1370 | return cmd_status(sk, index, MGMT_OP_PAIR_DEVICE, ENODEV); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1371 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1372 | hci_dev_lock_bh(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1373 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 1374 | sec_level = BT_SECURITY_MEDIUM; |
| 1375 | if (cp->io_cap == 0x03) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1376 | auth_type = HCI_AT_DEDICATED_BONDING; |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 1377 | else |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1378 | auth_type = HCI_AT_DEDICATED_BONDING_MITM; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1379 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 1380 | entry = hci_find_adv_entry(hdev, &cp->bdaddr); |
| 1381 | if (entry) |
| 1382 | conn = hci_connect(hdev, LE_LINK, &cp->bdaddr, sec_level, |
| 1383 | auth_type); |
| 1384 | else |
| 1385 | conn = hci_connect(hdev, ACL_LINK, &cp->bdaddr, sec_level, |
| 1386 | auth_type); |
| 1387 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1388 | if (IS_ERR(conn)) { |
| 1389 | err = PTR_ERR(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1390 | goto unlock; |
| 1391 | } |
| 1392 | |
| 1393 | if (conn->connect_cfm_cb) { |
| 1394 | hci_conn_put(conn); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1395 | err = cmd_status(sk, index, MGMT_OP_PAIR_DEVICE, EBUSY); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1396 | goto unlock; |
| 1397 | } |
| 1398 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1399 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, index, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1400 | if (!cmd) { |
| 1401 | err = -ENOMEM; |
| 1402 | hci_conn_put(conn); |
| 1403 | goto unlock; |
| 1404 | } |
| 1405 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 1406 | /* For LE, just connecting isn't a proof that the pairing finished */ |
| 1407 | if (!entry) |
| 1408 | conn->connect_cfm_cb = pairing_complete_cb; |
| 1409 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1410 | conn->security_cfm_cb = pairing_complete_cb; |
| 1411 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 1412 | conn->io_capability = cp->io_cap; |
| 1413 | cmd->user_data = conn; |
| 1414 | |
| 1415 | if (conn->state == BT_CONNECTED && |
| 1416 | hci_conn_security(conn, sec_level, auth_type)) |
| 1417 | pairing_complete(cmd, 0); |
| 1418 | |
| 1419 | err = 0; |
| 1420 | |
| 1421 | unlock: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1422 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1423 | hci_dev_put(hdev); |
| 1424 | |
| 1425 | return err; |
| 1426 | } |
| 1427 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1428 | static int user_confirm_reply(struct sock *sk, u16 index, unsigned char *data, |
| 1429 | u16 len, int success) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1430 | { |
| 1431 | struct mgmt_cp_user_confirm_reply *cp = (void *) data; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1432 | u16 mgmt_op, hci_op; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1433 | struct pending_cmd *cmd; |
| 1434 | struct hci_dev *hdev; |
| 1435 | int err; |
| 1436 | |
| 1437 | BT_DBG(""); |
| 1438 | |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1439 | if (success) { |
| 1440 | mgmt_op = MGMT_OP_USER_CONFIRM_REPLY; |
| 1441 | hci_op = HCI_OP_USER_CONFIRM_REPLY; |
| 1442 | } else { |
| 1443 | mgmt_op = MGMT_OP_USER_CONFIRM_NEG_REPLY; |
| 1444 | hci_op = HCI_OP_USER_CONFIRM_NEG_REPLY; |
| 1445 | } |
| 1446 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1447 | if (len != sizeof(*cp)) |
| 1448 | return cmd_status(sk, index, mgmt_op, EINVAL); |
| 1449 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1450 | hdev = hci_dev_get(index); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1451 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1452 | return cmd_status(sk, index, mgmt_op, ENODEV); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1453 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1454 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 1455 | |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1456 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1457 | err = cmd_status(sk, index, mgmt_op, ENETDOWN); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1458 | goto failed; |
| 1459 | } |
| 1460 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1461 | cmd = mgmt_pending_add(sk, mgmt_op, index, data, len); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1462 | if (!cmd) { |
| 1463 | err = -ENOMEM; |
| 1464 | goto failed; |
| 1465 | } |
| 1466 | |
| 1467 | err = hci_send_cmd(hdev, hci_op, sizeof(cp->bdaddr), &cp->bdaddr); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1468 | if (err < 0) |
| 1469 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1470 | |
| 1471 | failed: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1472 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1473 | hci_dev_put(hdev); |
| 1474 | |
| 1475 | return err; |
| 1476 | } |
| 1477 | |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 1478 | static int set_local_name(struct sock *sk, u16 index, unsigned char *data, |
| 1479 | u16 len) |
| 1480 | { |
| 1481 | struct mgmt_cp_set_local_name *mgmt_cp = (void *) data; |
| 1482 | struct hci_cp_write_local_name hci_cp; |
| 1483 | struct hci_dev *hdev; |
| 1484 | struct pending_cmd *cmd; |
| 1485 | int err; |
| 1486 | |
| 1487 | BT_DBG(""); |
| 1488 | |
| 1489 | if (len != sizeof(*mgmt_cp)) |
| 1490 | return cmd_status(sk, index, MGMT_OP_SET_LOCAL_NAME, EINVAL); |
| 1491 | |
| 1492 | hdev = hci_dev_get(index); |
| 1493 | if (!hdev) |
| 1494 | return cmd_status(sk, index, MGMT_OP_SET_LOCAL_NAME, ENODEV); |
| 1495 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1496 | hci_dev_lock_bh(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 1497 | |
| 1498 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, index, data, len); |
| 1499 | if (!cmd) { |
| 1500 | err = -ENOMEM; |
| 1501 | goto failed; |
| 1502 | } |
| 1503 | |
| 1504 | memcpy(hci_cp.name, mgmt_cp->name, sizeof(hci_cp.name)); |
| 1505 | err = hci_send_cmd(hdev, HCI_OP_WRITE_LOCAL_NAME, sizeof(hci_cp), |
| 1506 | &hci_cp); |
| 1507 | if (err < 0) |
| 1508 | mgmt_pending_remove(cmd); |
| 1509 | |
| 1510 | failed: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1511 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 1512 | hci_dev_put(hdev); |
| 1513 | |
| 1514 | return err; |
| 1515 | } |
| 1516 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 1517 | static int read_local_oob_data(struct sock *sk, u16 index) |
| 1518 | { |
| 1519 | struct hci_dev *hdev; |
| 1520 | struct pending_cmd *cmd; |
| 1521 | int err; |
| 1522 | |
| 1523 | BT_DBG("hci%u", index); |
| 1524 | |
| 1525 | hdev = hci_dev_get(index); |
| 1526 | if (!hdev) |
| 1527 | return cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 1528 | ENODEV); |
| 1529 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1530 | hci_dev_lock_bh(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 1531 | |
| 1532 | if (!test_bit(HCI_UP, &hdev->flags)) { |
| 1533 | err = cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 1534 | ENETDOWN); |
| 1535 | goto unlock; |
| 1536 | } |
| 1537 | |
| 1538 | if (!(hdev->features[6] & LMP_SIMPLE_PAIR)) { |
| 1539 | err = cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 1540 | EOPNOTSUPP); |
| 1541 | goto unlock; |
| 1542 | } |
| 1543 | |
| 1544 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, index)) { |
| 1545 | err = cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, EBUSY); |
| 1546 | goto unlock; |
| 1547 | } |
| 1548 | |
| 1549 | cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, index, NULL, 0); |
| 1550 | if (!cmd) { |
| 1551 | err = -ENOMEM; |
| 1552 | goto unlock; |
| 1553 | } |
| 1554 | |
| 1555 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 1556 | if (err < 0) |
| 1557 | mgmt_pending_remove(cmd); |
| 1558 | |
| 1559 | unlock: |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1560 | hci_dev_unlock_bh(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 1561 | hci_dev_put(hdev); |
| 1562 | |
| 1563 | return err; |
| 1564 | } |
| 1565 | |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1566 | static int add_remote_oob_data(struct sock *sk, u16 index, unsigned char *data, |
| 1567 | u16 len) |
| 1568 | { |
| 1569 | struct hci_dev *hdev; |
| 1570 | struct mgmt_cp_add_remote_oob_data *cp = (void *) data; |
| 1571 | int err; |
| 1572 | |
| 1573 | BT_DBG("hci%u ", index); |
| 1574 | |
| 1575 | if (len != sizeof(*cp)) |
| 1576 | return cmd_status(sk, index, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 1577 | EINVAL); |
| 1578 | |
| 1579 | hdev = hci_dev_get(index); |
| 1580 | if (!hdev) |
| 1581 | return cmd_status(sk, index, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 1582 | ENODEV); |
| 1583 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1584 | hci_dev_lock_bh(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1585 | |
| 1586 | err = hci_add_remote_oob_data(hdev, &cp->bdaddr, cp->hash, |
| 1587 | cp->randomizer); |
| 1588 | if (err < 0) |
| 1589 | err = cmd_status(sk, index, MGMT_OP_ADD_REMOTE_OOB_DATA, -err); |
| 1590 | else |
| 1591 | err = cmd_complete(sk, index, MGMT_OP_ADD_REMOTE_OOB_DATA, NULL, |
| 1592 | 0); |
| 1593 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1594 | hci_dev_unlock_bh(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1595 | hci_dev_put(hdev); |
| 1596 | |
| 1597 | return err; |
| 1598 | } |
| 1599 | |
| 1600 | static int remove_remote_oob_data(struct sock *sk, u16 index, |
| 1601 | unsigned char *data, u16 len) |
| 1602 | { |
| 1603 | struct hci_dev *hdev; |
| 1604 | struct mgmt_cp_remove_remote_oob_data *cp = (void *) data; |
| 1605 | int err; |
| 1606 | |
| 1607 | BT_DBG("hci%u ", index); |
| 1608 | |
| 1609 | if (len != sizeof(*cp)) |
| 1610 | return cmd_status(sk, index, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 1611 | EINVAL); |
| 1612 | |
| 1613 | hdev = hci_dev_get(index); |
| 1614 | if (!hdev) |
| 1615 | return cmd_status(sk, index, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 1616 | ENODEV); |
| 1617 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1618 | hci_dev_lock_bh(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1619 | |
| 1620 | err = hci_remove_remote_oob_data(hdev, &cp->bdaddr); |
| 1621 | if (err < 0) |
| 1622 | err = cmd_status(sk, index, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 1623 | -err); |
| 1624 | else |
| 1625 | err = cmd_complete(sk, index, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 1626 | NULL, 0); |
| 1627 | |
Andre Guedes | 8c156c3 | 2011-07-07 10:30:36 -0300 | [diff] [blame] | 1628 | hci_dev_unlock_bh(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1629 | hci_dev_put(hdev); |
| 1630 | |
| 1631 | return err; |
| 1632 | } |
| 1633 | |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 1634 | static int start_discovery(struct sock *sk, u16 index) |
| 1635 | { |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 1636 | struct pending_cmd *cmd; |
| 1637 | struct hci_dev *hdev; |
| 1638 | int err; |
| 1639 | |
| 1640 | BT_DBG("hci%u", index); |
| 1641 | |
| 1642 | hdev = hci_dev_get(index); |
| 1643 | if (!hdev) |
| 1644 | return cmd_status(sk, index, MGMT_OP_START_DISCOVERY, ENODEV); |
| 1645 | |
| 1646 | hci_dev_lock_bh(hdev); |
| 1647 | |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 1648 | if (!test_bit(HCI_UP, &hdev->flags)) { |
| 1649 | err = cmd_status(sk, index, MGMT_OP_START_DISCOVERY, ENETDOWN); |
| 1650 | goto failed; |
| 1651 | } |
| 1652 | |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 1653 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, index, NULL, 0); |
| 1654 | if (!cmd) { |
| 1655 | err = -ENOMEM; |
| 1656 | goto failed; |
| 1657 | } |
| 1658 | |
Andre Guedes | 2519a1f | 2011-11-07 11:45:24 -0300 | [diff] [blame] | 1659 | err = hci_do_inquiry(hdev, INQUIRY_LEN_BREDR); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 1660 | if (err < 0) |
| 1661 | mgmt_pending_remove(cmd); |
| 1662 | |
| 1663 | failed: |
| 1664 | hci_dev_unlock_bh(hdev); |
| 1665 | hci_dev_put(hdev); |
| 1666 | |
| 1667 | return err; |
| 1668 | } |
| 1669 | |
| 1670 | static int stop_discovery(struct sock *sk, u16 index) |
| 1671 | { |
| 1672 | struct hci_dev *hdev; |
| 1673 | struct pending_cmd *cmd; |
| 1674 | int err; |
| 1675 | |
| 1676 | BT_DBG("hci%u", index); |
| 1677 | |
| 1678 | hdev = hci_dev_get(index); |
| 1679 | if (!hdev) |
| 1680 | return cmd_status(sk, index, MGMT_OP_STOP_DISCOVERY, ENODEV); |
| 1681 | |
| 1682 | hci_dev_lock_bh(hdev); |
| 1683 | |
| 1684 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, index, NULL, 0); |
| 1685 | if (!cmd) { |
| 1686 | err = -ENOMEM; |
| 1687 | goto failed; |
| 1688 | } |
| 1689 | |
Andre Guedes | 023d5049 | 2011-11-04 14:16:52 -0300 | [diff] [blame] | 1690 | err = hci_cancel_inquiry(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 1691 | if (err < 0) |
| 1692 | mgmt_pending_remove(cmd); |
| 1693 | |
| 1694 | failed: |
| 1695 | hci_dev_unlock_bh(hdev); |
| 1696 | hci_dev_put(hdev); |
| 1697 | |
| 1698 | return err; |
| 1699 | } |
| 1700 | |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 1701 | static int block_device(struct sock *sk, u16 index, unsigned char *data, |
| 1702 | u16 len) |
| 1703 | { |
| 1704 | struct hci_dev *hdev; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 1705 | struct mgmt_cp_block_device *cp = (void *) data; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 1706 | int err; |
| 1707 | |
| 1708 | BT_DBG("hci%u", index); |
| 1709 | |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 1710 | if (len != sizeof(*cp)) |
| 1711 | return cmd_status(sk, index, MGMT_OP_BLOCK_DEVICE, |
| 1712 | EINVAL); |
| 1713 | |
| 1714 | hdev = hci_dev_get(index); |
| 1715 | if (!hdev) |
| 1716 | return cmd_status(sk, index, MGMT_OP_BLOCK_DEVICE, |
| 1717 | ENODEV); |
| 1718 | |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 1719 | hci_dev_lock_bh(hdev); |
| 1720 | |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 1721 | err = hci_blacklist_add(hdev, &cp->bdaddr); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 1722 | if (err < 0) |
| 1723 | err = cmd_status(sk, index, MGMT_OP_BLOCK_DEVICE, -err); |
| 1724 | else |
| 1725 | err = cmd_complete(sk, index, MGMT_OP_BLOCK_DEVICE, |
| 1726 | NULL, 0); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 1727 | |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 1728 | hci_dev_unlock_bh(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 1729 | hci_dev_put(hdev); |
| 1730 | |
| 1731 | return err; |
| 1732 | } |
| 1733 | |
| 1734 | static int unblock_device(struct sock *sk, u16 index, unsigned char *data, |
| 1735 | u16 len) |
| 1736 | { |
| 1737 | struct hci_dev *hdev; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 1738 | struct mgmt_cp_unblock_device *cp = (void *) data; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 1739 | int err; |
| 1740 | |
| 1741 | BT_DBG("hci%u", index); |
| 1742 | |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 1743 | if (len != sizeof(*cp)) |
| 1744 | return cmd_status(sk, index, MGMT_OP_UNBLOCK_DEVICE, |
| 1745 | EINVAL); |
| 1746 | |
| 1747 | hdev = hci_dev_get(index); |
| 1748 | if (!hdev) |
| 1749 | return cmd_status(sk, index, MGMT_OP_UNBLOCK_DEVICE, |
| 1750 | ENODEV); |
| 1751 | |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 1752 | hci_dev_lock_bh(hdev); |
| 1753 | |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 1754 | err = hci_blacklist_del(hdev, &cp->bdaddr); |
| 1755 | |
| 1756 | if (err < 0) |
| 1757 | err = cmd_status(sk, index, MGMT_OP_UNBLOCK_DEVICE, -err); |
| 1758 | else |
| 1759 | err = cmd_complete(sk, index, MGMT_OP_UNBLOCK_DEVICE, |
| 1760 | NULL, 0); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 1761 | |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 1762 | hci_dev_unlock_bh(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 1763 | hci_dev_put(hdev); |
| 1764 | |
| 1765 | return err; |
| 1766 | } |
| 1767 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 1768 | static int set_fast_connectable(struct sock *sk, u16 index, |
| 1769 | unsigned char *data, u16 len) |
| 1770 | { |
| 1771 | struct hci_dev *hdev; |
| 1772 | struct mgmt_cp_set_fast_connectable *cp = (void *) data; |
| 1773 | struct hci_cp_write_page_scan_activity acp; |
| 1774 | u8 type; |
| 1775 | int err; |
| 1776 | |
| 1777 | BT_DBG("hci%u", index); |
| 1778 | |
| 1779 | if (len != sizeof(*cp)) |
| 1780 | return cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
| 1781 | EINVAL); |
| 1782 | |
| 1783 | hdev = hci_dev_get(index); |
| 1784 | if (!hdev) |
| 1785 | return cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
| 1786 | ENODEV); |
| 1787 | |
| 1788 | hci_dev_lock(hdev); |
| 1789 | |
| 1790 | if (cp->enable) { |
| 1791 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1792 | acp.interval = 0x0024; /* 22.5 msec page scan interval */ |
| 1793 | } else { |
| 1794 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1795 | acp.interval = 0x0800; /* default 1.28 sec page scan */ |
| 1796 | } |
| 1797 | |
| 1798 | acp.window = 0x0012; /* default 11.25 msec page scan window */ |
| 1799 | |
| 1800 | err = hci_send_cmd(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1801 | sizeof(acp), &acp); |
| 1802 | if (err < 0) { |
| 1803 | err = cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
| 1804 | -err); |
| 1805 | goto done; |
| 1806 | } |
| 1807 | |
| 1808 | err = hci_send_cmd(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
| 1809 | if (err < 0) { |
| 1810 | err = cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
| 1811 | -err); |
| 1812 | goto done; |
| 1813 | } |
| 1814 | |
| 1815 | err = cmd_complete(sk, index, MGMT_OP_SET_FAST_CONNECTABLE, |
| 1816 | NULL, 0); |
| 1817 | done: |
| 1818 | hci_dev_unlock(hdev); |
| 1819 | hci_dev_put(hdev); |
| 1820 | |
| 1821 | return err; |
| 1822 | } |
| 1823 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1824 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 1825 | { |
| 1826 | unsigned char *buf; |
| 1827 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1828 | u16 opcode, index, len; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1829 | int err; |
| 1830 | |
| 1831 | BT_DBG("got %zu bytes", msglen); |
| 1832 | |
| 1833 | if (msglen < sizeof(*hdr)) |
| 1834 | return -EINVAL; |
| 1835 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 1836 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1837 | if (!buf) |
| 1838 | return -ENOMEM; |
| 1839 | |
| 1840 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 1841 | err = -EFAULT; |
| 1842 | goto done; |
| 1843 | } |
| 1844 | |
| 1845 | hdr = (struct mgmt_hdr *) buf; |
| 1846 | opcode = get_unaligned_le16(&hdr->opcode); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1847 | index = get_unaligned_le16(&hdr->index); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1848 | len = get_unaligned_le16(&hdr->len); |
| 1849 | |
| 1850 | if (len != msglen - sizeof(*hdr)) { |
| 1851 | err = -EINVAL; |
| 1852 | goto done; |
| 1853 | } |
| 1854 | |
| 1855 | switch (opcode) { |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 1856 | case MGMT_OP_READ_VERSION: |
| 1857 | err = read_version(sk); |
| 1858 | break; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 1859 | case MGMT_OP_READ_INDEX_LIST: |
| 1860 | err = read_index_list(sk); |
| 1861 | break; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1862 | case MGMT_OP_READ_INFO: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1863 | err = read_controller_info(sk, index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1864 | break; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1865 | case MGMT_OP_SET_POWERED: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1866 | err = set_powered(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1867 | break; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1868 | case MGMT_OP_SET_DISCOVERABLE: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1869 | err = set_discoverable(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1870 | break; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1871 | case MGMT_OP_SET_CONNECTABLE: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1872 | err = set_connectable(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1873 | break; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 1874 | case MGMT_OP_SET_PAIRABLE: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1875 | err = set_pairable(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 1876 | break; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1877 | case MGMT_OP_ADD_UUID: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1878 | err = add_uuid(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1879 | break; |
| 1880 | case MGMT_OP_REMOVE_UUID: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1881 | err = remove_uuid(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1882 | break; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1883 | case MGMT_OP_SET_DEV_CLASS: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1884 | err = set_dev_class(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1885 | break; |
| 1886 | case MGMT_OP_SET_SERVICE_CACHE: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1887 | err = set_service_cache(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1888 | break; |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 1889 | case MGMT_OP_LOAD_LINK_KEYS: |
| 1890 | err = load_link_keys(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1891 | break; |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 1892 | case MGMT_OP_REMOVE_KEYS: |
| 1893 | err = remove_keys(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1894 | break; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1895 | case MGMT_OP_DISCONNECT: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1896 | err = disconnect(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1897 | break; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1898 | case MGMT_OP_GET_CONNECTIONS: |
Szymon Janc | 8ce6284 | 2011-03-01 16:55:32 +0100 | [diff] [blame] | 1899 | err = get_connections(sk, index); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1900 | break; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1901 | case MGMT_OP_PIN_CODE_REPLY: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1902 | err = pin_code_reply(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1903 | break; |
| 1904 | case MGMT_OP_PIN_CODE_NEG_REPLY: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1905 | err = pin_code_neg_reply(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1906 | break; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1907 | case MGMT_OP_SET_IO_CAPABILITY: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1908 | err = set_io_capability(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1909 | break; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1910 | case MGMT_OP_PAIR_DEVICE: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1911 | err = pair_device(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1912 | break; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1913 | case MGMT_OP_USER_CONFIRM_REPLY: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1914 | err = user_confirm_reply(sk, index, buf + sizeof(*hdr), len, 1); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1915 | break; |
| 1916 | case MGMT_OP_USER_CONFIRM_NEG_REPLY: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1917 | err = user_confirm_reply(sk, index, buf + sizeof(*hdr), len, 0); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1918 | break; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 1919 | case MGMT_OP_SET_LOCAL_NAME: |
| 1920 | err = set_local_name(sk, index, buf + sizeof(*hdr), len); |
| 1921 | break; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 1922 | case MGMT_OP_READ_LOCAL_OOB_DATA: |
| 1923 | err = read_local_oob_data(sk, index); |
| 1924 | break; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 1925 | case MGMT_OP_ADD_REMOTE_OOB_DATA: |
| 1926 | err = add_remote_oob_data(sk, index, buf + sizeof(*hdr), len); |
| 1927 | break; |
| 1928 | case MGMT_OP_REMOVE_REMOTE_OOB_DATA: |
| 1929 | err = remove_remote_oob_data(sk, index, buf + sizeof(*hdr), |
| 1930 | len); |
| 1931 | break; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 1932 | case MGMT_OP_START_DISCOVERY: |
| 1933 | err = start_discovery(sk, index); |
| 1934 | break; |
| 1935 | case MGMT_OP_STOP_DISCOVERY: |
| 1936 | err = stop_discovery(sk, index); |
| 1937 | break; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 1938 | case MGMT_OP_BLOCK_DEVICE: |
| 1939 | err = block_device(sk, index, buf + sizeof(*hdr), len); |
| 1940 | break; |
| 1941 | case MGMT_OP_UNBLOCK_DEVICE: |
| 1942 | err = unblock_device(sk, index, buf + sizeof(*hdr), len); |
| 1943 | break; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 1944 | case MGMT_OP_SET_FAST_CONNECTABLE: |
| 1945 | err = set_fast_connectable(sk, index, buf + sizeof(*hdr), |
| 1946 | len); |
| 1947 | break; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1948 | default: |
| 1949 | BT_DBG("Unknown op %u", opcode); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1950 | err = cmd_status(sk, index, opcode, 0x01); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1951 | break; |
| 1952 | } |
| 1953 | |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1954 | if (err < 0) |
| 1955 | goto done; |
| 1956 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1957 | err = msglen; |
| 1958 | |
| 1959 | done: |
| 1960 | kfree(buf); |
| 1961 | return err; |
| 1962 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 1963 | |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 1964 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 1965 | { |
| 1966 | u8 *status = data; |
| 1967 | |
| 1968 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 1969 | mgmt_pending_remove(cmd); |
| 1970 | } |
| 1971 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 1972 | int mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 1973 | { |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 1974 | return mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 1975 | } |
| 1976 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 1977 | int mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 1978 | { |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 1979 | u8 status = ENODEV; |
| 1980 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 1981 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 1982 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 1983 | return mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1984 | } |
| 1985 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1986 | struct cmd_lookup { |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1987 | u8 val; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1988 | struct sock *sk; |
| 1989 | }; |
| 1990 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1991 | static void mode_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1992 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 1993 | struct mgmt_mode *cp = cmd->param; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1994 | struct cmd_lookup *match = data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1995 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1996 | if (cp->val != match->val) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1997 | return; |
| 1998 | |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1999 | send_mode_rsp(cmd->sk, cmd->opcode, cmd->index, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2000 | |
| 2001 | list_del(&cmd->list); |
| 2002 | |
| 2003 | if (match->sk == NULL) { |
| 2004 | match->sk = cmd->sk; |
| 2005 | sock_hold(match->sk); |
| 2006 | } |
| 2007 | |
| 2008 | mgmt_pending_free(cmd); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 2009 | } |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 2010 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2011 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 2012 | { |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2013 | struct mgmt_mode ev; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2014 | struct cmd_lookup match = { powered, NULL }; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2015 | int ret; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 2016 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2017 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, mode_rsp, &match); |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 2018 | |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 2019 | if (!powered) { |
| 2020 | u8 status = ENETDOWN; |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2021 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 2022 | } |
| 2023 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2024 | ev.val = powered; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2025 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2026 | ret = mgmt_event(MGMT_EV_POWERED, hdev, &ev, sizeof(ev), match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2027 | |
| 2028 | if (match.sk) |
| 2029 | sock_put(match.sk); |
| 2030 | |
| 2031 | return ret; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 2032 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2033 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2034 | int mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2035 | { |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2036 | struct mgmt_mode ev; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2037 | struct cmd_lookup match = { discoverable, NULL }; |
| 2038 | int ret; |
| 2039 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2040 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, mode_rsp, &match); |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2041 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2042 | ev.val = discoverable; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2043 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2044 | ret = mgmt_event(MGMT_EV_DISCOVERABLE, hdev, &ev, sizeof(ev), |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2045 | match.sk); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2046 | |
| 2047 | if (match.sk) |
| 2048 | sock_put(match.sk); |
| 2049 | |
| 2050 | return ret; |
| 2051 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2052 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2053 | int mgmt_connectable(struct hci_dev *hdev, u8 connectable) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2054 | { |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2055 | struct mgmt_mode ev; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2056 | struct cmd_lookup match = { connectable, NULL }; |
| 2057 | int ret; |
| 2058 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2059 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, mode_rsp, &match); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2060 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2061 | ev.val = connectable; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2062 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2063 | ret = mgmt_event(MGMT_EV_CONNECTABLE, hdev, &ev, sizeof(ev), match.sk); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2064 | |
| 2065 | if (match.sk) |
| 2066 | sock_put(match.sk); |
| 2067 | |
| 2068 | return ret; |
| 2069 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2070 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2071 | int mgmt_write_scan_failed(struct hci_dev *hdev, u8 scan, u8 status) |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 2072 | { |
| 2073 | if (scan & SCAN_PAGE) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2074 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 2075 | cmd_status_rsp, &status); |
| 2076 | |
| 2077 | if (scan & SCAN_INQUIRY) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2078 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 2079 | cmd_status_rsp, &status); |
| 2080 | |
| 2081 | return 0; |
| 2082 | } |
| 2083 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2084 | int mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 2085 | u8 persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2086 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2087 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2088 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2089 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2090 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2091 | ev.store_hint = persistent; |
| 2092 | bacpy(&ev.key.bdaddr, &key->bdaddr); |
| 2093 | ev.key.type = key->type; |
| 2094 | memcpy(ev.key.val, key->val, 16); |
| 2095 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2096 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2097 | return mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2098 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2099 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2100 | int mgmt_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2101 | { |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2102 | struct mgmt_addr_info ev; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2103 | |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2104 | bacpy(&ev.bdaddr, bdaddr); |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2105 | ev.type = link_to_mgmt(link_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2106 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2107 | return mgmt_event(MGMT_EV_CONNECTED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2108 | } |
| 2109 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2110 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 2111 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 2112 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2113 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2114 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2115 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2116 | bacpy(&rp.bdaddr, &cp->bdaddr); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2117 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2118 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2119 | |
| 2120 | *sk = cmd->sk; |
| 2121 | sock_hold(*sk); |
| 2122 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2123 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2124 | } |
| 2125 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2126 | int mgmt_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2127 | { |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2128 | struct mgmt_addr_info ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2129 | struct sock *sk = NULL; |
| 2130 | int err; |
| 2131 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2132 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2133 | |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2134 | bacpy(&ev.bdaddr, bdaddr); |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2135 | ev.type = link_to_mgmt(type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2136 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2137 | err = mgmt_event(MGMT_EV_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2138 | |
| 2139 | if (sk) |
| 2140 | sock_put(sk); |
| 2141 | |
| 2142 | return err; |
| 2143 | } |
| 2144 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2145 | int mgmt_disconnect_failed(struct hci_dev *hdev) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2146 | { |
| 2147 | struct pending_cmd *cmd; |
| 2148 | int err; |
| 2149 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2150 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev->id); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2151 | if (!cmd) |
| 2152 | return -ENOENT; |
| 2153 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2154 | err = cmd_status(cmd->sk, hdev->id, MGMT_OP_DISCONNECT, EIO); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2155 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2156 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2157 | |
| 2158 | return err; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2159 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 2160 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2161 | int mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type, |
| 2162 | u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 2163 | { |
| 2164 | struct mgmt_ev_connect_failed ev; |
| 2165 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2166 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2167 | ev.addr.type = link_to_mgmt(type); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 2168 | ev.status = status; |
| 2169 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2170 | return mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 2171 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2172 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2173 | int mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2174 | { |
| 2175 | struct mgmt_ev_pin_code_request ev; |
| 2176 | |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2177 | bacpy(&ev.bdaddr, bdaddr); |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 2178 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2179 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2180 | return mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2181 | NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2182 | } |
| 2183 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2184 | int mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 2185 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2186 | { |
| 2187 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 2188 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2189 | int err; |
| 2190 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2191 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev->id); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2192 | if (!cmd) |
| 2193 | return -ENOENT; |
| 2194 | |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 2195 | bacpy(&rp.bdaddr, bdaddr); |
| 2196 | rp.status = status; |
| 2197 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2198 | err = cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, &rp, |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2199 | sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2200 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2201 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2202 | |
| 2203 | return err; |
| 2204 | } |
| 2205 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2206 | int mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 2207 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2208 | { |
| 2209 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 2210 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2211 | int err; |
| 2212 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2213 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev->id); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2214 | if (!cmd) |
| 2215 | return -ENOENT; |
| 2216 | |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 2217 | bacpy(&rp.bdaddr, bdaddr); |
| 2218 | rp.status = status; |
| 2219 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2220 | err = cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, &rp, |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2221 | sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2222 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2223 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2224 | |
| 2225 | return err; |
| 2226 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2227 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2228 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 2229 | __le32 value, u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2230 | { |
| 2231 | struct mgmt_ev_user_confirm_request ev; |
| 2232 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2233 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2234 | |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2235 | bacpy(&ev.bdaddr, bdaddr); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 2236 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2237 | put_unaligned_le32(value, &ev.value); |
| 2238 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2239 | return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev), |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2240 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2241 | } |
| 2242 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2243 | static int confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 2244 | u8 status, u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2245 | { |
| 2246 | struct pending_cmd *cmd; |
| 2247 | struct mgmt_rp_user_confirm_reply rp; |
| 2248 | int err; |
| 2249 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2250 | cmd = mgmt_pending_find(opcode, hdev->id); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2251 | if (!cmd) |
| 2252 | return -ENOENT; |
| 2253 | |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2254 | bacpy(&rp.bdaddr, bdaddr); |
| 2255 | rp.status = status; |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2256 | err = cmd_complete(cmd->sk, hdev->id, opcode, &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2257 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2258 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2259 | |
| 2260 | return err; |
| 2261 | } |
| 2262 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2263 | int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 2264 | u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2265 | { |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2266 | return confirm_reply_complete(hdev, bdaddr, status, |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2267 | MGMT_OP_USER_CONFIRM_REPLY); |
| 2268 | } |
| 2269 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2270 | int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, |
| 2271 | bdaddr_t *bdaddr, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2272 | { |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2273 | return confirm_reply_complete(hdev, bdaddr, status, |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2274 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
| 2275 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 2276 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2277 | int mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 2278 | { |
| 2279 | struct mgmt_ev_auth_failed ev; |
| 2280 | |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 2281 | bacpy(&ev.bdaddr, bdaddr); |
| 2282 | ev.status = status; |
| 2283 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2284 | return mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 2285 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2286 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2287 | int mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2288 | { |
| 2289 | struct pending_cmd *cmd; |
| 2290 | struct mgmt_cp_set_local_name ev; |
| 2291 | int err; |
| 2292 | |
| 2293 | memset(&ev, 0, sizeof(ev)); |
| 2294 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
| 2295 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2296 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev->id); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2297 | if (!cmd) |
| 2298 | goto send_event; |
| 2299 | |
| 2300 | if (status) { |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2301 | err = cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 2302 | EIO); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2303 | goto failed; |
| 2304 | } |
| 2305 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2306 | hci_dev_lock_bh(hdev); |
| 2307 | update_eir(hdev); |
| 2308 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2309 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2310 | err = cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, &ev, |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2311 | sizeof(ev)); |
| 2312 | if (err < 0) |
| 2313 | goto failed; |
| 2314 | |
| 2315 | send_event: |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2316 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2317 | cmd ? cmd->sk : NULL); |
| 2318 | |
| 2319 | failed: |
| 2320 | if (cmd) |
| 2321 | mgmt_pending_remove(cmd); |
| 2322 | return err; |
| 2323 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2324 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2325 | int mgmt_read_local_oob_data_reply_complete(struct hci_dev *hdev, u8 *hash, |
| 2326 | u8 *randomizer, u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2327 | { |
| 2328 | struct pending_cmd *cmd; |
| 2329 | int err; |
| 2330 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2331 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2332 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2333 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev->id); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2334 | if (!cmd) |
| 2335 | return -ENOENT; |
| 2336 | |
| 2337 | if (status) { |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2338 | err = cmd_status(cmd->sk, hdev->id, |
| 2339 | MGMT_OP_READ_LOCAL_OOB_DATA, EIO); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2340 | } else { |
| 2341 | struct mgmt_rp_read_local_oob_data rp; |
| 2342 | |
| 2343 | memcpy(rp.hash, hash, sizeof(rp.hash)); |
| 2344 | memcpy(rp.randomizer, randomizer, sizeof(rp.randomizer)); |
| 2345 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2346 | err = cmd_complete(cmd->sk, hdev->id, |
| 2347 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 2348 | &rp, sizeof(rp)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2349 | } |
| 2350 | |
| 2351 | mgmt_pending_remove(cmd); |
| 2352 | |
| 2353 | return err; |
| 2354 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 2355 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2356 | int mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type, |
| 2357 | u8 *dev_class, s8 rssi, u8 *eir) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 2358 | { |
| 2359 | struct mgmt_ev_device_found ev; |
| 2360 | |
| 2361 | memset(&ev, 0, sizeof(ev)); |
| 2362 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2363 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2364 | ev.addr.type = link_to_mgmt(type); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 2365 | ev.rssi = rssi; |
| 2366 | |
| 2367 | if (eir) |
| 2368 | memcpy(ev.eir, eir, sizeof(ev.eir)); |
| 2369 | |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 2370 | if (dev_class) |
| 2371 | memcpy(ev.dev_class, dev_class, sizeof(ev.dev_class)); |
| 2372 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2373 | return mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 2374 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 2375 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2376 | int mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 *name) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 2377 | { |
| 2378 | struct mgmt_ev_remote_name ev; |
| 2379 | |
| 2380 | memset(&ev, 0, sizeof(ev)); |
| 2381 | |
| 2382 | bacpy(&ev.bdaddr, bdaddr); |
| 2383 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
| 2384 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2385 | return mgmt_event(MGMT_EV_REMOTE_NAME, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 2386 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 2387 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2388 | int mgmt_inquiry_failed(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 2389 | { |
| 2390 | struct pending_cmd *cmd; |
| 2391 | int err; |
| 2392 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2393 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev->id); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 2394 | if (!cmd) |
| 2395 | return -ENOENT; |
| 2396 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2397 | err = cmd_status(cmd->sk, hdev->id, cmd->opcode, status); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 2398 | mgmt_pending_remove(cmd); |
| 2399 | |
| 2400 | return err; |
| 2401 | } |
| 2402 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2403 | int mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 2404 | { |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 2405 | struct pending_cmd *cmd; |
| 2406 | |
| 2407 | if (discovering) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2408 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev->id); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 2409 | else |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2410 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev->id); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 2411 | |
| 2412 | if (cmd != NULL) { |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2413 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, NULL, 0); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 2414 | mgmt_pending_remove(cmd); |
| 2415 | } |
| 2416 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2417 | return mgmt_event(MGMT_EV_DISCOVERING, hdev, &discovering, |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 2418 | sizeof(discovering), NULL); |
| 2419 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2420 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2421 | int mgmt_device_blocked(struct hci_dev *hdev, bdaddr_t *bdaddr) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2422 | { |
| 2423 | struct pending_cmd *cmd; |
| 2424 | struct mgmt_ev_device_blocked ev; |
| 2425 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2426 | cmd = mgmt_pending_find(MGMT_OP_BLOCK_DEVICE, hdev->id); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2427 | |
| 2428 | bacpy(&ev.bdaddr, bdaddr); |
| 2429 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2430 | return mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &ev, sizeof(ev), |
| 2431 | cmd ? cmd->sk : NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2432 | } |
| 2433 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2434 | int mgmt_device_unblocked(struct hci_dev *hdev, bdaddr_t *bdaddr) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2435 | { |
| 2436 | struct pending_cmd *cmd; |
| 2437 | struct mgmt_ev_device_unblocked ev; |
| 2438 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2439 | cmd = mgmt_pending_find(MGMT_OP_UNBLOCK_DEVICE, hdev->id); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2440 | |
| 2441 | bacpy(&ev.bdaddr, bdaddr); |
| 2442 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame^] | 2443 | return mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &ev, sizeof(ev), |
| 2444 | cmd ? cmd->sk : NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2445 | } |