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