Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | BlueZ - Bluetooth protocol stack for Linux |
| 3 | Copyright (C) 2010 Nokia Corporation |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 4 | Copyright (c) 2011-2012 Code Aurora Forum. All rights reserved. |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License version 2 as |
| 8 | published by the Free Software Foundation; |
| 9 | |
| 10 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 11 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 12 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 13 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
| 14 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 15 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 16 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 17 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 18 | |
| 19 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 20 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
| 21 | SOFTWARE IS DISCLAIMED. |
| 22 | */ |
| 23 | |
| 24 | /* Bluetooth HCI Management interface */ |
| 25 | |
Szymon Janc | 7235975 | 2011-02-17 14:16:32 +0100 | [diff] [blame] | 26 | #include <linux/uaccess.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 27 | #include <linux/interrupt.h> |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 28 | #include <linux/module.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 29 | #include <asm/unaligned.h> |
| 30 | |
| 31 | #include <net/bluetooth/bluetooth.h> |
| 32 | #include <net/bluetooth/hci_core.h> |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 33 | #include <net/bluetooth/l2cap.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 34 | #include <net/bluetooth/mgmt.h> |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 35 | #include <net/bluetooth/smp.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 36 | |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 37 | #define MGMT_VERSION 0 |
| 38 | #define MGMT_REVISION 1 |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 39 | |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 40 | #define SCAN_IDLE 0x00 |
| 41 | #define SCAN_LE 0x01 |
| 42 | #define SCAN_BR 0x02 |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 43 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 44 | struct pending_cmd { |
| 45 | struct list_head list; |
| 46 | __u16 opcode; |
| 47 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 48 | void *param; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 49 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 50 | void *user_data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 51 | }; |
| 52 | |
Mat Martineau | 8cd0df0 | 2011-08-23 16:23:36 -0700 | [diff] [blame] | 53 | struct mgmt_pending_free_work { |
| 54 | struct work_struct work; |
| 55 | struct sock *sk; |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 56 | }; |
| 57 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 58 | LIST_HEAD(cmd_list); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 59 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 60 | static int cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status) |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 61 | { |
| 62 | struct sk_buff *skb; |
| 63 | struct mgmt_hdr *hdr; |
| 64 | struct mgmt_ev_cmd_status *ev; |
| 65 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 66 | BT_DBG("sock %p, index %u, cmd %u, status %u", sk, index, cmd, status); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 67 | |
| 68 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_ATOMIC); |
| 69 | if (!skb) |
| 70 | return -ENOMEM; |
| 71 | |
| 72 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 73 | |
| 74 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 75 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 76 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 77 | |
| 78 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 79 | ev->status = status; |
| 80 | put_unaligned_le16(cmd, &ev->opcode); |
| 81 | |
| 82 | if (sock_queue_rcv_skb(sk, skb) < 0) |
| 83 | kfree_skb(skb); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 88 | static int cmd_complete(struct sock *sk, u16 index, u16 cmd, void *rp, |
| 89 | size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 90 | { |
| 91 | struct sk_buff *skb; |
| 92 | struct mgmt_hdr *hdr; |
| 93 | struct mgmt_ev_cmd_complete *ev; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 94 | |
| 95 | BT_DBG("sock %p", sk); |
| 96 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 97 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_ATOMIC); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 98 | if (!skb) |
| 99 | return -ENOMEM; |
| 100 | |
| 101 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 102 | |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 103 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 104 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 105 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 106 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 107 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
| 108 | put_unaligned_le16(cmd, &ev->opcode); |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 109 | |
| 110 | if (rp) |
| 111 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 112 | |
| 113 | if (sock_queue_rcv_skb(sk, skb) < 0) |
| 114 | kfree_skb(skb); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 119 | static int read_version(struct sock *sk) |
| 120 | { |
| 121 | struct mgmt_rp_read_version rp; |
| 122 | |
| 123 | BT_DBG("sock %p", sk); |
| 124 | |
| 125 | rp.version = MGMT_VERSION; |
| 126 | put_unaligned_le16(MGMT_REVISION, &rp.revision); |
| 127 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 128 | return cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, &rp, |
| 129 | sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 132 | static int read_index_list(struct sock *sk) |
| 133 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 134 | struct mgmt_rp_read_index_list *rp; |
| 135 | struct list_head *p; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 136 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 137 | u16 count; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 138 | int i, err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 139 | |
| 140 | BT_DBG("sock %p", sk); |
| 141 | |
| 142 | read_lock(&hci_dev_list_lock); |
| 143 | |
| 144 | count = 0; |
| 145 | list_for_each(p, &hci_dev_list) { |
Peter Krystad | 1fc4407 | 2011-08-30 15:38:12 -0700 | [diff] [blame] | 146 | struct hci_dev *d = list_entry(p, struct hci_dev, list); |
| 147 | if (d->dev_type != HCI_BREDR) |
| 148 | continue; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 149 | count++; |
| 150 | } |
| 151 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 152 | rp_len = sizeof(*rp) + (2 * count); |
| 153 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 154 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 155 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 156 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 157 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 158 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 159 | put_unaligned_le16(0, &rp->num_controllers); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 160 | |
| 161 | i = 0; |
| 162 | list_for_each(p, &hci_dev_list) { |
| 163 | struct hci_dev *d = list_entry(p, struct hci_dev, list); |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 164 | |
| 165 | hci_del_off_timer(d); |
| 166 | |
Peter Krystad | 1fc4407 | 2011-08-30 15:38:12 -0700 | [diff] [blame] | 167 | if (d->dev_type != HCI_BREDR) |
| 168 | continue; |
| 169 | |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 170 | set_bit(HCI_MGMT, &d->flags); |
| 171 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 172 | if (test_bit(HCI_SETUP, &d->flags)) |
| 173 | continue; |
| 174 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 175 | put_unaligned_le16(d->id, &rp->index[i++]); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 176 | put_unaligned_le16((u16)i, &rp->num_controllers); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 177 | BT_DBG("Added hci%u", d->id); |
| 178 | } |
| 179 | |
| 180 | read_unlock(&hci_dev_list_lock); |
| 181 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 182 | err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, rp, |
| 183 | rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 184 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 185 | kfree(rp); |
| 186 | |
| 187 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 190 | static int read_controller_info(struct sock *sk, u16 index) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 191 | { |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 192 | struct mgmt_rp_read_info rp; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 193 | struct hci_dev *hdev; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 194 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 195 | BT_DBG("sock %p hci%u", sk, index); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 196 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 197 | hdev = hci_dev_get(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 198 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 199 | return cmd_status(sk, index, MGMT_OP_READ_INFO, ENODEV); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 200 | |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 201 | hci_del_off_timer(hdev); |
| 202 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 203 | hci_dev_lock_bh(hdev); |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 204 | |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 205 | set_bit(HCI_MGMT, &hdev->flags); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 206 | |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 207 | memset(&rp, 0, sizeof(rp)); |
| 208 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 209 | rp.type = hdev->dev_type; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 210 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 211 | rp.powered = test_bit(HCI_UP, &hdev->flags); |
| 212 | rp.connectable = test_bit(HCI_PSCAN, &hdev->flags); |
| 213 | rp.discoverable = test_bit(HCI_ISCAN, &hdev->flags); |
| 214 | rp.pairable = test_bit(HCI_PSCAN, &hdev->flags); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 215 | |
| 216 | if (test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 217 | rp.sec_mode = 3; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 218 | else if (hdev->ssp_mode > 0) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 219 | rp.sec_mode = 4; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 220 | else |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 221 | rp.sec_mode = 2; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 222 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 223 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
| 224 | memcpy(rp.features, hdev->features, 8); |
| 225 | memcpy(rp.dev_class, hdev->dev_class, 3); |
| 226 | put_unaligned_le16(hdev->manufacturer, &rp.manufacturer); |
| 227 | rp.hci_ver = hdev->hci_ver; |
| 228 | put_unaligned_le16(hdev->hci_rev, &rp.hci_rev); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 229 | |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 230 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
| 231 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 232 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 233 | hci_dev_put(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 234 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 235 | return cmd_complete(sk, index, MGMT_OP_READ_INFO, &rp, sizeof(rp)); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 236 | } |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 237 | |
Mat Martineau | 8cd0df0 | 2011-08-23 16:23:36 -0700 | [diff] [blame] | 238 | static void mgmt_pending_free_worker(struct work_struct *work) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 239 | { |
Mat Martineau | 8cd0df0 | 2011-08-23 16:23:36 -0700 | [diff] [blame] | 240 | struct mgmt_pending_free_work *free_work = |
| 241 | container_of(work, struct mgmt_pending_free_work, work); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 242 | |
Mat Martineau | 8cd0df0 | 2011-08-23 16:23:36 -0700 | [diff] [blame] | 243 | BT_DBG("sk %p", free_work->sk); |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 244 | |
Mat Martineau | 8cd0df0 | 2011-08-23 16:23:36 -0700 | [diff] [blame] | 245 | sock_put(free_work->sk); |
| 246 | kfree(free_work); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 247 | } |
| 248 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 249 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 250 | { |
Mat Martineau | 8cd0df0 | 2011-08-23 16:23:36 -0700 | [diff] [blame] | 251 | struct mgmt_pending_free_work *free_work; |
| 252 | struct sock *sk = cmd->sk; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 253 | |
Mat Martineau | 8cd0df0 | 2011-08-23 16:23:36 -0700 | [diff] [blame] | 254 | BT_DBG("opcode %d, sk %p", cmd->opcode, sk); |
| 255 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 256 | kfree(cmd->param); |
| 257 | kfree(cmd); |
Mat Martineau | 8cd0df0 | 2011-08-23 16:23:36 -0700 | [diff] [blame] | 258 | |
| 259 | free_work = kzalloc(sizeof(*free_work), GFP_ATOMIC); |
| 260 | if (free_work) { |
| 261 | INIT_WORK(&free_work->work, mgmt_pending_free_worker); |
| 262 | free_work->sk = sk; |
| 263 | |
| 264 | if (!schedule_work(&free_work->work)) |
| 265 | kfree(free_work); |
| 266 | } |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static struct pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode, |
| 270 | u16 index, void *data, u16 len) |
| 271 | { |
| 272 | struct pending_cmd *cmd; |
| 273 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 274 | BT_DBG("%d", opcode); |
| 275 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 276 | cmd = kmalloc(sizeof(*cmd), GFP_ATOMIC); |
| 277 | if (!cmd) |
| 278 | return NULL; |
| 279 | |
| 280 | cmd->opcode = opcode; |
| 281 | cmd->index = index; |
| 282 | |
| 283 | cmd->param = kmalloc(len, GFP_ATOMIC); |
| 284 | if (!cmd->param) { |
| 285 | kfree(cmd); |
| 286 | return NULL; |
| 287 | } |
| 288 | |
| 289 | if (data) |
| 290 | memcpy(cmd->param, data, len); |
| 291 | |
| 292 | cmd->sk = sk; |
| 293 | sock_hold(sk); |
| 294 | |
| 295 | list_add(&cmd->list, &cmd_list); |
| 296 | |
| 297 | return cmd; |
| 298 | } |
| 299 | |
| 300 | static void mgmt_pending_foreach(u16 opcode, int index, |
| 301 | void (*cb)(struct pending_cmd *cmd, void *data), |
| 302 | void *data) |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 303 | { |
| 304 | struct list_head *p, *n; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 305 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 306 | BT_DBG(" %d", opcode); |
| 307 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 308 | list_for_each_safe(p, n, &cmd_list) { |
| 309 | struct pending_cmd *cmd; |
| 310 | |
| 311 | cmd = list_entry(p, struct pending_cmd, list); |
| 312 | |
Johan Hedberg | 931bc4e | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 313 | if (opcode > 0 && cmd->opcode != opcode) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 314 | continue; |
| 315 | |
| 316 | if (index >= 0 && cmd->index != index) |
| 317 | continue; |
| 318 | |
| 319 | cb(cmd, data); |
| 320 | } |
| 321 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 322 | |
| 323 | static struct pending_cmd *mgmt_pending_find(u16 opcode, int index) |
| 324 | { |
| 325 | struct list_head *p; |
| 326 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 327 | BT_DBG(" %d", opcode); |
| 328 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 329 | list_for_each(p, &cmd_list) { |
| 330 | struct pending_cmd *cmd; |
| 331 | |
| 332 | cmd = list_entry(p, struct pending_cmd, list); |
| 333 | |
| 334 | if (cmd->opcode != opcode) |
| 335 | continue; |
| 336 | |
| 337 | if (index >= 0 && cmd->index != index) |
| 338 | continue; |
| 339 | |
| 340 | return cmd; |
| 341 | } |
| 342 | |
| 343 | return NULL; |
| 344 | } |
| 345 | |
| 346 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
| 347 | { |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 348 | BT_DBG(" %d", cmd->opcode); |
| 349 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 350 | list_del(&cmd->list); |
| 351 | mgmt_pending_free(cmd); |
| 352 | } |
| 353 | |
| 354 | static int set_powered(struct sock *sk, u16 index, unsigned char *data, u16 len) |
| 355 | { |
| 356 | struct mgmt_mode *cp; |
| 357 | struct hci_dev *hdev; |
| 358 | struct pending_cmd *cmd; |
| 359 | int err, up; |
| 360 | |
| 361 | cp = (void *) data; |
| 362 | |
| 363 | BT_DBG("request for hci%u", index); |
| 364 | |
| 365 | if (len != sizeof(*cp)) |
| 366 | return cmd_status(sk, index, MGMT_OP_SET_POWERED, EINVAL); |
| 367 | |
| 368 | hdev = hci_dev_get(index); |
| 369 | if (!hdev) |
| 370 | return cmd_status(sk, index, MGMT_OP_SET_POWERED, ENODEV); |
| 371 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 372 | hci_dev_lock_bh(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 373 | |
| 374 | up = test_bit(HCI_UP, &hdev->flags); |
| 375 | if ((cp->val && up) || (!cp->val && !up)) { |
| 376 | err = cmd_status(sk, index, MGMT_OP_SET_POWERED, EALREADY); |
| 377 | goto failed; |
| 378 | } |
| 379 | |
| 380 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, index)) { |
| 381 | err = cmd_status(sk, index, MGMT_OP_SET_POWERED, EBUSY); |
| 382 | goto failed; |
| 383 | } |
| 384 | |
| 385 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, index, data, len); |
| 386 | if (!cmd) { |
| 387 | err = -ENOMEM; |
| 388 | goto failed; |
| 389 | } |
| 390 | |
| 391 | if (cp->val) |
| 392 | queue_work(hdev->workqueue, &hdev->power_on); |
| 393 | else |
| 394 | queue_work(hdev->workqueue, &hdev->power_off); |
| 395 | |
| 396 | err = 0; |
| 397 | |
| 398 | failed: |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 399 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 400 | hci_dev_put(hdev); |
| 401 | return err; |
| 402 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 403 | |
Brian Gix | 8a7f164 | 2011-10-17 17:39:46 -0700 | [diff] [blame] | 404 | static u8 get_service_classes(struct hci_dev *hdev) |
| 405 | { |
| 406 | struct list_head *p; |
| 407 | u8 val = 0; |
| 408 | |
| 409 | list_for_each(p, &hdev->uuids) { |
| 410 | struct bt_uuid *uuid = list_entry(p, struct bt_uuid, list); |
| 411 | |
| 412 | val |= uuid->svc_hint; |
| 413 | } |
| 414 | |
| 415 | return val; |
| 416 | } |
| 417 | |
| 418 | static int update_class(struct hci_dev *hdev) |
| 419 | { |
| 420 | u8 cod[3]; |
Srinivas Krovvidi | 58562d8 | 2012-06-25 16:46:56 +0530 | [diff] [blame] | 421 | int err = 0; |
Brian Gix | 8a7f164 | 2011-10-17 17:39:46 -0700 | [diff] [blame] | 422 | |
| 423 | BT_DBG("%s", hdev->name); |
| 424 | |
| 425 | if (test_bit(HCI_SERVICE_CACHE, &hdev->flags)) |
| 426 | return 0; |
| 427 | |
| 428 | cod[0] = hdev->minor_class; |
| 429 | cod[1] = hdev->major_class; |
| 430 | cod[2] = get_service_classes(hdev); |
| 431 | |
| 432 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
| 433 | return 0; |
| 434 | |
Srinivas Krovvidi | 58562d8 | 2012-06-25 16:46:56 +0530 | [diff] [blame] | 435 | err = hci_send_cmd(hdev, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod); |
| 436 | |
| 437 | if (err == 0) |
| 438 | memcpy(hdev->dev_class, cod, 3); |
| 439 | |
| 440 | return err; |
Brian Gix | 8a7f164 | 2011-10-17 17:39:46 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | static int set_limited_discoverable(struct sock *sk, u16 index, |
| 444 | unsigned char *data, u16 len) |
| 445 | { |
| 446 | struct mgmt_mode *cp; |
| 447 | struct hci_dev *hdev; |
| 448 | struct pending_cmd *cmd; |
| 449 | struct hci_cp_write_current_iac_lap dcp; |
| 450 | int update_cod; |
| 451 | int err = 0; |
| 452 | /* General Inquiry LAP: 0x9E8B33, Limited Inquiry LAP: 0x9E8B00 */ |
| 453 | u8 lap[] = { 0x33, 0x8b, 0x9e, 0x00, 0x8b, 0x9e }; |
| 454 | |
| 455 | cp = (void *) data; |
| 456 | |
| 457 | BT_DBG("hci%u discoverable: %d", index, cp->val); |
| 458 | |
| 459 | if (!cp || len != sizeof(*cp)) |
| 460 | return cmd_status(sk, index, MGMT_OP_SET_LIMIT_DISCOVERABLE, |
| 461 | EINVAL); |
| 462 | |
| 463 | hdev = hci_dev_get(index); |
| 464 | if (!hdev) |
| 465 | return cmd_status(sk, index, MGMT_OP_SET_LIMIT_DISCOVERABLE, |
| 466 | ENODEV); |
| 467 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 468 | hci_dev_lock_bh(hdev); |
Brian Gix | 8a7f164 | 2011-10-17 17:39:46 -0700 | [diff] [blame] | 469 | |
| 470 | if (!test_bit(HCI_UP, &hdev->flags)) { |
| 471 | err = cmd_status(sk, index, MGMT_OP_SET_LIMIT_DISCOVERABLE, |
| 472 | ENETDOWN); |
| 473 | goto failed; |
| 474 | } |
| 475 | |
| 476 | if (mgmt_pending_find(MGMT_OP_SET_LIMIT_DISCOVERABLE, index)) { |
| 477 | err = cmd_status(sk, index, MGMT_OP_SET_LIMIT_DISCOVERABLE, |
| 478 | EBUSY); |
| 479 | goto failed; |
| 480 | } |
| 481 | |
| 482 | if (cp->val == test_bit(HCI_ISCAN, &hdev->flags) && |
| 483 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 484 | err = cmd_status(sk, index, MGMT_OP_SET_LIMIT_DISCOVERABLE, |
| 485 | EALREADY); |
| 486 | goto failed; |
| 487 | } |
| 488 | |
| 489 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LIMIT_DISCOVERABLE, index, data, |
| 490 | len); |
| 491 | if (!cmd) { |
| 492 | err = -ENOMEM; |
| 493 | goto failed; |
| 494 | } |
| 495 | |
| 496 | memset(&dcp, 0, sizeof(dcp)); |
| 497 | dcp.num_current_iac = cp->val ? 2 : 1; |
| 498 | memcpy(&dcp.lap, lap, dcp.num_current_iac * 3); |
| 499 | update_cod = 1; |
| 500 | |
| 501 | if (cp->val) { |
| 502 | if (hdev->major_class & MGMT_MAJOR_CLASS_LIMITED) |
| 503 | update_cod = 0; |
| 504 | hdev->major_class |= MGMT_MAJOR_CLASS_LIMITED; |
| 505 | } else { |
| 506 | if (!(hdev->major_class & MGMT_MAJOR_CLASS_LIMITED)) |
| 507 | update_cod = 0; |
| 508 | hdev->major_class &= ~MGMT_MAJOR_CLASS_LIMITED; |
| 509 | } |
| 510 | |
| 511 | if (update_cod) |
| 512 | err = update_class(hdev); |
| 513 | |
| 514 | if (err >= 0) |
| 515 | err = hci_send_cmd(hdev, HCI_OP_WRITE_CURRENT_IAC_LAP, |
| 516 | sizeof(dcp), &dcp); |
| 517 | |
| 518 | if (err < 0) |
| 519 | mgmt_pending_remove(cmd); |
| 520 | |
| 521 | failed: |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 522 | hci_dev_unlock_bh(hdev); |
Brian Gix | 8a7f164 | 2011-10-17 17:39:46 -0700 | [diff] [blame] | 523 | hci_dev_put(hdev); |
| 524 | |
| 525 | return err; |
| 526 | } |
| 527 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 528 | static int set_discoverable(struct sock *sk, u16 index, unsigned char *data, |
| 529 | u16 len) |
| 530 | { |
| 531 | struct mgmt_mode *cp; |
| 532 | struct hci_dev *hdev; |
| 533 | struct pending_cmd *cmd; |
| 534 | u8 scan; |
| 535 | int err; |
| 536 | |
| 537 | cp = (void *) data; |
| 538 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 539 | BT_DBG("request for hci%u", index); |
| 540 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 541 | if (len != sizeof(*cp)) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 542 | return cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, EINVAL); |
| 543 | |
| 544 | hdev = hci_dev_get(index); |
| 545 | if (!hdev) |
| 546 | return cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, ENODEV); |
| 547 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 548 | hci_dev_lock_bh(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 549 | |
| 550 | if (!test_bit(HCI_UP, &hdev->flags)) { |
| 551 | err = cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, ENETDOWN); |
| 552 | goto failed; |
| 553 | } |
| 554 | |
| 555 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, index) || |
| 556 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, index)) { |
| 557 | err = cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, EBUSY); |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 558 | goto failed; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | if (cp->val == test_bit(HCI_ISCAN, &hdev->flags) && |
| 562 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 563 | err = cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE, EALREADY); |
| 564 | goto failed; |
| 565 | } |
| 566 | |
| 567 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, index, data, len); |
| 568 | if (!cmd) { |
| 569 | err = -ENOMEM; |
| 570 | goto failed; |
| 571 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 572 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 573 | scan = SCAN_PAGE; |
| 574 | |
| 575 | if (cp->val) |
| 576 | scan |= SCAN_INQUIRY; |
| 577 | |
| 578 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 579 | if (err < 0) |
| 580 | mgmt_pending_remove(cmd); |
| 581 | |
| 582 | failed: |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 583 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 584 | hci_dev_put(hdev); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 585 | |
| 586 | return err; |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 587 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 588 | |
| 589 | static int set_connectable(struct sock *sk, u16 index, unsigned char *data, |
| 590 | u16 len) |
| 591 | { |
| 592 | struct mgmt_mode *cp; |
| 593 | struct hci_dev *hdev; |
| 594 | struct pending_cmd *cmd; |
| 595 | u8 scan; |
| 596 | int err; |
| 597 | |
| 598 | cp = (void *) data; |
| 599 | |
| 600 | BT_DBG("request for hci%u", index); |
| 601 | |
| 602 | if (len != sizeof(*cp)) |
| 603 | return cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, EINVAL); |
| 604 | |
| 605 | hdev = hci_dev_get(index); |
| 606 | if (!hdev) |
| 607 | return cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, ENODEV); |
| 608 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 609 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 610 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 611 | if (!test_bit(HCI_UP, &hdev->flags)) { |
| 612 | err = cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, ENETDOWN); |
| 613 | goto failed; |
| 614 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 615 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 616 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, index) || |
| 617 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, index)) { |
| 618 | err = cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, EBUSY); |
| 619 | goto failed; |
| 620 | } |
| 621 | |
| 622 | if (cp->val == test_bit(HCI_PSCAN, &hdev->flags)) { |
| 623 | err = cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE, EALREADY); |
| 624 | goto failed; |
| 625 | } |
| 626 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 627 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, index, data, len); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 628 | if (!cmd) { |
| 629 | err = -ENOMEM; |
| 630 | goto failed; |
| 631 | } |
| 632 | |
| 633 | if (cp->val) |
| 634 | scan = SCAN_PAGE; |
| 635 | else |
| 636 | scan = 0; |
| 637 | |
| 638 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 639 | if (err < 0) |
| 640 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 641 | |
| 642 | failed: |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 643 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 644 | hci_dev_put(hdev); |
| 645 | |
| 646 | return err; |
| 647 | } |
| 648 | |
| 649 | static int mgmt_event(u16 event, u16 index, void *data, u16 data_len, |
| 650 | struct sock *skip_sk) |
| 651 | { |
| 652 | struct sk_buff *skb; |
| 653 | struct mgmt_hdr *hdr; |
| 654 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 655 | BT_DBG("hci%d %d", index, event); |
| 656 | |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 657 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_ATOMIC); |
| 658 | if (!skb) |
| 659 | return -ENOMEM; |
| 660 | |
| 661 | bt_cb(skb)->channel = HCI_CHANNEL_CONTROL; |
| 662 | |
| 663 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 664 | hdr->opcode = cpu_to_le16(event); |
| 665 | hdr->index = cpu_to_le16(index); |
| 666 | hdr->len = cpu_to_le16(data_len); |
| 667 | |
| 668 | if (data) |
| 669 | memcpy(skb_put(skb, data_len), data, data_len); |
| 670 | |
| 671 | hci_send_to_sock(NULL, skb, skip_sk); |
| 672 | kfree_skb(skb); |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 673 | |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | static int send_mode_rsp(struct sock *sk, u16 opcode, u16 index, u8 val) |
| 678 | { |
| 679 | struct mgmt_mode rp; |
| 680 | |
| 681 | rp.val = val; |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 682 | |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 683 | return cmd_complete(sk, index, opcode, &rp, sizeof(rp)); |
| 684 | } |
| 685 | |
| 686 | static int set_pairable(struct sock *sk, u16 index, unsigned char *data, |
| 687 | u16 len) |
| 688 | { |
| 689 | struct mgmt_mode *cp, ev; |
| 690 | struct hci_dev *hdev; |
| 691 | int err; |
| 692 | |
| 693 | cp = (void *) data; |
| 694 | |
| 695 | BT_DBG("request for hci%u", index); |
| 696 | |
| 697 | if (len != sizeof(*cp)) |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 698 | return cmd_status(sk, index, MGMT_OP_SET_PAIRABLE, EINVAL); |
| 699 | |
| 700 | hdev = hci_dev_get(index); |
| 701 | if (!hdev) |
| 702 | return cmd_status(sk, index, MGMT_OP_SET_PAIRABLE, ENODEV); |
| 703 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 704 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 705 | |
| 706 | if (cp->val) |
| 707 | set_bit(HCI_PAIRABLE, &hdev->flags); |
| 708 | else |
| 709 | clear_bit(HCI_PAIRABLE, &hdev->flags); |
| 710 | |
| 711 | err = send_mode_rsp(sk, MGMT_OP_SET_PAIRABLE, index, cp->val); |
| 712 | if (err < 0) |
| 713 | goto failed; |
| 714 | |
| 715 | ev.val = cp->val; |
| 716 | |
| 717 | err = mgmt_event(MGMT_EV_PAIRABLE, index, &ev, sizeof(ev), sk); |
| 718 | |
| 719 | failed: |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 720 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 721 | hci_dev_put(hdev); |
| 722 | |
| 723 | return err; |
| 724 | } |
| 725 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 726 | #define EIR_FLAGS 0x01 /* flags */ |
| 727 | #define EIR_UUID16_SOME 0x02 /* 16-bit UUID, more available */ |
| 728 | #define EIR_UUID16_ALL 0x03 /* 16-bit UUID, all listed */ |
| 729 | #define EIR_UUID32_SOME 0x04 /* 32-bit UUID, more available */ |
| 730 | #define EIR_UUID32_ALL 0x05 /* 32-bit UUID, all listed */ |
| 731 | #define EIR_UUID128_SOME 0x06 /* 128-bit UUID, more available */ |
| 732 | #define EIR_UUID128_ALL 0x07 /* 128-bit UUID, all listed */ |
| 733 | #define EIR_NAME_SHORT 0x08 /* shortened local name */ |
| 734 | #define EIR_NAME_COMPLETE 0x09 /* complete local name */ |
| 735 | #define EIR_TX_POWER 0x0A /* transmit power level */ |
| 736 | #define EIR_DEVICE_ID 0x10 /* device ID */ |
| 737 | |
| 738 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 739 | |
| 740 | static u8 bluetooth_base_uuid[] = { |
| 741 | 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, |
| 742 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 743 | }; |
| 744 | |
| 745 | static u16 get_uuid16(u8 *uuid128) |
| 746 | { |
| 747 | u32 val; |
| 748 | int i; |
| 749 | |
| 750 | for (i = 0; i < 12; i++) { |
| 751 | if (bluetooth_base_uuid[i] != uuid128[i]) |
| 752 | return 0; |
| 753 | } |
| 754 | |
| 755 | memcpy(&val, &uuid128[12], 4); |
| 756 | |
| 757 | val = le32_to_cpu(val); |
| 758 | if (val > 0xffff) |
| 759 | return 0; |
| 760 | |
| 761 | return (u16) val; |
| 762 | } |
| 763 | |
| 764 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 765 | { |
| 766 | u8 *ptr = data; |
| 767 | u16 eir_len = 0; |
| 768 | u16 uuid16_list[HCI_MAX_EIR_LENGTH / sizeof(u16)]; |
| 769 | int i, truncated = 0; |
| 770 | struct list_head *p; |
| 771 | size_t name_len; |
| 772 | |
Bhasker Neti | a6e6a4f | 2012-01-27 15:25:43 +0530 | [diff] [blame] | 773 | name_len = strnlen(hdev->dev_name, HCI_MAX_EIR_LENGTH); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 774 | |
| 775 | if (name_len > 0) { |
| 776 | /* EIR Data type */ |
| 777 | if (name_len > 48) { |
| 778 | name_len = 48; |
| 779 | ptr[1] = EIR_NAME_SHORT; |
| 780 | } else |
| 781 | ptr[1] = EIR_NAME_COMPLETE; |
| 782 | |
| 783 | /* EIR Data length */ |
| 784 | ptr[0] = name_len + 1; |
| 785 | |
| 786 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 787 | |
| 788 | eir_len += (name_len + 2); |
| 789 | ptr += (name_len + 2); |
| 790 | } |
| 791 | |
| 792 | memset(uuid16_list, 0, sizeof(uuid16_list)); |
| 793 | |
| 794 | /* Group all UUID16 types */ |
| 795 | list_for_each(p, &hdev->uuids) { |
| 796 | struct bt_uuid *uuid = list_entry(p, struct bt_uuid, list); |
| 797 | u16 uuid16; |
| 798 | |
| 799 | uuid16 = get_uuid16(uuid->uuid); |
| 800 | if (uuid16 == 0) |
| 801 | return; |
| 802 | |
| 803 | if (uuid16 < 0x1100) |
| 804 | continue; |
| 805 | |
| 806 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 807 | continue; |
| 808 | |
| 809 | /* Stop if not enough space to put next UUID */ |
| 810 | if (eir_len + 2 + sizeof(u16) > HCI_MAX_EIR_LENGTH) { |
| 811 | truncated = 1; |
| 812 | break; |
| 813 | } |
| 814 | |
| 815 | /* Check for duplicates */ |
| 816 | for (i = 0; uuid16_list[i] != 0; i++) |
| 817 | if (uuid16_list[i] == uuid16) |
| 818 | break; |
| 819 | |
| 820 | if (uuid16_list[i] == 0) { |
| 821 | uuid16_list[i] = uuid16; |
| 822 | eir_len += sizeof(u16); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | if (uuid16_list[0] != 0) { |
| 827 | u8 *length = ptr; |
| 828 | |
| 829 | /* EIR Data type */ |
| 830 | ptr[1] = truncated ? EIR_UUID16_SOME : EIR_UUID16_ALL; |
| 831 | |
| 832 | ptr += 2; |
| 833 | eir_len += 2; |
| 834 | |
| 835 | for (i = 0; uuid16_list[i] != 0; i++) { |
| 836 | *ptr++ = (uuid16_list[i] & 0x00ff); |
| 837 | *ptr++ = (uuid16_list[i] & 0xff00) >> 8; |
| 838 | } |
| 839 | |
| 840 | /* EIR Data length */ |
| 841 | *length = (i * sizeof(u16)) + 1; |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | static int update_eir(struct hci_dev *hdev) |
| 846 | { |
| 847 | struct hci_cp_write_eir cp; |
| 848 | |
| 849 | if (!(hdev->features[6] & LMP_EXT_INQ)) |
| 850 | return 0; |
| 851 | |
| 852 | if (hdev->ssp_mode == 0) |
| 853 | return 0; |
| 854 | |
| 855 | if (test_bit(HCI_SERVICE_CACHE, &hdev->flags)) |
| 856 | return 0; |
| 857 | |
| 858 | memset(&cp, 0, sizeof(cp)); |
| 859 | |
| 860 | create_eir(hdev, cp.data); |
| 861 | |
| 862 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
| 863 | return 0; |
| 864 | |
| 865 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 866 | |
| 867 | return hci_send_cmd(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
| 868 | } |
| 869 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 870 | static int add_uuid(struct sock *sk, u16 index, unsigned char *data, u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 871 | { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 872 | struct mgmt_cp_add_uuid *cp; |
| 873 | struct hci_dev *hdev; |
| 874 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 875 | int err; |
| 876 | |
| 877 | cp = (void *) data; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 878 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 879 | BT_DBG("request for hci%u", index); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 880 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 881 | if (len != sizeof(*cp)) |
| 882 | return cmd_status(sk, index, MGMT_OP_ADD_UUID, EINVAL); |
| 883 | |
| 884 | hdev = hci_dev_get(index); |
| 885 | if (!hdev) |
| 886 | return cmd_status(sk, index, MGMT_OP_ADD_UUID, ENODEV); |
| 887 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 888 | hci_dev_lock_bh(hdev); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 889 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 890 | uuid = kmalloc(sizeof(*uuid), GFP_ATOMIC); |
| 891 | if (!uuid) { |
| 892 | err = -ENOMEM; |
| 893 | goto failed; |
| 894 | } |
| 895 | |
| 896 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 897 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 898 | |
| 899 | list_add(&uuid->list, &hdev->uuids); |
| 900 | |
Bhasker Neti | a6e6a4f | 2012-01-27 15:25:43 +0530 | [diff] [blame] | 901 | if (test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 902 | |
Bhasker Neti | a6e6a4f | 2012-01-27 15:25:43 +0530 | [diff] [blame] | 903 | err = update_class(hdev); |
| 904 | if (err < 0) |
| 905 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 906 | |
Bhasker Neti | a6e6a4f | 2012-01-27 15:25:43 +0530 | [diff] [blame] | 907 | err = update_eir(hdev); |
| 908 | if (err < 0) |
| 909 | goto failed; |
| 910 | } else |
| 911 | err = 0; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 912 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 913 | err = cmd_complete(sk, index, MGMT_OP_ADD_UUID, NULL, 0); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 914 | |
| 915 | failed: |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 916 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 917 | hci_dev_put(hdev); |
| 918 | |
| 919 | return err; |
| 920 | } |
| 921 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 922 | static int remove_uuid(struct sock *sk, u16 index, unsigned char *data, u16 len) |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 923 | { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 924 | struct list_head *p, *n; |
Szymon Janc | 779cb85 | 2011-02-25 19:05:47 +0100 | [diff] [blame] | 925 | struct mgmt_cp_remove_uuid *cp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 926 | struct hci_dev *hdev; |
| 927 | 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] | 928 | int err, found; |
| 929 | |
| 930 | cp = (void *) data; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 931 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 932 | BT_DBG("request for hci%u", index); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 933 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 934 | if (len != sizeof(*cp)) |
| 935 | return cmd_status(sk, index, MGMT_OP_REMOVE_UUID, EINVAL); |
| 936 | |
| 937 | hdev = hci_dev_get(index); |
| 938 | if (!hdev) |
| 939 | return cmd_status(sk, index, MGMT_OP_REMOVE_UUID, ENODEV); |
| 940 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 941 | hci_dev_lock_bh(hdev); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 942 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 943 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
| 944 | err = hci_uuids_clear(hdev); |
| 945 | goto unlock; |
| 946 | } |
| 947 | |
| 948 | found = 0; |
| 949 | |
| 950 | list_for_each_safe(p, n, &hdev->uuids) { |
| 951 | struct bt_uuid *match = list_entry(p, struct bt_uuid, list); |
| 952 | |
| 953 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 954 | continue; |
| 955 | |
| 956 | list_del(&match->list); |
| 957 | found++; |
| 958 | } |
| 959 | |
| 960 | if (found == 0) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 961 | err = cmd_status(sk, index, MGMT_OP_REMOVE_UUID, ENOENT); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 962 | goto unlock; |
| 963 | } |
| 964 | |
Bhasker Neti | a6e6a4f | 2012-01-27 15:25:43 +0530 | [diff] [blame] | 965 | if (test_bit(HCI_UP, &hdev->flags)) { |
| 966 | err = update_class(hdev); |
| 967 | if (err < 0) |
| 968 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 969 | |
Bhasker Neti | a6e6a4f | 2012-01-27 15:25:43 +0530 | [diff] [blame] | 970 | err = update_eir(hdev); |
| 971 | if (err < 0) |
| 972 | goto unlock; |
| 973 | } else |
| 974 | err = 0; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 975 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 976 | err = cmd_complete(sk, index, MGMT_OP_REMOVE_UUID, NULL, 0); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 977 | |
| 978 | unlock: |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 979 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 980 | hci_dev_put(hdev); |
| 981 | |
| 982 | return err; |
| 983 | } |
| 984 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 985 | static int set_dev_class(struct sock *sk, u16 index, unsigned char *data, |
| 986 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 987 | { |
| 988 | struct hci_dev *hdev; |
| 989 | struct mgmt_cp_set_dev_class *cp; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 990 | int err; |
| 991 | |
| 992 | cp = (void *) data; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 993 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 994 | BT_DBG("request for hci%u", index); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 995 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 996 | if (len != sizeof(*cp)) |
| 997 | return cmd_status(sk, index, MGMT_OP_SET_DEV_CLASS, EINVAL); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 998 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 999 | hdev = hci_dev_get(index); |
| 1000 | if (!hdev) |
| 1001 | return cmd_status(sk, index, MGMT_OP_SET_DEV_CLASS, ENODEV); |
| 1002 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1003 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1004 | |
Brian Gix | 8a7f164 | 2011-10-17 17:39:46 -0700 | [diff] [blame] | 1005 | hdev->major_class &= ~MGMT_MAJOR_CLASS_MASK; |
| 1006 | hdev->major_class |= cp->major & MGMT_MAJOR_CLASS_MASK; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1007 | hdev->minor_class = cp->minor; |
| 1008 | |
Srinivas Krovvidi | 58562d8 | 2012-06-25 16:46:56 +0530 | [diff] [blame] | 1009 | if (test_bit(HCI_UP, &hdev->flags)) { |
Bhasker Neti | a6e6a4f | 2012-01-27 15:25:43 +0530 | [diff] [blame] | 1010 | err = update_class(hdev); |
Srinivas Krovvidi | 58562d8 | 2012-06-25 16:46:56 +0530 | [diff] [blame] | 1011 | if (err == 0) |
| 1012 | err = cmd_complete(sk, index, |
| 1013 | MGMT_OP_SET_DEV_CLASS, hdev->dev_class, sizeof(u8)*3); |
| 1014 | } else |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1015 | err = cmd_complete(sk, index, MGMT_OP_SET_DEV_CLASS, NULL, 0); |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 1016 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1017 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1018 | hci_dev_put(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1019 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1020 | return err; |
| 1021 | } |
| 1022 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1023 | static int set_service_cache(struct sock *sk, u16 index, unsigned char *data, |
| 1024 | u16 len) |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1025 | { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1026 | struct hci_dev *hdev; |
| 1027 | struct mgmt_cp_set_service_cache *cp; |
| 1028 | int err; |
| 1029 | |
| 1030 | cp = (void *) data; |
| 1031 | |
| 1032 | if (len != sizeof(*cp)) |
| 1033 | return cmd_status(sk, index, MGMT_OP_SET_SERVICE_CACHE, EINVAL); |
| 1034 | |
| 1035 | hdev = hci_dev_get(index); |
| 1036 | if (!hdev) |
| 1037 | return cmd_status(sk, index, MGMT_OP_SET_SERVICE_CACHE, ENODEV); |
| 1038 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1039 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1040 | |
| 1041 | BT_DBG("hci%u enable %d", index, cp->enable); |
| 1042 | |
| 1043 | if (cp->enable) { |
| 1044 | set_bit(HCI_SERVICE_CACHE, &hdev->flags); |
| 1045 | err = 0; |
| 1046 | } else { |
| 1047 | clear_bit(HCI_SERVICE_CACHE, &hdev->flags); |
Bhasker Neti | a6e6a4f | 2012-01-27 15:25:43 +0530 | [diff] [blame] | 1048 | if (test_bit(HCI_UP, &hdev->flags)) { |
| 1049 | err = update_class(hdev); |
| 1050 | if (err == 0) |
| 1051 | err = update_eir(hdev); |
| 1052 | } else |
| 1053 | err = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | if (err == 0) |
| 1057 | err = cmd_complete(sk, index, MGMT_OP_SET_SERVICE_CACHE, NULL, |
| 1058 | 0); |
| 1059 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1060 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1061 | hci_dev_put(hdev); |
| 1062 | |
| 1063 | return err; |
| 1064 | } |
| 1065 | |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1066 | static int load_keys(struct sock *sk, u16 index, unsigned char *data, u16 len) |
| 1067 | { |
| 1068 | struct hci_dev *hdev; |
| 1069 | struct mgmt_cp_load_keys *cp; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1070 | u16 key_count, expected_len; |
Vinicius Costa Gomes | c00ddbf | 2011-07-08 18:31:44 -0300 | [diff] [blame] | 1071 | int i, err; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1072 | |
| 1073 | cp = (void *) data; |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1074 | |
| 1075 | if (len < sizeof(*cp)) |
| 1076 | return -EINVAL; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1077 | |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1078 | key_count = get_unaligned_le16(&cp->key_count); |
| 1079 | |
| 1080 | expected_len = sizeof(*cp) + key_count * sizeof(struct mgmt_key_info); |
Vinicius Costa Gomes | c00ddbf | 2011-07-08 18:31:44 -0300 | [diff] [blame] | 1081 | if (expected_len > len) { |
| 1082 | BT_ERR("load_keys: expected at least %u bytes, got %u bytes", |
| 1083 | expected_len, len); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1084 | return -EINVAL; |
| 1085 | } |
| 1086 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1087 | hdev = hci_dev_get(index); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1088 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1089 | return cmd_status(sk, index, MGMT_OP_LOAD_KEYS, ENODEV); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1090 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1091 | BT_DBG("hci%u debug_keys %u key_count %u", index, cp->debug_keys, |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1092 | key_count); |
| 1093 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1094 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1095 | |
| 1096 | hci_link_keys_clear(hdev); |
| 1097 | |
| 1098 | set_bit(HCI_LINK_KEYS, &hdev->flags); |
| 1099 | |
| 1100 | if (cp->debug_keys) |
| 1101 | set_bit(HCI_DEBUG_KEYS, &hdev->flags); |
| 1102 | else |
| 1103 | clear_bit(HCI_DEBUG_KEYS, &hdev->flags); |
| 1104 | |
Vinicius Costa Gomes | c00ddbf | 2011-07-08 18:31:44 -0300 | [diff] [blame] | 1105 | len -= sizeof(*cp); |
| 1106 | i = 0; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1107 | |
Vinicius Costa Gomes | c00ddbf | 2011-07-08 18:31:44 -0300 | [diff] [blame] | 1108 | while (i < len) { |
| 1109 | struct mgmt_key_info *key = (void *) cp->keys + i; |
| 1110 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1111 | i += sizeof(*key); |
Vinicius Costa Gomes | c00ddbf | 2011-07-08 18:31:44 -0300 | [diff] [blame] | 1112 | |
Brian Gix | cf95677 | 2011-10-20 15:18:51 -0700 | [diff] [blame] | 1113 | if (key->key_type == KEY_TYPE_LTK) { |
Vinicius Costa Gomes | c00ddbf | 2011-07-08 18:31:44 -0300 | [diff] [blame] | 1114 | struct key_master_id *id = (void *) key->data; |
| 1115 | |
| 1116 | if (key->dlen != sizeof(struct key_master_id)) |
| 1117 | continue; |
| 1118 | |
Brian Gix | cf95677 | 2011-10-20 15:18:51 -0700 | [diff] [blame] | 1119 | hci_add_ltk(hdev, 0, &key->bdaddr, key->addr_type, |
| 1120 | key->pin_len, key->auth, id->ediv, |
| 1121 | id->rand, key->val); |
Vinicius Costa Gomes | c00ddbf | 2011-07-08 18:31:44 -0300 | [diff] [blame] | 1122 | |
| 1123 | continue; |
| 1124 | } |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1125 | |
Brian Gix | cf95677 | 2011-10-20 15:18:51 -0700 | [diff] [blame] | 1126 | hci_add_link_key(hdev, 0, &key->bdaddr, key->val, key->key_type, |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1127 | key->pin_len); |
| 1128 | } |
| 1129 | |
Vinicius Costa Gomes | c00ddbf | 2011-07-08 18:31:44 -0300 | [diff] [blame] | 1130 | err = cmd_complete(sk, index, MGMT_OP_LOAD_KEYS, NULL, 0); |
Johan Hedberg | 0e5f875 | 2011-11-11 16:18:54 +0200 | [diff] [blame] | 1131 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1132 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1133 | hci_dev_put(hdev); |
| 1134 | |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 1135 | return err; |
| 1136 | } |
| 1137 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1138 | static int remove_key(struct sock *sk, u16 index, unsigned char *data, u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1139 | { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1140 | struct hci_dev *hdev; |
| 1141 | struct mgmt_cp_remove_key *cp; |
| 1142 | struct hci_conn *conn; |
| 1143 | int err; |
| 1144 | |
| 1145 | cp = (void *) data; |
| 1146 | |
| 1147 | if (len != sizeof(*cp)) |
| 1148 | return cmd_status(sk, index, MGMT_OP_REMOVE_KEY, EINVAL); |
| 1149 | |
| 1150 | hdev = hci_dev_get(index); |
| 1151 | if (!hdev) |
| 1152 | return cmd_status(sk, index, MGMT_OP_REMOVE_KEY, ENODEV); |
| 1153 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1154 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1155 | |
| 1156 | err = hci_remove_link_key(hdev, &cp->bdaddr); |
| 1157 | if (err < 0) { |
| 1158 | err = cmd_status(sk, index, MGMT_OP_REMOVE_KEY, -err); |
| 1159 | goto unlock; |
| 1160 | } |
| 1161 | |
| 1162 | err = 0; |
| 1163 | |
| 1164 | if (!test_bit(HCI_UP, &hdev->flags) || !cp->disconnect) |
| 1165 | goto unlock; |
| 1166 | |
| 1167 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr); |
| 1168 | if (conn) { |
| 1169 | struct hci_cp_disconnect dc; |
| 1170 | |
| 1171 | put_unaligned_le16(conn->handle, &dc.handle); |
| 1172 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1173 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, 0, NULL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | unlock: |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1177 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1178 | hci_dev_put(hdev); |
| 1179 | |
| 1180 | return err; |
| 1181 | } |
| 1182 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1183 | static int disconnect(struct sock *sk, u16 index, unsigned char *data, u16 len) |
| 1184 | { |
| 1185 | struct hci_dev *hdev; |
| 1186 | struct mgmt_cp_disconnect *cp; |
| 1187 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1188 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1189 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1190 | int err; |
| 1191 | |
| 1192 | BT_DBG(""); |
| 1193 | |
| 1194 | cp = (void *) data; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1195 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1196 | if (len != sizeof(*cp)) |
| 1197 | return cmd_status(sk, index, MGMT_OP_DISCONNECT, EINVAL); |
| 1198 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1199 | hdev = hci_dev_get(index); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1200 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1201 | return cmd_status(sk, index, MGMT_OP_DISCONNECT, ENODEV); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1202 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1203 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1204 | |
| 1205 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1206 | err = cmd_status(sk, index, MGMT_OP_DISCONNECT, ENETDOWN); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1207 | goto failed; |
| 1208 | } |
| 1209 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1210 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, index)) { |
| 1211 | err = cmd_status(sk, index, MGMT_OP_DISCONNECT, EBUSY); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1212 | goto failed; |
| 1213 | } |
| 1214 | |
| 1215 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr); |
| 1216 | if (!conn) { |
Inga Stotland | bd6a49a | 2011-08-23 16:13:39 -0700 | [diff] [blame] | 1217 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->bdaddr); |
| 1218 | if (!conn) { |
| 1219 | err = cmd_status(sk, index, MGMT_OP_DISCONNECT, |
| 1220 | ENOTCONN); |
| 1221 | goto failed; |
| 1222 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1223 | } |
| 1224 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1225 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, index, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1226 | if (!cmd) { |
| 1227 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1228 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1229 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1230 | |
| 1231 | put_unaligned_le16(conn->handle, &dc.handle); |
| 1232 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 1233 | |
| 1234 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1235 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1236 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1237 | |
| 1238 | failed: |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1239 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 1240 | hci_dev_put(hdev); |
| 1241 | |
| 1242 | return err; |
| 1243 | } |
| 1244 | |
Szymon Janc | 8ce6284 | 2011-03-01 16:55:32 +0100 | [diff] [blame] | 1245 | static int get_connections(struct sock *sk, u16 index) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1246 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1247 | struct mgmt_rp_get_connections *rp; |
| 1248 | struct hci_dev *hdev; |
| 1249 | struct list_head *p; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 1250 | size_t rp_len; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1251 | u16 count; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1252 | int i, err; |
| 1253 | |
| 1254 | BT_DBG(""); |
| 1255 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1256 | hdev = hci_dev_get(index); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1257 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1258 | return cmd_status(sk, index, MGMT_OP_GET_CONNECTIONS, ENODEV); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1259 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1260 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1261 | |
| 1262 | count = 0; |
| 1263 | list_for_each(p, &hdev->conn_hash.list) { |
| 1264 | count++; |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 1265 | } |
| 1266 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 1267 | rp_len = sizeof(*rp) + (count * sizeof(bdaddr_t)); |
| 1268 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 1269 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1270 | err = -ENOMEM; |
| 1271 | goto unlock; |
| 1272 | } |
| 1273 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1274 | put_unaligned_le16(count, &rp->conn_count); |
| 1275 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1276 | read_lock(&hci_dev_list_lock); |
| 1277 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1278 | i = 0; |
| 1279 | list_for_each(p, &hdev->conn_hash.list) { |
| 1280 | struct hci_conn *c = list_entry(p, struct hci_conn, list); |
| 1281 | |
| 1282 | bacpy(&rp->conn[i++], &c->dst); |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 1283 | } |
| 1284 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1285 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | 60fc5fb | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 1286 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1287 | err = cmd_complete(sk, index, MGMT_OP_GET_CONNECTIONS, rp, rp_len); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 1288 | |
| 1289 | unlock: |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 1290 | kfree(rp); |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1291 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 1292 | hci_dev_put(hdev); |
| 1293 | return err; |
| 1294 | } |
| 1295 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1296 | static int pin_code_reply(struct sock *sk, u16 index, unsigned char *data, |
| 1297 | u16 len) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 1298 | { |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1299 | struct hci_dev *hdev; |
| 1300 | struct mgmt_cp_pin_code_reply *cp; |
| 1301 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1302 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1303 | int err; |
| 1304 | |
| 1305 | BT_DBG(""); |
| 1306 | |
| 1307 | cp = (void *) data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1308 | |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1309 | if (len != sizeof(*cp)) |
| 1310 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, EINVAL); |
| 1311 | |
| 1312 | hdev = hci_dev_get(index); |
| 1313 | if (!hdev) |
| 1314 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, ENODEV); |
| 1315 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1316 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1317 | |
| 1318 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1319 | err = cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY, ENETDOWN); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1320 | goto failed; |
| 1321 | } |
| 1322 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1323 | cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, index, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1324 | if (!cmd) { |
| 1325 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1326 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 1327 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1328 | |
| 1329 | bacpy(&reply.bdaddr, &cp->bdaddr); |
| 1330 | reply.pin_len = cp->pin_len; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1331 | memcpy(reply.pin_code, cp->pin_code, 16); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1332 | |
| 1333 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 1334 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1335 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1336 | |
| 1337 | failed: |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1338 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1339 | hci_dev_put(hdev); |
| 1340 | |
| 1341 | return err; |
| 1342 | } |
| 1343 | |
Prabhakaran Mc | 46230fa | 2011-11-30 18:11:21 +0530 | [diff] [blame] | 1344 | static int encrypt_link(struct sock *sk, u16 index, unsigned char *data, |
| 1345 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1346 | { |
Prabhakaran Mc | 46230fa | 2011-11-30 18:11:21 +0530 | [diff] [blame] | 1347 | struct hci_dev *hdev; |
| 1348 | struct mgmt_cp_encrypt_link *cp; |
| 1349 | struct hci_cp_set_conn_encrypt enc; |
| 1350 | struct hci_conn *conn; |
| 1351 | int err = 0; |
| 1352 | |
| 1353 | BT_DBG(""); |
| 1354 | |
| 1355 | cp = (void *) data; |
| 1356 | |
| 1357 | if (len != sizeof(*cp)) |
| 1358 | return cmd_status(sk, index, MGMT_OP_ENCRYPT_LINK, EINVAL); |
| 1359 | |
| 1360 | hdev = hci_dev_get(index); |
| 1361 | if (!hdev) |
| 1362 | return cmd_status(sk, index, MGMT_OP_ENCRYPT_LINK, ENODEV); |
| 1363 | |
Brian Gix | 384ec67 | 2012-03-08 18:41:15 -0800 | [diff] [blame] | 1364 | hci_dev_lock_bh(hdev); |
Prabhakaran Mc | 46230fa | 2011-11-30 18:11:21 +0530 | [diff] [blame] | 1365 | |
| 1366 | if (!test_bit(HCI_UP, &hdev->flags)) { |
| 1367 | err = cmd_status(sk, index, MGMT_OP_ENCRYPT_LINK, ENETDOWN); |
Brian Gix | 384ec67 | 2012-03-08 18:41:15 -0800 | [diff] [blame] | 1368 | goto done; |
Prabhakaran Mc | 46230fa | 2011-11-30 18:11:21 +0530 | [diff] [blame] | 1369 | } |
| 1370 | |
Brian Gix | 384ec67 | 2012-03-08 18:41:15 -0800 | [diff] [blame] | 1371 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr); |
| 1372 | if (!conn) { |
| 1373 | err = cmd_status(sk, index, MGMT_OP_ENCRYPT_LINK, ENOTCONN); |
| 1374 | goto done; |
| 1375 | } |
Prabhakaran Mc | 46230fa | 2011-11-30 18:11:21 +0530 | [diff] [blame] | 1376 | |
Brian Gix | 384ec67 | 2012-03-08 18:41:15 -0800 | [diff] [blame] | 1377 | if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) { |
| 1378 | err = cmd_status(sk, index, MGMT_OP_ENCRYPT_LINK, EINPROGRESS); |
| 1379 | goto done; |
| 1380 | } |
Prabhakaran Mc | 46230fa | 2011-11-30 18:11:21 +0530 | [diff] [blame] | 1381 | |
| 1382 | if (conn->link_mode & HCI_LM_AUTH) { |
| 1383 | enc.handle = cpu_to_le16(conn->handle); |
| 1384 | enc.encrypt = cp->enable; |
| 1385 | err = hci_send_cmd(hdev, |
| 1386 | HCI_OP_SET_CONN_ENCRYPT, sizeof(enc), &enc); |
| 1387 | } else { |
| 1388 | conn->auth_initiator = 1; |
| 1389 | if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) { |
| 1390 | struct hci_cp_auth_requested cp; |
| 1391 | cp.handle = cpu_to_le16(conn->handle); |
| 1392 | err = hci_send_cmd(conn->hdev, |
| 1393 | HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp); |
| 1394 | } |
| 1395 | } |
| 1396 | |
Brian Gix | 384ec67 | 2012-03-08 18:41:15 -0800 | [diff] [blame] | 1397 | done: |
| 1398 | hci_dev_unlock_bh(hdev); |
Prabhakaran Mc | 46230fa | 2011-11-30 18:11:21 +0530 | [diff] [blame] | 1399 | hci_dev_put(hdev); |
| 1400 | |
| 1401 | return err; |
| 1402 | } |
| 1403 | |
| 1404 | |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1405 | static int pin_code_neg_reply(struct sock *sk, u16 index, unsigned char *data, |
| 1406 | u16 len) |
| 1407 | { |
| 1408 | struct hci_dev *hdev; |
| 1409 | struct mgmt_cp_pin_code_neg_reply *cp; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1410 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1411 | int err; |
| 1412 | |
| 1413 | BT_DBG(""); |
| 1414 | |
| 1415 | cp = (void *) data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1416 | |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1417 | if (len != sizeof(*cp)) |
| 1418 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 1419 | EINVAL); |
| 1420 | |
| 1421 | hdev = hci_dev_get(index); |
| 1422 | if (!hdev) |
| 1423 | return cmd_status(sk, index, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 1424 | ENODEV); |
| 1425 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1426 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1427 | |
| 1428 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1429 | err = cmd_status(sk, index, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 1430 | ENETDOWN); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1431 | goto failed; |
| 1432 | } |
| 1433 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1434 | cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, index, |
| 1435 | data, len); |
| 1436 | if (!cmd) { |
| 1437 | err = -ENOMEM; |
| 1438 | goto failed; |
| 1439 | } |
| 1440 | |
| 1441 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, sizeof(cp->bdaddr), |
| 1442 | &cp->bdaddr); |
| 1443 | if (err < 0) |
| 1444 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1445 | |
| 1446 | failed: |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1447 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 1448 | hci_dev_put(hdev); |
| 1449 | |
| 1450 | return err; |
| 1451 | } |
| 1452 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1453 | static int set_io_capability(struct sock *sk, u16 index, unsigned char *data, |
| 1454 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1455 | { |
| 1456 | struct hci_dev *hdev; |
| 1457 | struct mgmt_cp_set_io_capability *cp; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1458 | |
| 1459 | BT_DBG(""); |
| 1460 | |
| 1461 | cp = (void *) data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1462 | |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1463 | if (len != sizeof(*cp)) |
Szymon Janc | b8534e0f | 2011-03-01 16:55:34 +0100 | [diff] [blame] | 1464 | return cmd_status(sk, index, MGMT_OP_SET_IO_CAPABILITY, EINVAL); |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1465 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1466 | hdev = hci_dev_get(index); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1467 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1468 | return cmd_status(sk, index, MGMT_OP_SET_IO_CAPABILITY, ENODEV); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1469 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1470 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1471 | |
| 1472 | hdev->io_capability = cp->io_capability; |
| 1473 | |
| 1474 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Szymon Janc | b8534e0f | 2011-03-01 16:55:34 +0100 | [diff] [blame] | 1475 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1476 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1477 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1478 | hci_dev_put(hdev); |
| 1479 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1480 | return cmd_complete(sk, index, MGMT_OP_SET_IO_CAPABILITY, NULL, 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 1481 | } |
| 1482 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1483 | static inline struct pending_cmd *find_pairing(struct hci_conn *conn) |
| 1484 | { |
| 1485 | struct hci_dev *hdev = conn->hdev; |
| 1486 | struct list_head *p; |
| 1487 | |
| 1488 | list_for_each(p, &cmd_list) { |
| 1489 | struct pending_cmd *cmd; |
| 1490 | |
| 1491 | cmd = list_entry(p, struct pending_cmd, list); |
| 1492 | |
| 1493 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 1494 | continue; |
| 1495 | |
| 1496 | if (cmd->index != hdev->id) |
| 1497 | continue; |
| 1498 | |
| 1499 | if (cmd->user_data != conn) |
| 1500 | continue; |
| 1501 | |
| 1502 | return cmd; |
| 1503 | } |
| 1504 | |
| 1505 | return NULL; |
| 1506 | } |
| 1507 | |
| 1508 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 1509 | { |
| 1510 | struct mgmt_rp_pair_device rp; |
| 1511 | struct hci_conn *conn = cmd->user_data; |
| 1512 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1513 | BT_DBG(" %u", status); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1514 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1515 | bacpy(&rp.bdaddr, &conn->dst); |
| 1516 | rp.status = status; |
| 1517 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1518 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1519 | |
| 1520 | /* So we don't get further callbacks for this connection */ |
| 1521 | conn->connect_cfm_cb = NULL; |
| 1522 | conn->security_cfm_cb = NULL; |
| 1523 | conn->disconn_cfm_cb = NULL; |
| 1524 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1525 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 1529 | { |
| 1530 | struct pending_cmd *cmd; |
| 1531 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1532 | BT_DBG(" %u", status); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1533 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 1534 | cmd = find_pairing(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1535 | if (!cmd) { |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 1536 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1537 | return; |
| 1538 | } |
| 1539 | |
| 1540 | pairing_complete(cmd, status); |
Brian Gix | 80fb3a9 | 2012-01-31 13:15:20 -0800 | [diff] [blame] | 1541 | hci_conn_put(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1542 | } |
| 1543 | |
Brian Gix | 6d5fb8a | 2011-09-09 14:53:04 -0700 | [diff] [blame] | 1544 | static void pairing_security_complete_cb(struct hci_conn *conn, u8 status) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1545 | { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1546 | struct pending_cmd *cmd; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1547 | |
| 1548 | BT_DBG(" %u", status); |
| 1549 | |
| 1550 | cmd = find_pairing(conn); |
| 1551 | if (!cmd) { |
| 1552 | BT_DBG("Unable to find a pending command"); |
| 1553 | return; |
| 1554 | } |
| 1555 | |
| 1556 | if (conn->type == LE_LINK) |
| 1557 | smp_link_encrypt_cmplt(conn->l2cap_data, status, |
| 1558 | status ? 0 : 1); |
| 1559 | else |
| 1560 | pairing_complete(cmd, status); |
| 1561 | } |
| 1562 | |
Brian Gix | 6d5fb8a | 2011-09-09 14:53:04 -0700 | [diff] [blame] | 1563 | static void pairing_connect_complete_cb(struct hci_conn *conn, u8 status) |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1564 | { |
| 1565 | struct pending_cmd *cmd; |
| 1566 | |
| 1567 | BT_DBG("conn: %p %u", conn, status); |
| 1568 | |
| 1569 | cmd = find_pairing(conn); |
| 1570 | if (!cmd) { |
| 1571 | BT_DBG("Unable to find a pending command"); |
| 1572 | return; |
| 1573 | } |
Brian Gix | 114f3a6 | 2011-09-27 14:02:20 -0700 | [diff] [blame] | 1574 | |
| 1575 | if (status) |
| 1576 | pairing_complete(cmd, status); |
| 1577 | |
Brian Gix | 6d5fb8a | 2011-09-09 14:53:04 -0700 | [diff] [blame] | 1578 | hci_conn_put(conn); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | static void discovery_terminated(struct pending_cmd *cmd, void *data) |
| 1582 | { |
Brian Gix | 6e349d0 | 2011-11-28 14:51:14 -0800 | [diff] [blame] | 1583 | struct hci_dev *hdev; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1584 | struct mgmt_mode ev = {0}; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1585 | |
| 1586 | BT_DBG(""); |
Brian Gix | 6e349d0 | 2011-11-28 14:51:14 -0800 | [diff] [blame] | 1587 | hdev = hci_dev_get(cmd->index); |
| 1588 | if (!hdev) |
| 1589 | goto not_found; |
| 1590 | |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 1591 | del_timer(&hdev->disco_le_timer); |
| 1592 | del_timer(&hdev->disco_timer); |
Brian Gix | 6e349d0 | 2011-11-28 14:51:14 -0800 | [diff] [blame] | 1593 | hci_dev_put(hdev); |
| 1594 | |
| 1595 | not_found: |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1596 | mgmt_event(MGMT_EV_DISCOVERING, cmd->index, &ev, sizeof(ev), NULL); |
| 1597 | |
| 1598 | list_del(&cmd->list); |
| 1599 | |
| 1600 | mgmt_pending_free(cmd); |
| 1601 | } |
| 1602 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1603 | static int pair_device(struct sock *sk, u16 index, unsigned char *data, u16 len) |
| 1604 | { |
| 1605 | struct hci_dev *hdev; |
| 1606 | struct mgmt_cp_pair_device *cp; |
| 1607 | struct pending_cmd *cmd; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1608 | u8 sec_level, auth_type, io_cap; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1609 | struct hci_conn *conn; |
Brian Gix | fdd3892 | 2011-09-28 16:23:48 -0700 | [diff] [blame] | 1610 | struct adv_entry *entry; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1611 | int err; |
| 1612 | |
| 1613 | BT_DBG(""); |
| 1614 | |
Brian Gix | 64bd530 | 2011-09-08 11:35:48 -0700 | [diff] [blame] | 1615 | cp = (void *) data; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1616 | |
Brian Gix | 64bd530 | 2011-09-08 11:35:48 -0700 | [diff] [blame] | 1617 | if (len != sizeof(*cp)) |
| 1618 | return cmd_status(sk, index, MGMT_OP_PAIR_DEVICE, EINVAL); |
| 1619 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1620 | hdev = hci_dev_get(index); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1621 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1622 | if (!hdev) |
| 1623 | return cmd_status(sk, index, MGMT_OP_PAIR_DEVICE, ENODEV); |
| 1624 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1625 | hci_dev_lock_bh(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1626 | |
Prabhakaran Mc | 6001a71 | 2011-09-06 11:56:25 +0530 | [diff] [blame] | 1627 | io_cap = cp->io_cap; |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 1628 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 1629 | sec_level = BT_SECURITY_MEDIUM; |
Prabhakaran Mc | 76a8355 | 2012-04-09 14:43:18 +0530 | [diff] [blame] | 1630 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1631 | |
Brian Gix | fdd3892 | 2011-09-28 16:23:48 -0700 | [diff] [blame] | 1632 | entry = hci_find_adv_entry(hdev, &cp->bdaddr); |
| 1633 | if (entry && entry->flags & 0x04) { |
Brian Gix | a94b612 | 2012-02-23 16:07:10 -0800 | [diff] [blame] | 1634 | conn = hci_le_connect(hdev, 0, &cp->bdaddr, sec_level, |
| 1635 | auth_type, NULL); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1636 | } else { |
| 1637 | /* ACL-SSP does not support io_cap 0x04 (KeyboadDisplay) */ |
| 1638 | if (io_cap == 0x04) |
| 1639 | io_cap = 0x01; |
| 1640 | conn = hci_connect(hdev, ACL_LINK, 0, &cp->bdaddr, sec_level, |
| 1641 | auth_type); |
Prabhakaran Mc | 453651c | 2012-03-02 11:55:59 +0530 | [diff] [blame] | 1642 | conn->auth_initiator = 1; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1643 | } |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 1644 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1645 | if (IS_ERR(conn)) { |
| 1646 | err = PTR_ERR(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1647 | goto unlock; |
| 1648 | } |
| 1649 | |
| 1650 | if (conn->connect_cfm_cb) { |
| 1651 | hci_conn_put(conn); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1652 | err = cmd_status(sk, index, MGMT_OP_PAIR_DEVICE, EBUSY); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1653 | goto unlock; |
| 1654 | } |
| 1655 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1656 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, index, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1657 | if (!cmd) { |
| 1658 | err = -ENOMEM; |
| 1659 | hci_conn_put(conn); |
| 1660 | goto unlock; |
| 1661 | } |
| 1662 | |
Brian Gix | 6d5fb8a | 2011-09-09 14:53:04 -0700 | [diff] [blame] | 1663 | conn->connect_cfm_cb = pairing_connect_complete_cb; |
| 1664 | conn->security_cfm_cb = pairing_security_complete_cb; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1665 | conn->disconn_cfm_cb = pairing_complete_cb; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1666 | conn->io_capability = io_cap; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1667 | cmd->user_data = conn; |
| 1668 | |
| 1669 | if (conn->state == BT_CONNECTED && |
| 1670 | hci_conn_security(conn, sec_level, auth_type)) |
| 1671 | pairing_complete(cmd, 0); |
| 1672 | |
| 1673 | err = 0; |
| 1674 | |
| 1675 | unlock: |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1676 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 1677 | hci_dev_put(hdev); |
| 1678 | |
| 1679 | return err; |
| 1680 | } |
| 1681 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1682 | static int user_confirm_reply(struct sock *sk, u16 index, unsigned char *data, |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1683 | u16 len, u16 opcode) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 1684 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1685 | struct mgmt_cp_user_confirm_reply *cp = (void *) data; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1686 | u16 mgmt_op = opcode, hci_op; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 1687 | struct pending_cmd *cmd; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1688 | struct hci_dev *hdev; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1689 | struct hci_conn *le_conn; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 1690 | int err; |
| 1691 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1692 | BT_DBG("%d", mgmt_op); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 1693 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1694 | if (mgmt_op == MGMT_OP_USER_CONFIRM_NEG_REPLY) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1695 | hci_op = HCI_OP_USER_CONFIRM_NEG_REPLY; |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 1696 | else |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1697 | hci_op = HCI_OP_USER_CONFIRM_REPLY; |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 1698 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1699 | if (len < sizeof(*cp)) |
Szymon Janc | bdce7ba | 2011-02-25 19:05:49 +0100 | [diff] [blame] | 1700 | return cmd_status(sk, index, mgmt_op, EINVAL); |
| 1701 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1702 | hdev = hci_dev_get(index); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1703 | if (!hdev) |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1704 | return cmd_status(sk, index, mgmt_op, ENODEV); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1705 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1706 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 1707 | |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1708 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1709 | err = cmd_status(sk, index, mgmt_op, ENETDOWN); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 1710 | goto done; |
| 1711 | } |
| 1712 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1713 | le_conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->bdaddr); |
| 1714 | if (le_conn) { |
| 1715 | err = le_user_confirm_reply(le_conn, mgmt_op, (void *) cp); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 1716 | goto done; |
| 1717 | } |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1718 | BT_DBG("BR/EDR: %s", mgmt_op == MGMT_OP_USER_CONFIRM_NEG_REPLY ? |
| 1719 | "Reject" : "Accept"); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 1720 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 1721 | cmd = mgmt_pending_add(sk, mgmt_op, index, data, len); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1722 | if (!cmd) { |
| 1723 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 1724 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1725 | } |
| 1726 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1727 | err = hci_send_cmd(hdev, hci_op, sizeof(cp->bdaddr), &cp->bdaddr); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 1728 | if (err < 0) |
| 1729 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1730 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 1731 | done: |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1732 | hci_dev_unlock_bh(hdev); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1733 | hci_dev_put(hdev); |
| 1734 | |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 1735 | return err; |
| 1736 | } |
| 1737 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1738 | static int resolve_name(struct sock *sk, u16 index, unsigned char *data, |
| 1739 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 1740 | { |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1741 | struct mgmt_cp_resolve_name *mgmt_cp = (void *) data; |
| 1742 | struct hci_cp_remote_name_req hci_cp; |
| 1743 | struct hci_dev *hdev; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 1744 | struct pending_cmd *cmd; |
| 1745 | int err; |
| 1746 | |
| 1747 | BT_DBG(""); |
| 1748 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1749 | if (len != sizeof(*mgmt_cp)) |
| 1750 | return cmd_status(sk, index, MGMT_OP_RESOLVE_NAME, EINVAL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 1751 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1752 | hdev = hci_dev_get(index); |
| 1753 | if (!hdev) |
| 1754 | return cmd_status(sk, index, MGMT_OP_RESOLVE_NAME, ENODEV); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 1755 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1756 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 1757 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1758 | cmd = mgmt_pending_add(sk, MGMT_OP_RESOLVE_NAME, index, data, len); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 1759 | if (!cmd) { |
| 1760 | err = -ENOMEM; |
| 1761 | goto failed; |
| 1762 | } |
| 1763 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1764 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 1765 | bacpy(&hci_cp.bdaddr, &mgmt_cp->bdaddr); |
| 1766 | err = hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(hci_cp), |
| 1767 | &hci_cp); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 1768 | if (err < 0) |
| 1769 | mgmt_pending_remove(cmd); |
| 1770 | |
| 1771 | failed: |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1772 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1773 | hci_dev_put(hdev); |
| 1774 | |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 1775 | return err; |
| 1776 | } |
| 1777 | |
Brian Gix | 7f7e16c | 2011-11-01 16:27:25 -0700 | [diff] [blame] | 1778 | static int set_connection_params(struct sock *sk, u16 index, |
| 1779 | unsigned char *data, u16 len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 1780 | { |
Brian Gix | 7f7e16c | 2011-11-01 16:27:25 -0700 | [diff] [blame] | 1781 | struct mgmt_cp_set_connection_params *cp = (void *) data; |
| 1782 | struct hci_dev *hdev; |
| 1783 | struct hci_conn *conn; |
| 1784 | int err; |
| 1785 | |
| 1786 | BT_DBG(""); |
| 1787 | |
| 1788 | if (len != sizeof(*cp)) |
| 1789 | return cmd_status(sk, index, MGMT_OP_SET_CONNECTION_PARAMS, |
| 1790 | EINVAL); |
| 1791 | |
| 1792 | hdev = hci_dev_get(index); |
| 1793 | if (!hdev) |
| 1794 | return cmd_status(sk, index, MGMT_OP_SET_CONNECTION_PARAMS, |
| 1795 | ENODEV); |
| 1796 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1797 | hci_dev_lock_bh(hdev); |
Brian Gix | 7f7e16c | 2011-11-01 16:27:25 -0700 | [diff] [blame] | 1798 | |
| 1799 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->bdaddr); |
| 1800 | if (!conn) { |
| 1801 | err = cmd_status(sk, index, MGMT_OP_SET_CONNECTION_PARAMS, |
| 1802 | ENOTCONN); |
| 1803 | goto failed; |
| 1804 | } |
| 1805 | |
| 1806 | hci_le_conn_update(conn, le16_to_cpu(cp->interval_min), |
| 1807 | le16_to_cpu(cp->interval_max), |
| 1808 | le16_to_cpu(cp->slave_latency), |
| 1809 | le16_to_cpu(cp->timeout_multiplier)); |
| 1810 | |
| 1811 | err = cmd_status(sk, index, MGMT_OP_SET_CONNECTION_PARAMS, 0); |
| 1812 | |
| 1813 | failed: |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1814 | hci_dev_unlock_bh(hdev); |
Brian Gix | 7f7e16c | 2011-11-01 16:27:25 -0700 | [diff] [blame] | 1815 | hci_dev_put(hdev); |
| 1816 | |
| 1817 | return err; |
| 1818 | } |
| 1819 | |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 1820 | static int set_rssi_reporter(struct sock *sk, u16 index, |
| 1821 | unsigned char *data, u16 len) |
| 1822 | { |
| 1823 | struct mgmt_cp_set_rssi_reporter *cp = (void *) data; |
| 1824 | struct hci_dev *hdev; |
| 1825 | struct hci_conn *conn; |
| 1826 | int err = 0; |
| 1827 | |
| 1828 | if (len != sizeof(*cp)) |
| 1829 | return cmd_status(sk, index, MGMT_OP_SET_RSSI_REPORTER, |
| 1830 | EINVAL); |
| 1831 | |
| 1832 | hdev = hci_dev_get(index); |
| 1833 | if (!hdev) |
| 1834 | return cmd_status(sk, index, MGMT_OP_SET_RSSI_REPORTER, |
| 1835 | ENODEV); |
| 1836 | |
Archana Ramachandran | f32d982 | 2012-04-09 17:52:01 -0700 | [diff] [blame] | 1837 | hci_dev_lock_bh(hdev); |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 1838 | |
| 1839 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->bdaddr); |
| 1840 | |
| 1841 | if (!conn) { |
| 1842 | err = cmd_status(sk, index, MGMT_OP_SET_RSSI_REPORTER, |
| 1843 | ENOTCONN); |
| 1844 | goto failed; |
| 1845 | } |
| 1846 | |
| 1847 | BT_DBG("updateOnThreshExceed %d ", cp->updateOnThreshExceed); |
| 1848 | hci_conn_set_rssi_reporter(conn, cp->rssi_threshold, |
| 1849 | __le16_to_cpu(cp->interval), cp->updateOnThreshExceed); |
| 1850 | |
| 1851 | failed: |
Archana Ramachandran | f32d982 | 2012-04-09 17:52:01 -0700 | [diff] [blame] | 1852 | hci_dev_unlock_bh(hdev); |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 1853 | hci_dev_put(hdev); |
| 1854 | |
| 1855 | return err; |
| 1856 | } |
| 1857 | |
| 1858 | static int unset_rssi_reporter(struct sock *sk, u16 index, |
| 1859 | unsigned char *data, u16 len) |
| 1860 | { |
| 1861 | struct mgmt_cp_unset_rssi_reporter *cp = (void *) data; |
| 1862 | struct hci_dev *hdev; |
| 1863 | struct hci_conn *conn; |
| 1864 | int err = 0; |
| 1865 | |
| 1866 | if (len != sizeof(*cp)) |
| 1867 | return cmd_status(sk, index, MGMT_OP_UNSET_RSSI_REPORTER, |
| 1868 | EINVAL); |
| 1869 | |
| 1870 | hdev = hci_dev_get(index); |
| 1871 | |
| 1872 | if (!hdev) |
| 1873 | return cmd_status(sk, index, MGMT_OP_UNSET_RSSI_REPORTER, |
| 1874 | ENODEV); |
| 1875 | |
Archana Ramachandran | f32d982 | 2012-04-09 17:52:01 -0700 | [diff] [blame] | 1876 | hci_dev_lock_bh(hdev); |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 1877 | |
| 1878 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->bdaddr); |
| 1879 | |
| 1880 | if (!conn) { |
| 1881 | err = cmd_status(sk, index, MGMT_OP_UNSET_RSSI_REPORTER, |
| 1882 | ENOTCONN); |
| 1883 | goto failed; |
| 1884 | } |
| 1885 | |
| 1886 | hci_conn_unset_rssi_reporter(conn); |
| 1887 | |
| 1888 | failed: |
Archana Ramachandran | f32d982 | 2012-04-09 17:52:01 -0700 | [diff] [blame] | 1889 | hci_dev_unlock_bh(hdev); |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 1890 | hci_dev_put(hdev); |
| 1891 | |
| 1892 | return err; |
| 1893 | } |
| 1894 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1895 | static int set_local_name(struct sock *sk, u16 index, unsigned char *data, |
| 1896 | u16 len) |
| 1897 | { |
| 1898 | struct mgmt_cp_set_local_name *mgmt_cp = (void *) data; |
| 1899 | struct hci_cp_write_local_name hci_cp; |
| 1900 | struct hci_dev *hdev; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 1901 | struct pending_cmd *cmd; |
| 1902 | int err; |
| 1903 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1904 | BT_DBG(""); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 1905 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1906 | if (len != sizeof(*mgmt_cp)) |
| 1907 | return cmd_status(sk, index, MGMT_OP_SET_LOCAL_NAME, EINVAL); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 1908 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1909 | hdev = hci_dev_get(index); |
| 1910 | if (!hdev) |
| 1911 | return cmd_status(sk, index, MGMT_OP_SET_LOCAL_NAME, ENODEV); |
| 1912 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1913 | hci_dev_lock_bh(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1914 | |
| 1915 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, index, data, len); |
| 1916 | if (!cmd) { |
| 1917 | err = -ENOMEM; |
| 1918 | goto failed; |
| 1919 | } |
| 1920 | |
| 1921 | memcpy(hci_cp.name, mgmt_cp->name, sizeof(hci_cp.name)); |
| 1922 | err = hci_send_cmd(hdev, HCI_OP_WRITE_LOCAL_NAME, sizeof(hci_cp), |
| 1923 | &hci_cp); |
| 1924 | if (err < 0) |
| 1925 | mgmt_pending_remove(cmd); |
| 1926 | |
| 1927 | failed: |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 1928 | hci_dev_unlock_bh(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1929 | hci_dev_put(hdev); |
| 1930 | |
| 1931 | return err; |
| 1932 | } |
| 1933 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1934 | static void discovery_rsp(struct pending_cmd *cmd, void *data) |
| 1935 | { |
| 1936 | struct mgmt_mode ev; |
| 1937 | |
| 1938 | BT_DBG(""); |
| 1939 | if (cmd->opcode == MGMT_OP_START_DISCOVERY) { |
| 1940 | ev.val = 1; |
| 1941 | cmd_status(cmd->sk, cmd->index, MGMT_OP_START_DISCOVERY, 0); |
| 1942 | } else { |
| 1943 | ev.val = 0; |
| 1944 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_STOP_DISCOVERY, |
| 1945 | NULL, 0); |
| 1946 | if (cmd->opcode == MGMT_OP_STOP_DISCOVERY) { |
Brian Gix | 6e349d0 | 2011-11-28 14:51:14 -0800 | [diff] [blame] | 1947 | struct hci_dev *hdev = hci_dev_get(cmd->index); |
| 1948 | if (hdev) { |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 1949 | del_timer(&hdev->disco_le_timer); |
| 1950 | del_timer(&hdev->disco_timer); |
Brian Gix | 6e349d0 | 2011-11-28 14:51:14 -0800 | [diff] [blame] | 1951 | hci_dev_put(hdev); |
| 1952 | } |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1953 | } |
| 1954 | } |
| 1955 | |
| 1956 | mgmt_event(MGMT_EV_DISCOVERING, cmd->index, &ev, sizeof(ev), NULL); |
| 1957 | |
| 1958 | list_del(&cmd->list); |
| 1959 | |
| 1960 | mgmt_pending_free(cmd); |
| 1961 | } |
| 1962 | |
| 1963 | void mgmt_inquiry_started(u16 index) |
| 1964 | { |
| 1965 | BT_DBG(""); |
| 1966 | mgmt_pending_foreach(MGMT_OP_START_DISCOVERY, index, |
| 1967 | discovery_rsp, NULL); |
| 1968 | } |
| 1969 | |
| 1970 | void mgmt_inquiry_complete_evt(u16 index, u8 status) |
| 1971 | { |
| 1972 | struct hci_dev *hdev; |
| 1973 | struct hci_cp_le_set_scan_enable le_cp = {1, 0}; |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 1974 | struct mgmt_mode cp = {0}; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1975 | int err = -1; |
| 1976 | |
| 1977 | BT_DBG(""); |
| 1978 | |
| 1979 | hdev = hci_dev_get(index); |
Brian Gix | 64bd530 | 2011-09-08 11:35:48 -0700 | [diff] [blame] | 1980 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1981 | if (!hdev || !lmp_le_capable(hdev)) { |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1982 | |
| 1983 | mgmt_pending_foreach(MGMT_OP_STOP_DISCOVERY, index, |
| 1984 | discovery_terminated, NULL); |
| 1985 | |
| 1986 | mgmt_event(MGMT_EV_DISCOVERING, index, &cp, sizeof(cp), NULL); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1987 | |
Brian Gix | 64bd530 | 2011-09-08 11:35:48 -0700 | [diff] [blame] | 1988 | if (hdev) |
| 1989 | goto done; |
| 1990 | else |
| 1991 | return; |
| 1992 | } |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1993 | |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 1994 | if (hdev->disco_state != SCAN_IDLE) { |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1995 | err = hci_send_cmd(hdev, HCI_OP_LE_SET_SCAN_ENABLE, |
| 1996 | sizeof(le_cp), &le_cp); |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 1997 | if (err >= 0) { |
| 1998 | mod_timer(&hdev->disco_le_timer, jiffies + |
| 1999 | msecs_to_jiffies(hdev->disco_int_phase * 1000)); |
| 2000 | hdev->disco_state = SCAN_LE; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2001 | } else |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2002 | hdev->disco_state = SCAN_IDLE; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2003 | } |
| 2004 | |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2005 | if (hdev->disco_state == SCAN_IDLE) |
| 2006 | mgmt_event(MGMT_EV_DISCOVERING, index, &cp, sizeof(cp), NULL); |
| 2007 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2008 | if (err < 0) |
| 2009 | mgmt_pending_foreach(MGMT_OP_STOP_DISCOVERY, index, |
| 2010 | discovery_terminated, NULL); |
| 2011 | |
Brian Gix | 64bd530 | 2011-09-08 11:35:48 -0700 | [diff] [blame] | 2012 | done: |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2013 | hci_dev_put(hdev); |
| 2014 | } |
| 2015 | |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2016 | void mgmt_disco_timeout(unsigned long data) |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2017 | { |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2018 | struct hci_dev *hdev = (void *) data; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2019 | struct pending_cmd *cmd; |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2020 | struct mgmt_mode cp = {0}; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2021 | |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2022 | BT_DBG("hci%d", hdev->id); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2023 | |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2024 | hdev = hci_dev_get(hdev->id); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2025 | |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2026 | if (!hdev) |
| 2027 | return; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2028 | |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2029 | hci_dev_lock_bh(hdev); |
| 2030 | del_timer(&hdev->disco_le_timer); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2031 | |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2032 | if (hdev->disco_state != SCAN_IDLE) { |
| 2033 | struct hci_cp_le_set_scan_enable le_cp = {0, 0}; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2034 | |
Bhasker Neti | a6e6a4f | 2012-01-27 15:25:43 +0530 | [diff] [blame] | 2035 | if (test_bit(HCI_UP, &hdev->flags)) { |
| 2036 | if (hdev->disco_state == SCAN_LE) |
| 2037 | hci_send_cmd(hdev, HCI_OP_LE_SET_SCAN_ENABLE, |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2038 | sizeof(le_cp), &le_cp); |
Bhasker Neti | a6e6a4f | 2012-01-27 15:25:43 +0530 | [diff] [blame] | 2039 | else |
| 2040 | hci_send_cmd(hdev, HCI_OP_INQUIRY_CANCEL, 0, |
| 2041 | NULL); |
| 2042 | } |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2043 | hdev->disco_state = SCAN_IDLE; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2044 | } |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2045 | |
| 2046 | mgmt_event(MGMT_EV_DISCOVERING, hdev->id, &cp, sizeof(cp), NULL); |
| 2047 | |
| 2048 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev->id); |
| 2049 | if (cmd) |
| 2050 | mgmt_pending_remove(cmd); |
| 2051 | |
| 2052 | hci_dev_unlock_bh(hdev); |
| 2053 | hci_dev_put(hdev); |
| 2054 | } |
| 2055 | |
| 2056 | void mgmt_disco_le_timeout(unsigned long data) |
| 2057 | { |
| 2058 | struct hci_dev *hdev = (void *)data; |
| 2059 | struct hci_cp_le_set_scan_enable le_cp = {0, 0}; |
| 2060 | |
| 2061 | BT_DBG("hci%d", hdev->id); |
| 2062 | |
| 2063 | hdev = hci_dev_get(hdev->id); |
| 2064 | |
| 2065 | if (!hdev) |
| 2066 | return; |
| 2067 | |
| 2068 | hci_dev_lock_bh(hdev); |
| 2069 | |
Bhasker Neti | a6e6a4f | 2012-01-27 15:25:43 +0530 | [diff] [blame] | 2070 | if (test_bit(HCI_UP, &hdev->flags)) { |
| 2071 | if (hdev->disco_state == SCAN_LE) |
| 2072 | hci_send_cmd(hdev, HCI_OP_LE_SET_SCAN_ENABLE, |
| 2073 | sizeof(le_cp), &le_cp); |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2074 | |
| 2075 | /* re-start BR scan */ |
Bhasker Neti | a6e6a4f | 2012-01-27 15:25:43 +0530 | [diff] [blame] | 2076 | if (hdev->disco_state != SCAN_IDLE) { |
| 2077 | struct hci_cp_inquiry cp = {{0x33, 0x8b, 0x9e}, 4, 0}; |
| 2078 | hdev->disco_int_phase *= 2; |
| 2079 | hdev->disco_int_count = 0; |
| 2080 | cp.num_rsp = (u8) hdev->disco_int_phase; |
| 2081 | hci_send_cmd(hdev, HCI_OP_INQUIRY, sizeof(cp), &cp); |
| 2082 | hdev->disco_state = SCAN_BR; |
| 2083 | } |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2084 | } |
| 2085 | |
| 2086 | hci_dev_unlock_bh(hdev); |
| 2087 | hci_dev_put(hdev); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | static int start_discovery(struct sock *sk, u16 index) |
| 2091 | { |
| 2092 | struct hci_cp_inquiry cp = {{0x33, 0x8b, 0x9e}, 8, 0}; |
| 2093 | struct hci_dev *hdev; |
| 2094 | struct pending_cmd *cmd; |
| 2095 | int err; |
| 2096 | |
| 2097 | BT_DBG(""); |
| 2098 | |
| 2099 | hdev = hci_dev_get(index); |
| 2100 | if (!hdev) |
| 2101 | return cmd_status(sk, index, MGMT_OP_START_DISCOVERY, ENODEV); |
| 2102 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 2103 | hci_dev_lock_bh(hdev); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2104 | |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2105 | if (hdev->disco_state && timer_pending(&hdev->disco_timer)) { |
| 2106 | err = -EBUSY; |
| 2107 | goto failed; |
| 2108 | } |
| 2109 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2110 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, index, NULL, 0); |
| 2111 | if (!cmd) { |
| 2112 | err = -ENOMEM; |
| 2113 | goto failed; |
| 2114 | } |
| 2115 | |
| 2116 | /* If LE Capable, we will alternate between BR/EDR and LE */ |
| 2117 | if (lmp_le_capable(hdev)) { |
| 2118 | struct hci_cp_le_set_scan_parameters le_cp; |
| 2119 | |
| 2120 | /* Shorten BR scan params */ |
| 2121 | cp.num_rsp = 1; |
| 2122 | cp.length /= 2; |
| 2123 | |
| 2124 | /* Setup LE scan params */ |
| 2125 | memset(&le_cp, 0, sizeof(le_cp)); |
| 2126 | le_cp.type = 0x01; /* Active scanning */ |
| 2127 | /* The recommended value for scan interval and window is |
| 2128 | * 11.25 msec. It is calculated by: time = n * 0.625 msec */ |
| 2129 | le_cp.interval = cpu_to_le16(0x0012); |
| 2130 | le_cp.window = cpu_to_le16(0x0012); |
| 2131 | le_cp.own_bdaddr_type = 0; /* Public address */ |
| 2132 | le_cp.filter = 0; /* Accept all adv packets */ |
| 2133 | |
| 2134 | hci_send_cmd(hdev, HCI_OP_LE_SET_SCAN_PARAMETERS, |
| 2135 | sizeof(le_cp), &le_cp); |
| 2136 | } |
| 2137 | |
| 2138 | err = hci_send_cmd(hdev, HCI_OP_INQUIRY, sizeof(cp), &cp); |
| 2139 | |
Bhasker Neti | 0976074 | 2012-05-25 12:30:39 +0530 | [diff] [blame^] | 2140 | if (err < 0) { |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2141 | mgmt_pending_remove(cmd); |
Bhasker Neti | 0976074 | 2012-05-25 12:30:39 +0530 | [diff] [blame^] | 2142 | hdev->disco_state = SCAN_IDLE; |
| 2143 | } else if (lmp_le_capable(hdev)) { |
Brian Gix | 474e0f2 | 2012-01-14 20:21:55 -0800 | [diff] [blame] | 2144 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, index); |
| 2145 | if (!cmd) |
| 2146 | mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, index, |
| 2147 | NULL, 0); |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2148 | hdev->disco_int_phase = 1; |
| 2149 | hdev->disco_int_count = 0; |
| 2150 | hdev->disco_state = SCAN_BR; |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2151 | del_timer(&hdev->disco_le_timer); |
| 2152 | del_timer(&hdev->disco_timer); |
| 2153 | mod_timer(&hdev->disco_timer, |
| 2154 | jiffies + msecs_to_jiffies(20000)); |
Bhasker Neti | 0976074 | 2012-05-25 12:30:39 +0530 | [diff] [blame^] | 2155 | } else |
| 2156 | hdev->disco_state = SCAN_BR; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2157 | |
| 2158 | failed: |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 2159 | hci_dev_unlock_bh(hdev); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2160 | hci_dev_put(hdev); |
| 2161 | |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2162 | if (err < 0) |
| 2163 | return cmd_status(sk, index, MGMT_OP_START_DISCOVERY, -err); |
| 2164 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2165 | return err; |
| 2166 | } |
| 2167 | |
| 2168 | static int stop_discovery(struct sock *sk, u16 index) |
| 2169 | { |
| 2170 | struct hci_cp_le_set_scan_enable le_cp = {0, 0}; |
| 2171 | struct mgmt_mode mode_cp = {0}; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2172 | struct hci_dev *hdev; |
| 2173 | struct pending_cmd *cmd = NULL; |
| 2174 | int err = -EPERM; |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2175 | u8 state; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2176 | |
| 2177 | BT_DBG(""); |
| 2178 | |
| 2179 | hdev = hci_dev_get(index); |
| 2180 | if (!hdev) |
| 2181 | return cmd_status(sk, index, MGMT_OP_STOP_DISCOVERY, ENODEV); |
| 2182 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 2183 | hci_dev_lock_bh(hdev); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2184 | |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2185 | state = hdev->disco_state; |
| 2186 | hdev->disco_state = SCAN_IDLE; |
| 2187 | del_timer(&hdev->disco_le_timer); |
| 2188 | del_timer(&hdev->disco_timer); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2189 | |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2190 | if (state == SCAN_LE) { |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2191 | err = hci_send_cmd(hdev, HCI_OP_LE_SET_SCAN_ENABLE, |
| 2192 | sizeof(le_cp), &le_cp); |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2193 | if (err >= 0) { |
| 2194 | mgmt_pending_foreach(MGMT_OP_STOP_DISCOVERY, index, |
| 2195 | discovery_terminated, NULL); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2196 | |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2197 | err = cmd_complete(sk, index, MGMT_OP_STOP_DISCOVERY, |
| 2198 | NULL, 0); |
| 2199 | } |
Bhasker Neti | 0976074 | 2012-05-25 12:30:39 +0530 | [diff] [blame^] | 2200 | } else if (state == SCAN_BR) |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2201 | err = hci_send_cmd(hdev, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2202 | |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2203 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, index); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2204 | if (err < 0 && cmd) |
| 2205 | mgmt_pending_remove(cmd); |
| 2206 | |
| 2207 | mgmt_event(MGMT_EV_DISCOVERING, index, &mode_cp, sizeof(mode_cp), NULL); |
| 2208 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 2209 | hci_dev_unlock_bh(hdev); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2210 | hci_dev_put(hdev); |
| 2211 | |
| 2212 | if (err < 0) |
| 2213 | return cmd_status(sk, index, MGMT_OP_STOP_DISCOVERY, -err); |
| 2214 | else |
| 2215 | return err; |
| 2216 | } |
| 2217 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2218 | static int read_local_oob_data(struct sock *sk, u16 index) |
| 2219 | { |
| 2220 | struct hci_dev *hdev; |
| 2221 | struct pending_cmd *cmd; |
| 2222 | int err; |
| 2223 | |
| 2224 | BT_DBG("hci%u", index); |
| 2225 | |
| 2226 | hdev = hci_dev_get(index); |
| 2227 | if (!hdev) |
| 2228 | return cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 2229 | ENODEV); |
| 2230 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 2231 | hci_dev_lock_bh(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2232 | |
| 2233 | if (!test_bit(HCI_UP, &hdev->flags)) { |
| 2234 | err = cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 2235 | ENETDOWN); |
| 2236 | goto unlock; |
| 2237 | } |
| 2238 | |
| 2239 | if (!(hdev->features[6] & LMP_SIMPLE_PAIR)) { |
| 2240 | err = cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 2241 | EOPNOTSUPP); |
| 2242 | goto unlock; |
| 2243 | } |
| 2244 | |
| 2245 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, index)) { |
| 2246 | err = cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, EBUSY); |
| 2247 | goto unlock; |
| 2248 | } |
| 2249 | |
| 2250 | cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, index, NULL, 0); |
| 2251 | if (!cmd) { |
| 2252 | err = -ENOMEM; |
| 2253 | goto unlock; |
| 2254 | } |
| 2255 | |
| 2256 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 2257 | if (err < 0) |
| 2258 | mgmt_pending_remove(cmd); |
| 2259 | |
| 2260 | unlock: |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 2261 | hci_dev_unlock_bh(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2262 | hci_dev_put(hdev); |
| 2263 | |
| 2264 | return err; |
| 2265 | } |
| 2266 | |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2267 | static int add_remote_oob_data(struct sock *sk, u16 index, unsigned char *data, |
| 2268 | u16 len) |
| 2269 | { |
| 2270 | struct hci_dev *hdev; |
| 2271 | struct mgmt_cp_add_remote_oob_data *cp = (void *) data; |
| 2272 | int err; |
| 2273 | |
| 2274 | BT_DBG("hci%u ", index); |
| 2275 | |
| 2276 | if (len != sizeof(*cp)) |
| 2277 | return cmd_status(sk, index, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 2278 | EINVAL); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2279 | |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2280 | hdev = hci_dev_get(index); |
| 2281 | if (!hdev) |
| 2282 | return cmd_status(sk, index, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 2283 | ENODEV); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2284 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 2285 | hci_dev_lock_bh(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2286 | |
| 2287 | err = hci_add_remote_oob_data(hdev, &cp->bdaddr, cp->hash, |
| 2288 | cp->randomizer); |
| 2289 | if (err < 0) |
| 2290 | err = cmd_status(sk, index, MGMT_OP_ADD_REMOTE_OOB_DATA, -err); |
| 2291 | else |
| 2292 | err = cmd_complete(sk, index, MGMT_OP_ADD_REMOTE_OOB_DATA, NULL, |
| 2293 | 0); |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 2294 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 2295 | hci_dev_unlock_bh(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2296 | hci_dev_put(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2297 | |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2298 | return err; |
| 2299 | } |
| 2300 | |
| 2301 | static int remove_remote_oob_data(struct sock *sk, u16 index, |
| 2302 | unsigned char *data, u16 len) |
| 2303 | { |
| 2304 | struct hci_dev *hdev; |
| 2305 | struct mgmt_cp_remove_remote_oob_data *cp = (void *) data; |
| 2306 | int err; |
| 2307 | |
| 2308 | BT_DBG("hci%u ", index); |
| 2309 | |
| 2310 | if (len != sizeof(*cp)) |
| 2311 | return cmd_status(sk, index, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 2312 | EINVAL); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2313 | |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2314 | hdev = hci_dev_get(index); |
| 2315 | if (!hdev) |
| 2316 | return cmd_status(sk, index, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 2317 | ENODEV); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2318 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 2319 | hci_dev_lock_bh(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2320 | |
| 2321 | err = hci_remove_remote_oob_data(hdev, &cp->bdaddr); |
| 2322 | if (err < 0) |
| 2323 | err = cmd_status(sk, index, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 2324 | -err); |
| 2325 | else |
| 2326 | err = cmd_complete(sk, index, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 2327 | NULL, 0); |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 2328 | |
Subramanian Srinivasan | a727a49 | 2011-11-30 13:06:07 -0800 | [diff] [blame] | 2329 | hci_dev_unlock_bh(hdev); |
Antti Julku | 58d1980 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 2330 | hci_dev_put(hdev); |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 2331 | |
| 2332 | return err; |
| 2333 | } |
| 2334 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2335 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 2336 | { |
| 2337 | unsigned char *buf; |
| 2338 | struct mgmt_hdr *hdr; |
| 2339 | u16 opcode, index, len; |
| 2340 | int err; |
| 2341 | |
| 2342 | BT_DBG("got %zu bytes", msglen); |
| 2343 | |
| 2344 | if (msglen < sizeof(*hdr)) |
| 2345 | return -EINVAL; |
| 2346 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 2347 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2348 | if (!buf) |
| 2349 | return -ENOMEM; |
| 2350 | |
| 2351 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 2352 | err = -EFAULT; |
| 2353 | goto done; |
| 2354 | } |
| 2355 | |
| 2356 | hdr = (struct mgmt_hdr *) buf; |
| 2357 | opcode = get_unaligned_le16(&hdr->opcode); |
| 2358 | index = get_unaligned_le16(&hdr->index); |
| 2359 | len = get_unaligned_le16(&hdr->len); |
| 2360 | |
| 2361 | if (len != msglen - sizeof(*hdr)) { |
| 2362 | err = -EINVAL; |
| 2363 | goto done; |
| 2364 | } |
| 2365 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2366 | BT_DBG("got opcode %x", opcode); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2367 | switch (opcode) { |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 2368 | case MGMT_OP_READ_VERSION: |
| 2369 | err = read_version(sk); |
| 2370 | break; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 2371 | case MGMT_OP_READ_INDEX_LIST: |
| 2372 | err = read_index_list(sk); |
| 2373 | break; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 2374 | case MGMT_OP_READ_INFO: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2375 | err = read_controller_info(sk, index); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 2376 | break; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2377 | case MGMT_OP_SET_POWERED: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2378 | err = set_powered(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2379 | break; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2380 | case MGMT_OP_SET_DISCOVERABLE: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2381 | err = set_discoverable(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2382 | break; |
Brian Gix | 8a7f164 | 2011-10-17 17:39:46 -0700 | [diff] [blame] | 2383 | case MGMT_OP_SET_LIMIT_DISCOVERABLE: |
| 2384 | err = set_limited_discoverable(sk, index, buf + sizeof(*hdr), |
| 2385 | len); |
| 2386 | break; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2387 | case MGMT_OP_SET_CONNECTABLE: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2388 | err = set_connectable(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2389 | break; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 2390 | case MGMT_OP_SET_PAIRABLE: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2391 | err = set_pairable(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 2392 | break; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2393 | case MGMT_OP_ADD_UUID: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2394 | err = add_uuid(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2395 | break; |
| 2396 | case MGMT_OP_REMOVE_UUID: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2397 | err = remove_uuid(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2398 | break; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2399 | case MGMT_OP_SET_DEV_CLASS: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2400 | err = set_dev_class(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2401 | break; |
| 2402 | case MGMT_OP_SET_SERVICE_CACHE: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2403 | err = set_service_cache(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2404 | break; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2405 | case MGMT_OP_LOAD_KEYS: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2406 | err = load_keys(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2407 | break; |
| 2408 | case MGMT_OP_REMOVE_KEY: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2409 | err = remove_key(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2410 | break; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2411 | case MGMT_OP_DISCONNECT: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2412 | err = disconnect(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2413 | break; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2414 | case MGMT_OP_GET_CONNECTIONS: |
Szymon Janc | 8ce6284 | 2011-03-01 16:55:32 +0100 | [diff] [blame] | 2415 | err = get_connections(sk, index); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2416 | break; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2417 | case MGMT_OP_PIN_CODE_REPLY: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2418 | err = pin_code_reply(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2419 | break; |
| 2420 | case MGMT_OP_PIN_CODE_NEG_REPLY: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2421 | err = pin_code_neg_reply(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2422 | break; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2423 | case MGMT_OP_SET_IO_CAPABILITY: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2424 | err = set_io_capability(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2425 | break; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2426 | case MGMT_OP_PAIR_DEVICE: |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2427 | err = pair_device(sk, index, buf + sizeof(*hdr), len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2428 | break; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2429 | case MGMT_OP_USER_CONFIRM_REPLY: |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2430 | case MGMT_OP_USER_PASSKEY_REPLY: |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2431 | case MGMT_OP_USER_CONFIRM_NEG_REPLY: |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2432 | err = user_confirm_reply(sk, index, buf + sizeof(*hdr), |
| 2433 | len, opcode); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2434 | break; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2435 | case MGMT_OP_SET_LOCAL_NAME: |
| 2436 | err = set_local_name(sk, index, buf + sizeof(*hdr), len); |
| 2437 | break; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2438 | case MGMT_OP_START_DISCOVERY: |
| 2439 | err = start_discovery(sk, index); |
| 2440 | break; |
| 2441 | case MGMT_OP_STOP_DISCOVERY: |
| 2442 | err = stop_discovery(sk, index); |
| 2443 | break; |
| 2444 | case MGMT_OP_RESOLVE_NAME: |
| 2445 | err = resolve_name(sk, index, buf + sizeof(*hdr), len); |
| 2446 | break; |
Brian Gix | 7f7e16c | 2011-11-01 16:27:25 -0700 | [diff] [blame] | 2447 | case MGMT_OP_SET_CONNECTION_PARAMS: |
| 2448 | err = set_connection_params(sk, index, buf + sizeof(*hdr), len); |
| 2449 | break; |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 2450 | case MGMT_OP_SET_RSSI_REPORTER: |
| 2451 | err = set_rssi_reporter(sk, index, buf + sizeof(*hdr), len); |
| 2452 | break; |
| 2453 | case MGMT_OP_UNSET_RSSI_REPORTER: |
| 2454 | err = unset_rssi_reporter(sk, index, buf + sizeof(*hdr), len); |
| 2455 | break; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2456 | case MGMT_OP_READ_LOCAL_OOB_DATA: |
| 2457 | err = read_local_oob_data(sk, index); |
| 2458 | break; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 2459 | case MGMT_OP_ADD_REMOTE_OOB_DATA: |
| 2460 | err = add_remote_oob_data(sk, index, buf + sizeof(*hdr), len); |
| 2461 | break; |
| 2462 | case MGMT_OP_REMOVE_REMOTE_OOB_DATA: |
| 2463 | err = remove_remote_oob_data(sk, index, buf + sizeof(*hdr), |
| 2464 | len); |
| 2465 | break; |
Prabhakaran Mc | 46230fa | 2011-11-30 18:11:21 +0530 | [diff] [blame] | 2466 | case MGMT_OP_ENCRYPT_LINK: |
| 2467 | err = encrypt_link(sk, index, buf + sizeof(*hdr), len); |
| 2468 | break; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2469 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2470 | default: |
| 2471 | BT_DBG("Unknown op %u", opcode); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2472 | err = cmd_status(sk, index, opcode, 0x01); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2473 | break; |
| 2474 | } |
| 2475 | |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 2476 | if (err < 0) |
| 2477 | goto done; |
| 2478 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2479 | err = msglen; |
| 2480 | |
| 2481 | done: |
| 2482 | kfree(buf); |
| 2483 | return err; |
| 2484 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 2485 | |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 2486 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 2487 | { |
| 2488 | u8 *status = data; |
| 2489 | |
| 2490 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 2491 | mgmt_pending_remove(cmd); |
| 2492 | } |
| 2493 | |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 2494 | int mgmt_index_added(u16 index) |
| 2495 | { |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2496 | BT_DBG("%d", index); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2497 | return mgmt_event(MGMT_EV_INDEX_ADDED, index, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 2498 | } |
| 2499 | |
| 2500 | int mgmt_index_removed(u16 index) |
| 2501 | { |
Johan Hedberg | 931bc4e | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 2502 | u8 status = ENODEV; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 2503 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2504 | BT_DBG("%d", index); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 2505 | |
Johan Hedberg | 931bc4e | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 2506 | mgmt_pending_foreach(0, index, cmd_status_rsp, &status); |
| 2507 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2508 | return mgmt_event(MGMT_EV_INDEX_REMOVED, index, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2509 | } |
| 2510 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2511 | struct cmd_lookup { |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2512 | u8 val; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2513 | struct sock *sk; |
| 2514 | }; |
| 2515 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2516 | static void mode_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2517 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 2518 | struct mgmt_mode *cp = cmd->param; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2519 | struct cmd_lookup *match = data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2520 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2521 | if (cp->val != match->val) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2522 | return; |
| 2523 | |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 2524 | send_mode_rsp(cmd->sk, cmd->opcode, cmd->index, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2525 | |
| 2526 | list_del(&cmd->list); |
| 2527 | |
| 2528 | if (match->sk == NULL) { |
| 2529 | match->sk = cmd->sk; |
| 2530 | sock_hold(match->sk); |
| 2531 | } |
| 2532 | |
| 2533 | mgmt_pending_free(cmd); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 2534 | } |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 2535 | |
| 2536 | int mgmt_powered(u16 index, u8 powered) |
| 2537 | { |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2538 | struct mgmt_mode ev; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2539 | struct cmd_lookup match = { powered, NULL }; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2540 | int ret; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 2541 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2542 | BT_DBG("hci%u %d", index, powered); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 2543 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2544 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, index, mode_rsp, &match); |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 2545 | |
Johan Hedberg | 931bc4e | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 2546 | if (!powered) { |
| 2547 | u8 status = ENETDOWN; |
| 2548 | mgmt_pending_foreach(0, index, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 2549 | } |
| 2550 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2551 | ev.val = powered; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2552 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2553 | ret = mgmt_event(MGMT_EV_POWERED, index, &ev, sizeof(ev), match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2554 | |
| 2555 | if (match.sk) |
| 2556 | sock_put(match.sk); |
| 2557 | |
| 2558 | return ret; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 2559 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2560 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2561 | int mgmt_discoverable(u16 index, u8 discoverable) |
| 2562 | { |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2563 | struct mgmt_mode ev; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2564 | struct cmd_lookup match = { discoverable, NULL }; |
| 2565 | int ret; |
| 2566 | |
Szymon Janc | b8534e0f | 2011-03-01 16:55:34 +0100 | [diff] [blame] | 2567 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, index, mode_rsp, &match); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2568 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2569 | ev.val = discoverable; |
Johan Hedberg | ed9b5f2 | 2012-02-21 20:47:06 +0200 | [diff] [blame] | 2570 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2571 | ret = mgmt_event(MGMT_EV_DISCOVERABLE, index, &ev, sizeof(ev), |
| 2572 | match.sk); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 2573 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2574 | if (match.sk) |
| 2575 | sock_put(match.sk); |
| 2576 | |
| 2577 | return ret; |
| 2578 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2579 | |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2580 | int mgmt_connectable(u16 index, u8 connectable) |
| 2581 | { |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2582 | struct mgmt_mode ev; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2583 | struct cmd_lookup match = { connectable, NULL }; |
| 2584 | int ret; |
| 2585 | |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2586 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, index, mode_rsp, &match); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2587 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 2588 | ev.val = connectable; |
Johan Hedberg | ed9b5f2 | 2012-02-21 20:47:06 +0200 | [diff] [blame] | 2589 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2590 | ret = mgmt_event(MGMT_EV_CONNECTABLE, index, &ev, sizeof(ev), match.sk); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 2591 | |
| 2592 | if (match.sk) |
| 2593 | sock_put(match.sk); |
| 2594 | |
| 2595 | return ret; |
| 2596 | } |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2597 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2598 | int mgmt_new_key(u16 index, struct link_key *key, u8 bonded) |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2599 | { |
Vinicius Costa Gomes | c00ddbf | 2011-07-08 18:31:44 -0300 | [diff] [blame] | 2600 | struct mgmt_ev_new_key *ev; |
| 2601 | int err, total; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2602 | |
Vinicius Costa Gomes | c00ddbf | 2011-07-08 18:31:44 -0300 | [diff] [blame] | 2603 | total = sizeof(struct mgmt_ev_new_key) + key->dlen; |
| 2604 | ev = kzalloc(total, GFP_ATOMIC); |
| 2605 | if (!ev) |
| 2606 | return -ENOMEM; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2607 | |
Vinicius Costa Gomes | c00ddbf | 2011-07-08 18:31:44 -0300 | [diff] [blame] | 2608 | bacpy(&ev->key.bdaddr, &key->bdaddr); |
Brian Gix | cf95677 | 2011-10-20 15:18:51 -0700 | [diff] [blame] | 2609 | ev->key.addr_type = key->addr_type; |
| 2610 | ev->key.key_type = key->key_type; |
Vinicius Costa Gomes | c00ddbf | 2011-07-08 18:31:44 -0300 | [diff] [blame] | 2611 | memcpy(ev->key.val, key->val, 16); |
| 2612 | ev->key.pin_len = key->pin_len; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2613 | ev->key.auth = key->auth; |
| 2614 | ev->store_hint = bonded; |
Vinicius Costa Gomes | c00ddbf | 2011-07-08 18:31:44 -0300 | [diff] [blame] | 2615 | ev->key.dlen = key->dlen; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2616 | |
Vinicius Costa Gomes | c00ddbf | 2011-07-08 18:31:44 -0300 | [diff] [blame] | 2617 | memcpy(ev->key.data, key->data, key->dlen); |
| 2618 | |
| 2619 | err = mgmt_event(MGMT_EV_NEW_KEY, index, ev, total, NULL); |
| 2620 | |
| 2621 | kfree(ev); |
| 2622 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 2623 | return err; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2624 | } |
| 2625 | |
Brian Gix | 2e2f50d | 2011-09-13 12:36:04 -0700 | [diff] [blame] | 2626 | int mgmt_connected(u16 index, bdaddr_t *bdaddr, u8 le) |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 2627 | { |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2628 | struct mgmt_ev_connected ev; |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 2629 | |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2630 | bacpy(&ev.bdaddr, bdaddr); |
Brian Gix | 2e2f50d | 2011-09-13 12:36:04 -0700 | [diff] [blame] | 2631 | ev.le = le; |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 2632 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2633 | return mgmt_event(MGMT_EV_CONNECTED, index, &ev, sizeof(ev), NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2634 | } |
| 2635 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2636 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 2637 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 2638 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2639 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2640 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2641 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2642 | bacpy(&rp.bdaddr, &cp->bdaddr); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2643 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2644 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2645 | |
| 2646 | *sk = cmd->sk; |
| 2647 | sock_hold(*sk); |
| 2648 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2649 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2650 | } |
| 2651 | |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2652 | int mgmt_disconnected(u16 index, bdaddr_t *bdaddr) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2653 | { |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2654 | struct mgmt_ev_disconnected ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2655 | struct sock *sk = NULL; |
| 2656 | int err; |
| 2657 | |
| 2658 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, index, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2659 | |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2660 | bacpy(&ev.bdaddr, bdaddr); |
| 2661 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2662 | err = mgmt_event(MGMT_EV_DISCONNECTED, index, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2663 | |
| 2664 | if (sk) |
| 2665 | sock_put(sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2666 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2667 | return err; |
| 2668 | } |
| 2669 | |
| 2670 | int mgmt_disconnect_failed(u16 index) |
| 2671 | { |
| 2672 | struct pending_cmd *cmd; |
| 2673 | int err; |
| 2674 | |
| 2675 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, index); |
| 2676 | if (!cmd) |
| 2677 | return -ENOENT; |
| 2678 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2679 | err = cmd_status(cmd->sk, index, MGMT_OP_DISCONNECT, EIO); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2680 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2681 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2682 | |
| 2683 | return err; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 2684 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 2685 | |
| 2686 | int mgmt_connect_failed(u16 index, bdaddr_t *bdaddr, u8 status) |
| 2687 | { |
| 2688 | struct mgmt_ev_connect_failed ev; |
| 2689 | |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 2690 | bacpy(&ev.bdaddr, bdaddr); |
| 2691 | ev.status = status; |
| 2692 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2693 | return mgmt_event(MGMT_EV_CONNECT_FAILED, index, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 2694 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2695 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2696 | int mgmt_pin_code_request(u16 index, bdaddr_t *bdaddr) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2697 | { |
| 2698 | struct mgmt_ev_pin_code_request ev; |
| 2699 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2700 | BT_DBG("hci%u", index); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2701 | |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2702 | bacpy(&ev.bdaddr, bdaddr); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2703 | ev.secure = 0; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2704 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2705 | return mgmt_event(MGMT_EV_PIN_CODE_REQUEST, index, &ev, sizeof(ev), |
| 2706 | NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2707 | } |
| 2708 | |
| 2709 | int mgmt_pin_code_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status) |
| 2710 | { |
| 2711 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 2712 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2713 | int err; |
| 2714 | |
| 2715 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, index); |
| 2716 | if (!cmd) |
| 2717 | return -ENOENT; |
| 2718 | |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 2719 | bacpy(&rp.bdaddr, bdaddr); |
| 2720 | rp.status = status; |
| 2721 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2722 | err = cmd_complete(cmd->sk, index, MGMT_OP_PIN_CODE_REPLY, &rp, |
| 2723 | sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2724 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2725 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2726 | |
| 2727 | return err; |
| 2728 | } |
| 2729 | |
| 2730 | int mgmt_pin_code_neg_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status) |
| 2731 | { |
| 2732 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 2733 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2734 | int err; |
| 2735 | |
| 2736 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, index); |
| 2737 | if (!cmd) |
| 2738 | return -ENOENT; |
| 2739 | |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 2740 | bacpy(&rp.bdaddr, bdaddr); |
| 2741 | rp.status = status; |
| 2742 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2743 | err = cmd_complete(cmd->sk, index, MGMT_OP_PIN_CODE_NEG_REPLY, &rp, |
| 2744 | sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2745 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2746 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2747 | |
| 2748 | return err; |
| 2749 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2750 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2751 | int mgmt_user_confirm_request(u16 index, u8 event, |
| 2752 | bdaddr_t *bdaddr, __le32 value) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2753 | { |
| 2754 | struct mgmt_ev_user_confirm_request ev; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2755 | struct hci_conn *conn = NULL; |
| 2756 | struct hci_dev *hdev; |
| 2757 | u8 loc_cap, rem_cap, loc_mitm, rem_mitm; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2758 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2759 | BT_DBG("hci%u", index); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2760 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2761 | hdev = hci_dev_get(index); |
| 2762 | |
Brian Gix | 64bd530 | 2011-09-08 11:35:48 -0700 | [diff] [blame] | 2763 | if (!hdev) |
| 2764 | return -ENODEV; |
| 2765 | |
Brian Gix | 64bd530 | 2011-09-08 11:35:48 -0700 | [diff] [blame] | 2766 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, bdaddr); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2767 | |
| 2768 | ev.auto_confirm = 0; |
| 2769 | |
| 2770 | if (!conn || event != HCI_EV_USER_CONFIRM_REQUEST) |
| 2771 | goto no_auto_confirm; |
| 2772 | |
| 2773 | loc_cap = (conn->io_capability == 0x04) ? 0x01 : conn->io_capability; |
| 2774 | rem_cap = conn->remote_cap; |
| 2775 | loc_mitm = conn->auth_type & 0x01; |
| 2776 | rem_mitm = conn->remote_auth & 0x01; |
| 2777 | |
Brian Gix | dbf5929 | 2011-11-11 15:45:17 -0800 | [diff] [blame] | 2778 | if ((conn->auth_type & HCI_AT_DEDICATED_BONDING) && |
| 2779 | conn->auth_initiator && rem_cap == 0x03) |
| 2780 | ev.auto_confirm = 1; |
AnubhavGupta | 07c2a0f | 2012-02-16 19:03:59 +0530 | [diff] [blame] | 2781 | else if (loc_cap == 0x01 && (rem_cap == 0x00 || rem_cap == 0x03)) { |
| 2782 | if (!loc_mitm && !rem_mitm) |
| 2783 | value = 0; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2784 | goto no_auto_confirm; |
AnubhavGupta | 07c2a0f | 2012-02-16 19:03:59 +0530 | [diff] [blame] | 2785 | } |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2786 | |
| 2787 | |
| 2788 | if ((!loc_mitm || rem_cap == 0x03) && (!rem_mitm || loc_cap == 0x03)) |
| 2789 | ev.auto_confirm = 1; |
| 2790 | |
| 2791 | no_auto_confirm: |
| 2792 | bacpy(&ev.bdaddr, bdaddr); |
| 2793 | ev.event = event; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2794 | put_unaligned_le32(value, &ev.value); |
| 2795 | |
Brian Gix | 64bd530 | 2011-09-08 11:35:48 -0700 | [diff] [blame] | 2796 | hci_dev_put(hdev); |
| 2797 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2798 | return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, index, &ev, sizeof(ev), |
| 2799 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2800 | } |
| 2801 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2802 | int mgmt_user_passkey_request(u16 index, bdaddr_t *bdaddr) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2803 | { |
| 2804 | struct mgmt_ev_user_passkey_request ev; |
| 2805 | |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2806 | BT_DBG("hci%u", index); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2807 | |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2808 | bacpy(&ev.bdaddr, bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2809 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2810 | return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, index, &ev, sizeof(ev), |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2811 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2812 | } |
| 2813 | |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2814 | static int confirm_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status, |
| 2815 | u8 opcode) |
| 2816 | { |
| 2817 | struct pending_cmd *cmd; |
| 2818 | struct mgmt_rp_user_confirm_reply rp; |
| 2819 | int err; |
| 2820 | |
| 2821 | cmd = mgmt_pending_find(opcode, index); |
| 2822 | if (!cmd) |
| 2823 | return -ENOENT; |
| 2824 | |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2825 | bacpy(&rp.bdaddr, bdaddr); |
| 2826 | rp.status = status; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2827 | err = cmd_complete(cmd->sk, index, opcode, &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2828 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2829 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2830 | |
| 2831 | return err; |
| 2832 | } |
| 2833 | |
| 2834 | int mgmt_user_confirm_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status) |
| 2835 | { |
| 2836 | return confirm_reply_complete(index, bdaddr, status, |
| 2837 | MGMT_OP_USER_CONFIRM_REPLY); |
| 2838 | } |
| 2839 | |
Szymon Janc | b8534e0f | 2011-03-01 16:55:34 +0100 | [diff] [blame] | 2840 | int mgmt_user_confirm_neg_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2841 | { |
| 2842 | return confirm_reply_complete(index, bdaddr, status, |
| 2843 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
| 2844 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 2845 | |
| 2846 | int mgmt_auth_failed(u16 index, bdaddr_t *bdaddr, u8 status) |
| 2847 | { |
| 2848 | struct mgmt_ev_auth_failed ev; |
| 2849 | |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 2850 | bacpy(&ev.bdaddr, bdaddr); |
| 2851 | ev.status = status; |
| 2852 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2853 | return mgmt_event(MGMT_EV_AUTH_FAILED, index, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 2854 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2855 | |
| 2856 | int mgmt_set_local_name_complete(u16 index, u8 *name, u8 status) |
| 2857 | { |
| 2858 | struct pending_cmd *cmd; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2859 | struct hci_dev *hdev; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2860 | struct mgmt_cp_set_local_name ev; |
| 2861 | int err; |
| 2862 | |
| 2863 | memset(&ev, 0, sizeof(ev)); |
| 2864 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
| 2865 | |
| 2866 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, index); |
| 2867 | if (!cmd) |
| 2868 | goto send_event; |
| 2869 | |
| 2870 | if (status) { |
| 2871 | err = cmd_status(cmd->sk, index, MGMT_OP_SET_LOCAL_NAME, EIO); |
| 2872 | goto failed; |
| 2873 | } |
| 2874 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2875 | hdev = hci_dev_get(index); |
| 2876 | if (hdev) { |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2877 | update_eir(hdev); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2878 | hci_dev_put(hdev); |
| 2879 | } |
| 2880 | |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 2881 | err = cmd_complete(cmd->sk, index, MGMT_OP_SET_LOCAL_NAME, &ev, |
| 2882 | sizeof(ev)); |
| 2883 | if (err < 0) |
| 2884 | goto failed; |
| 2885 | |
| 2886 | send_event: |
| 2887 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, index, &ev, sizeof(ev), |
| 2888 | cmd ? cmd->sk : NULL); |
| 2889 | |
| 2890 | failed: |
| 2891 | if (cmd) |
| 2892 | mgmt_pending_remove(cmd); |
| 2893 | return err; |
| 2894 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 2895 | |
| 2896 | int mgmt_read_local_oob_data_reply_complete(u16 index, u8 *hash, u8 *randomizer, |
| 2897 | u8 status) |
| 2898 | { |
| 2899 | struct pending_cmd *cmd; |
| 2900 | int err; |
| 2901 | |
| 2902 | BT_DBG("hci%u status %u", index, status); |
| 2903 | |
| 2904 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, index); |
| 2905 | if (!cmd) |
| 2906 | return -ENOENT; |
| 2907 | |
| 2908 | if (status) { |
| 2909 | err = cmd_status(cmd->sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 2910 | EIO); |
| 2911 | } else { |
| 2912 | struct mgmt_rp_read_local_oob_data rp; |
| 2913 | |
| 2914 | memcpy(rp.hash, hash, sizeof(rp.hash)); |
| 2915 | memcpy(rp.randomizer, randomizer, sizeof(rp.randomizer)); |
| 2916 | |
| 2917 | err = cmd_complete(cmd->sk, index, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 2918 | &rp, sizeof(rp)); |
| 2919 | } |
| 2920 | |
| 2921 | mgmt_pending_remove(cmd); |
| 2922 | |
| 2923 | return err; |
| 2924 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 2925 | |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 2926 | void mgmt_read_rssi_complete(u16 index, s8 rssi, bdaddr_t *bdaddr, |
| 2927 | u16 handle, u8 status) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2928 | { |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 2929 | struct mgmt_ev_rssi_update ev; |
| 2930 | struct hci_conn *conn; |
| 2931 | struct hci_dev *hdev; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2932 | |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 2933 | if (status) |
| 2934 | return; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2935 | |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 2936 | hdev = hci_dev_get(index); |
| 2937 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2938 | |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 2939 | if (!conn) |
| 2940 | return; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2941 | |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 2942 | BT_DBG("rssi_update_thresh_exceed : %d ", |
| 2943 | conn->rssi_update_thresh_exceed); |
| 2944 | BT_DBG("RSSI Threshold : %d , recvd RSSI : %d ", |
| 2945 | conn->rssi_threshold, rssi); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2946 | |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 2947 | if (conn->rssi_update_thresh_exceed == 1) { |
| 2948 | BT_DBG("rssi_update_thresh_exceed == 1"); |
Archana Ramachandran | 5d332a4 | 2012-03-22 15:35:34 -0700 | [diff] [blame] | 2949 | if (rssi > conn->rssi_threshold) { |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 2950 | memset(&ev, 0, sizeof(ev)); |
| 2951 | bacpy(&ev.bdaddr, bdaddr); |
| 2952 | ev.rssi = rssi; |
| 2953 | mgmt_event(MGMT_EV_RSSI_UPDATE, index, &ev, |
| 2954 | sizeof(ev), NULL); |
| 2955 | } else { |
| 2956 | hci_conn_set_rssi_reporter(conn, conn->rssi_threshold, |
| 2957 | conn->rssi_update_interval, |
| 2958 | conn->rssi_update_thresh_exceed); |
| 2959 | } |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2960 | } else { |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 2961 | BT_DBG("rssi_update_thresh_exceed == 0"); |
Archana Ramachandran | 5d332a4 | 2012-03-22 15:35:34 -0700 | [diff] [blame] | 2962 | if (rssi < conn->rssi_threshold) { |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 2963 | memset(&ev, 0, sizeof(ev)); |
| 2964 | bacpy(&ev.bdaddr, bdaddr); |
| 2965 | ev.rssi = rssi; |
| 2966 | mgmt_event(MGMT_EV_RSSI_UPDATE, index, &ev, |
| 2967 | sizeof(ev), NULL); |
| 2968 | } else { |
| 2969 | hci_conn_set_rssi_reporter(conn, conn->rssi_threshold, |
| 2970 | conn->rssi_update_interval, |
| 2971 | conn->rssi_update_thresh_exceed); |
| 2972 | } |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2973 | } |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2974 | } |
| 2975 | |
Archana Ramachandran | 26a752b | 2011-12-20 11:27:40 -0800 | [diff] [blame] | 2976 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2977 | int mgmt_device_found(u16 index, bdaddr_t *bdaddr, u8 type, u8 le, |
| 2978 | u8 *dev_class, s8 rssi, u8 eir_len, u8 *eir) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 2979 | { |
| 2980 | struct mgmt_ev_device_found ev; |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 2981 | struct hci_dev *hdev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 2982 | int err; |
| 2983 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2984 | BT_DBG("le: %d", le); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 2985 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 2986 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 2987 | |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 2988 | bacpy(&ev.bdaddr, bdaddr); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 2989 | ev.rssi = rssi; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2990 | ev.type = type; |
| 2991 | ev.le = le; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 2992 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2993 | if (dev_class) |
| 2994 | memcpy(ev.dev_class, dev_class, sizeof(ev.dev_class)); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 2995 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 2996 | if (eir && eir_len) |
| 2997 | memcpy(ev.eir, eir, eir_len); |
| 2998 | |
| 2999 | err = mgmt_event(MGMT_EV_DEVICE_FOUND, index, &ev, sizeof(ev), NULL); |
| 3000 | |
| 3001 | if (err < 0) |
| 3002 | return err; |
| 3003 | |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 3004 | hdev = hci_dev_get(index); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 3005 | |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 3006 | if (!hdev) |
| 3007 | return 0; |
Brian Gix | 64bd530 | 2011-09-08 11:35:48 -0700 | [diff] [blame] | 3008 | |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 3009 | if (hdev->disco_state == SCAN_IDLE) |
| 3010 | goto done; |
| 3011 | |
| 3012 | hdev->disco_int_count++; |
| 3013 | |
| 3014 | if (hdev->disco_int_count >= hdev->disco_int_phase) { |
| 3015 | /* Inquiry scan for General Discovery LAP */ |
| 3016 | struct hci_cp_inquiry cp = {{0x33, 0x8b, 0x9e}, 4, 0}; |
| 3017 | struct hci_cp_le_set_scan_enable le_cp = {0, 0}; |
| 3018 | |
| 3019 | hdev->disco_int_phase *= 2; |
| 3020 | hdev->disco_int_count = 0; |
| 3021 | if (hdev->disco_state == SCAN_LE) { |
| 3022 | /* cancel LE scan */ |
| 3023 | hci_send_cmd(hdev, HCI_OP_LE_SET_SCAN_ENABLE, |
| 3024 | sizeof(le_cp), &le_cp); |
| 3025 | /* start BR scan */ |
| 3026 | cp.num_rsp = (u8) hdev->disco_int_phase; |
| 3027 | hci_send_cmd(hdev, HCI_OP_INQUIRY, |
| 3028 | sizeof(cp), &cp); |
| 3029 | hdev->disco_state = SCAN_BR; |
| 3030 | del_timer_sync(&hdev->disco_le_timer); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 3031 | } |
| 3032 | } |
| 3033 | |
Brian Gix | 568dde9 | 2012-01-11 16:18:04 -0800 | [diff] [blame] | 3034 | done: |
| 3035 | hci_dev_put(hdev); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 3036 | return 0; |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 3037 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3038 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 3039 | |
| 3040 | int mgmt_remote_name(u16 index, bdaddr_t *bdaddr, u8 status, u8 *name) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3041 | { |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 3042 | struct mgmt_ev_remote_name ev; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3043 | |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 3044 | memset(&ev, 0, sizeof(ev)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3045 | |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 3046 | bacpy(&ev.bdaddr, bdaddr); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 3047 | ev.status = status; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 3048 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3049 | |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 3050 | return mgmt_event(MGMT_EV_REMOTE_NAME, index, &ev, sizeof(ev), NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3051 | } |
| 3052 | |
Prabhakaran Mc | 46230fa | 2011-11-30 18:11:21 +0530 | [diff] [blame] | 3053 | int mgmt_encrypt_change(u16 index, bdaddr_t *bdaddr, u8 status) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3054 | { |
Prabhakaran Mc | 46230fa | 2011-11-30 18:11:21 +0530 | [diff] [blame] | 3055 | struct mgmt_ev_encrypt_change ev; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3056 | |
Prabhakaran Mc | 46230fa | 2011-11-30 18:11:21 +0530 | [diff] [blame] | 3057 | BT_DBG("hci%u", index); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3058 | |
Prabhakaran Mc | 46230fa | 2011-11-30 18:11:21 +0530 | [diff] [blame] | 3059 | bacpy(&ev.bdaddr, bdaddr); |
| 3060 | ev.status = status; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3061 | |
Prabhakaran Mc | 46230fa | 2011-11-30 18:11:21 +0530 | [diff] [blame] | 3062 | return mgmt_event(MGMT_EV_ENCRYPT_CHANGE, index, &ev, sizeof(ev), |
| 3063 | NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3064 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 3065 | |
Srinivas Krovvidi | 0916aed | 2011-12-20 12:06:34 +0530 | [diff] [blame] | 3066 | int mgmt_remote_class(u16 index, bdaddr_t *bdaddr, u8 dev_class[3]) |
| 3067 | { |
| 3068 | struct mgmt_ev_remote_class ev; |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 3069 | |
Srinivas Krovvidi | 0916aed | 2011-12-20 12:06:34 +0530 | [diff] [blame] | 3070 | memset(&ev, 0, sizeof(ev)); |
| 3071 | |
| 3072 | bacpy(&ev.bdaddr, bdaddr); |
| 3073 | memcpy(ev.dev_class, dev_class, 3); |
| 3074 | |
| 3075 | return mgmt_event(MGMT_EV_REMOTE_CLASS, index, &ev, sizeof(ev), NULL); |
| 3076 | } |
Srinivas Krovvidi | d352b26 | 2012-01-12 19:46:26 +0530 | [diff] [blame] | 3077 | |
| 3078 | int mgmt_remote_version(u16 index, bdaddr_t *bdaddr, u8 ver, u16 mnf, |
| 3079 | u16 sub_ver) |
| 3080 | { |
| 3081 | struct mgmt_ev_remote_version ev; |
| 3082 | |
| 3083 | memset(&ev, 0, sizeof(ev)); |
| 3084 | |
| 3085 | bacpy(&ev.bdaddr, bdaddr); |
| 3086 | ev.lmp_ver = ver; |
| 3087 | ev.manufacturer = mnf; |
| 3088 | ev.lmp_subver = sub_ver; |
| 3089 | |
| 3090 | return mgmt_event(MGMT_EV_REMOTE_VERSION, index, &ev, sizeof(ev), NULL); |
| 3091 | } |
Sunny Kapdi | f3caf88 | 2012-02-25 19:27:09 -0800 | [diff] [blame] | 3092 | |
| 3093 | int mgmt_remote_features(u16 index, bdaddr_t *bdaddr, u8 features[8]) |
| 3094 | { |
| 3095 | struct mgmt_ev_remote_features ev; |
| 3096 | |
| 3097 | memset(&ev, 0, sizeof(ev)); |
| 3098 | |
| 3099 | bacpy(&ev.bdaddr, bdaddr); |
| 3100 | memcpy(ev.features, features, sizeof(ev.features)); |
| 3101 | |
| 3102 | return mgmt_event(MGMT_EV_REMOTE_FEATURES, index, &ev, sizeof(ev), |
| 3103 | NULL); |
| 3104 | } |