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