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