Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | BlueZ - Bluetooth protocol stack for Linux |
Johan Hedberg | ea585ab | 2012-02-17 14:50:39 +0200 | [diff] [blame] | 3 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4 | Copyright (C) 2010 Nokia Corporation |
Johan Hedberg | ea585ab | 2012-02-17 14:50:39 +0200 | [diff] [blame] | 5 | Copyright (C) 2011-2012 Intel Corporation |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License version 2 as |
| 9 | published by the Free Software Foundation; |
| 10 | |
| 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 12 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 13 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 14 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
| 15 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 16 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 | |
| 20 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 21 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
| 22 | SOFTWARE IS DISCLAIMED. |
| 23 | */ |
| 24 | |
| 25 | /* Bluetooth HCI Management interface */ |
| 26 | |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 28 | #include <asm/unaligned.h> |
| 29 | |
| 30 | #include <net/bluetooth/bluetooth.h> |
| 31 | #include <net/bluetooth/hci_core.h> |
| 32 | #include <net/bluetooth/mgmt.h> |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 33 | #include <net/bluetooth/smp.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 34 | |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 35 | bool enable_hs; |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 36 | |
Johan Hedberg | 2da9c55 | 2012-02-17 14:39:28 +0200 | [diff] [blame] | 37 | #define MGMT_VERSION 1 |
Marcel Holtmann | 053262d | 2012-03-27 18:49:02 +0200 | [diff] [blame] | 38 | #define MGMT_REVISION 1 |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 39 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 40 | static const u16 mgmt_commands[] = { |
| 41 | MGMT_OP_READ_INDEX_LIST, |
| 42 | MGMT_OP_READ_INFO, |
| 43 | MGMT_OP_SET_POWERED, |
| 44 | MGMT_OP_SET_DISCOVERABLE, |
| 45 | MGMT_OP_SET_CONNECTABLE, |
| 46 | MGMT_OP_SET_FAST_CONNECTABLE, |
| 47 | MGMT_OP_SET_PAIRABLE, |
| 48 | MGMT_OP_SET_LINK_SECURITY, |
| 49 | MGMT_OP_SET_SSP, |
| 50 | MGMT_OP_SET_HS, |
| 51 | MGMT_OP_SET_LE, |
| 52 | MGMT_OP_SET_DEV_CLASS, |
| 53 | MGMT_OP_SET_LOCAL_NAME, |
| 54 | MGMT_OP_ADD_UUID, |
| 55 | MGMT_OP_REMOVE_UUID, |
| 56 | MGMT_OP_LOAD_LINK_KEYS, |
| 57 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 58 | MGMT_OP_DISCONNECT, |
| 59 | MGMT_OP_GET_CONNECTIONS, |
| 60 | MGMT_OP_PIN_CODE_REPLY, |
| 61 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 62 | MGMT_OP_SET_IO_CAPABILITY, |
| 63 | MGMT_OP_PAIR_DEVICE, |
| 64 | MGMT_OP_CANCEL_PAIR_DEVICE, |
| 65 | MGMT_OP_UNPAIR_DEVICE, |
| 66 | MGMT_OP_USER_CONFIRM_REPLY, |
| 67 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 68 | MGMT_OP_USER_PASSKEY_REPLY, |
| 69 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 70 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 71 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 72 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 73 | MGMT_OP_START_DISCOVERY, |
| 74 | MGMT_OP_STOP_DISCOVERY, |
| 75 | MGMT_OP_CONFIRM_NAME, |
| 76 | MGMT_OP_BLOCK_DEVICE, |
| 77 | MGMT_OP_UNBLOCK_DEVICE, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 78 | MGMT_OP_SET_DEVICE_ID, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | static const u16 mgmt_events[] = { |
| 82 | MGMT_EV_CONTROLLER_ERROR, |
| 83 | MGMT_EV_INDEX_ADDED, |
| 84 | MGMT_EV_INDEX_REMOVED, |
| 85 | MGMT_EV_NEW_SETTINGS, |
| 86 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 87 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 88 | MGMT_EV_NEW_LINK_KEY, |
| 89 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 90 | MGMT_EV_DEVICE_CONNECTED, |
| 91 | MGMT_EV_DEVICE_DISCONNECTED, |
| 92 | MGMT_EV_CONNECT_FAILED, |
| 93 | MGMT_EV_PIN_CODE_REQUEST, |
| 94 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 95 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 96 | MGMT_EV_AUTH_FAILED, |
| 97 | MGMT_EV_DEVICE_FOUND, |
| 98 | MGMT_EV_DISCOVERING, |
| 99 | MGMT_EV_DEVICE_BLOCKED, |
| 100 | MGMT_EV_DEVICE_UNBLOCKED, |
| 101 | MGMT_EV_DEVICE_UNPAIRED, |
| 102 | }; |
| 103 | |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 104 | /* |
| 105 | * These LE scan and inquiry parameters were chosen according to LE General |
| 106 | * Discovery Procedure specification. |
| 107 | */ |
| 108 | #define LE_SCAN_TYPE 0x01 |
| 109 | #define LE_SCAN_WIN 0x12 |
| 110 | #define LE_SCAN_INT 0x12 |
| 111 | #define LE_SCAN_TIMEOUT_LE_ONLY 10240 /* TGAP(gen_disc_scan_min) */ |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 112 | #define LE_SCAN_TIMEOUT_BREDR_LE 5120 /* TGAP(100)/2 */ |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 113 | |
Andre Guedes | e877752 | 2012-02-03 17:48:02 -0300 | [diff] [blame] | 114 | #define INQUIRY_LEN_BREDR 0x08 /* TGAP(100) */ |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 115 | #define INQUIRY_LEN_BREDR_LE 0x04 /* TGAP(100)/2 */ |
Andre Guedes | 2519a1f | 2011-11-07 11:45:24 -0300 | [diff] [blame] | 116 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 117 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 118 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 119 | #define hdev_is_powered(hdev) (test_bit(HCI_UP, &hdev->flags) && \ |
| 120 | !test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
| 121 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 122 | struct pending_cmd { |
| 123 | struct list_head list; |
Johan Hedberg | fc2f4b1 | 2011-11-09 13:58:56 +0200 | [diff] [blame] | 124 | u16 opcode; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 125 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 126 | void *param; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 127 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 128 | void *user_data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 129 | }; |
| 130 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 131 | /* HCI to MGMT error code conversion table */ |
| 132 | static u8 mgmt_status_table[] = { |
| 133 | MGMT_STATUS_SUCCESS, |
| 134 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 135 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 136 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 137 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 138 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
| 139 | MGMT_STATUS_NOT_PAIRED, /* PIN or Key Missing */ |
| 140 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 141 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 142 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 143 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 144 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 145 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 146 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 147 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 148 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 149 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 150 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 151 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 152 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 153 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 154 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 155 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 156 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 157 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 158 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 159 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 160 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 161 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 162 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 163 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 164 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 165 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 166 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 167 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 168 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 169 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 170 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 171 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 172 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 173 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 174 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 175 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 176 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 177 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 178 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 179 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 180 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 181 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 182 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 183 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 184 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 185 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 186 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 187 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 188 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 189 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 190 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 191 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 192 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 193 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 194 | }; |
| 195 | |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 196 | bool mgmt_valid_hdev(struct hci_dev *hdev) |
| 197 | { |
| 198 | return hdev->dev_type == HCI_BREDR; |
| 199 | } |
| 200 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 201 | static u8 mgmt_status(u8 hci_status) |
| 202 | { |
| 203 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 204 | return mgmt_status_table[hci_status]; |
| 205 | |
| 206 | return MGMT_STATUS_FAILED; |
| 207 | } |
| 208 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 209 | 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] | 210 | { |
| 211 | struct sk_buff *skb; |
| 212 | struct mgmt_hdr *hdr; |
| 213 | struct mgmt_ev_cmd_status *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 214 | int err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 215 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 216 | 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] | 217 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 218 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 219 | if (!skb) |
| 220 | return -ENOMEM; |
| 221 | |
| 222 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 223 | |
| 224 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 225 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 226 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 227 | |
| 228 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 229 | ev->status = status; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 230 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 231 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 232 | err = sock_queue_rcv_skb(sk, skb); |
| 233 | if (err < 0) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 234 | kfree_skb(skb); |
| 235 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 236 | return err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 237 | } |
| 238 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 239 | static int cmd_complete(struct sock *sk, u16 index, u16 cmd, u8 status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 240 | void *rp, size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 241 | { |
| 242 | struct sk_buff *skb; |
| 243 | struct mgmt_hdr *hdr; |
| 244 | struct mgmt_ev_cmd_complete *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 245 | int err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 246 | |
| 247 | BT_DBG("sock %p", sk); |
| 248 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 249 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 250 | if (!skb) |
| 251 | return -ENOMEM; |
| 252 | |
| 253 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 254 | |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 255 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 256 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 257 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 258 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 259 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 260 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 261 | ev->status = status; |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 262 | |
| 263 | if (rp) |
| 264 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 265 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 266 | err = sock_queue_rcv_skb(sk, skb); |
| 267 | if (err < 0) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 268 | kfree_skb(skb); |
| 269 | |
Marcel Holtmann | e5f0e15 | 2012-02-22 11:59:01 +0100 | [diff] [blame] | 270 | return err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 271 | } |
| 272 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 273 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 274 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 275 | { |
| 276 | struct mgmt_rp_read_version rp; |
| 277 | |
| 278 | BT_DBG("sock %p", sk); |
| 279 | |
| 280 | rp.version = MGMT_VERSION; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 281 | rp.revision = __constant_cpu_to_le16(MGMT_REVISION); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 282 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 283 | return cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 284 | sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 285 | } |
| 286 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 287 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 288 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 289 | { |
| 290 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 291 | const u16 num_commands = ARRAY_SIZE(mgmt_commands); |
| 292 | const u16 num_events = ARRAY_SIZE(mgmt_events); |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 293 | __le16 *opcode; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 294 | size_t rp_size; |
| 295 | int i, err; |
| 296 | |
| 297 | BT_DBG("sock %p", sk); |
| 298 | |
| 299 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 300 | |
| 301 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 302 | if (!rp) |
| 303 | return -ENOMEM; |
| 304 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 305 | rp->num_commands = __constant_cpu_to_le16(num_commands); |
| 306 | rp->num_events = __constant_cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 307 | |
| 308 | for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++) |
| 309 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 310 | |
| 311 | for (i = 0; i < num_events; i++, opcode++) |
| 312 | put_unaligned_le16(mgmt_events[i], opcode); |
| 313 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 314 | err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0, rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 315 | rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 316 | kfree(rp); |
| 317 | |
| 318 | return err; |
| 319 | } |
| 320 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 321 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 322 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 323 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 324 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 325 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 326 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 327 | u16 count; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 328 | int i, err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 329 | |
| 330 | BT_DBG("sock %p", sk); |
| 331 | |
| 332 | read_lock(&hci_dev_list_lock); |
| 333 | |
| 334 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 335 | list_for_each_entry(d, &hci_dev_list, list) { |
| 336 | if (!mgmt_valid_hdev(d)) |
| 337 | continue; |
| 338 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 339 | count++; |
| 340 | } |
| 341 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 342 | rp_len = sizeof(*rp) + (2 * count); |
| 343 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 344 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 345 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 346 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 347 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 348 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 349 | rp->num_controllers = cpu_to_le16(count); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 350 | |
| 351 | i = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 352 | list_for_each_entry(d, &hci_dev_list, list) { |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 353 | if (test_bit(HCI_SETUP, &d->dev_flags)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 354 | continue; |
| 355 | |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 356 | if (!mgmt_valid_hdev(d)) |
| 357 | continue; |
| 358 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 359 | rp->index[i++] = cpu_to_le16(d->id); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 360 | BT_DBG("Added hci%u", d->id); |
| 361 | } |
| 362 | |
| 363 | read_unlock(&hci_dev_list_lock); |
| 364 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 365 | err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, 0, rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 366 | rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 367 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 368 | kfree(rp); |
| 369 | |
| 370 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 371 | } |
| 372 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 373 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 374 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 375 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 376 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 377 | settings |= MGMT_SETTING_POWERED; |
| 378 | settings |= MGMT_SETTING_CONNECTABLE; |
| 379 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 380 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 381 | settings |= MGMT_SETTING_PAIRABLE; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 382 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 383 | if (hdev->features[6] & LMP_SIMPLE_PAIR) |
| 384 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 385 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame^] | 386 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 387 | settings |= MGMT_SETTING_BREDR; |
| 388 | settings |= MGMT_SETTING_LINK_SECURITY; |
| 389 | } |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 390 | |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 391 | if (enable_hs) |
| 392 | settings |= MGMT_SETTING_HS; |
| 393 | |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 394 | if (hdev->features[4] & LMP_LE) |
| 395 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 396 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 397 | return settings; |
| 398 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 399 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 400 | static u32 get_current_settings(struct hci_dev *hdev) |
| 401 | { |
| 402 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 403 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 404 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 405 | settings |= MGMT_SETTING_POWERED; |
| 406 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 407 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 408 | settings |= MGMT_SETTING_CONNECTABLE; |
| 409 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 410 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 411 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 412 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 413 | if (test_bit(HCI_PAIRABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 414 | settings |= MGMT_SETTING_PAIRABLE; |
| 415 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame^] | 416 | if (lmp_bredr_capable(hdev)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 417 | settings |= MGMT_SETTING_BREDR; |
| 418 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 419 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 420 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 421 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 422 | if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 423 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 424 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 425 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 426 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 427 | |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 428 | if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags)) |
| 429 | settings |= MGMT_SETTING_HS; |
| 430 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 431 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 432 | } |
| 433 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 434 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 435 | |
| 436 | static u8 bluetooth_base_uuid[] = { |
| 437 | 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, |
| 438 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 439 | }; |
| 440 | |
| 441 | static u16 get_uuid16(u8 *uuid128) |
| 442 | { |
| 443 | u32 val; |
| 444 | int i; |
| 445 | |
| 446 | for (i = 0; i < 12; i++) { |
| 447 | if (bluetooth_base_uuid[i] != uuid128[i]) |
| 448 | return 0; |
| 449 | } |
| 450 | |
Andrei Emeltchenko | 3e9fb6d | 2012-03-20 10:32:25 +0200 | [diff] [blame] | 451 | val = get_unaligned_le32(&uuid128[12]); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 452 | if (val > 0xffff) |
| 453 | return 0; |
| 454 | |
| 455 | return (u16) val; |
| 456 | } |
| 457 | |
| 458 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 459 | { |
| 460 | u8 *ptr = data; |
| 461 | u16 eir_len = 0; |
| 462 | u16 uuid16_list[HCI_MAX_EIR_LENGTH / sizeof(u16)]; |
| 463 | int i, truncated = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 464 | struct bt_uuid *uuid; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 465 | size_t name_len; |
| 466 | |
| 467 | name_len = strlen(hdev->dev_name); |
| 468 | |
| 469 | if (name_len > 0) { |
| 470 | /* EIR Data type */ |
| 471 | if (name_len > 48) { |
| 472 | name_len = 48; |
| 473 | ptr[1] = EIR_NAME_SHORT; |
| 474 | } else |
| 475 | ptr[1] = EIR_NAME_COMPLETE; |
| 476 | |
| 477 | /* EIR Data length */ |
| 478 | ptr[0] = name_len + 1; |
| 479 | |
| 480 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 481 | |
| 482 | eir_len += (name_len + 2); |
| 483 | ptr += (name_len + 2); |
| 484 | } |
| 485 | |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 486 | if (hdev->inq_tx_power) { |
| 487 | ptr[0] = 2; |
| 488 | ptr[1] = EIR_TX_POWER; |
| 489 | ptr[2] = (u8) hdev->inq_tx_power; |
| 490 | |
| 491 | eir_len += 3; |
| 492 | ptr += 3; |
| 493 | } |
| 494 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 495 | if (hdev->devid_source > 0) { |
| 496 | ptr[0] = 9; |
| 497 | ptr[1] = EIR_DEVICE_ID; |
| 498 | |
| 499 | put_unaligned_le16(hdev->devid_source, ptr + 2); |
| 500 | put_unaligned_le16(hdev->devid_vendor, ptr + 4); |
| 501 | put_unaligned_le16(hdev->devid_product, ptr + 6); |
| 502 | put_unaligned_le16(hdev->devid_version, ptr + 8); |
| 503 | |
| 504 | eir_len += 10; |
| 505 | ptr += 10; |
| 506 | } |
| 507 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 508 | memset(uuid16_list, 0, sizeof(uuid16_list)); |
| 509 | |
| 510 | /* Group all UUID16 types */ |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 511 | list_for_each_entry(uuid, &hdev->uuids, list) { |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 512 | u16 uuid16; |
| 513 | |
| 514 | uuid16 = get_uuid16(uuid->uuid); |
| 515 | if (uuid16 == 0) |
| 516 | return; |
| 517 | |
| 518 | if (uuid16 < 0x1100) |
| 519 | continue; |
| 520 | |
| 521 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 522 | continue; |
| 523 | |
| 524 | /* Stop if not enough space to put next UUID */ |
| 525 | if (eir_len + 2 + sizeof(u16) > HCI_MAX_EIR_LENGTH) { |
| 526 | truncated = 1; |
| 527 | break; |
| 528 | } |
| 529 | |
| 530 | /* Check for duplicates */ |
| 531 | for (i = 0; uuid16_list[i] != 0; i++) |
| 532 | if (uuid16_list[i] == uuid16) |
| 533 | break; |
| 534 | |
| 535 | if (uuid16_list[i] == 0) { |
| 536 | uuid16_list[i] = uuid16; |
| 537 | eir_len += sizeof(u16); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | if (uuid16_list[0] != 0) { |
| 542 | u8 *length = ptr; |
| 543 | |
| 544 | /* EIR Data type */ |
| 545 | ptr[1] = truncated ? EIR_UUID16_SOME : EIR_UUID16_ALL; |
| 546 | |
| 547 | ptr += 2; |
| 548 | eir_len += 2; |
| 549 | |
| 550 | for (i = 0; uuid16_list[i] != 0; i++) { |
| 551 | *ptr++ = (uuid16_list[i] & 0x00ff); |
| 552 | *ptr++ = (uuid16_list[i] & 0xff00) >> 8; |
| 553 | } |
| 554 | |
| 555 | /* EIR Data length */ |
| 556 | *length = (i * sizeof(u16)) + 1; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | static int update_eir(struct hci_dev *hdev) |
| 561 | { |
| 562 | struct hci_cp_write_eir cp; |
| 563 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 564 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 565 | return 0; |
| 566 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 567 | if (!(hdev->features[6] & LMP_EXT_INQ)) |
| 568 | return 0; |
| 569 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 570 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 571 | return 0; |
| 572 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 573 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 574 | return 0; |
| 575 | |
| 576 | memset(&cp, 0, sizeof(cp)); |
| 577 | |
| 578 | create_eir(hdev, cp.data); |
| 579 | |
| 580 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
| 581 | return 0; |
| 582 | |
| 583 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 584 | |
| 585 | return hci_send_cmd(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
| 586 | } |
| 587 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 588 | static u8 get_service_classes(struct hci_dev *hdev) |
| 589 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 590 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 591 | u8 val = 0; |
| 592 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 593 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 594 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 595 | |
| 596 | return val; |
| 597 | } |
| 598 | |
| 599 | static int update_class(struct hci_dev *hdev) |
| 600 | { |
| 601 | u8 cod[3]; |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 602 | int err; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 603 | |
| 604 | BT_DBG("%s", hdev->name); |
| 605 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 606 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 607 | return 0; |
| 608 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 609 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 610 | return 0; |
| 611 | |
| 612 | cod[0] = hdev->minor_class; |
| 613 | cod[1] = hdev->major_class; |
| 614 | cod[2] = get_service_classes(hdev); |
| 615 | |
| 616 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
| 617 | return 0; |
| 618 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 619 | err = hci_send_cmd(hdev, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod); |
| 620 | if (err == 0) |
| 621 | set_bit(HCI_PENDING_CLASS, &hdev->dev_flags); |
| 622 | |
| 623 | return err; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 624 | } |
| 625 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 626 | static void service_cache_off(struct work_struct *work) |
| 627 | { |
| 628 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 629 | service_cache.work); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 630 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 631 | if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 632 | return; |
| 633 | |
| 634 | hci_dev_lock(hdev); |
| 635 | |
| 636 | update_eir(hdev); |
| 637 | update_class(hdev); |
| 638 | |
| 639 | hci_dev_unlock(hdev); |
| 640 | } |
| 641 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 642 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 643 | { |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 644 | if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 645 | return; |
| 646 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 647 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 648 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 649 | /* Non-mgmt controlled devices get this bit set |
| 650 | * implicitly so that pairing works for them, however |
| 651 | * for mgmt we require user-space to explicitly enable |
| 652 | * it |
| 653 | */ |
| 654 | clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 655 | } |
| 656 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 657 | static int read_controller_info(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 658 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 659 | { |
| 660 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 661 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 662 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 663 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 664 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 665 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 666 | memset(&rp, 0, sizeof(rp)); |
| 667 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 668 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 669 | |
| 670 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 671 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 672 | |
| 673 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 674 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 675 | |
| 676 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 677 | |
| 678 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 679 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 680 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 681 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 682 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 683 | return cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 684 | sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 688 | { |
| 689 | sock_put(cmd->sk); |
| 690 | kfree(cmd->param); |
| 691 | kfree(cmd); |
| 692 | } |
| 693 | |
| 694 | static struct pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 695 | struct hci_dev *hdev, void *data, |
| 696 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 697 | { |
| 698 | struct pending_cmd *cmd; |
| 699 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 700 | cmd = kmalloc(sizeof(*cmd), GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 701 | if (!cmd) |
| 702 | return NULL; |
| 703 | |
| 704 | cmd->opcode = opcode; |
| 705 | cmd->index = hdev->id; |
| 706 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 707 | cmd->param = kmalloc(len, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 708 | if (!cmd->param) { |
| 709 | kfree(cmd); |
| 710 | return NULL; |
| 711 | } |
| 712 | |
| 713 | if (data) |
| 714 | memcpy(cmd->param, data, len); |
| 715 | |
| 716 | cmd->sk = sk; |
| 717 | sock_hold(sk); |
| 718 | |
| 719 | list_add(&cmd->list, &hdev->mgmt_pending); |
| 720 | |
| 721 | return cmd; |
| 722 | } |
| 723 | |
| 724 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 725 | void (*cb)(struct pending_cmd *cmd, |
| 726 | void *data), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 727 | void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 728 | { |
| 729 | struct list_head *p, *n; |
| 730 | |
| 731 | list_for_each_safe(p, n, &hdev->mgmt_pending) { |
| 732 | struct pending_cmd *cmd; |
| 733 | |
| 734 | cmd = list_entry(p, struct pending_cmd, list); |
| 735 | |
| 736 | if (opcode > 0 && cmd->opcode != opcode) |
| 737 | continue; |
| 738 | |
| 739 | cb(cmd, data); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev) |
| 744 | { |
| 745 | struct pending_cmd *cmd; |
| 746 | |
| 747 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 748 | if (cmd->opcode == opcode) |
| 749 | return cmd; |
| 750 | } |
| 751 | |
| 752 | return NULL; |
| 753 | } |
| 754 | |
| 755 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
| 756 | { |
| 757 | list_del(&cmd->list); |
| 758 | mgmt_pending_free(cmd); |
| 759 | } |
| 760 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 761 | static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 762 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 763 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 764 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 765 | return cmd_complete(sk, hdev->id, opcode, 0, &settings, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 766 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 767 | } |
| 768 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 769 | static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 770 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 771 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 772 | struct mgmt_mode *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 773 | struct pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 774 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 775 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 776 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 777 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 778 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 779 | |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 780 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
| 781 | cancel_delayed_work(&hdev->power_off); |
| 782 | |
| 783 | if (cp->val) { |
| 784 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
| 785 | mgmt_powered(hdev, 1); |
| 786 | goto failed; |
| 787 | } |
| 788 | } |
| 789 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 790 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 791 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 792 | goto failed; |
| 793 | } |
| 794 | |
| 795 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 796 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 797 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 798 | goto failed; |
| 799 | } |
| 800 | |
| 801 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 802 | if (!cmd) { |
| 803 | err = -ENOMEM; |
| 804 | goto failed; |
| 805 | } |
| 806 | |
| 807 | if (cp->val) |
Gustavo F. Padovan | 7f97104 | 2011-12-18 12:40:32 -0200 | [diff] [blame] | 808 | schedule_work(&hdev->power_on); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 809 | else |
Gustavo F. Padovan | 80b7ab3 | 2011-12-17 14:52:27 -0200 | [diff] [blame] | 810 | schedule_work(&hdev->power_off.work); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 811 | |
| 812 | err = 0; |
| 813 | |
| 814 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 815 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 816 | return err; |
| 817 | } |
| 818 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 819 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len, |
| 820 | struct sock *skip_sk) |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 821 | { |
| 822 | struct sk_buff *skb; |
| 823 | struct mgmt_hdr *hdr; |
| 824 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 825 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 826 | if (!skb) |
| 827 | return -ENOMEM; |
| 828 | |
| 829 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 830 | hdr->opcode = cpu_to_le16(event); |
| 831 | if (hdev) |
| 832 | hdr->index = cpu_to_le16(hdev->id); |
| 833 | else |
| 834 | hdr->index = cpu_to_le16(MGMT_INDEX_NONE); |
| 835 | hdr->len = cpu_to_le16(data_len); |
| 836 | |
| 837 | if (data) |
| 838 | memcpy(skb_put(skb, data_len), data, data_len); |
| 839 | |
Marcel Holtmann | 97e0bde | 2012-02-22 13:49:28 +0100 | [diff] [blame] | 840 | /* Time stamp */ |
| 841 | __net_timestamp(skb); |
| 842 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 843 | hci_send_to_control(skb, skip_sk); |
| 844 | kfree_skb(skb); |
| 845 | |
| 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 850 | { |
| 851 | __le32 ev; |
| 852 | |
| 853 | ev = cpu_to_le32(get_current_settings(hdev)); |
| 854 | |
| 855 | return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip); |
| 856 | } |
| 857 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 858 | static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 859 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 860 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 861 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 862 | struct pending_cmd *cmd; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 863 | u16 timeout; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 864 | u8 scan; |
| 865 | int err; |
| 866 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 867 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 868 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 869 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 24c54a9 | 2012-02-22 18:06:34 +0100 | [diff] [blame] | 870 | if (!cp->val && timeout > 0) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 871 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 872 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 873 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 874 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 875 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 876 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 877 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 878 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 879 | goto failed; |
| 880 | } |
| 881 | |
| 882 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 883 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 884 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 885 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 886 | goto failed; |
| 887 | } |
| 888 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 889 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 890 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 891 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 892 | goto failed; |
| 893 | } |
| 894 | |
| 895 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 896 | bool changed = false; |
| 897 | |
| 898 | if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { |
| 899 | change_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 900 | changed = true; |
| 901 | } |
| 902 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 903 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 904 | if (err < 0) |
| 905 | goto failed; |
| 906 | |
| 907 | if (changed) |
| 908 | err = new_settings(hdev, sk); |
| 909 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 910 | goto failed; |
| 911 | } |
| 912 | |
| 913 | if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 914 | if (hdev->discov_timeout > 0) { |
| 915 | cancel_delayed_work(&hdev->discov_off); |
| 916 | hdev->discov_timeout = 0; |
| 917 | } |
| 918 | |
| 919 | if (cp->val && timeout > 0) { |
| 920 | hdev->discov_timeout = timeout; |
| 921 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
| 922 | msecs_to_jiffies(hdev->discov_timeout * 1000)); |
| 923 | } |
| 924 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 925 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 926 | goto failed; |
| 927 | } |
| 928 | |
| 929 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 930 | if (!cmd) { |
| 931 | err = -ENOMEM; |
| 932 | goto failed; |
| 933 | } |
| 934 | |
| 935 | scan = SCAN_PAGE; |
| 936 | |
| 937 | if (cp->val) |
| 938 | scan |= SCAN_INQUIRY; |
| 939 | else |
| 940 | cancel_delayed_work(&hdev->discov_off); |
| 941 | |
| 942 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 943 | if (err < 0) |
| 944 | mgmt_pending_remove(cmd); |
| 945 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 946 | if (cp->val) |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 947 | hdev->discov_timeout = timeout; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 948 | |
| 949 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 950 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 951 | return err; |
| 952 | } |
| 953 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 954 | static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 955 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 956 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 957 | struct mgmt_mode *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 958 | struct pending_cmd *cmd; |
| 959 | u8 scan; |
| 960 | int err; |
| 961 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 962 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 963 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 964 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 965 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 966 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 967 | bool changed = false; |
| 968 | |
| 969 | if (!!cp->val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 970 | changed = true; |
| 971 | |
Andrei Emeltchenko | 6bf0e46 | 2012-02-22 13:21:16 +0200 | [diff] [blame] | 972 | if (cp->val) { |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 973 | set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Andrei Emeltchenko | 6bf0e46 | 2012-02-22 13:21:16 +0200 | [diff] [blame] | 974 | } else { |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 975 | clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 976 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 977 | } |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 978 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 979 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 980 | if (err < 0) |
| 981 | goto failed; |
| 982 | |
| 983 | if (changed) |
| 984 | err = new_settings(hdev, sk); |
| 985 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 986 | goto failed; |
| 987 | } |
| 988 | |
| 989 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 990 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 991 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 992 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 993 | goto failed; |
| 994 | } |
| 995 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 996 | if (!!cp->val == test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 997 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 998 | goto failed; |
| 999 | } |
| 1000 | |
| 1001 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1002 | if (!cmd) { |
| 1003 | err = -ENOMEM; |
| 1004 | goto failed; |
| 1005 | } |
| 1006 | |
Andrei Emeltchenko | 6bf0e46 | 2012-02-22 13:21:16 +0200 | [diff] [blame] | 1007 | if (cp->val) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1008 | scan = SCAN_PAGE; |
Andrei Emeltchenko | 6bf0e46 | 2012-02-22 13:21:16 +0200 | [diff] [blame] | 1009 | } else { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1010 | scan = 0; |
| 1011 | |
Johan Hedberg | df2c6c5 | 2012-02-21 19:15:49 +0200 | [diff] [blame] | 1012 | if (test_bit(HCI_ISCAN, &hdev->flags) && |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1013 | hdev->discov_timeout > 0) |
Johan Hedberg | df2c6c5 | 2012-02-21 19:15:49 +0200 | [diff] [blame] | 1014 | cancel_delayed_work(&hdev->discov_off); |
| 1015 | } |
| 1016 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1017 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1018 | if (err < 0) |
| 1019 | mgmt_pending_remove(cmd); |
| 1020 | |
| 1021 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1022 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1023 | return err; |
| 1024 | } |
| 1025 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1026 | static int set_pairable(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1027 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1028 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1029 | struct mgmt_mode *cp = data; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1030 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1031 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1032 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1033 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1034 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1035 | |
| 1036 | if (cp->val) |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1037 | set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1038 | else |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1039 | clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1040 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1041 | err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1042 | if (err < 0) |
| 1043 | goto failed; |
| 1044 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1045 | err = new_settings(hdev, sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1046 | |
| 1047 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1048 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1049 | return err; |
| 1050 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1051 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1052 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1053 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1054 | { |
| 1055 | struct mgmt_mode *cp = data; |
| 1056 | struct pending_cmd *cmd; |
Johan Hedberg | 816a11d | 2012-02-26 13:04:52 +0200 | [diff] [blame] | 1057 | u8 val; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1058 | int err; |
| 1059 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1060 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1061 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1062 | hci_dev_lock(hdev); |
| 1063 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1064 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1065 | bool changed = false; |
| 1066 | |
| 1067 | if (!!cp->val != test_bit(HCI_LINK_SECURITY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1068 | &hdev->dev_flags)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1069 | change_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 1070 | changed = true; |
| 1071 | } |
| 1072 | |
| 1073 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1074 | if (err < 0) |
| 1075 | goto failed; |
| 1076 | |
| 1077 | if (changed) |
| 1078 | err = new_settings(hdev, sk); |
| 1079 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1080 | goto failed; |
| 1081 | } |
| 1082 | |
| 1083 | if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1084 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1085 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1086 | goto failed; |
| 1087 | } |
| 1088 | |
| 1089 | val = !!cp->val; |
| 1090 | |
| 1091 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1092 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1093 | goto failed; |
| 1094 | } |
| 1095 | |
| 1096 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1097 | if (!cmd) { |
| 1098 | err = -ENOMEM; |
| 1099 | goto failed; |
| 1100 | } |
| 1101 | |
| 1102 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1103 | if (err < 0) { |
| 1104 | mgmt_pending_remove(cmd); |
| 1105 | goto failed; |
| 1106 | } |
| 1107 | |
| 1108 | failed: |
| 1109 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1110 | return err; |
| 1111 | } |
| 1112 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1113 | static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1114 | { |
| 1115 | struct mgmt_mode *cp = data; |
| 1116 | struct pending_cmd *cmd; |
Johan Hedberg | 816a11d | 2012-02-26 13:04:52 +0200 | [diff] [blame] | 1117 | u8 val; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1118 | int err; |
| 1119 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1120 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1121 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1122 | hci_dev_lock(hdev); |
| 1123 | |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1124 | if (!(hdev->features[6] & LMP_SIMPLE_PAIR)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1125 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1126 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1127 | goto failed; |
| 1128 | } |
| 1129 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1130 | val = !!cp->val; |
| 1131 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1132 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1133 | bool changed = false; |
| 1134 | |
| 1135 | if (val != test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 1136 | change_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 1137 | changed = true; |
| 1138 | } |
| 1139 | |
| 1140 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1141 | if (err < 0) |
| 1142 | goto failed; |
| 1143 | |
| 1144 | if (changed) |
| 1145 | err = new_settings(hdev, sk); |
| 1146 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1147 | goto failed; |
| 1148 | } |
| 1149 | |
| 1150 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev)) { |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 1151 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1152 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1153 | goto failed; |
| 1154 | } |
| 1155 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1156 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) == val) { |
| 1157 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1158 | goto failed; |
| 1159 | } |
| 1160 | |
| 1161 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1162 | if (!cmd) { |
| 1163 | err = -ENOMEM; |
| 1164 | goto failed; |
| 1165 | } |
| 1166 | |
| 1167 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, sizeof(val), &val); |
| 1168 | if (err < 0) { |
| 1169 | mgmt_pending_remove(cmd); |
| 1170 | goto failed; |
| 1171 | } |
| 1172 | |
| 1173 | failed: |
| 1174 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1175 | return err; |
| 1176 | } |
| 1177 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1178 | static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1179 | { |
| 1180 | struct mgmt_mode *cp = data; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1181 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1182 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1183 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1184 | if (!enable_hs) |
| 1185 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1186 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1187 | |
| 1188 | if (cp->val) |
| 1189 | set_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
| 1190 | else |
| 1191 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
| 1192 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1193 | return send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1194 | } |
| 1195 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1196 | static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1197 | { |
| 1198 | struct mgmt_mode *cp = data; |
| 1199 | struct hci_cp_write_le_host_supported hci_cp; |
| 1200 | struct pending_cmd *cmd; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1201 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1202 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1203 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1204 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1205 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1206 | hci_dev_lock(hdev); |
| 1207 | |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 1208 | if (!(hdev->features[4] & LMP_LE)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1209 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1210 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1211 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | val = !!cp->val; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1215 | enabled = !!(hdev->host_features[0] & LMP_HOST_LE); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1216 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1217 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1218 | bool changed = false; |
| 1219 | |
| 1220 | if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 1221 | change_bit(HCI_LE_ENABLED, &hdev->dev_flags); |
| 1222 | changed = true; |
| 1223 | } |
| 1224 | |
| 1225 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 1226 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1227 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1228 | |
| 1229 | if (changed) |
| 1230 | err = new_settings(hdev, sk); |
| 1231 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1232 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | if (mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1236 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1237 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1238 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 1242 | if (!cmd) { |
| 1243 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1244 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 1248 | |
| 1249 | if (val) { |
| 1250 | hci_cp.le = val; |
| 1251 | hci_cp.simul = !!(hdev->features[6] & LMP_SIMUL_LE_BR); |
| 1252 | } |
| 1253 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1254 | err = hci_send_cmd(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 1255 | &hci_cp); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 1256 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1257 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1258 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1259 | unlock: |
| 1260 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1261 | return err; |
| 1262 | } |
| 1263 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1264 | static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1265 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1266 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 1267 | struct pending_cmd *cmd; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1268 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1269 | int err; |
| 1270 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1271 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1272 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1273 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1274 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 1275 | if (test_bit(HCI_PENDING_CLASS, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1276 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1277 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 1278 | goto failed; |
| 1279 | } |
| 1280 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 1281 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1282 | if (!uuid) { |
| 1283 | err = -ENOMEM; |
| 1284 | goto failed; |
| 1285 | } |
| 1286 | |
| 1287 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1288 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1289 | |
| 1290 | list_add(&uuid->list, &hdev->uuids); |
| 1291 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1292 | err = update_class(hdev); |
| 1293 | if (err < 0) |
| 1294 | goto failed; |
| 1295 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1296 | err = update_eir(hdev); |
| 1297 | if (err < 0) |
| 1298 | goto failed; |
| 1299 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 1300 | if (!test_bit(HCI_PENDING_CLASS, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1301 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1302 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 1303 | goto failed; |
| 1304 | } |
| 1305 | |
| 1306 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 1307 | if (!cmd) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 1308 | err = -ENOMEM; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1309 | |
| 1310 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1311 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1312 | return err; |
| 1313 | } |
| 1314 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 1315 | static bool enable_service_cache(struct hci_dev *hdev) |
| 1316 | { |
| 1317 | if (!hdev_is_powered(hdev)) |
| 1318 | return false; |
| 1319 | |
| 1320 | if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 1321 | schedule_delayed_work(&hdev->service_cache, CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 1322 | return true; |
| 1323 | } |
| 1324 | |
| 1325 | return false; |
| 1326 | } |
| 1327 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1328 | static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1329 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1330 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1331 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 1332 | struct pending_cmd *cmd; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1333 | struct list_head *p, *n; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1334 | 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] | 1335 | int err, found; |
| 1336 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1337 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1338 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1339 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1340 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 1341 | if (test_bit(HCI_PENDING_CLASS, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1342 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1343 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 1344 | goto unlock; |
| 1345 | } |
| 1346 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1347 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
| 1348 | err = hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 1349 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 1350 | if (enable_service_cache(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1351 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1352 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 1353 | goto unlock; |
| 1354 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 1355 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 1356 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | found = 0; |
| 1360 | |
| 1361 | list_for_each_safe(p, n, &hdev->uuids) { |
| 1362 | struct bt_uuid *match = list_entry(p, struct bt_uuid, list); |
| 1363 | |
| 1364 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 1365 | continue; |
| 1366 | |
| 1367 | list_del(&match->list); |
| 1368 | found++; |
| 1369 | } |
| 1370 | |
| 1371 | if (found == 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1372 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1373 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1374 | goto unlock; |
| 1375 | } |
| 1376 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 1377 | update_class: |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1378 | err = update_class(hdev); |
| 1379 | if (err < 0) |
| 1380 | goto unlock; |
| 1381 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1382 | err = update_eir(hdev); |
| 1383 | if (err < 0) |
| 1384 | goto unlock; |
| 1385 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 1386 | if (!test_bit(HCI_PENDING_CLASS, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1387 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1388 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 1389 | goto unlock; |
| 1390 | } |
| 1391 | |
| 1392 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 1393 | if (!cmd) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 1394 | err = -ENOMEM; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1395 | |
| 1396 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1397 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1398 | return err; |
| 1399 | } |
| 1400 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1401 | static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1402 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1403 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1404 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 1405 | struct pending_cmd *cmd; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1406 | int err; |
| 1407 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1408 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1409 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1410 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1411 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 1412 | if (test_bit(HCI_PENDING_CLASS, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1413 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1414 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 1415 | goto unlock; |
| 1416 | } |
| 1417 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1418 | hdev->major_class = cp->major; |
| 1419 | hdev->minor_class = cp->minor; |
| 1420 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 1421 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1422 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1423 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 1424 | goto unlock; |
| 1425 | } |
| 1426 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1427 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1428 | hci_dev_unlock(hdev); |
| 1429 | cancel_delayed_work_sync(&hdev->service_cache); |
| 1430 | hci_dev_lock(hdev); |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 1431 | update_eir(hdev); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1432 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 1433 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1434 | err = update_class(hdev); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 1435 | if (err < 0) |
| 1436 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1437 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 1438 | if (!test_bit(HCI_PENDING_CLASS, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1439 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1440 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 1441 | goto unlock; |
| 1442 | } |
| 1443 | |
| 1444 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 1445 | if (!cmd) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 1446 | err = -ENOMEM; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1447 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 1448 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1449 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1450 | return err; |
| 1451 | } |
| 1452 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1453 | static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1454 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1455 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1456 | struct mgmt_cp_load_link_keys *cp = data; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1457 | u16 key_count, expected_len; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 1458 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1459 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1460 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1461 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 1462 | expected_len = sizeof(*cp) + key_count * |
| 1463 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 1464 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 1465 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1466 | len, expected_len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1467 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1468 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1469 | } |
| 1470 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1471 | BT_DBG("%s debug_keys %u key_count %u", hdev->name, cp->debug_keys, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1472 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1473 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1474 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1475 | |
| 1476 | hci_link_keys_clear(hdev); |
| 1477 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1478 | set_bit(HCI_LINK_KEYS, &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1479 | |
| 1480 | if (cp->debug_keys) |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1481 | set_bit(HCI_DEBUG_KEYS, &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1482 | else |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1483 | clear_bit(HCI_DEBUG_KEYS, &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1484 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 1485 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 1486 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1487 | |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 1488 | hci_add_link_key(hdev, NULL, 0, &key->addr.bdaddr, key->val, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1489 | key->type, key->pin_len); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1490 | } |
| 1491 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1492 | cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0); |
Johan Hedberg | 0e5f875 | 2011-11-11 16:18:54 +0200 | [diff] [blame] | 1493 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1494 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1495 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 1496 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1497 | } |
| 1498 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 1499 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1500 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 1501 | { |
| 1502 | struct mgmt_ev_device_unpaired ev; |
| 1503 | |
| 1504 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 1505 | ev.addr.type = addr_type; |
| 1506 | |
| 1507 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1508 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 1509 | } |
| 1510 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1511 | static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1512 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1513 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 1514 | struct mgmt_cp_unpair_device *cp = data; |
| 1515 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 1516 | struct hci_cp_disconnect dc; |
| 1517 | struct pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1518 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1519 | int err; |
| 1520 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1521 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1522 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 1523 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 1524 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 1525 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 1526 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 1527 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1528 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1529 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 1530 | goto unlock; |
| 1531 | } |
| 1532 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 1533 | if (cp->addr.type == BDADDR_BREDR) |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 1534 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
| 1535 | else |
| 1536 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr); |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 1537 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1538 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1539 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1540 | MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1541 | goto unlock; |
| 1542 | } |
| 1543 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 1544 | if (cp->disconnect) { |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 1545 | if (cp->addr.type == BDADDR_BREDR) |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 1546 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1547 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 1548 | else |
| 1549 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1550 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 1551 | } else { |
| 1552 | conn = NULL; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1553 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1554 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 1555 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1556 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1557 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 1558 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 1559 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1560 | } |
| 1561 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 1562 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1563 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 1564 | if (!cmd) { |
| 1565 | err = -ENOMEM; |
| 1566 | goto unlock; |
| 1567 | } |
| 1568 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 1569 | dc.handle = cpu_to_le16(conn->handle); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 1570 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 1571 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1572 | if (err < 0) |
| 1573 | mgmt_pending_remove(cmd); |
| 1574 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1575 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1576 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1577 | return err; |
| 1578 | } |
| 1579 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1580 | static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1581 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1582 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1583 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1584 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1585 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1586 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1587 | int err; |
| 1588 | |
| 1589 | BT_DBG(""); |
| 1590 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1591 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1592 | |
| 1593 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1594 | err = cmd_status(sk, hdev->id, MGMT_OP_DISCONNECT, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1595 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1596 | goto failed; |
| 1597 | } |
| 1598 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1599 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1600 | err = cmd_status(sk, hdev->id, MGMT_OP_DISCONNECT, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1601 | MGMT_STATUS_BUSY); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1602 | goto failed; |
| 1603 | } |
| 1604 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 1605 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1606 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 1607 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 1608 | else |
| 1609 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
Vinicius Costa Gomes | 365227e | 2011-05-06 18:41:44 -0300 | [diff] [blame] | 1610 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 1611 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1612 | err = cmd_status(sk, hdev->id, MGMT_OP_DISCONNECT, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1613 | MGMT_STATUS_NOT_CONNECTED); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1614 | goto failed; |
| 1615 | } |
| 1616 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1617 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1618 | if (!cmd) { |
| 1619 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1620 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1621 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1622 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 1623 | dc.handle = cpu_to_le16(conn->handle); |
Andre Guedes | 3701f94 | 2012-06-11 18:41:12 -0300 | [diff] [blame] | 1624 | dc.reason = HCI_ERROR_REMOTE_USER_TERM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1625 | |
| 1626 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1627 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1628 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1629 | |
| 1630 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1631 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1632 | return err; |
| 1633 | } |
| 1634 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 1635 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1636 | { |
| 1637 | switch (link_type) { |
| 1638 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 1639 | switch (addr_type) { |
| 1640 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 1641 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 1642 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 1643 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 1644 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 1645 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 1646 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 1647 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1648 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 1649 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 1650 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1651 | } |
| 1652 | } |
| 1653 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1654 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1655 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1656 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1657 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1658 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 1659 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 1660 | int err; |
| 1661 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1662 | |
| 1663 | BT_DBG(""); |
| 1664 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1665 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1666 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 1667 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1668 | err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1669 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 1670 | goto unlock; |
| 1671 | } |
| 1672 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 1673 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 1674 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 1675 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 1676 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1677 | } |
| 1678 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 1679 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 1680 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 1681 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1682 | err = -ENOMEM; |
| 1683 | goto unlock; |
| 1684 | } |
| 1685 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1686 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1687 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 1688 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 1689 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1690 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 1691 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 1692 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1693 | continue; |
| 1694 | i++; |
| 1695 | } |
| 1696 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 1697 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 1698 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1699 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 1700 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1701 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1702 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1703 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1704 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 1705 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 1706 | |
| 1707 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1708 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1709 | return err; |
| 1710 | } |
| 1711 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1712 | static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1713 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1714 | { |
| 1715 | struct pending_cmd *cmd; |
| 1716 | int err; |
| 1717 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1718 | cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1719 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1720 | if (!cmd) |
| 1721 | return -ENOMEM; |
| 1722 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 1723 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1724 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1725 | if (err < 0) |
| 1726 | mgmt_pending_remove(cmd); |
| 1727 | |
| 1728 | return err; |
| 1729 | } |
| 1730 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1731 | static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1732 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1733 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1734 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1735 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1736 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1737 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1738 | int err; |
| 1739 | |
| 1740 | BT_DBG(""); |
| 1741 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1742 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1743 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1744 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1745 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1746 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1747 | goto failed; |
| 1748 | } |
| 1749 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 1750 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1751 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1752 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1753 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1754 | goto failed; |
| 1755 | } |
| 1756 | |
| 1757 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 1758 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 1759 | |
| 1760 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1761 | |
| 1762 | BT_ERR("PIN code is not 16 bytes long"); |
| 1763 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1764 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1765 | if (err >= 0) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1766 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1767 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1768 | |
| 1769 | goto failed; |
| 1770 | } |
| 1771 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 1772 | cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1773 | if (!cmd) { |
| 1774 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1775 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1776 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1777 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 1778 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1779 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 1780 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1781 | |
| 1782 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 1783 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1784 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1785 | |
| 1786 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1787 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1788 | return err; |
| 1789 | } |
| 1790 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1791 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1792 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1793 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1794 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1795 | |
| 1796 | BT_DBG(""); |
| 1797 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1798 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1799 | |
| 1800 | hdev->io_capability = cp->io_capability; |
| 1801 | |
| 1802 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1803 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1804 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1805 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1806 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1807 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL, |
| 1808 | 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1809 | } |
| 1810 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 1811 | static struct pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1812 | { |
| 1813 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1814 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1815 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1816 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1817 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 1818 | continue; |
| 1819 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1820 | if (cmd->user_data != conn) |
| 1821 | continue; |
| 1822 | |
| 1823 | return cmd; |
| 1824 | } |
| 1825 | |
| 1826 | return NULL; |
| 1827 | } |
| 1828 | |
| 1829 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 1830 | { |
| 1831 | struct mgmt_rp_pair_device rp; |
| 1832 | struct hci_conn *conn = cmd->user_data; |
| 1833 | |
Johan Hedberg | ba4e564 | 2011-11-11 00:07:34 +0200 | [diff] [blame] | 1834 | bacpy(&rp.addr.bdaddr, &conn->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 1835 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1836 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 1837 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1838 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1839 | |
| 1840 | /* So we don't get further callbacks for this connection */ |
| 1841 | conn->connect_cfm_cb = NULL; |
| 1842 | conn->security_cfm_cb = NULL; |
| 1843 | conn->disconn_cfm_cb = NULL; |
| 1844 | |
| 1845 | hci_conn_put(conn); |
| 1846 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1847 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1848 | } |
| 1849 | |
| 1850 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 1851 | { |
| 1852 | struct pending_cmd *cmd; |
| 1853 | |
| 1854 | BT_DBG("status %u", status); |
| 1855 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 1856 | cmd = find_pairing(conn); |
| 1857 | if (!cmd) |
| 1858 | BT_DBG("Unable to find a pending command"); |
| 1859 | else |
Johan Hedberg | e211326 | 2012-02-18 15:20:03 +0200 | [diff] [blame] | 1860 | pairing_complete(cmd, mgmt_status(status)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1861 | } |
| 1862 | |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 1863 | static void le_connect_complete_cb(struct hci_conn *conn, u8 status) |
| 1864 | { |
| 1865 | struct pending_cmd *cmd; |
| 1866 | |
| 1867 | BT_DBG("status %u", status); |
| 1868 | |
| 1869 | if (!status) |
| 1870 | return; |
| 1871 | |
| 1872 | cmd = find_pairing(conn); |
| 1873 | if (!cmd) |
| 1874 | BT_DBG("Unable to find a pending command"); |
| 1875 | else |
| 1876 | pairing_complete(cmd, mgmt_status(status)); |
| 1877 | } |
| 1878 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1879 | static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1880 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1881 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1882 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 1883 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1884 | struct pending_cmd *cmd; |
| 1885 | u8 sec_level, auth_type; |
| 1886 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1887 | int err; |
| 1888 | |
| 1889 | BT_DBG(""); |
| 1890 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1891 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1892 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 1893 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1894 | err = cmd_status(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1895 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 1896 | goto unlock; |
| 1897 | } |
| 1898 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 1899 | sec_level = BT_SECURITY_MEDIUM; |
| 1900 | if (cp->io_cap == 0x03) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1901 | auth_type = HCI_AT_DEDICATED_BONDING; |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 1902 | else |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1903 | auth_type = HCI_AT_DEDICATED_BONDING_MITM; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1904 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 1905 | if (cp->addr.type == BDADDR_BREDR) |
Andre Guedes | b12f62c | 2012-04-24 21:02:54 -0300 | [diff] [blame] | 1906 | conn = hci_connect(hdev, ACL_LINK, &cp->addr.bdaddr, |
| 1907 | cp->addr.type, sec_level, auth_type); |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 1908 | else |
Andre Guedes | b12f62c | 2012-04-24 21:02:54 -0300 | [diff] [blame] | 1909 | conn = hci_connect(hdev, LE_LINK, &cp->addr.bdaddr, |
| 1910 | cp->addr.type, sec_level, auth_type); |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 1911 | |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 1912 | memset(&rp, 0, sizeof(rp)); |
| 1913 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 1914 | rp.addr.type = cp->addr.type; |
| 1915 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1916 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 1917 | int status; |
| 1918 | |
| 1919 | if (PTR_ERR(conn) == -EBUSY) |
| 1920 | status = MGMT_STATUS_BUSY; |
| 1921 | else |
| 1922 | status = MGMT_STATUS_CONNECT_FAILED; |
| 1923 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1924 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 1925 | status, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1926 | sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1927 | goto unlock; |
| 1928 | } |
| 1929 | |
| 1930 | if (conn->connect_cfm_cb) { |
| 1931 | hci_conn_put(conn); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1932 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1933 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1934 | goto unlock; |
| 1935 | } |
| 1936 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 1937 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1938 | if (!cmd) { |
| 1939 | err = -ENOMEM; |
| 1940 | hci_conn_put(conn); |
| 1941 | goto unlock; |
| 1942 | } |
| 1943 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 1944 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 1945 | if (cp->addr.type == BDADDR_BREDR) |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 1946 | conn->connect_cfm_cb = pairing_complete_cb; |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 1947 | else |
| 1948 | conn->connect_cfm_cb = le_connect_complete_cb; |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 1949 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1950 | conn->security_cfm_cb = pairing_complete_cb; |
| 1951 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 1952 | conn->io_capability = cp->io_cap; |
| 1953 | cmd->user_data = conn; |
| 1954 | |
| 1955 | if (conn->state == BT_CONNECTED && |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1956 | hci_conn_security(conn, sec_level, auth_type)) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1957 | pairing_complete(cmd, 0); |
| 1958 | |
| 1959 | err = 0; |
| 1960 | |
| 1961 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1962 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1963 | return err; |
| 1964 | } |
| 1965 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1966 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1967 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 1968 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 1969 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 1970 | struct pending_cmd *cmd; |
| 1971 | struct hci_conn *conn; |
| 1972 | int err; |
| 1973 | |
| 1974 | BT_DBG(""); |
| 1975 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 1976 | hci_dev_lock(hdev); |
| 1977 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 1978 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1979 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1980 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 1981 | goto unlock; |
| 1982 | } |
| 1983 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 1984 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 1985 | if (!cmd) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1986 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1987 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 1988 | goto unlock; |
| 1989 | } |
| 1990 | |
| 1991 | conn = cmd->user_data; |
| 1992 | |
| 1993 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1994 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1995 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 1996 | goto unlock; |
| 1997 | } |
| 1998 | |
| 1999 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 2000 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2001 | err = cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2002 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2003 | unlock: |
| 2004 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2005 | return err; |
| 2006 | } |
| 2007 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2008 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2009 | bdaddr_t *bdaddr, u8 type, u16 mgmt_op, |
| 2010 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2011 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2012 | struct pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2013 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2014 | int err; |
| 2015 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2016 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 2017 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2018 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2019 | err = cmd_status(sk, hdev->id, mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2020 | MGMT_STATUS_NOT_POWERED); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2021 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2022 | } |
| 2023 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2024 | if (type == BDADDR_BREDR) |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 2025 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, bdaddr); |
| 2026 | else |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2027 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2028 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 2029 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2030 | err = cmd_status(sk, hdev->id, mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2031 | MGMT_STATUS_NOT_CONNECTED); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 2032 | goto done; |
| 2033 | } |
| 2034 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2035 | if (type == BDADDR_LE_PUBLIC || type == BDADDR_LE_RANDOM) { |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2036 | /* Continue with pairing via SMP */ |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 2037 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2038 | |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 2039 | if (!err) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2040 | err = cmd_status(sk, hdev->id, mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2041 | MGMT_STATUS_SUCCESS); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 2042 | else |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2043 | err = cmd_status(sk, hdev->id, mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2044 | MGMT_STATUS_FAILED); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 2045 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2046 | goto done; |
| 2047 | } |
| 2048 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2049 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, bdaddr, sizeof(*bdaddr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2050 | if (!cmd) { |
| 2051 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2052 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2053 | } |
| 2054 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2055 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2056 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 2057 | struct hci_cp_user_passkey_reply cp; |
| 2058 | |
| 2059 | bacpy(&cp.bdaddr, bdaddr); |
| 2060 | cp.passkey = passkey; |
| 2061 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 2062 | } else |
| 2063 | err = hci_send_cmd(hdev, hci_op, sizeof(*bdaddr), bdaddr); |
| 2064 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2065 | if (err < 0) |
| 2066 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2067 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2068 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2069 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2070 | return err; |
| 2071 | } |
| 2072 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 2073 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 2074 | void *data, u16 len) |
| 2075 | { |
| 2076 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 2077 | |
| 2078 | BT_DBG(""); |
| 2079 | |
| 2080 | return user_pairing_resp(sk, hdev, &cp->addr.bdaddr, cp->addr.type, |
| 2081 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 2082 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 2083 | } |
| 2084 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2085 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2086 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2087 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2088 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2089 | |
| 2090 | BT_DBG(""); |
| 2091 | |
| 2092 | if (len != sizeof(*cp)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2093 | return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2094 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2095 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2096 | return user_pairing_resp(sk, hdev, &cp->addr.bdaddr, cp->addr.type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2097 | MGMT_OP_USER_CONFIRM_REPLY, |
| 2098 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2099 | } |
| 2100 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2101 | static int user_confirm_neg_reply(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2102 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2103 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 2104 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2105 | |
| 2106 | BT_DBG(""); |
| 2107 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2108 | return user_pairing_resp(sk, hdev, &cp->addr.bdaddr, cp->addr.type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2109 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 2110 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2111 | } |
| 2112 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2113 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2114 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2115 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2116 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2117 | |
| 2118 | BT_DBG(""); |
| 2119 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2120 | return user_pairing_resp(sk, hdev, &cp->addr.bdaddr, cp->addr.type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2121 | MGMT_OP_USER_PASSKEY_REPLY, |
| 2122 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2123 | } |
| 2124 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2125 | static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2126 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2127 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2128 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2129 | |
| 2130 | BT_DBG(""); |
| 2131 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2132 | return user_pairing_resp(sk, hdev, &cp->addr.bdaddr, cp->addr.type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2133 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 2134 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2135 | } |
| 2136 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 2137 | static int update_name(struct hci_dev *hdev, const char *name) |
| 2138 | { |
| 2139 | struct hci_cp_write_local_name cp; |
| 2140 | |
| 2141 | memcpy(cp.name, name, sizeof(cp.name)); |
| 2142 | |
| 2143 | return hci_send_cmd(hdev, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
| 2144 | } |
| 2145 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2146 | static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2147 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2148 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 2149 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2150 | struct pending_cmd *cmd; |
| 2151 | int err; |
| 2152 | |
| 2153 | BT_DBG(""); |
| 2154 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2155 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2156 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 2157 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 2158 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2159 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 2160 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 2161 | |
| 2162 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2163 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 2164 | if (err < 0) |
| 2165 | goto failed; |
| 2166 | |
| 2167 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2168 | sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 2169 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2170 | goto failed; |
| 2171 | } |
| 2172 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 2173 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2174 | if (!cmd) { |
| 2175 | err = -ENOMEM; |
| 2176 | goto failed; |
| 2177 | } |
| 2178 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 2179 | err = update_name(hdev, cp->name); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2180 | if (err < 0) |
| 2181 | mgmt_pending_remove(cmd); |
| 2182 | |
| 2183 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2184 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2185 | return err; |
| 2186 | } |
| 2187 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 2188 | static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2189 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2190 | { |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2191 | struct pending_cmd *cmd; |
| 2192 | int err; |
| 2193 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2194 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2195 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2196 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2197 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2198 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2199 | err = cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2200 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2201 | goto unlock; |
| 2202 | } |
| 2203 | |
| 2204 | if (!(hdev->features[6] & LMP_SIMPLE_PAIR)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2205 | err = cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2206 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2207 | goto unlock; |
| 2208 | } |
| 2209 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2210 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2211 | err = cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2212 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2213 | goto unlock; |
| 2214 | } |
| 2215 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2216 | cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2217 | if (!cmd) { |
| 2218 | err = -ENOMEM; |
| 2219 | goto unlock; |
| 2220 | } |
| 2221 | |
| 2222 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 2223 | if (err < 0) |
| 2224 | mgmt_pending_remove(cmd); |
| 2225 | |
| 2226 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2227 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2228 | return err; |
| 2229 | } |
| 2230 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2231 | static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2232 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2233 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2234 | struct mgmt_cp_add_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 2235 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2236 | int err; |
| 2237 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2238 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2239 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2240 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2241 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2242 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2243 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2244 | MGMT_STATUS_NOT_POWERED, &cp->addr, |
| 2245 | sizeof(cp->addr)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2246 | goto unlock; |
| 2247 | } |
| 2248 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 2249 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, cp->hash, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2250 | cp->randomizer); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2251 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 2252 | status = MGMT_STATUS_FAILED; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2253 | else |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 2254 | status = 0; |
| 2255 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2256 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2257 | &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2258 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2259 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2260 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2261 | return err; |
| 2262 | } |
| 2263 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2264 | static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2265 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2266 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2267 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 2268 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2269 | int err; |
| 2270 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2271 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2272 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2273 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2274 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2275 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2276 | err = cmd_complete(sk, hdev->id, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2277 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 2278 | MGMT_STATUS_NOT_POWERED, &cp->addr, |
| 2279 | sizeof(cp->addr)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2280 | goto unlock; |
| 2281 | } |
| 2282 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 2283 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2284 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 2285 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2286 | else |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 2287 | status = 0; |
| 2288 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2289 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2290 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2291 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2292 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2293 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2294 | return err; |
| 2295 | } |
| 2296 | |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 2297 | int mgmt_interleaved_discovery(struct hci_dev *hdev) |
| 2298 | { |
| 2299 | int err; |
| 2300 | |
| 2301 | BT_DBG("%s", hdev->name); |
| 2302 | |
| 2303 | hci_dev_lock(hdev); |
| 2304 | |
| 2305 | err = hci_do_inquiry(hdev, INQUIRY_LEN_BREDR_LE); |
| 2306 | if (err < 0) |
| 2307 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 2308 | |
| 2309 | hci_dev_unlock(hdev); |
| 2310 | |
| 2311 | return err; |
| 2312 | } |
| 2313 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2314 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2315 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2316 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2317 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2318 | struct pending_cmd *cmd; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2319 | int err; |
| 2320 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2321 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2322 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2323 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2324 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2325 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2326 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2327 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 2328 | goto failed; |
| 2329 | } |
| 2330 | |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 2331 | if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { |
| 2332 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 2333 | MGMT_STATUS_BUSY); |
| 2334 | goto failed; |
| 2335 | } |
| 2336 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 2337 | if (hdev->discovery.state != DISCOVERY_STOPPED) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2338 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2339 | MGMT_STATUS_BUSY); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 2340 | goto failed; |
| 2341 | } |
| 2342 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2343 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2344 | if (!cmd) { |
| 2345 | err = -ENOMEM; |
| 2346 | goto failed; |
| 2347 | } |
| 2348 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 2349 | hdev->discovery.type = cp->type; |
| 2350 | |
| 2351 | switch (hdev->discovery.type) { |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 2352 | case DISCOV_TYPE_BREDR: |
Andre Guedes | 8b90129 | 2012-02-23 18:09:27 -0300 | [diff] [blame] | 2353 | if (lmp_bredr_capable(hdev)) |
| 2354 | err = hci_do_inquiry(hdev, INQUIRY_LEN_BREDR); |
| 2355 | else |
| 2356 | err = -ENOTSUPP; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 2357 | break; |
| 2358 | |
| 2359 | case DISCOV_TYPE_LE: |
Andre Guedes | 8b90129 | 2012-02-23 18:09:27 -0300 | [diff] [blame] | 2360 | if (lmp_host_le_capable(hdev)) |
| 2361 | err = hci_le_scan(hdev, LE_SCAN_TYPE, LE_SCAN_INT, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2362 | LE_SCAN_WIN, LE_SCAN_TIMEOUT_LE_ONLY); |
Andre Guedes | 8b90129 | 2012-02-23 18:09:27 -0300 | [diff] [blame] | 2363 | else |
| 2364 | err = -ENOTSUPP; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 2365 | break; |
| 2366 | |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 2367 | case DISCOV_TYPE_INTERLEAVED: |
Andre Guedes | 426c189 | 2012-02-24 11:41:04 -0300 | [diff] [blame] | 2368 | if (lmp_host_le_capable(hdev) && lmp_bredr_capable(hdev)) |
| 2369 | err = hci_le_scan(hdev, LE_SCAN_TYPE, LE_SCAN_INT, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2370 | LE_SCAN_WIN, |
| 2371 | LE_SCAN_TIMEOUT_BREDR_LE); |
Andre Guedes | 426c189 | 2012-02-24 11:41:04 -0300 | [diff] [blame] | 2372 | else |
| 2373 | err = -ENOTSUPP; |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 2374 | break; |
| 2375 | |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 2376 | default: |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 2377 | err = -EINVAL; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 2378 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 2379 | |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2380 | if (err < 0) |
| 2381 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 2382 | else |
| 2383 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2384 | |
| 2385 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2386 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2387 | return err; |
| 2388 | } |
| 2389 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2390 | static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2391 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2392 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 2393 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2394 | struct pending_cmd *cmd; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2395 | struct hci_cp_remote_name_req_cancel cp; |
| 2396 | struct inquiry_entry *e; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2397 | int err; |
| 2398 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2399 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2400 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2401 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2402 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2403 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2404 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2405 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 2406 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 2407 | goto unlock; |
| 2408 | } |
| 2409 | |
| 2410 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2411 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2412 | MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type, |
| 2413 | sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2414 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 2415 | } |
| 2416 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2417 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2418 | if (!cmd) { |
| 2419 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2420 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2421 | } |
| 2422 | |
Andre Guedes | e0d9727e | 2012-03-20 15:15:36 -0300 | [diff] [blame] | 2423 | switch (hdev->discovery.state) { |
| 2424 | case DISCOVERY_FINDING: |
Andre Guedes | c9ecc48 | 2012-03-15 16:52:08 -0300 | [diff] [blame] | 2425 | if (test_bit(HCI_INQUIRY, &hdev->flags)) |
| 2426 | err = hci_cancel_inquiry(hdev); |
| 2427 | else |
| 2428 | err = hci_cancel_le_scan(hdev); |
| 2429 | |
Andre Guedes | e0d9727e | 2012-03-20 15:15:36 -0300 | [diff] [blame] | 2430 | break; |
| 2431 | |
| 2432 | case DISCOVERY_RESOLVING: |
| 2433 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2434 | NAME_PENDING); |
Andre Guedes | e0d9727e | 2012-03-20 15:15:36 -0300 | [diff] [blame] | 2435 | if (!e) { |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2436 | mgmt_pending_remove(cmd); |
Andre Guedes | e0d9727e | 2012-03-20 15:15:36 -0300 | [diff] [blame] | 2437 | err = cmd_complete(sk, hdev->id, |
| 2438 | MGMT_OP_STOP_DISCOVERY, 0, |
| 2439 | &mgmt_cp->type, |
| 2440 | sizeof(mgmt_cp->type)); |
| 2441 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 2442 | goto unlock; |
| 2443 | } |
| 2444 | |
| 2445 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
| 2446 | err = hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL, |
| 2447 | sizeof(cp), &cp); |
| 2448 | |
| 2449 | break; |
| 2450 | |
| 2451 | default: |
| 2452 | BT_DBG("unknown discovery state %u", hdev->discovery.state); |
| 2453 | err = -EFAULT; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2454 | } |
| 2455 | |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2456 | if (err < 0) |
| 2457 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 2458 | else |
| 2459 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2460 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2461 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2462 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 2463 | return err; |
| 2464 | } |
| 2465 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2466 | static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2467 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2468 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2469 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2470 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2471 | int err; |
| 2472 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2473 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2474 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2475 | hci_dev_lock(hdev); |
| 2476 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2477 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2478 | err = cmd_status(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2479 | MGMT_STATUS_FAILED); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 2480 | goto failed; |
| 2481 | } |
| 2482 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 2483 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2484 | if (!e) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2485 | err = cmd_status(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2486 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2487 | goto failed; |
| 2488 | } |
| 2489 | |
| 2490 | if (cp->name_known) { |
| 2491 | e->name_state = NAME_KNOWN; |
| 2492 | list_del(&e->list); |
| 2493 | } else { |
| 2494 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20a | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 2495 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2496 | } |
| 2497 | |
| 2498 | err = 0; |
| 2499 | |
| 2500 | failed: |
| 2501 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 2502 | return err; |
| 2503 | } |
| 2504 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2505 | static int block_device(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2506 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2507 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2508 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 2509 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2510 | int err; |
| 2511 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2512 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2513 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2514 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2515 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 2516 | err = hci_blacklist_add(hdev, &cp->addr.bdaddr, cp->addr.type); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2517 | if (err < 0) |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 2518 | status = MGMT_STATUS_FAILED; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2519 | else |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 2520 | status = 0; |
| 2521 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2522 | err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2523 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2524 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2525 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2526 | |
| 2527 | return err; |
| 2528 | } |
| 2529 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2530 | static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2531 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2532 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2533 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 2534 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2535 | int err; |
| 2536 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2537 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2538 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2539 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2540 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 2541 | err = hci_blacklist_del(hdev, &cp->addr.bdaddr, cp->addr.type); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2542 | if (err < 0) |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 2543 | status = MGMT_STATUS_INVALID_PARAMS; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2544 | else |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 2545 | status = 0; |
| 2546 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2547 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2548 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 2549 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2550 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2551 | |
| 2552 | return err; |
| 2553 | } |
| 2554 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 2555 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2556 | u16 len) |
| 2557 | { |
| 2558 | struct mgmt_cp_set_device_id *cp = data; |
| 2559 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 2560 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 2561 | |
| 2562 | BT_DBG("%s", hdev->name); |
| 2563 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 2564 | source = __le16_to_cpu(cp->source); |
| 2565 | |
| 2566 | if (source > 0x0002) |
| 2567 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 2568 | MGMT_STATUS_INVALID_PARAMS); |
| 2569 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 2570 | hci_dev_lock(hdev); |
| 2571 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 2572 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 2573 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 2574 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 2575 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 2576 | |
| 2577 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0); |
| 2578 | |
| 2579 | update_eir(hdev); |
| 2580 | |
| 2581 | hci_dev_unlock(hdev); |
| 2582 | |
| 2583 | return err; |
| 2584 | } |
| 2585 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2586 | static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2587 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2588 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2589 | struct mgmt_mode *cp = data; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2590 | struct hci_cp_write_page_scan_activity acp; |
| 2591 | u8 type; |
| 2592 | int err; |
| 2593 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2594 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2595 | |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 2596 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2597 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2598 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 2599 | |
| 2600 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2601 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2602 | MGMT_STATUS_REJECTED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2603 | |
| 2604 | hci_dev_lock(hdev); |
| 2605 | |
Johan Hedberg | f7c6869ce | 2011-12-15 00:47:36 +0200 | [diff] [blame] | 2606 | if (cp->val) { |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2607 | type = PAGE_SCAN_TYPE_INTERLACED; |
Andrei Emeltchenko | 76ec9de | 2012-03-12 12:13:11 +0200 | [diff] [blame] | 2608 | |
Johan Hedberg | 83ce9a06 | 2012-06-28 13:44:30 +0300 | [diff] [blame] | 2609 | /* 160 msec page scan interval */ |
| 2610 | acp.interval = __constant_cpu_to_le16(0x0100); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2611 | } else { |
| 2612 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
Andrei Emeltchenko | 76ec9de | 2012-03-12 12:13:11 +0200 | [diff] [blame] | 2613 | |
| 2614 | /* default 1.28 sec page scan */ |
| 2615 | acp.interval = __constant_cpu_to_le16(0x0800); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2616 | } |
| 2617 | |
Andrei Emeltchenko | 76ec9de | 2012-03-12 12:13:11 +0200 | [diff] [blame] | 2618 | /* default 11.25 msec page scan window */ |
| 2619 | acp.window = __constant_cpu_to_le16(0x0012); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2620 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2621 | err = hci_send_cmd(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, sizeof(acp), |
| 2622 | &acp); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2623 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2624 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2625 | MGMT_STATUS_FAILED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2626 | goto done; |
| 2627 | } |
| 2628 | |
| 2629 | err = hci_send_cmd(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
| 2630 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2631 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2632 | MGMT_STATUS_FAILED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2633 | goto done; |
| 2634 | } |
| 2635 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2636 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2637 | NULL, 0); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2638 | done: |
| 2639 | hci_dev_unlock(hdev); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 2640 | return err; |
| 2641 | } |
| 2642 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2643 | static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2644 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 2645 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 2646 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
| 2647 | u16 key_count, expected_len; |
| 2648 | int i; |
| 2649 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2650 | key_count = __le16_to_cpu(cp->key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 2651 | |
| 2652 | expected_len = sizeof(*cp) + key_count * |
| 2653 | sizeof(struct mgmt_ltk_info); |
| 2654 | if (expected_len != len) { |
| 2655 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2656 | len, expected_len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2657 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2658 | EINVAL); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 2659 | } |
| 2660 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2661 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 2662 | |
| 2663 | hci_dev_lock(hdev); |
| 2664 | |
| 2665 | hci_smp_ltks_clear(hdev); |
| 2666 | |
| 2667 | for (i = 0; i < key_count; i++) { |
| 2668 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 2669 | u8 type; |
| 2670 | |
| 2671 | if (key->master) |
| 2672 | type = HCI_SMP_LTK; |
| 2673 | else |
| 2674 | type = HCI_SMP_LTK_SLAVE; |
| 2675 | |
Hemant Gupta | 4596fde | 2012-04-16 14:57:40 +0530 | [diff] [blame] | 2676 | hci_add_ltk(hdev, &key->addr.bdaddr, |
Andre Guedes | 378b5b7 | 2012-04-24 21:02:51 -0300 | [diff] [blame] | 2677 | bdaddr_to_le(key->addr.type), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2678 | type, 0, key->authenticated, key->val, |
| 2679 | key->enc_size, key->ediv, key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 2680 | } |
| 2681 | |
| 2682 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 2683 | |
| 2684 | return 0; |
| 2685 | } |
| 2686 | |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 2687 | static const struct mgmt_handler { |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2688 | int (*func) (struct sock *sk, struct hci_dev *hdev, void *data, |
| 2689 | u16 data_len); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 2690 | bool var_len; |
| 2691 | size_t data_len; |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 2692 | } mgmt_handlers[] = { |
| 2693 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 2694 | { read_version, false, MGMT_READ_VERSION_SIZE }, |
| 2695 | { read_commands, false, MGMT_READ_COMMANDS_SIZE }, |
| 2696 | { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE }, |
| 2697 | { read_controller_info, false, MGMT_READ_INFO_SIZE }, |
| 2698 | { set_powered, false, MGMT_SETTING_SIZE }, |
| 2699 | { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE }, |
| 2700 | { set_connectable, false, MGMT_SETTING_SIZE }, |
| 2701 | { set_fast_connectable, false, MGMT_SETTING_SIZE }, |
| 2702 | { set_pairable, false, MGMT_SETTING_SIZE }, |
| 2703 | { set_link_security, false, MGMT_SETTING_SIZE }, |
| 2704 | { set_ssp, false, MGMT_SETTING_SIZE }, |
| 2705 | { set_hs, false, MGMT_SETTING_SIZE }, |
| 2706 | { set_le, false, MGMT_SETTING_SIZE }, |
| 2707 | { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE }, |
| 2708 | { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE }, |
| 2709 | { add_uuid, false, MGMT_ADD_UUID_SIZE }, |
| 2710 | { remove_uuid, false, MGMT_REMOVE_UUID_SIZE }, |
| 2711 | { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE }, |
| 2712 | { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE }, |
| 2713 | { disconnect, false, MGMT_DISCONNECT_SIZE }, |
| 2714 | { get_connections, false, MGMT_GET_CONNECTIONS_SIZE }, |
| 2715 | { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE }, |
| 2716 | { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 2717 | { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 2718 | { pair_device, false, MGMT_PAIR_DEVICE_SIZE }, |
| 2719 | { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 2720 | { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE }, |
| 2721 | { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 2722 | { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 2723 | { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 2724 | { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
| 2725 | { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
| 2726 | { add_remote_oob_data, false, MGMT_ADD_REMOTE_OOB_DATA_SIZE }, |
| 2727 | { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 2728 | { start_discovery, false, MGMT_START_DISCOVERY_SIZE }, |
| 2729 | { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE }, |
| 2730 | { confirm_name, false, MGMT_CONFIRM_NAME_SIZE }, |
| 2731 | { block_device, false, MGMT_BLOCK_DEVICE_SIZE }, |
| 2732 | { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE }, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 2733 | { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 2734 | }; |
| 2735 | |
| 2736 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2737 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 2738 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2739 | void *buf; |
| 2740 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2741 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2742 | u16 opcode, index, len; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2743 | struct hci_dev *hdev = NULL; |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 2744 | const struct mgmt_handler *handler; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2745 | int err; |
| 2746 | |
| 2747 | BT_DBG("got %zu bytes", msglen); |
| 2748 | |
| 2749 | if (msglen < sizeof(*hdr)) |
| 2750 | return -EINVAL; |
| 2751 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 2752 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2753 | if (!buf) |
| 2754 | return -ENOMEM; |
| 2755 | |
| 2756 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 2757 | err = -EFAULT; |
| 2758 | goto done; |
| 2759 | } |
| 2760 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2761 | hdr = buf; |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2762 | opcode = __le16_to_cpu(hdr->opcode); |
| 2763 | index = __le16_to_cpu(hdr->index); |
| 2764 | len = __le16_to_cpu(hdr->len); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2765 | |
| 2766 | if (len != msglen - sizeof(*hdr)) { |
| 2767 | err = -EINVAL; |
| 2768 | goto done; |
| 2769 | } |
| 2770 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 2771 | if (index != MGMT_INDEX_NONE) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2772 | hdev = hci_dev_get(index); |
| 2773 | if (!hdev) { |
| 2774 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2775 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2776 | goto done; |
| 2777 | } |
| 2778 | } |
| 2779 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 2780 | if (opcode >= ARRAY_SIZE(mgmt_handlers) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2781 | mgmt_handlers[opcode].func == NULL) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2782 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2783 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2784 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 2785 | goto done; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2786 | } |
| 2787 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 2788 | if ((hdev && opcode < MGMT_OP_READ_INFO) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2789 | (!hdev && opcode >= MGMT_OP_READ_INFO)) { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 2790 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2791 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 2792 | goto done; |
| 2793 | } |
| 2794 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 2795 | handler = &mgmt_handlers[opcode]; |
| 2796 | |
| 2797 | if ((handler->var_len && len < handler->data_len) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2798 | (!handler->var_len && len != handler->data_len)) { |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 2799 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2800 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 2801 | goto done; |
| 2802 | } |
| 2803 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 2804 | if (hdev) |
| 2805 | mgmt_init_hdev(sk, hdev); |
| 2806 | |
| 2807 | cp = buf + sizeof(*hdr); |
| 2808 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 2809 | err = handler->func(sk, hdev, cp, len); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 2810 | if (err < 0) |
| 2811 | goto done; |
| 2812 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2813 | err = msglen; |
| 2814 | |
| 2815 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2816 | if (hdev) |
| 2817 | hci_dev_put(hdev); |
| 2818 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2819 | kfree(buf); |
| 2820 | return err; |
| 2821 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 2822 | |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 2823 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 2824 | { |
| 2825 | u8 *status = data; |
| 2826 | |
| 2827 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 2828 | mgmt_pending_remove(cmd); |
| 2829 | } |
| 2830 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2831 | int mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 2832 | { |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 2833 | if (!mgmt_valid_hdev(hdev)) |
| 2834 | return -ENOTSUPP; |
| 2835 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2836 | return mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 2837 | } |
| 2838 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2839 | int mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 2840 | { |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 2841 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 2842 | |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 2843 | if (!mgmt_valid_hdev(hdev)) |
| 2844 | return -ENOTSUPP; |
| 2845 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2846 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 2847 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2848 | return mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2849 | } |
| 2850 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2851 | struct cmd_lookup { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2852 | struct sock *sk; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2853 | struct hci_dev *hdev; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2854 | u8 mgmt_status; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2855 | }; |
| 2856 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2857 | static void settings_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2858 | { |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2859 | struct cmd_lookup *match = data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2860 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2861 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2862 | |
| 2863 | list_del(&cmd->list); |
| 2864 | |
| 2865 | if (match->sk == NULL) { |
| 2866 | match->sk = cmd->sk; |
| 2867 | sock_hold(match->sk); |
| 2868 | } |
| 2869 | |
| 2870 | mgmt_pending_free(cmd); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 2871 | } |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 2872 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2873 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 2874 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 2875 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 2876 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 2877 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 2878 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 2879 | return 0; |
| 2880 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 2881 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 2882 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 2883 | if (powered) { |
| 2884 | u8 scan = 0; |
| 2885 | |
| 2886 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 2887 | scan |= SCAN_PAGE; |
| 2888 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 2889 | scan |= SCAN_INQUIRY; |
| 2890 | |
| 2891 | if (scan) |
| 2892 | hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 2893 | |
| 2894 | update_class(hdev); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 2895 | update_name(hdev, hdev->dev_name); |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 2896 | update_eir(hdev); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 2897 | } else { |
Johan Hedberg | d4f6852 | 2012-03-02 03:07:07 +0200 | [diff] [blame] | 2898 | u8 status = MGMT_STATUS_NOT_POWERED; |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2899 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 2900 | } |
| 2901 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 2902 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2903 | |
| 2904 | if (match.sk) |
| 2905 | sock_put(match.sk); |
| 2906 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 2907 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 2908 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2909 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2910 | int mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2911 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 2912 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 2913 | bool changed = false; |
| 2914 | int err = 0; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2915 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 2916 | if (discoverable) { |
| 2917 | if (!test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 2918 | changed = true; |
| 2919 | } else { |
| 2920 | if (test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 2921 | changed = true; |
| 2922 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2923 | |
Johan Hedberg | ed9b5f2 | 2012-02-21 20:47:06 +0200 | [diff] [blame] | 2924 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2925 | &match); |
Johan Hedberg | ed9b5f2 | 2012-02-21 20:47:06 +0200 | [diff] [blame] | 2926 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 2927 | if (changed) |
| 2928 | err = new_settings(hdev, match.sk); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 2929 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2930 | if (match.sk) |
| 2931 | sock_put(match.sk); |
| 2932 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 2933 | return err; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2934 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2935 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2936 | int mgmt_connectable(struct hci_dev *hdev, u8 connectable) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2937 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 2938 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 2939 | bool changed = false; |
| 2940 | int err = 0; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2941 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 2942 | if (connectable) { |
| 2943 | if (!test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 2944 | changed = true; |
| 2945 | } else { |
| 2946 | if (test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 2947 | changed = true; |
| 2948 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2949 | |
Johan Hedberg | ed9b5f2 | 2012-02-21 20:47:06 +0200 | [diff] [blame] | 2950 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2951 | &match); |
Johan Hedberg | ed9b5f2 | 2012-02-21 20:47:06 +0200 | [diff] [blame] | 2952 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 2953 | if (changed) |
| 2954 | err = new_settings(hdev, match.sk); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2955 | |
| 2956 | if (match.sk) |
| 2957 | sock_put(match.sk); |
| 2958 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 2959 | return err; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2960 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2961 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2962 | int mgmt_write_scan_failed(struct hci_dev *hdev, u8 scan, u8 status) |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 2963 | { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2964 | u8 mgmt_err = mgmt_status(status); |
| 2965 | |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 2966 | if (scan & SCAN_PAGE) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2967 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2968 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 2969 | |
| 2970 | if (scan & SCAN_INQUIRY) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2971 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2972 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 2973 | |
| 2974 | return 0; |
| 2975 | } |
| 2976 | |
Cristian Chilipirea | 53168e5 | 2012-05-09 08:44:52 +0300 | [diff] [blame] | 2977 | int mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 2978 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2979 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2980 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2981 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2982 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2983 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2984 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 2985 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2986 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2987 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 2988 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2989 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2990 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 2991 | return mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2992 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2993 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 2994 | int mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, u8 persistent) |
| 2995 | { |
| 2996 | struct mgmt_ev_new_long_term_key ev; |
| 2997 | |
| 2998 | memset(&ev, 0, sizeof(ev)); |
| 2999 | |
| 3000 | ev.store_hint = persistent; |
| 3001 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 3002 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 3003 | ev.key.authenticated = key->authenticated; |
| 3004 | ev.key.enc_size = key->enc_size; |
| 3005 | ev.key.ediv = key->ediv; |
| 3006 | |
| 3007 | if (key->type == HCI_SMP_LTK) |
| 3008 | ev.key.master = 1; |
| 3009 | |
| 3010 | memcpy(ev.key.rand, key->rand, sizeof(key->rand)); |
| 3011 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 3012 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3013 | return mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), |
| 3014 | NULL); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 3015 | } |
| 3016 | |
Johan Hedberg | afc747a | 2012-01-15 18:11:07 +0200 | [diff] [blame] | 3017 | int mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3018 | u8 addr_type, u32 flags, u8 *name, u8 name_len, |
| 3019 | u8 *dev_class) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 3020 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3021 | char buf[512]; |
| 3022 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 3023 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 3024 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3025 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 3026 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 3027 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 3028 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 3029 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3030 | if (name_len > 0) |
| 3031 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3032 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3033 | |
| 3034 | if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0) |
Brian Gix | 5315638 | 2012-03-09 14:07:03 -0800 | [diff] [blame] | 3035 | eir_len = eir_append_data(ev->eir, eir_len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3036 | EIR_CLASS_OF_DEV, dev_class, 3); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3037 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 3038 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3039 | |
| 3040 | return mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3041 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 3042 | } |
| 3043 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3044 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 3045 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 3046 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3047 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 3048 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3049 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 3050 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 3051 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3052 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 3053 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3054 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3055 | |
| 3056 | *sk = cmd->sk; |
| 3057 | sock_hold(*sk); |
| 3058 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3059 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3060 | } |
| 3061 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 3062 | static void unpair_device_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 3063 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 3064 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 3065 | struct mgmt_cp_unpair_device *cp = cmd->param; |
| 3066 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 3067 | |
| 3068 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 3069 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 3070 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 3071 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 3072 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 3073 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 3074 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 3075 | |
| 3076 | mgmt_pending_remove(cmd); |
| 3077 | } |
| 3078 | |
Johan Hedberg | afc747a | 2012-01-15 18:11:07 +0200 | [diff] [blame] | 3079 | int mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3080 | u8 link_type, u8 addr_type) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 3081 | { |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 3082 | struct mgmt_addr_info ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3083 | struct sock *sk = NULL; |
| 3084 | int err; |
| 3085 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3086 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 3087 | |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 3088 | bacpy(&ev.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 3089 | ev.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 3090 | |
Johan Hedberg | afc747a | 2012-01-15 18:11:07 +0200 | [diff] [blame] | 3091 | err = mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3092 | sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3093 | |
| 3094 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 3095 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3096 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 3097 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3098 | hdev); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 3099 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3100 | return err; |
| 3101 | } |
| 3102 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 3103 | int mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3104 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3105 | { |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 3106 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3107 | struct pending_cmd *cmd; |
| 3108 | int err; |
| 3109 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3110 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3111 | if (!cmd) |
| 3112 | return -ENOENT; |
| 3113 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 3114 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 3115 | rp.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 3116 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 3117 | err = cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3118 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3119 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3120 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3121 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 3122 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3123 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3124 | return err; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 3125 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 3126 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 3127 | int mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3128 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 3129 | { |
| 3130 | struct mgmt_ev_connect_failed ev; |
| 3131 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 3132 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 3133 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 3134 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 3135 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3136 | return mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 3137 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3138 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3139 | int mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3140 | { |
| 3141 | struct mgmt_ev_pin_code_request ev; |
| 3142 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 3143 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 3144 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 3145 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3146 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3147 | return mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3148 | NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3149 | } |
| 3150 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3151 | int mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3152 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3153 | { |
| 3154 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 3155 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3156 | int err; |
| 3157 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3158 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3159 | if (!cmd) |
| 3160 | return -ENOENT; |
| 3161 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 3162 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 3163 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 3164 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 3165 | err = cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3166 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3167 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3168 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3169 | |
| 3170 | return err; |
| 3171 | } |
| 3172 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3173 | int mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3174 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3175 | { |
| 3176 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 3177 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3178 | int err; |
| 3179 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3180 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3181 | if (!cmd) |
| 3182 | return -ENOENT; |
| 3183 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 3184 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 3185 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 3186 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 3187 | err = cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3188 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3189 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3190 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3191 | |
| 3192 | return err; |
| 3193 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3194 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3195 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3196 | u8 link_type, u8 addr_type, __le32 value, |
| 3197 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3198 | { |
| 3199 | struct mgmt_ev_user_confirm_request ev; |
| 3200 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3201 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3202 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3203 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 3204 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 3205 | ev.confirm_hint = confirm_hint; |
Andrei Emeltchenko | 78e8098 | 2012-03-09 13:00:50 +0200 | [diff] [blame] | 3206 | ev.value = value; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3207 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3208 | return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3209 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3210 | } |
| 3211 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3212 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3213 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3214 | { |
| 3215 | struct mgmt_ev_user_passkey_request ev; |
| 3216 | |
| 3217 | BT_DBG("%s", hdev->name); |
| 3218 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3219 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 3220 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3221 | |
| 3222 | return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3223 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3224 | } |
| 3225 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3226 | static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3227 | u8 link_type, u8 addr_type, u8 status, |
| 3228 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3229 | { |
| 3230 | struct pending_cmd *cmd; |
| 3231 | struct mgmt_rp_user_confirm_reply rp; |
| 3232 | int err; |
| 3233 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3234 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3235 | if (!cmd) |
| 3236 | return -ENOENT; |
| 3237 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3238 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 3239 | rp.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 3240 | err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3241 | &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3242 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3243 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3244 | |
| 3245 | return err; |
| 3246 | } |
| 3247 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3248 | int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3249 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3250 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3251 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3252 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3253 | } |
| 3254 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3255 | int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3256 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3257 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3258 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 3259 | status, |
| 3260 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3261 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 3262 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3263 | int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3264 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3265 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3266 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3267 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3268 | } |
| 3269 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3270 | int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3271 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3272 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3273 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 3274 | status, |
| 3275 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3276 | } |
| 3277 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 3278 | int mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3279 | u8 addr_type, u8 status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 3280 | { |
| 3281 | struct mgmt_ev_auth_failed ev; |
| 3282 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 3283 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 3284 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 3285 | ev.status = mgmt_status(status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 3286 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3287 | return mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 3288 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3289 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 3290 | int mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
| 3291 | { |
| 3292 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 3293 | bool changed = false; |
| 3294 | int err = 0; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 3295 | |
| 3296 | if (status) { |
| 3297 | u8 mgmt_err = mgmt_status(status); |
| 3298 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3299 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 3300 | return 0; |
| 3301 | } |
| 3302 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 3303 | if (test_bit(HCI_AUTH, &hdev->flags)) { |
| 3304 | if (!test_and_set_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) |
| 3305 | changed = true; |
| 3306 | } else { |
| 3307 | if (test_and_clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) |
| 3308 | changed = true; |
| 3309 | } |
| 3310 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 3311 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3312 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 3313 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 3314 | if (changed) |
| 3315 | err = new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 3316 | |
| 3317 | if (match.sk) |
| 3318 | sock_put(match.sk); |
| 3319 | |
| 3320 | return err; |
| 3321 | } |
| 3322 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 3323 | static int clear_eir(struct hci_dev *hdev) |
| 3324 | { |
| 3325 | struct hci_cp_write_eir cp; |
| 3326 | |
| 3327 | if (!(hdev->features[6] & LMP_EXT_INQ)) |
| 3328 | return 0; |
| 3329 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 3330 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 3331 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 3332 | memset(&cp, 0, sizeof(cp)); |
| 3333 | |
| 3334 | return hci_send_cmd(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
| 3335 | } |
| 3336 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 3337 | int mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 3338 | { |
| 3339 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 3340 | bool changed = false; |
| 3341 | int err = 0; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 3342 | |
| 3343 | if (status) { |
| 3344 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 3345 | |
| 3346 | if (enable && test_and_clear_bit(HCI_SSP_ENABLED, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3347 | &hdev->dev_flags)) |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 3348 | err = new_settings(hdev, NULL); |
| 3349 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3350 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 3351 | &mgmt_err); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 3352 | |
| 3353 | return err; |
| 3354 | } |
| 3355 | |
| 3356 | if (enable) { |
| 3357 | if (!test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 3358 | changed = true; |
| 3359 | } else { |
| 3360 | if (test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 3361 | changed = true; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 3362 | } |
| 3363 | |
| 3364 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 3365 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 3366 | if (changed) |
| 3367 | err = new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 3368 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 3369 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 3370 | sock_put(match.sk); |
| 3371 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 3372 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 3373 | update_eir(hdev); |
| 3374 | else |
| 3375 | clear_eir(hdev); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 3376 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 3377 | return err; |
| 3378 | } |
| 3379 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 3380 | static void class_rsp(struct pending_cmd *cmd, void *data) |
| 3381 | { |
| 3382 | struct cmd_lookup *match = data; |
| 3383 | |
| 3384 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, match->mgmt_status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3385 | match->hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 3386 | |
| 3387 | list_del(&cmd->list); |
| 3388 | |
| 3389 | if (match->sk == NULL) { |
| 3390 | match->sk = cmd->sk; |
| 3391 | sock_hold(match->sk); |
| 3392 | } |
| 3393 | |
| 3394 | mgmt_pending_free(cmd); |
| 3395 | } |
| 3396 | |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 3397 | int mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3398 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 3399 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 3400 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
| 3401 | int err = 0; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 3402 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 3403 | clear_bit(HCI_PENDING_CLASS, &hdev->dev_flags); |
| 3404 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 3405 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, class_rsp, &match); |
| 3406 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, class_rsp, &match); |
| 3407 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, class_rsp, &match); |
| 3408 | |
| 3409 | if (!status) |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3410 | err = mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, |
| 3411 | 3, NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 3412 | |
| 3413 | if (match.sk) |
| 3414 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 3415 | |
| 3416 | return err; |
| 3417 | } |
| 3418 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3419 | int mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3420 | { |
| 3421 | struct pending_cmd *cmd; |
| 3422 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3423 | bool changed = false; |
| 3424 | int err = 0; |
| 3425 | |
| 3426 | if (memcmp(name, hdev->dev_name, sizeof(hdev->dev_name)) != 0) { |
| 3427 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
| 3428 | changed = true; |
| 3429 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3430 | |
| 3431 | memset(&ev, 0, sizeof(ev)); |
| 3432 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3433 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3434 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3435 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3436 | if (!cmd) |
| 3437 | goto send_event; |
| 3438 | |
Johan Hedberg | 7bdaae4 | 2012-02-22 21:39:58 +0200 | [diff] [blame] | 3439 | /* Always assume that either the short or the complete name has |
| 3440 | * changed if there was a pending mgmt command */ |
| 3441 | changed = true; |
| 3442 | |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3443 | if (status) { |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3444 | err = cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3445 | mgmt_status(status)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3446 | goto failed; |
| 3447 | } |
| 3448 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 3449 | err = cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, &ev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3450 | sizeof(ev)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3451 | if (err < 0) |
| 3452 | goto failed; |
| 3453 | |
| 3454 | send_event: |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3455 | if (changed) |
| 3456 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3457 | sizeof(ev), cmd ? cmd->sk : NULL); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3458 | |
Johan Hedberg | f51d5b2 | 2012-02-22 18:17:32 +0200 | [diff] [blame] | 3459 | update_eir(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3460 | |
| 3461 | failed: |
| 3462 | if (cmd) |
| 3463 | mgmt_pending_remove(cmd); |
| 3464 | return err; |
| 3465 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3466 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3467 | int mgmt_read_local_oob_data_reply_complete(struct hci_dev *hdev, u8 *hash, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3468 | u8 *randomizer, u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3469 | { |
| 3470 | struct pending_cmd *cmd; |
| 3471 | int err; |
| 3472 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3473 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3474 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3475 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3476 | if (!cmd) |
| 3477 | return -ENOENT; |
| 3478 | |
| 3479 | if (status) { |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3480 | err = cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3481 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3482 | } else { |
| 3483 | struct mgmt_rp_read_local_oob_data rp; |
| 3484 | |
| 3485 | memcpy(rp.hash, hash, sizeof(rp.hash)); |
| 3486 | memcpy(rp.randomizer, randomizer, sizeof(rp.randomizer)); |
| 3487 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3488 | err = cmd_complete(cmd->sk, hdev->id, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3489 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, &rp, |
| 3490 | sizeof(rp)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | mgmt_pending_remove(cmd); |
| 3494 | |
| 3495 | return err; |
| 3496 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 3497 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 3498 | int mgmt_le_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
| 3499 | { |
| 3500 | struct cmd_lookup match = { NULL, hdev }; |
| 3501 | bool changed = false; |
| 3502 | int err = 0; |
| 3503 | |
| 3504 | if (status) { |
| 3505 | u8 mgmt_err = mgmt_status(status); |
| 3506 | |
| 3507 | if (enable && test_and_clear_bit(HCI_LE_ENABLED, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3508 | &hdev->dev_flags)) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 3509 | err = new_settings(hdev, NULL); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 3510 | |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 3511 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 3512 | &mgmt_err); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 3513 | |
| 3514 | return err; |
| 3515 | } |
| 3516 | |
| 3517 | if (enable) { |
| 3518 | if (!test_and_set_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 3519 | changed = true; |
| 3520 | } else { |
| 3521 | if (test_and_clear_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 3522 | changed = true; |
| 3523 | } |
| 3524 | |
| 3525 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 3526 | |
| 3527 | if (changed) |
| 3528 | err = new_settings(hdev, match.sk); |
| 3529 | |
| 3530 | if (match.sk) |
| 3531 | sock_put(match.sk); |
| 3532 | |
| 3533 | return err; |
| 3534 | } |
| 3535 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 3536 | int mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3537 | u8 addr_type, u8 *dev_class, s8 rssi, u8 cfm_name, u8 |
| 3538 | ssp, u8 *eir, u16 eir_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 3539 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 3540 | char buf[512]; |
| 3541 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 3542 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 3543 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 3544 | /* Leave 5 bytes for a potential CoD field */ |
| 3545 | if (sizeof(*ev) + eir_len + 5 > sizeof(buf)) |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 3546 | return -EINVAL; |
| 3547 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 3548 | memset(buf, 0, sizeof(buf)); |
| 3549 | |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 3550 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 3551 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 3552 | ev->rssi = rssi; |
Johan Hedberg | 9a395a8 | 2012-02-23 00:00:32 +0200 | [diff] [blame] | 3553 | if (cfm_name) |
Andrei Emeltchenko | 33cef26 | 2012-06-18 13:03:46 +0300 | [diff] [blame] | 3554 | ev->flags |= cpu_to_le32(MGMT_DEV_FOUND_CONFIRM_NAME); |
Johan Hedberg | 388fc8f | 2012-02-23 00:38:59 +0200 | [diff] [blame] | 3555 | if (!ssp) |
Andrei Emeltchenko | 33cef26 | 2012-06-18 13:03:46 +0300 | [diff] [blame] | 3556 | ev->flags |= cpu_to_le32(MGMT_DEV_FOUND_LEGACY_PAIRING); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 3557 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 3558 | if (eir_len > 0) |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 3559 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 3560 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 3561 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 3562 | eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3563 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 3564 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 3565 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 3566 | ev_size = sizeof(*ev) + eir_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 3567 | |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 3568 | return mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 3569 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 3570 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3571 | int mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3572 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 3573 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3574 | struct mgmt_ev_device_found *ev; |
| 3575 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 3576 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 3577 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3578 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 3579 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3580 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 3581 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3582 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 3583 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3584 | ev->rssi = rssi; |
| 3585 | |
| 3586 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, name, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3587 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3588 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 3589 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3590 | |
Johan Hedberg | 053c7e0 | 2012-02-04 00:06:00 +0200 | [diff] [blame] | 3591 | return mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3592 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 3593 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 3594 | |
Andre Guedes | 7a13510 | 2011-11-09 17:14:25 -0300 | [diff] [blame] | 3595 | int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3596 | { |
| 3597 | struct pending_cmd *cmd; |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 3598 | u8 type; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3599 | int err; |
| 3600 | |
Andre Guedes | 203159d | 2012-02-13 15:41:01 -0300 | [diff] [blame] | 3601 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3602 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3603 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3604 | if (!cmd) |
| 3605 | return -ENOENT; |
| 3606 | |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 3607 | type = hdev->discovery.type; |
| 3608 | |
| 3609 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3610 | &type, sizeof(type)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3611 | mgmt_pending_remove(cmd); |
| 3612 | |
| 3613 | return err; |
| 3614 | } |
| 3615 | |
Andre Guedes | e6d465c | 2011-11-09 17:14:26 -0300 | [diff] [blame] | 3616 | int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3617 | { |
| 3618 | struct pending_cmd *cmd; |
| 3619 | int err; |
| 3620 | |
| 3621 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 3622 | if (!cmd) |
| 3623 | return -ENOENT; |
| 3624 | |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3625 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3626 | &hdev->discovery.type, sizeof(hdev->discovery.type)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 3627 | mgmt_pending_remove(cmd); |
| 3628 | |
| 3629 | return err; |
| 3630 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 3631 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3632 | int mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 3633 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 3634 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3635 | struct pending_cmd *cmd; |
| 3636 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 3637 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 3638 | |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3639 | if (discovering) |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3640 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3641 | else |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3642 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3643 | |
| 3644 | if (cmd != NULL) { |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 3645 | u8 type = hdev->discovery.type; |
| 3646 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3647 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, &type, |
| 3648 | sizeof(type)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 3649 | mgmt_pending_remove(cmd); |
| 3650 | } |
| 3651 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 3652 | memset(&ev, 0, sizeof(ev)); |
| 3653 | ev.type = hdev->discovery.type; |
| 3654 | ev.discovering = discovering; |
| 3655 | |
| 3656 | return mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 3657 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3658 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3659 | int mgmt_device_blocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3660 | { |
| 3661 | struct pending_cmd *cmd; |
| 3662 | struct mgmt_ev_device_blocked ev; |
| 3663 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3664 | cmd = mgmt_pending_find(MGMT_OP_BLOCK_DEVICE, hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3665 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3666 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 3667 | ev.addr.type = type; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3668 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3669 | return mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3670 | cmd ? cmd->sk : NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3671 | } |
| 3672 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3673 | int mgmt_device_unblocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3674 | { |
| 3675 | struct pending_cmd *cmd; |
| 3676 | struct mgmt_ev_device_unblocked ev; |
| 3677 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3678 | cmd = mgmt_pending_find(MGMT_OP_UNBLOCK_DEVICE, hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3679 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3680 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 3681 | ev.addr.type = type; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3682 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 3683 | return mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3684 | cmd ? cmd->sk : NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3685 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 3686 | |
| 3687 | module_param(enable_hs, bool, 0644); |
| 3688 | MODULE_PARM_DESC(enable_hs, "Enable High Speed support"); |