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